Search in sources :

Example 26 with LineSegment

use of com.revolsys.geometry.model.segment.LineSegment in project com.revolsys.open by revolsys.

the class CoordinatesListUtil method containsWithinTolerance.

/**
 * <p>
 * Check within a given tolerance that the LINESTRING defined by points2 is
 * contained within the points1.
 * </p>
 * <p>
 * The algorithm is as follows:
 * <ol>
 * <li>Find all coordinates from points2 that are within the tolerance from
 * the line segments of points1.</li>
 * <li>Find all coordinates from points1 that are within the tolerance from
 * the line segments of points2.</li>
 * <li>Split all the line sgements of points1 that were matched in step 1.</li>
 * <li>Split all the line sgements of points2 that were matched in step 2.</li>
 * <li>Line is contained if all line segments from point2 have matching lines
 * in points1.</li>
 * </ol>
 *
 * @param points1
 * @param points2
 * @param tolerance
 * @return
 */
public static boolean containsWithinTolerance(final LineString points1, final LineString points2, final double tolerance) {
    final LineStringGraph graph1 = new LineStringGraph(points1);
    final LineStringGraph graph2 = new LineStringGraph(points2);
    graph1.forEachNode((node) -> movePointsWithinTolerance(null, graph2, tolerance, node));
    graph1.forEachNode((node) -> movePointsWithinTolerance(null, graph1, tolerance, node));
    final Map<Edge<LineSegment>, List<Node<LineSegment>>> pointsOnEdge1 = graph1.getPointsOnEdges(graph2, tolerance);
    final Map<Edge<LineSegment>, List<Node<LineSegment>>> pointsOnEdge2 = graph2.getPointsOnEdges(graph1, tolerance);
    graph1.splitEdges(pointsOnEdge1);
    graph2.splitEdges(pointsOnEdge2);
    for (final Edge<LineSegment> edge : graph2.getEdges()) {
        final Node<LineSegment> fromNode = edge.getFromNode();
        final Node<LineSegment> toNode = edge.getToNode();
        if (!graph1.hasEdgeBetween(fromNode, toNode)) {
            return false;
        }
    }
    return true;
}
Also used : LineStringGraph(com.revolsys.geometry.graph.linestring.LineStringGraph) ArrayList(java.util.ArrayList) List(java.util.List) Edge(com.revolsys.geometry.graph.Edge) LineSegment(com.revolsys.geometry.model.segment.LineSegment)

Example 27 with LineSegment

use of com.revolsys.geometry.model.segment.LineSegment in project com.revolsys.open by revolsys.

the class CoordinatesListUtil method intersection.

public static List<LineString> intersection(final GeometryFactory geometryFactory, final LineString points1, final LineString points2, final double maxDistance) {
    final LineStringGraph graph1 = new LineStringGraph(points1);
    graph1.setPrecisionModel(geometryFactory);
    final LineStringGraph graph2 = new LineStringGraph(points2);
    graph2.setPrecisionModel(geometryFactory);
    final Map<Point, Point> movedNodes = new HashMap<>();
    graph1.forEachNode((node) -> movePointsWithinTolerance(movedNodes, graph2, maxDistance, node));
    graph2.forEachNode((node) -> movePointsWithinTolerance(movedNodes, graph1, maxDistance, node));
    final Map<Edge<LineSegment>, List<Node<LineSegment>>> pointsOnEdge1 = graph1.getPointsOnEdges(graph2, maxDistance);
    final Map<Edge<LineSegment>, List<Node<LineSegment>>> pointsOnEdge2 = graph2.getPointsOnEdges(graph1, maxDistance);
    graph1.splitEdges(pointsOnEdge1);
    graph2.splitEdges(pointsOnEdge2);
    Point startPoint = points1.getPoint(0);
    if (movedNodes.containsKey(startPoint)) {
        startPoint = movedNodes.get(startPoint);
    }
    Point endPoint = points1.getPoint(points1.getVertexCount() - 1);
    if (movedNodes.containsKey(endPoint)) {
        endPoint = movedNodes.get(endPoint);
    }
    final List<LineString> intersections = new ArrayList<>();
    final List<Point> currentCoordinates = new ArrayList<>();
    Node<LineSegment> previousNode = graph1.getNode(startPoint);
    do {
        final List<Edge<LineSegment>> outEdges = previousNode.getOutEdges();
        if (outEdges.isEmpty()) {
            previousNode = null;
        } else if (outEdges.size() > 1) {
            throw new IllegalArgumentException("Cannot handle overlaps\n" + points1 + "\n " + points2);
        } else {
            final Edge<LineSegment> edge = outEdges.get(0);
            final LineSegment line = edge.getObject();
            final Node<LineSegment> nextNode = edge.getToNode();
            if (graph2.hasEdgeBetween(previousNode, nextNode)) {
                if (currentCoordinates.size() == 0) {
                    currentCoordinates.add(line.getPoint(0));
                }
                currentCoordinates.add(line.getPoint(1));
            } else {
                if (currentCoordinates.size() > 0) {
                    final LineString points = new LineStringDouble(points1.getAxisCount(), currentCoordinates);
                    intersections.add(points);
                    currentCoordinates.clear();
                }
            }
            previousNode = nextNode;
        }
    } while (previousNode != null && !endPoint.equals(2, startPoint));
    if (currentCoordinates.size() > 0) {
        final LineString points = new LineStringDouble(points1.getAxisCount(), currentCoordinates);
        intersections.add(points);
    }
    return intersections;
}
Also used : HashMap(java.util.HashMap) LineStringGraph(com.revolsys.geometry.graph.linestring.LineStringGraph) Node(com.revolsys.geometry.graph.Node) ArrayList(java.util.ArrayList) Point(com.revolsys.geometry.model.Point) LineString(com.revolsys.geometry.model.LineString) ArrayList(java.util.ArrayList) List(java.util.List) Edge(com.revolsys.geometry.graph.Edge) LineSegment(com.revolsys.geometry.model.segment.LineSegment) LineStringDouble(com.revolsys.geometry.model.impl.LineStringDouble)

