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
|
/*
* GNU AFFERO GENERAL PUBLIC LICENSE
* Version 3, 19 November 2007
* Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Xml.Serialization;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using UVtools.Core.Extensions;
using UVtools.Core.Objects;
namespace UVtools.Core.Operations
{
[Serializable]
public sealed class OperationCalibrateElephantFoot : Operation
{
#region Members
private Size _resolution = Size.Empty;
private decimal _layerHeight = 0.05M;
private bool _syncLayers;
private ushort _bottomLayers = 10;
private ushort _normalLayers = 70;
private decimal _bottomExposure = 60;
private decimal _normalExposure = 12;
private byte _margin = 30;
private bool _isErodeEnabled = true;
private byte _erodeStartIteration = 2;
private byte _erodeEndIteration = 6;
private byte _erodeIterationSteps = 1;
private bool _isDimmingEnabled = true;
private byte _dimmingWallThickness = 20;
private byte _dimmingStartBrightness = 140;
private byte _dimmingEndBrightness = 200;
private byte _dimmingBrightnessSteps = 20;
private bool _outputOriginalPart = true;
private bool _enableAntiAliasing = true;
#endregion
#region Overrides
public override bool CanROI => false;
public override bool CanCancel => false;
public override Enumerations.LayerRangeSelection StartLayerRangeSelection => Enumerations.LayerRangeSelection.None;
public override string Title => "Elephant foot";
public override string Description =>
"Generates test models with various strategies and increments to verify the best method/values to remove the elephant foot.\n" +
"Elephant foot is removed when bottom layers are flush and aligned with normal layers.\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 elephant foot test?";
public override string ProgressTitle =>
$"Generating the elephant foot test";
public override string ProgressAction => "Generated";
public override StringTag Validate(params object[] parameters)
{
var sb = new StringBuilder();
if (_isErodeEnabled && _erodeStartIteration > _erodeEndIteration)
{
sb.AppendLine("Erode start iterations can't be higher than end iterations.");
}
if (_isDimmingEnabled && _dimmingStartBrightness > _dimmingEndBrightness)
{
sb.AppendLine("Wall dimming start brightness can't be higher than end brightness.");
}
if (ObjectCount <= 0)
{
sb.AppendLine("No objects to output, please adjust the settings.");
}
return new StringTag(sb.ToString());
}
public override string ToString()
{
var result = $"[Layer Height: {_layerHeight}] " +
$"[Layers: {_bottomLayers}/{_normalLayers}] " +
$"[Exposure: {_bottomExposure}/{_normalExposure}] " +
$"[Margin: {_margin}] [ORI: {_outputOriginalPart}]" +
$"[E: {_erodeStartIteration}-{_erodeEndIteration} S{_erodeIterationSteps}] " +
$"[D: W{_dimmingWallThickness} B{_dimmingStartBrightness}-{_dimmingEndBrightness} S{_dimmingBrightnessSteps}] " +
$"[AA: {_enableAntiAliasing}]";
if (!string.IsNullOrEmpty(ProfileName)) result = $"{ProfileName}: {result}";
return result;
}
#endregion
#region Properties
[XmlIgnore]
public Size Resolution
{
get => _resolution;
set => RaiseAndSetIfChanged(ref _resolution, value);
}
public decimal LayerHeight
{
get => _layerHeight;
set
{
if(!RaiseAndSetIfChanged(ref _layerHeight, Math.Round(value, 2))) return;
RaisePropertyChanged(nameof(BottomHeight));
RaisePropertyChanged(nameof(NormalHeight));
RaisePropertyChanged(nameof(TotalHeight));
}
}
public ushort Microns => (ushort) (LayerHeight * 1000);
public bool SyncLayers
{
get => _syncLayers;
set
{
if(!RaiseAndSetIfChanged(ref _syncLayers, value)) return;
if (_syncLayers)
{
NormalLayers = _bottomLayers;
}
}
}
public ushort BottomLayers
{
get => _bottomLayers;
set
{
if(!RaiseAndSetIfChanged(ref _bottomLayers, value)) return;
if (_syncLayers)
{
NormalLayers = _bottomLayers;
}
RaisePropertyChanged(nameof(BottomHeight));
RaisePropertyChanged(nameof(TotalHeight));
RaisePropertyChanged(nameof(LayerCount));
}
}
public ushort NormalLayers
{
get => _normalLayers;
set
{
if (!RaiseAndSetIfChanged(ref _normalLayers, value)) return;
if (_syncLayers)
{
BottomLayers = _normalLayers;
}
RaisePropertyChanged(nameof(NormalHeight));
RaisePropertyChanged(nameof(TotalHeight));
RaisePropertyChanged(nameof(LayerCount));
}
}
public uint LayerCount => (uint)(_bottomLayers + _normalLayers);
public decimal BottomHeight => Math.Round(LayerHeight * BottomLayers, 2);
public decimal NormalHeight => Math.Round(LayerHeight * NormalLayers, 2);
public decimal TotalHeight => BottomHeight + NormalHeight;
public decimal BottomExposure
{
get => _bottomExposure;
set => RaiseAndSetIfChanged(ref _bottomExposure, Math.Round(value, 2));
}
public decimal NormalExposure
{
get => _normalExposure;
set => RaiseAndSetIfChanged(ref _normalExposure, Math.Round(value, 2));
}
public byte Margin
{
get => _margin;
set => RaiseAndSetIfChanged(ref _margin, value);
}
public bool OutputOriginalPart
{
get => _outputOriginalPart;
set
{
if(!RaiseAndSetIfChanged(ref _outputOriginalPart, value)) return;
RaisePropertyChanged(nameof(ObjectCount));
}
}
public bool EnableAntiAliasing
{
get => _enableAntiAliasing;
set => RaiseAndSetIfChanged(ref _enableAntiAliasing, value);
}
public bool IsErodeEnabled
{
get => _isErodeEnabled;
set
{
if(!RaiseAndSetIfChanged(ref _isErodeEnabled, value)) return;
RaisePropertyChanged(nameof(ErodeObjects));
RaisePropertyChanged(nameof(ObjectCount));
}
}
public byte ErodeStartIteration
{
get => _erodeStartIteration;
set
{
if(!RaiseAndSetIfChanged(ref _erodeStartIteration, value)) return;
RaisePropertyChanged(nameof(ErodeObjects));
RaisePropertyChanged(nameof(ObjectCount));
}
}
public byte ErodeEndIteration
{
get => _erodeEndIteration;
set
{
if(!RaiseAndSetIfChanged(ref _erodeEndIteration, value)) return;
RaisePropertyChanged(nameof(ErodeObjects));
RaisePropertyChanged(nameof(ObjectCount));
}
}
public byte ErodeIterationSteps
{
get => _erodeIterationSteps;
set
{
if(!RaiseAndSetIfChanged(ref _erodeIterationSteps, value)) return;
RaisePropertyChanged(nameof(ErodeObjects));
RaisePropertyChanged(nameof(ObjectCount));
}
}
[XmlIgnore]
public Kernel ErodeKernel { get; set; } = new();
public bool IsDimmingEnabled
{
get => _isDimmingEnabled;
set
{
if(!RaiseAndSetIfChanged(ref _isDimmingEnabled, value)) return;
RaisePropertyChanged(nameof(DimmingObjects));
RaisePropertyChanged(nameof(ObjectCount));
}
}
public byte DimmingWallThickness
{
get => _dimmingWallThickness;
set => RaiseAndSetIfChanged(ref _dimmingWallThickness, value);
}
public byte DimmingStartBrightness
{
get => _dimmingStartBrightness;
set
{
if(!RaiseAndSetIfChanged(ref _dimmingStartBrightness, value)) return;
RaisePropertyChanged(nameof(DimmingStartBrightnessPercent));
RaisePropertyChanged(nameof(DimmingObjects));
RaisePropertyChanged(nameof(ObjectCount));
}
}
public float DimmingStartBrightnessPercent => (float) Math.Round(_dimmingStartBrightness * 100 / 255M, 2);
public byte DimmingEndBrightness
{
get => _dimmingEndBrightness;
set
{
if(!RaiseAndSetIfChanged(ref _dimmingEndBrightness, value)) return;
RaisePropertyChanged(nameof(DimmingEndBrightnessPercent));
RaisePropertyChanged(nameof(DimmingObjects));
RaisePropertyChanged(nameof(ObjectCount));
}
}
public float DimmingEndBrightnessPercent => (float)Math.Round(_dimmingEndBrightness * 100 / 255M, 2);
public byte DimmingBrightnessSteps
{
get => _dimmingBrightnessSteps;
set
{
if(!RaiseAndSetIfChanged(ref _dimmingBrightnessSteps, value)) return;
RaisePropertyChanged(nameof(DimmingObjects));
RaisePropertyChanged(nameof(ObjectCount));
}
}
public int ErodeObjects => _isErodeEnabled ?
(int)Math.Floor((_erodeEndIteration - _erodeStartIteration) / (decimal)_erodeIterationSteps) + 1
: 0;
public int DimmingObjects => _isDimmingEnabled ?
(int) Math.Floor((_dimmingEndBrightness - _dimmingStartBrightness) / (decimal) _dimmingBrightnessSteps) + 1
: 0;
public int ObjectCount => (_outputOriginalPart ? 1 : 0) + ErodeObjects + DimmingObjects;
#endregion
#region Equality
private bool Equals(OperationCalibrateElephantFoot other)
{
return _layerHeight == other._layerHeight && _syncLayers == other._syncLayers && _bottomLayers == other._bottomLayers && _normalLayers == other._normalLayers && _bottomExposure == other._bottomExposure && _normalExposure == other._normalExposure && _margin == other._margin && _isErodeEnabled == other._isErodeEnabled && _erodeStartIteration == other._erodeStartIteration && _erodeEndIteration == other._erodeEndIteration && _erodeIterationSteps == other._erodeIterationSteps && _isDimmingEnabled == other._isDimmingEnabled && _dimmingWallThickness == other._dimmingWallThickness && _dimmingStartBrightness == other._dimmingStartBrightness && _dimmingEndBrightness == other._dimmingEndBrightness && _dimmingBrightnessSteps == other._dimmingBrightnessSteps && _outputOriginalPart == other._outputOriginalPart && _enableAntiAliasing == other._enableAntiAliasing;
}
public override bool Equals(object obj)
{
return ReferenceEquals(this, obj) || obj is OperationCalibrateElephantFoot other && Equals(other);
}
public override int GetHashCode()
{
var hashCode = new HashCode();
hashCode.Add(_layerHeight);
hashCode.Add(_syncLayers);
hashCode.Add(_bottomLayers);
hashCode.Add(_normalLayers);
hashCode.Add(_bottomExposure);
hashCode.Add(_normalExposure);
hashCode.Add(_margin);
hashCode.Add(_isErodeEnabled);
hashCode.Add(_erodeStartIteration);
hashCode.Add(_erodeEndIteration);
hashCode.Add(_erodeIterationSteps);
hashCode.Add(_isDimmingEnabled);
hashCode.Add(_dimmingWallThickness);
hashCode.Add(_dimmingStartBrightness);
hashCode.Add(_dimmingEndBrightness);
hashCode.Add(_dimmingBrightnessSteps);
hashCode.Add(_outputOriginalPart);
hashCode.Add(_enableAntiAliasing);
return hashCode.ToHashCode();
}
#endregion
#region Methods
/// <summary>
/// Gets the bottom and normal layers, 0 = bottom | 1 = normal
/// </summary>
/// <returns></returns>
public Mat[] GetLayers()
{
Mat[] layers = new Mat[2];
var anchor = new Point(-1, -1);
var kernel = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(3, 3), anchor);
layers[0] = EmguExtensions.InitMat(Resolution);
LineType lineType = _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected;
const int length = 250;
const int triangleLength = 50;
const byte startX = 2;
const byte startY = 2;
int x = startX;
int y = startY;
int maxX = x;
int maxY = y;
var pointList = new List<Point> { new(x, y) };
void addPoint()
{
maxX = Math.Max(maxX, x);
maxY = Math.Max(maxY, y);
pointList.Add(new Point(x, y));
}
x += length;
addPoint();
x -= triangleLength;
y += triangleLength;
addPoint();
y += 50;
addPoint();
x += triangleLength;
y += triangleLength;
addPoint();
x -= triangleLength;
y += triangleLength;
addPoint();
x += triangleLength;
y += triangleLength;
addPoint();
x -= triangleLength;
y += triangleLength;
addPoint();
x += triangleLength;
addPoint();
y += triangleLength;
addPoint();
x -= triangleLength;
y += triangleLength;
addPoint();
x = startX;
addPoint();
const byte ellipseHeight = 50;
maxY += ellipseHeight;
using Mat shape = EmguExtensions.InitMat(new Size(maxX + startX, maxY + startY));
CvInvoke.FillPoly(shape, new VectorOfPoint(pointList.ToArray()), EmguExtensions.WhiteByte, lineType);
CvInvoke.Circle(shape, new Point(0, 0), length / 4, EmguExtensions.BlackByte,
-1, lineType);
CvInvoke.Ellipse(shape, new Point(maxX / 2, maxY - ellipseHeight), new Size(maxX / 3, ellipseHeight), 0, 0, 360,
EmguExtensions.WhiteByte, -1, lineType);
CvInvoke.Circle(shape, new Point(length / 2, maxY - 100), length / 5, EmguExtensions.BlackByte, -1, lineType);
int currentX = 0;
int currentY = 0;
maxX = 0;
const FontFace font = FontFace.HersheyDuplex;
const byte fontStartX = 30;
const byte fontStartY = length / 4 + 42;
const double fontScale = 1.3;
const byte fontThickness = 3;
const byte fontMargin = 42;
void addText(Mat mat, ushort number, params string[] text)
{
CvInvoke.PutText(mat, number.ToString(), new Point(100, 55), font, 1.5, EmguExtensions.BlackByte, 4, lineType);
CvInvoke.PutText(mat, "UVtools EP", new Point(fontStartX, fontStartY), font, 0.8, EmguExtensions.BlackByte, 2, lineType);
CvInvoke.PutText(mat, $"{Microns}um", new Point(fontStartX, fontStartY + fontMargin), font, fontScale, EmguExtensions.BlackByte, fontThickness, lineType);
CvInvoke.PutText(mat, $"{BottomExposure}|{NormalExposure}s", new Point(fontStartX, fontStartY + fontMargin * 2), font, fontScale, EmguExtensions.BlackByte, fontThickness, lineType);
if (text is null) return;
for (var i = 0; i < text.Length; i++)
{
CvInvoke.PutText(mat, text[i], new Point(fontStartX, fontStartY + fontMargin * (i + 3)), font,
fontScale, EmguExtensions.BlackByte, fontThickness, lineType);
}
}
ushort count = 0;
if (OutputOriginalPart)
{
using var roi = new Mat(layers[0], new Rectangle(new Point(currentX, currentY), shape.Size));
shape.CopyTo(roi);
addText(roi, ++count, "ORI");
}
else
{
currentX -= shape.Width + Margin;
}
layers[1] = layers[0].Clone();
if (IsErodeEnabled)
{
for (int iteration = ErodeStartIteration;
iteration <= ErodeEndIteration;
iteration += ErodeIterationSteps)
{
currentX += shape.Width + Margin;
maxX = Math.Max(maxX, currentX);
if (currentX + shape.Width >= layers[0].Width)
{
currentX = startX;
currentY += shape.Height + Margin;
}
if (currentY + shape.Height >= layers[0].Height)
{
break; // Insufficient size
}
count++;
using (var roi = new Mat(layers[1], new Rectangle(new Point(currentX, currentY), shape.Size)))
{
shape.CopyTo(roi);
addText(roi, count, $"E: {iteration}i");
}
using (var roi = new Mat(layers[0],
new Rectangle(new Point(currentX, currentY), shape.Size)))
using (var erode = new Mat())
{
CvInvoke.Erode(shape, erode, ErodeKernel.Matrix, ErodeKernel.Anchor, iteration, BorderType.Reflect101,
default);
erode.CopyTo(roi);
addText(roi, count, $"E: {iteration}i");
}
}
}
if (IsDimmingEnabled)
{
for (int brightness = DimmingStartBrightness;
brightness <= DimmingEndBrightness;
brightness += DimmingBrightnessSteps)
{
currentX += shape.Width + Margin;
if (currentX + shape.Width >= layers[0].Width)
{
currentX = 0;
currentY += shape.Height + Margin;
}
if (currentY + shape.Height >= layers[0].Height)
{
break; // Insufficient size
}
count++;
using (var roi = new Mat(layers[1], new Rectangle(new Point(currentX, currentY), shape.Size)))
{
shape.CopyTo(roi);
addText(roi, count, $"W: {DimmingWallThickness}", $"B: {brightness}");
}
using (var roi = new Mat(layers[0],
new Rectangle(new Point(currentX, currentY), shape.Size)))
using (var erode = new Mat())
using (var diff = new Mat())
using (var target = new Mat())
using (var mask = shape.CloneBlank())
{
mask.SetTo(new MCvScalar(brightness));
CvInvoke.Erode(shape, erode, kernel, anchor, DimmingWallThickness, BorderType.Reflect101,
default);
CvInvoke.Subtract(shape, erode, diff);
CvInvoke.BitwiseAnd(diff, mask, target);
CvInvoke.Add(erode, target, target);
target.CopyTo(roi);
addText(roi, count, $"W: {DimmingWallThickness}", $"B: {brightness}");
}
}
}
// Preview
//layers[2] = new Mat(layers[0], new Rectangle(0, 0, Math.Min(layers[0].Width, maxX), Math.Min(layers[0].Height, currentY)));
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, "Elephant Foot 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.White3Byte, fontThickness);
CvInvoke.PutText(thumbnail, $"{ObjectCount} Objects", new Point(xSpacing, ySpacing * 4), fontFace, fontScale, EmguExtensions.White3Byte, fontThickness);
/*thumbnail.SetTo(EmguExtensions.Black3Byte);
CvInvoke.Circle(thumbnail, new Point(400/2, 200/2), 200/2, EmguExtensions.White3Byte, -1);
for (int angle = 0; angle < 360; angle+=20)
{
CvInvoke.Line(thumbnail, new Point(400 / 2, 200 / 2), new Point((int)(400 / 2 + 100 * Math.Cos(angle * Math.PI / 180)), (int)(200 / 2 + 100 * Math.Sin(angle * Math.PI / 180))), new MCvScalar(255, 27, 245), 3);
}
thumbnail.Save("D:\\Thumbnail.png");*/
return thumbnail;
}
#endregion
}
}
|