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
|
/*
* 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.Linq;
using System.Text;
using System.Threading.Tasks;
using Emgu.CV;
using UVtools.Core.Extensions;
using UVtools.Core.FileFormats;
using UVtools.Core.Objects;
namespace UVtools.Core.Operations
{
[Serializable]
public sealed class OperationLayerReHeight : Operation
{
#region Members
private OperationLayerReHeightItem _item;
private bool _antiAliasing = true;
#endregion
#region Overrides
public override Enumerations.LayerRangeSelection StartLayerRangeSelection => Enumerations.LayerRangeSelection.None;
public override bool CanROI => false;
public override string Title => "Adjust layer height";
public override string Description =>
"Adjust the layer height of the model\n\n" +
"Adjusting to values lower than current height will reduce layer lines, adjusting to values higher" +
" than current height will reduce model detail.\n\n" +
"Note: Using dedicated slicer software to re-slice will usually yeild better results.";
public override string ConfirmationText =>
$"adjust layer height to {Item.LayerHeight}mm?";
public override string ProgressTitle =>
$"Adjusting layer height to {Item.LayerHeight}mm";
public override string ProgressAction => "Height adjusted layers";
public override bool CanHaveProfiles => false;
public override string ValidateInternally()
{
var sb = new StringBuilder();
if (Item is null)
{
sb.AppendLine("No valid configurations, unable to proceed.");
}
return sb.ToString();
}
public override string ToString()
{
var result = $"[Layer Count: {Item.LayerCount}] [Layer Height: {Item.LayerHeight}]" + LayerRangeString;
if (!string.IsNullOrEmpty(ProfileName)) result = $"{ProfileName}: {result}";
return result;
}
#endregion
#region Properties
public OperationLayerReHeightItem Item
{
get => _item;
set
{
if(!RaiseAndSetIfChanged(ref _item, value)) return;
RaisePropertyChanged(nameof(AntiAliasing));
}
}
public bool CanAntiAliasing => _item?.IsMultiply ?? false;
public bool AntiAliasing
{
get => _antiAliasing;
set => RaiseAndSetIfChanged(ref _antiAliasing, value);
}
public static OperationLayerReHeightItem[] GetItems(uint layerCount, decimal layerHeight)
{
var list = new List<OperationLayerReHeightItem>();
for (byte i = 2; i < 255; i++) // Go lower heights
{
if (layerHeight / i < Layer.MinimumHeight) break;
if ((layerCount * (decimal)i).DecimalDigits() > 0) continue; // Cant multiply layers, no half layers!
if ((layerHeight / i).DecimalDigits() > Layer.HeightPrecision) continue; // Cant divide height, more than 3 digits
var item = new OperationLayerReHeightItem(false, i, Layer.RoundHeight(layerHeight / i), layerCount * i);
list.Add(item);
}
for (byte i = 2; i < 255; i++) // Go higher heights
{
if (layerHeight * i > Layer.MaximumHeight) break;
if ((layerCount / (decimal)i).DecimalDigits() > 0) continue; // Cant divide layers, no half layers!
if ((layerHeight * i).DecimalDigits() > Layer.HeightPrecision) continue; // Cant multiply height, more than 3 digits
var item = new OperationLayerReHeightItem(true, i, Layer.RoundHeight(layerHeight * i), layerCount / i);
list.Add(item);
}
return list.ToArray();
}
#endregion
#region Constructor
public OperationLayerReHeight() { }
public OperationLayerReHeight(FileFormat slicerFile) : base(slicerFile) { }
#endregion
#region Subclasses
public class OperationLayerReHeightItem
{
public bool IsMultiply { get; }
public bool IsDivision => !IsMultiply;
public byte Modifier { get; }
public decimal LayerHeight { get; }
public uint LayerCount { get; }
public OperationLayerReHeightItem(bool isMultiply, byte modifier, decimal layerHeight, uint layerCount)
{
IsMultiply = isMultiply;
Modifier = modifier;
LayerHeight = layerHeight;
LayerCount = layerCount;
}
public override string ToString()
{
return (IsMultiply ? 'x' : '÷') + $" {Modifier} → {LayerCount} layers at {LayerHeight}mm";
}
}
#endregion
#region Methods
protected override bool ExecuteInternally(OperationProgress progress)
{
progress.ItemCount = _item.LayerCount;
var layers = new Layer[_item.LayerCount];
if (_item.IsDivision)
{
uint newLayerIndex = 0;
for (uint layerIndex = 0; layerIndex < SlicerFile.LayerCount; layerIndex++)
{
progress.Token.ThrowIfCancellationRequested();
var oldLayer = SlicerFile[layerIndex];
for (byte i = 0; i < _item.Modifier; i++)
{
var newLayer = oldLayer.Clone();
//newLayer.Index = newLayerIndex;
//newLayer.PositionZ = (float)(Item.LayerHeight * (newLayerIndex + 1));
layers[newLayerIndex] = newLayer;
newLayerIndex++;
progress++;
}
}
}
else
{
var layerIndexes = new uint[SlicerFile.LayerCount / _item.Modifier];
for (uint i = 0; i < layerIndexes.Length; i++)
{
layerIndexes[i] = i * _item.Modifier;
}
Parallel.ForEach(layerIndexes, layerIndex =>
{
if (progress.Token.IsCancellationRequested) return;
var oldLayer = SlicerFile[layerIndex];
using var mat = oldLayer.LayerMat;
using var original = mat.Clone();
for (byte i = 1; i < Item.Modifier; i++)
{
using var nextMat = SlicerFile[layerIndex+i].LayerMat;
CvInvoke.Add(mat, nextMat, mat);
}
/*if (_antiAliasing)
{
CvInvoke.Subtract(mat, original, mat);
CvInvoke.PyrDown(mat, mat);
CvInvoke.PyrUp(mat, mat);
CvInvoke.Add(original, mat, mat);
}*/
var newLayer = oldLayer.Clone();
//newLayer.Index = newLayerIndex;
//newLayer.PositionZ = (float)(Item.LayerHeight * (newLayerIndex + 1));
newLayer.LayerMat = mat;
layers[layerIndex / _item.Modifier] = newLayer;
progress.LockAndIncrement();
});
}
SlicerFile.LayerHeight = (float)Item.LayerHeight;
SlicerFile.LayerManager.Layers = layers;
return !progress.Token.IsCancellationRequested;
}
#endregion
}
}
|