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

OperationCalibrateExposureFinder.cs « Operations « UVtools.Core - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8430fe5bd2dbbeec1f347007d34f92b96a447623 (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
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
/*
 *                     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.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using MoreLinq;
using UVtools.Core.Extensions;
using UVtools.Core.FileFormats;
using UVtools.Core.Objects;

namespace UVtools.Core.Operations
{
    [Serializable]
    public sealed class OperationCalibrateExposureFinder : Operation
    {
        #region Subclasses

        public sealed class BullsEyeCircle
        {
            public ushort Diameter { get; set; }
            public ushort Radius => (ushort) (Diameter / 2);
            public ushort Thickness { get; set; } = 10;

            public BullsEyeCircle() {}

            public BullsEyeCircle(ushort diameter, ushort thickness)
            {
                Diameter = diameter;
                Thickness = thickness;
            }
        }
        #endregion

        #region Constants

        const byte TextMarkingSpacing = 60;
        const byte TextMarkingLineBreak = 30;
        const FontFace TextMarkingFontFace = Emgu.CV.CvEnum.FontFace.HersheyDuplex;
        const byte TextMarkingStartX = 10;
        //const byte TextStartY = 50;
        const double TextMarkingScale = 0.8;
        const byte TextMarkingThickness = 2;

        #endregion

        #region Members
        private decimal _displayWidth;
        private decimal _displayHeight;
        private decimal _layerHeight = 0.05M;
        private ushort _bottomLayers = 3;
        private decimal _bottomExposure = 60;
        private decimal _normalExposure = 12;
        private decimal _topBottomMargin = 5;
        private decimal _leftRightMargin = 10;
        private byte _chamferLayers = 0;
        private byte _erodeBottomIterations = 0;
        private decimal _partMargin = 0;
        private bool _enableAntiAliasing = false;
        private bool _mirrorOutput;
        private decimal _baseHeight = 1;
        private decimal _featuresHeight = 1;
        private decimal _featuresMargin = 2m;
        
        private ushort _staircaseThickness = 40;
        
        private bool _holesEnabled = false;
        private CalibrateExposureFinderShapes _holeShape = CalibrateExposureFinderShapes.Square;
        private Measures _unitOfMeasure = Measures.Pixels;
        private string _holeDiametersPx = "2, 3, 4, 5, 6, 7, 8, 9, 10, 11";
        private string _holeDiametersMm = "0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.2";

        private bool _barsEnabled = true;
        private decimal _barSpacing = 1.5m;
        private decimal _barLength = 4;
        private sbyte _barVerticalSplitter = 0;
        private byte _barFenceThickness = 10;
        private sbyte _barFenceOffset = 4;
        private string _barThicknessesPx = "4, 6, 8, 60"; //"4, 6, 8, 10, 12, 14, 16, 18, 20";
        private string _barThicknessesMm = "0.2, 0.3, 0.4, 3"; //"0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.2";

        private bool _textEnabled = true;
        private FontFace _textFont = TextMarkingFontFace;
        private double _textScale = 1;
        private byte _textThickness = 2;
        private string _text = "ABHJQRWZ%&#"; //"ABGHJKLMQRSTUVWXZ%&#";

        private bool _multipleBrightness;
        private CalibrateExposureFinderMultipleBrightnessExcludeFrom _multipleBrightnessExcludeFrom = CalibrateExposureFinderMultipleBrightnessExcludeFrom.BottomAndBase;
        private string _multipleBrightnessValues = "255, 242, 230, 217, 204, 191";
        private decimal _multipleBrightnessGenExposureTime;


        private bool _multipleLayerHeight;
        private decimal _multipleLayerHeightMaximum = 0.1m;
        private decimal _multipleLayerHeightStep = 0.01m;

        private bool _dontLiftSamePositionedLayers;
        private bool _zeroLightOffSamePositionedLayers;
        private bool _multipleExposures;
        private ExposureGenTypes _exposureGenType = ExposureGenTypes.Linear;
        private bool _exposureGenIgnoreBaseExposure;
        private decimal _exposureGenBottomStep = 0;
        private decimal _exposureGenNormalStep = 0.2m;
        private byte _exposureGenTests = 4;
        private decimal _exposureGenManualLayerHeight;
        private decimal _exposureGenManualBottom;
        private decimal _exposureGenManualNormal;
        private RangeObservableCollection<ExposureItem> _exposureTable = new();

        private bool _bullsEyeEnabled = true;
        private string _bullsEyeConfigurationPx = "26:5, 60:10, 116:15, 190:20";
        private string _bullsEyeConfigurationMm = "1.3:0.25, 3:0.5, 5.8:0.75, 9.5:1";
        private bool _bullsEyeInvertQuadrants = true;

        private bool _counterTrianglesEnabled = true;
        private sbyte _counterTrianglesTipOffset = 3;
        private bool _counterTrianglesFence = false;

        private bool _patternModel;
        private byte _bullsEyeFenceThickness = 10;
        private sbyte _bullsEyeFenceOffset;
        private bool _patternModelGlueBottomLayers = true;

        #endregion

        #region Overrides

        public override bool CanROI => false;

        public override Enumerations.LayerRangeSelection StartLayerRangeSelection => Enumerations.LayerRangeSelection.None;

        public override string Title => "Exposure time finder";
        public override string Description =>
            "Generates test models with various strategies and increments to verify the best exposure time for a given layer height.\n" +
            "You must repeat this test when change any of the following: printer, LEDs, resin and exposure times.\n" +
            "Note: The current opened file will be overwritten with this test, use a dummy or a not needed file.";

        public override string ConfirmationText =>
            $"generate the exposure time finder test?";

        public override string ProgressTitle =>
            $"Generating the exposure time finder test";

        public override string ProgressAction => "Generated layers";

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

            if (_displayWidth <= 0)
            {
                sb.AppendLine("Display width must be a positive value.");
            }

            if (_displayHeight <= 0)
            {
                sb.AppendLine("Display height must be a positive value.");
            }

            if (_chamferLayers * _layerHeight > _baseHeight)
            {
                sb.AppendLine("The chamfer can't be higher than the base height, lower the chamfer layer count.");
            }

            if (_multipleExposures)
            {
                var endLayerHeight = _multipleLayerHeight ? _multipleLayerHeightMaximum : _layerHeight;
                for (decimal layerHeight = _layerHeight;
                    layerHeight <= endLayerHeight;
                    layerHeight += _multipleLayerHeightStep)
                {
                    bool found = false;
                    foreach (var exposureItem in _exposureTable)
                    {
                        if (exposureItem.LayerHeight == layerHeight && exposureItem.IsValid)
                        {
                            found = true;
                            break;
                        }
                    }
                    if(!found)
                        sb.AppendLine($"[ME]: The {Layer.ShowHeight(layerHeight)}mm layer height have no set exposure(s).");
                }
            }

            if (_multipleBrightness)
            {
                if (MultipleBrightnessValuesArray.Length == 0)
                {
                    sb.AppendLine($"Multiple brightness tests are enabled but no valid values are set, use from 1 to 255.");
                }
            }

            if (_patternModel)
            {
                if (!CanPatternModel)
                {
                    sb.AppendLine($"Unable to pattern the loaded model within the available space.");
                }

                if (!_multipleBrightness && !_multipleExposures)
                {
                    sb.AppendLine($"Pattern the loaded model requires either multiple brightness or multiple exposures to use with.");
                }
            }
            else
            {
                if (Bars.Length <= 0 && Holes.Length <= 0 && BullsEyes.Length <= 0 && TextSize.IsEmpty)
                {
                    sb.AppendLine("No objects to output, enable at least 1 feature.");
                }
            }

            return sb.ToString();
        }

        public override string ToString()
        {
            var result = $"[Layer Height: {_layerHeight}] " +
                         $"[Bottom layers: {_bottomLayers}] " +
                         $"[Exposure: {_bottomExposure}/{_normalExposure}] " +
                         $"[TB:{_topBottomMargin} LR:{_leftRightMargin} PM:{_partMargin} FM:{_featuresMargin}]  " +
                         $"[Chamfer: {_chamferLayers}] [Erode: {_erodeBottomIterations}] " +
                         $"[Obj height: {_featuresHeight}] " +
                         $"[Holes: {Holes.Length}] [Bars: {Bars.Length}] [BE: {BullsEyes.Length}] [Text: {!string.IsNullOrWhiteSpace(_text)}]" +
                         $"[AA: {_enableAntiAliasing}] [Mirror: {_mirrorOutput}]";
            if (!string.IsNullOrEmpty(ProfileName)) result = $"{ProfileName}: {result}";
            return result;
        }

        #endregion

        #region Properties

        public decimal DisplayWidth
        {
            get => _displayWidth;
            set
            {
                if(!RaiseAndSetIfChanged(ref _displayWidth, Math.Round(value, 2))) return;
                RaisePropertyChanged(nameof(Xppmm));
            }
        }

        public decimal DisplayHeight
        {
            get => _displayHeight;
            set
            {
                if(!RaiseAndSetIfChanged(ref _displayHeight, Math.Round(value, 2))) return;
                RaisePropertyChanged(nameof(Yppmm));
            }
        }

        public decimal Xppmm => DisplayWidth > 0 ? Math.Round(SlicerFile.Resolution.Width / DisplayWidth, 2) : 0;
        public decimal Yppmm => DisplayHeight > 0 ? Math.Round(SlicerFile.Resolution.Height / DisplayHeight, 2) : 0;
        public decimal Ppmm => Math.Max(Xppmm, Yppmm);

        public decimal LayerHeight
        {
            get => _layerHeight;
            set
            {
                if(!RaiseAndSetIfChanged(ref _layerHeight, Layer.RoundHeight(value))) return;
                RaisePropertyChanged(nameof(BottomLayersMM));
                RaisePropertyChanged(nameof(AvailableLayerHeights));
            }
        }

        public ushort Microns => (ushort)(LayerHeight * 1000);

        public ushort BottomLayers
        {
            get => _bottomLayers;
            set
            {
                if(!RaiseAndSetIfChanged(ref _bottomLayers, value)) return;
                RaisePropertyChanged(nameof(BottomLayersMM));
            }
        }

        public decimal BottomLayersMM => Layer.RoundHeight(LayerHeight * BottomLayers);

        public decimal BottomExposure
        {
            get => _bottomExposure;
            set
            {
                if(!RaiseAndSetIfChanged(ref _bottomExposure, Math.Round(value, 2))) return;
                RaisePropertyChanged(nameof(MultipleBrightnessTable));
            }
        }

        public decimal NormalExposure
        {
            get => _normalExposure;
            set
            {
                if(!RaiseAndSetIfChanged(ref _normalExposure, Math.Round(value, 2))) return;
                RaisePropertyChanged(nameof(MultipleBrightnessTable));
            }
        }

        public decimal TopBottomMargin
        {
            get => _topBottomMargin;
            set => RaiseAndSetIfChanged(ref _topBottomMargin, Math.Round(value, 2));
        }

        public decimal LeftRightMargin
        {
            get => _leftRightMargin;
            set => RaiseAndSetIfChanged(ref _leftRightMargin, Math.Round(value, 2));
        }

        public byte ChamferLayers
        {
            get => _chamferLayers;
            set => RaiseAndSetIfChanged(ref _chamferLayers, value);
        }

        public byte ErodeBottomIterations
        {
            get => _erodeBottomIterations;
            set => RaiseAndSetIfChanged(ref _erodeBottomIterations, value);
        }

        public decimal PartMargin
        {
            get => _partMargin;
            set => RaiseAndSetIfChanged(ref _partMargin, Math.Round(value, 2));
        }

        public bool EnableAntiAliasing
        {
            get => _enableAntiAliasing;
            set => RaiseAndSetIfChanged(ref _enableAntiAliasing, value);
        }

        public bool MirrorOutput
        {
            get => _mirrorOutput;
            set => RaiseAndSetIfChanged(ref _mirrorOutput, value);
        }

        public decimal BaseHeight
        {
            get => _baseHeight;
            set => RaiseAndSetIfChanged(ref _baseHeight, Math.Round(value, 2));
        }

        public decimal FeaturesHeight
        {
            get => _featuresHeight;
            set => RaiseAndSetIfChanged(ref _featuresHeight, Math.Round(value, 2));
        }

        public decimal TotalHeight => _baseHeight + _featuresHeight;
        
        public decimal FeaturesMargin
        {
            get => _featuresMargin;
            set => RaiseAndSetIfChanged(ref _featuresMargin, Math.Round(value, 2));
        }

        public ushort StaircaseThickness
        {
            get => _staircaseThickness;
            set => RaiseAndSetIfChanged(ref _staircaseThickness, value);
        }

        public bool CounterTrianglesEnabled
        {
            get => _counterTrianglesEnabled;
            set => RaiseAndSetIfChanged(ref _counterTrianglesEnabled, value);
        }

        public sbyte CounterTrianglesTipOffset
        {
            get => _counterTrianglesTipOffset;
            set => RaiseAndSetIfChanged(ref _counterTrianglesTipOffset, value);
        }

        public bool CounterTrianglesFence
        {
            get => _counterTrianglesFence;
            set => RaiseAndSetIfChanged(ref _counterTrianglesFence, value);
        }

        public bool HolesEnabled
        {
            get => _holesEnabled;
            set => RaiseAndSetIfChanged(ref _holesEnabled, value);
        }

        public CalibrateExposureFinderShapes HoleShape
        {
            get => _holeShape;
            set => RaiseAndSetIfChanged(ref _holeShape, value);
        }

        public Measures UnitOfMeasure
        {
            get => _unitOfMeasure;
            set
            {
                if (!RaiseAndSetIfChanged(ref _unitOfMeasure, value)) return;
                RaisePropertyChanged(nameof(IsUnitOfMeasureMm));
            }
        }

        public bool IsUnitOfMeasureMm => _unitOfMeasure == Measures.Millimeters;

        public string HoleDiametersMm
        {
            get => _holeDiametersMm;
            set => RaiseAndSetIfChanged(ref _holeDiametersMm, value);
        }

        public string HoleDiametersPx
        {
            get => _holeDiametersPx;
            set => RaiseAndSetIfChanged(ref _holeDiametersPx, value);
        }

        /// <summary>
        /// Gets all holes in pixels and ordered
        /// </summary>
        public int[] Holes
        {
            get
            {
                if (!_holesEnabled)
                {
                    return Array.Empty<int>();
                }

                List<int> holes = new();

                if (_unitOfMeasure == Measures.Millimeters)
                {
                    var split = _holeDiametersMm.Split(',', StringSplitOptions.TrimEntries);
                    foreach (var mmStr in split)
                    {
                        if (string.IsNullOrWhiteSpace(mmStr)) continue;
                        if (!decimal.TryParse(mmStr, out var mm)) continue;
                        var mmPx = (int)Math.Floor(mm * Ppmm);
                        if (mmPx is <= 0 or > 500) continue;
                        if(holes.Contains(mmPx)) continue;
                        holes.Add(mmPx);
                    }
                }
                else
                {
                    var split = _holeDiametersPx.Split(',', StringSplitOptions.TrimEntries);
                    foreach (var pxStr in split)
                    {
                        if (string.IsNullOrWhiteSpace(pxStr)) continue;
                        if (!int.TryParse(pxStr, out var px)) continue;
                        if (px is <= 0 or > 500) continue;
                        if (holes.Contains(px)) continue;
                        holes.Add(px);
                    }
                }

                return holes.OrderBy(pixels => pixels).ToArray();
            }
        }

        public int GetHolesHeight(int[] holes)
        {
            if (holes.Length == 0) return 0;
            return (int) (holes.Sum() + (holes.Length-1) * _featuresMargin * Yppmm);
        }

        public bool BarsEnabled
        {
            get => _barsEnabled;
            set => RaiseAndSetIfChanged(ref _barsEnabled, value);
        }

        public decimal BarSpacing
        {
            get => _barSpacing;
            set => RaiseAndSetIfChanged(ref _barSpacing, value);
        }

        public decimal BarLength
        {
            get => _barLength;
            set => RaiseAndSetIfChanged(ref _barLength, value);
        }

        public sbyte BarVerticalSplitter
        {
            get => _barVerticalSplitter;
            set => RaiseAndSetIfChanged(ref _barVerticalSplitter, value);
        }

        public byte BarFenceThickness
        {
            get => _barFenceThickness;
            set => RaiseAndSetIfChanged(ref _barFenceThickness, value);
        }

        public sbyte BarFenceOffset
        {
            get => _barFenceOffset;
            set => RaiseAndSetIfChanged(ref _barFenceOffset, value);
        }

        public string BarThicknessesPx
        {
            get => _barThicknessesPx;
            set => RaiseAndSetIfChanged(ref _barThicknessesPx, value);
        }

        public string BarThicknessesMm
        {
            get => _barThicknessesMm;
            set => RaiseAndSetIfChanged(ref _barThicknessesMm, value);
        }

        /// <summary>
        /// Gets all holes in pixels and ordered
        /// </summary>
        public int[] Bars
        {
            get
            {
                if (!_barsEnabled)
                {
                    return Array.Empty<int>();
                }

                List<int> bars = new();

                if (_unitOfMeasure == Measures.Millimeters)
                {
                    var split = _barThicknessesMm.Split(',', StringSplitOptions.TrimEntries);
                    foreach (var mmStr in split)
                    {
                        if (string.IsNullOrWhiteSpace(mmStr)) continue;
                        if (!decimal.TryParse(mmStr, out var mm)) continue;
                        var mmPx = (int)Math.Floor(mm * Xppmm);
                        if (mmPx is <= 0 or > 500) continue;
                        if (bars.Contains(mmPx)) continue;
                        bars.Add(mmPx);
                    }
                }
                else
                {
                    var split = _barThicknessesPx.Split(',', StringSplitOptions.TrimEntries);
                    foreach (var pxStr in split)
                    {
                        if (string.IsNullOrWhiteSpace(pxStr)) continue;
                        if (!int.TryParse(pxStr, out var px)) continue;
                        if (px is <= 0 or > 500) continue;
                        if (bars.Contains(px)) continue;
                        bars.Add(px);
                    }
                }

                return bars.OrderBy(pixels => pixels).ToArray();
            }
        }

        public int GetBarsLength(int[] bars)
        {
            if (bars.Length == 0) return 0;
            int len = (int) (bars.Sum() + (bars.Length + 1) * _barSpacing * Yppmm);
            if (_barFenceThickness > 0)
            {
                len = Math.Max(len, len + _barFenceThickness * 2 + _barFenceOffset * 2);
            }
            return len;
        }

        public bool TextEnabled
        {
            get => _textEnabled;
            set => RaiseAndSetIfChanged(ref _textEnabled, value);
        }

        public static Array TextFonts => Enum.GetValues(typeof(FontFace));

        public FontFace TextFont
        {
            get => _textFont;
            set => RaiseAndSetIfChanged(ref _textFont, value);
        }

        public double TextScale
        {
            get => _textScale;
            set => RaiseAndSetIfChanged(ref _textScale, Math.Round(value, 2));
        }

        public byte TextThickness
        {
            get => _textThickness;
            set => RaiseAndSetIfChanged(ref _textThickness, value);
        }

        public string Text
        {
            get => _text;
            set => RaiseAndSetIfChanged(ref _text, value);
        }

        public Size TextSize
        {
            get
            {
                if (!_textEnabled || string.IsNullOrWhiteSpace(_text)) return Size.Empty;
                int baseline = 0;
                return CvInvoke.GetTextSize(_text, _textFont, _textScale, _textThickness, ref baseline);
            }
        }

        public bool MultipleBrightness
        {
            get => _multipleBrightness;
            set => RaiseAndSetIfChanged(ref _multipleBrightness, value);
        }

        public CalibrateExposureFinderMultipleBrightnessExcludeFrom MultipleBrightnessExcludeFrom
        {
            get => _multipleBrightnessExcludeFrom;
            set => RaiseAndSetIfChanged(ref _multipleBrightnessExcludeFrom, value);
        }

        public string MultipleBrightnessValues
        {
            get => _multipleBrightnessValues;
            set
            {
                if(!RaiseAndSetIfChanged(ref _multipleBrightnessValues, value)) return;
                RaisePropertyChanged(nameof(MultipleBrightnessTable));
            }
        }

        public List<ExposureItem> MultipleBrightnessTable
        {
            get
            {
                var brightnesses = MultipleBrightnessValuesArray;
                return brightnesses.Select(brightness => (ExposureItem) 
                    new(
                        _layerHeight,
                        Math.Round(brightness * _bottomExposure / byte.MaxValue, 2),
                        Math.Round(brightness * _normalExposure / byte.MaxValue, 2),
                        brightness)).ToList();
            }
        }

        public decimal MultipleBrightnessGenExposureTime
        {
            get => _multipleBrightnessGenExposureTime;
            set => RaiseAndSetIfChanged(ref _multipleBrightnessGenExposureTime, value);
        }

        /// <summary>
        /// Gets all holes in pixels and ordered
        /// </summary>
        public byte[] MultipleBrightnessValuesArray
        {
            get
            {
                List<byte> values = new();

                var split = _multipleBrightnessValues.Split(',', StringSplitOptions.TrimEntries);
                foreach (var brightnessStr in split)
                {
                    if (string.IsNullOrWhiteSpace(brightnessStr)) continue;
                    if (!byte.TryParse(brightnessStr, out var brightness)) continue;
                    if (brightness <= 0 || brightness > 255) continue;
                    if (values.Contains(brightness)) continue;
                    values.Add(brightness);
                }

                return values.OrderByDescending(brightness => brightness).ToArray();
            }
        }


        public bool MultipleLayerHeight
        {
            get => _multipleLayerHeight;
            set
            {
                if(!RaiseAndSetIfChanged(ref _multipleLayerHeight, value)) return;
                RaisePropertyChanged(nameof(AvailableLayerHeights));
            }
        }

        public decimal MultipleLayerHeightMaximum
        {
            get => _multipleLayerHeightMaximum;
            set
            {
                if(!RaiseAndSetIfChanged(ref _multipleLayerHeightMaximum, value)) return;
                RaisePropertyChanged(nameof(AvailableLayerHeights));
            }
        }

        public decimal MultipleLayerHeightStep
        {
            get => _multipleLayerHeightStep;
            set
            {
                if(!RaiseAndSetIfChanged(ref _multipleLayerHeightStep, value)) return;
                RaisePropertyChanged(nameof(AvailableLayerHeights));
            }
        }

        public bool DontLiftSamePositionedLayers
        {
            get => _dontLiftSamePositionedLayers;
            set => RaiseAndSetIfChanged(ref _dontLiftSamePositionedLayers, value);
        }

        public bool ZeroLightOffSamePositionedLayers
        {
            get => _zeroLightOffSamePositionedLayers;
            set => RaiseAndSetIfChanged(ref _zeroLightOffSamePositionedLayers, value);
        }

        public bool MultipleExposures
        {
            get => _multipleExposures;
            set => RaiseAndSetIfChanged(ref _multipleExposures, value);
        }

        public ExposureGenTypes ExposureGenType
        {
            get => _exposureGenType;
            set => RaiseAndSetIfChanged(ref _exposureGenType, value);
        }

        public bool ExposureGenIgnoreBaseExposure
        {
            get => _exposureGenIgnoreBaseExposure;
            set => RaiseAndSetIfChanged(ref _exposureGenIgnoreBaseExposure, value);
        }

        public decimal ExposureGenBottomStep
        {
            get => _exposureGenBottomStep;
            set => RaiseAndSetIfChanged(ref _exposureGenBottomStep, Math.Round(value, 2));
        }

        public decimal ExposureGenNormalStep
        {
            get => _exposureGenNormalStep;
            set => RaiseAndSetIfChanged(ref _exposureGenNormalStep, Math.Round(value, 2));
        }

        public byte ExposureGenTests
        {
            get => _exposureGenTests;
            set => RaiseAndSetIfChanged(ref _exposureGenTests, value);
        }

        public decimal ExposureGenManualLayerHeight
        {
            get => _exposureGenManualLayerHeight;
            set => RaiseAndSetIfChanged(ref _exposureGenManualLayerHeight, value);
        }

        public decimal[] AvailableLayerHeights
        {
            get
            {
                List<decimal> layerHeights = new();
                var endLayerHeight = _multipleLayerHeight ? _multipleLayerHeightMaximum : _layerHeight;
                for (decimal layerHeight = _layerHeight; layerHeight <= endLayerHeight; layerHeight += _multipleLayerHeightStep)
                {
                    layerHeights.Add(Layer.RoundHeight(layerHeight));
                }

                return layerHeights.ToArray();
            }
        }

        public decimal ExposureGenManualBottom
        {
            get => _exposureGenManualBottom;
            set => RaiseAndSetIfChanged(ref _exposureGenManualBottom, value);
        }

        public decimal ExposureGenManualNormal
        {
            get => _exposureGenManualNormal;
            set => RaiseAndSetIfChanged(ref _exposureGenManualNormal, value);
        }

        public ExposureItem ExposureManualEntry => new (_exposureGenManualLayerHeight, _exposureGenManualBottom, _exposureGenManualNormal);


        public RangeObservableCollection<ExposureItem> ExposureTable
        {
            get => _exposureTable;
            set => RaiseAndSetIfChanged(ref _exposureTable, value);
        }

        public bool BullsEyeEnabled
        {
            get => _bullsEyeEnabled;
            set => RaiseAndSetIfChanged(ref _bullsEyeEnabled, value);
        }

        public string BullsEyeConfigurationPx
        {
            get => _bullsEyeConfigurationPx;
            set => RaiseAndSetIfChanged(ref _bullsEyeConfigurationPx, value);
        }

        public string BullsEyeConfigurationMm
        {
            get => _bullsEyeConfigurationMm;
            set => RaiseAndSetIfChanged(ref _bullsEyeConfigurationMm, value);
        }

        public byte BullsEyeFenceThickness
        {
            get => _bullsEyeFenceThickness;
            set => RaiseAndSetIfChanged(ref _bullsEyeFenceThickness, value);
        }

        public sbyte BullsEyeFenceOffset
        {
            get => _bullsEyeFenceOffset;
            set => RaiseAndSetIfChanged(ref _bullsEyeFenceOffset, value);
        }

        public bool BullsEyeInvertQuadrants
        {
            get => _bullsEyeInvertQuadrants;
            set => RaiseAndSetIfChanged(ref _bullsEyeInvertQuadrants, value);
        }

        /// <summary>
        /// Gets all holes in pixels and ordered
        /// </summary>
        public BullsEyeCircle[] BullsEyes
        {
            get
            {
                if (!_bullsEyeEnabled)
                {
                    return Array.Empty<BullsEyeCircle>();
                }

                List<BullsEyeCircle> bulleyes = new();
                
                if (_unitOfMeasure == Measures.Millimeters)
                {
                    var splitGroup = _bullsEyeConfigurationMm.Split(',', StringSplitOptions.TrimEntries);
                    foreach (var group in splitGroup)
                    {
                        var splitDiameterThickness = group.Split(':', StringSplitOptions.TrimEntries);
                        if (splitDiameterThickness.Length < 2) continue;

                        if (string.IsNullOrWhiteSpace(splitDiameterThickness[0]) ||
                            string.IsNullOrWhiteSpace(splitDiameterThickness[1])) continue;
                        if (!decimal.TryParse(splitDiameterThickness[0], out var diameterMm)) continue;
                        if (!decimal.TryParse(splitDiameterThickness[1], out var thicknessMm)) continue;
                        var diameter = (int)Math.Floor(diameterMm * Ppmm);
                        if (diameterMm is <= 0 or > 500) continue;
                        var thickness = (int)Math.Floor(thicknessMm * Ppmm);
                        if (thickness is <= 0 or > 500) continue;
                        if (bulleyes.Exists(circle => circle.Diameter == diameter)) continue;
                        bulleyes.Add(new BullsEyeCircle((ushort)diameter, (ushort)thickness));
                    }
                }
                else
                {
                    var splitGroup = _bullsEyeConfigurationPx.Split(',', StringSplitOptions.TrimEntries);
                    foreach (var group in splitGroup)
                    {
                        var splitDiameterThickness = group.Split(':', StringSplitOptions.TrimEntries);
                        if (splitDiameterThickness.Length < 2) continue;

                        if (string.IsNullOrWhiteSpace(splitDiameterThickness[0]) ||
                            string.IsNullOrWhiteSpace(splitDiameterThickness[1])) continue;
                        if (!int.TryParse(splitDiameterThickness[0], out var diameter)) continue;
                        if (!int.TryParse(splitDiameterThickness[1], out var thickness)) continue;
                        if (diameter is <= 0 or > 500) continue;
                        if (thickness is <= 0 or > 500) continue;
                        if (bulleyes.Exists(circle => circle.Diameter == diameter)) continue;
                        bulleyes.Add(new BullsEyeCircle((ushort) diameter, (ushort) thickness));
                    }
                }
                
                return bulleyes.OrderBy(circle => circle.Diameter).DistinctBy(circle => circle.Diameter).ToArray();
            }
        }
        public int GetBullsEyeMaxPanelDiameter(BullsEyeCircle[] bullseyes)
        {
            if (!_bullsEyeEnabled || bullseyes.Length == 0) return 0;
            var diameter = GetBullsEyeMaxDiameter(bullseyes);
            return Math.Max(diameter, diameter + _bullsEyeFenceThickness + _bullsEyeFenceOffset * 2);
        }

        public int GetBullsEyeMaxDiameter(BullsEyeCircle[] bullseyes)
        {
            if (!_bullsEyeEnabled || bullseyes.Length == 0) return 0;
            return bullseyes[^1].Diameter + bullseyes[^1].Thickness / 2;
        }

        public bool PatternModel
        {
            get => _patternModel;
            set
            {
                if(!RaiseAndSetIfChanged(ref _patternModel, value)) return;
                if (_patternModel)
                {
                    LayerHeight = (decimal) SlicerFile.LayerHeight;
                    MultipleLayerHeight = false;
                }
            }
        }

        public bool PatternModelGlueBottomLayers
        {
            get => _patternModelGlueBottomLayers;
            set => RaiseAndSetIfChanged(ref _patternModelGlueBottomLayers, value);
        }


        public bool CanPatternModel => SlicerFile.BoundingRectangle.Width * 2 + _leftRightMargin * 2 + _partMargin * Xppmm < SlicerFile.ResolutionX ||
                                       SlicerFile.BoundingRectangle.Height * 2 + _topBottomMargin * 2 + _partMargin * Yppmm < SlicerFile.ResolutionY;

        #endregion

        #region Constructor

        public OperationCalibrateExposureFinder() { }

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

        public override void InitWithSlicerFile()
        {
            base.InitWithSlicerFile();
            _layerHeight = (decimal)SlicerFile.LayerHeight;
            _bottomLayers = SlicerFile.BottomLayerCount;
            _bottomExposure = (decimal)SlicerFile.BottomExposureTime;
            _normalExposure = (decimal)SlicerFile.ExposureTime;
            _mirrorOutput = SlicerFile.MirrorDisplay;

            if (SlicerFile.DisplayWidth > 0)
                DisplayWidth = (decimal)SlicerFile.DisplayWidth;
            if (SlicerFile.DisplayHeight > 0)
                DisplayHeight = (decimal)SlicerFile.DisplayHeight;

            if (_exposureGenManualBottom == 0)
                _exposureGenManualBottom = (decimal) SlicerFile.BottomExposureTime;
            if (_exposureGenManualNormal == 0)
                _exposureGenManualNormal = (decimal)SlicerFile.ExposureTime;
            if (_multipleBrightnessGenExposureTime == 0)
            {
                _multipleBrightnessGenExposureTime = (decimal)SlicerFile.ExposureTime;
            }

            if (!SlicerFile.HavePrintParameterPerLayerModifier(FileFormat.PrintParameterModifier.ExposureSeconds))
            {
                _multipleLayerHeight = false;
                _multipleExposures = false;
            }
        }

        #endregion

        #region Enums

        public enum CalibrateExposureFinderShapes : byte
        {
            Square,
            Circle
        }
        public static Array ShapesItems => Enum.GetValues(typeof(CalibrateExposureFinderShapes));

        public enum Measures : byte
        {
            Pixels,
            Millimeters,
        }

        public static Array MeasuresItems => Enum.GetValues(typeof(Measures));

        public enum CalibrateExposureFinderMultipleBrightnessExcludeFrom : byte
        {
            None,
            Bottom,
            BottomAndBase
        }
        public static Array MultipleBrightnessExcludeFromItems => Enum.GetValues(typeof(CalibrateExposureFinderMultipleBrightnessExcludeFrom));

        public enum ExposureGenTypes : byte
        {
            Linear,
            Multiplier
        }

        public static Array ExposureGenTypeItems => Enum.GetValues(typeof(ExposureGenTypes));
        #endregion

        #region Equality

        private bool Equals(OperationCalibrateExposureFinder other)
        {
            return _displayWidth == other._displayWidth && _displayHeight == other._displayHeight && _layerHeight == other._layerHeight && _bottomLayers == other._bottomLayers && _bottomExposure == other._bottomExposure && _normalExposure == other._normalExposure && _topBottomMargin == other._topBottomMargin && _leftRightMargin == other._leftRightMargin && _chamferLayers == other._chamferLayers && _erodeBottomIterations == other._erodeBottomIterations && _partMargin == other._partMargin && _enableAntiAliasing == other._enableAntiAliasing && _mirrorOutput == other._mirrorOutput && _baseHeight == other._baseHeight && _featuresHeight == other._featuresHeight && _featuresMargin == other._featuresMargin && _staircaseThickness == other._staircaseThickness && _holesEnabled == other._holesEnabled && _holeShape == other._holeShape && _unitOfMeasure == other._unitOfMeasure && _holeDiametersPx == other._holeDiametersPx && _holeDiametersMm == other._holeDiametersMm && _barsEnabled == other._barsEnabled && _barSpacing == other._barSpacing && _barLength == other._barLength && _barVerticalSplitter == other._barVerticalSplitter && _barFenceThickness == other._barFenceThickness && _barFenceOffset == other._barFenceOffset && _barThicknessesPx == other._barThicknessesPx && _barThicknessesMm == other._barThicknessesMm && _textEnabled == other._textEnabled && _textFont == other._textFont && _textScale.Equals(other._textScale) && _textThickness == other._textThickness && _text == other._text && _multipleBrightness == other._multipleBrightness && _multipleBrightnessExcludeFrom == other._multipleBrightnessExcludeFrom && _multipleBrightnessValues == other._multipleBrightnessValues && _multipleBrightnessGenExposureTime == other._multipleBrightnessGenExposureTime && _multipleLayerHeight == other._multipleLayerHeight && _multipleLayerHeightMaximum == other._multipleLayerHeightMaximum && _multipleLayerHeightStep == other._multipleLayerHeightStep && _dontLiftSamePositionedLayers == other._dontLiftSamePositionedLayers && _zeroLightOffSamePositionedLayers == other._zeroLightOffSamePositionedLayers && _multipleExposures == other._multipleExposures && _exposureGenType == other._exposureGenType && _exposureGenIgnoreBaseExposure == other._exposureGenIgnoreBaseExposure && _exposureGenBottomStep == other._exposureGenBottomStep && _exposureGenNormalStep == other._exposureGenNormalStep && _exposureGenTests == other._exposureGenTests && _exposureGenManualLayerHeight == other._exposureGenManualLayerHeight && _exposureGenManualBottom == other._exposureGenManualBottom && _exposureGenManualNormal == other._exposureGenManualNormal && Equals(_exposureTable, other._exposureTable) && _bullsEyeEnabled == other._bullsEyeEnabled && _bullsEyeConfigurationPx == other._bullsEyeConfigurationPx && _bullsEyeConfigurationMm == other._bullsEyeConfigurationMm && _bullsEyeInvertQuadrants == other._bullsEyeInvertQuadrants && _counterTrianglesEnabled == other._counterTrianglesEnabled && _counterTrianglesTipOffset == other._counterTrianglesTipOffset && _counterTrianglesFence == other._counterTrianglesFence && _patternModel == other._patternModel && _bullsEyeFenceThickness == other._bullsEyeFenceThickness && _bullsEyeFenceOffset == other._bullsEyeFenceOffset && _patternModelGlueBottomLayers == other._patternModelGlueBottomLayers;
        }

        public override bool Equals(object obj)
        {
            return ReferenceEquals(this, obj) || obj is OperationCalibrateExposureFinder other && Equals(other);
        }

        public override int GetHashCode()
        {
            var hashCode = new HashCode();
            hashCode.Add(_displayWidth);
            hashCode.Add(_displayHeight);
            hashCode.Add(_layerHeight);
            hashCode.Add(_bottomLayers);
            hashCode.Add(_bottomExposure);
            hashCode.Add(_normalExposure);
            hashCode.Add(_topBottomMargin);
            hashCode.Add(_leftRightMargin);
            hashCode.Add(_chamferLayers);
            hashCode.Add(_erodeBottomIterations);
            hashCode.Add(_partMargin);
            hashCode.Add(_enableAntiAliasing);
            hashCode.Add(_mirrorOutput);
            hashCode.Add(_baseHeight);
            hashCode.Add(_featuresHeight);
            hashCode.Add(_featuresMargin);
            hashCode.Add(_staircaseThickness);
            hashCode.Add(_holesEnabled);
            hashCode.Add((int) _holeShape);
            hashCode.Add((int) _unitOfMeasure);
            hashCode.Add(_holeDiametersPx);
            hashCode.Add(_holeDiametersMm);
            hashCode.Add(_barsEnabled);
            hashCode.Add(_barSpacing);
            hashCode.Add(_barLength);
            hashCode.Add(_barVerticalSplitter);
            hashCode.Add(_barFenceThickness);
            hashCode.Add(_barFenceOffset);
            hashCode.Add(_barThicknessesPx);
            hashCode.Add(_barThicknessesMm);
            hashCode.Add(_textEnabled);
            hashCode.Add((int) _textFont);
            hashCode.Add(_textScale);
            hashCode.Add(_textThickness);
            hashCode.Add(_text);
            hashCode.Add(_multipleBrightness);
            hashCode.Add((int) _multipleBrightnessExcludeFrom);
            hashCode.Add(_multipleBrightnessValues);
            hashCode.Add(_multipleBrightnessGenExposureTime);
            hashCode.Add(_multipleLayerHeight);
            hashCode.Add(_multipleLayerHeightMaximum);
            hashCode.Add(_multipleLayerHeightStep);
            hashCode.Add(_dontLiftSamePositionedLayers);
            hashCode.Add(_zeroLightOffSamePositionedLayers);
            hashCode.Add(_multipleExposures);
            hashCode.Add((int) _exposureGenType);
            hashCode.Add(_exposureGenIgnoreBaseExposure);
            hashCode.Add(_exposureGenBottomStep);
            hashCode.Add(_exposureGenNormalStep);
            hashCode.Add(_exposureGenTests);
            hashCode.Add(_exposureGenManualLayerHeight);
            hashCode.Add(_exposureGenManualBottom);
            hashCode.Add(_exposureGenManualNormal);
            hashCode.Add(_exposureTable);
            hashCode.Add(_bullsEyeEnabled);
            hashCode.Add(_bullsEyeConfigurationPx);
            hashCode.Add(_bullsEyeConfigurationMm);
            hashCode.Add(_bullsEyeInvertQuadrants);
            hashCode.Add(_counterTrianglesEnabled);
            hashCode.Add(_counterTrianglesTipOffset);
            hashCode.Add(_counterTrianglesFence);
            hashCode.Add(_patternModel);
            hashCode.Add(_bullsEyeFenceThickness);
            hashCode.Add(_bullsEyeFenceOffset);
            hashCode.Add(_patternModelGlueBottomLayers);
            return hashCode.ToHashCode();
        }

        #endregion

        #region Methods

        public void SortExposureTable()
        {
            _exposureTable.Sort();
        }

        public void SanitizeExposureTable()
        {
            _exposureTable.ReplaceCollection(GetSanitizedExposureTable());
        }

        public List<ExposureItem> GetSanitizedExposureTable()
        {
            var list = _exposureTable.ToList().Distinct().ToList();
            list.Sort();
            return list;
        }

        public void GenerateExposure()
        {
            var endLayerHeight = _multipleLayerHeight ? _multipleLayerHeightMaximum : _layerHeight;
            List<ExposureItem> list = new();
            for (decimal layerHeight = _layerHeight;
                layerHeight <= endLayerHeight;
                layerHeight += _multipleLayerHeightStep)
            {
                if(!_exposureGenIgnoreBaseExposure)
                    list.Add(new ExposureItem(layerHeight, _bottomExposure, _normalExposure));
                for (ushort testN = 1; testN <= _exposureGenTests; testN++)
                {
                    decimal bottomExposureTime = 0;
                    decimal exposureTime = 0;

                    switch (_exposureGenType)
                    {
                        case ExposureGenTypes.Linear:
                            bottomExposureTime = _bottomExposure + _exposureGenBottomStep * testN; 
                            exposureTime = _normalExposure + _exposureGenNormalStep * testN; 
                            break;
                        case ExposureGenTypes.Multiplier:
                            bottomExposureTime = _bottomExposure + _bottomExposure * layerHeight * _exposureGenBottomStep * testN;
                            exposureTime = _normalExposure + _normalExposure * layerHeight * _exposureGenNormalStep * testN;
                            break;
                    }

                    ExposureItem item = new(layerHeight, bottomExposureTime, exposureTime);
                    if(list.Contains(item)) continue; // Already on list, skip
                    list.Add(item);
                }
            }

            ExposureTable = new(list);
        }

        public Mat[] GetLayers(bool isPreview = false)
        {
            var holes = Holes;
            var bars = Bars;
            var bulleyes = BullsEyes;
            var textSize = TextSize;

            int featuresMarginX = (int)(Xppmm * _featuresMargin);
            int featuresMarginY = (int)(Yppmm * _featuresMargin);

            int holePanelWidth = holes.Length > 0 ? featuresMarginX * 2 + holes[^1] : 0;
            int holePanelHeight = GetHolesHeight(holes);
            int barsPanelHeight = GetBarsLength(bars);
            int bulleyesDiameter = GetBullsEyeMaxDiameter(bulleyes);
            int bulleyesPanelDiameter = GetBullsEyeMaxPanelDiameter(bulleyes);
            int bulleyesRadius = bulleyesDiameter / 2;
            int yLeftMaxSize = _staircaseThickness + featuresMarginY + Math.Max(barsPanelHeight, textSize.Width) + bulleyesPanelDiameter;
            int yRightMaxSize = _staircaseThickness + holePanelHeight + featuresMarginY * 2;
            
            int xSize = featuresMarginX;
            int ySize = TextMarkingSpacing + featuresMarginY;

            if (barsPanelHeight > 0 || textSize.Width > 0)
            {
                yLeftMaxSize += featuresMarginY;
            }

            int barLengthPx = (int) (_barLength * Xppmm);
            int barSpacingPx = (int) (_barSpacing * Yppmm);
            int barsPanelWidth = 0;

            if (bars.Length > 0)
            {
                barsPanelWidth = barLengthPx * 2 + _barVerticalSplitter;
                if (_barFenceThickness > 0)
                {
                    barsPanelWidth = Math.Max(barsPanelWidth, barsPanelWidth + _barFenceThickness * 2 + _barFenceOffset * 2);
                }
                xSize += barsPanelWidth + featuresMarginX;
            }

            if (!textSize.IsEmpty)
            {
                xSize += textSize.Height + featuresMarginX;
            }

            int bullseyeYPos = yLeftMaxSize - bulleyesPanelDiameter / 2;

            if (bulleyes.Length > 0)
            {
                xSize = Math.Max(xSize, bulleyesPanelDiameter + featuresMarginX * 2);
                yLeftMaxSize += featuresMarginY + 24;
            }

            int bullseyeXPos = xSize / 2;
            
            if (holePanelWidth > 0)
            {
                xSize -= featuresMarginX;
            }
            
            xSize += holePanelWidth;
            int negativeSideWidth = xSize;
            xSize += holePanelWidth;

            int positiveSideWidth = xSize - holePanelWidth;

            ySize += Math.Max(yLeftMaxSize, yRightMaxSize+10);

            Rectangle rect = new(new Point(0, 0), new Size(xSize, ySize));
            var layers = new Mat[2];
            layers[0] = EmguExtensions.InitMat(rect.Size);

            CvInvoke.Rectangle(layers[0], rect, EmguExtensions.WhiteColor, -1, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
            layers[1] = layers[0].CloneBlank();
            if (holes.Length > 0)
            {
                CvInvoke.Rectangle(layers[1],
                    new Rectangle(rect.Size.Width - holePanelWidth, 0, rect.Size.Width, layers[0].Height),
                    EmguExtensions.WhiteColor, -1, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
            }

            
            int xPos = 0;
            int yPos = 0;

            // Print staircase
            if (isPreview && _staircaseThickness > 0)
            {
                CvInvoke.Rectangle(layers[1],
                    new Rectangle(0, 0, layers[1].Size.Width-holePanelWidth, _staircaseThickness),
                    EmguExtensions.WhiteColor, -1, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
            }

            // Print holes
            for (var layerIndex = 0; layerIndex < layers.Length; layerIndex++)
            {
                var layer = layers[layerIndex];
                yPos = featuresMarginY + _staircaseThickness;
                for (int i = 0; i < holes.Length; i++)
                {
                    var diameter = holes[i];
                    var radius = diameter / 2;
                    xPos = layers[0].Width - holePanelWidth - featuresMarginX;

                    CalibrateExposureFinderShapes effectiveShape = _holeShape == CalibrateExposureFinderShapes.Square || diameter < 6 ?
                        CalibrateExposureFinderShapes.Square : CalibrateExposureFinderShapes.Circle;

                    switch (effectiveShape)
                    {
                        case CalibrateExposureFinderShapes.Square:
                            xPos -= diameter;
                            break;
                        case CalibrateExposureFinderShapes.Circle:
                            xPos -= radius;
                            yPos += radius;
                            break;
                    }


                    // Left side
                    if (layerIndex == 1)
                    {
                        if (diameter == 1)
                        {
                            layer.SetByte(xPos, yPos, 255);
                        }
                        else
                        {
                            switch (effectiveShape)
                            {
                                case CalibrateExposureFinderShapes.Square:
                                    CvInvoke.Rectangle(layers[layerIndex],
                                        new Rectangle(new Point(xPos, yPos), new Size(diameter-1, diameter-1)),
                                        EmguExtensions.WhiteColor, -1,
                                        _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                                    break;
                                case CalibrateExposureFinderShapes.Circle:
                                    CvInvoke.Circle(layers[layerIndex],
                                        new Point(xPos, yPos),
                                        radius, EmguExtensions.WhiteColor, -1,
                                        _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                                    break;
                            }

                        }
                    }

                    //holeXPos = layers[0].Width - holeXPos;
                    switch (effectiveShape)
                    {
                        case CalibrateExposureFinderShapes.Square:
                            xPos = layers[0].Width - rect.X - featuresMarginX - holes[^1];
                            break;
                        case CalibrateExposureFinderShapes.Circle:
                            xPos = layers[0].Width - rect.X - featuresMarginX - holes[^1] + radius;
                            break;
                    }

                    // Right side
                    if (diameter == 1)
                    {
                        layer.SetByte(xPos, yPos, 0);
                    }
                    else
                    {
                        switch (effectiveShape)
                        {
                            case CalibrateExposureFinderShapes.Square:
                                CvInvoke.Rectangle(layers[layerIndex],
                                    new Rectangle(new Point(xPos, yPos), new Size(diameter-1, diameter-1)),
                                    EmguExtensions.BlackColor, -1,
                                    _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                                break;
                            case CalibrateExposureFinderShapes.Circle:
                                CvInvoke.Circle(layers[layerIndex],
                                    new Point(xPos, yPos),
                                    radius, EmguExtensions.BlackColor, -1,
                                    _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                                break;
                        }
                    }


                    yPos += featuresMarginY;

                    switch (effectiveShape)
                    {
                        case CalibrateExposureFinderShapes.Square:
                            yPos += diameter;
                            break;
                        case CalibrateExposureFinderShapes.Circle:
                            yPos += radius;
                            break;
                    }
                }
            }

            xPos = featuresMarginX;
            
            // Print Zebra bars
            if (bars.Length > 0)
            {
                int yStartPos = _staircaseThickness + featuresMarginY;
                int xStartPos = xPos;
                yPos = yStartPos + _barFenceThickness / 2 + _barFenceOffset;
                xPos += _barFenceThickness / 2 + _barFenceOffset;
                for (int i = 0; i < bars.Length; i++)
                {
                    // Print positive bottom
                    CvInvoke.Rectangle(layers[1], new Rectangle(xPos, yPos, barLengthPx - 1, barSpacingPx - 1),
                        EmguExtensions.WhiteColor, -1, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                    // Print positive top
                    yPos += barSpacingPx;
                    CvInvoke.Rectangle(layers[1], new Rectangle(xPos + barLengthPx + _barVerticalSplitter, yPos, barLengthPx - 1, bars[i] - 1),
                        EmguExtensions.WhiteColor, -1, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                    yPos += bars[i];
                }

                // Left over
                CvInvoke.Rectangle(layers[1], new Rectangle(xPos, yPos, barLengthPx - 1, barSpacingPx - 1),
                    EmguExtensions.WhiteColor, -1, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);

                yPos += barSpacingPx;

                if (_barFenceThickness > 0)
                {
                    CvInvoke.Rectangle(layers[1], new Rectangle(
                            xStartPos - 1, 
                            yStartPos - 1, 
                            barsPanelWidth - _barFenceThickness + 1,
                            yPos - yStartPos + _barFenceThickness / 2 + _barFenceOffset + 1),
                        EmguExtensions.WhiteColor, _barFenceThickness, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);

                    yPos += _barFenceThickness * 2 + _barFenceOffset * 2;
                }

                xPos += featuresMarginX;
            }

            if (!textSize.IsEmpty)
            {
                CvInvoke.Rotate(layers[1], layers[1], RotateFlags.Rotate90CounterClockwise);
                CvInvoke.PutText(layers[1], _text, new Point(_staircaseThickness + featuresMarginX, layers[1].Height - barsPanelWidth - featuresMarginX * (barsPanelWidth > 0 ? 2 : 1)), _textFont, _textScale, EmguExtensions.WhiteColor, _textThickness, _enableAntiAliasing ? LineType.AntiAlias :  LineType.EightConnected);
                CvInvoke.Rotate(layers[1], layers[1], RotateFlags.Rotate90Clockwise);
            }

            // Print bullseye
            if (bulleyes.Length > 0)
            {
                yPos = bullseyeYPos;
                foreach (var circle in bulleyes)
                {
                    CvInvoke.Circle(layers[1], new Point(bullseyeXPos, yPos), circle.Radius, EmguExtensions.WhiteColor, circle.Thickness, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                }

                if (_bullsEyeInvertQuadrants)
                {
                    var matRoi1 = new Mat(layers[1], new Rectangle(bullseyeXPos, yPos - bulleyesRadius - 5, bulleyesRadius + 6, bulleyesRadius + 5));
                    var matRoi2 = new Mat(layers[1], new Rectangle(bullseyeXPos - bulleyesRadius - 5, yPos, bulleyesRadius + 5, bulleyesRadius + 6));
                    //using var mask = matRoi1.CloneBlank();

                    //CvInvoke.Circle(mask, new Point(mask.Width / 2, mask.Height / 2), bulleyesRadius, EmguExtensions.WhiteByte, -1, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                    //CvInvoke.Circle(mask, new Point(mask.Width / 2, mask.Height / 2), BullsEyes[^1].Radius, EmguExtensions.WhiteByte, BullsEyes[^1].Thickness, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);

                    CvInvoke.BitwiseNot(matRoi1, matRoi1);
                    CvInvoke.BitwiseNot(matRoi2, matRoi2);
                }

                if (_bullsEyeFenceThickness > 0)
                {
                    CvInvoke.Rectangle(layers[1],
                        new Rectangle(
                            new Point(
                                bullseyeXPos - bulleyesRadius - 5 - _bullsEyeFenceOffset - _bullsEyeFenceThickness / 2, 
                                yPos - bulleyesRadius - 5 - _bullsEyeFenceOffset - _bullsEyeFenceThickness / 2), 
                            new Size(
                                bulleyesDiameter + 10 + _bullsEyeFenceOffset*2 + _bullsEyeFenceThickness, 
                                bulleyesDiameter + 10 + _bullsEyeFenceOffset*2 + _bullsEyeFenceThickness)), 
                        EmguExtensions.WhiteColor,
                        _bullsEyeFenceThickness, 
                        _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                }
                

                yPos += bulleyesRadius;
            }

            if (isPreview)
            {
                var textHeightStart = layers[1].Height - featuresMarginY - TextMarkingSpacing;
                CvInvoke.PutText(layers[1], $"{Microns}u", new Point(TextMarkingStartX, textHeightStart), TextMarkingFontFace, TextMarkingScale, EmguExtensions.WhiteColor, TextMarkingThickness, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                CvInvoke.PutText(layers[1], $"{_bottomExposure}s", new Point(TextMarkingStartX, textHeightStart + TextMarkingLineBreak), TextMarkingFontFace, TextMarkingScale, EmguExtensions.WhiteColor, TextMarkingThickness, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                CvInvoke.PutText(layers[1], $"{_normalExposure}s", new Point(TextMarkingStartX, textHeightStart + TextMarkingLineBreak * 2), TextMarkingFontFace, TextMarkingScale, EmguExtensions.WhiteColor, TextMarkingThickness, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                if (holes.Length > 0)
                {
                    CvInvoke.PutText(layers[1], $"{Microns}u", new Point(layers[1].Width - featuresMarginX * 2 - holes[^1] + TextMarkingStartX, textHeightStart), TextMarkingFontFace, TextMarkingScale, EmguExtensions.BlackColor, TextMarkingThickness, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                    CvInvoke.PutText(layers[1], $"{_bottomExposure}s", new Point(layers[1].Width - featuresMarginX * 2 - holes[^1] + TextMarkingStartX, textHeightStart + TextMarkingLineBreak), TextMarkingFontFace, TextMarkingScale, EmguExtensions.BlackColor, TextMarkingThickness, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                    CvInvoke.PutText(layers[1], $"{_normalExposure}s", new Point(layers[1].Width - featuresMarginX * 2 - holes[^1] + TextMarkingStartX, textHeightStart + TextMarkingLineBreak * 2), TextMarkingFontFace, TextMarkingScale, EmguExtensions.BlackColor, TextMarkingThickness, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                }
            }

            if (negativeSideWidth >= 200 && _counterTrianglesEnabled)
            {
                xPos = 120;
                int triangleHeight = TextMarkingSpacing + 19;
                int triangleWidth = (negativeSideWidth - xPos - featuresMarginX) / 2;
                int triangleWidthQuarter = triangleWidth / 4;

                if (triangleWidth > 5)
                {
                    yPos = layers[1].Height - featuresMarginY - triangleHeight + 1;
                    int yHalfPos = yPos + triangleHeight / 2;
                    int yPosEnd = layers[1].Height - featuresMarginY + 1;

                    var triangles = new Point[4][];

                    triangles[0] = new Point[]  // Left
                    {
                        new(xPos, yPos), // Top Left
                        new(xPos + triangleWidth, yHalfPos), // Middle
                        new(xPos, yPosEnd), // Bottom Left
                    };
                    triangles[1] = new Point[] // Right
                    {
                        new(xPos + triangleWidth * 2, yPos), // Top Right
                        new(xPos + triangleWidth, yHalfPos), // Middle
                        new(xPos + triangleWidth * 2, yPosEnd), // Bottom Right
                    };
                    triangles[2] = new Point[] // Top
                    {
                        new(xPos + triangleWidth - triangleWidthQuarter, yPos),  // Top Left
                        new(xPos + triangleWidth + triangleWidthQuarter, yPos),  // Top Right
                        new(xPos + triangleWidth, yHalfPos - _counterTrianglesTipOffset), // Middle
                    };
                    triangles[3] = new Point[] // Bottom
                    {
                        new(xPos + triangleWidth - triangleWidthQuarter, yPosEnd),  // Bottom Left
                        new(xPos + triangleWidth + triangleWidthQuarter, yPosEnd),  // Bottom Right
                        new(xPos + triangleWidth, yHalfPos + _counterTrianglesTipOffset), // Middle
                    };

                    foreach (var triangle in triangles)
                    {
                        using var vec = new VectorOfPoint(triangle);
                        CvInvoke.FillPoly(layers[1], vec, EmguExtensions.WhiteColor,
                            _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                    }

                    /*byte size = 60;
                    var matRoi = new Mat(layers[1], new Rectangle(
                        new Point(xPos + triangleWidth - size / 2, yHalfPos - size / 2),
                        new Size(size, size)));

                    CvInvoke.BitwiseNot(matRoi, matRoi);*/
                    

                    if (_counterTrianglesFence)
                    {
                        byte outlineThickness = 8;
                        //byte outlineThicknessHalf = (byte)(outlineThickness / 2);

                        CvInvoke.Rectangle(layers[1], new Rectangle(
                                new Point(triangles[0][0].X - 0, triangles[0][0].Y - 0),
                                new Size(triangleWidth * 2 + 0, triangleHeight + 0)
                            ), EmguExtensions.WhiteColor, outlineThickness,
                            _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                    }
                }
            }
            // Print a hardcoded spiral if have space
            /*if (positiveSideWidth >= 250000)
            {
                var mat = layers[0].CloneBlank();
                var matMask = layers[0].CloneBlank();
                xPos = (int) ((layers[0].Width - holePanelWidth) / 1.8);
                yPos = layers[0].Height - featuresMarginY - TextMarkingSpacing / 2;
                byte circleThickness = 5;
                byte radiusStep = 13;
                int count = -1;
                int maxRadius = 0;
                //bool white = true;

                for (int radius = radiusStep;radius <= 100; radius += (radiusStep + count))
                {
                    count++;
                    CvInvoke.Circle(mat, new Point(xPos, yPos), radius, EmguExtensions.WhiteByte, circleThickness, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                    maxRadius = radius;
                }

                CvInvoke.Circle(mat, new Point(xPos, yPos), 5, EmguExtensions.WhiteByte, -1, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                CvInvoke.Circle(matMask, new Point(xPos, yPos), maxRadius+2, EmguExtensions.WhiteByte, -1);

                var matRoi1 = new Mat(mat, new Rectangle(xPos, yPos - maxRadius-1, maxRadius+2, maxRadius+1));
                var matRoi2 = new Mat(mat, new Rectangle(xPos-maxRadius-1, yPos, maxRadius+1, Math.Min(mat.Height- yPos, maxRadius)));

                CvInvoke.BitwiseNot(matRoi1, matRoi1);
                CvInvoke.BitwiseNot(matRoi2, matRoi2);

                CvInvoke.BitwiseAnd(layers[0], mat, layers[1], matMask);

                Point anchor = new Point(-1, -1);
                //CvInvoke.MorphologyEx(layers[1], layers[1], MorphOp.Open, CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(3,3), anchor), anchor, 1, BorderType.Reflect101, default);
                
                mat.Dispose();
                matMask.Dispose();
            }*/

            return layers;
        }

        public Mat GetThumbnail()
        {
            Mat thumbnail = EmguExtensions.InitMat(new Size(400, 200), 3);
            var fontFace = FontFace.HersheyDuplex;
            var fontScale = 1;
            var fontThickness = 2;
            const byte xSpacing = 45;
            const byte ySpacing = 45;
            CvInvoke.PutText(thumbnail, "UVtools", new Point(140, 35), fontFace, fontScale, new MCvScalar(255, 27, 245), fontThickness + 1);
            CvInvoke.Line(thumbnail, new Point(xSpacing, 0), new Point(xSpacing, ySpacing + 5), new MCvScalar(255, 27, 245), 3);
            CvInvoke.Line(thumbnail, new Point(xSpacing, ySpacing + 5), new Point(thumbnail.Width - xSpacing, ySpacing + 5), new MCvScalar(255, 27, 245), 3);
            CvInvoke.Line(thumbnail, new Point(thumbnail.Width - xSpacing, 0), new Point(thumbnail.Width - xSpacing, ySpacing + 5), new MCvScalar(255, 27, 245), 3);
            CvInvoke.PutText(thumbnail, "Exposure Time Cal.", new Point(xSpacing, ySpacing * 2), fontFace, fontScale, new MCvScalar(0, 255, 255), fontThickness);
            CvInvoke.PutText(thumbnail, $"{Microns}um @ {BottomExposure}s/{NormalExposure}s", new Point(xSpacing, ySpacing * 3), fontFace, fontScale, EmguExtensions.WhiteColor, fontThickness);
            if (_patternModel)
            {
                CvInvoke.PutText(thumbnail, $"Patterned Model", new Point(xSpacing, ySpacing * 4), fontFace, fontScale, EmguExtensions.WhiteColor, fontThickness);
            }
            else
            {
                CvInvoke.PutText(thumbnail, $"Features: {(_staircaseThickness > 0 ? 1 : 0) + Holes.Length + Bars.Length + BullsEyes.Length + (_counterTrianglesEnabled ? 1 : 0)}", new Point(xSpacing, ySpacing * 4), fontFace, fontScale, EmguExtensions.WhiteColor, fontThickness);
            }
            

            return thumbnail;
        }

        protected override bool ExecuteInternally(OperationProgress progress)
        {
            int sideMarginPx = (int)Math.Floor(_leftRightMargin * Xppmm);
            int topBottomMarginPx = (int)Math.Floor(_topBottomMargin * Yppmm);
            int partMarginXPx = (int) Math.Floor(_partMargin * Xppmm);
            int partMarginYPx = (int) Math.Floor(_partMargin * Yppmm);

            var anchor = new Point(-1, -1);
            using var kernel = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(3, 3), anchor);

            if (_patternModel)
            {
                ConcurrentBag<Layer> parallelLayers = new();
                Dictionary<ExposureItem, Point> table = new();
                var boundingRectangle = SlicerFile.BoundingRectangle;
                int xHalf = boundingRectangle.Width / 2;
                int yHalf = boundingRectangle.Height / 2;

                var brightnesses = MultipleBrightnessValuesArray;
                var multipleExposures = _exposureTable.Where(item => item.IsValid && item.LayerHeight == (decimal) SlicerFile.LayerHeight).ToArray();
                if (brightnesses.Length == 0 || !_multipleBrightness) brightnesses = new[] { byte.MaxValue };
                if (multipleExposures.Length == 0 || !_multipleExposures) multipleExposures = new[] { new ExposureItem((decimal)SlicerFile.LayerHeight, _bottomExposure, _normalExposure) };

                int currentX = sideMarginPx;
                int currentY = topBottomMarginPx;
                Rectangle glueBottomLayerRectangle = new(new Point(currentX, currentY), Size.Empty);
                foreach (var multipleExposure in multipleExposures)
                {
                    foreach (var brightness in brightnesses)
                    {
                        if (currentX + boundingRectangle.Width + sideMarginPx >= SlicerFile.ResolutionX)
                        {
                            currentX = sideMarginPx;
                            currentY += boundingRectangle.Height + partMarginYPx;
                        }

                        if (currentY + topBottomMarginPx >= SlicerFile.ResolutionY) break;

                        var item = multipleExposure.Clone();
                        item.Brightness = brightness;
                        table.Add(item, new Point(currentX, currentY));

                        glueBottomLayerRectangle.Size = new Size(currentX + boundingRectangle.Width, currentY + boundingRectangle.Height);

                        currentX += boundingRectangle.Width + partMarginXPx;
                    }
                }

                if (table.Count <= 1) return false;
                ushort microns = SlicerFile.LayerHeightUm;

                var tableGrouped = table.GroupBy(pair => new {pair.Key.LayerHeight, pair.Key.BottomExposure, pair.Key.Exposure}).Distinct();
                SlicerFile.BottomLayerCount = _bottomLayers;
                progress.ItemCount = (uint) (SlicerFile.LayerCount * table.Count); 
                Parallel.For(0, SlicerFile.LayerCount, layerIndex =>
                {
                    if (progress.Token.IsCancellationRequested) return;
                    var layer = SlicerFile[layerIndex];
                    using var mat = layer.LayerMat;
                    var matRoi = new Mat(mat, boundingRectangle);
                    int layerCountOnHeight = (int)Math.Floor(layer.PositionZ / SlicerFile.LayerHeight);
                    foreach (var group in tableGrouped)
                    {
                        var newLayer = layer.Clone();
                        newLayer.ExposureTime = (float)(newLayer.IsBottomLayer ? group.Key.BottomExposure : group.Key.Exposure);
                        using var newMat = mat.CloneBlank();
                        foreach (var brightness in brightnesses)
                        {
                            ExposureItem item = new(group.Key.LayerHeight, group.Key.BottomExposure, group.Key.Exposure, brightness);
                            if(!table.TryGetValue(item, out var point)) continue;
                            
                            var newMatRoi = new Mat(newMat, new Rectangle(point, matRoi.Size));
                            matRoi.CopyTo(newMatRoi);

                            if (layer.IsBottomLayer)
                            {
                                if (_patternModelGlueBottomLayers)
                                {
                                    newMatRoi.SetTo(EmguExtensions.WhiteColor);
                                }
                            }

                            if (layerCountOnHeight < _chamferLayers)
                            {
                                CvInvoke.Erode(newMatRoi, newMatRoi, kernel, anchor, _chamferLayers - layerCountOnHeight, BorderType.Reflect101, default);
                            }

                            if (layer.IsBottomLayer)
                            {
                                if (_erodeBottomIterations > 0)
                                {
                                    CvInvoke.Erode(newMatRoi, newMatRoi, kernel, anchor, _erodeBottomIterations, BorderType.Reflect101, default);
                                }

                                if (_multipleBrightness)
                                {
                                    CvInvoke.PutText(newMatRoi, brightness.ToString(), new(xHalf - 60, yHalf + 20 - TextMarkingLineBreak * 4), TextMarkingFontFace, 2, EmguExtensions.BlackColor, 3, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                                }
                                CvInvoke.PutText(newMatRoi, $"{microns}u", new(xHalf - 60, yHalf + 20 - TextMarkingLineBreak * 2), TextMarkingFontFace, 2, EmguExtensions.BlackColor, 3, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                                CvInvoke.PutText(newMatRoi, $"{group.Key.BottomExposure}s", new(xHalf - 60, yHalf + 20), TextMarkingFontFace, 2, EmguExtensions.BlackColor, 3, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                                CvInvoke.PutText(newMatRoi, $"{group.Key.Exposure}s", new(xHalf - 60, yHalf + 20 + TextMarkingLineBreak * 2), TextMarkingFontFace, 2, EmguExtensions.BlackColor, 3, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                            }

                            if (brightness < 255)
                            {
                                if (_multipleBrightnessExcludeFrom == CalibrateExposureFinderMultipleBrightnessExcludeFrom.None ||
                                    _multipleBrightnessExcludeFrom == CalibrateExposureFinderMultipleBrightnessExcludeFrom.Bottom && !layer.IsBottomLayer ||
                                    _multipleBrightnessExcludeFrom == CalibrateExposureFinderMultipleBrightnessExcludeFrom.BottomAndBase && !layer.IsBottomLayer)
                                {
                                    using var pattern = matRoi.New();
                                    pattern.SetTo(new MCvScalar(byte.MaxValue - brightness));
                                    CvInvoke.Subtract(newMatRoi, pattern, newMatRoi);
                                }
                            }
                        }
                        

                        newLayer.LayerMat = newMat;
                        parallelLayers.Add(newLayer);
                        progress.LockAndIncrement();
                    }
                });

                progress.Token.ThrowIfCancellationRequested();
                if (parallelLayers.IsEmpty) return false;
                var layers = parallelLayers.OrderBy(layer => layer.PositionZ).ThenBy(layer => layer.ExposureTime).ToList();

                progress.ResetNameAndProcessed("Optimized layers");

                Layer currentLayer = layers[0];
                for (var layerIndex = 1; layerIndex < layers.Count; layerIndex++)
                {
                    progress.Token.ThrowIfCancellationRequested();
                    progress++;
                    var layer = layers[layerIndex];
                    if (currentLayer.PositionZ != layer.PositionZ ||
                        currentLayer.ExposureTime != layer.ExposureTime) // Different layers, cache and continue
                    {
                        currentLayer = layer;
                        continue;
                    }

                    using var matCurrent = currentLayer.LayerMat;
                    using var mat = layer.LayerMat;

                    CvInvoke.Add(matCurrent, mat, matCurrent); // Sum layers
                    currentLayer.LayerMat = matCurrent;

                    layers[layerIndex] = null; // Discard
                }

                layers.RemoveAll(layer => layer is null); // Discard equal layers

                SlicerFile.SuppressRebuildPropertiesWork(() =>
                {
                    SlicerFile.BottomExposureTime = (float)BottomExposure;
                    SlicerFile.ExposureTime = (float)NormalExposure;
                    SlicerFile.LayerManager.Layers = layers.ToArray();
                });
            }
            else // No patterned
            {
                var layers = GetLayers();
                if (layers is null) return false;
                progress.ItemCount = 0;
                //SanitizeExposureTable();
                if (layers[0].Width > SlicerFile.ResolutionX || layers[0].Height > SlicerFile.ResolutionY)
                {
                    return false;
                }

                List<Layer> newLayers = new();

                Dictionary<ExposureItem, Point> table = new();
                var endLayerHeight = _multipleLayerHeight ? _multipleLayerHeightMaximum : _layerHeight;
                var totalHeight = TotalHeight;
                uint layerIndex = 0;
                int currentX = sideMarginPx;
                int currentY = topBottomMarginPx;
                int featuresMarginX = (int)(Xppmm * _featuresMargin);
                int featuresMarginY = (int)(Yppmm * _featuresMargin);

                var holes = Holes;
                int holePanelWidth = holes.Length > 0 ? featuresMarginX * 2 + holes[^1] : 0;
                int staircaseWidth = layers[0].Width - holePanelWidth;

                var brightnesses = MultipleBrightnessValuesArray;
                if (brightnesses.Length == 0 || !_multipleBrightness) brightnesses = new[] { byte.MaxValue };

                ExposureItem lastExposureItem = null;
                decimal lastcurrentHeight = 0;

                void AddLayer(decimal currentHeight, decimal layerHeight, decimal bottomExposure, decimal normalExposure)
                {
                    var layerDifference = currentHeight / layerHeight;

                    if (!layerDifference.IsInteger()) return; // Not at right height to process with layer height
                                                              //Debug.WriteLine($"{currentHeight} / {layerHeight} = {layerDifference}, Floor={Math.Floor(layerDifference)}");

                    int firstFeatureLayer = (int)Math.Floor(_baseHeight / layerHeight);
                    int lastLayer = (int)Math.Floor((_baseHeight + _featuresHeight) / layerHeight);
                    int layerCountOnHeight = (int)Math.Floor(currentHeight / layerHeight);
                    bool isBottomLayer = layerCountOnHeight <= _bottomLayers;
                    bool isBaseLayer = currentHeight <= _baseHeight;
                    ushort microns = (ushort)Math.Floor(layerHeight * 1000);
                    Point position;
                    bool addSomething = false;

                    bool reUseLastLayer =
                        lastExposureItem is not null &&
                        lastcurrentHeight == currentHeight &&
                        lastExposureItem.LayerHeight == layerHeight &&
                        (isBottomLayer && lastExposureItem.BottomExposure == bottomExposure || !isBottomLayer && lastExposureItem.Exposure == normalExposure);

                    using var mat = reUseLastLayer ? newLayers[^1].LayerMat : EmguExtensions.InitMat(SlicerFile.Resolution);

                    lastcurrentHeight = currentHeight;

                    foreach (var brightness in brightnesses)
                    {
                        var bottomExposureTemp = bottomExposure;
                        var normalExposureTemp = normalExposure;
                        ExposureItem key = new(layerHeight, bottomExposure, normalExposure, brightness);
                        lastExposureItem = key;

                        if (table.TryGetValue(key, out var pos))
                        {
                            position = pos;
                        }
                        else
                        {
                            if (currentX + layers[0].Width + sideMarginPx > SlicerFile.ResolutionX)
                            {
                                currentX = sideMarginPx;
                                currentY += layers[0].Height + partMarginYPx;
                            }

                            if (currentY + layers[0].Height + topBottomMarginPx > SlicerFile.ResolutionY)
                            {
                                break; // Reach the end
                            }

                            position = new Point(currentX, currentY);
                            table.Add(key, new Point(currentX, currentY));

                            currentX += layers[0].Width + partMarginXPx;
                        }


                        Mat matRoi = new(mat, new Rectangle(position, layers[0].Size));

                        layers[isBaseLayer ? 0 : 1].CopyTo(matRoi);

                        if (!isBaseLayer && _staircaseThickness > 0)
                        {
                            int staircaseWidthIncrement = (int) Math.Ceiling(staircaseWidth / (_featuresHeight / layerHeight-1));
                            int staircaseLayer = layerCountOnHeight - firstFeatureLayer - 1;
                            int staircaseWidthForLayer = staircaseWidth - staircaseWidthIncrement * staircaseLayer;
                            if (staircaseWidthForLayer >= 0 && layerCountOnHeight != lastLayer)
                            {
                                CvInvoke.Rectangle(matRoi,
                                    new Rectangle(staircaseWidth - staircaseWidthForLayer, 0, staircaseWidthForLayer, _staircaseThickness),
                                    EmguExtensions.WhiteColor, -1,
                                    _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                            }
                        }

                        if (isBottomLayer && _erodeBottomIterations > 0)
                        {
                            CvInvoke.Erode(matRoi, matRoi, kernel, anchor, _erodeBottomIterations, BorderType.Reflect101, default);
                        }

                        if (layerCountOnHeight < _chamferLayers)
                        {
                            CvInvoke.Erode(matRoi, matRoi, kernel, anchor, _chamferLayers - layerCountOnHeight, BorderType.Reflect101, default);
                        }

                        if (_multipleBrightness && brightness < 255)
                        {
                            // normalExposure - 255
                            //       x        - brightness
                            normalExposureTemp = Math.Round(normalExposure * brightness / byte.MaxValue, 2);
                            if (_multipleBrightnessExcludeFrom == CalibrateExposureFinderMultipleBrightnessExcludeFrom.None)
                            {
                                bottomExposureTemp = Math.Round(bottomExposure * brightness / byte.MaxValue, 2);
                            }
                        }

                        var textHeightStart = matRoi.Height - featuresMarginY - TextMarkingSpacing;
                        CvInvoke.PutText(matRoi, $"{microns}u", new Point(TextMarkingStartX, textHeightStart), TextMarkingFontFace, TextMarkingScale, EmguExtensions.WhiteColor, TextMarkingThickness, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                        CvInvoke.PutText(matRoi, $"{bottomExposureTemp}s", new Point(TextMarkingStartX, textHeightStart + TextMarkingLineBreak), TextMarkingFontFace, TextMarkingScale, EmguExtensions.WhiteColor, TextMarkingThickness, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                        CvInvoke.PutText(matRoi, $"{normalExposureTemp}s", new Point(TextMarkingStartX, textHeightStart + TextMarkingLineBreak * 2), TextMarkingFontFace, TextMarkingScale, EmguExtensions.WhiteColor, TextMarkingThickness, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                        if (holes.Length > 0)
                        {
                            CvInvoke.PutText(matRoi, $"{microns}u", new Point(matRoi.Width - featuresMarginX * 2 - holes[^1] + TextMarkingStartX, textHeightStart), TextMarkingFontFace, TextMarkingScale, EmguExtensions.BlackColor, TextMarkingThickness, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                            CvInvoke.PutText(matRoi, $"{bottomExposureTemp}s", new Point(matRoi.Width - featuresMarginX * 2 - holes[^1] + TextMarkingStartX, textHeightStart + TextMarkingLineBreak), TextMarkingFontFace, TextMarkingScale, EmguExtensions.BlackColor, TextMarkingThickness, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                            CvInvoke.PutText(matRoi, $"{normalExposureTemp}s", new Point(matRoi.Width - featuresMarginX * 2 - holes[^1] + TextMarkingStartX, textHeightStart + TextMarkingLineBreak * 2), TextMarkingFontFace, TextMarkingScale, EmguExtensions.BlackColor, TextMarkingThickness, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                        }

                        if (_multipleBrightness)
                        {
                            CvInvoke.PutText(matRoi, brightness.ToString(), new Point(matRoi.Width / 3, 35), TextMarkingFontFace, TextMarkingScale, EmguExtensions.WhiteColor, TextMarkingThickness, _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected);
                            if (brightness < 255 &&
                                (_multipleBrightnessExcludeFrom == CalibrateExposureFinderMultipleBrightnessExcludeFrom.None ||
                                 _multipleBrightnessExcludeFrom == CalibrateExposureFinderMultipleBrightnessExcludeFrom.Bottom && !isBottomLayer ||
                                 _multipleBrightnessExcludeFrom == CalibrateExposureFinderMultipleBrightnessExcludeFrom.BottomAndBase && !isBottomLayer && !isBaseLayer)
                            )
                            {
                                using var pattern = matRoi.New();
                                //pattern.SetTo(new MCvScalar(brightness)); OLD
                                //CvInvoke.BitwiseAnd(matRoi, pattern, matRoi, matRoi); OLD

                                pattern.SetTo(new MCvScalar(byte.MaxValue - brightness));
                                CvInvoke.Subtract(matRoi, pattern, matRoi);
                            }
                        }

                        addSomething = true;
                    }

                    if (!addSomething) return;

                    if (reUseLastLayer)
                    {
                        newLayers[^1].LayerMat = mat;
                    }
                    else
                    {
                        Layer layer = new(layerIndex++, mat, SlicerFile)
                        {
                            PositionZ = (float)currentHeight,
                            ExposureTime = isBottomLayer ? (float)bottomExposure : (float)normalExposure,
                            LiftHeight = isBottomLayer ? SlicerFile.BottomLiftHeight : SlicerFile.LiftHeight,
                            LiftSpeed = isBottomLayer ? SlicerFile.BottomLiftSpeed : SlicerFile.LiftSpeed,
                            RetractSpeed = SlicerFile.RetractSpeed,
                            LightOffDelay = isBottomLayer ? SlicerFile.BottomLightOffDelay : SlicerFile.LightOffDelay,
                            LightPWM = isBottomLayer ? SlicerFile.BottomLightPWM : SlicerFile.LightPWM,
                            IsModified = true
                        };
                        newLayers.Add(layer);
                    }
                    

                    progress++;
                }

                for (decimal currentHeight = _layerHeight; currentHeight <= totalHeight; currentHeight += Layer.HeightPrecisionIncrement)
                {
                    currentHeight = Layer.RoundHeight(currentHeight);
                    for (decimal layerHeight = _layerHeight; layerHeight <= endLayerHeight; layerHeight += _multipleLayerHeightStep)
                    {
                        progress.Token.ThrowIfCancellationRequested();
                        layerHeight = Layer.RoundHeight(layerHeight);

                        if (_multipleExposures)
                        {
                            foreach (var exposureItem in _exposureTable)
                            {
                                if (exposureItem.IsValid && exposureItem.LayerHeight == layerHeight)
                                {
                                    AddLayer(currentHeight, layerHeight, exposureItem.BottomExposure, exposureItem.Exposure);
                                }
                            }
                        }
                        else
                        {
                            AddLayer(currentHeight, layerHeight, _bottomExposure, _normalExposure);
                        }
                    }
                }

                SlicerFile.SuppressRebuildPropertiesWork(() =>
                {
                    SlicerFile.LayerHeight = (float)LayerHeight;
                    SlicerFile.BottomExposureTime = (float)BottomExposure;
                    SlicerFile.ExposureTime = (float)NormalExposure;
                    SlicerFile.BottomLayerCount = BottomLayers;
                    SlicerFile.LayerManager.Layers = newLayers.ToArray();
                });

                if (_mirrorOutput)
                {
                    new OperationFlip(SlicerFile) { FlipDirection = Enumerations.FlipDirection.Horizontally }.Execute(progress);
                }
            }

            if (SlicerFile.ThumbnailsCount > 0)
                SlicerFile.SetThumbnails(GetThumbnail());

            if (_dontLiftSamePositionedLayers)
            {
                SlicerFile.LayerManager.SetNoLiftForSamePositionedLayers(_zeroLightOffSamePositionedLayers);
            }

            new OperationMove(SlicerFile).Execute(progress);

            return !progress.Token.IsCancellationRequested;
        }

        #endregion
    }
}