Welcome to mirror list, hosted at ThFree Co, Russian Federation.

OperationRepairLayers.cs « Operations « UVtools.Core - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a3ac3ea8cc2f850e6e927e1a43137f61789f84fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
 *                     GNU AFFERO GENERAL PUBLIC LICENSE
 *                       Version 3, 19 November 2007
 *  Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
 *  Everyone is permitted to copy and distribute verbatim copies
 *  of this license document, but changing it is not allowed.
 */

using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Util;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Serialization;
using UVtools.Core.Extensions;
using UVtools.Core.FileFormats;
using UVtools.Core.Layers;
using UVtools.Core.Managers;

namespace UVtools.Core.Operations;

[Serializable]
public class OperationRepairLayers : Operation
{
    #region Members
    private bool _repairIslands = true;
    private bool _repairResinTraps = true;
    private bool _repairSuctionCups;
    private bool _removeEmptyLayers = true;
    private ushort _removeIslandsBelowEqualPixelCount = 5;
    private ushort _removeIslandsRecursiveIterations = 4;
    private ushort _attachIslandsBelowLayers = 2;
    private byte _resinTrapsOverlapBy = 5;
    private byte _suctionCupsVentHole = 16;
    private uint _gapClosingIterations = 1;
    private uint _noiseRemovalIterations;

    #endregion

    #region Overrides
    public override bool CanROI => false;
    public override string IconClass => "fa-solid fa-toolbox";
    public override string Title => "Repair layers and issues";
    public override string Description => string.Empty;

    public override string ConfirmationText => "attempt  this repair?";

    public override string ProgressTitle =>
        $"Reparing layers {LayerIndexStart} through {LayerIndexEnd}";

    public override string ProgressAction => "Repaired layers";

    public override string? ValidateInternally()
    {
        var sb = new StringBuilder();

        if (!_repairIslands && !_repairResinTraps && !_repairSuctionCups && !_removeEmptyLayers)
        {
            sb.AppendLine("You must select at least one repair operation.");
        }

        return sb.ToString();
    }

    public override string ToString()
    {
        var repair = new List<string>();
        if(_repairIslands) repair.Add("Islands");
        if(_repairResinTraps) repair.Add("Resin traps");
        if(_repairSuctionCups) repair.Add("Suction cups");
        if(_removeEmptyLayers) repair.Add("Empty layers");
        var result = $"[Repair: {string.Join('/', repair)}] " +
                     $"[Gap closing: {_gapClosingIterations}px] " +
                     $"[Noise removal: {_noiseRemovalIterations}px]" + LayerRangeString;
        if (!string.IsNullOrEmpty(ProfileName)) result = $"{ProfileName}: {result}";
        return result;
    }
    #endregion

    #region Constructor

    public OperationRepairLayers() { }

    public OperationRepairLayers(FileFormat slicerFile) : base(slicerFile) { }

    #endregion

    #region Properties
    public bool RepairIslands
    {
        get => _repairIslands;
        set => RaiseAndSetIfChanged(ref _repairIslands, value);
    }

    public bool RepairResinTraps
    {
        get => _repairResinTraps;
        set => RaiseAndSetIfChanged(ref _repairResinTraps, value);
    }

    public bool RepairSuctionCups
    {
        get => _repairSuctionCups;
        set => RaiseAndSetIfChanged(ref _repairSuctionCups, value);
    }

    public bool RemoveEmptyLayers
    {
        get => _removeEmptyLayers;
        set => RaiseAndSetIfChanged(ref _removeEmptyLayers, value);
    }

    public ushort RemoveIslandsBelowEqualPixelCount
    {
        get => _removeIslandsBelowEqualPixelCount;
        set => RaiseAndSetIfChanged(ref _removeIslandsBelowEqualPixelCount, value);
    }

    public ushort RemoveIslandsRecursiveIterations
    {
        get => _removeIslandsRecursiveIterations;
        set => RaiseAndSetIfChanged(ref _removeIslandsRecursiveIterations, value);
    }

    public ushort AttachIslandsBelowLayers
    {
        get => _attachIslandsBelowLayers;
        set => RaiseAndSetIfChanged(ref _attachIslandsBelowLayers, value);
    }

    public byte ResinTrapsOverlapBy
    {
        get => _resinTrapsOverlapBy;
        set => RaiseAndSetIfChanged(ref _resinTrapsOverlapBy, value);
    }

