Search in sources :

Example 11 with LineSegmentDoubleGF

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

the class GeometryGraph method getBoundaryIntersection.

/**
 * Get the intersection between the line and the boundary of this geometry.
 *
 * @param line
 * @return
 */
public Geometry getBoundaryIntersection(final LineString line) {
    final List<Point> pointIntersections = new ArrayList<>();
    final List<LineString> lineIntersections = new ArrayList<>();
    final GeometryFactory geometryFactory = getGeometryFactory();
    final BoundingBox boundingBox = getBoundingBox(line);
    if (boundingBox.intersects(this.boundingBox)) {
        final LineString points = line;
        final int vertexCount = points.getVertexCount();
        final Point fromPoint = points.getPoint(0);
        final Point toPoint = points.getPoint(vertexCount - 1);
        Point previousPoint = fromPoint;
        for (int vertexIndex = 1; vertexIndex < vertexCount; vertexIndex++) {
            final Point nextPoint = points.getPoint(vertexIndex);
            final LineSegment line1 = new LineSegmentDoubleGF(getGeometryFactory(), previousPoint, nextPoint);
            final List<Edge<LineSegment>> edges = EdgeLessThanDistance.getEdges(this, line1, this.maxDistance);
            for (final Edge<LineSegment> edge2 : edges) {
                final LineSegment line2 = edge2.getObject();
                final Geometry segmentIntersection = line1.getIntersection(line2);
                if (segmentIntersection instanceof Point) {
                    final Point intersection = (Point) segmentIntersection;
                    if (intersection.equals(fromPoint) || intersection.equals(toPoint)) {
                        // Point intersection, make sure it's not at the start
                        final Node<LineSegment> node = findNode(intersection);
                        if (node == null) {
                            pointIntersections.add(geometryFactory.point(intersection));
                        } else {
                            final int degree = node.getDegree();
                            if (isStartPoint(node)) {
                                if (degree > 2) {
                                    // Intersection not at the start/end of the other line,
                                    // taking
                                    // into account loops
                                    pointIntersections.add(geometryFactory.point(intersection));
                                }
                            } else if (degree > 1) {
                                // Intersection not at the start/end of the other line
                                pointIntersections.add(geometryFactory.point(intersection));
                            }
                        }
                    } else {
                        // Intersection not at the start/end of the line
                        pointIntersections.add(geometryFactory.point(intersection));
                    }
                } else if (segmentIntersection instanceof LineSegment) {
                    lineIntersections.add((LineSegment) segmentIntersection);
                }
                for (final Point point : line1.vertices()) {
                    if (line2.distancePoint(point) < this.maxDistance) {
                        if (point.equals(fromPoint) || point.equals(toPoint)) {
                            // Point intersection, make sure it's not at the start
                            final double maxDistance1 = this.maxDistance;
                            for (final Node<LineSegment> node : this.getNodes(point, maxDistance1)) {
                                final int degree = node.getDegree();
                                if (isStartPoint(node)) {
                                    if (degree > 2) {
                                        // Intersection not at the start/end of the other line,
                                        // taking
                                        // into account loops
                                        pointIntersections.add(geometryFactory.point(point));
                                    }
                                } else if (degree > 1) {
                                    // Intersection not at the start/end of the other line
                                    pointIntersections.add(geometryFactory.point(point));
                                }
                            }
                        } else {
                            // Intersection not at the start/end of the line
                            pointIntersections.add(geometryFactory.point(point));
                        }
                    }
                }
            }
            previousPoint = nextPoint;
        }
    }
    if (lineIntersections.isEmpty()) {
        return geometryFactory.punctual(pointIntersections);
    } else {
        final List<LineString> mergedLines = LineMerger.merge(lineIntersections);
        final Lineal multiLine = geometryFactory.lineal(mergedLines);
        if (pointIntersections.isEmpty()) {
            return multiLine;
        } else {
            final Punctual multiPoint = geometryFactory.punctual(pointIntersections);
            return multiPoint.union(multiLine);
        }
    }
}
Also used : GeometryFactory(com.revolsys.geometry.model.GeometryFactory) ArrayList(java.util.ArrayList) Point(com.revolsys.geometry.model.Point) Point(com.revolsys.geometry.model.Point) LineSegmentDoubleGF(com.revolsys.geometry.model.segment.LineSegmentDoubleGF) Geometry(com.revolsys.geometry.model.Geometry) Punctual(com.revolsys.geometry.model.Punctual) Lineal(com.revolsys.geometry.model.Lineal) LineString(com.revolsys.geometry.model.LineString) BoundingBox(com.revolsys.geometry.model.BoundingBox) Edge(com.revolsys.geometry.graph.Edge) LineSegment(com.revolsys.geometry.model.segment.LineSegment)

Example 12 with LineSegmentDoubleGF

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

the class LineStringGraph method intersects.

