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

OperationPattern.cs « Operations « UVtools.Core - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 97d75f7425b41633469b6b757c7375787f1e37da (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
/*
 *                     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.Drawing;
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 class OperationPattern : Operation
    {
        #region Members
        private Enumerations.Anchor _anchor = Enumerations.Anchor.None;
        private ushort _colSpacing;
        private ushort _rowSpacing;
        private ushort _maxColSpacing;
        private ushort _maxRowSpacing;
        private ushort _cols = 1;
        private ushort _rows = 1;
        private ushort _maxCols;
        private ushort _maxRows;
        private bool _isWithinBoundary = true;
        #endregion

        #region Overrides

        public override string Title => "Pattern";
        public override string Description =>
            "Duplicates the model in a rectangular pattern around the build plate.\n" +
            "Note: Before perform this operation, un-rotate the layer preview to see the real orientation.";
        public override string ConfirmationText =>
            $"pattern the object across {Cols} columns and {Rows} rows?";

        public override string ProgressTitle =>
            $"Patterning the object across {Cols} columns and {Rows} rows";

        public override string ProgressAction => "Patterned layers";

        public override bool CanHaveProfiles => false;

        public override StringTag Validate(params object[] parameters)
        {
            var sb = new StringBuilder();

            if (Cols <= 1 && Rows <= 1)
            {
                sb.AppendLine("Either columns or rows must be greater than 1.");
            }
            
            if (!ValidateBounds())
            {
                sb.AppendLine("Your parameters will put the object outside of the build plate, please adjust the margins.");
            }

            return new StringTag(sb.ToString());
        }

        #endregion

        #region Properties
        public Enumerations.Anchor Anchor
        {
            get => _anchor;
            set => RaiseAndSetIfChanged(ref _anchor, value);
        }

        public uint ImageWidth { get; }
        public uint ImageHeight { get; }

        public ushort Cols
        {
            get => _cols;
            set
            {
                if (!RaiseAndSetIfChanged(ref _cols, value)) return;
                RaisePropertyChanged(nameof(InfoCols));
                RaisePropertyChanged(nameof(InfoWidthStr));
                RaisePropertyChanged(nameof(InfoModelWithinBoundaryStr));
                ValidateBounds();
            }
        }

        public ushort Rows
        {
            get => _rows;
            set
            {
                if (!RaiseAndSetIfChanged(ref _rows, value)) return;
                RaisePropertyChanged(nameof(InfoRows));
                RaisePropertyChanged(nameof(InfoHeightStr));
                RaisePropertyChanged(nameof(InfoModelWithinBoundaryStr));
                ValidateBounds();
            }
        }

        public ushort MaxCols
        {
            get => _maxCols;
            set
            {
                if(!RaiseAndSetIfChanged(ref _maxCols, value)) return;
                RaisePropertyChanged(nameof(InfoCols));
                ValidateBounds();
            }
        }

        public ushort MaxRows
        {
            get => _maxRows;
            set
            {
                if (!RaiseAndSetIfChanged(ref _maxRows, value)) return;
                RaisePropertyChanged(nameof(InfoRows));
                ValidateBounds();
            }
        }

        public ushort ColSpacing
        {
            get => _colSpacing;
            set
            {
                if(!RaiseAndSetIfChanged(ref _colSpacing, value)) return;
                RaisePropertyChanged(nameof(InfoWidthStr));
                RaisePropertyChanged(nameof(InfoModelWithinBoundaryStr));
                ValidateBounds();
            }
        }

        public ushort RowSpacing
        {
            get => _rowSpacing;
            set
            {
                if (!RaiseAndSetIfChanged(ref _rowSpacing, value)) return;
                RaisePropertyChanged(nameof(InfoHeightStr));
                RaisePropertyChanged(nameof(InfoModelWithinBoundaryStr));
                ValidateBounds();
            }
        }

        public ushort MaxColSpacing
        {
            get => _maxColSpacing;
            set => RaiseAndSetIfChanged(ref _maxColSpacing, value);
        }

        public ushort MaxRowSpacing
        {
            get => _maxRowSpacing;
            set => RaiseAndSetIfChanged(ref _maxRowSpacing, value);
        }

        public string InfoCols => $"Columns: {Cols} / {MaxCols}";
        public string InfoRows => $"Rows: {Rows} / {MaxRows}";

        public string InfoWidthStr =>
            $"Width: {GetPatternVolume.Width} (Min: {ROI.Width}, Max: {ImageWidth})";

        public string InfoHeightStr =>
            $"Width: {GetPatternVolume.Height} (Min: {ROI.Height}, Max: {ImageHeight})";

        public bool IsWithinBoundary
        {
            get => _isWithinBoundary;
            set
            {
                if (!RaiseAndSetIfChanged(ref _isWithinBoundary, value)) return;
                RaisePropertyChanged(nameof(InfoModelWithinBoundaryStr));
            }
        }

        public string InfoModelWithinBoundaryStr => "Model within boundary: " + (_isWithinBoundary ? "Yes" : "No");

        public Size GetPatternVolume => new(Cols * ROI.Width + (Cols - 1) * ColSpacing, Rows * ROI.Height + (Rows - 1) * RowSpacing);
        #endregion

        public OperationPattern()
        {
        }

        public OperationPattern(Rectangle srcRoi, Size resolution)
        {
            ImageWidth = (uint) resolution.Width;
            ImageHeight = (uint) resolution.Height;

            SetRoi(srcRoi);
            Fill();
        }

        #region Methods
        public void SetAnchor(byte value)
        {
            Anchor = (Enumerations.Anchor)value;
        }

        public void SetRoi(Rectangle srcRoi)
        {
            ROI = srcRoi;

            MaxCols = (ushort)(ImageWidth / srcRoi.Width);
            MaxRows = (ushort)(ImageHeight / srcRoi.Height);

            MaxColSpacing = CalculateAutoColSpacing(MaxCols);
            MaxRowSpacing = CalculateAutoRowSpacing(MaxRows);
        }

        /// <summary>
        /// Fills the plate with maximum cols and rows
        /// </summary>
        public void Fill()
        {
            Cols = MaxCols;
            ColSpacing = MaxColSpacing;

            Rows = MaxRows;
            RowSpacing = MaxRowSpacing;
        }

        public ushort CalculateAutoColSpacing(ushort cols)
        {
            if (cols <= 1) return 0;
            return (ushort)((ImageWidth - ROI.Width * cols) / cols);
        }

        public ushort CalculateAutoRowSpacing(ushort rows)
        {
            if (rows <= 1) return 0;
            return (ushort)((ImageHeight - ROI.Height * rows) / rows);
        }

        public Rectangle GetRoi(ushort col, ushort row)
        {
            var patternVolume = GetPatternVolume;

            return new Rectangle(new Point(
                (int) (col * ROI.Width + col * ColSpacing + (ImageWidth - patternVolume.Width) / 2), 
                (int) (row * ROI.Height + row * RowSpacing + (ImageHeight - patternVolume.Height) / 2)), ROI.Size);
        }

        public void FillColumnSpacing()
        {
            ColSpacing = CalculateAutoColSpacing(_cols);
        }

        public void FillRowSpacing()
        {
            RowSpacing = CalculateAutoRowSpacing(_rows);
        }

        public bool ValidateBounds()
        {
            var volume = GetPatternVolume;
            return IsWithinBoundary = volume.Width <= ImageWidth && volume.Height <= ImageHeight;
        }

        public override string ToString()
        {
            var result = $"[Rows: {Rows}] [Cols: {Cols}]" + LayerRangeString;
            if (!string.IsNullOrEmpty(ProfileName)) result = $"{ProfileName}: {result}";
            return result;
        }

        public override bool Execute(FileFormat slicerFile, OperationProgress progress = null)
        {
            progress ??= new OperationProgress();
            progress.Reset(ProgressAction, LayerRangeCount);

            Parallel.For(LayerIndexStart, LayerIndexEnd + 1, layerIndex =>
            {
                if (progress.Token.IsCancellationRequested) return;

                using var mat = slicerFile[layerIndex].LayerMat;
                using var layerRoi = new Mat(mat, ROI);
                using var dstLayer = mat.CloneBlank();
                for (ushort col = 0; col < Cols; col++)
                for (ushort row = 0; row < Rows; row++)
                {
                    var roi = GetRoi(col, row);
                    using var dstRoi = new Mat(dstLayer, roi);
                    layerRoi.CopyTo(dstRoi);
                }
                //Execute(mat);
                slicerFile[layerIndex].LayerMat = dstLayer;

                lock (progress.Mutex)
                {
                    progress++;
                }
            });

            slicerFile.LayerManager.BoundingRectangle = Rectangle.Empty;

            progress.Token.ThrowIfCancellationRequested();

            if (Anchor == Enumerations.Anchor.None) return true;
            var operationMove = new OperationMove(slicerFile.LayerManager.BoundingRectangle, ImageWidth, ImageHeight, Anchor)
            {
                LayerIndexStart = LayerIndexStart,
                LayerIndexEnd = LayerIndexEnd
            };
            operationMove.Execute(slicerFile, progress);

            return true;
        }

        /*public override bool Execute(Mat mat, params object[] arguments)
        {
            using var layerRoi = new Mat(mat, ROI);
            using var dstLayer = mat.CloneBlank();
            for (ushort col = 0; col < Cols; col++)
            for (ushort row = 0; row < Rows; row++)
            {
                var dstRoi = new Mat(dstLayer, GetRoi(col, row));
                layerRoi.CopyTo(dstRoi);
            }

            return true;
        }*/

        #endregion
    }
}