PostGIS  2.3.8dev-r@@SVN_REVISION@@

## ◆ circ_tree_distance_tree_internal()

 static double circ_tree_distance_tree_internal ( const CIRC_NODE * n1, const CIRC_NODE * n2, double threshold, double * min_dist, double * max_dist, GEOGRAPHIC_POINT * closest1, GEOGRAPHIC_POINT * closest2 )
static

Definition at line 603 of file lwgeodetic_tree.c.

Referenced by circ_tree_distance_tree().

604 {
605  double max;
606  double d, d_min;
607  int i;
608
609  LWDEBUGF(4, "entered, min_dist=%.8g max_dist=%.8g, type1=%d, type2=%d", *min_dist, *max_dist, n1->geom_type, n2->geom_type);
610 /*
611  circ_tree_print(n1, 0);
612  circ_tree_print(n2, 0);
613 */
614
615  /* Short circuit if we've already hit the minimum */
616  if( *min_dist < threshold || *min_dist == 0.0 )
617  return *min_dist;
618
619  /* If your minimum is greater than anyone's maximum, you can't hold the winner */
620  if( circ_node_min_distance(n1, n2) > *max_dist )
621  {
622  LWDEBUGF(4, "pruning pair %p, %p", n1, n2);
623  return FLT_MAX;
624  }
625
626  /* If your maximum is a new low, we'll use that as our new global tolerance */
627  max = circ_node_max_distance(n1, n2);
628  LWDEBUGF(5, "max %.8g", max);
629  if( max < *max_dist )
630  *max_dist = max;
631
632  /* Polygon on one side, primitive type on the other. Check for point-in-polygon */
633  /* short circuit. */
634  if ( n1->geom_type == POLYGONTYPE && n2->geom_type && ! lwtype_is_collection(n2->geom_type) )
635  {
636  POINT2D pt;
637  circ_tree_get_point(n2, &pt);
638  LWDEBUGF(4, "n1 is polygon, testing if contains (%.5g,%.5g)", pt.x, pt.y);
639  if ( circ_tree_contains_point(n1, &pt, &(n1->pt_outside), NULL) )
640  {
641  LWDEBUG(4, "it does");
642  *min_dist = 0.0;
643  geographic_point_init(pt.x, pt.y, closest1);
644  geographic_point_init(pt.x, pt.y, closest2);
645  return *min_dist;
646  }
647  }
648  /* Polygon on one side, primitive type on the other. Check for point-in-polygon */
649  /* short circuit. */
650  if ( n2->geom_type == POLYGONTYPE && n1->geom_type && ! lwtype_is_collection(n1->geom_type) )
651  {
652  POINT2D pt;
653  circ_tree_get_point(n1, &pt);
654  LWDEBUGF(4, "n2 is polygon, testing if contains (%.5g,%.5g)", pt.x, pt.y);
655  if ( circ_tree_contains_point(n2, &pt, &(n2->pt_outside), NULL) )
656  {
657  LWDEBUG(4, "it does");
658  geographic_point_init(pt.x, pt.y, closest1);
659  geographic_point_init(pt.x, pt.y, closest2);
660  *min_dist = 0.0;
661  return *min_dist;
662  }
663  }
664
665  /* Both leaf nodes, do a real distance calculation */
666  if( circ_node_is_leaf(n1) && circ_node_is_leaf(n2) )
667  {
668  double d;
669  GEOGRAPHIC_POINT close1, close2;
670  LWDEBUGF(4, "testing leaf pair [%d], [%d]", n1->edge_num, n2->edge_num);
671  /* One of the nodes is a point */
672  if ( n1->p1 == n1->p2 || n2->p1 == n2->p2 )
673  {
674  GEOGRAPHIC_EDGE e;
675  GEOGRAPHIC_POINT gp1, gp2;
676
677  /* Both nodes are points! */
678  if ( n1->p1 == n1->p2 && n2->p1 == n2->p2 )
679  {
680  geographic_point_init(n1->p1->x, n1->p1->y, &gp1);
681  geographic_point_init(n2->p1->x, n2->p1->y, &gp2);
682  close1 = gp1; close2 = gp2;
683  d = sphere_distance(&gp1, &gp2);
684  }
685  /* Node 1 is a point */
686  else if ( n1->p1 == n1->p2 )
687  {
688  geographic_point_init(n1->p1->x, n1->p1->y, &gp1);
689  geographic_point_init(n2->p1->x, n2->p1->y, &(e.start));
690  geographic_point_init(n2->p2->x, n2->p2->y, &(e.end));
691  close1 = gp1;
692  d = edge_distance_to_point(&e, &gp1, &close2);
693  }
694  /* Node 2 is a point */
695  else
696  {
697  geographic_point_init(n2->p1->x, n2->p1->y, &gp1);
698  geographic_point_init(n1->p1->x, n1->p1->y, &(e.start));
699  geographic_point_init(n1->p2->x, n1->p2->y, &(e.end));
700  close1 = gp1;
701  d = edge_distance_to_point(&e, &gp1, &close2);
702  }
703  LWDEBUGF(4, " got distance %g", d);
704  }
705  /* Both nodes are edges */
706  else
707  {
708  GEOGRAPHIC_EDGE e1, e2;
710  POINT3D A1, A2, B1, B2;
711  geographic_point_init(n1->p1->x, n1->p1->y, &(e1.start));
712  geographic_point_init(n1->p2->x, n1->p2->y, &(e1.end));
713  geographic_point_init(n2->p1->x, n2->p1->y, &(e2.start));
714  geographic_point_init(n2->p2->x, n2->p2->y, &(e2.end));
715  geog2cart(&(e1.start), &A1);
716  geog2cart(&(e1.end), &A2);
717  geog2cart(&(e2.start), &B1);
718  geog2cart(&(e2.end), &B2);
719  if ( edge_intersects(&A1, &A2, &B1, &B2) )
720  {
721  d = 0.0;
722  edge_intersection(&e1, &e2, &g);
723  close1 = close2 = g;
724  }
725  else
726  {
727  d = edge_distance_to_edge(&e1, &e2, &close1, &close2);
728  }
729  LWDEBUGF(4, "edge_distance_to_edge returned %g", d);
730  }
731  if ( d < *min_dist )
732  {
733  *min_dist = d;
734  *closest1 = close1;
735  *closest2 = close2;
736  }
737  return d;
738  }
739  else
740  {
741  d_min = FLT_MAX;
742  /* Drive the recursion into the COLLECTION types first so we end up with */
743  /* pairings of primitive geometries that can be forced into the point-in-polygon */
744  /* tests above. */
745  if ( n1->geom_type && lwtype_is_collection(n1->geom_type) )
746  {
747  for ( i = 0; i < n1->num_nodes; i++ )
748  {
749  d = circ_tree_distance_tree_internal(n1->nodes[i], n2, threshold, min_dist, max_dist, closest1, closest2);
750  d_min = FP_MIN(d_min, d);
751  }
752  }
753  else if ( n2->geom_type && lwtype_is_collection(n2->geom_type) )
754  {
755  for ( i = 0; i < n2->num_nodes; i++ )
756  {
757  d = circ_tree_distance_tree_internal(n1, n2->nodes[i], threshold, min_dist, max_dist, closest1, closest2);
758  d_min = FP_MIN(d_min, d);
759  }
760  }
761  else if ( ! circ_node_is_leaf(n1) )
762  {
763  for ( i = 0; i < n1->num_nodes; i++ )
764  {
765  d = circ_tree_distance_tree_internal(n1->nodes[i], n2, threshold, min_dist, max_dist, closest1, closest2);
766  d_min = FP_MIN(d_min, d);
767  }
768  }
769  else if ( ! circ_node_is_leaf(n2) )
770  {
771  for ( i = 0; i < n2->num_nodes; i++ )
772  {
773  d = circ_tree_distance_tree_internal(n1, n2->nodes[i], threshold, min_dist, max_dist, closest1, closest2);
774  d_min = FP_MIN(d_min, d);
775  }
776  }
777  else
778  {
779  /* Never get here */
780  }
781
782  return d_min;
783  }
784 }
double sphere_distance(const GEOGRAPHIC_POINT *s, const GEOGRAPHIC_POINT *e)
Given two points on a unit sphere, calculate their distance apart in radians.
Definition: lwgeodetic.c:913
double edge_distance_to_edge(const GEOGRAPHIC_EDGE *e1, const GEOGRAPHIC_EDGE *e2, GEOGRAPHIC_POINT *closest1, GEOGRAPHIC_POINT *closest2)
Calculate the distance between two edges.
Definition: lwgeodetic.c:1234
Two-point great circle segment from a to b.
Definition: lwgeodetic.h:56
int circ_tree_get_point(const CIRC_NODE *node, POINT2D *pt)
Returns a POINT2D that is a vertex of the input shape.
POINT2D * p2
#define POLYGONTYPE
Definition: liblwgeom.h:86
POINT2D * p1
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
static double circ_tree_distance_tree_internal(const CIRC_NODE *n1, const CIRC_NODE *n2, double threshold, double *min_dist, double *max_dist, GEOGRAPHIC_POINT *closest1, GEOGRAPHIC_POINT *closest2)
#define FP_MIN(A, B)
Point in spherical coordinates on the world.
Definition: lwgeodetic.h:47
double x
Definition: liblwgeom.h:327
static int circ_node_is_leaf(const CIRC_NODE *node)
Internal nodes have their point references set to NULL.
static double circ_node_max_distance(const CIRC_NODE *n1, const CIRC_NODE *n2)
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:1183
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.
int lwtype_is_collection(uint8_t type)
Determine whether a type number is a collection or not.
Definition: lwgeom.c:1012
GEOGRAPHIC_POINT end
Definition: lwgeodetic.h:59
double y
Definition: liblwgeom.h:327
void geog2cart(const GEOGRAPHIC_POINT *g, POINT3D *p)
Convert spherical coordinates to cartesion coordinates on unit sphere.
Definition: lwgeodetic.c:369
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:3110
static double circ_node_min_distance(const CIRC_NODE *n1, const CIRC_NODE *n2)
void geographic_point_init(double lon, double lat, GEOGRAPHIC_POINT *g)
Initialize a geographic point.
Definition: lwgeodetic.c:171
POINT2D pt_outside
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
int edge_intersection(const GEOGRAPHIC_EDGE *e1, const GEOGRAPHIC_EDGE *e2, GEOGRAPHIC_POINT *g)
Returns true if an intersection can be calculated, and places it in *g.
Definition: lwgeodetic.c:1092
struct circ_node ** nodes
Here is the call graph for this function:
Here is the caller graph for this function: