PostGIS  2.5.1dev-r@@SVN_REVISION@@

◆ circ_tree_contains_point()

int circ_tree_contains_point ( const CIRC_NODE node,
const POINT2D pt,
const POINT2D pt_outside,
int *  on_boundary 
)

Walk the tree and count intersections between the stab line and the edges.

odd => containment, even => no containment. KNOWN PROBLEM: Grazings (think of a sharp point, just touching the stabline) will be counted for one, which will throw off the count.

Definition at line 482 of file lwgeodetic_tree.c.

References circ_node::center, circ_node_is_leaf(), circ_tree_contains_point(), edge_distance_to_point(), edge_intersects(), circ_node::edge_num, GEOGRAPHIC_EDGE::end, FP_LTEQ, geog2cart(), geographic_point_init(), GEOGRAPHIC_POINT::lat, GEOGRAPHIC_POINT::lon, LWDEBUG, LWDEBUGF, circ_node::nodes, circ_node::num_nodes, circ_node::p1, circ_node::p2, PIR_B_TOUCH_RIGHT, PIR_COLINEAR, PIR_INTERSECTS, rad2deg, circ_node::radius, GEOGRAPHIC_EDGE::start, POINT2D::x, and POINT2D::y.

Referenced by circ_tree_contains_point(), circ_tree_distance_tree_internal(), CircTreePIP(), test_tree_circ_pip(), and test_tree_circ_pip2().

483 {
484  GEOGRAPHIC_POINT closest;
485  GEOGRAPHIC_EDGE stab_edge, edge;
486  POINT3D S1, S2, E1, E2;
487  double d;
488  uint32_t i, c;
489 
490  /* Construct a stabline edge from our "inside" to our known outside point */
491  geographic_point_init(pt->x, pt->y, &(stab_edge.start));
492  geographic_point_init(pt_outside->x, pt_outside->y, &(stab_edge.end));
493  geog2cart(&(stab_edge.start), &S1);
494  geog2cart(&(stab_edge.end), &S2);
495 
496  LWDEBUG(3, "entered");
497 
498  /*
499  * If the stabline doesn't cross within the radius of a node, there's no
500  * way it can cross.
501  */
502 
503  LWDEBUGF(3, "working on node %p, edge_num %d, radius %g, center POINT(%g %g)", node, node->edge_num, node->radius, rad2deg(node->center.lon), rad2deg(node->center.lat));
504  d = edge_distance_to_point(&stab_edge, &(node->center), &closest);
505  LWDEBUGF(3, "edge_distance_to_point=%g, node_radius=%g", d, node->radius);
506  if ( FP_LTEQ(d, node->radius) )
507  {
508  LWDEBUGF(3,"entering this branch (%p)", node);
509 
510  /* Return the crossing number of this leaf */
511  if ( circ_node_is_leaf(node) )
512  {
513  int inter;
514  LWDEBUGF(3, "leaf node calculation (edge %d)", node->edge_num);
515  geographic_point_init(node->p1->x, node->p1->y, &(edge.start));
516  geographic_point_init(node->p2->x, node->p2->y, &(edge.end));
517  geog2cart(&(edge.start), &E1);
518  geog2cart(&(edge.end), &E2);
519 
520  inter = edge_intersects(&S1, &S2, &E1, &E2);
521 
522  if ( inter & PIR_INTERSECTS )
523  {
524  LWDEBUG(3," got stab line edge_intersection with this edge!");
525  /* To avoid double counting crossings-at-a-vertex, */
526  /* always ignore crossings at "lower" ends of edges*/
527 
528  if ( inter & PIR_B_TOUCH_RIGHT || inter & PIR_COLINEAR )
529  {
530  LWDEBUG(3," rejecting stab line grazing by left-side edge");
531  return 0;
532  }
533  else
534  {
535  LWDEBUG(3," accepting stab line intersection");
536  return 1;
537  }
538  }
539  }
540  /* Or, add up the crossing numbers of all children of this node. */
541  else
542  {
543  c = 0;
544  for ( i = 0; i < node->num_nodes; i++ )
545  {
546  LWDEBUG(3,"internal node calculation");
547  LWDEBUGF(3," calling circ_tree_contains_point on child %d!", i);
548  c += circ_tree_contains_point(node->nodes[i], pt, pt_outside, on_boundary);
549  }
550  return c % 2;
551  }
552  }
553  else
554  {
555  LWDEBUGF(3,"skipping this branch (%p)", node);
556  }
557 
558  return 0;
559 }
Two-point great circle segment from a to b.
Definition: lwgeodetic.h:56
#define PIR_B_TOUCH_RIGHT
Definition: lwgeodetic.h:86
GEOGRAPHIC_POINT center
uint32_t num_nodes
POINT2D * p2
POINT2D * p1
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
#define FP_LTEQ(A, B)
Point in spherical coordinates on the world.
Definition: lwgeodetic.h:47
unsigned int uint32_t
Definition: uthash.h:78
double x
Definition: liblwgeom.h:330
static int circ_node_is_leaf(const CIRC_NODE *node)
Internal nodes have their point references set to NULL.
#define rad2deg(r)
Definition: lwgeodetic.h:75
GEOGRAPHIC_POINT start
Definition: lwgeodetic.h:58
double edge_distance_to_point(const GEOGRAPHIC_EDGE *e, const GEOGRAPHIC_POINT *gp, GEOGRAPHIC_POINT *closest)
Definition: lwgeodetic.c:1187
int circ_tree_contains_point(const CIRC_NODE *node, const POINT2D *pt, const POINT2D *pt_outside, int *on_boundary)
Walk the tree and count intersections between the stab line and the edges.
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:3424
GEOGRAPHIC_POINT end
Definition: lwgeodetic.h:59
double y
Definition: liblwgeom.h:330
void geog2cart(const GEOGRAPHIC_POINT *g, POINT3D *p)
Convert spherical coordinates to cartesian coordinates on unit sphere.
Definition: lwgeodetic.c:373
void geographic_point_init(double lon, double lat, GEOGRAPHIC_POINT *g)
Initialize a geographic point.
Definition: lwgeodetic.c:171
double radius
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
#define PIR_COLINEAR
Definition: lwgeodetic.h:83
struct circ_node ** nodes
#define PIR_INTERSECTS
Definition: lwgeodetic.h:82
Here is the call graph for this function:
Here is the caller graph for this function: