Skip to content

locateFromEdge gets stuck in loop of same origin vertex #1121

Closed

Description

@james-willis

Overview

I am creating ConformingDelaunayTriangulators from coordinates with lat/lon data. In less than 1% of cases triangulation fails, throwing a LocateFailureException during the formInitialDelaunay call. In the example case I provide below, I find that line 304 of QuadEdgeSubdivision.java always evaluates to true for all edges from some origin. The while loop iterates through these same 3 points until it hits the iter > maxIter case and throws.

Visual inspection doesn't reveal to me anything odd about the set of coordinates. Precision reduction to 4 decimal places does not resolve the issue. Increasing the tolerance can resolve. I have other cases that need as much as 102 meters of tolerance to resolve.

Visual Example

Purple are the incremental triangulation at the start of this site insertion. The orange point is the new site. Green are the segments traversed successfully. Red are the 3 segments that, once reached, are looped through until maxIters is reached.

Image

Example Code

I can generate more cases of sites.csv and tolerance values that reproduce the same issue.

import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.triangulate.ConformingDelaunayTriangulator;
import org.locationtech.jts.triangulate.ConstraintVertex;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

class Triangulation {

    public static void main(String[] args) throws IOException {
        String filePath = "/path/to/sites.csv";

        List<Coordinate> coordinates;

        try (java.util.stream.Stream<String> lines = Files.lines(Paths.get(filePath), StandardCharsets.UTF_8)) {
            coordinates = lines.map(line -> {
                String[] parts = line.split(",");
                return new Coordinate(
                        Double.parseDouble(parts[0].trim()),
                        Double.parseDouble(parts[1].trim()),
                        Double.parseDouble(parts[2].trim())
                );
            }).toList();
        }

        double tolerance = 8.993216059187306E-5; // 10 meters in degrees

        ConformingDelaunayTriangulator conformingDelaunayTriangulator = new ConformingDelaunayTriangulator(
                coordinates.stream().map(ConstraintVertex::new).collect(Collectors.toList()),
                tolerance
        );
        conformingDelaunayTriangulator.setConstraints(new ArrayList<>(), new ArrayList<>());
        conformingDelaunayTriangulator.formInitialDelaunay();
    }
}

sites.csv

Activity

  1. dr-jts commented on Feb 22, 2025

    @dr-jts
    Contributor

    Analysis

    The cause of this is 3 input points which are almost collinear:

    MULTIPOINT ((-121.9686093 37.3248237), (-121.968466 37.3247775), (-121.9687248 37.324861))
    

    The middle point is about 3e-8 units off the line joining the other two points. Perhaps these were originally generated by densifying a line?

    For some reason this causes an invalid triangulation to be created when the middle site is inserted (visible in the image below, with Magnify Topology showing that the constructed triangles overlap, which they should not. Once the triangulation has entered an invalid state, searching for triangles containing other sites is likely to fail.

    Image

    Solution?

    Perhaps the situation of an invalid triangulation could be detected after site insertion, and somehow resolved. However, this problem has occurred before (hence the error detection). It's been a long time since I worked on this code, and I can't remember if I this approach before. It may be that correcting the triangulation is either infeasible or too time-consuming.

    Workaround

    A workaround is to perturb the sites slightly to avoid nearly collinear sites. One way to do this is to reduce the precision of the sites. I reduced to 5 decimal places and this allowed the CDT to be constructed successfully. Another option is to perturb each point by a small random offset. Once the triangulation is constructed, the topology could be mapped back to the original points, if desired. This procedure could be automated and invoked on failure.

    Image
  2. dr-jts commented on Feb 22, 2025

    @dr-jts
    Contributor

    Another observation: Using the ConformingDelaunayTriangulationBuilder allows this case to complete. The difference is that the ConformingDelaunayTriangulationBuilder calls DelaunayTriangulationBuilder.extractUniqueCoordinates (here). This sorts the input site coordinates, which changes the order they are inserted into the triangulation. This allows the triangulation to complete.

    So another possible workaround is to randomize the order of the input points if a failure is encountered (e.g. using Collections.shuffle). This worked for me.

  3. dr-jts commented on Feb 24, 2025

    @dr-jts
    Contributor

    @james-willis will you be able to try one of the proposed fixes on your failing datasets? (I suggest the input order randomization).

    I'm curious about how you find so many failing datasets. Are they just different extracts from the underlying map?

  4. james-willis commented on Feb 24, 2025

    @james-willis
    ContributorAuthor

    I'm going to try some of these right now. I appreciate your help.

    I'm curious about how you find so many failing datasets. Are they just different extracts from the underlying map?

    I'm building an isochrone generation system that sits on top of Apache Spark/Sedona and an open source library using a road network graph built from Overture Map Foundation data. So I'm generating 10s/100s of thousands of isochrones to test the performance of the feature I've built.

    If I find one of your fixes is improving the robustness of the isochrone generation from this open source library I'll raise a PR against it.

  5. james-willis commented on Feb 24, 2025

    @james-willis
    ContributorAuthor

    Shuffling seems to have solved all the issues in my test case which is generating 10 minute isochrones for all all 351524 coffee shops in the Overture map places dataset.

  6. dr-jts commented on Feb 25, 2025

    @dr-jts
    Contributor

    Shuffling seems to have solved all the issues in my test case which is generating 10 minute isochrones for all all 351524 coffee shops in the Overture map places dataset.

    Excellent!

    This heuristic could be provided in ConformingDelaunayBuilder and DelaunayTriangulationBuilder (controlled by a flag and possibly a parameter controlling number of retry attempts.

  7. dr-jts commented on Feb 25, 2025

    @dr-jts
    Contributor

    @james-willis some questions to help understanding the context:

    • Are you using ConformingDelaunayBuilder, or the code you provided above? As I noted, CDB sorts the input points, which seems to help with robustness.
    • If you are using ConformingDelaunayBuilder, does this mean there are cases which failed even with the initial sort?
    • Do you have linear constraints in your input data? The given code doesn't contain any. If not using constraints, can you just use DelaunayTriangulationBuilder
  8. james-willis commented on Feb 25, 2025

    @james-willis
    ContributorAuthor

    Are you using ConformingDelaunayBuilder, or the code you provided above?

    I was using the JTSTriangulator.triangulate method from graphhopper which does not use the ConformingDelaunayBuilder. The code above comes from the original JTSTriangulator implemenation linked. When I confirmed shuffling fixes, I forked that class with a version that adds code to shuffle if the first formInitialDelaunay fails.

    If you are using ConformingDelaunayBuilder, does this mean there are cases which failed even with the initial sort?

    Let me test this solution in my distributed environment. If this solves I can contribute it back to graphhopper

    Do you have linear constraints in your input data? The given code doesn't contain any. If not using constraints, can you just use DelaunayTriangulationBuilder

    The JTSTriangulator does not set any so they are not used. I will also make this change to the JTSTriangulator class.

  9. james-willis commented on Feb 25, 2025

    @james-willis
    ContributorAuthor

    Let me test this solution in my distributed environment. If this solves I can contribute it back to graphhopper

    Using the DelaunayTriangulationBuilder fixed the issue with no shuffling. I raised a PR against graphhopper to implement the switch to DelaunayTriangulationBuilder in their JTSTriangulator class.

  10. dr-jts commented on Feb 25, 2025

    @dr-jts
    Contributor

    Using the DelaunayTriangulationBuilder fixed the issue with no shuffling.

    Good to hear. I think that the initial sorting of input points prevents runs of nearly collinear points, which is probably the cause of the robustness failure. No proof of this, however. But there haven't been reports of this issue for a very long time, if ever, which is reassuring.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions