Files
UVtools/UVtools.Core/Operations/OperationArithmetic.cs
T
Tiago Conceição 0ba61780b7 v2.4.0
* (Upgrade) EmguCV/OpenCV to v4.5.1
* (Upgrade) AvaloniaUI to 1.0
* (Improvement) GUI re-touched
* (Improvement) Make pixel editor tab to disappear when pixel editor is disabled
* (Improvement) Simplify the output filename from PrusaSlicer profiles
* (Improvement) All operations require a slicer file at constructor rather than on execute, this allow exposure the open file to the operation before run it
* (Improvement) Calibrations: Auto set "Mirror Output" if open file have MirrorDisplay set
* (Change) Tool - Redraw model/supports icon
* (Change) photon and cbddlp to use version 3 by default
* (Add) Tool - Dynamic layer height: Analyze and optimize the model with dynamic layer heights, larger angles will slice at lower layer height
        while more straight angles will slice larger layer height. (#131)
* (Add) Calibration - Exposure time finder: Generates test models with various strategies and increments to verify the best exposure time for a given layer height
* (Add) File load checks, trigger error when a file have critical errors and attempt to fix non-critical errors
  * Layers must have an valid image, otherwise trigger an error
  * Layers must have a incremental or equal position Z than it previous, otherwise trigger an error
  * If layer 0 starts at 0mm it will auto fix all layers, it will add Layer Height to the current z at every layer
* (Add) Tool - Edit print parameters: Allow set parameters to each x layers and skip n layers inside the given range.
        This allow the use of optimizations in a layer pattern, for example, to set 3s for a layer but 2.5s for the next.
* (Add) Layer height property to "Layer Data" table: Shows layer height for the slice
* (Fix) When automations applied and file is saved, it will not warn user about file overwrite for the first time save
* (Fix) Tool - Redraw model/supports: Disable apply button when no file selected
* (Fix) Tool - Infill: Lack of equality member to test if same infill profile already exists
* (Fix) Auto converted files from SL1 where clipping filename at first dot (.), now it only strips known extensions
* (Fix) SL1 encoded files wasn't generating the right information and lead to printer crash
* (Fix) PrusaSlicer printer "Anycubic Photon S" LiftSpeed was missing and contains a typo (#135)
* (Fix) PrusaSlicer profile manager wasnt marking missing profiles to be installed (#135)
* (Fix) PrusaSlicer folder search on linux to also look at %HOME%/.config/PrusaSlicer (#135, #136)
* (Fix) Operations were revised and some bug fixed, most about can't cancel the progress
* (Fix) Some typos on tooltips
* (Fix) Prevent PhotonS from enconding, it will trigger error now as this format is read-only
* **(Fix) Ctrl + Shift + Z to redo the last operation:**
  * The layer range is reseted instead of pull the used values
  * Tool - Arithmetic always disabled
  * Action - Layer import didn't generate info and always disabled
2021-02-06 22:09:55 +00:00

293 lines
10 KiB
C#

/*
* 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.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using Emgu.CV;
using UVtools.Core.FileFormats;
using UVtools.Core.Objects;
namespace UVtools.Core.Operations
{
[Serializable]
public class OperationArithmetic : Operation
{
#region Members
private string _sentence;
#endregion
#region Enums
public enum ArithmeticOperators : byte
{
None,
Add,
Subtract,
Multiply,
Divide,
BitwiseAnd,
BitwiseOr,
BitwiseXor
}
#endregion
#region SubClasses
public sealed class ArithmeticOperation
{
public uint LayerIndex { get; }
public ArithmeticOperators Operator { get; }
public ArithmeticOperation(uint layerIndex, ArithmeticOperators arithmeticOperator)
{
LayerIndex = layerIndex;
Operator = arithmeticOperator;
}
}
#endregion
#region Overrides
public override string Title => "Arithmetic";
public override string Description =>
"Perform arithmetic operations over the layers pixels.\n\n" +
"Available operators:\n" +
" + - * / = Add, Subtract, Multiply, Divide\n" +
" & | ^ = Bitwise AND, OR, XOR\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\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.";
public override string ConfirmationText =>
$"perform this arithmetic operation";
public override string ProgressTitle =>
$"performing the arithmetic operations";
public override string ProgressAction => "Calculated layers";
public override StringTag Validate(params object[] parameters)
{
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 (Operations.Count == 0)
sb.AppendLine("No operations to perform.");
return new StringTag(sb.ToString());
}
public override string ToString()
{
var result = $"{_sentence}" + LayerRangeString;
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<ArithmeticOperation> Operations { get; } = new();
[XmlIgnore]
public List<uint> SetLayers { get; } = new List<uint>();
public bool IsValid => SetLayers.Count > 0 & Operations.Count > 0;
#endregion
#region Constructor
public OperationArithmetic() { }
public OperationArithmetic(FileFormat slicerFile) : base(slicerFile) { }
#endregion
#region Methods
public bool Parse()
{
if (string.IsNullOrEmpty(_sentence)) return false;
SetLayers.Clear();
Operations.Clear();
var splitSentence = _sentence.Split('=');
var operations = splitSentence[0];
if (splitSentence.Length >= 2)
{
operations = splitSentence[1];
var setLayers = splitSentence[0].Replace(" ", string.Empty).Split(',');
foreach (var layer in setLayers)
{
if (!uint.TryParse(layer.Trim(), out var layerIndex)) continue;
if (SetLayers.Contains(layerIndex)) continue;
SetLayers.Add(layerIndex);
}
}
operations = operations.Replace(" ", string.Empty);
if (string.IsNullOrWhiteSpace(operations)) return false;
string layerIndexStr = string.Empty;
foreach (char c in operations)
{
if (c >= '0' && c <= '9')
{
layerIndexStr += c;
continue;
}
ArithmeticOperators op = ArithmeticOperators.None;
switch (c)
{
case '+':
op = ArithmeticOperators.Add;
break;
case '-':
op = ArithmeticOperators.Subtract;
break;
case '*':
op = ArithmeticOperators.Multiply;
break;
case '/':
op = ArithmeticOperators.Divide;
break;
case '&':
op = ArithmeticOperators.BitwiseAnd;
break;
case '|':
op = ArithmeticOperators.BitwiseOr;
break;
case '^':
op = ArithmeticOperators.BitwiseXor;
break;
}
if (op == ArithmeticOperators.None // No valid operator
|| string.IsNullOrWhiteSpace(layerIndexStr) // Started with a operator instead of layer
) continue;
if (uint.TryParse(layerIndexStr, out var layerIndex))
{
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))
{
Operations.Add(new ArithmeticOperation(layerIndex, ArithmeticOperators.None));
}
}
if (Operations.Count == 0) return false;
if (SetLayers.Count == 0)
{
SetLayers.Add(Operations[0].LayerIndex);
}
return true;
}
protected override bool ExecuteInternally(OperationProgress progress)
{
if (!IsValid) return false;
using Mat result = SlicerFile[Operations[0].LayerIndex].LayerMat;
Mat resultRoi = GetRoiOrDefault(result);
for (int i = 1; i < Operations.Count; i++)
{
progress.Token.ThrowIfCancellationRequested();
using var image = SlicerFile[Operations[i].LayerIndex].LayerMat;
Mat imageRoi = GetRoiOrDefault(image);
switch (Operations[i - 1].Operator)
{
case ArithmeticOperators.Add:
CvInvoke.Add(resultRoi, imageRoi, resultRoi);
break;
case ArithmeticOperators.Subtract:
CvInvoke.Subtract(resultRoi, imageRoi, resultRoi);
break;
case ArithmeticOperators.Multiply:
CvInvoke.Multiply(resultRoi, imageRoi, resultRoi);
break;
case ArithmeticOperators.Divide:
CvInvoke.Divide(resultRoi, imageRoi, resultRoi);
break;
case ArithmeticOperators.BitwiseAnd:
CvInvoke.BitwiseAnd(resultRoi, imageRoi, resultRoi);
break;
case ArithmeticOperators.BitwiseOr:
CvInvoke.BitwiseOr(resultRoi, imageRoi, resultRoi);
break;
case ArithmeticOperators.BitwiseXor:
CvInvoke.BitwiseXor(resultRoi, imageRoi, resultRoi);
break;
}
}
Parallel.ForEach(SetLayers, layerIndex =>
{
if (progress.Token.IsCancellationRequested) return;
if (Operations.Count == 1 && HaveROI)
{
var mat = SlicerFile[layerIndex].LayerMat;
var matRoi = GetRoiOrDefault(mat);
resultRoi.CopyTo(matRoi);
SlicerFile[layerIndex].LayerMat = mat;
return;
}
SlicerFile[layerIndex].LayerMat = result;
});
return !progress.Token.IsCancellationRequested;
}
#endregion
#region Equality
protected bool Equals(OperationArithmetic 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((OperationArithmetic) obj);
}
public override int GetHashCode()
{
return (_sentence != null ? _sentence.GetHashCode() : 0);
}
#endregion
}
}