PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ circ_tree_contains_point()

int circ_tree_contains_point ( const CIRC_NODE node,
const POINT2D pt,
const POINT2D pt_outside,
int  level,
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 493 of file lwgeodetic_tree.c.

References cart2geog(), 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, 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().

494 {
495  GEOGRAPHIC_POINT closest;
496  GEOGRAPHIC_EDGE stab_edge, edge;
497  POINT3D S1, S2, E1, E2;
498  double d;
499  int i, c;
500 
501  /* Construct a stabline edge from our "inside" to our known outside point */
502  geographic_point_init(pt->x, pt->y, &(stab_edge.start));
503  geographic_point_init(pt_outside->x, pt_outside->y, &(stab_edge.end));
504  geog2cart(&(stab_edge.start), &S1);
505  geog2cart(&(stab_edge.end), &S2);
506 
507  LWDEBUGF(3, "%*s entered", level, "");
508 
509  /*
510  * If the stabline doesn't cross within the radius of a node, there's no
511  * way it can cross.
512  */
513 
514  LWDEBUGF(3, "%*s :working on node %p, edge_num %d, radius %g, center POINT(%.12g %.12g)", level, "", node, node->edge_num, node->radius, rad2deg(node->center.lon), rad2deg(node->center.lat));
515  d = edge_distance_to_point(&stab_edge, &(node->center), &closest);
516  LWDEBUGF(3, "%*s :edge_distance_to_point=%g, node_radius=%g", level, "", d, node->radius);
517  if ( FP_LTEQ(d, node->radius) )
518  {
519  LWDEBUGF(3,"%*s :entering this branch (%p)", level, "", node);
520 
521  /* Return the crossing number of this leaf */
522  if ( circ_node_is_leaf(node) )
523  {
524  int inter;
525  LWDEBUGF(3, "%*s :leaf node calculation (edge %d)", level, "", node->edge_num);
526  geographic_point_init(node->p1->x, node->p1->y, &(edge.start));
527  geographic_point_init(node->p2->x, node->p2->y, &(edge.end));
528  geog2cart(&(edge.start), &E1);
529  geog2cart(&(edge.end), &E2);
530 
531  inter = edge_intersects(&S1, &S2, &E1, &E2);
532  LWDEBUGF(3, "%*s :inter = %d", level, "", inter);
533 
534  if ( inter & PIR_INTERSECTS )
535  {
536  LWDEBUGF(3,"%*s ::got stab line edge_intersection with this edge!", level, "");
537  /* To avoid double counting crossings-at-a-vertex, */
538  /* always ignore crossings at "lower" ends of edges*/
539  GEOGRAPHIC_POINT e1, e2;
540  cart2geog(&E1,&e1); cart2geog(&E2,&e2);
541 
542  LWDEBUGF(3,"%*s LINESTRING(%.15g %.15g,%.15g %.15g)", level, "",
543  pt->x, pt->y,
544  pt_outside->x, pt_outside->y
545  );
546 
547  LWDEBUGF(3,"%*s LINESTRING(%.15g %.15g,%.15g %.15g)", level, "",
548  rad2deg(e1.lon), rad2deg(e1.lat),
549  rad2deg(e2.lon), rad2deg(e2.lat)
550  );
551 
552  if ( inter & PIR_B_TOUCH_RIGHT || inter & PIR_COLINEAR )
553  {
554  LWDEBUGF(3,"%*s ::rejecting stab line grazing by left-side edge", level, "");
555  return 0;
556  }
557  else
558  {
559  LWDEBUGF(3,"%*s ::accepting stab line intersection", level, "");
560  return 1;
561  }
562  }
563  else
564  {
565  LWDEBUGF(3,"%*s edge does not intersect", level, "");
566  }
567  }
568  /* Or, add up the crossing numbers of all children of this node. */
569  else
570  {
571  c = 0;
572  for ( i = 0; i < node->num_nodes; i++ )
573  {
574  LWDEBUGF(3,"%*s calling circ_tree_contains_point on child %d!", level, "", i);
575  c += circ_tree_contains_point(node->nodes[i], pt, pt_outside, level + 1, on_boundary);
576  }
577  return c % 2;
578  }
579  }
580  else
581  {
582  LWDEBUGF(3,"%*s skipping this branch (%p)", level, "", node);
583  }
584  return 0;
585 }
Two-point great circle segment from a to b.
Definition: lwgeodetic.h:61
#define PIR_B_TOUCH_RIGHT
Definition: lwgeodetic.h:91
GEOGRAPHIC_POINT center
POINT2D * p2
POINT2D * p1
void cart2geog(const POINT3D *p, GEOGRAPHIC_POINT *g)
Convert cartesion coordinates on unit sphere to spherical coordinates.
Definition: lwgeodetic.c:410
#define FP_LTEQ(A, B)
Point in spherical coordinates on the world.
Definition: lwgeodetic.h:52
int circ_tree_contains_point(const CIRC_NODE *node, const POINT2D *pt, const POINT2D *pt_outside, int level, int *on_boundary)
Walk the tree and count intersections between the stab line and the edges.
double x
Definition: liblwgeom.h:328
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:80
GEOGRAPHIC_POINT start
Definition: lwgeodetic.h:63
double edge_distance_to_point(const GEOGRAPHIC_EDGE *e, const GEOGRAPHIC_POINT *gp, GEOGRAPHIC_POINT *closest)
Definition: lwgeodetic.c:1214
GEOGRAPHIC_POINT end
Definition: lwgeodetic.h:64
double y
Definition: liblwgeom.h:328
void geog2cart(const GEOGRAPHIC_POINT *g, POINT3D *p)
Convert spherical coordinates to cartesion coordinates on unit sphere.
Definition: lwgeodetic.c:400
int 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:3525
void geographic_point_init(double lon, double lat, GEOGRAPHIC_POINT *g)
Initialize a geographic point.
Definition: lwgeodetic.c:180
double radius
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
#define PIR_COLINEAR
Definition: lwgeodetic.h:88
struct circ_node ** nodes
#define PIR_INTERSECTS
Definition: lwgeodetic.h:87
Here is the call graph for this function:
Here is the caller graph for this function: