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

Layer.cs « Layer « UVtools.Core - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2939be43ed0a94246c85e15ca33ea5b5c8499b29 (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
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
/*
 *                     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.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using Emgu.CV;
using Emgu.CV.CvEnum;
using UVtools.Core.Extensions;
using UVtools.Core.FileFormats;
using UVtools.Core.Objects;
using UVtools.Core.Operations;
using Stream = System.IO.Stream;

namespace UVtools.Core
{
    /// <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 byte[] _compressedBytes;
        private uint _nonZeroPixelCount;
        private Rectangle _boundingRectangle = Rectangle.Empty;
        private bool _isModified;
        private uint _index;
        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;
        #endregion

        #region Properties

        /// <summary>
        /// Gets the parent layer manager
        /// </summary>
        public LayerManager ParentLayerManager { get; set; }

        public FileFormat SlicerFile => ParentLayerManager?.SlicerFile;

        /// <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(ExposureMillimeters));
                MaterialMilliliters = 0; // Recalculate
            }
        }

        /// <summary>
        /// Gets if this layer is empty/all black pixels
        /// </summary>
        public bool IsEmpty => _nonZeroPixelCount == 0;

        public float ExposureMillimeters
        {
            get
            {
                if (SlicerFile is null) return 0;
                return (float) Math.Round(SlicerFile.PixelSizeMax * _nonZeroPixelCount, 2);
            }
        }

        /// <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
            {
                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 bool IsBottomLayer => (uint)(PositionZ / SlicerFile.LayerHeight) <= ParentLayerManager.SlicerFile.BottomLayerCount;
        public bool IsNormalLayer => !IsBottomLayer;

        /// <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 layer position on Z in mm
        /// </summary>
        public float PositionZ
        {
            get => _positionZ;
            set
            {
                if (!RaiseAndSetIfChanged(ref _positionZ, RoundHeight(value))) return;
                RaisePropertyChanged(nameof(LayerHeight));
                MaterialMilliliters = 0; // Recalculate
            }
        }

        /// <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
            {
                if (!RaiseAndSetIfChanged(ref _waitTimeBeforeCure, (float)Math.Round(value, 2))) return;
                SlicerFile?.UpdatePrintTimeQueued();
            }
        }

        /// <summary>
        /// Gets or sets the exposure time in seconds
        /// </summary>
        public float ExposureTime
        {
            get => _exposureTime;
            set
            {
                if (value <= 0) value = SlicerFile.GetInitialLayerValueOrNormal(Index, SlicerFile.BottomExposureTime, SlicerFile.ExposureTime);
                if(!RaiseAndSetIfChanged(ref _exposureTime, (float)Math.Round(value, 2))) 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
            {
                if (!RaiseAndSetIfChanged(ref _waitTimeAfterCure, (float)Math.Round(value, 2))) return;
                SlicerFile?.UpdatePrintTimeQueued();
            }
        }

        /// <summary>
        /// Gets or sets the layer off time in seconds
        /// </summary>
        public float LightOffDelay
        {
            get => _lightOffDelay;
            set
            {
                if (value < 0) value = SlicerFile.GetInitialLayerValueOrNormal(Index, SlicerFile.BottomLightOffDelay, SlicerFile.LightOffDelay);
                if(!RaiseAndSetIfChanged(ref _lightOffDelay, (float)Math.Round(value, 2))) 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
            {
                if (value < 0) value = SlicerFile.GetInitialLayerValueOrNormal(Index, SlicerFile.BottomLiftHeight, SlicerFile.LiftHeight);
                if(!RaiseAndSetIfChanged(ref _liftHeight, (float)Math.Round(value, 2))) 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
            {
                if (value <= 0) value = SlicerFile.GetInitialLayerValueOrNormal(Index, SlicerFile.BottomLiftSpeed, SlicerFile.LiftSpeed);
                if(!RaiseAndSetIfChanged(ref _liftSpeed, (float)Math.Round(value, 2))) return;
                SlicerFile?.UpdatePrintTimeQueued();
            }
        }

        /// <summary>
        /// Gets or sets the lift height in mm
        /// </summary>
        public float LiftHeight2
        {
            get => _liftHeight2;
            set
            {
                if (value < 0) value = SlicerFile.GetInitialLayerValueOrNormal(Index, SlicerFile.BottomLiftHeight2, SlicerFile.LiftHeight2);
                if (!RaiseAndSetIfChanged(ref _liftHeight2, (float)Math.Round(value, 2))) 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
            {
                if (value <= 0) value = SlicerFile.GetInitialLayerValueOrNormal(Index, SlicerFile.BottomLiftSpeed2, SlicerFile.LiftSpeed2);
                if (!RaiseAndSetIfChanged(ref _liftSpeed2, (float)Math.Round(value, 2))) return;
                SlicerFile?.UpdatePrintTimeQueued();
            }
        }

        public float WaitTimeAfterLift
        {
            get => _waitTimeAfterLift;
            set
            {
                if (!RaiseAndSetIfChanged(ref _waitTimeAfterLift, (float)Math.Round(value, 2))) 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
            {
                if (value <= 0) value = SlicerFile.RetractSpeed;
                if(!RaiseAndSetIfChanged(ref _retractSpeed, (float)Math.Round(value, 2))) 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));
            }
        }

        /// <summary>
        /// Gets the speed in mm/min for the retracts
        /// </summary>
        public virtual float RetractSpeed2
        {
            get => _retractSpeed2;
            set => RaiseAndSetIfChanged(ref _retractSpeed2, (float)Math.Round(value, 2));
        }

        /// <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 if this layer can be exposed to UV light
        /// </summary>
        public bool CanExpose => _exposureTime > 0 && _lightPWM > 0;

        /// <summary>
        /// Gets the layer height in millimeters of this layer
        /// </summary>
        public float LayerHeight
        {
            get
            {
                if (_index == 0) 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;
            private set
            {
                if (SlicerFile is null) return;
                //var globalMilliliters = SlicerFile.MaterialMilliliters - _materialMilliliters;
                if (value <= 0)
                {
                    value = (float) Math.Round(SlicerFile.PixelArea * LayerHeight * NonZeroPixelCount / 1000f, 4);
                }

                if(!RaiseAndSetIfChanged(ref _materialMilliliters, value)) return;
                SlicerFile.MaterialMilliliters = 0; // Recalculate global
                RaisePropertyChanged(nameof(MaterialMillilitersPercent));
                //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 => _materialMilliliters * 100 / SlicerFile.MaterialMilliliters;

        /// <summary>
        /// Gets or sets layer image compressed data
        /// </summary>
        public byte[] CompressedBytes
        {
            get => _compressedBytes;
            set
            {
                _compressedBytes = value;
                IsModified = true;
                if (ParentLayerManager is not null)
                    ParentLayerManager.BoundingRectangle = Rectangle.Empty;
                RaisePropertyChanged();
                RaisePropertyChanged(nameof(HaveImage));
            }
        }

        /// <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>
        public Mat LayerMat
        {
            get
            {
                Mat mat = new();
                CvInvoke.Imdecode(_compressedBytes, ImreadModes.Grayscale, mat);
                return mat;
            }
            set
            {
                CompressedBytes = value.GetPngByes();
                GetBoundingRectangle(value, true);
                RaisePropertyChanged();
            }
        }

        public Mat LayerMatBoundingRectangle => new(LayerMat, BoundingRectangle);

        /// <summary>
        /// Gets a new Brg image instance
        /// </summary>
        public Mat BrgMat
        {
            get
            {
                var mat = LayerMat;
                CvInvoke.CvtColor(mat, mat, ColorConversion.Gray2Bgr);
                return mat;
            }
        }

        /// <summary>
        /// Gets a computed layer filename, padding zeros are equal to layer count digits
        /// </summary>
        public string Filename => FormatFileName("layer");

        /// <summary>
        /// Gets if layer image has been modified
        /// </summary>
        public bool IsModified
        {
            get => _isModified;
            set => RaiseAndSetIfChanged(ref _isModified, value);
        }

        /// <summary>
        /// Gets if this layer have same value parameters as global settings
        /// </summary>
        public bool IsUsingGlobalParameters
        {
            get
            {
                if (SlicerFile is null) return false; // Cant verify
                if (IsBottomLayer)
                {
                    if (
                        _positionZ != RoundHeight(SlicerFile.LayerHeight * Number) ||
                        _lightOffDelay != SlicerFile.BottomLightOffDelay ||
                        _waitTimeBeforeCure != SlicerFile.BottomWaitTimeBeforeCure ||
                        _exposureTime != SlicerFile.BottomExposureTime ||
                        _waitTimeAfterCure != SlicerFile.BottomWaitTimeAfterCure ||
                        _liftHeight != SlicerFile.BottomLiftHeight ||
                        _liftSpeed != SlicerFile.BottomLiftSpeed ||
                        _liftHeight2 != SlicerFile.BottomLiftHeight2 ||
                        _liftSpeed2 != SlicerFile.BottomLiftSpeed2 ||
                        _waitTimeAfterLift != SlicerFile.BottomWaitTimeAfterLift ||
                        _retractSpeed != SlicerFile.BottomRetractSpeed ||
                        _retractHeight2 != SlicerFile.BottomRetractHeight2 ||
                        _retractSpeed2 != SlicerFile.BottomRetractSpeed2 ||
                        _lightPWM != SlicerFile.BottomLightPWM 
                        ) 
                        return false;
                }
                else
                {
                    if (
                        _positionZ != RoundHeight(SlicerFile.LayerHeight * Number) ||
                        _lightOffDelay != SlicerFile.LightOffDelay ||
                        _waitTimeBeforeCure != SlicerFile.WaitTimeBeforeCure ||
                        _exposureTime != SlicerFile.ExposureTime ||
                        _waitTimeAfterCure != SlicerFile.WaitTimeAfterCure ||
                        _liftHeight != SlicerFile.LiftHeight ||
                        _liftSpeed != SlicerFile.LiftSpeed ||
                        _liftHeight2 != SlicerFile.LiftHeight2 ||
                        _liftSpeed2 != SlicerFile.LiftSpeed2 ||
                        _waitTimeAfterLift != SlicerFile.WaitTimeAfterLift ||
                        _retractSpeed != SlicerFile.RetractSpeed ||
                        _retractHeight2 != SlicerFile.RetractHeight2 ||
                        _retractSpeed2 != SlicerFile.RetractSpeed2 ||
                        _lightPWM != SlicerFile.LightPWM
                    ) 
                        return false;
                }

                return true;
            }
        }

        /// <summary>
        /// True if this layer is using TSMC values, otherwise false
        /// </summary>
        public bool IsUsingTSMC => LiftHeight2 > 0 || RetractHeight2 > 0;

        #endregion

        #region Constructor

        public Layer(uint index, LayerManager parentLayerManager)
        {
            ParentLayerManager = parentLayerManager;
            _index = index;

            if (parentLayerManager is null) return;
            _positionZ = SlicerFile.GetHeightFromLayer(index);
            _lightOffDelay = SlicerFile.GetInitialLayerValueOrNormal(index, SlicerFile.BottomLightOffDelay, SlicerFile.LightOffDelay);
            _waitTimeBeforeCure = SlicerFile.GetInitialLayerValueOrNormal(index, SlicerFile.BottomWaitTimeBeforeCure, SlicerFile.WaitTimeBeforeCure);
            _exposureTime = SlicerFile.GetInitialLayerValueOrNormal(index, SlicerFile.BottomExposureTime, SlicerFile.ExposureTime);
            _waitTimeAfterCure = SlicerFile.GetInitialLayerValueOrNormal(index, SlicerFile.BottomWaitTimeAfterCure, SlicerFile.WaitTimeAfterCure);
            _liftHeight = SlicerFile.GetInitialLayerValueOrNormal(index, SlicerFile.BottomLiftHeight, SlicerFile.LiftHeight);
            _liftSpeed = SlicerFile.GetInitialLayerValueOrNormal(index, SlicerFile.BottomLiftSpeed, SlicerFile.LiftSpeed);
            _liftHeight2 = SlicerFile.GetInitialLayerValueOrNormal(index, SlicerFile.BottomLiftHeight2, SlicerFile.LiftHeight2);
            _liftSpeed2 = SlicerFile.GetInitialLayerValueOrNormal(index, SlicerFile.BottomLiftSpeed2, SlicerFile.LiftSpeed2);
            _waitTimeAfterLift = SlicerFile.GetInitialLayerValueOrNormal(index, SlicerFile.BottomWaitTimeAfterLift, SlicerFile.WaitTimeAfterLift);
            _retractSpeed = SlicerFile.GetInitialLayerValueOrNormal(index, SlicerFile.BottomRetractSpeed, SlicerFile.RetractSpeed);
            _retractHeight2 = SlicerFile.GetInitialLayerValueOrNormal(index, SlicerFile.BottomRetractHeight2, SlicerFile.RetractHeight2);
            _retractSpeed2 = SlicerFile.GetInitialLayerValueOrNormal(index, SlicerFile.BottomRetractSpeed2, SlicerFile.RetractSpeed2);
            _lightPWM = SlicerFile.GetInitialLayerValueOrNormal(index, SlicerFile.BottomLightPWM, SlicerFile.LightPWM);
        }

        public Layer(uint index, byte[] compressedBytes, LayerManager parentLayerManager) : this(index, parentLayerManager)
        {
            CompressedBytes = compressedBytes;
            _isModified = false;
            /*if (compressedBytes.Length > 0)
            {
                GetBoundingRectangle();
            }*/
        }

        public Layer(uint index, byte[] compressedBytes, FileFormat slicerFile) : this(index, compressedBytes, slicerFile.LayerManager)
        {}

        public Layer(uint index, Mat layerMat, LayerManager parentLayerManager) : this(index, parentLayerManager)
        {
            LayerMat = layerMat;
            _isModified = false;
        }

        public Layer(uint index, Mat layerMat, FileFormat slicerFile) : this(index, layerMat, slicerFile.LayerManager) { }

        public Layer(uint index, Stream stream, LayerManager parentLayerManager) : this(index, stream.ToArray(), parentLayerManager) { }
        public Layer(uint index, Stream stream, FileFormat slicerFile) : this(index, stream.ToArray(), slicerFile.LayerManager) { }
        #endregion

        #region Equatables

        public static bool operator ==(Layer obj1, Layer obj2)
        {
            return obj1.Equals(obj2);
        }

        public static bool operator !=(Layer obj1, Layer obj2)
        {
            return !obj1.Equals(obj2);
        }

        public static bool operator >(Layer obj1, Layer obj2)
        {
            return obj1.Index > obj2.Index;
        }

        public static bool operator <(Layer obj1, Layer obj2)
        {
            return obj1.Index < obj2.Index;
        }

        public static bool operator >=(Layer obj1, Layer obj2)
        {
            return obj1.Index >= obj2.Index;
        }

        public static bool operator <=(Layer obj1, Layer obj2)
        {
            return obj1.Index <= obj2.Index;
        }

        public bool Equals(uint other)
        {
            return Index == other;
        }

        public bool Equals(Layer other)
        {
            if (other is null) return false;
            if (ReferenceEquals(this, other)) return true;
            if (_index != other._index) return false;
            if (_compressedBytes.Length != other._compressedBytes.Length) return false;
            return _compressedBytes.AsSpan().SequenceEqual(other._compressedBytes.AsSpan());
            //return Equals(_compressedBytes, other._compressedBytes);
        }

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

        public override int GetHashCode()
        {
            return (_compressedBytes != null ? _compressedBytes.GetHashCode() : 0);
        }

        private sealed class IndexRelationalComparer : IComparer<Layer>
        {
            public int Compare(Layer x, Layer y)
            {
                if (ReferenceEquals(x, y)) return 0;
                if (ReferenceEquals(null, y)) return 1;
                if (ReferenceEquals(null, x)) return -1;
                return x.Index.CompareTo(y.Index);
            }
        }

        public static IComparer<Layer> IndexComparer { get; } = new IndexRelationalComparer();
        #endregion

        #region Formaters

        public override string ToString()
        {
            return $"{nameof(Index)}: {Index}, " +
                   $"{nameof(Filename)}: {Filename}, " +
                   $"{nameof(NonZeroPixelCount)}: {NonZeroPixelCount}, " +
                   $"{nameof(BoundingRectangle)}: {BoundingRectangle}, " +
                   $"{nameof(IsBottomLayer)}: {IsBottomLayer}, " +
                   $"{nameof(IsNormalLayer)}: {IsNormalLayer}, " +
                   $"{nameof(LayerHeight)}: {LayerHeight}mm, " +
                   $"{nameof(PositionZ)}: {PositionZ}mm, " +
                   $"{nameof(LightOffDelay)}: {LightOffDelay}s, " +
                   $"{nameof(WaitTimeBeforeCure)}: {WaitTimeBeforeCure}s, " +
                   $"{nameof(ExposureTime)}: {ExposureTime}s, " +
                   $"{nameof(WaitTimeAfterCure)}: {WaitTimeAfterCure}s, " +
                   $"{nameof(LiftHeight)}: {LiftHeight}mm, " +
                   $"{nameof(LiftSpeed)}: {LiftSpeed}mm/mim, " +
                   $"{nameof(LiftHeight2)}: {LiftHeight2}mm, " +
                   $"{nameof(LiftSpeed2)}: {LiftSpeed2}mm/mim, " +
                   $"{nameof(WaitTimeAfterLift)}: {WaitTimeAfterLift}s, " +
                   $"{nameof(RetractHeight)}: {RetractHeight}mm, " +
                   $"{nameof(RetractSpeed)}: {RetractSpeed}mm/mim, " +
                   $"{nameof(RetractHeight2)}: {RetractHeight2}mm, " +
                   $"{nameof(RetractSpeed2)}: {RetractSpeed2}mm/mim, " +
                   $"{nameof(LightPWM)}: {LightPWM}, " +
                   $"{nameof(IsModified)}: {IsModified}, " +
                   $"{nameof(IsUsingGlobalParameters)}: {IsUsingGlobalParameters}";
        }
        #endregion

        #region Methods

        public float CalculateMotorMovementTime(float extraTime = 0)
        {
            return OperationCalculator.LightOffDelayC.CalculateSeconds(this, extraTime);
        }

        public float CalculateLightOffDelay(float extraTime = 0)
        {
            if (SlicerFile is null) return OperationCalculator.LightOffDelayC.CalculateSeconds(this, extraTime);
            return SlicerFile.SupportsGCode ? extraTime : OperationCalculator.LightOffDelayC.CalculateSeconds(this, extraTime);
        }

        public void SetLightOffDelay(float extraTime = 0)
        {
            LightOffDelay = CalculateLightOffDelay(extraTime);
        }

        /// <summary>
        /// Zero all 'wait times / delays' for this layer
        /// </summary>
        public void SetNoDelays()
        {
            LightOffDelay = 0;
            WaitTimeBeforeCure = 0;
            WaitTimeAfterCure = 0;
            WaitTimeAfterLift = 0;
        }

        public string FormatFileName(string prepend, byte padDigits, bool layerIndexZeroStarted = true, string appendExt = ".png")
        {
            var index = Index;
            if (!layerIndexZeroStarted)
            {
                index++;
            }
            return $"{prepend}{index.ToString().PadLeft(padDigits, '0')}{appendExt}";
        }

        public string FormatFileName(string prepend = "", bool layerIndexZeroStarted = true, string appendExt = ".png")
            => FormatFileName(prepend, ParentLayerManager.LayerDigits, layerIndexZeroStarted, appendExt);

        public string FormatFileName(byte padDigits, bool layerIndexZeroStarted = true, string appendExt = ".png")
            => FormatFileName(string.Empty, padDigits, layerIndexZeroStarted, appendExt);


        public Rectangle GetBoundingRectangle(Mat mat = null, bool reCalculate = false)
        {
            if (_nonZeroPixelCount > 0 && !reCalculate)
            {
                return BoundingRectangle;
            }
            bool needDispose = false;
            if (mat is null)
            {
                if (_compressedBytes is null || _compressedBytes.Length == 0) return Rectangle.Empty;
                mat = LayerMat;
                needDispose = true;
            }

            using var nonZeroMat = new Mat();
            CvInvoke.FindNonZero(mat, nonZeroMat);
            NonZeroPixelCount = (uint)nonZeroMat.Height;
            BoundingRectangle = NonZeroPixelCount > 0 ? CvInvoke.BoundingRectangle(nonZeroMat) : Rectangle.Empty;


            if (needDispose) mat.Dispose();

            return BoundingRectangle;
        }

        public Layer PreviousLayer()
        {
            if (ParentLayerManager is null || _index == 0 || _index > ParentLayerManager.Count) return null;
            return ParentLayerManager[_index - 1];
        }

        public Layer NextLayer()
        {
            if (ParentLayerManager is null || _index >= ParentLayerManager.LayerCount - 1) return null;
            return ParentLayerManager[_index + 1];
        }

        public bool SetValueFromPrintParameterModifier(FileFormat.PrintParameterModifier modifier, decimal value)
        {
            if (ReferenceEquals(modifier, FileFormat.PrintParameterModifier.LightOffDelay))
            {
                LightOffDelay = (float)value;
                return true;
            }

            if (ReferenceEquals(modifier, FileFormat.PrintParameterModifier.WaitTimeBeforeCure))
            {
                WaitTimeBeforeCure = (float)value;
                return true;
            }

            if (ReferenceEquals(modifier, FileFormat.PrintParameterModifier.ExposureTime))
            {
                ExposureTime = (float)value;
                return true;
            }

            if (ReferenceEquals(modifier, FileFormat.PrintParameterModifier.WaitTimeAfterCure))
            {
                WaitTimeAfterCure = (float)value;
                return true;
            }

            if (ReferenceEquals(modifier, FileFormat.PrintParameterModifier.LiftHeight))
            {
                LiftHeight = (float)value;
                return true;
            }

            if (ReferenceEquals(modifier, FileFormat.PrintParameterModifier.LiftSpeed))
            {
                LiftSpeed = (float)value;
                return true;
            }

            if (ReferenceEquals(modifier, FileFormat.PrintParameterModifier.LiftHeight2))
            {
                LiftHeight2 = (float)value;
                return true;
            }

            if (ReferenceEquals(modifier, FileFormat.PrintParameterModifier.LiftSpeed2))
            {
                LiftSpeed2 = (float)value;
                return true;
            }

            if (ReferenceEquals(modifier, FileFormat.PrintParameterModifier.WaitTimeAfterLift))
            {
                WaitTimeAfterLift = (float)value;
                return true;
            }

            if (ReferenceEquals(modifier, FileFormat.PrintParameterModifier.RetractSpeed))
            {
                RetractSpeed = (float)value;
                return true;
            }

            if (ReferenceEquals(modifier, FileFormat.PrintParameterModifier.RetractHeight2))
            {
                RetractHeight2 = (float)value;
                return true;
            }

            if (ReferenceEquals(modifier, FileFormat.PrintParameterModifier.RetractSpeed2))
            {
                RetractSpeed2 = (float)value;
                return true;
            }

            if (ReferenceEquals(modifier, FileFormat.PrintParameterModifier.LightPWM))
            {
                LightPWM = (byte)value;
                return true;
            }

            return false;
        }

        public byte SetValuesFromPrintParametersModifiers(FileFormat.PrintParameterModifier[] modifiers)
        {
            if (modifiers is null) return 0;
            byte changed = 0;
            foreach (var modifier in modifiers)
            {
                if (!modifier.HasChanged) continue;
                SetValueFromPrintParameterModifier(modifier, modifier.NewValue);
                changed++;
            }

            return changed;
        }

        /*
        /// <summary>
        /// Gets all islands start pixel location for this layer
        /// https://www.geeksforgeeks.org/find-number-of-islands/
        /// </summary>
        /// <returns><see cref="List{T}"/> holding all islands coordinates</returns>
        public List<LayerIssue> GetIssues(uint requiredPixelsToSupportIsland = 5)
        {
            if (requiredPixelsToSupportIsland == 0)
                requiredPixelsToSupportIsland = 1;

            // These arrays are used to 
            // get row and column numbers 
            // of 8 neighbors of a given cell 
            List<LayerIssue> result = new();
            List<Point> pixels = new();



            var mat = LayerMat;
            var bytes = mat.GetDataSpan<byte>();



            var previousLayerImage = PreviousLayer()?.LayerMat;
            var previousBytes = previousLayerImage?.GetBytes();

            // Make a bool array to
            // mark visited cells. 
            // Initially all cells 
            // are unvisited 
            bool[,] visited = new bool[mat.Width, mat.Height];

            // Initialize count as 0 and 
            // traverse through the all 
            // cells of given matrix 
            //uint count = 0;

            // Island checker
            sbyte[] rowNbr = { -1, -1, -1, 0, 0, 1, 1, 1 };
            sbyte[] colNbr = { -1, 0, 1, -1, 1, -1, 0, 1 };
            const uint minPixel = 10;
            const uint minPixelForSupportIsland = 200;
            int pixelIndex;
            uint islandSupportingPixels;
            if (Index > 0)
            {
                for (int y = 0; y < mat.Height; y++)
                {
                    for (int x = 0; x < mat.Width; x++)
                    {
                        pixelIndex = y * mat.Width + x;

                        if (bytes[pixelIndex] > minPixel && !visited[x, y])
                        {
                            // If a cell with value 1 is not 
                            // visited yet, then new island 
                            // found, Visit all cells in this 
                            // island and increment island count 
                            pixels.Clear();
                            pixels.Add(new Point(x, y));
                            islandSupportingPixels = previousBytes[pixelIndex] >= minPixelForSupportIsland ? 1u : 0;

                            int minX = x;
                            int maxX = x;
                            int minY = y;
                            int maxY = y;

                            int x2;
                            int y2;


                            Queue<Point> queue = new();
                            queue.Enqueue(new Point(x, y));
                            // Mark this cell as visited 
                            visited[x, y] = true;

                            while (queue.Count > 0)
                            {
                                var point = queue.Dequeue();
                                y2 = point.Y;
                                x2 = point.X;
                                for (byte k = 0; k < 8; k++)
                                {
                                    //if (isSafe(y2 + rowNbr[k], x2 + colNbr[k]))
                                    var tempy2 = y2 + rowNbr[k];
                                    var tempx2 = x2 + colNbr[k];
                                    pixelIndex = tempy2 * mat.Width + tempx2;
                                    if (tempy2 >= 0 &&
                                        tempy2 < mat.Height &&
                                        tempx2 >= 0 && tempx2 < mat.Width &&
                                        bytes[pixelIndex] >= minPixel &&
                                        !visited[tempx2, tempy2])
                                    {
                                        visited[tempx2, tempy2] = true;
                                        point = new Point(tempx2, tempy2);
                                        pixels.Add(point);
                                        queue.Enqueue(point);

                                        minX = Math.Min(minX, tempx2);
                                        maxX = Math.Max(maxX, tempx2);
                                        minY = Math.Min(minY, tempy2);
                                        maxY = Math.Max(maxY, tempy2);

                                        islandSupportingPixels += previousBytes[pixelIndex] >= minPixelForSupportIsland ? 1u : 0;
                                    }
                                }
                            }
                            //count++;

                            if (islandSupportingPixels >= requiredPixelsToSupportIsland)
                                continue; // Not a island, bounding is strong
                            if (islandSupportingPixels > 0 && pixels.Count < requiredPixelsToSupportIsland &&
                                islandSupportingPixels >= Math.Max(1, pixels.Count / 2)) continue; // Not a island
                            result.Add(new LayerIssue(this, LayerIssue.IssueType.Island, pixels.ToArray(), new Rectangle(minX, minY, maxX - minX, maxY - minY)));
                        }
                    }
                }
            }

            pixels.Clear();

            // TouchingBounds Checker
            for (int x = 0; x < mat.Width; x++) // Check Top and Bottom bounds
            {
                if (bytes[x] >= 200) // Top
                {
                    pixels.Add(new Point(x, 0));
                }

                if (bytes[mat.Width * mat.Height - mat.Width + x] >= 200) // Bottom
                {
                    pixels.Add(new Point(x, mat.Height - 1));
                }
            }

            for (int y = 0; y < mat.Height; y++) // Check Left and Right bounds
            {
                if (bytes[y * mat.Width] >= 200) // Left
                {
                    pixels.Add(new Point(0, y));
                }

                if (bytes[y * mat.Width + mat.Width - 1] >= 200) // Right
                {
                    pixels.Add(new Point(mat.Width - 1, y));
                }
            }

            if (pixels.Count > 0)
            {
                result.Add(new LayerIssue(this, LayerIssue.IssueType.TouchingBound, pixels.ToArray()));
            }

            pixels.Clear();

            return result;
        }*/

        public void CopyImageTo(Layer layer)
        {
            if (!HaveImage) return;
            layer.CompressedBytes = _compressedBytes.ToArray();
            layer.BoundingRectangle = _boundingRectangle;
            layer.NonZeroPixelCount = _nonZeroPixelCount;
        }

        public Layer Clone()
        {
            //var layer = (Layer)MemberwiseClone();
            //layer.CompressedBytes = _compressedBytes.ToArray();
            //Debug.WriteLine(ReferenceEquals(_compressedBytes, layer.CompressedBytes));
            //return layer;
            return new (_index, CompressedBytes.ToArray(), ParentLayerManager)
            {
                _positionZ = _positionZ,
                _lightOffDelay = _lightOffDelay,
                _waitTimeBeforeCure = _waitTimeBeforeCure,
                _exposureTime = _exposureTime,
                _waitTimeAfterCure = _waitTimeAfterCure,
                _liftHeight = _liftHeight,
                _liftSpeed = _liftSpeed,
                _liftHeight2 = _liftHeight2,
                _liftSpeed2 = _liftSpeed2,
                _waitTimeAfterLift = _waitTimeAfterLift,
                _retractSpeed = _retractSpeed,
                _retractHeight2 = _retractHeight2,
                _retractSpeed2 = _retractSpeed2,
                _lightPWM = _lightPWM,
                _boundingRectangle = _boundingRectangle,
                _nonZeroPixelCount = _nonZeroPixelCount,
                _isModified = _isModified,
                _materialMilliliters = _materialMilliliters
            };
        }
        #endregion

        #region Static Methods

        public static float RoundHeight(float height) => (float) Math.Round(height, HeightPrecision);
        public static double RoundHeight(double height) => Math.Round(height, HeightPrecision);
        public static decimal RoundHeight(decimal height) => Math.Round(height, HeightPrecision);

        public static string ShowHeight(float height) => string.Format($"{{0:F{HeightPrecision}}}", height);
        public static string ShowHeight(double height) => string.Format($"{{0:F{HeightPrecision}}}", height);
        public static string ShowHeight(decimal height) => string.Format($"{{0:F{HeightPrecision}}}", height);

        public static Layer[] CloneLayers(Layer[] layers)
        {
            var clonedLayers = new Layer[layers.Length];
            for (uint layerIndex = 0; layerIndex < layers.Length; layerIndex++)
            {
                clonedLayers[layerIndex] = layers[layerIndex]?.Clone();
            }
            return clonedLayers;
        }

        #endregion
    }
}