-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathLayer.cs
1941 lines (1709 loc) · 65.9 KB
/
Layer.cs
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
/*
* 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 K4os.Compression.LZ4;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text.Json.Serialization;
using System.Xml.Serialization;
using UVtools.Core.EmguCV;
using UVtools.Core.Extensions;
using UVtools.Core.FileFormats;
using UVtools.Core.Objects;
using UVtools.Core.Operations;
namespace UVtools.Core.Layers;
#region Enums
public enum LayerCompressionCodec : byte
{
[Description("PNG: Compression=High Speed=Slow (Use with low RAM)")]
Png,
[Description("GZip: Compression=Medium Speed=Medium (Optimal)")]
GZip,
[Description("Deflate: Compression=Medium Speed=Medium (Optimal)")]
Deflate,
[Description("LZ4: Compression=Low Speed=Fast (Use with high RAM)")]
Lz4,
//[Description("None: Compression=None Speed=Fastest (Your soul belongs to RAM)")]
//None
}
#endregion
/// <summary>
/// Represent a Layer
/// </summary>
public class Layer : BindableBase, IEquatable<Layer>, IEquatable<uint>
{
#region Constants
public const byte HeightPrecision = 3;
public const decimal HeightPrecisionIncrement = 0.001M;
public const decimal MinimumHeight = 0.01M;
public const decimal MaximumHeight = 0.2M;
#endregion
#region Members
public object Mutex = new();
private LayerCompressionCodec _compressionCodec;
private byte[]? _compressedBytes;
private uint _nonZeroPixelCount;
private Rectangle _boundingRectangle = Rectangle.Empty;
private uint _firstPixelIndex;
private uint _lastPixelIndex;
private Point _firstPixelPosition;
private Point _lastPixelPosition;
private bool _isModified;
private uint _index;
private uint _resolutionX;
private uint _resolutionY;
private float _positionZ;
private float _lightOffDelay;
private float _waitTimeBeforeCure;
private float _exposureTime;
private float _waitTimeAfterCure;
private float _liftHeight = FileFormat.DefaultLiftHeight;
private float _liftSpeed = FileFormat.DefaultLiftSpeed;
private float _liftHeight2 = FileFormat.DefaultLiftHeight2;
private float _liftSpeed2 = FileFormat.DefaultLiftSpeed2;
private float _waitTimeAfterLift;
private float _retractSpeed = FileFormat.DefaultRetractSpeed;
private float _retractHeight2 = FileFormat.DefaultRetractHeight2;
private float _retractSpeed2 = FileFormat.DefaultRetractSpeed2;
private byte _lightPWM = FileFormat.DefaultLightPWM;
private float _materialMilliliters;
private EmguContours? _contours;
#endregion
#region Properties
/// <summary>
/// Gets or sets the parent SlicerFile
/// </summary>
public FileFormat SlicerFile { get; set; }
/// <summary>
/// Image resolution X
/// </summary>
public uint ResolutionX
{
get => _resolutionX;
set => RaiseAndSetIfChanged(ref _resolutionX, value);
}
/// <summary>
/// Image resolution Y
/// </summary>
public uint ResolutionY
{
get => _resolutionY;
set => RaiseAndSetIfChanged(ref _resolutionY, value);
}
/// <summary>
/// Image resolution
/// </summary>
public Size Resolution
{
get => new((int)ResolutionX, (int)ResolutionY);
set
{
ResolutionX = (uint)value.Width;
ResolutionY = (uint)value.Height;
RaisePropertyChanged();
}
}
/// <summary>
/// Gets the number of non zero pixels on this layer image
/// </summary>
public uint NonZeroPixelCount
{
get => _nonZeroPixelCount;
internal set
{
if (!RaiseAndSetIfChanged(ref _nonZeroPixelCount, value)) return;
RaisePropertyChanged(nameof(NonZeroPixelRatio));
RaisePropertyChanged(nameof(NonZeroPixelPercentage));
RaisePropertyChanged(nameof(Area));
RaisePropertyChanged(nameof(Volume));
MaterialMilliliters = -1; // Recalculate
}
}
/// <summary>
/// Gets the ratio between non zero pixels and display number of pixels
/// </summary>
public double NonZeroPixelRatio
{
get
{
var displayPixelCount = SlicerFile.DisplayPixelCount;
if (displayPixelCount == 0) return double.NaN;
return (double)_nonZeroPixelCount / displayPixelCount;
}
}
/// <summary>
/// Gets the percentage of non zero pixels relative to the display number of pixels
/// </summary>
public double NonZeroPixelPercentage
{
get
{
var pixelRatio = NonZeroPixelRatio;
if (double.IsNaN(pixelRatio)) return double.NaN;
return pixelRatio * 100.0;
}
}
/// <summary>
/// Gets if this layer is empty/all black pixels
/// </summary>
public bool IsEmpty => _nonZeroPixelCount == 0;
/// <summary>
/// Gets if this layer is a dummy layer to bypass a firmware constrain, that is contain at most one pixel and exposure time no more than 0.01s
/// </summary>
public bool IsDummy => _nonZeroPixelCount <= 1 || _exposureTime <= 0.01;
/// <summary>
/// Gets the layer area (XY) in mm^2
/// Pixel size * number of pixels
/// </summary>
public float Area => GetArea(3);
/// <summary>
/// Gets the layer volume (XYZ) in mm^3
/// Pixel size * number of pixels * layer height
/// </summary>
public float Volume => GetVolume(3);
/// <summary>
/// Gets the bounding rectangle for the image area
/// </summary>
public Rectangle BoundingRectangle
{
get => _boundingRectangle;
internal set
{
RaiseAndSetIfChanged(ref _boundingRectangle, value);
RaisePropertyChanged(nameof(BoundingRectangleMillimeters));
}
}
/// <summary>
/// Gets the bounding rectangle for the image area in millimeters
/// </summary>
public RectangleF BoundingRectangleMillimeters
{
get
{
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));
}
}
/// <summary>
/// Gets the first pixel index on the <see cref="BoundingRectangle"/>
/// </summary>
public uint BoundingRectangleFirstPixelIndex => (uint)(BoundingRectangle.Y * ResolutionX + BoundingRectangle.X);
/// <summary>
/// Gets the last pixel index on the <see cref="BoundingRectangle"/>
/// </summary>
public uint BoundingRectangleLastPixelIndex => (uint)(BoundingRectangle.Bottom * ResolutionX + BoundingRectangle.Right);
/// <summary>
/// Gets the first pixel <see cref="Point"/> on the <see cref="BoundingRectangle"/>
/// </summary>
public Point BoundingRectangleFirstPixelPosition => BoundingRectangle.Location;
/// <summary>
/// Gets the last pixel <see cref="Point"/> on the <see cref="BoundingRectangle"/>
/// </summary>
public Point BoundingRectangleLastPixelPosition => new (BoundingRectangle.Right, BoundingRectangle.Bottom);
/// <summary>
/// Gets the first pixel index on this layer
/// </summary>
public uint FirstPixelIndex
{
get => _firstPixelIndex;
private set => RaiseAndSetIfChanged(ref _firstPixelIndex, value);
}
/// <summary>
/// Gets the last pixel index on this layer
/// </summary>
public uint LastPixelIndex
{
get => _lastPixelIndex;
private set => RaiseAndSetIfChanged(ref _lastPixelIndex, value);
}
/// <summary>
/// Gets the first pixel <see cref="Point"/> on this layer
/// </summary>
public Point FirstPixelPosition
{
get => _firstPixelPosition;
private set => RaiseAndSetIfChanged(ref _firstPixelPosition, value);
}
/// <summary>
/// Gets the last pixel <see cref="Point"/> on this layer
/// </summary>
public Point LastPixelPosition
{
get => _lastPixelPosition;
private set => RaiseAndSetIfChanged(ref _lastPixelPosition, value);
}
/// <summary>
/// Gets if is the first layer
/// </summary>
public bool IsFirstLayer => _index == 0;
/// <summary>
/// Gets if layer is between first and last layer, aka, not first nor last layer
/// </summary>
public bool IsIntermediateLayer => !IsFirstLayer && !IsLastLayer;
/// <summary>
/// Gets if is the last layer
/// </summary>
public bool IsLastLayer => _index >= SlicerFile.LastLayerIndex;
/// <summary>
/// Gets if is in the bottom layer group
/// </summary>
public bool IsBottomLayer
{
get
{
var bottomLayers = SlicerFile.BottomLayerCount;
if (_index < bottomLayers) return true;
// For same positioned layers
/*uint layerCount = 1;
bool nullFallback = false;
for (uint layerIndex = 1; layerIndex < _index && layerCount < bottomLayers; layerIndex++)
{
if (SlicerFile[layerIndex] is null)
{
nullFallback = true;
break;
}
if (SlicerFile[layerIndex].RelativePositionZ != 0) layerCount++;
}
if (nullFallback) return PositionZ / SlicerFile.LayerHeight <= bottomLayers;
return layerCount <= bottomLayers;*/
return PositionZ / SlicerFile.LayerHeight <= bottomLayers;
}
}
/// <summary>
/// Gets if is in the normal layer group
/// </summary>
public bool IsNormalLayer => !IsBottomLayer;
/// <summary>
/// Gets if this layer is also an transition layer
/// </summary>
public bool IsTransitionLayer => SlicerFile.TransitionLayerCount > 0 &&
Index >= SlicerFile.BottomLayerCount && Index < SlicerFile.BottomLayerCount + SlicerFile.TransitionLayerCount;
/// <summary>
/// Gets the previous layer, returns null if no previous layer
/// </summary>
public Layer? PreviousLayer
{
get
{
if (IsFirstLayer || _index > SlicerFile.Count) return null;
return SlicerFile[_index - 1];
}
}
/// <summary>
/// Gets the previous layer if available, otherwise return the calling layer itself
/// </summary>
public Layer PreviousLayerOrThis
{
get
{
if (IsFirstLayer || _index > SlicerFile.Count) return this;
return SlicerFile[_index - 1];
}
}
/// <summary>
/// Gets the previous layer with a different height from the current, returns null if no previous layer
/// </summary>
public Layer? PreviousHeightLayer
{
get
{
if (IsFirstLayer || _index > SlicerFile.Count) return null;
for (int i = (int)_index - 1; i >= 0; i--)
{
if (SlicerFile[i].PositionZ < _positionZ) return SlicerFile[i];
}
return null;
}
}
/// <summary>
/// Gets the previous layer matching at least <param name="numberOfPixels"/> pixels, returns null if no previous layer
/// </summary>
public Layer? GetPreviousLayerWithAtLeastPixelCountOf(uint numberOfPixels)
{
if (IsFirstLayer || _index > SlicerFile.Count) return null;
for (int i = (int)_index - 1; i >= 0; i--)
{
if (SlicerFile[i].NonZeroPixelCount >= numberOfPixels) return SlicerFile[i];
}
return null;
}
/// <summary>
/// Gets the next layer, returns null if no next layer
/// </summary>
public Layer? NextLayer
{
get
{
if (_index >= SlicerFile.LastLayerIndex) return null;
return SlicerFile[_index + 1];
}
}
/// <summary>
/// Gets the next layer if available, otherwise return the calling layer itself
/// </summary>
public Layer NextLayerOrThis
{
get
{
if (_index >= SlicerFile.LastLayerIndex) return this;
return SlicerFile[_index + 1];
}
}
/// <summary>
/// Gets the next layer with a different height from the current, returns null if no next layer
/// </summary>
public Layer? NextHeightLayer
{
get
{
if (_index >= SlicerFile.LastLayerIndex) return null;
for (var i = _index + 1; i < SlicerFile.LayerCount; i++)
{
if (SlicerFile[i].PositionZ > _positionZ) return SlicerFile[i];
}
return null;
}
}
/// <summary>
/// Gets the next layer matching at least <param name="numberOfPixels"/> pixels, returns null if no next layer
/// </summary>
public Layer? GetNextLayerWithAtLeastPixelCountOf(uint numberOfPixels)
{
if (_index >= SlicerFile.LastLayerIndex) return null;
for (var i = _index + 1; i < SlicerFile.LayerCount; i++)
{
if (SlicerFile[i].NonZeroPixelCount >= numberOfPixels) return SlicerFile[i];
}
return null;
}
/// <summary>
/// Gets the layer index
/// </summary>
public uint Index
{
get => _index;
set
{
if(!RaiseAndSetIfChanged(ref _index, value)) return;
RaisePropertyChanged(nameof(Number));
}
}
/// <summary>
/// Gets the layer number, 1 started
/// </summary>
public uint Number => _index + 1;
/// <summary>
/// Gets or sets the absolute layer position on Z in mm
/// </summary>
public float PositionZ
{
get => _positionZ;
set
{
//if (value < 0) throw new ArgumentOutOfRangeException(nameof(PositionZ), "Value can't be negative");
if (!RaiseAndSetIfChanged(ref _positionZ, RoundHeight(value))) return;
RaisePropertyChanged(nameof(RelativePositionZ));
RaisePropertyChanged(nameof(LayerHeight));
//MaterialMilliliters = -1; // Recalculate
}
}
/// <summary>
/// Gets the relative layer position on Z in mm (Relative to the previous layer)
/// </summary>
public float RelativePositionZ
{
get
{
var previousLayer = PreviousLayer;
return previousLayer is null ? _positionZ : RoundHeight(_positionZ - previousLayer.PositionZ);
}
set => PositionZ = _positionZ - RelativePositionZ + value;
}
/// <summary>
/// Gets or sets the wait time in seconds before cure the layer
/// AKA: Light-off delay
/// Chitubox: Rest time after retract
/// Lychee: Wait before print
/// </summary>
public float WaitTimeBeforeCure
{
get => _waitTimeBeforeCure;
set
{
value = (float)Math.Round(value, 2);
if (value < 0) value = SlicerFile.GetBottomOrNormalValue(this, SlicerFile.BottomWaitTimeBeforeCure, SlicerFile.WaitTimeBeforeCure);
if (!RaiseAndSetIfChanged(ref _waitTimeBeforeCure, value)) return;
SlicerFile.UpdatePrintTimeQueued();
}
}
/// <summary>
/// Gets or sets the exposure time in seconds
/// </summary>
public float ExposureTime
{
get => _exposureTime;
set
{
value = (float)Math.Round(value, 2);
if (value < 0) value = SlicerFile.GetBottomOrNormalValue(this, SlicerFile.BottomExposureTime, SlicerFile.ExposureTime);
if(!RaiseAndSetIfChanged(ref _exposureTime, value)) return;
SlicerFile.UpdatePrintTimeQueued();
}
}
/// <summary>
/// Gets or sets the wait time in seconds after cure the layer
/// Chitubox: Rest time before lift
/// Lychee: Wait after print
/// </summary>
public float WaitTimeAfterCure
{
get => _waitTimeAfterCure;
set
{
value = (float)Math.Round(value, 2);
if (value < 0) value = SlicerFile.GetBottomOrNormalValue(this, SlicerFile.BottomWaitTimeAfterCure, SlicerFile.WaitTimeAfterCure);
if (!RaiseAndSetIfChanged(ref _waitTimeAfterCure, value)) return;
SlicerFile.UpdatePrintTimeQueued();
}
}
/// <summary>
/// Gets or sets the layer off time in seconds
/// </summary>
public float LightOffDelay
{
get => _lightOffDelay;
set
{
value = (float)Math.Round(value, 2);
if (value < 0) value = SlicerFile.GetBottomOrNormalValue(this, SlicerFile.BottomLightOffDelay, SlicerFile.LightOffDelay);
if(!RaiseAndSetIfChanged(ref _lightOffDelay, value)) return;
SlicerFile.UpdatePrintTimeQueued();
}
}
/// <summary>
/// Gets: Total lift height (lift1 + lift2)
/// Sets: Lift1 with value and lift2 with 0
/// </summary>
public float LiftHeightTotal
{
get => (float)Math.Round(_liftHeight + _liftHeight2, 2);
set
{
LiftHeight = (float)Math.Round(value, 2);
LiftHeight2 = 0;
}
}
/// <summary>
/// Gets or sets the lift height in mm
/// </summary>
public float LiftHeight
{
get => _liftHeight;
set
{
value = (float)Math.Round(value, 2);
if (value < 0) value = SlicerFile.GetBottomOrNormalValue(this, SlicerFile.BottomLiftHeight, SlicerFile.LiftHeight);
if(!RaiseAndSetIfChanged(ref _liftHeight, value)) return;
RaisePropertyChanged(nameof(LiftHeightTotal));
RetractHeight2 = _retractHeight2; // Sanitize
SlicerFile.UpdatePrintTimeQueued();
}
}
/// <summary>
/// Gets or sets the speed in mm/min
/// </summary>
public float LiftSpeed
{
get => _liftSpeed;
set
{
value = (float)Math.Round(value, 2);
if (value <= 0) value = SlicerFile.GetBottomOrNormalValue(this, SlicerFile.BottomLiftSpeed, SlicerFile.LiftSpeed);
if(!RaiseAndSetIfChanged(ref _liftSpeed, value)) return;
SlicerFile.UpdatePrintTimeQueued();
}
}
/// <summary>
/// Gets or sets the lift height in mm
/// </summary>
public float LiftHeight2
{
get => _liftHeight2;
set
{
value = (float)Math.Round(value, 2);
if (value < 0) value = SlicerFile.GetBottomOrNormalValue(this, SlicerFile.BottomLiftHeight2, SlicerFile.LiftHeight2);
if (!RaiseAndSetIfChanged(ref _liftHeight2, value)) return;
RaisePropertyChanged(nameof(LiftHeightTotal));
RetractHeight2 = _retractHeight2; // Sanitize
SlicerFile.UpdatePrintTimeQueued();
}
}
/// <summary>
/// Gets or sets the speed in mm/min
/// </summary>
public float LiftSpeed2
{
get => _liftSpeed2;
set
{
value = (float)Math.Round(value, 2);
if (value <= 0) value = SlicerFile.GetBottomOrNormalValue(this, SlicerFile.BottomLiftSpeed2, SlicerFile.LiftSpeed2);
if (!RaiseAndSetIfChanged(ref _liftSpeed2, value)) return;
SlicerFile.UpdatePrintTimeQueued();
}
}
public float WaitTimeAfterLift
{
get => _waitTimeAfterLift;
set
{
value = (float)Math.Round(value, 2);
if (value < 0) value = SlicerFile.GetBottomOrNormalValue(this, SlicerFile.BottomWaitTimeAfterLift, SlicerFile.WaitTimeAfterLift);
if (!RaiseAndSetIfChanged(ref _waitTimeAfterLift, value)) return;
SlicerFile.UpdatePrintTimeQueued();
}
}
/// <summary>
/// Gets: Total retract height (retract1 + retract2) alias of <see cref="LiftHeightTotal"/>
/// </summary>
public float RetractHeightTotal => LiftHeightTotal;
/// <summary>
/// Gets the retract height in mm
/// </summary>
public float RetractHeight => (float)Math.Round(LiftHeightTotal - _retractHeight2, 2);
/// <summary>
/// Gets the speed in mm/min for the retracts
/// </summary>
public float RetractSpeed
{
get => _retractSpeed;
set
{
value = (float)Math.Round(value, 2);
if (value <= 0) value = SlicerFile.GetBottomOrNormalValue(this, SlicerFile.BottomRetractSpeed, SlicerFile.RetractSpeed);
if (!RaiseAndSetIfChanged(ref _retractSpeed, value)) return;
SlicerFile.UpdatePrintTimeQueued();
}
}
/// <summary>
/// Gets or sets the second retract height in mm
/// </summary>
public virtual float RetractHeight2
{
get => _retractHeight2;
set
{
value = Math.Clamp((float)Math.Round(value, 2), 0, RetractHeightTotal);
RaiseAndSetIfChanged(ref _retractHeight2, value);
RaisePropertyChanged(nameof(RetractHeight));
RaisePropertyChanged(nameof(RetractHeightTotal));
SlicerFile.UpdatePrintTimeQueued();
}
}
/// <summary>
/// Gets the speed in mm/min for the retracts
/// </summary>
public virtual float RetractSpeed2
{
get => _retractSpeed2;
set
{
value = (float)Math.Round(value, 2);
if (value <= 0) value = SlicerFile.GetBottomOrNormalValue(this, SlicerFile.BottomRetractSpeed2, SlicerFile.RetractSpeed2);
if (!RaiseAndSetIfChanged(ref _retractSpeed2, value)) return;
SlicerFile.UpdatePrintTimeQueued();
}
}
/// <summary>
/// Gets or sets the pwm value from 0 to 255
/// </summary>
public byte LightPWM
{
get => _lightPWM;
set
{
//if (value == 0) value = SlicerFile.GetInitialLayerValueOrNormal(Index, SlicerFile.BottomLightPWM, SlicerFile.LightPWM);
//if (value == 0) value = FileFormat.DefaultLightPWM;
RaiseAndSetIfChanged(ref _lightPWM, value);
}
}
/// <summary>
/// Gets the minimum used speed in mm/min
/// </summary>
public float MinimumSpeed
{
get
{
float speed = float.MaxValue;
if (LiftSpeed > 0) speed = Math.Min(speed, LiftSpeed);
if (LiftSpeed2 > 0) speed = Math.Min(speed, LiftSpeed2);
if (RetractSpeed > 0) speed = Math.Min(speed, RetractSpeed);
if (RetractSpeed2 > 0) speed = Math.Min(speed, RetractSpeed2);
if (Math.Abs(speed - float.MaxValue) < 0.01) return 0;
return speed;
}
}
/// <summary>
/// Gets the maximum used speed in mm/min
/// </summary>
public float MaximumSpeed
{
get
{
float speed = LiftSpeed;
speed = Math.Max(speed, LiftSpeed2);
speed = Math.Max(speed, RetractSpeed);
speed = Math.Max(speed, RetractSpeed2);
return speed;
}
}
/// <summary>
/// Gets if this layer can be exposed to UV light
/// </summary>
public bool CanExpose => _exposureTime > 0 && _lightPWM > 0;
/// <summary>
/// Gets if this layer should be exposed to UV light, ie: if layer is empty or no exposure time then it useless to expose it
/// </summary>
public bool ShouldExpose => !IsEmpty && CanExpose;
/// <summary>
/// Gets the layer height in millimeters of this layer
/// </summary>
public float LayerHeight
{
get
{
if (IsFirstLayer) return _positionZ;
var previousLayer = this;
while ((previousLayer = previousLayer!.PreviousLayer) is not null) // This cycle returns the correct layer height if two or more layers have the same position z
{
var layerHeight = RoundHeight(_positionZ - previousLayer.PositionZ);
//Debug.WriteLine($"Layer {_index}-{previousLayer.Index}: {_positionZ} - {previousLayer.PositionZ}: {layerHeight}");
if (layerHeight == 0f) continue;
if (layerHeight < 0f) break;
return layerHeight;
}
return SlicerFile.LayerHeight;
}
}
/// <summary>
/// Gets the computed material milliliters spent on this layer
/// </summary>
public float MaterialMilliliters
{
get => _materialMilliliters;
set
{
if (SlicerFile is null) return;
//var globalMilliliters = SlicerFile.MaterialMilliliters - _materialMilliliters;
if (value < 0)
{
value = (float) Math.Round(GetVolume() / 1000f, 4);
}
if(!RaiseAndSetIfChanged(ref _materialMilliliters, value)) return;
RaisePropertyChanged(nameof(MaterialMillilitersPercent));
SlicerFile.MaterialMilliliters = -1; // Recalculate global
//ParentLayerManager.MaterialMillilitersTimer.Stop();
//if(!ParentLayerManager.MaterialMillilitersTimer.Enabled)
// ParentLayerManager.MaterialMillilitersTimer.Start();
}
}
/// <summary>
/// Gets the computed material milliliters percentage compared to the rest of the model
/// </summary>
public float MaterialMillilitersPercent => SlicerFile.MaterialMilliliters > 0 ? _materialMilliliters * 100 / SlicerFile.MaterialMilliliters : float.NaN;
/// <summary>
/// Gets or sets the compression method used to cache the image
/// </summary>
public LayerCompressionCodec CompressionCodec
{
get => _compressionCodec;
set
{
if (!HaveImage)
{
RaiseAndSetIfChanged(ref _compressionCodec, value);
return;
}
// Handle conversion
if (_compressionCodec == value) return;
using var mat = LayerMat;
_compressionCodec = value;
_compressedBytes = CompressMat(mat, value);
RaisePropertyChanged();
}
}
/// <summary>
/// Gets or sets layer image compressed data
/// </summary>
public byte[]? CompressedBytes
{
get => _compressedBytes;
set
{
_compressedBytes = value;
IsModified = true;
SlicerFile.BoundingRectangle = Rectangle.Empty;
_contours?.Dispose();
_contours = null;
RaisePropertyChanged();
RaisePropertyChanged(nameof(HaveImage));
}
}
public byte[]? CompressedPngBytes
{
get
{
if (_compressedBytes is null) return null;
if (_compressionCodec == LayerCompressionCodec.Png) return _compressedBytes;
using var mat = LayerMat;
return mat.GetPngByes();
}
}
/// <summary>
/// True if this layer have an valid initialized image, otherwise false
/// </summary>
public bool HaveImage => _compressedBytes is not null && _compressedBytes.Length > 0;
/// <summary>
/// Gets or sets a new image instance
/// </summary>
[XmlIgnore]
[JsonInclude]
public Mat LayerMat
{
get
{
if (!HaveImage) return null!;
Mat mat;
switch (_compressionCodec)
{
case LayerCompressionCodec.Png:
mat = new Mat();
CvInvoke.Imdecode(_compressedBytes, ImreadModes.Grayscale, mat);
break;
case LayerCompressionCodec.Lz4:
mat = new Mat(Resolution, DepthType.Cv8U, 1);
LZ4Codec.Decode(_compressedBytes.AsSpan(), mat.GetDataByteSpan());
break;
case LayerCompressionCodec.GZip:
{
mat = new Mat(Resolution, DepthType.Cv8U, 1);
unsafe
{
fixed (byte* pBuffer = _compressedBytes)
{
using var compressedStream = new UnmanagedMemoryStream(pBuffer, _compressedBytes!.Length);
using var matStream = mat.GetUnmanagedMemoryStream(FileAccess.Write);
using var gZipStream = new GZipStream(compressedStream, CompressionMode.Decompress);
gZipStream.CopyTo(matStream);
}
}
break;
}
case LayerCompressionCodec.Deflate:
{
mat = new Mat(Resolution, DepthType.Cv8U, 1);
unsafe
{
fixed (byte* pBuffer = _compressedBytes)
{
using var compressedStream = new UnmanagedMemoryStream(pBuffer, _compressedBytes!.Length);
using var matStream = mat.GetUnmanagedMemoryStream(FileAccess.Write);
using var deflateStream = new DeflateStream(compressedStream, CompressionMode.Decompress);
deflateStream.CopyTo(matStream);
}
}
break;
}
/*case LayerCompressionMethod.None:
mat = new Mat(Resolution, DepthType.Cv8U, 1);
//mat.SetBytes(_compressedBytes!);
_compressedBytes.CopyTo(mat.GetDataByteSpan());
break;*/
default:
throw new ArgumentOutOfRangeException(nameof(LayerMat));
}
return mat;
}
set
{
if (value is not null)
{
CompressedBytes = CompressMat(value, _compressionCodec);
_resolutionX = (uint)value.Width;
_resolutionY = (uint)value.Height;
}
else
{
_resolutionX = 0;
_resolutionY = 0;
}
GetBoundingRectangle(value, true);
RaisePropertyChanged();
}
}
/// <summary>
/// Gets the layer mat with roi of it bounding rectangle
/// </summary>
public MatRoi LayerMatBoundingRectangle => new(LayerMat, BoundingRectangle);
/// <summary>
/// Gets the layer mat with roi of model bounding rectangle
/// </summary>
public MatRoi LayerMatModelBoundingRectangle => new(LayerMat, SlicerFile.BoundingRectangle);
/// <summary>
/// Gets the layer mat with a specified roi
/// </summary>
/// <param name="roi">Region of interest</param>
/// <returns></returns>
public MatRoi GetLayerMat(Rectangle roi) => new(LayerMat, roi);
/// <summary>
/// Gets the layer mat with bounding rectangle mat
/// </summary>
/// <param name="margin">Margin from bounding rectangle</param>
/// <returns></returns>
public MatRoi GetLayerMatBoundingRectangle(int margin) => new(LayerMat, GetBoundingRectangle(margin));
/// <summary>
/// Gets the layer mat with bounding rectangle mat
/// </summary>
/// <param name="marginX">X margin from bounding rectangle</param>
/// <param name="marginY">Y margin from bounding rectangle</param>
/// <returns></returns>
public MatRoi GetLayerMatBoundingRectangle(int marginX, int marginY) => new(LayerMat, GetBoundingRectangle(marginX, marginY));
/// <summary>
/// Gets the layer mat with bounding rectangle mat
/// </summary>
/// <param name="margin">Margin from bounding rectangle</param>
/// <returns></returns>
public MatRoi GetLayerMatBoundingRectangle(Size margin) => new(LayerMat, GetBoundingRectangle(margin));
/// <summary>
/// Gets a new Brg image instance
/// </summary>