82 LWDEBUGF(3,
"edge #%d (%g %g, %g %g)", i, p1->
x, p1->
y, p2->
x, p2->
y);
102 node->
radius = diameter / 2.0;
155 if ( u1 < u2 )
return -1;
156 if ( u1 > u2 )
return 1;
172 LWDEBUGF(4,
"calculating spherical center", dir);
196 double proportion = offset/
distance;
198 LWDEBUG(4,
"calculating cartesian center");
204 p1p2.
x = p2.
x - p1.
x;
205 p1p2.
y = p2.
y - p1.
y;
206 p1p2.
z = p2.
z - p1.
z;
209 p1p2.
x *= proportion;
210 p1p2.
y *= proportion;
211 p1p2.
z *= proportion;
214 pc.
x = p1.
x + p1p2.
x;
215 pc.
y = p1.
y + p1p2.
y;
216 pc.
z = p1.
z + p1p2.
z;
236 double offset1, dist, D, r1, ri;
239 LWDEBUGF(3,
"called with %d nodes --", num_nodes);
246 new_center = c[0]->
center;
247 new_radius = c[0]->
radius;
251 for ( i = 1; i < num_nodes; i++ )
261 if ( ! new_geom_type )
269 if ( new_geom_type != c[i]->geom_type )
289 LWDEBUG(3,
" distance between centers is zero");
290 new_radius = r1 + 2*dist;
293 else if ( dist < fabs(r1 - ri) )
306 new_center = c[i]->
center;
312 LWDEBUG(3,
" calculating new center");
318 new_radius = D / 2.0;
321 offset1 = ri + (D - (2.0*r1 + 2.0*ri)) / 2.0;
322 LWDEBUGF(3,
" offset1 is %g", offset1);
334 LWDEBUGF(3,
" new center is (%g %g) new radius is %g", new_center.
lon, new_center.
lat, new_radius);
340 node->
center = new_center;
341 node->
radius = new_radius;
375 for ( i = 0; i < num_edges; i++ )
413 int num_children = num_nodes;
422 while( num_children > 1 )
424 for ( j = 0; j < num_children; j++ )
427 if ( inode_num == 0 )
430 inodes[inode_num] = nodes[j];
437 if ( inode_num == 0 )
440 nodes[num_parents++] = inodes[0];
449 num_children = num_parents;
518 LWDEBUGF(3,
"edge_distance_to_point=%g, node_radius=%g", d, node->
radius);
521 LWDEBUGF(3,
"entering this branch (%p)", node);
537 LWDEBUG(3,
" got stab line edge_intersection with this edge!");
543 LWDEBUG(3,
" rejecting stab line grazing by left-side edge");
548 LWDEBUG(3,
" accepting stab line intersection");
559 LWDEBUG(3,
"internal node calculation");
560 LWDEBUGF(3,
" calling circ_tree_contains_point on child %d!", i);
568 LWDEBUGF(3,
"skipping this branch (%p)", node);
596 double min_dist = FLT_MAX;
597 double max_dist = FLT_MAX;
602 double threshold_radians = 0.95 * threshold / spheroid->
radius;
607 if ( spheroid->
a == spheroid->
b )
632 if (node_a->
d < node_b->
d)
return -1;
633 else if (node_a->
d > node_b->
d)
return 1;
645 for (i = 0; i < num_nodes; i++)
647 sort_nodes[i].
node = nodes[i];
653 for (i = 0; i < num_nodes; i++)
655 nodes[i] = sort_nodes[i].
node;
676 if( *min_dist < threshold || *min_dist == 0.0 )
682 LWDEBUGF(4,
"pruning pair %p, %p", n1, n2);
689 if( max < *max_dist )
698 LWDEBUGF(4,
"n1 is polygon, testing if contains (%.5g,%.5g)", pt.
x, pt.
y);
714 LWDEBUGF(4,
"n2 is polygon, testing if contains (%.5g,%.5g)", pt.
x, pt.
y);
732 if ( n1->
p1 == n1->
p2 || n2->
p1 == n2->
p2 )
738 if ( n1->
p1 == n1->
p2 && n2->
p1 == n2->
p2 )
742 close1 = gp1; close2 = gp2;
746 else if ( n1->
p1 == n1->
p2 )
789 LWDEBUGF(4,
"edge_distance_to_edge returned %g",
d);
860 printf(
"%*s[%d] C(%.5g %.5g) R(%.5g) ((%.5g %.5g),(%.5g,%.5g))",
880 printf(
"%*s C(%.5g %.5g) R(%.5g)",
929 if ( lwpoly->
nrings == 1 )
937 for ( i = 0; i < lwpoly->
nrings; i++ )
972 for ( i = 0; i < lwcol->
ngeoms; i++ )
994 switch ( lwgeom->
type )
uint32_t lwtype_get_collectiontype(uint8_t type)
Given an lwtype number, what homogeneous collection can hold it?
uint8_t * getPoint_internal(const POINTARRAY *pa, uint32_t n)
unsigned int geohash_point_as_int(POINT2D *pt)
uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.
int lwtype_is_collection(uint8_t type)
Determine whether a type number is a collection or not.
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
void * lwalloc(size_t size)
void normalize(POINT3D *p)
Normalize to a unit vector.
void vector_scale(POINT3D *n, double scale)
Scale a vector out by a factor.
void cart2geog(const POINT3D *p, GEOGRAPHIC_POINT *g)
Convert cartesian coordinates on unit sphere to spherical coordinates.
int lwpoly_pt_outside(const LWPOLY *poly, POINT2D *pt_outside)
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 ...
void geographic_point_init(double lon, double lat, GEOGRAPHIC_POINT *g)
Initialize a geographic point.
double sphere_distance(const GEOGRAPHIC_POINT *s, const GEOGRAPHIC_POINT *e)
Given two points on a unit sphere, calculate their distance apart in radians.
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.
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.
void vector_sum(const POINT3D *a, const POINT3D *b, POINT3D *n)
Calculate the sum of two vectors.
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.
double edge_distance_to_point(const GEOGRAPHIC_EDGE *e, const GEOGRAPHIC_POINT *gp, GEOGRAPHIC_POINT *closest)
void geog2cart(const GEOGRAPHIC_POINT *g, POINT3D *p)
Convert spherical coordinates to cartesian coordinates on unit sphere.
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.
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.
#define PIR_B_TOUCH_RIGHT
double circ_tree_distance_tree(const CIRC_NODE *n1, const CIRC_NODE *n2, const SPHEROID *spheroid, double threshold)
static CIRC_NODE * lwpoly_calculate_circ_tree(const LWPOLY *lwpoly)
CIRC_NODE * circ_tree_new(const POINTARRAY *pa)
Build a tree of nodes from a point array, one node per edge.
int circ_tree_get_point(const CIRC_NODE *node, POINT2D *pt)
Returns a POINT2D that is a vertex of the input shape.
static CIRC_NODE * lwcollection_calculate_circ_tree(const LWCOLLECTION *lwcol)
static double circ_node_max_distance(const CIRC_NODE *n1, const CIRC_NODE *n2)
static int circ_nodes_sort_cmp(const void *a, const void *b)
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 * lwpoint_calculate_circ_tree(const LWPOINT *lwpoint)
static CIRC_NODE * lwline_calculate_circ_tree(const LWLINE *lwline)
void circ_tree_print(const CIRC_NODE *node, int depth)
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.
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 ...
static int circ_node_is_leaf(const CIRC_NODE *node)
Internal nodes have their point references set to NULL.
void circ_tree_free(CIRC_NODE *node)
Recurse from top of node tree and free all children.
static void circ_internal_nodes_sort(CIRC_NODE **nodes, uint32_t num_nodes, const CIRC_NODE *target_node)
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.
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.
CIRC_NODE * lwgeom_calculate_circ_tree(const LWGEOM *lwgeom)
int circ_tree_get_point_outside(const CIRC_NODE *node, POINT2D *pt)
static CIRC_NODE * circ_nodes_merge(CIRC_NODE **nodes, int num_nodes)
static double circ_node_min_distance(const CIRC_NODE *n1, const CIRC_NODE *n2)
static CIRC_NODE * circ_node_leaf_point_new(const POINTARRAY *pa)
Return a point node (zero radius, referencing one point)
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.
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 ...
static CIRC_NODE * circ_node_internal_new(CIRC_NODE **c, uint32_t num_nodes)
Create a new internal node, calculating the new measure range for the node, and storing pointers to t...
Datum distance(PG_FUNCTION_ARGS)
#define LWDEBUG(level, msg)
#define LWDEBUGF(level, msg,...)
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Two-point great circle segment from a to b.
Point in spherical coordinates on the world.
struct circ_node ** nodes
Note that p1 and p2 are pointers into an independent POINTARRAY, do not free them.