Skip to content

Commit

Permalink
Optimize fast movement in vectorized job
Browse files Browse the repository at this point in the history
  • Loading branch information
HomineLudens committed Mar 19, 2018
1 parent af2715c commit 9f15a2c
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 13 deletions.
79 changes: 74 additions & 5 deletions LaserGRBL/GrblFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Linq;

namespace LaserGRBL
{
Expand Down Expand Up @@ -181,7 +182,7 @@ public override bool IsSeparator



public void LoadImagePotrace(Bitmap bmp, string filename, bool UseSpotRemoval, int SpotRemoval, bool UseSmoothing, decimal Smoothing, bool UseOptimize, decimal Optimize, L2LConf c)
public void LoadImagePotrace(Bitmap bmp, string filename, bool UseSpotRemoval, int SpotRemoval, bool UseSmoothing, decimal Smoothing, bool UseOptimize, decimal Optimize, bool useOptimizeFast, L2LConf c)
{
bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);
long start = Tools.HiResTimer.TotalMilliseconds;
Expand Down Expand Up @@ -231,8 +232,12 @@ public void LoadImagePotrace(Bitmap bmp, string filename, bool UseSpotRemoval, i
}
}

//absolute
list.Add(new GrblCommand("G90"));
//Optimize fast movement
if(useOptimizeFast)
plist = OptimizePaths(plist);

//absolute
list.Add(new GrblCommand("G90"));
//move fast to offset
list.Add(new GrblCommand(String.Format("G0 X{0} Y{1}", formatnumber(c.oX), formatnumber(c.oY))));
//laser off and power to maxPower
Expand Down Expand Up @@ -558,8 +563,72 @@ private void ExtractSegment(Bitmap image, int x, int y, bool reverse, ref int le
prevCol = col;
}


private int GetColor(Bitmap I, int X, int Y, int min, int max, bool pwm)
private List<List<CsPotrace.Curve>> OptimizePaths(List<List<CsPotrace.Curve>> list)
{
//Order all paths in list to reduce travel distance
//Calculate and store all distances in a matrix
var distances = new double[list.Count, list.Count];
for (int p1 = 0; p1 < list.Count; p1++)
{
for (int p2 = 0; p2 < list.Count; p2++)
{
var dx = list[p1][0].A.X - list[p2][0].A.X;
var dy = list[p1][0].A.Y - list[p2][0].A.Y;
if (p1 != p2)
distances[p1, p2] = Math.Sqrt((dx * dx) + (dy * dy));
else
distances[p1, p2] = double.MaxValue;
}
}

List<List<CsPotrace.Curve>> best = new List<List<Curve>>();
var bestTotDistance = double.MaxValue;

//Test all possibile starting shape
for (int first = 0; first < list.Count; first++)
{
//Create a list of unvisited places
List<int> unvisited = Enumerable.Range(0, list.Count).ToList();

//Pick nearest points
List<List<CsPotrace.Curve>> nearest = new List<List<Curve>>();

//Save starting point index
var lastIndex = first;
var totDistance = 0.0;
while (unvisited.Count > 0)
{
var bestIndex = 0;
var bestDistance = double.MaxValue;
foreach (var nextIndex in unvisited)
{
var dist = distances[nextIndex, lastIndex];
if (dist < bestDistance)
{
bestIndex = nextIndex;
bestDistance = dist;
}
}

//Save nearest point
lastIndex = bestIndex;
nearest.Add(list[lastIndex]);
unvisited.Remove(lastIndex);
totDistance += bestDistance;
}

//Count traveled distance
if (totDistance < bestTotDistance)
{
bestTotDistance = totDistance;
//Save best list
best = nearest;
}
}
return best;
}

private int GetColor(Bitmap I, int X, int Y, int min, int max, bool pwm)
{
Color C = I.GetPixel(X, Y);
int rv = (255 - C.R) * C.A / 255;
Expand Down
16 changes: 15 additions & 1 deletion LaserGRBL/RasterConverter/ImageProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class ImageProcessor : ICloneable
private bool mUseSmootihing;
private decimal mDownSampling;
private bool mUseDownSampling;
private bool mOptimizeFast;
private Direction mDirection;
private Direction mFillingDirection;
private ImageTransform.DitheringMode mDithering;
Expand Down Expand Up @@ -516,6 +517,19 @@ public decimal DownSampling
}
}

