@Test public void lastSegmentShorterThanControlDistance() throws Exception { final double MIN_LEN = 1.0; final double base = MIN_LEN / 2.0; final double height = 10.0 * MIN_LEN; String wkt = String.format(Locale.US, "POLYGON((0 0, %f %f, %f 0, 0 0))", base / 2.0, height, base); Polygon p = (Polygon) reader.read(wkt); PolygonSmoother smoother = new PolygonSmoother(gf); smoother.setControl(new SmootherControl() { public double getMinLength() { return MIN_LEN; } public int getNumVertices(double length) { return 10; } }); Polygon ps = null; try { ps = smoother.smooth(p, 0); } catch (Exception ex) { fail("Smoother did not return a valid polygon"); } assertTrue(!ps.isEmpty() && ps.isValid()); }
public Polygon smooth(Polygon inputPoly, double alpha) { Coordinate[] coords = inputPoly.getExteriorRing().getCoordinates(); final int N = coords.length - 1; final int LAST = N - 1; Coordinate[][] controlPoints = getControlPoints(coords, N, alpha); List<Coordinate> smoothCoords = CollectionFactory.list(); double dist; for (int i = 0; i < N; i++) { int next = (i + 1) % N; dist = coords[i].distance(coords[next]); if (dist < control.getMinLength()) { smoothCoords.add(new Coordinate(coords[i])); if (i == LAST) { smoothCoords.add(new Coordinate(coords[0])); } } else { int smoothN = control.getNumVertices(dist); Coordinate[] segment = cubicBezier( coords[i], coords[next], controlPoints[i][1], controlPoints[next][0], smoothN); int copyN = i == LAST ? segment.length : segment.length - 1; for (int k = 0; k < copyN; k++) { smoothCoords.add(segment[k]); } } } LinearRing shell = geomFactory.createLinearRing(smoothCoords.toArray(new Coordinate[0])); Polygon smoothedPoly = geomFactory.createPolygon(shell, null); smoothedPoly.setUserData(inputPoly.getUserData()); return smoothedPoly; }
PolygonSmoother extends AbstractSmoother { public Polygon smooth(Polygon inputPoly, double alpha) { Coordinate[] coords = inputPoly.getExteriorRing().getCoordinates(); final int N = coords.length - 1; final int LAST = N - 1; Coordinate[][] controlPoints = getControlPoints(coords, N, alpha); List<Coordinate> smoothCoords = CollectionFactory.list(); double dist; for (int i = 0; i < N; i++) { int next = (i + 1) % N; dist = coords[i].distance(coords[next]); if (dist < control.getMinLength()) { smoothCoords.add(new Coordinate(coords[i])); if (i == LAST) { smoothCoords.add(new Coordinate(coords[0])); } } else { int smoothN = control.getNumVertices(dist); Coordinate[] segment = cubicBezier( coords[i], coords[next], controlPoints[i][1], controlPoints[next][0], smoothN); int copyN = i == LAST ? segment.length : segment.length - 1; for (int k = 0; k < copyN; k++) { smoothCoords.add(segment[k]); } } } LinearRing shell = geomFactory.createLinearRing(smoothCoords.toArray(new Coordinate[0])); Polygon smoothedPoly = geomFactory.createPolygon(shell, null); smoothedPoly.setUserData(inputPoly.getUserData()); return smoothedPoly; } }
PolygonSmoother extends AbstractSmoother { public Polygon smooth(Polygon inputPoly, double alpha) { Coordinate[] coords = inputPoly.getExteriorRing().getCoordinates(); final int N = coords.length - 1; final int LAST = N - 1; Coordinate[][] controlPoints = getControlPoints(coords, N, alpha); List<Coordinate> smoothCoords = CollectionFactory.list(); double dist; for (int i = 0; i < N; i++) { int next = (i + 1) % N; dist = coords[i].distance(coords[next]); if (dist < control.getMinLength()) { smoothCoords.add(new Coordinate(coords[i])); if (i == LAST) { smoothCoords.add(new Coordinate(coords[0])); } } else { int smoothN = control.getNumVertices(dist); Coordinate[] segment = cubicBezier( coords[i], coords[next], controlPoints[i][1], controlPoints[next][0], smoothN); int copyN = i == LAST ? segment.length : segment.length - 1; for (int k = 0; k < copyN; k++) { smoothCoords.add(segment[k]); } } } LinearRing shell = geomFactory.createLinearRing(smoothCoords.toArray(new Coordinate[0])); Polygon smoothedPoly = geomFactory.createPolygon(shell, null); smoothedPoly.setUserData(inputPoly.getUserData()); return smoothedPoly; } PolygonSmoother();  PolygonSmoother(GeometryFactory geomFactory); }
PolygonSmoother extends AbstractSmoother { public Polygon smooth(Polygon inputPoly, double alpha) { Coordinate[] coords = inputPoly.getExteriorRing().getCoordinates(); final int N = coords.length - 1; final int LAST = N - 1; Coordinate[][] controlPoints = getControlPoints(coords, N, alpha); List<Coordinate> smoothCoords = CollectionFactory.list(); double dist; for (int i = 0; i < N; i++) { int next = (i + 1) % N; dist = coords[i].distance(coords[next]); if (dist < control.getMinLength()) { smoothCoords.add(new Coordinate(coords[i])); if (i == LAST) { smoothCoords.add(new Coordinate(coords[0])); } } else { int smoothN = control.getNumVertices(dist); Coordinate[] segment = cubicBezier( coords[i], coords[next], controlPoints[i][1], controlPoints[next][0], smoothN); int copyN = i == LAST ? segment.length : segment.length - 1; for (int k = 0; k < copyN; k++) { smoothCoords.add(segment[k]); } } } LinearRing shell = geomFactory.createLinearRing(smoothCoords.toArray(new Coordinate[0])); Polygon smoothedPoly = geomFactory.createPolygon(shell, null); smoothedPoly.setUserData(inputPoly.getUserData()); return smoothedPoly; } PolygonSmoother();  PolygonSmoother(GeometryFactory geomFactory); Polygon smooth(Polygon inputPoly, double alpha); }
PolygonSmoother extends AbstractSmoother { public Polygon smooth(Polygon inputPoly, double alpha) { Coordinate[] coords = inputPoly.getExteriorRing().getCoordinates(); final int N = coords.length - 1; final int LAST = N - 1; Coordinate[][] controlPoints = getControlPoints(coords, N, alpha); List<Coordinate> smoothCoords = CollectionFactory.list(); double dist; for (int i = 0; i < N; i++) { int next = (i + 1) % N; dist = coords[i].distance(coords[next]); if (dist < control.getMinLength()) { smoothCoords.add(new Coordinate(coords[i])); if (i == LAST) { smoothCoords.add(new Coordinate(coords[0])); } } else { int smoothN = control.getNumVertices(dist); Coordinate[] segment = cubicBezier( coords[i], coords[next], controlPoints[i][1], controlPoints[next][0], smoothN); int copyN = i == LAST ? segment.length : segment.length - 1; for (int k = 0; k < copyN; k++) { smoothCoords.add(segment[k]); } } } LinearRing shell = geomFactory.createLinearRing(smoothCoords.toArray(new Coordinate[0])); Polygon smoothedPoly = geomFactory.createPolygon(shell, null); smoothedPoly.setUserData(inputPoly.getUserData()); return smoothedPoly; } PolygonSmoother();  PolygonSmoother(GeometryFactory geomFactory); Polygon smooth(Polygon inputPoly, double alpha);  }