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
|
/*
* GNU AFFERO GENERAL PUBLIC LICENSE
* Version 3, 19 November 2007
* Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using System;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using UVtools.Core.Extensions;
using UVtools.Core.FileFormats;
using UVtools.Core.Layers;
namespace UVtools.Core.Operations;
[Serializable]
public sealed class OperationCalibrateGrayscale : Operation
{
#region Members
private decimal _layerHeight;
private ushort _bottomLayers;
private ushort _interfaceLayers = 20;
private ushort _normalLayers = 20;
private decimal _bottomExposure;
private decimal _normalExposure;
private ushort _outerMargin = 200;
private ushort _innerMargin = 50;
private bool _enableAntiAliasing = false;
private bool _mirrorOutput;
private byte _startBrightness = 175;
private byte _endBrightness = 255;
private byte _brightnessSteps = 10;
private bool _enableCenterHoleRelief = true;
private ushort _centerHoleDiameter = 200;
private bool _textEnabled = true;
private bool _convertBrightnessToExposureTime;
private bool _enableLineDivisions = true;
private byte _lineDivisionThickness = 30;
private byte _lineDivisionBrightness = 255;
private short _textXOffset;
#endregion
#region Overrides
public override bool CanROI => false;
public override bool CanCancel => false;
public override LayerRangeSelection StartLayerRangeSelection => LayerRangeSelection.None;
public override string IconClass => "fas fa-chart-pie";
public override string Title => "Grayscale";
public override string Description =>
"Generates test models with various strategies and increments to verify the LED power against the grayscale levels.\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 grayscale test?";
public override string ProgressTitle =>
$"Generating the grayscale test";
public override string ProgressAction => "Generated";
public override string? ValidateInternally()
{
var sb = new StringBuilder();
if (_startBrightness > _endBrightness)
{
sb.AppendLine("Start brightness must be lower or equal to end brightness.");
}
if (Divisions <= 0)
{
sb.AppendLine("No divisions to output.");
}
return sb.ToString();
}
public override string ToString()
{
var result = $"[Layer Height: {_layerHeight}] " +
$"[Layers: {_bottomLayers}/{_interfaceLayers}/{_normalLayers}] " +
$"[Exposure: {_bottomExposure}/{_normalExposure}] " +
$"[Margin: {_outerMargin}/{_innerMargin}] " +
$"[B: {_startBrightness}-{_endBrightness} S{_brightnessSteps}] " +
$"[AA: {_enableAntiAliasing}] [Mirror: {_mirrorOutput}]";
if (!string.IsNullOrEmpty(ProfileName)) result = $"{ProfileName}: {result}";
return result;
}
#endregion
#region Constructor
public OperationCalibrateGrayscale() { }
public OperationCalibrateGrayscale(FileFormat slicerFile) : base(slicerFile)
{ }
public override void InitWithSlicerFile()
{
base.InitWithSlicerFile();
if(_layerHeight <= 0) _layerHeight = (decimal)SlicerFile.LayerHeight;
if(_bottomLayers <= 0) _bottomLayers = SlicerFile.BottomLayerCount;
if(_bottomExposure <= 0) _bottomExposure = (decimal)SlicerFile.BottomExposureTime;
if(_normalExposure <= 0) _normalExposure = (decimal)SlicerFile.ExposureTime;
_mirrorOutput = SlicerFile.DisplayMirror != FlipDirection.None;
}
#endregion
#region Properties
public decimal LayerHeight
{
get => _layerHeight;
set
{
if(!RaiseAndSetIfChanged(ref _layerHeight, Layer.RoundHeight(value))) return;
RaisePropertyChanged(nameof(BottomHeight));
RaisePropertyChanged(nameof(InterfaceHeight));
RaisePropertyChanged(nameof(NormalHeight));
RaisePropertyChanged(nameof(TotalHeight));
}
}
public ushort Microns => (ushort) (LayerHeight * 1000);
public ushort BottomLayers
{
get => _bottomLayers;
set
{
if(!RaiseAndSetIfChanged(ref _bottomLayers, value)) return;
RaisePropertyChanged(nameof(BottomHeight));
RaisePropertyChanged(nameof(TotalHeight));
RaisePropertyChanged(nameof(LayerCount));
}
}
public ushort InterfaceLayers
{
get => _interfaceLayers;
set
{
if(!RaiseAndSetIfChanged(ref _interfaceLayers, value)) return;
RaisePropertyChanged(nameof(InterfaceHeight));
RaisePropertyChanged(nameof(TotalHeight));
RaisePropertyChanged(nameof(LayerCount));
}
}
public ushort NormalLayers
{
get => _normalLayers;
set
{
if (!RaiseAndSetIfChanged(ref _normalLayers, value)) return;
RaisePropertyChanged(nameof(NormalHeight));
RaisePropertyChanged(nameof(TotalHeight));
RaisePropertyChanged(nameof(LayerCount));
}
}
public uint LayerCount => (uint) (_bottomLayers + _interfaceLayers + _normalLayers);
public decimal BottomHeight => Layer.RoundHeight(LayerHeight * _bottomLayers);
public decimal InterfaceHeight => Layer.RoundHeight(LayerHeight * _interfaceLayers);
public decimal NormalHeight => Layer.RoundHeight(LayerHeight * _normalLayers);
public decimal TotalHeight => BottomHeight + InterfaceHeight + 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 ushort OuterMargin
{
get => _outerMargin;
set => RaiseAndSetIfChanged(ref _outerMargin, value);
}
public ushort InnerMargin
{
get => _innerMargin;
set => RaiseAndSetIfChanged(ref _innerMargin, value);
}
public bool EnableAntiAliasing
{
get => _enableAntiAliasing;
set => RaiseAndSetIfChanged(ref _enableAntiAliasing, value);
}
public bool MirrorOutput
{
get => _mirrorOutput;
set => RaiseAndSetIfChanged(ref _mirrorOutput, value);
}
public byte StartBrightness
{
get => _startBrightness;
set
{
if (!RaiseAndSetIfChanged(ref _startBrightness, value)) return;
RaisePropertyChanged(nameof(StartBrightnessPercent));
RaisePropertyChanged(nameof(Divisions));
RaisePropertyChanged(nameof(AngleStep));
}
}
public float StartBrightnessPercent => (float)Math.Round(_startBrightness * 100 / 255M, 2);
public byte EndBrightness
{
get => _endBrightness;
set
{
if (!RaiseAndSetIfChanged(ref _endBrightness, value)) return;
RaisePropertyChanged(nameof(EndBrightnessPercent));
RaisePropertyChanged(nameof(Divisions));
RaisePropertyChanged(nameof(AngleStep));
}
}
public float EndBrightnessPercent => (float)Math.Round(_endBrightness * 100 / 255M, 2);
public byte BrightnessSteps
{
get => _brightnessSteps;
set
{
if (!RaiseAndSetIfChanged(ref _brightnessSteps, value)) return;
RaisePropertyChanged(nameof(Divisions));
RaisePropertyChanged(nameof(AngleStep));
}
}
public int Divisions => (int)((_endBrightness - _startBrightness) / (decimal)_brightnessSteps) + 1;
public float AngleStep => 360f / Divisions;
public bool EnableCenterHoleRelief
{
get => _enableCenterHoleRelief;
set => RaiseAndSetIfChanged(ref _enableCenterHoleRelief, value);
}
public ushort CenterHoleDiameter
{
get => _centerHoleDiameter;
set => RaiseAndSetIfChanged(ref _centerHoleDiameter, value);
}
public bool TextEnabled
{
get => _textEnabled;
set => RaiseAndSetIfChanged(ref _textEnabled, value);
}
public bool ConvertBrightnessToExposureTime
{
get => _convertBrightnessToExposureTime;
set => RaiseAndSetIfChanged(ref _convertBrightnessToExposureTime, value);
}
public bool EnableLineDivisions
{
get => _enableLineDivisions;
set => RaiseAndSetIfChanged(ref _enableLineDivisions, value);
}
public byte LineDivisionThickness
{
get => _lineDivisionThickness;
set => RaiseAndSetIfChanged(ref _lineDivisionThickness, value);
}
public byte LineDivisionBrightness
{
get => _lineDivisionBrightness;
set
{
if(!RaiseAndSetIfChanged(ref _lineDivisionBrightness, value)) return;
RaisePropertyChanged(nameof(LineDivisionBrightnessPercent));
}
}
public float LineDivisionBrightnessPercent => (float)Math.Round(_lineDivisionBrightness * 100 / 255M, 2);
public short TextXOffset
{
get => _textXOffset;
set => RaiseAndSetIfChanged(ref _textXOffset, value);
}
#endregion
#region Equality
private bool Equals(OperationCalibrateGrayscale other)
{
return _layerHeight == other._layerHeight && _bottomLayers == other._bottomLayers && _interfaceLayers == other._interfaceLayers && _normalLayers == other._normalLayers && _bottomExposure == other._bottomExposure && _normalExposure == other._normalExposure && _outerMargin == other._outerMargin && _innerMargin == other._innerMargin && _enableAntiAliasing == other._enableAntiAliasing && _mirrorOutput == other._mirrorOutput && _startBrightness == other._startBrightness && _endBrightness == other._endBrightness && _brightnessSteps == other._brightnessSteps && _enableCenterHoleRelief == other._enableCenterHoleRelief && _centerHoleDiameter == other._centerHoleDiameter && _textEnabled == other._textEnabled && _convertBrightnessToExposureTime == other._convertBrightnessToExposureTime && _enableLineDivisions == other._enableLineDivisions && _lineDivisionThickness == other._lineDivisionThickness && _lineDivisionBrightness == other._lineDivisionBrightness && _textXOffset == other._textXOffset;
}
public override bool Equals(object? obj)
{
return ReferenceEquals(this, obj) || obj is OperationCalibrateGrayscale other && Equals(other);
}
public override int GetHashCode()
{
var hashCode = new HashCode();
hashCode.Add(_layerHeight);
hashCode.Add(_bottomLayers);
hashCode.Add(_interfaceLayers);
hashCode.Add(_normalLayers);
hashCode.Add(_bottomExposure);
hashCode.Add(_normalExposure);
hashCode.Add(_outerMargin);
hashCode.Add(_innerMargin);
hashCode.Add(_enableAntiAliasing);
hashCode.Add(_mirrorOutput);
hashCode.Add(_startBrightness);
hashCode.Add(_endBrightness);
hashCode.Add(_brightnessSteps);
hashCode.Add(_enableCenterHoleRelief);
hashCode.Add(_centerHoleDiameter);
hashCode.Add(_textEnabled);
hashCode.Add(_convertBrightnessToExposureTime);
hashCode.Add(_enableLineDivisions);
hashCode.Add(_lineDivisionThickness);
hashCode.Add(_lineDivisionBrightness);
hashCode.Add(_textXOffset);
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[3];
layers[0] = EmguExtensions.InitMat(SlicerFile.Resolution);
int radius = Math.Max(100, Math.Min(SlicerFile.Resolution.Width, SlicerFile.Resolution.Height) - _outerMargin * 2) / 2 ;
Point center = new(SlicerFile.Resolution.Width / 2, SlicerFile.Resolution.Height / 2);
int innerRadius = Math.Max(100, radius - _innerMargin);
double topLineLength = 0;
LineType lineType = _enableAntiAliasing ? LineType.AntiAlias : LineType.EightConnected;
CvInvoke.Circle(layers[0], center, radius, EmguExtensions.WhiteColor, -1, lineType);
layers[1] = layers[0].Clone();
layers[2] = layers[0].Clone();
int i = 0;
for (ushort brightness = _startBrightness; brightness <= _endBrightness; brightness += _brightnessSteps)
{
var radians = new float[2];
var degrees = new SizeF[2];
for (int n = 0; n < 2; n++)
{
radians[n] = -AngleStep * (i + n);
degrees[n] = new SizeF((float) Math.Cos(radians[n] * Math.PI / 180), (float) Math.Sin(radians[n] * Math.PI / 180));
}
Point[] points = new Point[3];
points[0] = center;
points[1] = new(center.X + (int) (innerRadius * degrees[0].Width), center.Y + (int) (innerRadius * degrees[0].Height));
points[2] = new(center.X + (int) (innerRadius * degrees[1].Width), center.Y + (int) (innerRadius * degrees[1].Height));
using var vec = new VectorOfPoint(points);
if (topLineLength == 0) topLineLength = PointExtensions.FindLength(points[1], points[2]);
CvInvoke.FillPoly(layers[2], vec, new MCvScalar(brightness), lineType);
if (_enableLineDivisions && _lineDivisionThickness > 0)
{
CvInvoke.Polylines(layers[2], vec, false, new MCvScalar(_lineDivisionBrightness), _lineDivisionThickness, lineType);
}
i++;
}
FontFace fontFace = FontFace.HersheyDuplex;
double fontScale = 2;
int fontThickness = 5;
if (_textEnabled)
{
Point fontPoint = new((int)(center.X + radius / 2.5f + _textXOffset), (int)(center.Y + AngleStep / 1.5));
var halfAngleStep = AngleStep / 2;
var rotatedAngle = halfAngleStep;
layers[2].Rotate(halfAngleStep);
for (ushort brightness = _startBrightness; brightness <= _endBrightness; brightness += _brightnessSteps)
{
var text = brightness.ToString();
if (_convertBrightnessToExposureTime)
{
text = $"{Math.Round(brightness * _normalExposure / byte.MaxValue, 2)}s";
}
CvInvoke.PutText(layers[2], text, fontPoint, fontFace, fontScale, EmguExtensions.BlackColor, fontThickness, lineType);
rotatedAngle += AngleStep;
layers[2].Rotate(AngleStep);
}
layers[2].Rotate(-rotatedAngle);
}
if (_enableCenterHoleRelief && _centerHoleDiameter > 1)
{
var holeRadius = Math.Min(radius, _centerHoleDiameter) / 2;
if (_innerMargin > 0)
{
CvInvoke.Circle(layers[2], center, holeRadius + _innerMargin, EmguExtensions.WhiteColor, -1, lineType);
}
foreach (var layer in layers)
{
CvInvoke.Circle(layer, center, holeRadius, EmguExtensions.BlackColor, -1, lineType);
}
}
fontScale = 1.5;
fontThickness = 3;
CvInvoke.PutText(layers[0], $"{Microns}um at {_bottomExposure}s/{_normalExposure}s",
new Point(center.X - radius / 2, center.Y + radius / 2 +40),
fontFace, fontScale, EmguExtensions.BlackColor, fontThickness, lineType, true);
CvInvoke.PutText(layers[0], $"{_startBrightness}-{_endBrightness} S:{_brightnessSteps}",
new Point(center.X - radius / 2, center.Y + radius / 2 - 40),
fontFace, fontScale, EmguExtensions.BlackColor, fontThickness, lineType, true);
if (_mirrorOutput)
{
var flip = SlicerFile.DisplayMirror;
if (flip == FlipDirection.None) flip = FlipDirection.Horizontally;
Parallel.ForEach(layers, CoreSettings.ParallelOptions, mat => CvInvoke.Flip(mat, mat, (FlipType)flip));
}
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, "Grayscale 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);
CvInvoke.PutText(thumbnail, $"Divs:{Divisions} Angle:{AngleStep}", new Point(xSpacing, ySpacing * 4), fontFace, fontScale, EmguExtensions.WhiteColor, fontThickness);
return thumbnail;
}
protected override bool ExecuteInternally(OperationProgress progress)
{
progress.ItemCount = LayerCount;
var newLayers = new Layer[LayerCount];
var layers = GetLayers();
var bottomLayer = new Layer(0, layers[0], SlicerFile)
{
IsModified = true
};
var interfaceLayer = InterfaceLayers > 0 ? new Layer(0, layers[1], SlicerFile)
{
IsModified = true
} : null;
var layer = new Layer(0, layers[2], SlicerFile)
{
IsModified = true
};
uint layerIndex = 0;
for (uint i = 0; i < BottomLayers; i++)
{
newLayers[layerIndex] = bottomLayer.Clone();
progress++;
layerIndex++;
}
for (uint i = 0; i < InterfaceLayers; i++)
{
newLayers[layerIndex] = interfaceLayer!.Clone();
progress++;
layerIndex++;
}
for (uint i = 0; i < NormalLayers; i++)
{
newLayers[layerIndex] = layer.Clone();
progress++;
layerIndex++;
}
foreach (var mat in layers)
{
mat?.Dispose();
}
if (SlicerFile.ThumbnailsCount > 0)
SlicerFile.SetThumbnails(GetThumbnail());
SlicerFile.SuppressRebuildPropertiesWork(() =>
{
SlicerFile.LayerHeight = (float)LayerHeight;
SlicerFile.BottomExposureTime = (float)BottomExposure;
SlicerFile.ExposureTime = (float)NormalExposure;
SlicerFile.BottomLayerCount = BottomLayers;
SlicerFile.TransitionLayerCount = 0;
SlicerFile.Layers = newLayers;
}, true);
return !progress.Token.IsCancellationRequested;
}
#endregion
}
|