locationtech / jts

The JTS Topology Suite is a Java library for creating and manipulating vector geometry.
Other
1.94k stars 442 forks source link

Negative buffer operation leaves some collinear points (regression from 1.19.0) #1007

Open grimsa opened 10 months ago

grimsa commented 10 months ago

While testing 1.20.0-SNAPSHOT version built from https://github.com/locationtech/jts/pull/1005 I noticed a minor change in behavior since 1.19.0.

A negative buffer operation no longer removes some collinear points (blue is input geometry, red is result after positive and then negative buffering. Problematic points are marked in red): image

Context: We have a geometry of photovoltaic (PV) module array (a MultiPolygon) and we want to "fill the gaps" between individual PV modules to arrive at a single polygon for the whole PV module array. For this we use an inflate-deflate approach, where we buffer the geometry using a positive distance, and then buffer the result using an equal but negative distance. In general this seems to work reliably.

Test case that reproduces it (and from which the example image was generated):

@Test
void simplifyByInflateDeflate() throws ParseException {
    MultiPolygon polygons = (MultiPolygon) new WKTReader().read(
            "MULTIPOLYGON (((0 0, 1 0, 1 1.5, 0 1.5, 0 0)), ((1.01 0, 2.01 0, 2.01 1.5, 1.01 1.5, 1.01 0)), ((2.02 0, 3.0199999999999996 0, 3.0199999999999996 1.5, 2.02 1.5, 2.02 0)), ((0 1.53, 1 1.53, 1 3.0300000000000002, 0 3.0300000000000002, 0 1.53)), ((1.01 1.53, 2.01 1.53, 2.01 3.0300000000000002, 1.01 3.0300000000000002, 1.01 1.53)), ((2.02 1.53, 3.0199999999999996 1.53, 3.0199999999999996 3.0300000000000002, 2.02 3.0300000000000002, 2.02 1.53)), ((0 3.06, 1 3.06, 1 4.5600000000000005, 0 4.5600000000000005, 0 3.06)), ((1.01 3.06, 2.01 3.06, 2.01 4.5600000000000005, 1.01 4.5600000000000005, 1.01 3.06)), ((2.02 3.06, 3.0199999999999996 3.06, 3.0199999999999996 4.5600000000000005, 2.02 4.5600000000000005, 2.02 3.06)))"
    );
    var bufferParameters = new BufferParameters();
    bufferParameters.setJoinStyle(BufferParameters.JOIN_MITRE);
    bufferParameters.setMitreLimit(10);

    var inflatedResult = BufferOp.bufferOp(polygons, 0.05, bufferParameters);
    var deflatedResult = BufferOp.bufferOp(inflatedResult, -0.05, bufferParameters);

    // 1.18.2:
    // IR:   POLYGON ((-0.05 -0.05, -0.05 1.48, -0.05 1.55, -0.05 3.0100000000000002, -0.05 3.08, -0.05 4.61, 0.96 4.61, 1.05 4.61, 1.97 4.61, 2.0599999999999996 4.61, 3.0699999999999994 4.61, 3.0699999999999994 3.08, 3.0699999999999994 3.0100000000000002, 3.0699999999999994 1.55, 3.0699999999999994 1.48, 3.0699999999999994 -0.05, 2.0599999999999996 -0.05, 1.97 -0.05, 1.05 -0.05, 0.96 -0.05, -0.05 -0.05))
    // DR:   POLYGON ((0 0, 0 4.5600000000000005, 3.0199999999999996 4.5600000000000005, 3.0199999999999996 0, 0 0))

    // 1.19.0:
    // IR:   POLYGON ((-0.05 -0.05, -0.05 1.48, -0.05 1.55, -0.05 3.0100000000000002, -0.05 3.08, -0.05 4.61, 0.96 4.61, 1.05 4.61, 1.97 4.61, 2.0599999999999996 4.61, 3.0699999999999994 4.61, 3.0699999999999994 3.08, 3.0699999999999994 3.0100000000000002, 3.0699999999999994 1.55, 3.0699999999999994 1.48, 3.0699999999999994 -0.05, 2.0599999999999996 -0.05, 1.97 -0.05, 1.05 -0.05, 0.96 -0.05, -0.05 -0.05))
    // DR:   POLYGON ((0 0, 0 4.5600000000000005, 3.0199999999999996 4.5600000000000005, 3.0199999999999996 0, 0 0))

    // 1.20.0-SNAPSHOT (built from "Improve OverlayNG difference check heuristic" PR):
    // IR:   POLYGON ((-0.05 -0.05, -0.05 1.55, -0.05 1.55, -0.05 3.0100000000000002, -0.05 3.08, -0.05 4.61, 0.96 4.61, 1.05 4.61, 1.97 4.61, 2.0599999999999996 4.61, 3.0699999999999994 4.61, 3.0699999999999994 3.08, 3.0699999999999994 3.0100000000000002, 3.0699999999999994 1.55, 3.0699999999999994 1.48, 3.0699999999999994 -0.05, 2.0599999999999996 -0.05, 1.97 -0.05, 1.05 -0.05, 0.96 -0.05, -0.05 -0.05))
    // DR:   POLYGON ((0 0, 0 1.5, 0 1.55, 0 4.5600000000000005, 3.0199999999999996 4.5600000000000005, 3.0199999999999996 0, 0 0))
}

It seems that inflatedResult (IR) is the same in all 3 tested versions. deflatedResult (DR) is less optimal in 1.20.0-SNAPSHOT (has some extra collinear points).

This is not a big problem for us, but wanted to report anyway in case this regression is important for some other use cases.