public boolean intersects(final LineString line) {
    BoundingBox envelope = line.getBoundingBox();
    final double scaleXY = this.geometryFactory.getScaleXY();
    double maxDistance = 0;
    if (scaleXY > 0) {
        maxDistance = 1 / scaleXY;
    }
    envelope = envelope.expand(maxDistance);
    if (envelope.intersects(this.envelope)) {
        final LineString points = line;
        final int numPoints = points.getVertexCount();
        final Point fromPoint = points.getPoint(0);
        final Point toPoint = points.getPoint(numPoints - 1);
        Point previousPoint = fromPoint;
        for (int i = 1; i < numPoints; i++) {
            final Point nextPoint = points.getPoint(i);
            final LineSegment line1 = new LineSegmentDoubleGF(previousPoint, nextPoint);
            final List<Edge<LineSegment>> edges = EdgeLessThanDistance.getEdges(this, line1, maxDistance);
            for (final Edge<LineSegment> edge2 : edges) {
                final LineSegment line2 = edge2.getObject();
                final Geometry intersections = line1.getIntersection(line2);
                for (final Point intersection : intersections.vertices()) {
                    if (intersection.equals(fromPoint) || intersection.equals(toPoint)) {
                        // Point intersection, make sure it's not at the start
                        final Node<LineSegment> node = findNode(intersection);
                        final int degree = node.getDegree();
                        if (node.equals(2, this.fromPoint)) {
                            if (degree > 2) {
                                // into account loops
                                return true;
                            }
                        } else if (degree > 1) {
                            // Intersection not at the start/end of the other line
                            return true;
                        }
                    } else {
                        // Intersection not at the start/end of the line
                        return true;
                    }
                }
                for (final Point point : line1.vertices()) {
                    if (line2.distancePoint(point) < maxDistance) {
                        if (point.equals(fromPoint) || point.equals(toPoint)) {
                            // Point intersection, make sure it's not at the start
                            final double maxDistance1 = maxDistance;
                            for (final Node<LineSegment> node : this.getNodes(point, maxDistance1)) {
                                final int degree = node.getDegree();
                                if (node.equals(2, this.fromPoint)) {
                                    if (degree > 2) {
                                        // into account loops
                                        return true;
                                    }
                                } else if (degree > 1) {
                                    // Intersection not at the start/end of the other line
                                    return true;
                                }
                            }
                        } else {
                            // Intersection not at the start/end of the line
                            return true;
                        }
                    }
                }
            }
            previousPoint = nextPoint;
        }
    }
    return false;
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) LineString(com.revolsys.geometry.model.LineString) BoundingBox(com.revolsys.geometry.model.BoundingBox) Point(com.revolsys.geometry.model.Point) Edge(com.revolsys.geometry.graph.Edge) Point(com.revolsys.geometry.model.Point) LineSegmentDoubleGF(com.revolsys.geometry.model.segment.LineSegmentDoubleGF) LineSegment(com.revolsys.geometry.model.segment.LineSegment) PointOnLineSegment(com.revolsys.geometry.model.coordinates.filter.PointOnLineSegment)

Example 13 with LineSegmentDoubleGF

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

the class LineSegmentTest method assertLinearIntersection.

public void assertLinearIntersection(final double l1x1, final double l1y1, final double l1x2, final double l1y2, final double l2x1, final double l2y1, final double l2x2, final double l2y2, final double lx1, final double ly1, final double lx2, final double ly2) {
    final LineSegment line1 = new LineSegmentDoubleGF(GEOMETRY_FACTORY_2D, 2, l1x1, l1y1, l1x2, l1y2);
    final LineSegment line2 = new LineSegmentDoubleGF(GEOMETRY_FACTORY_2D, 2, l2x1, l2y1, l2x2, l2y2);
    final LineSegment line = new LineSegmentDoubleGF(GEOMETRY_FACTORY_2D, 2, lx1, ly1, lx2, ly2);
    final Geometry intersection = line1.getIntersection(line2);
    TestUtil.equalsExact(2, intersection, line);
}
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)13 LineSegmentDoubleGF (com.revolsys.geometry.model.segment.LineSegmentDoubleGF)13 Point (com.revolsys.geometry.model.Point)8 Geometry (com.revolsys.geometry.model.Geometry)6 Edge (com.revolsys.geometry.graph.Edge)4 LineString (com.revolsys.geometry.model.LineString)4 BoundingBox (com.revolsys.geometry.model.BoundingBox)3 PointOnLineSegment (com.revolsys.geometry.model.coordinates.filter.PointOnLineSegment)2 Shape (java.awt.Shape)2 AffineTransform (java.awt.geom.AffineTransform)2 ArrayList (java.util.ArrayList)2 Node (com.revolsys.geometry.graph.Node)1 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)1 Lineal (com.revolsys.geometry.model.Lineal)1 Punctual (com.revolsys.geometry.model.Punctual)1 Triangle (com.revolsys.geometry.model.Triangle)1 CoordinatesDistanceComparator (com.revolsys.geometry.model.coordinates.comparator.CoordinatesDistanceComparator)1 PointDoubleXYZ (com.revolsys.geometry.model.impl.PointDoubleXYZ)1 TreeSet (java.util.TreeSet)1