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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
|
/*
* 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 System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using MoreLinq.Extensions;
using UVtools.Core.EmguCV;
using UVtools.Core.Extensions;
using UVtools.Core.FileFormats;
using UVtools.Core.Layers;
using UVtools.Core.Managers;
using UVtools.Core.Objects;
using UVtools.Core.Operations;
using UVtools.Core.PixelEditor;
namespace UVtools.Core
{
public class LayerManager : BindableBase, IList<Layer>, IDisposable
{
#region Properties
public FileFormat SlicerFile { get; set; }
private Layer[] _layers;
/// <summary>
/// Layers List
/// </summary>
public Layer[] Layers
{
get => _layers;
set
{
//if (ReferenceEquals(_layers, value)) return;
var rebuildProperties = false;
var oldLayerCount = LayerCount;
var oldLayers = _layers;
_layers = value;
BoundingRectangle = Rectangle.Empty;
if (LayerCount != oldLayerCount)
{
SlicerFile.LayerCount = LayerCount;
}
SlicerFile.RequireFullEncode = true;
SlicerFile.PrintHeight = SlicerFile.PrintHeight;
SlicerFile.UpdatePrintTime();
if (value is not null && LayerCount > 0)
{
//SetAllIsModified(true);
for (uint layerIndex = 0; layerIndex < LayerCount; layerIndex++) // Forced sanitize
{
if (_layers[layerIndex] is null) continue;
_layers[layerIndex].Index = layerIndex;
_layers[layerIndex].ParentLayerManager = this;
if (layerIndex >= oldLayerCount || layerIndex < oldLayerCount && !_layers[layerIndex].Equals(oldLayers[layerIndex]))
{
// Marks as modified only if layer image changed on this index
_layers[layerIndex].IsModified = true;
}
}
if (LayerCount != oldLayerCount && !SlicerFile.SuppressRebuildProperties && LastLayer is not null)
{
RebuildLayersProperties();
rebuildProperties = true;
}
}
if (!rebuildProperties)
{
SlicerFile.MaterialMilliliters = -1;
SlicerFile.RebuildGCode();
}
RaisePropertyChanged();
}
}
/// <summary>
/// Gets the last layer index
/// </summary>
public uint LastLayerIndex => LayerCount - 1;
/// <summary>
/// Gets the first layer
/// </summary>
public Layer FirstLayer => _layers?[0];
/// <summary>
/// Gets the last layer
/// </summary>
public Layer LastLayer => _layers?[^1];
/// <summary>
/// Gets the smallest bottom layer using the pixel count
/// </summary>
public Layer SmallestBottomLayer => _layers?.Where(layer => layer.IsBottomLayer && !layer.IsEmpty).MinBy(layer => layer.NonZeroPixelCount).FirstOrDefault();
/// <summary>
/// Gets the largest bottom layer using the pixel count
/// </summary>
public Layer LargestBottomLayer => _layers?.Where(layer => layer.IsBottomLayer && !layer.IsEmpty).MaxBy(layer => layer.NonZeroPixelCount).FirstOrDefault();
/// <summary>
/// Gets the smallest normal layer using the pixel count
/// </summary>
public Layer SmallestNormalLayer => _layers?.Where(layer => layer.IsNormalLayer && !layer.IsEmpty).MinBy(layer => layer.NonZeroPixelCount).FirstOrDefault();
/// <summary>
/// Gets the largest layer using the pixel count
/// </summary>
public Layer LargestNormalLayer => _layers?.Where(layer => layer.IsNormalLayer && !layer.IsEmpty).MaxBy(layer => layer.NonZeroPixelCount).FirstOrDefault();
/// <summary>
/// Gets the smallest normal layer using the pixel count
/// </summary>
public Layer SmallestLayer => _layers?.Where(layer => !layer.IsEmpty).MinBy(layer => layer.NonZeroPixelCount).FirstOrDefault();
/// <summary>
/// Gets the largest layer using the pixel count
/// </summary>
public Layer LargestLayer => _layers?.MaxBy(layer => layer.NonZeroPixelCount).FirstOrDefault();
/// <summary>
/// Gets the bounding rectangle of the object
/// </summary>
private Rectangle _boundingRectangle = Rectangle.Empty;
/// <summary>
/// Gets the bounding rectangle of the object
/// </summary>
public Rectangle BoundingRectangle
{
get => GetBoundingRectangle();
set
{
RaiseAndSetIfChanged(ref _boundingRectangle, value);
RaisePropertyChanged(nameof(BoundingRectangleMillimeters));
}
}
/// <summary>
/// Gets the bounding rectangle of the object in millimeters
/// </summary>
public RectangleF BoundingRectangleMillimeters
{
get
{
if (SlicerFile is null) return RectangleF.Empty;
var pixelSize = SlicerFile.PixelSize;
return new RectangleF(
(float)Math.Round(_boundingRectangle.X * pixelSize.Width, 2),
(float)Math.Round(_boundingRectangle.Y * pixelSize.Height, 2),
(float)Math.Round(_boundingRectangle.Width * pixelSize.Width, 2),
(float)Math.Round(_boundingRectangle.Height * pixelSize.Height, 2));
}
}
public void Init(uint layerCount, bool initializeLayers = false)
{
_layers = new Layer[layerCount];
if (!initializeLayers) return;
for (uint layerIndex = 0; layerIndex < layerCount; layerIndex++)
{
this[layerIndex] = new Layer(layerIndex, this);
}
}
public void Init(Layer[] layers)
{
_layers = layers;
}
public void Add(Layer layer)
{
Layers = Enumerable.Append(_layers, layer).ToArray();
}
public void Add(IEnumerable<Layer> layers)
{
var list = _layers.ToList();
list.AddRange(layers);
Layers = list.ToArray();
}
public void Clear()
{
//Layers = Array.Empty<Layer>();
_layers = null;
}
public bool Contains(Layer layer)
{
return _layers.Contains(layer);
}
public void CopyTo(Layer[] array, int arrayIndex)
{
_layers.CopyTo(array, arrayIndex);
}
public bool Remove(Layer layer)
{
var list = _layers.ToList();
var result = list.Remove(layer);
if (result)
{
Layers = list.ToArray();
}
return result;
}
public int IndexOf(Layer layer)
{
for (int layerIndex = 0; layerIndex < Count; layerIndex++)
{
if (_layers[layerIndex].Equals(layer)) return layerIndex;
}
return -1;
}
public void Prepend(Layer layer) => Insert(0, layer);
public void Prepend(IEnumerable<Layer> layers) => InsertRange(0, layers);
public void Append(Layer layer) => Add(layer);
public void AppendRange(IEnumerable<Layer> layers) => Add(layers);
public void Insert(int index, Layer layer)
{
if (index < 0) return;
if (index > Count)
{
Add(layer); // Append
return;
}
var list = _layers.ToList();
list.Insert(index, layer);
Layers = list.ToArray();
}
public void InsertRange(int index, IEnumerable<Layer> layers)
{
if (index < 0) return;
if (index > Count)
{
Add(layers);
return;
}
var list = _layers.ToList();
list.InsertRange(index, layers);
Layers = list.ToArray();
}
public void RemoveAt(int index)
{
if (index >= LastLayerIndex) return;
var list = _layers.ToList();
list.RemoveAt(index);
Layers = list.ToArray();
}
public void RemoveRange(int index, int count)
{
if (count <= 0 || index >= LastLayerIndex) return;
var list = _layers.ToList();
list.RemoveRange(index, count);
Layers = list.ToArray();
}
/// <summary>
/// Removes all null layers in the collection
/// </summary>
public void RemoveNulls()
{
Layers = _layers.Where(layer => layer is not null).ToArray();
}
public int Count => _layers?.Length ?? 0;
public bool IsReadOnly => false;
/// <summary>
/// Gets the layers count
/// </summary>
public uint LayerCount => (uint)(_layers?.Length ?? 0);
public byte LayerDigits => (byte)LayerCount.ToString().Length;
/// <summary>
/// Gets if any layer got modified, otherwise false
/// </summary>
public bool IsModified
{
get
{
for (uint i = 0; i < LayerCount; i++)
{
if (_layers[i].IsModified) return true;
}
return false;
}
}
/// <summary>
/// True if all layers are using same value parameters as global settings, otherwise false
/// </summary>
public bool AllLayersAreUsingGlobalParameters => _layers.Where(layer => layer is not null).All(layer => layer.IsUsingGlobalParameters);
/// <summary>
/// True if any layer is using TSMC, otherwise false when none of layers is using TSMC
/// </summary>
public bool AnyLayerIsUsingTSMC => _layers.Where(layer => layer is not null).Any(layer => layer.IsUsingTSMC);
//public float LayerHeight => Layers[0].PositionZ;
#endregion
#region Constructors
public LayerManager(FileFormat slicerFile)
{
SlicerFile = slicerFile;
}
public LayerManager(uint layerCount, FileFormat slicerFile) : this(slicerFile)
{
SlicerFile = slicerFile;
_layers = new Layer[layerCount];
}
#endregion
#region Indexers
public Layer this[uint index]
{
get => _layers[index];
set => SetLayer(index, value);
}
public Layer this[int index]
{
get => _layers[index];
set => SetLayer((uint) index, value);
}
public Layer this[long index]
{
get => _layers[index];
set => SetLayer((uint) index, value);
}
public Layer[] this[System.Range range] => _layers[range];
#endregion
#region Numerators
public IEnumerator<Layer> GetEnumerator()
{
return ((IEnumerable<Layer>)Layers).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
#region Static Methods
/// <summary>
/// Compress a layer from a <see cref="Stream"/>
/// </summary>
/// <param name="input"><see cref="Stream"/> to compress</param>
/// <returns>Compressed byte array</returns>
public static byte[] CompressLayer(Stream input)
{
return CompressLayer(input.ToArray());
}
/// <summary>
/// Compress a layer from a byte array
/// </summary>
/// <param name="input">byte array to compress</param>
/// <returns>Compressed byte array</returns>
public static byte[] CompressLayer(byte[] input)
{
return input;
/*using (MemoryStream output = new MemoryStream())
{
using (DeflateStream dstream = new DeflateStream(output, CompressionLevel.Optimal))
{
dstream.Write(input, 0, input.Length);
}
return output.ToArray();
}*/
}
/// <summary>
/// Decompress a layer from a byte array
/// </summary>
/// <param name="input">byte array to decompress</param>
/// <returns>Decompressed byte array</returns>
public static byte[] DecompressLayer(byte[] input)
{
return input;
/*using (MemoryStream ms = new MemoryStream(input))
{
using (MemoryStream output = new MemoryStream())
{
using (DeflateStream dstream = new DeflateStream(ms, CompressionMode.Decompress))
{
dstream.CopyTo(output);
}
return output.ToArray();
}
}*/
}
#endregion
#region Methods
/// <summary>
/// Sanitize layers and thrown exception if a severe problem is found
/// </summary>
/// <returns>True if one or more corrections has been applied, otherwise false</returns>
public bool Sanitize()
{
bool appliedCorrections = false;
for (uint layerIndex = 0; layerIndex < LayerCount; layerIndex++)
{
// Check for null layers
if (this[layerIndex] is null) throw new InvalidDataException($"Layer {layerIndex} was defined but doesn't contain a valid image.");
if (layerIndex <= 0) continue;
// Check for bigger position z than it successor
if (this[layerIndex - 1].PositionZ > this[layerIndex].PositionZ) throw new InvalidDataException($"Layer {layerIndex - 1} ({this[layerIndex - 1].PositionZ}mm) have a higher Z position than the successor layer {layerIndex} ({this[layerIndex].PositionZ}mm).\n");
}
if (SlicerFile.ResolutionX == 0 || SlicerFile.ResolutionY == 0)
{
var layer = FirstLayer;
if (layer is not null)
{
using var mat = layer.LayerMat;
if (mat.Size.HaveZero())
{
throw new FileLoadException($"File resolution ({SlicerFile.Resolution}) is invalid and can't be auto fixed due invalid layers with same problem ({mat.Size}).", SlicerFile.FileFullPath);
}
SlicerFile.Resolution = mat.Size;
appliedCorrections = true;
}
}
// Fix 0mm positions at layer 0
if (this[0].PositionZ == 0)
{
for (uint layerIndex = 0; layerIndex < LayerCount; layerIndex++)
{
this[layerIndex].PositionZ = Layer.RoundHeight(this[layerIndex].PositionZ + SlicerFile.LayerHeight);
}
appliedCorrections = true;
}
// Fix LightPWM of 0
if (SlicerFile.LightPWM == 0)
{
SlicerFile.LightPWM = FileFormat.DefaultLightPWM;
appliedCorrections = true;
}
if (SlicerFile.BottomLightPWM == 0)
{
SlicerFile.BottomLightPWM = FileFormat.DefaultBottomLightPWM;
appliedCorrections = true;
}
return appliedCorrections;
}
/// <summary>
/// Rebuild layer properties based on slice settings
/// </summary>
public void RebuildLayersProperties(bool recalculateZPos = true, string property = null)
{
//var layerHeight = SlicerFile.LayerHeight;
for (uint layerIndex = 0; layerIndex < LayerCount; layerIndex++)
{
var layer = this[layerIndex];
layer.Index = layerIndex;
layer.ParentLayerManager = this;
if (recalculateZPos)
{
layer.PositionZ = SlicerFile.GetHeightFromLayer(layerIndex);
}
if (property != string.Empty)
{
if (property is null or nameof(SlicerFile.BottomLayerCount))
{
layer.LightOffDelay = SlicerFile.GetBottomOrNormalValue(layer, SlicerFile.BottomLightOffDelay, SlicerFile.LightOffDelay);
layer.WaitTimeBeforeCure = SlicerFile.GetBottomOrNormalValue(layer, SlicerFile.BottomWaitTimeBeforeCure, SlicerFile.WaitTimeBeforeCure);
layer.ExposureTime = SlicerFile.GetBottomOrNormalValue(layer, SlicerFile.BottomExposureTime, SlicerFile.ExposureTime);
layer.WaitTimeAfterCure = SlicerFile.GetBottomOrNormalValue(layer, SlicerFile.BottomWaitTimeAfterCure, SlicerFile.WaitTimeAfterCure);
layer.LiftHeight = SlicerFile.GetBottomOrNormalValue(layer, SlicerFile.BottomLiftHeight, SlicerFile.LiftHeight);
layer.LiftSpeed = SlicerFile.GetBottomOrNormalValue(layer, SlicerFile.BottomLiftSpeed, SlicerFile.LiftSpeed);
layer.LiftHeight2 = SlicerFile.GetBottomOrNormalValue(layer, SlicerFile.BottomLiftHeight2, SlicerFile.LiftHeight2);
layer.LiftSpeed2 = SlicerFile.GetBottomOrNormalValue(layer, SlicerFile.BottomLiftSpeed2, SlicerFile.LiftSpeed2);
layer.WaitTimeAfterLift = SlicerFile.GetBottomOrNormalValue(layer, SlicerFile.BottomWaitTimeAfterLift, SlicerFile.WaitTimeAfterLift);
layer.RetractSpeed = SlicerFile.GetBottomOrNormalValue(layer, SlicerFile.BottomRetractSpeed, SlicerFile.RetractSpeed);
layer.RetractHeight2 = SlicerFile.GetBottomOrNormalValue(layer, SlicerFile.BottomRetractHeight2, SlicerFile.RetractHeight2);
layer.RetractSpeed2 = SlicerFile.GetBottomOrNormalValue(layer, SlicerFile.BottomRetractSpeed2, SlicerFile.RetractSpeed2);
layer.LightPWM = SlicerFile.GetBottomOrNormalValue(layer, SlicerFile.BottomLightPWM, SlicerFile.LightPWM);
}
else
{
if (layer.IsBottomLayer)
{
if (property == nameof(SlicerFile.BottomLightOffDelay)) layer.LightOffDelay = SlicerFile.BottomLightOffDelay;
else if (property == nameof(SlicerFile.BottomWaitTimeBeforeCure)) layer.WaitTimeBeforeCure = SlicerFile.BottomWaitTimeBeforeCure;
else if (property == nameof(SlicerFile.BottomExposureTime)) layer.ExposureTime = SlicerFile.BottomExposureTime;
else if (property == nameof(SlicerFile.BottomWaitTimeAfterCure)) layer.WaitTimeAfterCure = SlicerFile.BottomWaitTimeAfterCure;
else if (property == nameof(SlicerFile.BottomLiftHeight)) layer.LiftHeight = SlicerFile.BottomLiftHeight;
else if (property == nameof(SlicerFile.BottomLiftSpeed)) layer.LiftSpeed = SlicerFile.BottomLiftSpeed;
else if (property == nameof(SlicerFile.BottomLiftHeight2)) layer.LiftHeight2 = SlicerFile.BottomLiftHeight2;
else if (property == nameof(SlicerFile.BottomLiftSpeed2)) layer.LiftSpeed2 = SlicerFile.BottomLiftSpeed2;
else if (property == nameof(SlicerFile.BottomWaitTimeAfterLift)) layer.WaitTimeAfterLift = SlicerFile.BottomWaitTimeAfterLift;
else if (property == nameof(SlicerFile.BottomRetractSpeed)) layer.RetractSpeed = SlicerFile.BottomRetractSpeed;
else if (property == nameof(SlicerFile.BottomRetractHeight2)) layer.RetractHeight2 = SlicerFile.BottomRetractHeight2;
else if (property == nameof(SlicerFile.BottomRetractSpeed2)) layer.RetractSpeed2 = SlicerFile.BottomRetractSpeed2;
else if (property == nameof(SlicerFile.BottomLightPWM)) layer.LightPWM = SlicerFile.BottomLightPWM;
// Propagate value to layer when bottom property does not exists
else if (property == nameof(SlicerFile.LightOffDelay) && !SlicerFile.CanUseBottomLightOffDelay) layer.LightOffDelay = SlicerFile.LightOffDelay;
else if (property == nameof(SlicerFile.WaitTimeBeforeCure) && !SlicerFile.CanUseBottomWaitTimeBeforeCure) layer.WaitTimeBeforeCure = SlicerFile.WaitTimeBeforeCure;
else if (property == nameof(SlicerFile.ExposureTime) && !SlicerFile.CanUseBottomExposureTime) layer.ExposureTime = SlicerFile.ExposureTime;
else if (property == nameof(SlicerFile.WaitTimeAfterCure) && !SlicerFile.CanUseBottomWaitTimeAfterCure) layer.WaitTimeAfterCure = SlicerFile.WaitTimeAfterCure;
else if (property == nameof(SlicerFile.LiftHeight) && !SlicerFile.CanUseBottomLiftHeight) layer.LiftHeight = SlicerFile.LiftHeight;
else if (property == nameof(SlicerFile.LiftSpeed) && !SlicerFile.CanUseBottomLiftSpeed) layer.LiftSpeed = SlicerFile.LiftSpeed;
else if (property == nameof(SlicerFile.LiftHeight2) && !SlicerFile.CanUseBottomLiftHeight2) layer.LiftHeight2 = SlicerFile.LiftHeight2;
else if (property == nameof(SlicerFile.LiftSpeed2) && !SlicerFile.CanUseBottomLiftSpeed2) layer.LiftSpeed2 = SlicerFile.LiftSpeed2;
else if (property == nameof(SlicerFile.WaitTimeAfterLift) && !SlicerFile.CanUseBottomWaitTimeAfterLift) layer.WaitTimeAfterLift = SlicerFile.WaitTimeAfterLift;
else if (property == nameof(SlicerFile.RetractSpeed) && !SlicerFile.CanUseBottomRetractSpeed) layer.RetractSpeed = SlicerFile.RetractSpeed;
else if (property == nameof(SlicerFile.RetractHeight2) && !SlicerFile.CanUseBottomRetractHeight2) layer.RetractHeight2 = SlicerFile.RetractHeight2;
else if (property == nameof(SlicerFile.RetractSpeed2) && !SlicerFile.CanUseRetractSpeed2) layer.RetractSpeed2 = SlicerFile.RetractSpeed2;
else if (property == nameof(SlicerFile.LightPWM) && !SlicerFile.CanUseBottomLightPWM) layer.LightPWM = SlicerFile.LightPWM;
}
else // Normal layers
{
if (property == nameof(SlicerFile.LightOffDelay)) layer.LightOffDelay = SlicerFile.LightOffDelay;
else if (property == nameof(SlicerFile.WaitTimeBeforeCure)) layer.WaitTimeBeforeCure = SlicerFile.WaitTimeBeforeCure;
else if (property == nameof(SlicerFile.ExposureTime)) layer.ExposureTime = SlicerFile.ExposureTime;
else if (property == nameof(SlicerFile.WaitTimeAfterCure)) layer.WaitTimeAfterCure = SlicerFile.WaitTimeAfterCure;
else if (property == nameof(SlicerFile.LiftHeight)) layer.LiftHeight = SlicerFile.LiftHeight;
else if (property == nameof(SlicerFile.LiftSpeed)) layer.LiftSpeed = SlicerFile.LiftSpeed;
else if (property == nameof(SlicerFile.LiftHeight2)) layer.LiftHeight2 = SlicerFile.LiftHeight2;
else if (property == nameof(SlicerFile.LiftSpeed2)) layer.LiftSpeed2 = SlicerFile.LiftSpeed2;
else if (property == nameof(SlicerFile.WaitTimeAfterLift)) layer.WaitTimeAfterLift = SlicerFile.WaitTimeAfterLift;
else if (property == nameof(SlicerFile.RetractSpeed)) layer.RetractSpeed = SlicerFile.RetractSpeed;
else if (property == nameof(SlicerFile.RetractHeight2)) layer.RetractHeight2 = SlicerFile.RetractHeight2;
else if (property == nameof(SlicerFile.RetractSpeed2)) layer.RetractSpeed2 = SlicerFile.RetractSpeed2;
else if (property == nameof(SlicerFile.LightPWM)) layer.LightPWM = SlicerFile.LightPWM;
}
}
}
layer.MaterialMilliliters = -1; // Recalculate this value to be sure
}
SlicerFile?.RebuildGCode();
}
/// <summary>
/// Set LiftHeight to 0 if previous and current have same PositionZ
/// <param name="zeroLightOffDelay">If true also set light off to 0, otherwise current value will be kept.</param>
/// </summary>
public void SetNoLiftForSamePositionedLayers(bool zeroLightOffDelay = false)
=> SetLiftForSamePositionedLayers(0, zeroLightOffDelay);
public void SetLiftForSamePositionedLayers(float liftHeight = 0, bool zeroLightOffDelay = false)
{
for (int layerIndex = 1; layerIndex < LayerCount; layerIndex++)
{
var layer = this[layerIndex];
if (this[layerIndex - 1].PositionZ != layer.PositionZ) continue;
layer.LiftHeightTotal = liftHeight;
layer.WaitTimeAfterLift = 0;
if (zeroLightOffDelay)
{
layer.LightOffDelay = 0;
layer.WaitTimeBeforeCure = 0;
layer.WaitTimeAfterCure = 0;
}
}
SlicerFile?.RebuildGCode();
}
public IEnumerable<Layer> GetSamePositionedLayers()
{
for (int layerIndex = 1; layerIndex < LayerCount; layerIndex++)
{
var layer = this[layerIndex];
if (this[layerIndex - 1].PositionZ != layer.PositionZ) continue;
yield return layer;
}
}
public IEnumerable<Layer> GetDistinctLayersByPositionZ(uint layerIndexStart = 0) =>
GetDistinctLayersByPositionZ(layerIndexStart, LastLayerIndex);
public IEnumerable<Layer> GetDistinctLayersByPositionZ(uint layerIndexStart, uint layerIndexEnd)
{
return layerIndexEnd - layerIndexStart >= LastLayerIndex
? SlicerFile.DistinctBy(layer => layer.PositionZ)
: SlicerFile.Where((_, layerIndex) => layerIndex >= layerIndexStart && layerIndex <= layerIndexEnd).DistinctBy(layer => layer.PositionZ);
}
public Mat GetMergedMatForSequentialPositionedLayers(uint layerIndex, MatCacheManager cacheManager, out uint lastLayerIndex)
{
var startLayerPositionZ = this[layerIndex].PositionZ;
lastLayerIndex = layerIndex;
var layerMat = cacheManager.Get1(layerIndex).Clone();
for (var curIndex = layerIndex + 1; curIndex < LayerCount && this[curIndex].PositionZ == startLayerPositionZ; curIndex++)
{
CvInvoke.Max(layerMat, cacheManager.Get1(curIndex), layerMat);
lastLayerIndex = curIndex;
}
return layerMat;
}
public Mat GetMergedMatForSequentialPositionedLayers(uint layerIndex, MatCacheManager cacheManager)
=> GetMergedMatForSequentialPositionedLayers(layerIndex, cacheManager, out _);
public Mat GetMergedMatForSequentialPositionedLayers(uint layerIndex, out uint lastLayerIndex)
{
var startLayer = this[layerIndex];
lastLayerIndex = layerIndex;
var layerMat = startLayer.LayerMat;
for (var curIndex = layerIndex + 1; curIndex < LayerCount && this[curIndex].PositionZ == startLayer.PositionZ; curIndex++)
{
using var nextLayer = this[curIndex].LayerMat;
CvInvoke.Max(nextLayer, layerMat, layerMat);
lastLayerIndex = curIndex;
}
return layerMat;
}
public Mat GetMergedMatForSequentialPositionedLayers(uint layerIndex)
=> GetMergedMatForSequentialPositionedLayers(layerIndex, out _);
public Rectangle GetBoundingRectangle(OperationProgress progress = null)
{
var firstLayer = FirstLayer;
if (!_boundingRectangle.IsEmpty || LayerCount == 0 || firstLayer is null || !firstLayer.HaveImage) return _boundingRectangle;
progress ??= new OperationProgress(OperationProgress.StatusOptimizingBounds, LayerCount - 1);
_boundingRectangle = firstLayer.BoundingRectangle;
if (_boundingRectangle.IsEmpty) // Safe checking
{
progress.Reset(OperationProgress.StatusOptimizingBounds, LayerCount-1);
Parallel.For(0, LayerCount, CoreSettings.ParallelOptions, layerIndex =>
{
if (progress.Token.IsCancellationRequested) return;
this[layerIndex].GetBoundingRectangle();
if (progress is null) return;
progress.LockAndIncrement();
});
_boundingRectangle = firstLayer.BoundingRectangle;
if (progress.Token.IsCancellationRequested)
{
_boundingRectangle = Rectangle.Empty;
progress.Token.ThrowIfCancellationRequested();
}
}
progress.Reset(OperationProgress.StatusCalculatingBounds, LayerCount-1);
for (int i = 1; i < LayerCount; i++)
{
if(this[i] is null || this[i].BoundingRectangle.IsEmpty) continue;
_boundingRectangle = Rectangle.Union(_boundingRectangle, this[i].BoundingRectangle);
progress++;
}
RaisePropertyChanged(nameof(BoundingRectangle));
return _boundingRectangle;
}
/// <summary>
/// Sets a layer
/// </summary>
/// <param name="index">Layer index</param>
/// <param name="layer">Layer to add</param>
/// <param name="makeClone">True to add a clone of the layer</param>
public void SetLayer(uint index, Layer layer, bool makeClone = false)
{
if (_layers[index] is not null && layer is not null) layer.IsModified = true;
_layers[index] = makeClone && layer is not null ? layer.Clone() : layer;
if (layer is null) return;
layer.Index = index;
layer.ParentLayerManager = this;
}
/// <summary>
/// Add a list of layers
/// </summary>
/// <param name="layers">Layers to add</param>
/// <param name="makeClone">True to add a clone of layers</param>
public void SetLayers(IEnumerable<Layer> layers, bool makeClone = false)
{
foreach (var layer in layers)
{
SetLayer(layer.Index, layer, makeClone);
}
}
/// <summary>
/// Get layer given index
/// </summary>
/// <param name="index">Layer index</param>
/// <returns></returns>
public Layer GetLayer(uint index)
{
return _layers[index];
}
public Layer GetSmallestLayerBetween(uint layerStartIndex, uint layerEndIndex)
{
return _layers?.Where((layer, index) => !layer.IsEmpty && index >= layerStartIndex && index <= layerEndIndex).MinBy(layer => layer.NonZeroPixelCount).FirstOrDefault();
}
public Layer GetLargestLayerBetween(uint layerStartIndex, uint layerEndIndex)
{
return _layers?.Where((layer, index) => !layer.IsEmpty && index >= layerStartIndex && index <= layerEndIndex).MaxBy(layer => layer.NonZeroPixelCount).FirstOrDefault();
}
public static void MutateGetVarsIterationChamfer(uint startLayerIndex, uint endLayerIndex, int iterationsStart, int iterationsEnd, ref bool isFade, out float iterationSteps, out int maxIteration)
{
iterationSteps = 0;
maxIteration = 0;
isFade = isFade && startLayerIndex != endLayerIndex && iterationsStart != iterationsEnd;
if (!isFade) return;
iterationSteps = Math.Abs((iterationsStart - (float)iterationsEnd) / ((float)endLayerIndex - startLayerIndex));
maxIteration = Math.Max(iterationsStart, iterationsEnd);
}
public static int MutateGetIterationVar(bool isFade, int iterationsStart, int iterationsEnd, float iterationSteps, int maxIteration, uint startLayerIndex, uint layerIndex)
{
if (!isFade) return iterationsStart;
// calculate iterations based on range
int iterations = (int)(iterationsStart < iterationsEnd
? iterationsStart + (layerIndex - startLayerIndex) * iterationSteps
: iterationsStart - (layerIndex - startLayerIndex) * iterationSteps);
// constrain
return Math.Min(Math.Max(0, iterations), maxIteration);
}
public static int MutateGetIterationChamfer(uint layerIndex, uint startLayerIndex, uint endLayerIndex, int iterationsStart,
int iterationsEnd, bool isFade)
{
MutateGetVarsIterationChamfer(startLayerIndex, endLayerIndex, iterationsStart, iterationsEnd, ref isFade,
out float iterationSteps, out int maxIteration);
return MutateGetIterationVar(isFade, iterationsStart, iterationsEnd, iterationSteps, maxIteration, startLayerIndex, layerIndex);
}
public void DrawModifications(IList<PixelOperation> drawings, OperationProgress progress = null)
{
progress ??= new OperationProgress();
progress.Reset("Drawings", (uint) drawings.Count);
ConcurrentDictionary<uint, Mat> modifiedLayers = new();
for (var i = 0; i < drawings.Count; i++)
{
var operation = drawings[i];
if (operation.OperationType == PixelOperation.PixelOperationType.Drawing)
{
var operationDrawing = (PixelDrawing) operation;
var mat = modifiedLayers.GetOrAdd(operation.LayerIndex, u => this[operation.LayerIndex].LayerMat);
if (operationDrawing.BrushSize == 1)
{
mat.SetByte(operation.Location.X, operation.Location.Y, operationDrawing.Brightness);
continue;
}
mat.DrawPolygon((byte)operationDrawing.BrushShape, operationDrawing.BrushSize / 2, operationDrawing.Location,
new MCvScalar(operationDrawing.Brightness), operationDrawing.RotationAngle, operationDrawing.Thickness, operationDrawing.LineType);
/*switch (operationDrawing.BrushShape)
{
case PixelDrawing.BrushShapeType.Square:
CvInvoke.Rectangle(mat, operationDrawing.Rectangle, new MCvScalar(operationDrawing.Brightness), operationDrawing.Thickness, operationDrawing.LineType);
break;
case PixelDrawing.BrushShapeType.Circle:
CvInvoke.Circle(mat, operation.Location, operationDrawing.BrushSize / 2,
new MCvScalar(operationDrawing.Brightness), operationDrawing.Thickness, operationDrawing.LineType);
break;
default:
throw new ArgumentOutOfRangeException();
}*/
}
else if (operation.OperationType == PixelOperation.PixelOperationType.Text)
{
var operationText = (PixelText)operation;
var mat = modifiedLayers.GetOrAdd(operation.LayerIndex, u => this[operation.LayerIndex].LayerMat);
mat.PutTextRotated(operationText.Text, operationText.Location, operationText.Font, operationText.FontScale, new MCvScalar(operationText.Brightness), operationText.Thickness, operationText.LineType, operationText.Mirror, operationText.LineAlignment, operationText.Angle);
}
else if (operation.OperationType == PixelOperation.PixelOperationType.Eraser)
{
var mat = modifiedLayers.GetOrAdd(operation.LayerIndex, u => this[operation.LayerIndex].LayerMat);
using var layerContours = mat.FindContours(out var hierarchy, RetrType.Tree);
if (mat.GetByte(operation.Location) >= 10)
{
using var vec = EmguContours.GetContoursInside(layerContours, hierarchy, operation.Location);
if (vec.Size > 0)
{
CvInvoke.DrawContours(mat, vec, -1, new MCvScalar(operation.PixelBrightness), -1);
}
}
}
else if (operation.OperationType == PixelOperation.PixelOperationType.Supports)
{
var operationSupport = (PixelSupport)operation;
int drawnLayers = 0;
for (int operationLayer = (int)operation.LayerIndex-1; operationLayer >= 0; operationLayer--)
{
var mat = modifiedLayers.GetOrAdd((uint) operationLayer, u => this[operationLayer].LayerMat);
int radius = (operationLayer > 10 ? Math.Min(operationSupport.TipDiameter + drawnLayers, operationSupport.PillarDiameter) : operationSupport.BaseDiameter) / 2;
uint whitePixels;
int yStart = Math.Max(0, operation.Location.Y - operationSupport.TipDiameter / 2);
int xStart = Math.Max(0, operation.Location.X - operationSupport.TipDiameter / 2);
using (var matCircleRoi = new Mat(mat, new Rectangle(xStart, yStart, operationSupport.TipDiameter, operationSupport.TipDiameter)))
{
using var matCircleMask = matCircleRoi.NewBlank();
CvInvoke.Circle(matCircleMask, new Point(operationSupport.TipDiameter / 2, operationSupport.TipDiameter / 2),
operationSupport.TipDiameter / 2, new MCvScalar(operation.PixelBrightness), -1);
CvInvoke.BitwiseAnd(matCircleRoi, matCircleMask, matCircleMask);
whitePixels = (uint) CvInvoke.CountNonZero(matCircleMask);
}
if (whitePixels >= Math.Pow(operationSupport.TipDiameter, 2) / 3)
{
//CvInvoke.Circle(mat, operation.Location, radius, new MCvScalar(255), -1);
if (drawnLayers == 0) continue; // Supports nonexistent, keep digging
break; // White area end supporting
}
CvInvoke.Circle(mat, operation.Location, radius, new MCvScalar(operation.PixelBrightness), -1, operationSupport.LineType);
drawnLayers++;
}
}
else if (operation.OperationType == PixelOperation.PixelOperationType.DrainHole)
{
uint drawnLayers = 0;
var operationDrainHole = (PixelDrainHole)operation;
for (int operationLayer = (int)operation.LayerIndex; operationLayer >= 0; operationLayer--)
{
var mat = modifiedLayers.GetOrAdd((uint)operationLayer, u => this[operationLayer].LayerMat);
int radius = operationDrainHole.Diameter / 2;
uint blackPixels;
int yStart = Math.Max(0, operation.Location.Y - radius);
int xStart = Math.Max(0, operation.Location.X - radius);
using (var matCircleRoi = new Mat(mat, new Rectangle(xStart, yStart, operationDrainHole.Diameter, operationDrainHole.Diameter)))
{
using var matCircleRoiInv = new Mat();
CvInvoke.Threshold(matCircleRoi, matCircleRoiInv, 100, 255, ThresholdType.BinaryInv);
using var matCircleMask = matCircleRoi.NewBlank();
CvInvoke.Circle(matCircleMask, new Point(radius, radius), radius, EmguExtensions.WhiteColor, -1);
CvInvoke.BitwiseAnd(matCircleRoiInv, matCircleMask, matCircleMask);
blackPixels = (uint) CvInvoke.CountNonZero(matCircleMask);
}
if (blackPixels >= Math.Pow(operationDrainHole.Diameter, 2) / 3) // Enough area to drain?
{
if (drawnLayers == 0) continue; // Drill not found a target yet, keep digging
break; // Stop drill drain found!
}
CvInvoke.Circle(mat, operation.Location, radius, EmguExtensions.BlackColor, -1, operationDrainHole.LineType);
drawnLayers++;
}
}
progress++;
}
progress.Reset("Saving", (uint) modifiedLayers.Count);
Parallel.ForEach(modifiedLayers, CoreSettings.ParallelOptions, modifiedLayer =>
{
this[modifiedLayer.Key].LayerMat = modifiedLayer.Value;
modifiedLayer.Value.Dispose();
progress.LockAndIncrement();
});
}
/// <summary>
/// Set the IsModified property for all layers
/// </summary>
public void SetAllIsModified(bool isModified)
{
for (uint i = 0; i < LayerCount; i++)
{
if(Layers[i] is null) continue;
Layers[i].IsModified = isModified;
}
}
/// <summary>
/// Reallocate with new size
/// </summary>
/// <returns></returns>
public Layer[] ReallocateNew(uint newLayerCount, bool makeClone = false)
{
var layers = new Layer[newLayerCount];
for (uint layerIndex = 0; layerIndex < LayerCount; layerIndex++)
{
if (layerIndex >= newLayerCount) break;
var layer = this[layerIndex];
layers[layerIndex] = makeClone && layer is not null ? layer.Clone() : layer;
}
return layers;
}
/// <summary>
/// Reallocate layer count with a new size
/// </summary>
/// <param name="newLayerCount">New layer count</param>
/// <param name="initBlack"></param>
public void Reallocate(uint newLayerCount, bool initBlack = false)
{
var oldLayerCount = LayerCount;
int differenceLayerCount = (int)newLayerCount - Count;
if (differenceLayerCount == 0) return;
Array.Resize(ref _layers, (int) newLayerCount);
if (differenceLayerCount > 0 && initBlack)
{
Parallel.For(oldLayerCount, newLayerCount, CoreSettings.ParallelOptions, layerIndex =>
{
this[layerIndex] = new Layer((uint)layerIndex, EmguExtensions.InitMat(SlicerFile.Resolution), this);
});
}
}
/// <summary>
/// Reallocate at given index
/// </summary>
/// <returns></returns>
public void ReallocateInsert(uint insertAtLayerIndex, uint layerCount, bool initBlack = false)
{
var newLayers = new Layer[LayerCount + layerCount];
// Rearrange
for (uint layerIndex = 0; layerIndex < insertAtLayerIndex; layerIndex++)
{
newLayers[layerIndex] = _layers[layerIndex];
}
// Rearrange
for (uint layerIndex = insertAtLayerIndex; layerIndex < _layers.Length; layerIndex++)
{
newLayers[layerCount + layerIndex] = _layers[layerIndex];
newLayers[layerCount + layerIndex].Index = layerCount + layerIndex;
}
// Allocate new layers
if (initBlack)
{
Parallel.For(insertAtLayerIndex, insertAtLayerIndex + layerCount, CoreSettings.ParallelOptions, layerIndex =>
{
newLayers[layerIndex] = new Layer((uint) layerIndex, EmguExtensions.InitMat(SlicerFile.Resolution), this);
});
}
/*for (uint layerIndex = insertAtLayerIndex; layerIndex < insertAtLayerIndex + layerCount; layerIndex++)
{
Layers[layerIndex] = initBlack ? new Layer(layerIndex, EmguExtensions.InitMat(SlicerFile.Resolution), this) : null;
}*/
_layers = newLayers;
}
/// <summary>
/// Reallocate at a kept range
/// </summary>
/// <param name="startLayerIndex"></param>
/// <param name="endLayerIndex"></param>
public void ReallocateRange(uint startLayerIndex, uint endLayerIndex)
{
if ((int)(endLayerIndex - startLayerIndex) < 0) return;
var newLayers = new Layer[1 + endLayerIndex - startLayerIndex];
uint currentLayerIndex = 0;
for (uint layerIndex = startLayerIndex; layerIndex <= endLayerIndex; layerIndex++)
{
newLayers[currentLayerIndex++] = _layers[layerIndex];
}
_layers = newLayers;
}
/// <summary>
/// Reallocate at start
/// </summary>
/// <returns></returns>
public void ReallocateStart(uint layerCount, bool initBlack = false) => ReallocateInsert(0, layerCount, initBlack);
/// <summary>
/// Reallocate at end
/// </summary>
/// <returns></returns>
public void ReallocateEnd(uint layerCount, bool initBlack = false) => ReallocateInsert(LayerCount, layerCount, initBlack);
/// <summary>
/// Allocate layers from a Mat array
/// </summary>
/// <param name="mats"></param>
/// <returns>The new Layer array</returns>
public Layer[] AllocateFromMat(Mat[] mats)
{
var layers = new Layer[mats.Length];
Parallel.For(0, mats.Length, CoreSettings.ParallelOptions, i =>
{
layers[i] = new Layer((uint)i, mats[i], this);
});
return layers;
}
/// <summary>
/// Allocate layers from a Mat array and set them to the current file
/// </summary>
/// <param name="mats"></param>
/// /// <returns>The new Layer array</returns>
public Layer[] AllocateAndSetFromMat(Mat[] mats)
{
var layers = AllocateFromMat(mats);
Layers = layers;
return layers;
}
/// <summary>
/// Clone this object
/// </summary>
/// <returns></returns>
public LayerManager Clone()
{
LayerManager layerManager = new(SlicerFile);
layerManager.Init(CloneLayers());
/*foreach (var layer in this)
{
layerManager[layer.Index] = layer.Clone();
}*/
layerManager.BoundingRectangle = BoundingRectangle;
return layerManager;
}
/// <summary>
/// Clone layers
/// </summary>
/// <returns></returns>
public Layer[] CloneLayers()
{
return Layer.CloneLayers(_layers);
}
public void Dispose() { }
#endregion
#region Formater
public override string ToString()
{
return $"{nameof(BoundingRectangle)}: {BoundingRectangle}, {nameof(LayerCount)}: {LayerCount}, {nameof(IsModified)}: {IsModified}";
}
#endregion
}
}
|