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

OperationInfill.cs « Operations « UVtools.Core - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b07ba06e2327cd3c0f4f38b705f5429a20c0f763 (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
/*
 *                     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.Threading.Tasks;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using UVtools.Core.Extensions;
using UVtools.Core.FileFormats;

namespace UVtools.Core.Operations
{
    [Serializable]
    public sealed class OperationInfill : Operation
    {
        #region Members
        private InfillAlgorithm _infillType = InfillAlgorithm.CubicDynamicLink;
        private ushort _wallThickness = 64;
        private ushort _infillThickness = 45;
        private ushort _infillSpacing = 160;
        private ushort _infillBrightness = 255;
        #endregion

        #region Overrides

        public override string Title => "Infill";

        public override string Description =>
            $"Generate infill patterns in the model\n\nNOTES:\n1) You must exclude floor and ceil layers from the range.\n2) You must take care of drain holes after the operation.";

        public override string ConfirmationText =>
            $"infill model with {InfillType} from layers {LayerIndexStart} through {LayerIndexEnd}?";

        public override string ProgressTitle =>
            $"Infill model with {InfillType} from layers {LayerIndexStart} through {LayerIndexEnd}";

        public override string ProgressAction => "Infilled layers";

        #endregion

        #region Enums
        public enum InfillAlgorithm
        {
            //Rhombus,
            Cubic,
            CubicCenterLink,
            CubicDynamicLink,
            CubicInterlinked,
        }
        #endregion

        #region Properties
        public static Array InfillAlgorithmTypes => Enum.GetValues(typeof(InfillAlgorithm));
        public InfillAlgorithm InfillType
        {
            get => _infillType;
            set => RaiseAndSetIfChanged(ref _infillType, value);
        }

        public ushort WallThickness
        {
            get => _wallThickness;
            set => RaiseAndSetIfChanged(ref _wallThickness, value);
        }

        public ushort InfillBrightness
        {
            get => _infillBrightness;
            set => RaiseAndSetIfChanged(ref _infillBrightness, value);
        }

        public ushort InfillThickness
        {
            get => _infillThickness;
            set => RaiseAndSetIfChanged(ref _infillThickness, value);
        }

        public ushort InfillSpacing
        {
            get => _infillSpacing;
            set => RaiseAndSetIfChanged(ref _infillSpacing, value);
        }

        public override string ToString()
        {
            var result = $"[{_infillType}] [Wall: {_wallThickness}px] [B: {_infillBrightness}px] [T: {_infillThickness}px] [S: {_infillSpacing}px]" + LayerRangeString;
            if (!string.IsNullOrEmpty(ProfileName)) result = $"{ProfileName}: {result}";
            return result;
        }

        #endregion

        #region Constructor

        public OperationInfill() { }

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

        #endregion

        #region Equality

        private bool Equals(OperationInfill other)
        {
            return _infillType == other._infillType && _wallThickness == other._wallThickness && _infillThickness == other._infillThickness && _infillSpacing == other._infillSpacing && _infillBrightness == other._infillBrightness;
        }

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

        public override int GetHashCode()
        {
            return HashCode.Combine((int) _infillType, _wallThickness, _infillThickness, _infillSpacing, _infillBrightness);
        }

        #endregion

        #region Methods

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

                using var mat = SlicerFile[layerIndex].LayerMat;
                Execute(mat, layerIndex);
                SlicerFile[layerIndex].LayerMat = mat;

                progress.LockAndIncrement();
            });

            return !progress.Token.IsCancellationRequested;
        }

        public override bool Execute(Mat mat, params object[] arguments)
        {
            if (arguments is null || arguments.Length < 1) return false;
            var anchor = new Point(-1, -1);
            var kernel = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(3, 3), anchor);
            uint index = Convert.ToUInt32(arguments[0]);
            uint layerIndex = index - LayerIndexStart;
            var infillColor = new MCvScalar(InfillBrightness);

            Mat patternMask = null;
            using Mat erode = new ();
            using Mat diff = new ();
            Mat target = GetRoiOrDefault(mat);
            using var mask = GetMask(mat);

             
            if (InfillType == InfillAlgorithm.Cubic ||
                InfillType == InfillAlgorithm.CubicCenterLink ||
                InfillType == InfillAlgorithm.CubicDynamicLink ||
                InfillType == InfillAlgorithm.CubicInterlinked)
            {
                using var infillPattern = EmguExtensions.InitMat(new Size(InfillSpacing, InfillSpacing));
                using Mat matPattern = mat.CloneBlank();
                bool firstPattern = true;
                uint accumulator = 0;
                bool dynamicCenter = false;
                while (accumulator < layerIndex)
                {
                    dynamicCenter = !dynamicCenter;
                    firstPattern = true;
                    accumulator += InfillSpacing;

                    if (accumulator >= layerIndex) break;
                    firstPattern = false;
                    accumulator += InfillThickness;
                }

                if (firstPattern)
                {
                    int thickness = InfillThickness / 2;
                    // Top Left
                    CvInvoke.Rectangle(infillPattern,
                        new Rectangle(0, 0, thickness, thickness),
                        infillColor, -1);

                    // Top Right
                    CvInvoke.Rectangle(infillPattern,
                        new Rectangle(infillPattern.Width - thickness, 0, thickness, thickness),
                        infillColor, -1);

                    // Bottom Left
                    CvInvoke.Rectangle(infillPattern,
                        new Rectangle(0, infillPattern.Height - thickness, thickness, thickness),
                        infillColor, -1);

                    // Bottom Right
                    CvInvoke.Rectangle(infillPattern,
                        new Rectangle(infillPattern.Width - thickness, infillPattern.Height - thickness,
                            thickness, thickness),
                        infillColor, -1);

                    // Center cross
                    int margin = (int) (InfillSpacing - accumulator + layerIndex) - thickness;
                    int marginInv = (int) (accumulator - layerIndex) - thickness;

                    if (InfillType == InfillAlgorithm.CubicCenterLink ||
                        (InfillType == InfillAlgorithm.CubicDynamicLink &&
                         dynamicCenter) ||
                        InfillType == InfillAlgorithm.CubicInterlinked)
                    {

                        CvInvoke.Rectangle(infillPattern,
                            new Rectangle(margin, margin, InfillThickness, InfillThickness),
                            infillColor, -1);

                        CvInvoke.Rectangle(infillPattern,
                            new Rectangle(marginInv, marginInv, InfillThickness,
                                InfillThickness),
                            infillColor, -1);

                        CvInvoke.Rectangle(infillPattern,
                            new Rectangle(margin, marginInv, InfillThickness,
                                InfillThickness),
                            infillColor, -1);

                        CvInvoke.Rectangle(infillPattern,
                            new Rectangle(marginInv, margin, InfillThickness,
                                InfillThickness),
                            infillColor, -1);
                    }


                    if (InfillType == InfillAlgorithm.CubicInterlinked ||
                        (InfillType == InfillAlgorithm.CubicDynamicLink &&
                         !dynamicCenter))
                    {
                        CvInvoke.Rectangle(infillPattern,
                            new Rectangle(margin, -thickness, InfillThickness,
                                InfillThickness),
                            infillColor, -1);

                        CvInvoke.Rectangle(infillPattern,
                            new Rectangle(marginInv, -thickness, InfillThickness,
                                InfillThickness),
                            infillColor, -1);

                        CvInvoke.Rectangle(infillPattern,
                            new Rectangle(-thickness, margin, InfillThickness,
                                InfillThickness),
                            infillColor, -1);

                        CvInvoke.Rectangle(infillPattern,
                            new Rectangle(-thickness, marginInv, InfillThickness,
                                InfillThickness),
                            infillColor, -1);

                        CvInvoke.Rectangle(infillPattern,
                            new Rectangle(InfillSpacing - thickness, margin,
                                InfillThickness, InfillThickness),
                            infillColor, -1);

                        CvInvoke.Rectangle(infillPattern,
                            new Rectangle(InfillSpacing - thickness, marginInv,
                                InfillThickness, InfillThickness),
                            infillColor, -1);

                        CvInvoke.Rectangle(infillPattern,
                            new Rectangle(margin, InfillSpacing - thickness,
                                InfillThickness, InfillThickness),
                            infillColor, -1);

                        CvInvoke.Rectangle(infillPattern,
                            new Rectangle(marginInv, InfillSpacing - thickness,
                                InfillThickness, InfillThickness),
                            infillColor, -1);
                    }


                }
                else
                {
                    CvInvoke.Rectangle(infillPattern,
                        new Rectangle(0, 0, InfillSpacing, InfillSpacing),
                        infillColor, InfillThickness);
                }


                {
                    CvInvoke.Repeat(infillPattern, target.Rows / infillPattern.Rows + 1,
                        target.Cols / infillPattern.Cols + 1, matPattern);
                    patternMask = new Mat(matPattern, new Rectangle(0, 0, target.Width, target.Height));
                }
            }

            CvInvoke.Erode(target, erode, kernel, anchor, WallThickness, BorderType.Reflect101,
                default);
            CvInvoke.Subtract(target, erode, diff);
            
            CvInvoke.BitwiseAnd(erode, patternMask, target, mask);
            CvInvoke.Add(target, diff, target, mask);
            patternMask?.Dispose();

            return true;
        }

        #endregion
    }
}