PostGIS  2.4.9dev-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 631 of file lwgeodetic_tree.c.

References circ_node_is_leaf(), circ_node_max_distance(), circ_node_min_distance(), circ_tree_contains_point(), circ_tree_get_point(), edge_distance_to_edge(), edge_distance_to_point(), edge_intersection(), edge_intersects(), circ_node::edge_num, GEOGRAPHIC_EDGE::end, FP_MIN, geog2cart(), geographic_point_init(), circ_node::geom_type, LWDEBUG, LWDEBUGF, lwtype_is_collection(), circ_node::nodes, circ_node::num_nodes, circ_node::p1, circ_node::p2, POLYGONTYPE, circ_node::pt_outside, sphere_distance(), GEOGRAPHIC_EDGE::start, POINT2D::x, and POINT2D::y.

Referenced by circ_tree_distance_tree().

632 {
633  double max;
634  double d, d_min;
635  int i;
636 
637  LWDEBUGF(4, "entered, min_dist=%.8g max_dist=%.8g, type1=%d, type2=%d", *min_dist, *max_dist, n1->geom_type, n2->geom_type);
638 
639  // printf("-==-\n");
640  // circ_tree_print(n1, 0);
641  // printf("--\n");
642  // circ_tree_print(n2, 0);
643 
644  /* Short circuit if we've already hit the minimum */
645  if( *min_dist < threshold || *min_dist == 0.0 )
646  return *min_dist;
647 
648  /* If your minimum is greater than anyone's maximum, you can't hold the winner */
649  if( circ_node_min_distance(n1, n2) > *max_dist )
650  {
651  LWDEBUGF(4, "pruning pair %p, %p", n1, n2);
652  return FLT_MAX;
653  }
654 
655  /* If your maximum is a new low, we'll use that as our new global tolerance */
656  max = circ_node_max_distance(n1, n2);
657  LWDEBUGF(5, "max %.8g", max);
658  if( max < *max_dist )
659  *max_dist = max;
660 
661  /* Polygon on one side, primitive type on the other. Check for point-in-polygon */
662  /* short circuit. */
663  if ( n1->geom_type == POLYGONTYPE && n2->geom_type && ! lwtype_is_collection(n2->geom_type) )
664  {
665  POINT2D pt;
666  circ_tree_get_point(n2, &pt);
667  LWDEBUGF(4, "n1 is polygon, testing if contains (%.5g,%.5g)", pt.x, pt.y);
668  if ( circ_tree_contains_point(n1, &pt, &(n1->pt_outside), 0, NULL) )
669  {
670  LWDEBUG(4, "it does");
671  *min_dist = 0.0;
672  geographic_point_init(pt.x, pt.y, closest1);
673  geographic_point_init(pt.x, pt.y, closest2);
674  return *min_dist;
675  }
676  }
677  /* Polygon on one side, primitive type on the other. Check for point-in-polygon */
678  /* short circuit. */
679  if ( n2->geom_type == POLYGONTYPE && n1->geom_type && ! lwtype_is_collection(n1->geom_type) )
680  {
681  POINT2D pt;
682  circ_tree_get_point(n1, &pt);
683  LWDEBUGF(4, "n2 is polygon, testing if contains (%.5g,%.5g)", pt.x, pt.y);
684  if ( circ_tree_contains_point(n2, &pt, &(n2->pt_outside), 0, NULL) )
685  {
686  LWDEBUG(4, "it does");
687  geographic_point_init(pt.x, pt.y, closest1);
688  geographic_point_init(pt.x, pt.y, closest2);
689  *min_dist = 0.0;
690  return *min_dist;
691  }
692  }
693 
694  /* Both leaf nodes, do a real distance calculation */
695  if( circ_node_is_leaf(n1) && circ_node_is_leaf(n2) )
696  {
697  double d;
698  GEOGRAPHIC_POINT close1, close2;
699  LWDEBUGF(4, "testing leaf pair [%d], [%d]", n1->edge_num, n2->edge_num);
700  /* One of the nodes is a point */
701  if ( n1->p1 == n1->p2 || n2->p1 == n2->p2 )
702  {
703  GEOGRAPHIC_EDGE e;
704  GEOGRAPHIC_POINT gp1, gp2;
705 
706  /* Both nodes are points! */
707  if ( n1->p1 == n1->p2 && n2->p1 == n2->p2 )
708  {
709  geographic_point_init(n1->p1->x, n1->p1->y, &gp1);
710  geographic_point_init(n2->p1->x, n2->p1->y, &gp2);
711  close1 = gp1; close2 = gp2;
712  d = sphere_distance(&gp1, &gp2);
713  }
714  /* Node 1 is a point */
715  else if ( n1->p1 == n1->p2 )
716  {
717  geographic_point_init(n1->p1->x, n1->p1->y, &gp1);
718  geographic_point_init(n2->p1->x, n2->p1->y, &(e.start));
719  geographic_point_init(n2->p2->x, n2->p2->y, &(e.end));
720  close1 = gp1;
721  d = edge_distance_to_point(&e, &gp1, &close2);
722  }
723  /* Node 2 is a point */
724  else
725  {
726  geographic_point_init(n2->p1->x, n2->p1->y, &gp1);
727  geographic_point_init(n1->p1->x, n1->p1->y, &(e.start));
728  geographic_point_init(n1->p2->x, n1->p2->y, &(e.end));
729  close1 = gp1;
730  d = edge_distance_to_point(&e, &gp1, &close2);
731  }
732  LWDEBUGF(4, " got distance %g", d);
733  }
734  /* Both nodes are edges */
735  else
736  {
737  GEOGRAPHIC_EDGE e1, e2;
739  POINT3D A1, A2, B1, B2;
740  geographic_point_init(n1->p1->x, n1->p1->y, &(e1.start));
741  geographic_point_init(n1->p2->x, n1->p2->y, &(e1.end));
742  geographic_point_init(n2->p1->x, n2->p1->y, &(e2.start));
743  geographic_point_init(n2->p2->x, n2->p2->y, &(e2.end));
744  geog2cart(&(e1.start), &A1);
745  geog2cart(&(e1.end), &A2);
746  geog2cart(&(e2.start), &B1);
747  geog2cart(&(e2.end), &B2);
748  if ( edge_intersects(&A1, &A2, &B1, &B2) )
749  {
750  d = 0.0;
751  edge_intersection(&e1, &e2, &g);
752  close1 = close2 = g;
753  }
754  else
755  {
756  d = edge_distance_to_edge(&e1, &e2, &close1, &close2);
757  }
758  LWDEBUGF(4, "edge_distance_to_edge returned %g", d);
759  }
760  if ( d < *min_dist )
761  {
762  *min_dist = d;
763  *closest1 = close1;
764  *closest2 = close2;
765  }
766  return d;
767  }
768  else
769  {
770  d_min = FLT_MAX;
771  /* Drive the recursion into the COLLECTION types first so we end up with */
772  /* pairings of primitive geometries that can be forced into the point-in-polygon */
773  /* tests above. */
774  if ( n1->geom_type && lwtype_is_collection(n1->geom_type) )
775  {
776  for ( i = 0; i < n1->num_nodes; i++ )
777  {
778  d = circ_tree_distance_tree_internal(n1->nodes[i], n2, threshold, min_dist, max_dist, closest1, closest2);
779  d_min = FP_MIN(d_min, d);
780  }
781  }
782  else if ( n2->geom_type && lwtype_is_collection(n2->geom_type) )
783  {
784  for ( i = 0; i < n2->num_nodes; i++ )
785  {
786  d = circ_tree_distance_tree_internal(n1, n2->nodes[i], threshold, min_dist, max_dist, closest1, closest2);
787  d_min = FP_MIN(d_min, d);
788  }
789  }
790  else if ( ! circ_node_is_leaf(n1) )
791  {
792  for ( i = 0; i < n1->num_nodes; i++ )
793  {
794  d = circ_tree_distance_tree_internal(n1->nodes[i], n2, threshold, min_dist, max_dist, closest1, closest2);
795  d_min = FP_MIN(d_min, d);
796  }
797  }
798  else if ( ! circ_node_is_leaf(n2) )
799  {
800  for ( i = 0; i < n2->num_nodes; i++ )
801  {
802  d = circ_tree_distance_tree_internal(n1, n2->nodes[i], threshold, min_dist, max_dist, closest1, closest2);
803  d_min = FP_MIN(d_min, d);
804  }
805  }
806  else
807  {
808  /* Never get here */
809  }
810 
811  return d_min;
812  }
813 }
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:944
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:1265
Two-point great circle segment from a to b.
Definition: lwgeodetic.h:61
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:87
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: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.
static double circ_node_max_distance(const CIRC_NODE *n1, const CIRC_NODE *n2)
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
int lwtype_is_collection(uint8_t type)
Determine whether a type number is a collection or not.
Definition: lwgeom.c:1048
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
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:180
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:1123
struct circ_node ** nodes
Here is the call graph for this function:
Here is the caller graph for this function: