PostGIS  2.1.10dev-r@@SVN_REVISION@@
lwgeodetic_tree.c
Go to the documentation of this file.
1 #include "liblwgeom_internal.h"
2 #include "lwgeodetic_tree.h"
3 #include "lwgeom_log.h"
4 
5 
6 /* Internal prototype */
7 static CIRC_NODE* circ_nodes_merge(CIRC_NODE** nodes, int num_nodes);
8 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);
9 
10 
14 static inline int
16 {
17  return (node->num_nodes == 0);
18 }
19 
24 void
26 {
27  int i;
28  if ( ! node ) return;
29 
30  for ( i = 0; i < node->num_nodes; i++ )
31  circ_tree_free(node->nodes[i]);
32 
33  if ( node->nodes ) lwfree(node->nodes);
34  lwfree(node);
35 }
36 
37 
41 static CIRC_NODE*
42 circ_node_leaf_new(const POINTARRAY* pa, int i)
43 {
44  POINT2D *p1, *p2;
45  POINT3D q1, q2, c;
46  GEOGRAPHIC_POINT g1, g2, gc;
47  CIRC_NODE *node;
48  double diameter;
49 
50  p1 = (POINT2D*)getPoint_internal(pa, i);
51  p2 = (POINT2D*)getPoint_internal(pa, i+1);
52  geographic_point_init(p1->x, p1->y, &g1);
53  geographic_point_init(p2->x, p2->y, &g2);
54 
55  LWDEBUGF(3,"edge #%d (%g %g, %g %g)", i, p1->x, p1->y, p2->x, p2->y);
56 
57  diameter = sphere_distance(&g1, &g2);
58 
59  /* Zero length edge, doesn't get a node */
60  if ( FP_EQUALS(diameter, 0.0) )
61  return NULL;
62 
63  /* Allocate */
64  node = lwalloc(sizeof(CIRC_NODE));
65  node->p1 = p1;
66  node->p2 = p2;
67 
68  /* Convert ends to X/Y/Z, sum, and normalize to get mid-point */
69  geog2cart(&g1, &q1);
70  geog2cart(&g2, &q2);
71  vector_sum(&q1, &q2, &c);
72  normalize(&c);
73  cart2geog(&c, &gc);
74  node->center = gc;
75  node->radius = diameter / 2.0;
76 
77  LWDEBUGF(3,"edge #%d CENTER(%g %g) RADIUS=%g", i, gc.lon, gc.lat, node->radius);
78 
79  /* Leaf has no children */
80  node->num_nodes = 0;
81  node->nodes = NULL;
82  node->edge_num = i;
83 
84  /* Zero out metadata */
85  node->pt_outside.x = 0.0;
86  node->pt_outside.y = 0.0;
87  node->geom_type = 0;
88 
89  return node;
90 }
91 
95 static CIRC_NODE*
97 {
98  CIRC_NODE* tree = lwalloc(sizeof(CIRC_NODE));
99  tree->p1 = tree->p2 = (POINT2D*)getPoint_internal(pa, 0);
100  geographic_point_init(tree->p1->x, tree->p1->y, &(tree->center));
101  tree->radius = 0.0;
102  tree->nodes = NULL;
103  tree->num_nodes = 0;
104  tree->edge_num = 0;
105  tree->geom_type = POINTTYPE;
106  tree->pt_outside.x = 0.0;
107  tree->pt_outside.y = 0.0;
108  return tree;
109 }
110 
115 static int
116 circ_node_compare(const void* v1, const void* v2)
117 {
118  POINT2D p1, p2;
119  unsigned int u1, u2;
120  CIRC_NODE *c1 = *((CIRC_NODE**)v1);
121  CIRC_NODE *c2 = *((CIRC_NODE**)v2);
122  p1.x = rad2deg((c1->center).lon);
123  p1.y = rad2deg((c1->center).lat);
124  p2.x = rad2deg((c2->center).lon);
125  p2.y = rad2deg((c2->center).lat);
126  u1 = geohash_point_as_int(&p1);
127  u2 = geohash_point_as_int(&p2);
128  if ( u1 < u2 ) return -1;
129  if ( u1 > u2 ) return 1;
130  return 0;
131 }
132 
139 static int
140 circ_center_spherical(const GEOGRAPHIC_POINT* c1, const GEOGRAPHIC_POINT* c2, double distance, double offset, GEOGRAPHIC_POINT* center)
141 {
142  /* Direction from c1 to c2 */
143  double dir = sphere_direction(c1, c2, distance);
144 
145  LWDEBUGF(4,"calculating spherical center", dir);
146 
147  LWDEBUGF(4,"dir is %g", dir);
148 
149  /* Catch sphere_direction when it barfs */
150  if ( isnan(dir) )
151  return LW_FAILURE;
152 
153  /* Center of new circle is projection from start point, using offset distance*/
154  return sphere_project(c1, offset, dir, center);
155 }
156 
164 static int
165 circ_center_cartesian(const GEOGRAPHIC_POINT* c1, const GEOGRAPHIC_POINT* c2, double distance, double offset, GEOGRAPHIC_POINT* center)
166 {
167  POINT3D p1, p2;
168  POINT3D p1p2, pc;
169  double proportion = offset/distance;
170 
171  LWDEBUG(4,"calculating cartesian center");
172 
173  geog2cart(c1, &p1);
174  geog2cart(c2, &p2);
175 
176  /* Difference between p2 and p1 */
177  p1p2.x = p2.x - p1.x;
178  p1p2.y = p2.y - p1.y;
179  p1p2.z = p2.z - p1.z;
180 
181  /* Scale difference to proportion */
182  p1p2.x *= proportion;
183  p1p2.y *= proportion;
184  p1p2.z *= proportion;
185 
186  /* Add difference to p1 to get approximate center point */
187  pc.x = p1.x + p1p2.x;
188  pc.y = p1.y + p1p2.y;
189  pc.z = p1.z + p1p2.z;
190  normalize(&pc);
191 
192  /* Convert center point to geographics */
193  cart2geog(&pc, center);
194 
195  return LW_SUCCESS;
196 }
197 
198 
203 static CIRC_NODE*
205 {
206  CIRC_NODE *node = NULL;
207  GEOGRAPHIC_POINT new_center, c1;
208  double new_radius;
209  double offset1, dist, D, r1, ri;
210  int i, new_geom_type;
211 
212  LWDEBUGF(3, "called with %d nodes --", num_nodes);
213 
214  /* Can't do anything w/ empty input */
215  if ( num_nodes < 1 )
216  return node;
217 
218  /* Initialize calculation with values of the first circle */
219  new_center = c[0]->center;
220  new_radius = c[0]->radius;
221  new_geom_type = c[0]->geom_type;
222 
223  /* Merge each remaining circle into the new circle */
224  for ( i = 1; i < num_nodes; i++ )
225  {
226  c1 = new_center;
227  r1 = new_radius;
228 
229  dist = sphere_distance(&c1, &(c[i]->center));
230  ri = c[i]->radius;
231 
232  /* Promote geometry types up the tree, getting more and more collected */
233  /* Go until we find a value */
234  if ( ! new_geom_type )
235  {
236  new_geom_type = c[i]->geom_type;
237  }
238  /* Promote singleton to a multi-type */
239  else if ( ! lwtype_is_collection(new_geom_type) )
240  {
241  /* Anonymous collection if types differ */
242  if ( new_geom_type != c[i]->geom_type )
243  {
244  new_geom_type = COLLECTIONTYPE;
245  }
246  else
247  {
248  new_geom_type = lwtype_get_collectiontype(new_geom_type);
249  }
250  }
251  /* If we can't add next feature to this collection cleanly, promote again to anonymous collection */
252  else if ( new_geom_type != lwtype_get_collectiontype(c[i]->geom_type) )
253  {
254  new_geom_type = COLLECTIONTYPE;
255  }
256 
257 
258  LWDEBUGF(3, "distance between new (%g %g) and %i (%g %g) is %g", c1.lon, c1.lat, i, c[i]->center.lon, c[i]->center.lat, dist);
259 
260  if ( FP_EQUALS(dist, 0) )
261  {
262  LWDEBUG(3, " distance between centers is zero");
263  new_radius = r1 + 2*dist;
264  new_center = c1;
265  }
266  else if ( dist < fabs(r1 - ri) )
267  {
268  /* new contains next */
269  if ( r1 > ri )
270  {
271  LWDEBUG(3, " c1 contains ci");
272  new_center = c1;
273  new_radius = r1;
274  }
275  /* next contains new */
276  else
277  {
278  LWDEBUG(3, " ci contains c1");
279  new_center = c[i]->center;
280  new_radius = ri;
281  }
282  }
283  else
284  {
285  LWDEBUG(3, " calculating new center");
286  /* New circle diameter */
287  D = dist + r1 + ri;
288  LWDEBUGF(3," D is %g", D);
289 
290  /* New radius */
291  new_radius = D / 2.0;
292 
293  /* Distance from cn1 center to the new center */
294  offset1 = ri + (D - (2.0*r1 + 2.0*ri)) / 2.0;
295  LWDEBUGF(3," offset1 is %g", offset1);
296 
297  /* Sometimes the sphere_direction function fails... this causes the center calculation */
298  /* to fail too. In that case, we're going to fall back ot a cartesian calculation, which */
299  /* is less exact, so we also have to pad the radius by (hack alert) an arbitrary amount */
300  /* which is hopefully always big enough to contain the input edges */
301  if ( circ_center_spherical(&c1, &(c[i]->center), dist, offset1, &new_center) == LW_FAILURE )
302  {
303  circ_center_cartesian(&c1, &(c[i]->center), dist, offset1, &new_center);
304  new_radius *= 1.1;
305  }
306  }
307  LWDEBUGF(3, " new center is (%g %g) new radius is %g", new_center.lon, new_center.lat, new_radius);
308  }
309 
310  node = lwalloc(sizeof(CIRC_NODE));
311  node->p1 = NULL;
312  node->p2 = NULL;
313  node->center = new_center;
314  node->radius = new_radius;
315  node->num_nodes = num_nodes;
316  node->nodes = c;
317  node->edge_num = -1;
318  node->geom_type = new_geom_type;
319  node->pt_outside.x = 0.0;
320  node->pt_outside.y = 0.0;
321  return node;
322 }
323 
327 CIRC_NODE*
329 {
330  int num_edges;
331  int i, j;
332  CIRC_NODE **nodes;
333  CIRC_NODE *node;
334  CIRC_NODE *tree;
335 
336  /* Can't do anything with no points */
337  if ( pa->npoints < 1 )
338  return NULL;
339 
340  /* Special handling for a single point */
341  if ( pa->npoints == 1 )
342  return circ_node_leaf_point_new(pa);
343 
344  /* First create a flat list of nodes, one per edge. */
345  num_edges = pa->npoints - 1;
346  nodes = lwalloc(sizeof(CIRC_NODE*) * pa->npoints);
347  j = 0;
348  for ( i = 0; i < num_edges; i++ )
349  {
350  node = circ_node_leaf_new(pa, i);
351  if ( node ) /* Not zero length? */
352  nodes[j++] = node;
353  }
354 
355  /* Special case: only zero-length edges. Make a point node. */
356  if ( j == 0 ) {
357  lwfree(nodes);
358  return circ_node_leaf_point_new(pa);
359  }
360 
361  /* Merge the node list pairwise up into a tree */
362  tree = circ_nodes_merge(nodes, j);
363 
364  /* Free the old list structure, leaving the tree in place */
365  lwfree(nodes);
366 
367  return tree;
368 }
369 
375 static void
376 circ_nodes_sort(CIRC_NODE** nodes, int num_nodes)
377 {
378  qsort(nodes, num_nodes, sizeof(CIRC_NODE*), circ_node_compare);
379 }
380 
381 
382 static CIRC_NODE*
383 circ_nodes_merge(CIRC_NODE** nodes, int num_nodes)
384 {
385  CIRC_NODE **inodes = NULL;
386  int num_children = num_nodes;
387  int inode_num = 0;
388  int num_parents = 0;
389  int j;
390 
391  // TODO, roll geom_type *up* as tree is built, changing to collection types as simple types are merged
392  // TODO, change the distance algorithm to drive down to simple types first, test pip on poly/other cases, then test edges
393 
394  while( num_children > 1 )
395  {
396  for ( j = 0; j < num_children; j++ )
397  {
398  inode_num = (j % CIRC_NODE_SIZE);
399  if ( inode_num == 0 )
400  inodes = lwalloc(sizeof(CIRC_NODE*)*CIRC_NODE_SIZE);
401 
402  inodes[inode_num] = nodes[j];
403 
404  if ( inode_num == CIRC_NODE_SIZE-1 )
405  nodes[num_parents++] = circ_node_internal_new(inodes, CIRC_NODE_SIZE);
406  }
407 
408  /* Clean up any remaining nodes... */
409  if ( inode_num == 0 )
410  {
411  /* Promote solo nodes without merging */
412  nodes[num_parents++] = inodes[0];
413  lwfree(inodes);
414  }
415  else if ( inode_num < CIRC_NODE_SIZE-1 )
416  {
417  /* Merge spare nodes */
418  nodes[num_parents++] = circ_node_internal_new(inodes, inode_num+1);
419  }
420 
421  num_children = num_parents;
422  num_parents = 0;
423  }
424 
425  /* Return a reference to the head of the tree */
426  return nodes[0];
427 }
428 
429 
433 int circ_tree_get_point(const CIRC_NODE* node, POINT2D* pt)
434 {
435  if ( circ_node_is_leaf(node) )
436  {
437  pt->x = node->p1->x;
438  pt->y = node->p1->y;
439  return LW_SUCCESS;
440  }
441  else
442  {
443  return circ_tree_get_point(node->nodes[0], pt);
444  }
445 }
446 
447 
454 int circ_tree_contains_point(const CIRC_NODE* node, const POINT2D* pt, const POINT2D* pt_outside, int* on_boundary)
455 {
456  GEOGRAPHIC_POINT closest;
457  GEOGRAPHIC_EDGE stab_edge, edge;
458  POINT3D S1, S2, E1, E2;
459  double d;
460  int i, c;
461 
462  /* Construct a stabline edge from our "inside" to our known outside point */
463  geographic_point_init(pt->x, pt->y, &(stab_edge.start));
464  geographic_point_init(pt_outside->x, pt_outside->y, &(stab_edge.end));
465  geog2cart(&(stab_edge.start), &S1);
466  geog2cart(&(stab_edge.end), &S2);
467 
468  LWDEBUG(3, "entered");
469 
470  /*
471  * If the stabline doesn't cross within the radius of a node, there's no
472  * way it can cross.
473  */
474 
475  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));
476  d = edge_distance_to_point(&stab_edge, &(node->center), &closest);
477  LWDEBUGF(3, "edge_distance_to_point=%g, node_radius=%g", d, node->radius);
478  if ( FP_LTEQ(d, node->radius) )
479  {
480  LWDEBUGF(3,"entering this branch (%p)", node);
481 
482  /* Return the crossing number of this leaf */
483  if ( circ_node_is_leaf(node) )
484  {
485  int inter;
486  LWDEBUGF(3, "leaf node calculation (edge %d)", node->edge_num);
487  geographic_point_init(node->p1->x, node->p1->y, &(edge.start));
488  geographic_point_init(node->p2->x, node->p2->y, &(edge.end));
489  geog2cart(&(edge.start), &E1);
490  geog2cart(&(edge.end), &E2);
491 
492  inter = edge_intersects(&S1, &S2, &E1, &E2);
493 
494  if ( inter & PIR_INTERSECTS )
495  {
496  LWDEBUG(3," got stab line edge_intersection with this edge!");
497  /* To avoid double counting crossings-at-a-vertex, */
498  /* always ignore crossings at "lower" ends of edges*/
499 
500  if ( inter & PIR_B_TOUCH_RIGHT || inter & PIR_COLINEAR )
501  {
502  LWDEBUG(3," rejecting stab line grazing by left-side edge");
503  return 0;
504  }
505  else
506  {
507  LWDEBUG(3," accepting stab line intersection");
508  return 1;
509  }
510  }
511  }
512  /* Or, add up the crossing numbers of all children of this node. */
513  else
514  {
515  c = 0;
516  for ( i = 0; i < node->num_nodes; i++ )
517  {
518  LWDEBUG(3,"internal node calculation");
519  LWDEBUGF(3," calling circ_tree_contains_point on child %d!", i);
520  c += circ_tree_contains_point(node->nodes[i], pt, pt_outside, on_boundary);
521  }
522  return c % 2;
523  }
524  }
525  else
526  {
527  LWDEBUGF(3,"skipping this branch (%p)", node);
528  }
529 
530  return 0;
531 }
532 
533 static double
535 {
536  double d = sphere_distance(&(n1->center), &(n2->center));
537  double r1 = n1->radius;
538  double r2 = n2->radius;
539 
540  if ( d < r1 + r2 )
541  return 0.0;
542 
543  return d - r1 - r2;
544 }
545 
546 static double
548 {
549  return sphere_distance(&(n1->center), &(n2->center)) + n1->radius + n2->radius;
550 }
551 
552 double
553 circ_tree_distance_tree(const CIRC_NODE* n1, const CIRC_NODE* n2, const SPHEROID* spheroid, double threshold)
554 {
555  double min_dist = MAXFLOAT;
556  double max_dist = MAXFLOAT;
557  GEOGRAPHIC_POINT closest1, closest2;
558  double threshold_radians = threshold / spheroid->radius;
559 
560  circ_tree_distance_tree_internal(n1, n2, threshold_radians, &min_dist, &max_dist, &closest1, &closest2);
561 
562  /* Spherical case */
563  if ( spheroid->a == spheroid->b )
564  {
565  return spheroid->radius * sphere_distance(&closest1, &closest2);
566  }
567  else
568  {
569  return spheroid_distance(&closest1, &closest2, spheroid);
570  }
571 }
572 
573 static double
574 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)
575 {
576  double max;
577  double d, d_min;
578  int i;
579 
580  LWDEBUGF(4, "entered, min_dist=%.8g max_dist=%.8g, type1=%d, type2=%d", *min_dist, *max_dist, n1->geom_type, n2->geom_type);
581 // circ_tree_print(n1, 0);
582 // circ_tree_print(n2, 0);
583 
584  /* Short circuit if we've already hit the minimum */
585  if( *min_dist <= threshold )
586  return *min_dist;
587 
588  /* If your minimum is greater than anyone's maximum, you can't hold the winner */
589  if( circ_node_min_distance(n1, n2) > *max_dist )
590  {
591  LWDEBUGF(4, "pruning pair %p, %p", n1, n2);
592  return MAXFLOAT;
593  }
594 
595  /* If your maximum is a new low, we'll use that as our new global tolerance */
596  max = circ_node_max_distance(n1, n2);
597  LWDEBUGF(5, "max %.8g", max);
598  if( max < *max_dist )
599  *max_dist = max;
600 
601  /* Polygon on one side, primitive type on the other. Check for point-in-polygon */
602  /* short circuit. */
603  if ( n1->geom_type == POLYGONTYPE && n2->geom_type && ! lwtype_is_collection(n2->geom_type) )
604  {
605  POINT2D pt;
606  circ_tree_get_point(n2, &pt);
607  LWDEBUGF(4, "n1 is polygon, testing if contains (%.5g,%.5g)", pt.x, pt.y);
608  if ( circ_tree_contains_point(n1, &pt, &(n1->pt_outside), NULL) )
609  {
610  LWDEBUG(4, "it does");
611  *min_dist = 0.0;
612  geographic_point_init(pt.x, pt.y, closest1);
613  geographic_point_init(pt.x, pt.y, closest2);
614  return *min_dist;
615  }
616  }
617  /* Polygon on one side, primitive type on the other. Check for point-in-polygon */
618  /* short circuit. */
619  if ( n2->geom_type == POLYGONTYPE && n1->geom_type && ! lwtype_is_collection(n1->geom_type) )
620  {
621  POINT2D pt;
622  circ_tree_get_point(n1, &pt);
623  LWDEBUGF(4, "n2 is polygon, testing if contains (%.5g,%.5g)", pt.x, pt.y);
624  if ( circ_tree_contains_point(n2, &pt, &(n2->pt_outside), NULL) )
625  {
626  LWDEBUG(4, "it does");
627  geographic_point_init(pt.x, pt.y, closest1);
628  geographic_point_init(pt.x, pt.y, closest2);
629  *min_dist = 0.0;
630  return *min_dist;
631  }
632  }
633 
634  /* Both leaf nodes, do a real distance calculation */
635  if( circ_node_is_leaf(n1) && circ_node_is_leaf(n2) )
636  {
637  double d;
638  GEOGRAPHIC_POINT close1, close2;
639  LWDEBUGF(4, "testing leaf pair [%d], [%d]", n1->edge_num, n2->edge_num);
640  /* One of the nodes is a point */
641  if ( n1->p1 == n1->p2 || n2->p1 == n2->p2 )
642  {
643  GEOGRAPHIC_EDGE e;
644  GEOGRAPHIC_POINT gp1, gp2;
645 
646  /* Both nodes are points! */
647  if ( n1->p1 == n1->p2 && n2->p1 == n2->p2 )
648  {
649  geographic_point_init(n1->p1->x, n1->p1->y, &gp1);
650  geographic_point_init(n2->p1->x, n2->p1->y, &gp2);
651  close1 = gp1; close2 = gp2;
652  d = sphere_distance(&gp1, &gp2);
653  }
654  /* Node 1 is a point */
655  else if ( n1->p1 == n1->p2 )
656  {
657  geographic_point_init(n1->p1->x, n1->p1->y, &gp1);
658  geographic_point_init(n2->p1->x, n2->p1->y, &(e.start));
659  geographic_point_init(n2->p2->x, n2->p2->y, &(e.end));
660  close1 = gp1;
661  d = edge_distance_to_point(&e, &gp1, &close2);
662  }
663  /* Node 2 is a point */
664  else
665  {
666  geographic_point_init(n2->p1->x, n2->p1->y, &gp1);
667  geographic_point_init(n1->p1->x, n1->p1->y, &(e.start));
668  geographic_point_init(n1->p2->x, n1->p2->y, &(e.end));
669  close1 = gp1;
670  d = edge_distance_to_point(&e, &gp1, &close2);
671  }
672  LWDEBUGF(4, " got distance %g", d);
673  }
674  /* Both nodes are edges */
675  else
676  {
677  GEOGRAPHIC_EDGE e1, e2;
679  POINT3D A1, A2, B1, B2;
680  geographic_point_init(n1->p1->x, n1->p1->y, &(e1.start));
681  geographic_point_init(n1->p2->x, n1->p2->y, &(e1.end));
682  geographic_point_init(n2->p1->x, n2->p1->y, &(e2.start));
683  geographic_point_init(n2->p2->x, n2->p2->y, &(e2.end));
684  geog2cart(&(e1.start), &A1);
685  geog2cart(&(e1.end), &A2);
686  geog2cart(&(e2.start), &B1);
687  geog2cart(&(e2.end), &B2);
688  if ( edge_intersects(&A1, &A2, &B1, &B2) )
689  {
690  d = 0.0;
691  edge_intersection(&e1, &e2, &g);
692  close1 = close2 = g;
693  }
694  else
695  {
696  d = edge_distance_to_edge(&e1, &e2, &close1, &close2);
697  }
698  LWDEBUGF(4, "edge_distance_to_edge returned %g", d);
699  }
700  if ( d < *min_dist )
701  {
702  *min_dist = d;
703  *closest1 = close1;
704  *closest2 = close2;
705  }
706  return d;
707  }
708  else
709  {
710  d_min = MAXFLOAT;
711  /* Drive the recursion into the COLLECTION types first so we end up with */
712  /* pairings of primitive geometries that can be forced into the point-in-polygon */
713  /* tests above. */
714  if ( n1->geom_type && lwtype_is_collection(n1->geom_type) )
715  {
716  for ( i = 0; i < n1->num_nodes; i++ )
717  {
718  d = circ_tree_distance_tree_internal(n1->nodes[i], n2, threshold, min_dist, max_dist, closest1, closest2);
719  d_min = FP_MIN(d_min, d);
720  }
721  }
722  else if ( n2->geom_type && lwtype_is_collection(n2->geom_type) )
723  {
724  for ( i = 0; i < n2->num_nodes; i++ )
725  {
726  d = circ_tree_distance_tree_internal(n1, n2->nodes[i], threshold, min_dist, max_dist, closest1, closest2);
727  d_min = FP_MIN(d_min, d);
728  }
729  }
730  else if ( ! circ_node_is_leaf(n1) )
731  {
732  for ( i = 0; i < n1->num_nodes; i++ )
733  {
734  d = circ_tree_distance_tree_internal(n1->nodes[i], n2, threshold, min_dist, max_dist, closest1, closest2);
735  d_min = FP_MIN(d_min, d);
736  }
737  }
738  else if ( ! circ_node_is_leaf(n2) )
739  {
740  for ( i = 0; i < n2->num_nodes; i++ )
741  {
742  d = circ_tree_distance_tree_internal(n1, n2->nodes[i], threshold, min_dist, max_dist, closest1, closest2);
743  d_min = FP_MIN(d_min, d);
744  }
745  }
746  else
747  {
748  /* Never get here */
749  }
750 
751  return d_min;
752  }
753 }
754 
755 
756 
757 
758 
759 void circ_tree_print(const CIRC_NODE* node, int depth)
760 {
761  int i;
762 
763  if (circ_node_is_leaf(node))
764  {
765  printf("%*s[%d] C(%.5g %.5g) R(%.5g) ((%.5g %.5g),(%.5g,%.5g))",
766  3*depth + 6, "NODE", node->edge_num,
767  node->center.lon, node->center.lat,
768  node->radius,
769  node->p1->x, node->p1->y,
770  node->p2->x, node->p2->y
771  );
772  if ( node->geom_type )
773  {
774  printf(" %s", lwtype_name(node->geom_type));
775  }
776  if ( node->geom_type == POLYGONTYPE )
777  {
778  printf(" O(%.5g %.5g)", node->pt_outside.x, node->pt_outside.y);
779  }
780  printf("\n");
781 
782  }
783  else
784  {
785  printf("%*s C(%.5g %.5g) R(%.5g)",
786  3*depth + 6, "NODE",
787  node->center.lon, node->center.lat,
788  node->radius
789  );
790  if ( node->geom_type )
791  {
792  printf(" %s", lwtype_name(node->geom_type));
793  }
794  if ( node->geom_type == POLYGONTYPE )
795  {
796  printf(" O(%.5g %.5g)", node->pt_outside.x, node->pt_outside.y);
797  }
798  printf("\n");
799  }
800  for ( i = 0; i < node->num_nodes; i++ )
801  {
802  circ_tree_print(node->nodes[i], depth + 1);
803  }
804  return;
805 }
806 
807 
808 static CIRC_NODE*
810 {
811  CIRC_NODE* node;
812  node = circ_tree_new(lwpoint->point);
813  node->geom_type = lwgeom_get_type((LWGEOM*)lwpoint);;
814  return node;
815 }
816 
817 static CIRC_NODE*
819 {
820  CIRC_NODE* node;
821  node = circ_tree_new(lwline->points);
822  node->geom_type = lwgeom_get_type((LWGEOM*)lwline);
823  return node;
824 }
825 
826 static CIRC_NODE*
828 {
829  int i = 0, j = 0;
830  CIRC_NODE** nodes;
831  CIRC_NODE* node;
832 
833  /* One ring? Handle it like a line. */
834  if ( lwpoly->nrings == 1 )
835  {
836  node = circ_tree_new(lwpoly->rings[0]);
837  }
838  else
839  {
840  /* Calculate a tree for each non-trivial ring of the polygon */
841  nodes = lwalloc(lwpoly->nrings * sizeof(CIRC_NODE*));
842  for ( i = 0; i < lwpoly->nrings; i++ )
843  {
844  node = circ_tree_new(lwpoly->rings[i]);
845  if ( node )
846  nodes[j++] = node;
847  }
848  /* Put the trees into a spatially correlated order */
849  circ_nodes_sort(nodes, j);
850  /* Merge the trees pairwise up to a parent node and return */
851  node = circ_nodes_merge(nodes, j);
852  /* Don't need the working list any more */
853  lwfree(nodes);
854  }
855 
856  /* Metatdata about polygons, we need this to apply P-i-P tests */
857  /* selectively when doing distance calculations */
858  node->geom_type = lwgeom_get_type((LWGEOM*)lwpoly);
859  lwpoly_pt_outside(lwpoly, &(node->pt_outside));
860 
861  return node;
862 }
863 
864 static CIRC_NODE*
866 {
867  int i = 0, j = 0;
868  CIRC_NODE** nodes;
869  CIRC_NODE* node;
870 
871  /* One geometry? Done! */
872  if ( lwcol->ngeoms == 1 )
873  return lwgeom_calculate_circ_tree(lwcol->geoms[0]);
874 
875  /* Calculate a tree for each sub-geometry*/
876  nodes = lwalloc(lwcol->ngeoms * sizeof(CIRC_NODE*));
877  for ( i = 0; i < lwcol->ngeoms; i++ )
878  {
879  node = lwgeom_calculate_circ_tree(lwcol->geoms[i]);
880  if ( node )
881  nodes[j++] = node;
882  }
883  /* Put the trees into a spatially correlated order */
884  circ_nodes_sort(nodes, j);
885  /* Merge the trees pairwise up to a parent node and return */
886  node = circ_nodes_merge(nodes, j);
887  /* Don't need the working list any more */
888  lwfree(nodes);
889 
890  node->geom_type = lwgeom_get_type((LWGEOM*)lwcol);
891 
892  return node;
893 }
894 
895 CIRC_NODE*
897 {
898  if ( lwgeom_is_empty(lwgeom) )
899  return NULL;
900 
901  switch ( lwgeom->type )
902  {
903  case POINTTYPE:
904  return lwpoint_calculate_circ_tree((LWPOINT*)lwgeom);
905  case LINETYPE:
906  return lwline_calculate_circ_tree((LWLINE*)lwgeom);
907  case POLYGONTYPE:
908  return lwpoly_calculate_circ_tree((LWPOLY*)lwgeom);
909  case MULTIPOINTTYPE:
910  case MULTILINETYPE:
911  case MULTIPOLYGONTYPE:
912  case COLLECTIONTYPE:
914  default:
915  lwerror("Unable to calculate spherical index tree for type %s", lwtype_name(lwgeom->type));
916  return NULL;
917  }
918 
919 }
#define LINETYPE
Definition: liblwgeom.h:61
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:897
static CIRC_NODE * lwpoint_calculate_circ_tree(const LWPOINT *lwpoint)
#define CIRC_NODE_SIZE
Note that p1 and p2 are pointers into an independent POINTARRAY, do not free them.
int sphere_project(const GEOGRAPHIC_POINT *r, double distance, double azimuth, GEOGRAPHIC_POINT *n)
Given a starting location r, a distance and an azimuth to the new point, compute the location of the ...
Definition: lwgeodetic.c:1261
int lwtype_get_collectiontype(uint8_t type)
Given an lwtype number, what homogeneous collection can hold it?
Definition: lwgeom.c:982
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:1216
Two-point great circle segment from a to b.
Definition: lwgeodetic.h:42
#define PIR_B_TOUCH_RIGHT
Definition: lwgeodetic.h:77
GEOGRAPHIC_POINT center
void normalize(POINT3D *p)
Normalize to a unit vector.
Definition: lwgeodetic.c:564
void lwfree(void *mem)
Definition: lwutil.c:190
double y
Definition: liblwgeom.h:296
int circ_tree_get_point(const CIRC_NODE *node, POINT2D *pt)
Returns a POINT2D that is a vertex of the input shape.
int npoints
Definition: liblwgeom.h:327
POINT2D * p2
uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.
Definition: lwgeom.c:785
#define POLYGONTYPE
Definition: liblwgeom.h:62
double b
Definition: liblwgeom.h:270
static int circ_node_compare(const void *v1, const void *v2)
Comparing on geohash ensures that nearby nodes will be close to each other in the list...
POINT2D * p1
#define MULTIPOINTTYPE
Definition: liblwgeom.h:63
#define LW_SUCCESS
Definition: liblwgeom.h:55
void cart2geog(const POINT3D *p, GEOGRAPHIC_POINT *g)
Convert cartesion coordinates on unit sphere to spherical coordinates.
Definition: lwgeodetic.c:365
double radius
Definition: liblwgeom.h:274
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:50
#define FP_LTEQ(A, B)
static int circ_center_spherical(const GEOGRAPHIC_POINT *c1, const GEOGRAPHIC_POINT *c2, double distance, double offset, GEOGRAPHIC_POINT *center)
Given the centers of two circles, and the offset distance we want to put the new center between them ...
double x
Definition: liblwgeom.h:296
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 CIRC_NODE * circ_node_leaf_new(const POINTARRAY *pa, int i)
Create a new leaf node, storing pointers back to the end points for later.
double circ_tree_distance_tree(const CIRC_NODE *n1, const CIRC_NODE *n2, const SPHEROID *spheroid, double threshold)
#define FP_MIN(A, B)
Point in spherical coordinates on the world.
Definition: lwgeodetic.h:33
POINTARRAY * point
Definition: liblwgeom.h:367
CIRC_NODE * lwgeom_calculate_circ_tree(const LWGEOM *lwgeom)
#define LW_FAILURE
Definition: liblwgeom.h:54
double z
Definition: liblwgeom.h:296
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:67
double x
Definition: liblwgeom.h:284
static int circ_node_is_leaf(const CIRC_NODE *node)
Internal nodes have their point references set to NULL.
static int circ_center_cartesian(const GEOGRAPHIC_POINT *c1, const GEOGRAPHIC_POINT *c2, double distance, double offset, GEOGRAPHIC_POINT *center)
Where the circ_center_spherical() function fails, we need a fall-back.
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:164
CIRC_NODE * circ_tree_new(const POINTARRAY *pa)
Build a tree of nodes from a point array, one node per edge.
static double circ_node_max_distance(const CIRC_NODE *n1, const CIRC_NODE *n2)
#define rad2deg(r)
Definition: lwgeodetic.h:61
GEOGRAPHIC_POINT start
Definition: lwgeodetic.h:44
double edge_distance_to_point(const GEOGRAPHIC_EDGE *e, const GEOGRAPHIC_POINT *gp, GEOGRAPHIC_POINT *closest)
Definition: lwgeodetic.c:1165
LWGEOM ** geoms
Definition: liblwgeom.h:465
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.
uint8_t * getPoint_internal(const POINTARRAY *pa, int n)
Definition: ptarray.c:1645
void circ_tree_print(const CIRC_NODE *node, int depth)
POINTARRAY ** rings
Definition: liblwgeom.h:413
int lwtype_is_collection(uint8_t type)
Determine whether a type number is a collection or not.
Definition: lwgeom.c:955
void lwpoly_pt_outside(const LWPOLY *poly, POINT2D *pt_outside)
Definition: lwgeodetic.c:1422
static CIRC_NODE * circ_nodes_merge(CIRC_NODE **nodes, int num_nodes)
int nrings
Definition: liblwgeom.h:411
GEOGRAPHIC_POINT end
Definition: lwgeodetic.h:45
double y
Definition: liblwgeom.h:284
static void circ_nodes_sort(CIRC_NODE **nodes, int num_nodes)
Given a list of nodes, sort them into a spatially consistent order, then pairwise merge them up into ...
Datum distance(PG_FUNCTION_ARGS)
unsigned int geohash_point_as_int(POINT2D *pt)
Definition: lwalgorithm.c:648
void geog2cart(const GEOGRAPHIC_POINT *g, POINT3D *p)
Convert spherical coordinates to cartesion coordinates on unit sphere.
Definition: lwgeodetic.c:355
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:65
void vector_sum(const POINT3D *a, const POINT3D *b, POINT3D *n)
Calculate the sum of two vectors.
Definition: lwgeodetic.c:416
void circ_tree_free(CIRC_NODE *node)
Recurse from top of node tree and free all children.
static CIRC_NODE * lwline_calculate_circ_tree(const LWLINE *lwline)
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:3096
static double circ_node_min_distance(const CIRC_NODE *n1, const CIRC_NODE *n2)
double a
Definition: liblwgeom.h:269
void geographic_point_init(double lon, double lat, GEOGRAPHIC_POINT *g)
Initialize a geographic point.
Definition: lwgeodetic.c:157
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:60
static CIRC_NODE * circ_node_leaf_point_new(const POINTARRAY *pa)
Return a point node (zero radius, referencing one point)
POINT2D pt_outside
#define MAXFLOAT
Largest float value.
uint8_t type
Definition: liblwgeom.h:352
#define FP_EQUALS(A, B)
static CIRC_NODE * lwpoly_calculate_circ_tree(const LWPOLY *lwpoly)
static CIRC_NODE * lwcollection_calculate_circ_tree(const LWCOLLECTION *lwcol)
double radius
double spheroid_distance(const GEOGRAPHIC_POINT *a, const GEOGRAPHIC_POINT *b, const SPHEROID *spheroid)
Computes the shortest distance along the surface of the spheroid between two points.
Definition: lwspheroid.c:59
void * lwalloc(size_t size)
Definition: lwutil.c:175
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
Definition: lwgeom.c:1229
#define MULTILINETYPE
Definition: liblwgeom.h:64
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:55
#define PIR_COLINEAR
Definition: lwgeodetic.h:74
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:1074
#define COLLECTIONTYPE
Definition: liblwgeom.h:66
struct circ_node ** nodes
#define PIR_INTERSECTS
Definition: lwgeodetic.h:73
static CIRC_NODE * circ_node_internal_new(CIRC_NODE **c, int num_nodes)
Create a new internal node, calculating the new measure range for the node, and storing pointers to t...
POINTARRAY * points
Definition: liblwgeom.h:378
double sphere_direction(const GEOGRAPHIC_POINT *s, const GEOGRAPHIC_POINT *e, double d)
Given two points on a unit sphere, calculate the direction from s to e.
Definition: lwgeodetic.c:924