PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ circ_node_internal_new()

static CIRC_NODE * circ_node_internal_new ( CIRC_NODE **  c,
uint32_t  num_nodes 
)
static

Create a new internal node, calculating the new measure range for the node, and storing pointers to the child nodes.

Definition at line 230 of file lwgeodetic_tree.c.

231{
232 CIRC_NODE *node = NULL;
233 GEOGRAPHIC_POINT new_center, c1;
234 double new_radius;
235 double offset1, dist, D, r1, ri;
236 uint32_t i, new_geom_type;
237
238 LWDEBUGF(3, "called with %d nodes --", num_nodes);
239
240 /* Can't do anything w/ empty input */
241 if ( num_nodes < 1 )
242 return node;
243
244 /* Initialize calculation with values of the first circle */
245 new_center = c[0]->center;
246 new_radius = c[0]->radius;
247 new_geom_type = c[0]->geom_type;
248
249 /* Merge each remaining circle into the new circle */
250 for ( i = 1; i < num_nodes; i++ )
251 {
252 c1 = new_center;
253 r1 = new_radius;
254
255 dist = sphere_distance(&c1, &(c[i]->center));
256 ri = c[i]->radius;
257
258 /* Promote geometry types up the tree, getting more and more collected */
259 /* Go until we find a value */
260 if ( ! new_geom_type )
261 {
262 new_geom_type = c[i]->geom_type;
263 }
264 /* Promote singleton to a multi-type */
265 else if ( ! lwtype_is_collection(new_geom_type) )
266 {
267 /* Anonymous collection if types differ */
268 if ( new_geom_type != c[i]->geom_type )
269 {
270 new_geom_type = COLLECTIONTYPE;
271 }
272 else
273 {
274 new_geom_type = lwtype_get_collectiontype(new_geom_type);
275 }
276 }
277 /* If we can't add next feature to this collection cleanly, promote again to anonymous collection */
278 else if ( new_geom_type != lwtype_get_collectiontype(c[i]->geom_type) )
279 {
280 new_geom_type = COLLECTIONTYPE;
281 }
282
283
284 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);
285
286 if ( FP_EQUALS(dist, 0) )
287 {
288 LWDEBUG(3, " distance between centers is zero");
289 new_radius = r1 + 2*dist;
290 new_center = c1;
291 }
292 else if ( dist < fabs(r1 - ri) )
293 {
294 /* new contains next */
295 if ( r1 > ri )
296 {
297 LWDEBUG(3, " c1 contains ci");
298 new_center = c1;
299 new_radius = r1;
300 }
301 /* next contains new */
302 else
303 {
304 LWDEBUG(3, " ci contains c1");
305 new_center = c[i]->center;
306 new_radius = ri;
307 }
308 }
309 else
310 {
311 LWDEBUG(3, " calculating new center");
312 /* New circle diameter */
313 D = dist + r1 + ri;
314 LWDEBUGF(3," D is %g", D);
315
316 /* New radius */
317 new_radius = D / 2.0;
318
319 /* Distance from cn1 center to the new center */
320 offset1 = ri + (D - (2.0*r1 + 2.0*ri)) / 2.0;
321 LWDEBUGF(3," offset1 is %g", offset1);
322
323 /* Sometimes the sphere_direction function fails... this causes the center calculation */
324 /* to fail too. In that case, we're going to fall back to a cartesian calculation, which */
325 /* is less exact, so we also have to pad the radius by (hack alert) an arbitrary amount */
326 /* which is hopefully always big enough to contain the input edges */
327 if ( circ_center_spherical(&c1, &(c[i]->center), dist, offset1, &new_center) == LW_FAILURE )
328 {
329 circ_center_cartesian(&c1, &(c[i]->center), dist, offset1, &new_center);
330 new_radius *= 1.1;
331 }
332 }
333 LWDEBUGF(3, " new center is (%g %g) new radius is %g", new_center.lon, new_center.lat, new_radius);
334 }
335
336 node = lwalloc(sizeof(CIRC_NODE));
337 node->p1 = NULL;
338 node->p2 = NULL;
339 node->center = new_center;
340 node->radius = new_radius;
341 node->num_nodes = num_nodes;
342 node->nodes = c;
343 node->edge_num = -1;
344 node->geom_type = new_geom_type;
345 node->pt_outside.x = 0.0;
346 node->pt_outside.y = 0.0;
347 return node;
348}
uint32_t lwtype_get_collectiontype(uint8_t type)
Given an lwtype number, what homogeneous collection can hold it?
Definition lwgeom.c:1222
#define COLLECTIONTYPE
Definition liblwgeom.h:108
#define LW_FAILURE
Definition liblwgeom.h:96
int lwtype_is_collection(uint8_t type)
Determine whether a type number is a collection or not.
Definition lwgeom.c:1196
void * lwalloc(size_t size)
Definition lwutil.c:227
#define FP_EQUALS(A, B)
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:896
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_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.
#define LWDEBUG(level, msg)
Definition lwgeom_log.h:101
#define LWDEBUGF(level, msg,...)
Definition lwgeom_log.h:106
Point in spherical coordinates on the world.
Definition lwgeodetic.h:54
double y
Definition liblwgeom.h:390
double x
Definition liblwgeom.h:390
uint32_t num_nodes
POINT2D * p2
struct circ_node ** nodes
POINT2D * p1
POINT2D pt_outside
GEOGRAPHIC_POINT center
uint32_t geom_type
Note that p1 and p2 are pointers into an independent POINTARRAY, do not free them.

References circ_node::center, circ_center_cartesian(), circ_center_spherical(), COLLECTIONTYPE, circ_node::edge_num, FP_EQUALS, circ_node::geom_type, GEOGRAPHIC_POINT::lat, GEOGRAPHIC_POINT::lon, LW_FAILURE, lwalloc(), LWDEBUG, LWDEBUGF, lwtype_get_collectiontype(), lwtype_is_collection(), circ_node::nodes, circ_node::num_nodes, circ_node::p1, circ_node::p2, circ_node::pt_outside, circ_node::radius, sphere_distance(), POINT2D::x, and POINT2D::y.

Referenced by circ_nodes_merge().

Here is the call graph for this function:
Here is the caller graph for this function: