Refactoring SuspendRebuild to be RubuildLock
Marching squares has more data on how it expects to be processed (edge color)
This commit is contained in:
parent
ec28305b56
commit
3e49946e5a
44 changed files with 531 additions and 195 deletions
|
|
@ -57,6 +57,8 @@ namespace MatterHackers.MatterControl.DesignTools
|
|||
return color.Alpha0To255 / 255.0;
|
||||
}
|
||||
|
||||
public Color ZeroColor => Color.Transparent;
|
||||
|
||||
public double Threshold(Color color)
|
||||
{
|
||||
return GetThresholded0To1(Transform(color));
|
||||
|
|
|
|||
|
|
@ -59,6 +59,8 @@ namespace MatterHackers.MatterControl.DesignTools
|
|||
return h;
|
||||
}
|
||||
|
||||
public Color ZeroColor => Color.Black;
|
||||
|
||||
public double Threshold(Color color)
|
||||
{
|
||||
return GetThresholded0To1(Transform(color));
|
||||
|
|
|
|||
|
|
@ -47,5 +47,10 @@ namespace MatterHackers.MatterControl.DesignTools
|
|||
/// <param name="color"></param>
|
||||
/// <returns></returns>
|
||||
double Threshold(Color color);
|
||||
|
||||
/// <summary>
|
||||
/// The color that represents 0 for this Threshold function
|
||||
/// </summary>
|
||||
Color ZeroColor { get; }
|
||||
}
|
||||
}
|
||||
|
|
@ -30,7 +30,6 @@ either expressed or implied, of the FreeBSD Project.
|
|||
using ClipperLib;
|
||||
using MatterHackers.Agg;
|
||||
using MatterHackers.Agg.Image;
|
||||
using MatterHackers.Agg.Platform;
|
||||
using MatterHackers.Agg.Transform;
|
||||
using MatterHackers.Agg.VertexSource;
|
||||
using MatterHackers.DataConverters3D;
|
||||
|
|
@ -39,7 +38,6 @@ using MatterHackers.MarchingSquares;
|
|||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
|
@ -53,16 +51,20 @@ namespace MatterHackers.MatterControl.DesignTools
|
|||
|
||||
public class ImageToPath : Object3D, IPathObject, IEditorDraw
|
||||
{
|
||||
private ThresholdFunctions _featureDetector = ThresholdFunctions.Silhouette;
|
||||
|
||||
private ImageBuffer _histogramRawCache = null;
|
||||
private ImageBuffer _histogramDisplayCache = null;
|
||||
|
||||
public ImageToPath()
|
||||
{
|
||||
Name = "Path".Localize();
|
||||
Name = "Image to Path".Localize();
|
||||
}
|
||||
|
||||
public enum ThresholdFunctions { Intensity, Alpha, Hue }
|
||||
public enum ThresholdFunctions { Silhouette, Intensity, Alpha, Hue }
|
||||
|
||||
public override bool CanRemove => true;
|
||||
|
||||
ThresholdFunctions _featureDetector = ThresholdFunctions.Intensity;
|
||||
public ThresholdFunctions FeatureDetector
|
||||
{
|
||||
get { return _featureDetector; }
|
||||
|
|
@ -70,29 +72,22 @@ namespace MatterHackers.MatterControl.DesignTools
|
|||
{
|
||||
if (value != _featureDetector)
|
||||
{
|
||||
_histogramCache = null;
|
||||
_histogramRawCache = null;
|
||||
_featureDetector = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Color GetRGBA(byte[] buffer, int offset)
|
||||
{
|
||||
return new Color(buffer[offset + 2], buffer[offset + 1], buffer[offset + 0], buffer[offset + 3]);
|
||||
}
|
||||
|
||||
ImageBuffer _histogramCache = null;
|
||||
[JsonIgnore]
|
||||
public ImageBuffer Histogram
|
||||
{
|
||||
get
|
||||
{
|
||||
_histogramCache = null;
|
||||
if (_histogramCache == null)
|
||||
if (_histogramRawCache == null)
|
||||
{
|
||||
_histogramCache = new ImageBuffer(256, 100);
|
||||
_histogramRawCache = new ImageBuffer(256, 100);
|
||||
var image = Image;
|
||||
var counts = new int[_histogramCache.Width];
|
||||
var counts = new int[_histogramRawCache.Width];
|
||||
var function = ThresholdFunction;
|
||||
|
||||
byte[] buffer = image.GetBuffer();
|
||||
|
|
@ -106,25 +101,25 @@ namespace MatterHackers.MatterControl.DesignTools
|
|||
{
|
||||
int imageBufferOffsetWithX = imageBufferOffset + x * 4;
|
||||
var color = GetRGBA(buffer, imageBufferOffsetWithX);
|
||||
counts[(int)(function.Transform(color) * (_histogramCache.Width - 1))]++;
|
||||
counts[(int)(function.Transform(color) * (_histogramRawCache.Width - 1))]++;
|
||||
}
|
||||
}
|
||||
|
||||
double max = counts.Select((value, index) => new { value, index })
|
||||
.OrderByDescending(vi => vi.value)
|
||||
.First().value;
|
||||
var graphics2D = _histogramCache.NewGraphics2D();
|
||||
graphics2D.Clear(Color.White);
|
||||
for(int i=0; i<256; i++)
|
||||
var graphics2D2 = _histogramRawCache.NewGraphics2D();
|
||||
graphics2D2.Clear(Color.White);
|
||||
for (int i = 0; i < 256; i++)
|
||||
{
|
||||
graphics2D.Line(i, 0, i, Easing.Exponential.Out(counts[i] / max) * _histogramCache.Height, Color.Black);
|
||||
graphics2D2.Line(i, 0, i, Easing.Exponential.Out(counts[i] / max) * _histogramRawCache.Height, Color.Black);
|
||||
}
|
||||
graphics2D.FillRectangle(0, 0, RangeStart * _histogramCache.Width, _histogramCache.Height, new Color(Color.Red, 100));
|
||||
graphics2D.FillRectangle(RangeEnd * _histogramCache.Width, 0, 255, _histogramCache.Height, new Color(Color.Red, 100));
|
||||
graphics2D.Line(RangeStart * _histogramCache.Width, 0, RangeStart * _histogramCache.Width, _histogramCache.Height, new Color(Color.LightGray, 200));
|
||||
graphics2D.Line(RangeEnd * _histogramCache.Width, 0, RangeEnd * _histogramCache.Width, _histogramCache.Height, new Color(Color.LightGray, 200));
|
||||
}
|
||||
return _histogramCache;
|
||||
|
||||
_histogramDisplayCache = new ImageBuffer(_histogramRawCache);
|
||||
UpdateHistogramDisplay();
|
||||
|
||||
return _histogramDisplayCache;
|
||||
}
|
||||
|
||||
set
|
||||
|
|
@ -132,20 +127,37 @@ namespace MatterHackers.MatterControl.DesignTools
|
|||
}
|
||||
}
|
||||
|
||||
private void UpdateHistogramDisplay()
|
||||
{
|
||||
if (_histogramRawCache != null)
|
||||
{
|
||||
var graphics2D = _histogramDisplayCache.NewGraphics2D();
|
||||
graphics2D.Clear(Color.Transparent);
|
||||
_histogramDisplayCache.CopyFrom(_histogramRawCache);
|
||||
graphics2D.FillRectangle(0, 0, RangeStart * _histogramDisplayCache.Width, _histogramDisplayCache.Height, new Color(Color.Red, 100));
|
||||
graphics2D.FillRectangle(RangeEnd * _histogramDisplayCache.Width, 0, 255, _histogramDisplayCache.Height, new Color(Color.Red, 100));
|
||||
graphics2D.Line(RangeStart * _histogramDisplayCache.Width, 0, RangeStart * _histogramDisplayCache.Width, _histogramDisplayCache.Height, new Color(Color.LightGray, 200));
|
||||
graphics2D.Line(RangeEnd * _histogramDisplayCache.Width, 0, RangeEnd * _histogramDisplayCache.Width, _histogramDisplayCache.Height, new Color(Color.LightGray, 200));
|
||||
}
|
||||
}
|
||||
|
||||
[JsonIgnore]
|
||||
private ImageBuffer Image => this.Descendants<ImageObject3D>().FirstOrDefault()?.Image;
|
||||
|
||||
public double RangeStart { get; set; } = .1;
|
||||
public double RangeEnd { get; set; } = 1;
|
||||
|
||||
public IVertexSource VertexSource { get; set; } = new VertexStorage();
|
||||
|
||||
[JsonIgnore]
|
||||
private ImageBuffer Image => this.Descendants<ImageObject3D>().FirstOrDefault()?.Image;
|
||||
|
||||
private IThresholdFunction ThresholdFunction
|
||||
{
|
||||
get
|
||||
{
|
||||
switch (FeatureDetector)
|
||||
{
|
||||
case ThresholdFunctions.Silhouette:
|
||||
return new SilhouetteThresholdFunction(RangeStart, RangeEnd);
|
||||
|
||||
case ThresholdFunctions.Intensity:
|
||||
return new MapOnMaxIntensity(RangeStart, RangeEnd);
|
||||
|
||||
|
|
@ -160,11 +172,6 @@ namespace MatterHackers.MatterControl.DesignTools
|
|||
}
|
||||
}
|
||||
|
||||
public void DrawEditor(object sender, DrawEventArgs e)
|
||||
{
|
||||
ImageToPath.DrawPath(this);
|
||||
}
|
||||
|
||||
public static void DrawPath(IObject3D item)
|
||||
{
|
||||
if (item is IPathObject pathObject)
|
||||
|
|
@ -194,7 +201,7 @@ namespace MatterHackers.MatterControl.DesignTools
|
|||
GL.Color4(255, 0, 0, 255);
|
||||
}
|
||||
|
||||
if(vertex.IsMoveTo)
|
||||
if (vertex.IsMoveTo)
|
||||
{
|
||||
firstMove = position;
|
||||
}
|
||||
|
|
@ -203,7 +210,7 @@ namespace MatterHackers.MatterControl.DesignTools
|
|||
GL.Vertex3(lastPosition.X, lastPosition.Y, aabb.maxXYZ.Z + 0.002);
|
||||
GL.Vertex3(position.X, position.Y, aabb.maxXYZ.Z + 0.002);
|
||||
}
|
||||
else if(vertex.IsClose)
|
||||
else if (vertex.IsClose)
|
||||
{
|
||||
GL.Vertex3(firstMove.X, firstMove.Y, aabb.maxXYZ.Z + 0.002);
|
||||
GL.Vertex3(lastPosition.X, lastPosition.Y, aabb.maxXYZ.Z + 0.002);
|
||||
|
|
@ -224,6 +231,11 @@ namespace MatterHackers.MatterControl.DesignTools
|
|||
}
|
||||
}
|
||||
|
||||
public void DrawEditor(object sender, DrawEventArgs e)
|
||||
{
|
||||
ImageToPath.DrawPath(this);
|
||||
}
|
||||
|
||||
public void GenerateMarchingSquaresAndLines(Action<double, string> progressReporter, ImageBuffer image, IThresholdFunction thresholdFunction)
|
||||
{
|
||||
if (image != null)
|
||||
|
|
@ -231,6 +243,7 @@ namespace MatterHackers.MatterControl.DesignTools
|
|||
// Regenerate outline
|
||||
var marchingSquaresData = new MarchingSquaresByte(
|
||||
image,
|
||||
thresholdFunction.ZeroColor,
|
||||
thresholdFunction.Threshold,
|
||||
0);
|
||||
|
||||
|
|
@ -288,42 +301,51 @@ namespace MatterHackers.MatterControl.DesignTools
|
|||
{
|
||||
if (invalidateType.InvalidateType == InvalidateType.Image
|
||||
&& invalidateType.Source != this
|
||||
&& !RebuildSuspended)
|
||||
&& !RebuildLocked)
|
||||
{
|
||||
Rebuild(null);
|
||||
}
|
||||
else if (invalidateType.InvalidateType == InvalidateType.Properties
|
||||
&& invalidateType.Source == this)
|
||||
{
|
||||
UpdateHistogramDisplay();
|
||||
Rebuild(null);
|
||||
}
|
||||
else
|
||||
{
|
||||
base.OnInvalidate(invalidateType);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Rebuild(UndoBuffer undoBuffer)
|
||||
private Color GetRGBA(byte[] buffer, int offset)
|
||||
{
|
||||
return new Color(buffer[offset + 2], buffer[offset + 1], buffer[offset + 0], buffer[offset + 3]);
|
||||
}
|
||||
|
||||
private void Rebuild(UndoBuffer undoBuffer)
|
||||
{
|
||||
var rebuildLock = RebuildLock();
|
||||
// now create a long running task to process the image
|
||||
ApplicationController.Instance.Tasks.Execute(
|
||||
"Calculate Path".Localize(),
|
||||
(reporter, cancellationToken) =>
|
||||
{
|
||||
var progressStatus = new ProgressStatus();
|
||||
this.GenerateMarchingSquaresAndLines(
|
||||
(progress0to1, status) =>
|
||||
{
|
||||
progressStatus.Progress0To1 = progress0to1;
|
||||
progressStatus.Status = status;
|
||||
reporter.Report(progressStatus);
|
||||
},
|
||||
Image,
|
||||
ThresholdFunction);
|
||||
// now create a long running task to process the image
|
||||
ApplicationController.Instance.Tasks.Execute(
|
||||
"Calculate Path".Localize(),
|
||||
(reporter, cancellationToken) =>
|
||||
{
|
||||
var progressStatus = new ProgressStatus();
|
||||
this.GenerateMarchingSquaresAndLines(
|
||||
(progress0to1, status) =>
|
||||
{
|
||||
progressStatus.Progress0To1 = progress0to1;
|
||||
progressStatus.Status = status;
|
||||
reporter.Report(progressStatus);
|
||||
},
|
||||
Image,
|
||||
ThresholdFunction);
|
||||
|
||||
rebuildLock.Dispose();
|
||||
Invalidate(new InvalidateArgs(this, InvalidateType.Path, null));
|
||||
rebuildLock.Dispose();
|
||||
Invalidate(new InvalidateArgs(this, InvalidateType.Path, null));
|
||||
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
|
||||
base.Rebuild(undoBuffer);
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -95,7 +95,12 @@ namespace MatterHackers.MatterControl.DesignTools
|
|||
|| invalidateType.InvalidateType == InvalidateType.Mesh
|
||||
|| invalidateType.InvalidateType == InvalidateType.Path)
|
||||
&& invalidateType.Source != this
|
||||
&& !RebuildSuspended)
|
||||
&& !RebuildLocked)
|
||||
{
|
||||
Rebuild(null);
|
||||
}
|
||||
else if (invalidateType.InvalidateType == InvalidateType.Properties
|
||||
&& invalidateType.Source == this)
|
||||
{
|
||||
Rebuild(null);
|
||||
}
|
||||
|
|
@ -105,7 +110,7 @@ namespace MatterHackers.MatterControl.DesignTools
|
|||
}
|
||||
}
|
||||
|
||||
public override void Rebuild(UndoBuffer undoBuffer)
|
||||
private void Rebuild(UndoBuffer undoBuffer)
|
||||
{
|
||||
using (RebuildLock())
|
||||
{
|
||||
|
|
|
|||
|
|
@ -49,6 +49,7 @@ namespace MatterHackers.MatterControl.DesignTools
|
|||
|
||||
public double Transform(Color color)
|
||||
{
|
||||
// we invert the gray value so we have black being the color we are finding
|
||||
return (color.Red0To1 * 0.2989) + (color.Blue0To1 * 0.5870) + (color.Green0To1 * 0.1140);
|
||||
}
|
||||
|
||||
|
|
@ -58,6 +59,8 @@ namespace MatterHackers.MatterControl.DesignTools
|
|||
return GetThresholded0To1(Transform(color));
|
||||
}
|
||||
|
||||
public Color ZeroColor => Color.Black;
|
||||
|
||||
protected double GetThresholded0To1(double rawValue)
|
||||
{
|
||||
double outValue = 0;
|
||||
|
|
|
|||
83
DesignTools/Operations/Image/SilhouetteThresholdFunction.cs
Normal file
83
DesignTools/Operations/Image/SilhouetteThresholdFunction.cs
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
/*
|
||||
Copyright (c) 2017, Lars Brubaker, John Lewin
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
The views and conclusions contained in the software and documentation are those
|
||||
of the authors and should not be interpreted as representing official policies,
|
||||
either expressed or implied, of the FreeBSD Project.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using MatterHackers.Agg;
|
||||
|
||||
namespace MatterHackers.MatterControl.DesignTools
|
||||
{
|
||||
public class SilhouetteThresholdFunction : IThresholdFunction
|
||||
{
|
||||
protected double rangeStart = .1;
|
||||
protected double rangeEnd = 1.0;
|
||||
|
||||
public SilhouetteThresholdFunction()
|
||||
{
|
||||
}
|
||||
|
||||
public SilhouetteThresholdFunction(double rangeStart, double rangeEnd)
|
||||
{
|
||||
this.rangeStart = Math.Max(0, Math.Min(1, rangeStart));
|
||||
this.rangeEnd = Math.Max(0, Math.Min(1, rangeEnd));
|
||||
}
|
||||
|
||||
public double Transform(Color color)
|
||||
{
|
||||
// we invert the gray value so we have black being the color we are finding
|
||||
return 1 - ((color.Red0To1 * 0.2989) + (color.Blue0To1 * 0.5870) + (color.Green0To1 * 0.1140));
|
||||
}
|
||||
|
||||
public double Threshold(Color color)
|
||||
{
|
||||
// this is on I from HSI
|
||||
return GetThresholded0To1(Transform(color));
|
||||
}
|
||||
|
||||
public Color ZeroColor => Color.White;
|
||||
|
||||
protected double GetThresholded0To1(double rawValue)
|
||||
{
|
||||
double outValue = 0;
|
||||
if (rawValue < rangeStart)
|
||||
{
|
||||
outValue = 0;
|
||||
}
|
||||
else if (rawValue > rangeEnd)
|
||||
{
|
||||
outValue = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
outValue = (double)(rawValue - rangeStart) / (double)(rangeEnd - rangeStart);
|
||||
}
|
||||
|
||||
return outValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue