2018-03-29 12:31:36 -07:00
|
|
|
|
/*
|
2018-10-27 15:33:02 -07:00
|
|
|
|
Copyright (c) 2018, Lars Brubaker
|
2018-03-29 12:31:36 -07:00
|
|
|
|
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.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2018-10-27 15:33:02 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Text;
|
2018-03-29 12:31:36 -07:00
|
|
|
|
using MatterControl.Printing;
|
|
|
|
|
|
using MatterHackers.MatterControl.SlicerConfiguration;
|
|
|
|
|
|
using MatterHackers.VectorMath;
|
2018-03-30 14:48:55 -07:00
|
|
|
|
using MIConvexHull;
|
2018-03-29 12:31:36 -07:00
|
|
|
|
|
|
|
|
|
|
namespace MatterHackers.MatterControl.ConfigurationPage.PrintLeveling
|
|
|
|
|
|
{
|
2018-04-28 13:33:17 -07:00
|
|
|
|
public class LevelingFunctions
|
2018-03-29 12:31:36 -07:00
|
|
|
|
{
|
2018-04-28 13:33:17 -07:00
|
|
|
|
private Vector2 bedSize;
|
2018-10-27 15:33:02 -07:00
|
|
|
|
private Dictionary<(int, int), int> positionToRegion = new Dictionary<(int, int), int>();
|
2019-02-27 15:32:43 -08:00
|
|
|
|
private PrinterConfig printer;
|
2018-03-29 12:31:36 -07:00
|
|
|
|
|
2019-02-27 15:32:43 -08:00
|
|
|
|
public LevelingFunctions(PrinterConfig printer, PrintLevelingData levelingData)
|
2018-03-29 12:31:36 -07:00
|
|
|
|
{
|
2019-02-27 15:32:43 -08:00
|
|
|
|
this.printer = printer;
|
2018-03-29 12:31:36 -07:00
|
|
|
|
this.SampledPositions = new List<Vector3>(levelingData.SampledPositions);
|
|
|
|
|
|
|
2019-02-27 15:32:43 -08:00
|
|
|
|
bedSize = printer.Settings.GetValue<Vector2>(SettingsKey.bed_size);
|
2018-04-28 13:33:17 -07:00
|
|
|
|
|
2018-03-30 14:48:55 -07:00
|
|
|
|
// get the delaunay triangulation
|
2018-04-02 18:20:39 -07:00
|
|
|
|
var zDictionary = new Dictionary<(double, double), double>();
|
2018-03-30 14:48:55 -07:00
|
|
|
|
var vertices = new List<DefaultVertex>();
|
2018-10-23 09:47:23 -07:00
|
|
|
|
|
|
|
|
|
|
if (SampledPositions.Count > 2)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var sample in SampledPositions)
|
|
|
|
|
|
{
|
|
|
|
|
|
vertices.Add(new DefaultVertex()
|
|
|
|
|
|
{
|
|
|
|
|
|
Position = new double[] { sample.X, sample.Y }
|
|
|
|
|
|
});
|
|
|
|
|
|
var key = (sample.X, sample.Y);
|
|
|
|
|
|
if (!zDictionary.ContainsKey(key))
|
|
|
|
|
|
{
|
|
|
|
|
|
zDictionary.Add(key, sample.Z);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
2018-03-29 12:31:36 -07:00
|
|
|
|
{
|
2018-03-30 14:48:55 -07:00
|
|
|
|
vertices.Add(new DefaultVertex()
|
2018-03-29 12:31:36 -07:00
|
|
|
|
{
|
2018-10-23 09:47:23 -07:00
|
|
|
|
Position = new double[] { 0, 0 }
|
2018-03-30 14:48:55 -07:00
|
|
|
|
});
|
2018-10-23 09:47:23 -07:00
|
|
|
|
zDictionary.Add((0, 0), 0);
|
|
|
|
|
|
|
|
|
|
|
|
vertices.Add(new DefaultVertex()
|
2018-04-02 18:20:39 -07:00
|
|
|
|
{
|
2018-10-23 09:47:23 -07:00
|
|
|
|
Position = new double[] { 200, 0 }
|
|
|
|
|
|
});
|
|
|
|
|
|
zDictionary.Add((200, 0), 0);
|
|
|
|
|
|
|
|
|
|
|
|
vertices.Add(new DefaultVertex()
|
|
|
|
|
|
{
|
|
|
|
|
|
Position = new double[] { 100, 200 }
|
|
|
|
|
|
});
|
|
|
|
|
|
zDictionary.Add((100, 200), 0);
|
|
|
|
|
|
}
|
2018-04-02 18:20:39 -07:00
|
|
|
|
|
2018-04-03 09:50:26 -07:00
|
|
|
|
int extraXPosition = -50000;
|
2018-04-02 18:20:39 -07:00
|
|
|
|
vertices.Add(new DefaultVertex()
|
|
|
|
|
|
{
|
2018-10-23 09:47:23 -07:00
|
|
|
|
Position = new double[] { extraXPosition, vertices[0].Position[1] }
|
2018-04-02 18:20:39 -07:00
|
|
|
|
});
|
|
|
|
|
|
|
2018-03-30 14:48:55 -07:00
|
|
|
|
var triangles = DelaunayTriangulation<DefaultVertex, DefaultTriangulationCell<DefaultVertex>>.Create(vertices, .001);
|
|
|
|
|
|
|
2019-05-22 13:31:33 -07:00
|
|
|
|
var probeZOffset = default(Vector3);
|
|
|
|
|
|
|
2021-11-30 14:25:15 -08:00
|
|
|
|
if (printer.Settings.Helpers.ProbeBeingUsed)
|
2019-05-22 13:31:33 -07:00
|
|
|
|
{
|
|
|
|
|
|
probeZOffset = new Vector3(0, 0, printer.Settings.GetValue<Vector3>(SettingsKey.probe_offset).Z);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-30 14:48:55 -07:00
|
|
|
|
// make all the triangle planes for these triangles
|
|
|
|
|
|
foreach (var triangle in triangles.Cells)
|
|
|
|
|
|
{
|
|
|
|
|
|
var p0 = triangle.Vertices[0].Position;
|
|
|
|
|
|
var p1 = triangle.Vertices[1].Position;
|
|
|
|
|
|
var p2 = triangle.Vertices[2].Position;
|
2018-04-03 09:50:26 -07:00
|
|
|
|
if (p0[0] != extraXPosition && p1[0] != extraXPosition && p2[0] != extraXPosition)
|
|
|
|
|
|
{
|
|
|
|
|
|
var v0 = new Vector3(p0[0], p0[1], zDictionary[(p0[0], p0[1])]);
|
|
|
|
|
|
var v1 = new Vector3(p1[0], p1[1], zDictionary[(p1[0], p1[1])]);
|
|
|
|
|
|
var v2 = new Vector3(p2[0], p2[1], zDictionary[(p2[0], p2[1])]);
|
|
|
|
|
|
// add all the regions
|
2019-04-02 15:17:58 -07:00
|
|
|
|
Regions.Add(new LevelingTriangle(v0 + probeZOffset, v1 + probeZOffset, v2 + probeZOffset));
|
2018-04-03 09:50:26 -07:00
|
|
|
|
}
|
2018-03-29 12:31:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-27 15:26:32 -07:00
|
|
|
|
public List<Vector3> SampledPositions { get; }
|
2018-03-29 12:31:36 -07:00
|
|
|
|
|
2019-02-22 18:38:06 -08:00
|
|
|
|
public List<LevelingTriangle> Regions { get; } = new List<LevelingTriangle>();
|
2018-03-29 12:31:36 -07:00
|
|
|
|
|
2019-02-20 17:06:42 -08:00
|
|
|
|
public string ApplyLeveling(string lineBeingSent, Vector3 destination)
|
2018-03-29 12:31:36 -07:00
|
|
|
|
{
|
2019-02-22 11:32:07 -08:00
|
|
|
|
bool hasMovement = lineBeingSent.Contains("X") || lineBeingSent.Contains("Y") || lineBeingSent.Contains("Z");
|
|
|
|
|
|
if (!hasMovement)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Leave non-leveling lines untouched
|
|
|
|
|
|
return lineBeingSent;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-29 12:31:36 -07:00
|
|
|
|
double extruderDelta = 0;
|
|
|
|
|
|
GCodeFile.GetFirstNumberAfter("E", lineBeingSent, ref extruderDelta);
|
|
|
|
|
|
double feedRate = 0;
|
|
|
|
|
|
GCodeFile.GetFirstNumberAfter("F", lineBeingSent, ref feedRate);
|
|
|
|
|
|
|
2019-02-19 16:01:08 -08:00
|
|
|
|
var newLine = new StringBuilder("G1");
|
2018-03-29 12:31:36 -07:00
|
|
|
|
|
2019-02-20 17:06:42 -08:00
|
|
|
|
// Position data is not optional for leveling - fall back to fixed defaults when not yet known
|
|
|
|
|
|
var correctedPosition = new Vector3(
|
2019-02-22 11:32:07 -08:00
|
|
|
|
(destination.X == double.PositiveInfinity) ? 0 : destination.X,
|
|
|
|
|
|
(destination.Y == double.PositiveInfinity) ? 0 : destination.Y,
|
|
|
|
|
|
(destination.Z == double.PositiveInfinity) ? 0 : destination.Z);
|
2019-02-20 17:06:42 -08:00
|
|
|
|
|
|
|
|
|
|
// level it
|
|
|
|
|
|
Vector3 outPosition = GetPositionWithZOffset(correctedPosition);
|
2018-03-29 12:31:36 -07:00
|
|
|
|
|
2019-02-20 17:06:42 -08:00
|
|
|
|
// Only output known positions
|
|
|
|
|
|
if (destination.X != double.PositiveInfinity)
|
|
|
|
|
|
{
|
|
|
|
|
|
newLine.Append($" X{outPosition.X:0.##}");
|
|
|
|
|
|
}
|
2018-03-29 12:31:36 -07:00
|
|
|
|
|
2019-02-20 17:06:42 -08:00
|
|
|
|
if (destination.Y != double.PositiveInfinity)
|
|
|
|
|
|
{
|
|
|
|
|
|
newLine.Append($" Y{outPosition.Y:0.##}");
|
2018-03-29 12:31:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-02-20 17:06:42 -08:00
|
|
|
|
newLine.Append($" Z{outPosition.Z:0.##}");
|
|
|
|
|
|
|
2019-01-14 15:35:21 -08:00
|
|
|
|
if (lineBeingSent.Contains("E"))
|
2018-03-29 12:31:36 -07:00
|
|
|
|
{
|
2019-02-19 16:01:08 -08:00
|
|
|
|
newLine.Append($" E{extruderDelta:0.###}");
|
2018-03-29 12:31:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-14 15:35:21 -08:00
|
|
|
|
if (lineBeingSent.Contains("F"))
|
2018-03-29 12:31:36 -07:00
|
|
|
|
{
|
2019-02-19 16:01:08 -08:00
|
|
|
|
newLine.Append($" F{feedRate:0.##}");
|
2018-03-29 12:31:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-02-19 16:01:08 -08:00
|
|
|
|
return newLine.ToString();
|
2018-03-29 12:31:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Vector3 GetPositionWithZOffset(Vector3 currentDestination)
|
|
|
|
|
|
{
|
2018-03-30 14:48:55 -07:00
|
|
|
|
LevelingTriangle region = GetCorrectRegion(currentDestination);
|
2018-03-29 12:31:36 -07:00
|
|
|
|
|
|
|
|
|
|
return region.GetPositionWithZOffset(currentDestination);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-04-28 13:33:17 -07:00
|
|
|
|
private LevelingTriangle GetCorrectRegion(Vector3 currentDestination)
|
2018-03-29 12:31:36 -07:00
|
|
|
|
{
|
2018-04-28 13:33:17 -07:00
|
|
|
|
int xIndex = (int)Math.Round(currentDestination.X * 100 / bedSize.X);
|
|
|
|
|
|
int yIndex = (int)Math.Round(currentDestination.Y * 100 / bedSize.Y);
|
2018-03-29 12:31:36 -07:00
|
|
|
|
|
2018-04-28 13:33:17 -07:00
|
|
|
|
int bestIndex;
|
2018-10-27 15:33:02 -07:00
|
|
|
|
if (!positionToRegion.TryGetValue((xIndex, yIndex), out bestIndex))
|
2018-03-29 12:31:36 -07:00
|
|
|
|
{
|
2018-04-28 13:33:17 -07:00
|
|
|
|
// else calculate the region and store it
|
|
|
|
|
|
double bestDist = double.PositiveInfinity;
|
2018-03-29 12:31:36 -07:00
|
|
|
|
|
2018-04-28 13:33:17 -07:00
|
|
|
|
currentDestination.Z = 0;
|
|
|
|
|
|
for (int regionIndex = 0; regionIndex < Regions.Count; regionIndex++)
|
|
|
|
|
|
{
|
|
|
|
|
|
var dist = (Regions[regionIndex].Center - currentDestination).LengthSquared;
|
2019-03-03 18:06:09 -08:00
|
|
|
|
if (Regions[regionIndex].PointInPolyXY(currentDestination.X, currentDestination.Y))
|
|
|
|
|
|
{
|
|
|
|
|
|
// we found the one it is in
|
|
|
|
|
|
return Regions[regionIndex];
|
|
|
|
|
|
}
|
2018-04-28 13:33:17 -07:00
|
|
|
|
if (dist < bestDist)
|
2018-03-29 12:31:36 -07:00
|
|
|
|
{
|
2018-04-28 13:33:17 -07:00
|
|
|
|
bestIndex = regionIndex;
|
|
|
|
|
|
bestDist = dist;
|
2018-03-29 12:31:36 -07:00
|
|
|
|
}
|
2018-04-28 13:33:17 -07:00
|
|
|
|
}
|
2018-03-29 12:31:36 -07:00
|
|
|
|
|
2018-10-27 15:33:02 -07:00
|
|
|
|
positionToRegion.Add((xIndex, yIndex), bestIndex);
|
2018-03-29 12:31:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Regions[bestIndex];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-30 14:48:55 -07:00
|
|
|
|
public class LevelingTriangle
|
2018-03-29 12:31:36 -07:00
|
|
|
|
{
|
2018-03-30 14:48:55 -07:00
|
|
|
|
public LevelingTriangle(Vector3 v0, Vector3 v1, Vector3 v2)
|
2018-03-29 12:31:36 -07:00
|
|
|
|
{
|
2018-10-27 15:29:43 -07:00
|
|
|
|
this.V0 = v0;
|
|
|
|
|
|
this.V1 = v1;
|
|
|
|
|
|
this.V2 = v2;
|
|
|
|
|
|
this.Center = (V0 + V1 + V2) / 3;
|
|
|
|
|
|
this.Plane = new Plane(V0, V1, V2);
|
2018-03-29 12:31:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-27 15:26:32 -07:00
|
|
|
|
public Vector3 Center { get; }
|
2018-10-27 15:27:59 -07:00
|
|
|
|
public Plane Plane { get; }
|
2018-10-27 15:26:32 -07:00
|
|
|
|
public Vector3 V0 { get; }
|
|
|
|
|
|
public Vector3 V1 { get; }
|
|
|
|
|
|
public Vector3 V2 { get; }
|
2018-04-28 13:33:17 -07:00
|
|
|
|
|
2018-03-30 14:48:55 -07:00
|
|
|
|
public Vector3 GetPositionWithZOffset(Vector3 currentDestination)
|
2018-03-29 12:31:36 -07:00
|
|
|
|
{
|
2018-03-30 14:48:55 -07:00
|
|
|
|
var destinationAtZ0 = new Vector3(currentDestination.X, currentDestination.Y, 0);
|
2018-03-29 12:31:36 -07:00
|
|
|
|
|
2018-10-27 15:27:59 -07:00
|
|
|
|
double hitDistance = this.Plane.GetDistanceToIntersection(destinationAtZ0, Vector3.UnitZ);
|
2018-03-30 14:48:55 -07:00
|
|
|
|
currentDestination.Z += hitDistance;
|
2018-03-29 12:31:36 -07:00
|
|
|
|
|
2018-03-30 14:48:55 -07:00
|
|
|
|
return currentDestination;
|
2018-03-29 12:31:36 -07:00
|
|
|
|
}
|
2019-03-03 18:06:09 -08:00
|
|
|
|
|
|
|
|
|
|
private int FindSideOfLine(Vector2 sidePoint0, Vector2 sidePoint1, Vector2 testPosition)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Vector2.Cross(testPosition - sidePoint0, sidePoint1 - sidePoint0) < 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool PointInPolyXY(double x, double y)
|
|
|
|
|
|
{
|
|
|
|
|
|
// check the bounding rect
|
|
|
|
|
|
Vector2 vertex0 = new Vector2(V0[0], V0[1]);
|
|
|
|
|
|
Vector2 vertex1 = new Vector2(V1[0], V1[1]);
|
|
|
|
|
|
Vector2 vertex2 = new Vector2(V2[0], V2[1]);
|
|
|
|
|
|
Vector2 hitPosition = new Vector2(x, y);
|
|
|
|
|
|
int sumOfLineSides = FindSideOfLine(vertex0, vertex1, hitPosition);
|
|
|
|
|
|
sumOfLineSides += FindSideOfLine(vertex1, vertex2, hitPosition);
|
|
|
|
|
|
sumOfLineSides += FindSideOfLine(vertex2, vertex0, hitPosition);
|
|
|
|
|
|
if (sumOfLineSides == -3 || sumOfLineSides == 3)
|
|
|
|
|
|
{
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-03-29 12:31:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|