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

OperationLayerArithmetic.cs « Operations « UVtools.Core - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7e027a761a7c2f9d730f0b8dc57248d838fca7a1 (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
335
336
337
/*
 *                     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 System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Serialization;
using UVtools.Core.FileFormats;

namespace UVtools.Core.Operations;

[Serializable]
public class OperationLayerArithmetic : Operation
{
    #region Members
    private string _sentence = null!;
    #endregion

    #region Enums
    public enum LayerArithmeticOperators : byte
    {
        None,
        Add,
        Subtract,
        Multiply,
        Divide,
        BitwiseAnd,
        BitwiseOr,
        BitwiseXor,
        AbsDiff
    }
    #endregion

    #region SubClasses
    public sealed class ArithmeticOperation
    {
        public uint LayerIndex { get; }
        public LayerArithmeticOperators Operator { get; }

        public ArithmeticOperation(uint layerIndex, LayerArithmeticOperators layerArithmeticOperator)
        {
            LayerIndex = layerIndex;
            Operator = layerArithmeticOperator;
        }
    }

    public sealed class ArithmeticOperationGroup
    {
        public List<uint> SetLayers { get; set; } = new();
        public List<ArithmeticOperation> Operations { get; set; } = new();

        public bool IsValid =>
            SetLayers.Count > 0 && Operations.Count > 0 &&
            !(SetLayers.Count == 1 && Operations.Count == 1 && SetLayers[0] == Operations[0].LayerIndex);
    }
    #endregion

    #region Overrides

    public override LayerRangeSelection StartLayerRangeSelection => LayerRangeSelection.None;
    public override string IconClass => "fa-solid fa-square-root-alt";
    public override string Title => "Layer arithmetic";
    public override string Description =>
        "Perform arithmetic operations over the layers\n" +
        "Available operators:\n" +
        " +  -  *  /  = Add, Subtract, Multiply, Divide\n" +
        " &  |  ^  = Bitwise AND, OR, XOR\n" +
        " $ = Absolute difference\n\n" +
        "Syntax: <set_to_layer_indexes> = <layer_index> <operator> <layer_index>\n" +
        "When: \"<set_to_layer_indexes> =\" is omitted, the result will assign to the first layer on the sentence.\n\n" +
        "Example 1: 10+11\n" +
        "Example 2: 10,11,12 = 11+12-10*5   Same as: 10:12 = 11+12-10*5\n" +
        "On example 1 the layer 10 will be set with the result of layer 10 plus layer 11.\n" +
        "On example 2 the layers 10,11,12 will be set with the result of layer 11 plus 12 minus 10 all multiplied by layer 5.\n\n" +
        "Note: Calculation are made sequential, math order rules wont apply here.\n" +
        "Use ; to split and start a new arithmetic operation.";

    public override string ConfirmationText =>
        $"perform this arithmetic operation";

    public override string ProgressTitle =>
        $"performing the arithmetic operations";

    public override string ProgressAction => "Calculated layers";

    public override string? ValidateInternally()
    {
        var sb = new StringBuilder();
        if (string.IsNullOrWhiteSpace(_sentence))
            sb.AppendLine("The sentence is empty.");
        else if(!Parse())
            sb.AppendLine("Unable to parse the sentence, malformed or incomplete.");
        /*else if (SetLayers.Count == 0)
            sb.AppendLine("No layers to assign.");
        else if (Core.Operations.Count == 0)
            sb.AppendLine("No operations to perform.");*/
        else if (!IsValid)
            sb.AppendLine("The operation will have no impact and will not be performed.");

        return sb.ToString();
    }

    public override string ToString()
    {
        var result = $"{_sentence}";
        if (!string.IsNullOrEmpty(ProfileName)) result = $"{ProfileName}: {result}";
        return result;
    }
    #endregion

    #region Properties
    public string Sentence
    {
        get => _sentence;
        set => RaiseAndSetIfChanged(ref _sentence, value);
    }
    [XmlIgnore]
    public List<ArithmeticOperationGroup> Operations { get; private set; } = new();

    public bool IsValid => Operations.Count > 0;
    #endregion

    #region Constructor

    public OperationLayerArithmetic() { }

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

    #endregion

    #region Methods

    public bool Parse()
    {
        if (string.IsNullOrEmpty(_sentence)) return false;
        Operations.Clear();

        
        var sentences = Regex.Replace(_sentence, @"\s+", string.Empty)
            .Split(';', StringSplitOptions.RemoveEmptyEntries);

        foreach (var sentence in sentences)
        {
            var group = new ArithmeticOperationGroup();
            var splitSentence = sentence.Split('=');
            var operations = splitSentence[0];
            if (splitSentence.Length >= 2)
            {
                operations = splitSentence[1];
                var setLayers = splitSentence[0].Split(',', StringSplitOptions.RemoveEmptyEntries);
                foreach (var layer in setLayers)
                {
                    var rangeSplit = layer.Split(':', StringSplitOptions.RemoveEmptyEntries);
                    if (rangeSplit.Length > 1)
                    {
                        uint.TryParse(rangeSplit[0], out var startLayer);
                        if (!uint.TryParse(rangeSplit[1], out var endLayer)) endLayer = SlicerFile.LastLayerIndex;
                        endLayer = Math.Min(endLayer, SlicerFile.LastLayerIndex);
                        for (var index = startLayer; index <= endLayer; index++)
                        {
                            if (group.SetLayers.Contains(index)) continue;
                            group.SetLayers.Add(index);
                        }
                        continue;
                    }

                    if (!uint.TryParse(layer, out var layerIndex)) continue;
                    if (group.SetLayers.Contains(layerIndex)) continue;
                    group.SetLayers.Add(layerIndex);
                }
            }

            group.SetLayers.Sort();

            if (string.IsNullOrWhiteSpace(operations)) return false;

            string layerIndexStr = string.Empty;
            foreach (char c in operations)
            {
                if (c is >= '0' and <= '9')
                {
                    layerIndexStr += c;
                    continue;
                }

                var op = c switch
                {
                    '+' => LayerArithmeticOperators.Add,
                    '-' => LayerArithmeticOperators.Subtract,
                    '*' => LayerArithmeticOperators.Multiply,
                    '/' => LayerArithmeticOperators.Divide,
                    '&' => LayerArithmeticOperators.BitwiseAnd,
                    '|' => LayerArithmeticOperators.BitwiseOr,
                    '^' => LayerArithmeticOperators.BitwiseXor,
                    '$' => LayerArithmeticOperators.AbsDiff,
                    _ => LayerArithmeticOperators.None
                };

                if (op == LayerArithmeticOperators.None  // No valid operator
                    || string.IsNullOrWhiteSpace(layerIndexStr) // Started with a operator instead of layer
                   ) continue;


                if (uint.TryParse(layerIndexStr, out var layerIndex) && layerIndex <= SlicerFile.LastLayerIndex)
                {
                    group.Operations.Add(new ArithmeticOperation(layerIndex, op));
                }

                // Reset layer string
                layerIndexStr = string.Empty;
            }

            // Append the left over
            if (!string.IsNullOrWhiteSpace(layerIndexStr))
            {
                if (uint.TryParse(layerIndexStr, out var layerIndex) && layerIndex <= SlicerFile.LastLayerIndex)
                {
                    group.Operations.Add(new ArithmeticOperation(layerIndex, LayerArithmeticOperators.None));
                }
            }

            //if (Operations.Count == 0) return false;
            if (group.SetLayers.Count == 0 && group.Operations.Count > 0)
            {
                group.SetLayers.Add(group.Operations[0].LayerIndex);
            }

            Operations.Add(group);
        }

        return true;
    }

    protected override bool ExecuteInternally(OperationProgress progress)
    {
        if (!IsValid) return false;

        foreach (var operation in Operations)
        {
            if(!operation.IsValid) continue;

            progress.ThrowIfCancellationRequested();
            using var result = SlicerFile[operation.Operations[0].LayerIndex].LayerMat;
            using var resultRoi = GetRoiOrDefault(result);
            using var imageMask = GetMask(resultRoi);

            progress.ItemCount = (uint)operation.Operations.Count;
            for (int i = 1; i < operation.Operations.Count; i++)
            {
                progress.ThrowIfCancellationRequested();
                using var image = SlicerFile[operation.Operations[i].LayerIndex].LayerMat;
                var imageRoi = GetRoiOrDefault(image);

                switch (operation.Operations[i - 1].Operator)
                {
                    case LayerArithmeticOperators.Add:
                        CvInvoke.Add(resultRoi, imageRoi, resultRoi, imageMask);
                        break;
                    case LayerArithmeticOperators.Subtract:
                        CvInvoke.Subtract(resultRoi, imageRoi, resultRoi, imageMask);
                        break;
                    case LayerArithmeticOperators.Multiply:
                        CvInvoke.Multiply(resultRoi, imageRoi, resultRoi);
                        break;
                    case LayerArithmeticOperators.Divide:
                        CvInvoke.Divide(resultRoi, imageRoi, resultRoi);
                        break;
                    case LayerArithmeticOperators.BitwiseAnd:
                        CvInvoke.BitwiseAnd(resultRoi, imageRoi, resultRoi, imageMask);
                        break;
                    case LayerArithmeticOperators.BitwiseOr:
                        CvInvoke.BitwiseOr(resultRoi, imageRoi, resultRoi, imageMask);
                        break;
                    case LayerArithmeticOperators.BitwiseXor:
                        CvInvoke.BitwiseXor(resultRoi, imageRoi, resultRoi, imageMask);
                        break;
                    case LayerArithmeticOperators.AbsDiff:
                        CvInvoke.AbsDiff(resultRoi, imageRoi, resultRoi);
                        break;
                }

                progress++;
            }

            progress.Reset("Applied layers", (uint)operation.SetLayers.Count);
            Parallel.ForEach(operation.SetLayers, CoreSettings.GetParallelOptions(progress), layerIndex =>
            {
                progress.LockAndIncrement();
                if (operation.Operations.Count == 1 || HaveROIorMask)
                {
                    using var mat = SlicerFile[layerIndex].LayerMat;
                    var matRoi = GetRoiOrDefault(mat);
                    resultRoi.CopyTo(matRoi, imageMask);
                    SlicerFile[layerIndex].LayerMat = mat;
                    return;
                }

                //ApplyMask(mat, resultRoi, imageMask);

                SlicerFile[layerIndex].LayerMat = result;
            });
        }
        

        return !progress.Token.IsCancellationRequested;
    }

    #endregion

    #region Equality
    protected bool Equals(OperationLayerArithmetic other)
    {
        return _sentence == other._sentence;
    }

    public override bool Equals(object? obj)
    {
        if (obj is null) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != GetType()) return false;
        return Equals((OperationLayerArithmetic) obj);
    }

    public override int GetHashCode()
    {
        return (_sentence != null ? _sentence.GetHashCode() : 0);
    }
    #endregion
}