    public byte SuctionCupsVentHole
    {
        get => _suctionCupsVentHole;
        set => RaiseAndSetIfChanged(ref _suctionCupsVentHole, value);
    }

    public uint GapClosingIterations
    {
        get => _gapClosingIterations;
        set => RaiseAndSetIfChanged(ref _gapClosingIterations, value);
    }

    public uint NoiseRemovalIterations
    {
        get => _noiseRemovalIterations;
        set => RaiseAndSetIfChanged(ref _noiseRemovalIterations, value);
    }

    [XmlIgnore] public IslandDetectionConfiguration IslandDetectionConfig { get; set; } = new();

    #endregion

    #region Equality

    protected bool Equals(OperationRepairLayers other)
    {
        return _repairIslands == other._repairIslands && _repairResinTraps == other._repairResinTraps && _removeEmptyLayers == other._removeEmptyLayers && _repairSuctionCups == other._repairSuctionCups && _removeIslandsBelowEqualPixelCount == other._removeIslandsBelowEqualPixelCount && _removeIslandsRecursiveIterations == other._removeIslandsRecursiveIterations && _attachIslandsBelowLayers == other._attachIslandsBelowLayers && _resinTrapsOverlapBy == other._resinTrapsOverlapBy && _suctionCupsVentHole == other._suctionCupsVentHole && _gapClosingIterations == other._gapClosingIterations && _noiseRemovalIterations == other._noiseRemovalIterations;
    }

