diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-03-14 13:25:32 +0200 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-03-14 13:25:32 +0200 |
| commit | c5cde62cecfdd413e9902b26b30b0d4dfd05a24d (patch) | |
| tree | bc6cd0fc62c13bc65bcb1eeebfac4f5d6112f7ae /Software/Visual_Studio/Tango.Integration/Operation/DefaultGradientGenerationConfiguration.cs | |
| parent | 81d88a18ac614604befb041a81781ab33eb08067 (diff) | |
| download | Tango-c5cde62cecfdd413e9902b26b30b0d4dfd05a24d.tar.gz Tango-c5cde62cecfdd413e9902b26b30b0d4dfd05a24d.zip | |
Machine Studio v4.0.10
PPC v1.0.9
Diffstat (limited to 'Software/Visual_Studio/Tango.Integration/Operation/DefaultGradientGenerationConfiguration.cs')
| -rw-r--r-- | Software/Visual_Studio/Tango.Integration/Operation/DefaultGradientGenerationConfiguration.cs | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.Integration/Operation/DefaultGradientGenerationConfiguration.cs b/Software/Visual_Studio/Tango.Integration/Operation/DefaultGradientGenerationConfiguration.cs new file mode 100644 index 000000000..2f531a95f --- /dev/null +++ b/Software/Visual_Studio/Tango.Integration/Operation/DefaultGradientGenerationConfiguration.cs @@ -0,0 +1,134 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Media; +using Tango.BL.ColorConversion; +using Tango.BL.Entities; +using Tango.BL.Enumerations; +using Tango.Core; + +namespace Tango.Integration.Operation +{ + /// <summary> + /// Represents the default <see cref="IMachineOperator"/> gradient steps generation configuration. + /// </summary> + public class DefaultGradientGenerationConfiguration : ExtendedObject, IGradientGenerationConfiguration + { + private bool _isEnabled; + /// <summary> + /// Gets or sets a value indicating whether to generate the gradient steps. + /// </summary> + public bool IsEnabled + { + get { return _isEnabled; } + set { _isEnabled = value; RaisePropertyChangedAuto(); } + } + + private int _resolutionCM; + /// <summary> + /// Gets or sets the gradient steps resolution in centimeters. + /// </summary> + public int ResolutionCM + { + get { return _resolutionCM; } + set { _resolutionCM = value; RaisePropertyChangedAuto(); } + } + + /// <summary> + /// Initializes a new instance of the <see cref="DefaultGradientGenerationConfiguration"/> class. + /// </summary> + public DefaultGradientGenerationConfiguration() + { + ResolutionCM = 10; + } + + /// <summary> + /// Creates a collection of brush stops representing the required gradient steps. + /// </summary> + /// <param name="segment">The segment.</param> + /// <param name="processParameters">The process parameters.</param> + /// <param name="progress">Progress callback.</param> + /// <returns></returns> + public List<BrushStop> Generate(Segment segment, ProcessParametersTable processParameters, Action<PreparingJobProgressEventArgs> progress = null) + { + List<BrushStop> stops = new List<BrushStop>(); + + int stopIndex = 1; + + for (double cm = 0; cm < segment.Length; cm += (ResolutionCM / 100d)) + { + double offset = (double)cm / segment.Length; + + var color = GetRelativeColor(segment.BrushStops.ToList(), offset); + var output = TangoColorConverter.GetSuggestions(segment.Job, color); + + BrushStop s = new BrushStop(); + s.Segment = segment; + s.ColorSpace = new ColorSpace(); + s.ColorSpace.Code = ColorSpaces.RGB.ToInt32(); + s.Corrected = true; + s.OffsetPercent = offset * 100d; + s.OffsetMeters = segment.Length * offset; + s.Red = color.R; + s.Green = color.G; + s.Blue = color.B; + s.StopIndex = stopIndex++; + + TangoColorConverter.ApplyBrushStopCorrection(s, processParameters, output); + + stops.Add(s); + + progress?.Invoke(new PreparingJobProgressEventArgs() + { + Job = segment.Job, + Total = segment.Length, + Progress = cm, + }); + } + + progress?.Invoke(new PreparingJobProgressEventArgs() + { + Job = segment.Job, + Total = segment.Length, + Progress = segment.Length, + }); + + return stops; + } + + private Color GetRelativeColor(List<BrushStop> brushStopsCollection, double offset) + { + brushStopsCollection = brushStopsCollection.Select(x => x.ShallowClone()).ToList(); + brushStopsCollection.ForEach(x => x.OffsetPercent = x.OffsetPercent / 100d); + + var point = brushStopsCollection.SingleOrDefault(f => f.OffsetPercent == offset); + if (point != null) return point.Color; + + BrushStop before = brushStopsCollection.Where(w => w.OffsetPercent == brushStopsCollection.Min(m => m.OffsetPercent)).First(); + BrushStop after = brushStopsCollection.Where(w => w.OffsetPercent == brushStopsCollection.Max(m => m.OffsetPercent)).First(); + + foreach (var gs in brushStopsCollection) + { + if (gs.OffsetPercent < offset && gs.OffsetPercent > before.OffsetPercent) + { + before = gs; + } + if (gs.OffsetPercent > offset && gs.OffsetPercent < after.OffsetPercent) + { + after = gs; + } + } + + var color = new Color(); + + color.ScA = (float)((offset - before.OffsetPercent) * (after.Color.ScA - before.Color.ScA) / (after.OffsetPercent - before.OffsetPercent) + before.Color.ScA); + color.ScR = (float)((offset - before.OffsetPercent) * (after.Color.ScR - before.Color.ScR) / (after.OffsetPercent - before.OffsetPercent) + before.Color.ScR); + color.ScG = (float)((offset - before.OffsetPercent) * (after.Color.ScG - before.Color.ScG) / (after.OffsetPercent - before.OffsetPercent) + before.Color.ScG); + color.ScB = (float)((offset - before.OffsetPercent) * (after.Color.ScB - before.Color.ScB) / (after.OffsetPercent - before.OffsetPercent) + before.Color.ScB); + + return color; + } + } +} |
