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)
334 if(!geom::Envelope::intersects(p1, p2, q1, q2)) {
335 return NO_INTERSECTION;
341 int Pq1 = Orientation::index(p1, p2, q1);
342 int Pq2 = Orientation::index(p1, p2, q2);
344 if((Pq1 > 0 && Pq2 > 0) || (Pq1 < 0 && Pq2 < 0)) {
345 return NO_INTERSECTION;
348 int Qp1 = Orientation::index(q1, q2, p1);
349 int Qp2 = Orientation::index(q1, q2, p2);
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)
472 bool q1inP = geom::Envelope::intersects(p1, p2, q1);
473 bool q2inP = geom::Envelope::intersects(p1, p2, q2);
474 bool p1inQ = geom::Envelope::intersects(q1, q2, p1);
475 bool p2inQ = geom::Envelope::intersects(q1, q2, p2);
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
590 geom::CoordinateXYZM ptInt(Intersection::intersection(p1, p2, q1, q2));
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);