Example 28 with LineSegment

use of com.revolsys.geometry.model.segment.LineSegment in project com.revolsys.open by revolsys.

the class TaggedLineString method extractCoordinates.

private static Point[] extractCoordinates(final List<LineSegment> segs) {
    final Point[] pts = new Point[segs.size() + 1];
    LineSegment seg = null;
    for (int i = 0; i < segs.size(); i++) {
        seg = segs.get(i);
        pts[i] = seg.getP0();
    }
    // add last point
    pts[pts.length - 1] = seg.getP1();
    return pts;
}
Also used : Point(com.revolsys.geometry.model.Point) Point(com.revolsys.geometry.model.Point) LineSegment(com.revolsys.geometry.model.segment.LineSegment)

Example 29 with LineSegment

use of com.revolsys.geometry.model.segment.LineSegment in project com.revolsys.open by revolsys.

the class TaggedLineStringSimplifier method flatten.

/**
 * Flattens a section of the taggedLine between
 * indexes <code>start</code> and <code>end</code>,
 * replacing them with a taggedLine between the endpoints.
 * The input and output indexes are updated
 * to reflect this.
 *
 * @param start the start index of the flattened section
 * @param end the end index of the flattened section
 * @return the new segment created
 */
private LineSegment flatten(final int start, final int end) {
    // make a new segment for the simplified geometry
    final Point p0 = this.line.getVertex(start);
    final Point p1 = this.line.getVertex(end);
    final LineSegment newSeg = new LineSegmentDouble(p0, p1);
    // update the indexes
    remove(this.taggedLine, start, end);
    this.outputIndex.add(newSeg);
    return newSeg;
}
Also used : LineSegmentDouble(com.revolsys.geometry.model.segment.LineSegmentDouble) Point(com.revolsys.geometry.model.Point) LineSegment(com.revolsys.geometry.model.segment.LineSegment)

Example 30 with LineSegment

use of com.revolsys.geometry.model.segment.LineSegment in project com.revolsys.open by revolsys.

the class LineSegmentTest method assertIntersection3d.

public void assertIntersection3d(final Point line1Start, final Point line1End, final Point line2Start, final Point line2End, final Geometry expectedIntersection) {
    final LineSegment line1 = new LineSegmentDoubleGF(GEOMETRY_FACTORY_3D, line1Start, line1End);
    final LineSegment line2 = new LineSegmentDoubleGF(GEOMETRY_FACTORY_3D, line2Start, line2End);
    final Geometry intersection = line1.getIntersection(line2);
    if (!TestUtil.equalsExact(3, intersection, expectedIntersection)) {
        TestUtil.failNotEquals("Equals Exact", expectedIntersection, intersection);
    }
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) LineSegmentDoubleGF(com.revolsys.geometry.model.segment.LineSegmentDoubleGF) LineSegment(com.revolsys.geometry.model.segment.LineSegment)

Aggregations

LineSegment (com.revolsys.geometry.model.segment.LineSegment)61 Point (com.revolsys.geometry.model.Point)34 Edge (com.revolsys.geometry.graph.Edge)19 LineSegmentDouble (com.revolsys.geometry.model.segment.LineSegmentDouble)14 LineSegmentDoubleGF (com.revolsys.geometry.model.segment.LineSegmentDoubleGF)13 Geometry (com.revolsys.geometry.model.Geometry)12 PointOnLineSegment (com.revolsys.geometry.model.coordinates.filter.PointOnLineSegment)11 ArrayList (java.util.ArrayList)10 LineString (com.revolsys.geometry.model.LineString)9 Node (com.revolsys.geometry.graph.Node)7 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)6 EdgeAttributeValueComparator (com.revolsys.geometry.graph.comparator.EdgeAttributeValueComparator)5 BoundingBox (com.revolsys.geometry.model.BoundingBox)4 PointDoubleXY (com.revolsys.geometry.model.impl.PointDoubleXY)4 List (java.util.List)4 EdgeObjectFilter (com.revolsys.geometry.graph.filter.EdgeObjectFilter)2 LineStringGraph (com.revolsys.geometry.graph.linestring.LineStringGraph)2 Polygon (com.revolsys.geometry.model.Polygon)2 Vertex (com.revolsys.geometry.model.vertex.Vertex)2 Shape (java.awt.Shape)2