micycle1 / PGS

Processing Geometry Suite
https://micycle1.github.io/PGS/
170 stars 13 forks source link

Investigate JTS OffsetCurve class #52

Closed micycle1 closed 2 years ago

micycle1 commented 2 years ago

Look at JTS' new OffsetCurve class and determine whether it should replace PGS' current offset curves solution (based on repeated buffering). https://github.com/locationtech/jts/pull/810

micycle1 commented 2 years ago

Output is basically identical, but OffsetCurve class is x10 slower!

Current solution

image

OffsetCurve class

image

public static PShape jtsOffsetCurveInwards(PShape shape, OffsetStyle style, double spacing) {
Geometry g = fromPShape(shape);

final BufferParameters bufParams = new BufferParameters(BufferParameters.DEFAULT_QUADRANT_SEGMENTS, BufferParameters.CAP_FLAT,
        style.style, BufferParameters.DEFAULT_MITRE_LIMIT);

//      if (g.getCoordinates().length > 2000) {
//          g = DouglasPeuckerSimplifier.simplify(g, 1);
//      }
int currentCurves = 0;
Geometry curve = g;
LineString l = ((Polygon) g).getExteriorRing();
final PShape parent = new PShape(PConstants.GROUP);
while (!curve.isEmpty()) {
//          GEOM_FACTORY.
//          CoordinateSequence c = GEOM_FACTORY.getCoordinateSequenceFactory().create(OffsetCurve.rawOffset(l, currentCurves++*spacing));
//          curve = GEOM_FACTORY.createPolygon(c.toCoordinateArray());
    curve = OffsetCurve.getCurve(g, currentCurves++ * spacing);

//          Coordinate[] coords = OffsetCurve.rawOffset(l, currentCurves++ * spacing, bufParams);
//          if (coords == null) {
//              break;
//          }
//          CoordinateList cl = new CoordinateList(coords);
//          cl.closeRing();
//          curve = GEOM_FACTORY.createPolygon(cl.toCoordinateArray());
//          CoordinateArraySequence c = new CoordinateArraySequence(, 2);

    PShape curveShape = toPShape(curve);
    curveShape.setFill(false);
    curveShape.setStrokeWeight(2);
    PGS_Conversion.getChildren(curveShape).forEach(parent::addChild);
}
PGS_Conversion.disableAllFill(parent);
PGS_Conversion.setAllStrokeColor(parent, RGB.PINK, 2);
return parent;
}