PostGIS 3.0.6dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ lwline_crossing_direction()

int lwline_crossing_direction ( const LWLINE l1,
const LWLINE l2 
)

Given two lines, characterize how (and if) they cross each other.

Given two lines, characterize how (and if) they cross each other.

Parameters
l1first line string
l2second line string
Returns
a CG_LINE_CROSS_TYPE LINE_NO_CROSS = 0 LINE_CROSS_LEFT = -1 LINE_CROSS_RIGHT = 1 LINE_MULTICROSS_END_LEFT = -2 LINE_MULTICROSS_END_RIGHT = 2 LINE_MULTICROSS_END_SAME_FIRST_LEFT = -3 LINE_MULTICROSS_END_SAME_FIRST_RIGHT = 3

Definition at line 462 of file lwalgorithm.c.

463{
464 uint32_t i = 0, j = 0;
465 const POINT2D *p1, *p2, *q1, *q2;
466 POINTARRAY *pa1 = NULL, *pa2 = NULL;
467 int cross_left = 0;
468 int cross_right = 0;
469 int first_cross = 0;
470 int this_cross = 0;
471#if POSTGIS_DEBUG_LEVEL >= 4
472 char *geom_ewkt;
473#endif
474
475 pa1 = (POINTARRAY*)l1->points;
476 pa2 = (POINTARRAY*)l2->points;
477
478 /* One-point lines can't intersect (and shouldn't exist). */
479 if ( pa1->npoints < 2 || pa2->npoints < 2 )
480 return LINE_NO_CROSS;
481
482 /* Zero length lines don't have a side. */
483 if ( ptarray_length_2d(pa1) == 0 || ptarray_length_2d(pa2) == 0 )
484 return LINE_NO_CROSS;
485
486
487#if POSTGIS_DEBUG_LEVEL >= 4
488 geom_ewkt = lwgeom_to_ewkt((LWGEOM*)l1);
489 LWDEBUGF(4, "l1 = %s", geom_ewkt);
490 lwfree(geom_ewkt);
491 geom_ewkt = lwgeom_to_ewkt((LWGEOM*)l2);
492 LWDEBUGF(4, "l2 = %s", geom_ewkt);
493 lwfree(geom_ewkt);
494#endif
495
496 /* Initialize first point of q */
497 q1 = getPoint2d_cp(pa2, 0);
498
499 for ( i = 1; i < pa2->npoints; i++ )
500 {
501
502 /* Update second point of q to next value */
503 q2 = getPoint2d_cp(pa2, i);
504
505 /* Initialize first point of p */
506 p1 = getPoint2d_cp(pa1, 0);
507
508 for ( j = 1; j < pa1->npoints; j++ )
509 {
510
511 /* Update second point of p to next value */
512 p2 = getPoint2d_cp(pa1, j);
513
514 this_cross = lw_segment_intersects(p1, p2, q1, q2);
515
516 LWDEBUGF(4, "i=%d, j=%d (%.8g %.8g, %.8g %.8g)", this_cross, i, j, p1->x, p1->y, p2->x, p2->y);
517
518 if ( this_cross == SEG_CROSS_LEFT )
519 {
520 LWDEBUG(4,"this_cross == SEG_CROSS_LEFT");
521 cross_left++;
522 if ( ! first_cross )
523 first_cross = SEG_CROSS_LEFT;
524 }
525
526 if ( this_cross == SEG_CROSS_RIGHT )
527 {
528 LWDEBUG(4,"this_cross == SEG_CROSS_RIGHT");
529 cross_right++;
530 if ( ! first_cross )
531 first_cross = SEG_CROSS_RIGHT;
532 }
533
534 /*
535 ** Crossing at a co-linearity can be turned handled by extending
536 ** segment to next vertex and seeing if the end points straddle
537 ** the co-linear segment.
538 */
539 if ( this_cross == SEG_COLINEAR )
540 {
541 LWDEBUG(4,"this_cross == SEG_COLINEAR");
542 /* TODO: Add logic here and in segment_intersects()
543 continue;
544 */
545 }
546
547 LWDEBUG(4,"this_cross == SEG_NO_INTERSECTION");
548
549 /* Turn second point of p into first point */
550 p1 = p2;
551
552 }
553
554 /* Turn second point of q into first point */
555 q1 = q2;
556
557 }
558
559 LWDEBUGF(4, "first_cross=%d, cross_left=%d, cross_right=%d", first_cross, cross_left, cross_right);
560
561 if ( !cross_left && !cross_right )
562 return LINE_NO_CROSS;
563
564 if ( !cross_left && cross_right == 1 )
565 return LINE_CROSS_RIGHT;
566
567 if ( !cross_right && cross_left == 1 )
568 return LINE_CROSS_LEFT;
569
570 if ( cross_left - cross_right == 1 )
572
573 if ( cross_left - cross_right == -1 )
575
576 if ( cross_left - cross_right == 0 && first_cross == SEG_CROSS_LEFT )
578
579 if ( cross_left - cross_right == 0 && first_cross == SEG_CROSS_RIGHT )
581
582 return LINE_NO_CROSS;
583
584}
double ptarray_length_2d(const POINTARRAY *pts)
Find the 2d length of the given POINTARRAY (even if it's 3d)
Definition ptarray.c:1708
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
Definition lwgeom.c:547
void lwfree(void *mem)
Definition lwutil.c:242
@ LINE_MULTICROSS_END_RIGHT
Definition liblwgeom.h:1628
@ LINE_MULTICROSS_END_SAME_FIRST_LEFT
Definition liblwgeom.h:1629
@ LINE_MULTICROSS_END_LEFT
Definition liblwgeom.h:1627
@ LINE_CROSS_LEFT
Definition liblwgeom.h:1625
@ LINE_MULTICROSS_END_SAME_FIRST_RIGHT
Definition liblwgeom.h:1630
@ LINE_CROSS_RIGHT
Definition liblwgeom.h:1626
@ LINE_NO_CROSS
Definition liblwgeom.h:1624
@ SEG_COLINEAR
@ SEG_CROSS_RIGHT
@ SEG_CROSS_LEFT
int lw_segment_intersects(const POINT2D *p1, const POINT2D *p2, const POINT2D *q1, const POINT2D *q2)
returns the kind of CG_SEGMENT_INTERSECTION_TYPE behavior of lineseg 1 (constructed from p1 and p2) a...
#define LWDEBUG(level, msg)
Definition lwgeom_log.h:83
#define LWDEBUGF(level, msg,...)
Definition lwgeom_log.h:88
static const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from.
Definition lwinline.h:91
POINTARRAY * points
Definition liblwgeom.h:469

References getPoint2d_cp(), LINE_CROSS_LEFT, LINE_CROSS_RIGHT, LINE_MULTICROSS_END_LEFT, LINE_MULTICROSS_END_RIGHT, LINE_MULTICROSS_END_SAME_FIRST_LEFT, LINE_MULTICROSS_END_SAME_FIRST_RIGHT, LINE_NO_CROSS, lw_segment_intersects(), LWDEBUG, LWDEBUGF, lwfree(), lwgeom_to_ewkt(), POINTARRAY::npoints, LWLINE::points, ptarray_length_2d(), SEG_COLINEAR, SEG_CROSS_LEFT, SEG_CROSS_RIGHT, POINT2D::x, and POINT2D::y.

Referenced by ST_LineCrossingDirection(), test_lwline_crossing_bugs(), test_lwline_crossing_long_lines(), and test_lwline_crossing_short_lines().

Here is the call graph for this function:
Here is the caller graph for this function: