PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ lwgeom_subdivide_recursive()

static void lwgeom_subdivide_recursive ( const LWGEOM geom,
uint8_t  dimension,
uint32_t  maxvertices,
uint32_t  depth,
LWCOLLECTION col,
double  gridSize 
)
static

Definition at line 2307 of file lwgeom.c.

2313 {
2314  const uint32_t maxdepth = 50;
2315 
2316  if (!geom)
2317  return;
2318 
2319  const GBOX *box_in = lwgeom_get_bbox(geom);
2320  if (!box_in)
2321  return;
2322 
2323  LW_ON_INTERRUPT(return);
2324 
2325  GBOX clip;
2326  gbox_duplicate(box_in, &clip);
2327  double width = clip.xmax - clip.xmin;
2328  double height = clip.ymax - clip.ymin;
2329 
2330  if ( geom->type == POLYHEDRALSURFACETYPE || geom->type == TINTYPE )
2331  lwerror("%s: unsupported geometry type '%s'", __func__, lwtype_name(geom->type));
2332 
2333  if ( width == 0.0 && height == 0.0 )
2334  {
2335  if ( geom->type == POINTTYPE && dimension == 0)
2337  return;
2338  }
2339 
2340  if (width == 0.0)
2341  {
2342  clip.xmax += FP_TOLERANCE;
2343  clip.xmin -= FP_TOLERANCE;
2344  width = 2 * FP_TOLERANCE;
2345  }
2346  if (height == 0.0)
2347  {
2348  clip.ymax += FP_TOLERANCE;
2349  clip.ymin -= FP_TOLERANCE;
2350  height = 2 * FP_TOLERANCE;
2351  }
2352 
2353  /* Always just recurse into collections */
2354  if ( lwgeom_is_collection(geom) && geom->type != MULTIPOINTTYPE )
2355  {
2356  LWCOLLECTION *incol = (LWCOLLECTION*)geom;
2357  /* Don't increment depth yet, since we aren't actually
2358  * subdividing geometries yet */
2359  for (uint32_t i = 0; i < incol->ngeoms; i++ )
2360  lwgeom_subdivide_recursive(incol->geoms[i], dimension, maxvertices, depth, col, gridSize);
2361  return;
2362  }
2363 
2364  if (lwgeom_dimension(geom) < dimension)
2365  {
2366  /* We've hit a lower dimension object produced by clipping at
2367  * a shallower recursion level. Ignore it. */
2368  return;
2369  }
2370 
2371  /* But don't go too far. 2^50 ~= 10^15, that's enough subdivision */
2372  /* Just add what's left */
2373  if ( depth > maxdepth )
2374  {
2376  return;
2377  }
2378 
2379  uint32_t nvertices = lwgeom_count_vertices(geom);
2380 
2381  /* Skip empties entirely */
2382  if (nvertices == 0)
2383  return;
2384 
2385  /* If it is under the vertex tolerance, just add it, we're done */
2386  if (nvertices <= maxvertices)
2387  {
2389  return;
2390  }
2391 
2392  uint8_t split_ordinate = (width > height) ? 0 : 1;
2393  double center = (split_ordinate == 0) ? (clip.xmin + clip.xmax) / 2 : (clip.ymin + clip.ymax) / 2;
2394  double pivot = DBL_MAX;
2395  if (geom->type == POLYGONTYPE)
2396  {
2397  uint32_t ring_to_trim = 0;
2398  double ring_area = 0;
2399  double pivot_eps = DBL_MAX;
2400  double pt_eps = DBL_MAX;
2401  POINTARRAY *pa;
2402  LWPOLY *lwpoly = (LWPOLY *)geom;
2403 
2404  /* if there are more points in holes than in outer ring */
2405  if (nvertices >= 2 * lwpoly->rings[0]->npoints)
2406  {
2407  /* trim holes starting from biggest */
2408  for (uint32_t i = 1; i < lwpoly->nrings; i++)
2409  {
2410  double current_ring_area = fabs(ptarray_signed_area(lwpoly->rings[i]));
2411  if (current_ring_area >= ring_area)
2412  {
2413  ring_area = current_ring_area;
2414  ring_to_trim = i;
2415  }
2416  }
2417  }
2418 
2419  pa = lwpoly->rings[ring_to_trim];
2420 
2421  /* find most central point in chosen ring */
2422  for (uint32_t i = 0; i < pa->npoints; i++)
2423  {
2424  double pt;
2425  if (split_ordinate == 0)
2426  pt = getPoint2d_cp(pa, i)->x;
2427  else
2428  pt = getPoint2d_cp(pa, i)->y;
2429  pt_eps = fabs(pt - center);
2430  if (pivot_eps > pt_eps)
2431  {
2432  pivot = pt;
2433  pivot_eps = pt_eps;
2434  }
2435  }
2436  }
2437  GBOX subbox1, subbox2;
2438  gbox_duplicate(&clip, &subbox1);
2439  gbox_duplicate(&clip, &subbox2);
2440 
2441  if (pivot == DBL_MAX)
2442  pivot = center;
2443 
2444  if (split_ordinate == 0)
2445  {
2446  if (FP_NEQUALS(subbox1.xmax, pivot) && FP_NEQUALS(subbox1.xmin, pivot))
2447  subbox1.xmax = subbox2.xmin = pivot;
2448  else
2449  subbox1.xmax = subbox2.xmin = center;
2450  }
2451  else
2452  {
2453  if (FP_NEQUALS(subbox1.ymax, pivot) && FP_NEQUALS(subbox1.ymin, pivot))
2454  subbox1.ymax = subbox2.ymin = pivot;
2455  else
2456  subbox1.ymax = subbox2.ymin = center;
2457  }
2458 
2459  ++depth;
2460 
2461  {
2463  geom->srid, subbox1.xmin, subbox1.ymin, subbox1.xmax, subbox1.ymax);
2464  LWGEOM *clipped = lwgeom_intersection_prec(geom, subbox, gridSize);
2465  lwgeom_simplify_in_place(clipped, 0.0, LW_TRUE);
2466  lwgeom_free(subbox);
2467  if (clipped && !lwgeom_is_empty(clipped))
2468  {
2469  lwgeom_subdivide_recursive(clipped, dimension, maxvertices, depth, col, gridSize);
2470  lwgeom_free(clipped);
2471  }
2472  }
2473  {
2475  geom->srid, subbox2.xmin, subbox2.ymin, subbox2.xmax, subbox2.ymax);
2476  LWGEOM *clipped = lwgeom_intersection_prec(geom, subbox, gridSize);
2477  lwgeom_simplify_in_place(clipped, 0.0, LW_TRUE);
2478  lwgeom_free(subbox);
2479  if (clipped && !lwgeom_is_empty(clipped))
2480  {
2481  lwgeom_subdivide_recursive(clipped, dimension, maxvertices, depth, col, gridSize);
2482  lwgeom_free(clipped);
2483  }
2484  }
2485 }
void gbox_duplicate(const GBOX *original, GBOX *duplicate)
Copy the values of original GBOX into duplicate.
Definition: gbox.c:433
#define MULTIPOINTTYPE
Definition: liblwgeom.h:120
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:117
#define TINTYPE
Definition: liblwgeom.h:131
LWPOLY * lwpoly_construct_envelope(int32_t srid, double x1, double y1, double x2, double y2)
Definition: lwpoly.c:98
#define POLYGONTYPE
Definition: liblwgeom.h:119
#define POLYHEDRALSURFACETYPE
Definition: liblwgeom.h:129
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:216
LWGEOM * lwgeom_intersection_prec(const LWGEOM *geom1, const LWGEOM *geom2, double gridSize)
LWCOLLECTION * lwcollection_add_lwgeom(LWCOLLECTION *col, const LWGEOM *geom)
Appends geom to the collection managed by col.
Definition: lwcollection.c:188
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:108
double ptarray_signed_area(const POINTARRAY *pa)
Returns the area in cartesian units.
Definition: ptarray.c:1003
#define LW_ON_INTERRUPT(x)
#define FP_TOLERANCE
Floating point comparators.
#define FP_NEQUALS(A, B)
int lwgeom_is_collection(const LWGEOM *geom)
Determine whether a LWGEOM can contain sub-geometries or not.
Definition: lwgeom.c:1097
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep-clone an LWGEOM object.
Definition: lwgeom.c:529
uint32_t lwgeom_count_vertices(const LWGEOM *geom)
Count points in an LWGEOM.
Definition: lwgeom.c:1246
int lwgeom_dimension(const LWGEOM *geom)
For an LWGEOM, returns 0 for points, 1 for lines, 2 for polygons, 3 for volume, and the max dimension...
Definition: lwgeom.c:1298
const GBOX * lwgeom_get_bbox(const LWGEOM *lwg)
Get a non-empty geometry bounding box, computing and caching it if not already there.
Definition: lwgeom.c:743
void lwgeom_free(LWGEOM *lwgeom)
Definition: lwgeom.c:1155
static void lwgeom_subdivide_recursive(const LWGEOM *geom, uint8_t dimension, uint32_t maxvertices, uint32_t depth, LWCOLLECTION *col, double gridSize)
Definition: lwgeom.c:2307
int lwgeom_simplify_in_place(LWGEOM *geom, double epsilon, int preserve_collapsed)
Definition: lwgeom.c:1760
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
static const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from.
Definition: lwinline.h:101
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
Definition: lwinline.h:203
static double pivot(double *left, double *right)
Definition: rt_statistics.c:40
double ymax
Definition: liblwgeom.h:372
double xmax
Definition: liblwgeom.h:370
double ymin
Definition: liblwgeom.h:371
double xmin
Definition: liblwgeom.h:369
uint32_t ngeoms
Definition: liblwgeom.h:595
LWGEOM ** geoms
Definition: liblwgeom.h:590
uint8_t type
Definition: liblwgeom.h:477
int32_t srid
Definition: liblwgeom.h:475
POINTARRAY ** rings
Definition: liblwgeom.h:534
uint32_t nrings
Definition: liblwgeom.h:539
double y
Definition: liblwgeom.h:405
double x
Definition: liblwgeom.h:405
uint32_t npoints
Definition: liblwgeom.h:442

References FP_NEQUALS, FP_TOLERANCE, gbox_duplicate(), LWCOLLECTION::geoms, getPoint2d_cp(), LW_ON_INTERRUPT, LW_TRUE, lwcollection_add_lwgeom(), lwerror(), lwgeom_clone_deep(), lwgeom_count_vertices(), lwgeom_dimension(), lwgeom_free(), lwgeom_get_bbox(), lwgeom_intersection_prec(), lwgeom_is_collection(), lwgeom_is_empty(), lwgeom_simplify_in_place(), lwpoly_construct_envelope(), lwtype_name(), MULTIPOINTTYPE, LWCOLLECTION::ngeoms, POINTARRAY::npoints, LWPOLY::nrings, pivot(), POINTTYPE, POLYGONTYPE, POLYHEDRALSURFACETYPE, ptarray_signed_area(), LWPOLY::rings, LWGEOM::srid, TINTYPE, LWGEOM::type, POINT2D::x, GBOX::xmax, GBOX::xmin, POINT2D::y, GBOX::ymax, and GBOX::ymin.

Referenced by lwgeom_subdivide_prec().

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