22 #include <geos/export.h>
23 #include <geos/algorithm/Intersection.h>
24 #include <geos/algorithm/Interpolate.h>
25 #include <geos/algorithm/Orientation.h>
26 #include <geos/geom/Coordinate.h>
27 #include <geos/geom/Envelope.h>
28 #include <geos/geom/PrecisionModel.h>
74 static double computeEdgeDistance(
const geom::CoordinateXY& p,
const geom::CoordinateXY& p0,
const geom::CoordinateXY& p1);
81 precisionModel(initialPrecisionModel),
98 if(isInteriorIntersection(0)) {
101 if(isInteriorIntersection(1)) {
116 for(std::size_t i = 0; i < result; ++i) {
117 if(!(intPt[i].equals2D(*inputLines[inputLineIndex][0])
118 || intPt[i].equals2D(*inputLines[inputLineIndex][1]))) {
134 precisionModel = newPM;
142 POINT_INTERSECTION = 1,
145 COLLINEAR_INTERSECTION = 2
149 template<
typename C1,
typename C2>
151 const C2& p3,
const C2& p4)
153 inputLines[0][0] = &p1;
154 inputLines[0][1] = &p2;
155 inputLines[1][0] = &p3;
156 inputLines[1][1] = &p4;
157 result = computeIntersect(p1, p2, p3, p4);
164 std::string toString()
const;
174 return result != NO_INTERSECTION;
185 const geom::CoordinateXY*
188 return inputLines[segmentIndex][ptIndex];
208 const geom::CoordinateXYZM&
211 return intPt[intIndex];
232 for(std::size_t i = 0; i < result; ++i) {
233 if(intPt[i].equals2D(pt)) {
257 return hasIntersection() && isProperVar;
304 const geom::CoordinateXY* inputLines[2][2];
310 geom::CoordinateXYZM intPt[2];
316 std::size_t intLineIndex[2][2];
325 return result == COLLINEAR_INTERSECTION;
328 template<
typename C1,
typename C2>
329 uint8_t computeIntersect(
const C1& p1,
const C1& p2,
const C2& q1,
const C2& q2)
335 return NO_INTERSECTION;
344 if((Pq1 > 0 && Pq2 > 0) || (Pq1 < 0 && Pq2 < 0)) {
345 return NO_INTERSECTION;
351 if((Qp1 > 0 && Qp2 > 0) || (Qp1 < 0 && Qp2 < 0)) {
352 return NO_INTERSECTION;
358 bool collinear = Pq1 == 0 && Pq2 == 0 && Qp1 == 0 && Qp2 == 0;
360 return computeCollinearIntersection(p1, p2, q1, q2);
378 geom::CoordinateXYZM p;
379 double z = DoubleNotANumber;
380 double m = DoubleNotANumber;
382 if(Pq1 == 0 || Pq2 == 0 || Qp1 == 0 || Qp2 == 0) {
403 if (p1.equals2D(q1)) {
405 z = Interpolate::zGet(p1, q1);
406 m = Interpolate::mGet(p1, q1);
408 else if (p1.equals2D(q2)) {
410 z = Interpolate::zGet(p1, q2);
411 m = Interpolate::mGet(p1, q2);
413 else if (p2.equals2D(q1)) {
415 z = Interpolate::zGet(p2, q1);
416 m = Interpolate::mGet(p2, q1);
418 else if (p2.equals2D(q2)) {
420 z = Interpolate::zGet(p2, q2);
421 m = Interpolate::mGet(p2, q2);
428 z = Interpolate::zGetOrInterpolate(q1, p1, p2);
429 m = Interpolate::mGetOrInterpolate(q1, p1, p2);
433 z = Interpolate::zGetOrInterpolate(q2, p1, p2);
434 m = Interpolate::mGetOrInterpolate(q2, p1, p2);
438 z = Interpolate::zGetOrInterpolate(p1, q1, q2);
439 m = Interpolate::mGetOrInterpolate(p1, q1, q2);
443 z = Interpolate::zGetOrInterpolate(p2, q1, q2);
444 m = Interpolate::mGetOrInterpolate(p2, q1, q2);
448 p = intersection(p1, p2, q1, q2);
449 z = Interpolate::zInterpolate(p, p1, p2, q1, q2);
450 m = Interpolate::mInterpolate(p, p1, p2, q1, q2);
452 intPt[0] = geom::CoordinateXYZM(p.x, p.y, z, m);
454 std::cerr <<
" POINT_INTERSECTION; intPt[0]:" << intPt[0].toString() << std::endl;
456 return POINT_INTERSECTION;
462 return hasIntersection() && !isProperVar;
465 void computeIntLineIndex();
467 void computeIntLineIndex(std::size_t segmentIndex);
469 template<
typename C1,
typename C2>
470 uint8_t computeCollinearIntersection(
const C1& p1,
const C1& p2,
const C2& q1,
const C2& q2)
478 intPt[0] = zmGetOrInterpolateCopy(q1, p1, p2);
479 intPt[1] = zmGetOrInterpolateCopy(q2, p1, p2);
480 return COLLINEAR_INTERSECTION;
483 intPt[0] = zmGetOrInterpolateCopy(p1, q1, q2);
484 intPt[1] = zmGetOrInterpolateCopy(p2, q1, q2);
485 return COLLINEAR_INTERSECTION;
489 intPt[0] = zmGetOrInterpolateCopy(q1, p1, p2);
490 intPt[1] = zmGetOrInterpolateCopy(p1, q1, q2);
492 return (q1 == p1) && !q2inP && !p2inQ ? POINT_INTERSECTION : COLLINEAR_INTERSECTION;
496 intPt[0] = zmGetOrInterpolateCopy(q1, p1, p2);
497 intPt[1] = zmGetOrInterpolateCopy(p2, q1, q2);
499 return (q1 == p2) && !q2inP && !p1inQ ? POINT_INTERSECTION : COLLINEAR_INTERSECTION;
503 intPt[0] = zmGetOrInterpolateCopy(q2, p1, p2);
504 intPt[1] = zmGetOrInterpolateCopy(p1, q1, q2);
506 return (q2 == p1) && !q1inP && !p2inQ ? POINT_INTERSECTION : COLLINEAR_INTERSECTION;
510 intPt[0] = zmGetOrInterpolateCopy(q2, p1, p2);
511 intPt[1] = zmGetOrInterpolateCopy(p2, q1, q2);
512 return (q2 == p2) && !q1inP && !p1inQ ? POINT_INTERSECTION : COLLINEAR_INTERSECTION;
514 return NO_INTERSECTION;
526 template<
typename C1,
typename C2>
527 geom::CoordinateXYZM intersection (
const C1& p1,
const C1& p2,
const C2& q1,
const C2& q2)
const {
528 auto intPtOut = intersectionSafe(p1, p2, q1, q2);
545 if(! isInSegmentEnvelopes(intPtOut)) {
547 intPtOut = nearestEndpoint(p1, p2, q1, q2);
550 if(precisionModel !=
nullptr) {
567 bool isInSegmentEnvelopes(
const geom::CoordinateXY& pt)
const
569 geom::Envelope env0(*inputLines[0][0], *inputLines[0][1]);
570 geom::Envelope env1(*inputLines[1][0], *inputLines[1][1]);
571 return env0.contains(pt) && env1.contains(pt);
586 template<
typename C1,
typename C2>
587 geom::CoordinateXYZM intersectionSafe(
const C1& p1,
const C1& p2,
588 const C2& q1,
const C2& q2)
const
591 if (ptInt.isNull()) {
592 const geom::CoordinateXY& nearest = nearestEndpoint(p1, p2, q1, q2);
593 #if __cplusplus >= 201703L
594 if constexpr (std::is_same<C1, C2>::value) {
596 if (std::is_same<C1, C2>::value) {
598 ptInt =
static_cast<const C1&
>(nearest);
600 if (&nearest ==
static_cast<const geom::CoordinateXY*
>(&p1) || &nearest ==
static_cast<const geom::CoordinateXY*
>(&p2)) {
601 ptInt =
static_cast<const C1&
>(nearest);
603 ptInt =
static_cast<const C2&
>(nearest);
629 static const geom::CoordinateXY& nearestEndpoint(
const geom::CoordinateXY& p1,
630 const geom::CoordinateXY& p2,
631 const geom::CoordinateXY& q1,
632 const geom::CoordinateXY& q2);
635 template<
typename C1,
typename C2>
636 static geom::CoordinateXYZM zmGetOrInterpolateCopy(
641 geom::CoordinateXYZM pCopy(p);
642 pCopy.z = Interpolate::zGetOrInterpolate(p, p1, p2);
643 pCopy.m = Interpolate::mGetOrInterpolate(p, p1, p2);
static geom::CoordinateXY intersection(const geom::CoordinateXY &p1, const geom::CoordinateXY &p2, const geom::CoordinateXY &q1, const geom::CoordinateXY &q2)
Computes the intersection point of two lines. If the lines are parallel or collinear this case is det...
A LineIntersector is an algorithm that can both test whether two line segments intersect and compute ...
Definition: LineIntersector.h:53
intersection_type
Definition: LineIntersector.h:137
void computeIntersection(const geom::CoordinateSequence &p, std::size_t p0, const geom::CoordinateSequence &q, std::size_t q0)
Compute the intersection between two segments, given a sequence and starting index of each.
const geom::Coordinate & getIntersectionAlongSegment(std::size_t segmentIndex, std::size_t intIndex)
Computes the intIndex'th intersection point in the direction of a specified input line segment.
bool isInteriorIntersection(std::size_t inputLineIndex)
Tests whether either intersection point is an interior point of the specified input segment.
Definition: LineIntersector.h:114
void setPrecisionModel(const geom::PrecisionModel *newPM)
Definition: LineIntersector.h:132
bool isInteriorIntersection()
Tests whether either intersection point is an interior point of one of the input segments.
Definition: LineIntersector.h:96
static double computeEdgeDistance(const geom::CoordinateXY &p, const geom::CoordinateXY &p0, const geom::CoordinateXY &p1)
bool hasIntersection() const
Definition: LineIntersector.h:172
static bool isSameSignAndNonZero(double a, double b)
void computeIntersection(const C1 &p1, const C1 &p2, const C2 &p3, const C2 &p4)
Computes the intersection of the lines p1-p2 and p3-p4.
Definition: LineIntersector.h:150
bool isProper() const
Tests whether an intersection is proper.
Definition: LineIntersector.h:255
const geom::CoordinateXYZM & getIntersection(std::size_t intIndex) const
Definition: LineIntersector.h:209
size_t getIntersectionNum() const
Definition: LineIntersector.h:196
const geom::CoordinateXY * getEndpoint(std::size_t segmentIndex, std::size_t ptIndex) const
Definition: LineIntersector.h:186
double getEdgeDistance(std::size_t geomIndex, std::size_t intIndex) const
Computes the "edge distance" of an intersection point along the specified input line segment.
std::size_t getIndexAlongSegment(std::size_t segmentIndex, std::size_t intIndex)
Computes the index of the intIndex'th intersection point in the direction of a specified input line s...
bool isIntersection(const geom::Coordinate &pt) const
Test whether a point is a intersection point of two line segments.
Definition: LineIntersector.h:230
static int index(const geom::CoordinateXY &p1, const geom::CoordinateXY &p2, const geom::CoordinateXY &q)
Returns the orientation index of the direction of the point q relative to a directed infinite line sp...
The internal representation of a list of coordinates inside a Geometry.
Definition: CoordinateSequence.h:56
Coordinate is the lightweight class used to store coordinates.
Definition: Coordinate.h:217
static bool intersects(const CoordinateXY &p1, const CoordinateXY &p2, const CoordinateXY &q)
Test the point q to see whether it intersects the Envelope defined by p1-p2.
Specifies the precision model of the Coordinate in a Geometry.
Definition: PrecisionModel.h:88
double makePrecise(double val) const
Rounds a numeric value to the PrecisionModel grid.
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25