public bool OptimizeFast
{
get { return mOptimizeFast; }
set
{
if (value != mOptimizeFast)
{
mOptimizeFast = value;
Refresh();
}
}
}

public bool UseSmoothing
{
get { return mUseSmootihing; }
Expand Down Expand Up @@ -726,7 +740,7 @@ void DoTrueWork()
if (SelectedTool == ImageProcessor.Tool.Line2Line || SelectedTool == ImageProcessor.Tool.Dithering)
mCore.LoadedFile.LoadImageL2L(bmp, mFileName, conf);
else if (SelectedTool == ImageProcessor.Tool.Vectorize)
mCore.LoadedFile.LoadImagePotrace(bmp, mFileName, UseSpotRemoval, (int)SpotRemoval, UseSmoothing, Smoothing, UseOptimize, Optimize, conf);
mCore.LoadedFile.LoadImagePotrace(bmp, mFileName, UseSpotRemoval, (int)SpotRemoval, UseSmoothing, Smoothing, UseOptimize, Optimize, OptimizeFast, conf);
}

if (GenerationComplete != null)
Expand Down
30 changes: 24 additions & 6 deletions LaserGRBL/RasterConverter/RasterToLaserForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions LaserGRBL/RasterConverter/RasterToLaserForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ private void StoreSettings()
// Settings.SetObject("GrayScaleConversion.VectorizeOptions.ShowImage.Enabled", CbShowImage.Checked);
Settings.SetObject("GrayScaleConversion.VectorizeOptions.FillingDirection", (ImageProcessor.Direction)CbFillingDirection.SelectedItem);
Settings.SetObject("GrayScaleConversion.VectorizeOptions.FillingQuality", UDFillingQuality.Value);
Settings.SetObject("GrayScaleConversion.VectorizeOptions.OptimizeFast.Enabled", CbOptimizeFast.Checked);

Settings.SetObject("GrayScaleConversion.DitheringOptions.DitheringMode", (ImageTransform.DitheringMode)CbDither.SelectedItem);

Expand Down Expand Up @@ -244,6 +245,7 @@ private void LoadSettings()
UDOptimize.Value = IP.Optimize = (decimal)Settings.GetObject("GrayScaleConversion.VectorizeOptions.Optimize.Value", 0.2m);
CbDownSample.Checked = IP.UseDownSampling = (bool)Settings.GetObject("GrayScaleConversion.VectorizeOptions.DownSample.Enabled", false);
UDDownSample.Value = IP.DownSampling = (decimal)Settings.GetObject("GrayScaleConversion.VectorizeOptions.DownSample.Value", 2.0m);
CbOptimizeFast.Checked = IP.OptimizeFast = (bool) Settings.GetObject("GrayScaleConversion.VectorizeOptions.OptimizeFast.Enabled", false);

//CbShowDots.Checked = IP.ShowDots = (bool)Settings.GetObject("GrayScaleConversion.VectorizeOptions.ShowDots.Enabled", false);
//CbShowImage.Checked = IP.ShowImage = (bool)Settings.GetObject("GrayScaleConversion.VectorizeOptions.ShowImage.Enabled", true);
Expand Down Expand Up @@ -593,6 +595,14 @@ private void UDDownSample_ValueChanged(object sender, EventArgs e)
IP.DownSampling = UDDownSample.Value;
}

private void CbOptimizeFast_CheckedChanged(object sender, EventArgs e)
{
if (IP != null)
{
IP.OptimizeFast = CbOptimizeFast.Checked;
}
}

private void PbConverted_Resize(object sender, EventArgs e)
{
if (IP != null)
Expand Down
2 changes: 1 addition & 1 deletion LaserGRBL/RasterConverter/RasterToLaserForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@
<value>System.Windows.Forms.Button, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="tableLayoutPanel5.RowCount" type="System.Int32, mscorlib">
<value>6</value>
<value>7</value>
</data>
<data name="RightGrid.RowCount" type="System.Int32, mscorlib">
<value>2</value>
Expand Down

0 comments on commit 9f15a2c

Please sign in to comment.