Skip to content

Commit

Permalink
change names
Browse files Browse the repository at this point in the history
  • Loading branch information
msbarry committed Dec 7, 2024
1 parent 91ea88d commit 4fecabe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* gets to small.
* <p>
* In order to avoid introducing too much error from the original shape when rounding corners between very long edges,
* you can set {@link #setMaxArea(double)} or {@link #setMaxOffset(double)} to move the new points closer to a vertex to
* limit the amount of error that is introduced.
* you can set {@link #setMaxVertexArea(double)} or {@link #setMaxVertexTolerance(double)} to move the new points closer
* to a vertex to limit the amount of error that is introduced.
* <p>
* When the points are {@code [0.25, 0.75]} this is equivalent to
* <a href="https://observablehq.com/@pamacha/chaikins-algorithm">Chaikin Smoothing</a>.
Expand All @@ -25,10 +25,10 @@ public class DualMidpointSmoother extends GeometryTransformer implements Geometr
private final double a;
private final double b;
private int iters = 1;
private double minSquaredVertexTolerance = 0;
private double minVertexArea = 0;
private double maxArea = 0;
private double maxSquaredOffset = 0;
private double minSquaredVertexTolerance = 0;
private double maxVertexArea = 0;
private double maxSquaredVertexTolerance = 0;

public DualMidpointSmoother(double a, double b) {
this.a = a;
Expand Down Expand Up @@ -69,7 +69,7 @@ public static DualMidpointSmoother chaikinToMinArea(double minArea) {
* of iterations is reached.
*/
public DualMidpointSmoother setMinVertexTolerance(double minVertexTolerance) {
this.minSquaredVertexTolerance = minVertexTolerance * minVertexTolerance;
this.minSquaredVertexTolerance = minVertexTolerance * Math.abs(minVertexTolerance);
return this;
}

Expand All @@ -90,8 +90,8 @@ public DualMidpointSmoother setMinVertexArea(double minVertexArea) {
* <p>
* This prevents smoothing 2 long adjacent edges from introducing a large deviation from the original shape.
*/
public DualMidpointSmoother setMaxArea(double maxArea) {
this.maxArea = maxArea;
public DualMidpointSmoother setMaxVertexArea(double maxArea) {
this.maxVertexArea = maxArea;
return this;
}

Expand All @@ -102,8 +102,8 @@ public DualMidpointSmoother setMaxArea(double maxArea) {
* <p>
* This prevents smoothing 2 long adjacent edges from introducing a large deviation from the original shape.
*/
public DualMidpointSmoother setMaxOffset(double maxOffset) {
this.maxSquaredOffset = maxOffset * maxOffset;
public DualMidpointSmoother setMaxVertexTolerance(double maxVertexTolerance) {
this.maxSquaredVertexTolerance = maxVertexTolerance * Math.abs(maxVertexTolerance);
return this;
}

Expand Down Expand Up @@ -180,22 +180,22 @@ private void squashVertex(MutableCoordinateSequence result, double x1, double y1

// check the amount of error introduced by removing this vertex (either by offset or area)
// and if it is too large, then move nextA/nextB ratios closer to the vertex to limit the error
if (maxArea > 0 || maxSquaredOffset > 0) {
if (maxVertexArea > 0 || maxSquaredVertexTolerance > 0) {
double magA = Math.hypot(x2 - x1, y2 - y1);
double magB = Math.hypot(x3 - x2, y3 - y2);
double den = magA * magB;
double aDist = magA * (1 - b);
double bDist = magB * a;
double maxDistSquared = Double.POSITIVE_INFINITY;
if (maxArea > 0) {
if (maxVertexArea > 0) {
double sin = den <= 0 ? 0 : Math.abs(((x1 - x2) * (y3 - y2)) - ((y1 - y2) * (x3 - x2))) / den;
if (sin != 0) {
maxDistSquared = 2 * maxArea / sin;
maxDistSquared = 2 * maxVertexArea / sin;
}
}
if (maxSquaredOffset > 0) {
if (maxSquaredVertexTolerance > 0) {
double cos = den <= 0 ? 0 : Math.clamp(((x1 - x2) * (x3 - x2) + (y1 - y2) * (y3 - y2)) / den, -1, 1);
maxDistSquared = Math.min(maxDistSquared, 2 * maxSquaredOffset / (1 + cos));
maxDistSquared = Math.min(maxDistSquared, 2 * maxSquaredVertexTolerance / (1 + cos));
}
double maxDist = Double.NaN;
if (aDist * aDist > maxDistSquared) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void testSmoothWithMaxArea(String inWKT, String outWKT) throws ParseException {
Geometry out = reader.read(outWKT);
assertEquals(
TestUtils.round(out),
TestUtils.round(DualMidpointSmoother.chaikin(1).setMaxArea(0.5).apply(in))
TestUtils.round(DualMidpointSmoother.chaikin(1).setMaxVertexArea(0.5).apply(in))
);
}

Expand All @@ -113,7 +113,7 @@ void testSmoothWithMaxOffset(String inWKT, String outWKT) throws ParseException
Geometry out = reader.read(outWKT);
assertEquals(
TestUtils.round(out),
TestUtils.round(DualMidpointSmoother.chaikin(1).setMaxOffset(Math.sqrt(0.5)).apply(in))
TestUtils.round(DualMidpointSmoother.chaikin(1).setMaxVertexTolerance(Math.sqrt(0.5)).apply(in))
);
}
}

0 comments on commit 4fecabe

Please sign in to comment.