PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ ptarray_contains_point_sphere()

int ptarray_contains_point_sphere ( const POINTARRAY pa,
const POINT2D pt_outside,
const POINT2D pt_to_test 
)

This routine returns LW_TRUE if the stabline joining the pt_outside and pt_to_test crosses the ring an odd number of times, or if the pt_to_test is on the ring boundary itself, returning LW_FALSE otherwise.

The pt_outside must be guaranteed to be outside the ring (use the geography_pt_outside() function to derive one in postgis, or the gbox_pt_outside() function if you don't mind burning CPU cycles building a gbox first).

Definition at line 3647 of file lwgeodetic.c.

3648 {
3649  POINT3D S1, S2; /* Stab line end points */
3650  POINT3D E1, E2; /* Edge end points (3-space) */
3651  POINT2D p; /* Edge end points (lon/lat) */
3652  uint32_t count = 0, i, inter;
3653 
3654  /* Null input, not enough points for a ring? You ain't closed! */
3655  if ( ! pa || pa->npoints < 4 )
3656  return LW_FALSE;
3657 
3658  /* Set up our stab line */
3659  ll2cart(pt_to_test, &S1);
3660  ll2cart(pt_outside, &S2);
3661 
3662  /* Initialize first point */
3663  getPoint2d_p(pa, 0, &p);
3664  ll2cart(&p, &E1);
3665 
3666  /* Walk every edge and see if the stab line hits it */
3667  for ( i = 1; i < pa->npoints; i++ )
3668  {
3669  LWDEBUGF(4, "testing edge (%d)", i);
3670  LWDEBUGF(4, " start point == POINT(%.12g %.12g)", p.x, p.y);
3671 
3672  /* Read next point. */
3673  getPoint2d_p(pa, i, &p);
3674  ll2cart(&p, &E2);
3675 
3676  /* Skip over too-short edges. */
3677  if ( point3d_equals(&E1, &E2) )
3678  {
3679  continue;
3680  }
3681 
3682  /* Our test point is on an edge end! Point is "in ring" by our definition */
3683  if ( point3d_equals(&S1, &E1) )
3684  {
3685  return LW_TRUE;
3686  }
3687 
3688  /* Calculate relationship between stab line and edge */
3689  inter = edge_intersects(&S1, &S2, &E1, &E2);
3690 
3691  /* We have some kind of interaction... */
3692  if ( inter & PIR_INTERSECTS )
3693  {
3694  /* If the stabline is touching the edge, that implies the test point */
3695  /* is on the edge, so we're done, the point is in (on) the ring. */
3696  if ( (inter & PIR_A_TOUCH_RIGHT) || (inter & PIR_A_TOUCH_LEFT) )
3697  {
3698  return LW_TRUE;
3699  }
3700 
3701  /* It's a touching interaction, disregard all the left-side ones. */
3702  /* It's a co-linear intersection, ignore those. */
3703  if ( inter & PIR_B_TOUCH_RIGHT || inter & PIR_COLINEAR )
3704  {
3705  /* Do nothing, to avoid double counts. */
3706  LWDEBUGF(4," edge (%d) crossed, disregarding to avoid double count", i, count);
3707  }
3708  else
3709  {
3710  /* Increment crossingn count. */
3711  count++;
3712  LWDEBUGF(4," edge (%d) crossed, count == %d", i, count);
3713  }
3714  }
3715  else
3716  {
3717  LWDEBUGF(4," edge (%d) did not cross", i);
3718  }
3719 
3720  /* Increment to next edge */
3721  E1 = E2;
3722  }
3723 
3724  LWDEBUGF(4,"final count == %d", count);
3725 
3726  /* An odd number of crossings implies containment! */
3727  if ( count % 2 )
3728  {
3729  return LW_TRUE;
3730  }
3731 
3732  return LW_FALSE;
3733 }
#define LW_FALSE
Definition: liblwgeom.h:108
int getPoint2d_p(const POINTARRAY *pa, uint32_t n, POINT2D *point)
Definition: lwgeom_api.c:349
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:107
void ll2cart(const POINT2D *g, POINT3D *p)
Convert lon/lat coordinates to cartesian coordinates on unit sphere.
Definition: lwgeodetic.c:423
uint32_t edge_intersects(const POINT3D *A1, const POINT3D *A2, const POINT3D *B1, const POINT3D *B2)
Returns non-zero if edges A and B interact.
Definition: lwgeodetic.c:3541
static int point3d_equals(const POINT3D *p1, const POINT3D *p2)
Utility function for ptarray_contains_point_sphere()
Definition: lwgeodetic.c:42
#define PIR_A_TOUCH_LEFT
Definition: lwgeodetic.h:91
#define PIR_COLINEAR
Definition: lwgeodetic.h:89
#define PIR_INTERSECTS
Definition: lwgeodetic.h:88
#define PIR_A_TOUCH_RIGHT
Definition: lwgeodetic.h:90
#define PIR_B_TOUCH_RIGHT
Definition: lwgeodetic.h:92
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
int count
Definition: genraster.py:57
double y
Definition: liblwgeom.h:376
double x
Definition: liblwgeom.h:376
uint32_t npoints
Definition: liblwgeom.h:413

References genraster::count, edge_intersects(), getPoint2d_p(), ll2cart(), LW_FALSE, LW_TRUE, LWDEBUGF, POINTARRAY::npoints, PIR_A_TOUCH_LEFT, PIR_A_TOUCH_RIGHT, PIR_B_TOUCH_RIGHT, PIR_COLINEAR, PIR_INTERSECTS, point3d_equals(), POINT2D::x, and POINT2D::y.

Referenced by lwpoly_covers_point2d(), test_ptarray_contains_point_sphere(), and test_ptarray_contains_point_sphere_iowa().

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