    public override bool Equals(object? obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != this.GetType()) return false;
        return Equals((OperationRepairLayers)obj);
    }

    public override int GetHashCode()
    {
        var hashCode = new HashCode();
        hashCode.Add(_repairIslands);
        hashCode.Add(_repairResinTraps);
        hashCode.Add(_removeEmptyLayers);
        hashCode.Add(_repairSuctionCups);
        hashCode.Add(_removeIslandsBelowEqualPixelCount);
        hashCode.Add(_removeIslandsRecursiveIterations);
        hashCode.Add(_attachIslandsBelowLayers);
        hashCode.Add(_resinTrapsOverlapBy);
        hashCode.Add(_suctionCupsVentHole);
        hashCode.Add(_gapClosingIterations);
        hashCode.Add(_noiseRemovalIterations);
        return hashCode.ToHashCode();
    }

    #endregion

    #region Methods

    protected override bool ExecuteInternally(OperationProgress progress)
    {
        var issues = SlicerFile.IssueManager.GetVisible().ToList();
        // Remove islands
        if (//Issues is not null
            //IslandDetectionConfig is not null
            _repairIslands
            && _removeIslandsBelowEqualPixelCount > 0
            && _removeIslandsRecursiveIterations != 1)
        {
            progress.Reset("Removed recursive islands");
            ushort limit = _removeIslandsRecursiveIterations == 0
                ? ushort.MaxValue
                : _removeIslandsRecursiveIterations;

            var recursiveIssues = issues;
            var islandsToRecompute = new ConcurrentBag<uint>();
            var islandConfig = IslandDetectionConfig.Clone();
            var overhangConfig = new OverhangDetectionConfiguration(false);
            var touchingBoundsConfig = new TouchingBoundDetectionConfiguration(false);
            var printHeightConfig = new PrintHeightDetectionConfiguration(false);
            var resinTrapsConfig = new ResinTrapDetectionConfiguration(false);
            var emptyLayersConfig = false;

            islandConfig.Enabled = true;
            //islandConfig.RequiredAreaToProcessCheck = (ushort)(_removeIslandsBelowEqualPixelCount / 2);

            for (uint i = 0; i < limit; i++)
            {
                if (i > 0)
                {
                    /*var whiteList = islandsToRecompute.GroupBy(u => u)
                        .Select(grp => grp.First())
                        .ToList();*/
                    islandConfig.WhiteListLayers = islandsToRecompute.ToList();
                    recursiveIssues = SlicerFile.IssueManager.DetectIssues(islandConfig, overhangConfig, resinTrapsConfig, touchingBoundsConfig, printHeightConfig, emptyLayersConfig);
                    //Debug.WriteLine(i);
                }

                var issuesGroup = IssueManager.GetIssuesBy(recursiveIssues, MainIssue.IssueType.Island)
                    .Where(issue => issue.PixelsCount <= RemoveIslandsBelowEqualPixelCount)
                    .GroupBy(issue => issue.LayerIndex);
                        

                if (!issuesGroup.Any()) break; // Nothing to process

                islandsToRecompute.Clear();
                Parallel.ForEach(issuesGroup, CoreSettings.GetParallelOptions(progress), group =>
                {
                    var layer = SlicerFile[group.Key];
                    var image = layer.LayerMat;
                    var span = image.GetDataByteSpan();
                    foreach (IssueOfPoints issue in group)
                    {
                        foreach (var issuePixel in issue.Points)
                        {
                            span[image.GetPixelPos(issuePixel)] = 0;
                        }

                        progress.LockAndIncrement();
                    }

                    var nextLayerIndex = group.Key + 1;
                    if (nextLayerIndex < SlicerFile.LayerCount)
                        islandsToRecompute.Add(nextLayerIndex);

                    layer.LayerMat = image;
                });

                // Remove from main list due the replicate below repair
                issues.RemoveAll(mainIssue => mainIssue.Type == MainIssue.IssueType.Island && mainIssue.Area <= RemoveIslandsBelowEqualPixelCount);

                if (islandsToRecompute.IsEmpty) break; // No more leftovers
            }
        }

        if (_repairIslands && _attachIslandsBelowLayers > 0)
        {
            var islandsToProcess = issues;

            if (islandsToProcess.Count == 0)
            {
                var islandConfig = IslandDetectionConfig.Clone();
                var overhangConfig = new OverhangDetectionConfiguration(false);
                var touchingBoundsConfig = new TouchingBoundDetectionConfiguration(false);
                var printHeightConfig = new PrintHeightDetectionConfiguration(false);
                var resinTrapsConfig = new ResinTrapDetectionConfiguration(false);
                var emptyLayersConfig = false;

                islandConfig.Enabled = true;

                islandsToProcess = SlicerFile.IssueManager.DetectIssues(islandConfig, overhangConfig, resinTrapsConfig, touchingBoundsConfig, printHeightConfig, emptyLayersConfig, progress);
                islandsToProcess.RemoveAll(mainIssue => SlicerFile.IssueManager.IgnoredIssues.Contains(mainIssue));
            }

            var issuesGroup = IssueManager.GetIssuesBy(islandsToProcess, MainIssue.IssueType.Island).GroupBy(issue => issue.LayerIndex);

            progress.Reset("Attempt to attach islands below", (uint) islandsToProcess.Count);
            var sync = new object();
            Parallel.ForEach(issuesGroup, CoreSettings.GetParallelOptions(progress), group =>
            {
                using var mat = SlicerFile[group.Key].LayerMat;
                var matSpan = mat.GetDataByteSpan();
                var matCache = new Dictionary<uint, Mat>();
                var matCacheModified = new Dictionary<uint, bool>();
                var startLayer = Math.Max(0, (int)group.Key - 2);
                var lowestPossibleLayer = (uint)Math.Max(0, (int)group.Key - 1 - _attachIslandsBelowLayers);
                    
                for (var layerIndex = startLayer+1; layerIndex >= lowestPossibleLayer; layerIndex--)
                {
                    //Debug.WriteLine(layerIndex);
                    Monitor.Enter(SlicerFile[layerIndex].Mutex);
                    matCache.Add((uint) layerIndex, SlicerFile[layerIndex].LayerMat);
                    matCacheModified.Add((uint) layerIndex, false);
                }

                foreach (IssueOfPoints issue in group)
                {
                    int foundAt = startLayer == 0 ? 0 : - 1;
                    var requiredSupportingPixels = Math.Max(1, issue.PixelsCount * IslandDetectionConfig.RequiredPixelsToSupportMultiplier);

                    for (var layerIndex = startLayer; layerIndex >= lowestPossibleLayer && foundAt < 0; layerIndex--)
                    {
                        uint pixelsSupportingIsland = 0;
                            
                        unsafe
                        {
                            var span = matCache[(uint) layerIndex].GetBytePointer();

                            foreach (var point in issue.Points)
                            {
                                if (span[mat.GetPixelPos(point)] < IslandDetectionConfig.RequiredPixelBrightnessToSupport)
                                    continue;

                                pixelsSupportingIsland++;

                                if (pixelsSupportingIsland >= requiredSupportingPixels)
                                {
                                    foundAt = layerIndex + 1;
                                    break;
                                }
                            }
                                
                        }
                    }

                    // Copy pixels
                    if (foundAt >= 0)
                    {
                        for (var layerIndex = startLayer + 1; layerIndex >= foundAt; layerIndex--)
                        {
                            matCacheModified[(uint) layerIndex] = true;
                            unsafe
                            {
                                var span = matCache[(uint) layerIndex].GetBytePointer();

                                foreach (var point in issue.Points)
                                {
                                    var pos = mat.GetPixelPos(point);
                                    span[pos] = (byte)Math.Min(span[pos] + matSpan[pos], byte.MaxValue);
                                }
                            }
                        }

                        lock (sync)
                        {
                            // Remove from processed issues
                            issues.Remove(issue.Parent!);
                        }
                    }

                    progress.LockAndIncrement();
                }

                foreach (var dict in matCache)
                {
                    if (matCacheModified[dict.Key])
                    {
                        SlicerFile[dict.Key].LayerMat = dict.Value;
                    }
                    dict.Value.Dispose();
                    Monitor.Exit(SlicerFile[dict.Key].Mutex);
                }
            });
        }

        progress.Reset(ProgressAction, LayerRangeCount);
        if (_repairIslands || _repairResinTraps)
        {
            Parallel.For(LayerIndexStart, LayerIndexEnd, CoreSettings.GetParallelOptions(progress), layerIndex =>
            {
                var layer = SlicerFile[layerIndex];
                Mat? image = null;

                void InitImage()
                {
                    image ??= layer.LayerMat;
                }

                if (issues.Count > 0)
                {
                    if (_repairIslands && _removeIslandsBelowEqualPixelCount > 0 && _removeIslandsRecursiveIterations == 1)
                    {
                        Span<byte> bytes = null;
                        foreach (IssueOfPoints issue in IssueManager.GetIssuesBy(issues, MainIssue.IssueType.Island, (uint)layerIndex))
                        {
                            if (issue.PixelsCount > _removeIslandsBelowEqualPixelCount) continue;

                            InitImage();
                            if (bytes == null) bytes = image!.GetDataByteSpan();

                            foreach (var issuePixel in issue.Points)
                            {
                                bytes[image!.GetPixelPos(issuePixel)] = 0;
                            }
                        }
                    }

                    if (_repairResinTraps)
                    {
                        foreach (IssueOfContours issue in IssueManager.GetIssuesBy(issues, MainIssue.IssueType.ResinTrap, (uint)layerIndex))
                        {
                            InitImage();
                            using var vec = new VectorOfVectorOfPoint(issue.Contours);
                            CvInvoke.DrawContours(image, vec, -1, EmguExtensions.WhiteColor, -1);
                            if (_resinTrapsOverlapBy > 0)
                            {
                                CvInvoke.DrawContours(image, vec, -1, EmguExtensions.WhiteColor, _resinTrapsOverlapBy * 2 + 1);
                            }
                        }
                    }
                }

                if (_repairIslands && (_gapClosingIterations > 0 || _noiseRemovalIterations > 0))
                {
                    InitImage();

                    if (_gapClosingIterations > 0)
                    {
                        CvInvoke.MorphologyEx(image, image, MorphOp.Close, EmguExtensions.Kernel3x3Rectangle, EmguExtensions.AnchorCenter,
                            (int)_gapClosingIterations, BorderType.Default, default);
                    }

                    if (_noiseRemovalIterations > 0)
                    {
                        CvInvoke.MorphologyEx(image, image, MorphOp.Open, EmguExtensions.Kernel3x3Rectangle, EmguExtensions.AnchorCenter,
                            (int)_noiseRemovalIterations, BorderType.Default, default);
                    }
                }

                if (image is not null)
                {
                    layer.LayerMat = image;
                    image.Dispose();
                }

                progress.LockAndIncrement();
            });
        }


        if (_repairSuctionCups && issues.Count > 0)
        {
            SlicerFile.IssueManager.DrillSuctionCupsForIssues(issues.Where(mainIssue => mainIssue.Type == MainIssue.IssueType.SuctionCup), _suctionCupsVentHole, progress);
        }

        if (_removeEmptyLayers)
        {
            var removeLayers = new List<uint>();
            for (var layerIndex = LayerIndexStart; layerIndex <= LayerIndexEnd; layerIndex++)
            {
                if (SlicerFile[layerIndex].NonZeroPixelCount == 0)
                {
                    removeLayers.Add(layerIndex);
                }
            }

            if (removeLayers.Count > 0)
            {
                OperationLayerRemove.RemoveLayers(SlicerFile, removeLayers, progress);
            }
        }

        return !progress.Token.IsCancellationRequested;
    }

    #endregion
}