PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ lwline_crossing_direction()

int lwline_crossing_direction ( const LWLINE l1,
const LWLINE l2 
)

lwline_crossing_direction: returns the kind of CG_LINE_CROSS_TYPE behavior of 2 linestrings

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 467 of file lwalgorithm.c.

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

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: