PostGIS  3.2.2dev-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 2290 of file lwgeom.c.

2296 {
2297  const uint32_t maxdepth = 50;
2298 
2299  if (!geom)
2300  return;
2301 
2302  const GBOX *box_in = lwgeom_get_bbox(geom);
2303  if (!box_in)
2304  return;
2305 
2306  LW_ON_INTERRUPT(return);
2307 
2308  GBOX clip;
2309  gbox_duplicate(box_in, &clip);
2310  double width = clip.xmax - clip.xmin;
2311  double height = clip.ymax - clip.ymin;
2312 
2313  if ( geom->type == POLYHEDRALSURFACETYPE || geom->type == TINTYPE )
2314  lwerror("%s: unsupported geometry type '%s'", __func__, lwtype_name(geom->type));
2315 
2316  if ( width == 0.0 && height == 0.0 )
2317  {
2318  if ( geom->type == POINTTYPE && dimension == 0)
2320  return;
2321  }
2322 
2323  if (width == 0.0)
2324  {
2325  clip.xmax += FP_TOLERANCE;
2326  clip.xmin -= FP_TOLERANCE;
2327  width = 2 * FP_TOLERANCE;
2328  }
2329  if (height == 0.0)
2330  {
2331  clip.ymax += FP_TOLERANCE;
2332  clip.ymin -= FP_TOLERANCE;
2333  height = 2 * FP_TOLERANCE;
2334  }
2335 
2336  /* Always just recurse into collections */
2337  if ( lwgeom_is_collection(geom) && geom->type != MULTIPOINTTYPE )
2338  {
2339  LWCOLLECTION *incol = (LWCOLLECTION*)geom;
2340  /* Don't increment depth yet, since we aren't actually
2341  * subdividing geometries yet */
2342  for (uint32_t i = 0; i < incol->ngeoms; i++ )
2343  lwgeom_subdivide_recursive(incol->geoms[i], dimension, maxvertices, depth, col, gridSize);
2344  return;
2345  }
2346 
2347  if (lwgeom_dimension(geom) < dimension)
2348  {
2349  /* We've hit a lower dimension object produced by clipping at
2350  * a shallower recursion level. Ignore it. */
2351  return;
2352  }
2353 
2354  /* But don't go too far. 2^50 ~= 10^15, that's enough subdivision */
2355  /* Just add what's left */
2356  if ( depth > maxdepth )
2357  {
2359  return;
2360  }
2361 
2362  uint32_t nvertices = lwgeom_count_vertices(geom);
2363 
2364  /* Skip empties entirely */
2365  if (nvertices == 0)
2366  return;
2367 
2368  /* If it is under the vertex tolerance, just add it, we're done */
2369  if (nvertices <= maxvertices)
2370  {
2372  return;
2373  }
2374 
2375  uint8_t split_ordinate = (width > height) ? 0 : 1;
2376  double center = (split_ordinate == 0) ? (clip.xmin + clip.xmax) / 2 : (clip.ymin + clip.ymax) / 2;
2377  double pivot = DBL_MAX;
2378  if (geom->type == POLYGONTYPE)
2379  {
2380  uint32_t ring_to_trim = 0;
2381  double ring_area = 0;
2382  double pivot_eps = DBL_MAX;
2383  double pt_eps = DBL_MAX;
2384  POINTARRAY *pa;
2385  LWPOLY *lwpoly = (LWPOLY *)geom;
2386 
2387  /* if there are more points in holes than in outer ring */
2388  if (nvertices >= 2 * lwpoly->rings[0]->npoints)
2389  {
2390  /* trim holes starting from biggest */
2391  for (uint32_t i = 1; i < lwpoly->nrings; i++)
2392  {
2393  double current_ring_area = fabs(ptarray_signed_area(lwpoly->rings[i]));
2394  if (current_ring_area >= ring_area)
2395  {
2396  ring_area = current_ring_area;
2397  ring_to_trim = i;
2398  }
2399  }
2400  }
2401 
2402  pa = lwpoly->rings[ring_to_trim];
2403 
2404  /* find most central point in chosen ring */
2405  for (uint32_t i = 0; i < pa->npoints; i++)
2406  {
2407  double pt;
2408  if (split_ordinate == 0)
2409  pt = getPoint2d_cp(pa, i)->x;
2410  else
2411  pt = getPoint2d_cp(pa, i)->y;
2412  pt_eps = fabs(pt - center);
2413  if (pivot_eps > pt_eps)
2414  {
2415  pivot = pt;
2416  pivot_eps = pt_eps;
2417  }
2418  }
2419  }
2420  GBOX subbox1, subbox2;
2421  gbox_duplicate(&clip, &subbox1);
2422  gbox_duplicate(&clip, &subbox2);
2423 
2424  if (pivot == DBL_MAX)
2425  pivot = center;
2426 
2427  if (split_ordinate == 0)
2428  {
2429  if (FP_NEQUALS(subbox1.xmax, pivot) && FP_NEQUALS(subbox1.xmin, pivot))
2430  subbox1.xmax = subbox2.xmin = pivot;
2431  else
2432  subbox1.xmax = subbox2.xmin = center;
2433  }
2434  else
2435  {
2436  if (FP_NEQUALS(subbox1.ymax, pivot) && FP_NEQUALS(subbox1.ymin, pivot))
2437  subbox1.ymax = subbox2.ymin = pivot;
2438  else
2439  subbox1.ymax = subbox2.ymin = center;
2440  }
2441 
2442  ++depth;
2443 
2444  {
2446  geom->srid, subbox1.xmin, subbox1.ymin, subbox1.xmax, subbox1.ymax);
2447  LWGEOM *clipped = lwgeom_intersection_prec(geom, subbox, gridSize);
2448  lwgeom_simplify_in_place(clipped, 0.0, LW_TRUE);
2449  lwgeom_free(subbox);
2450  if (clipped && !lwgeom_is_empty(clipped))
2451  {
2452  lwgeom_subdivide_recursive(clipped, dimension, maxvertices, depth, col, gridSize);
2453  lwgeom_free(clipped);
2454  }
2455  }
2456  {
2458  geom->srid, subbox2.xmin, subbox2.ymin, subbox2.xmax, subbox2.ymax);
2459  LWGEOM *clipped = lwgeom_intersection_prec(geom, subbox, gridSize);
2460  lwgeom_simplify_in_place(clipped, 0.0, LW_TRUE);
2461  lwgeom_free(subbox);
2462  if (clipped && !lwgeom_is_empty(clipped))
2463  {
2464  lwgeom_subdivide_recursive(clipped, dimension, maxvertices, depth, col, gridSize);
2465  lwgeom_free(clipped);
2466  }
2467  }
2468 }
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:119
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:116
#define TINTYPE
Definition: liblwgeom.h:130
LWPOLY * lwpoly_construct_envelope(int32_t srid, double x1, double y1, double x2, double y2)
Definition: lwpoly.c:98
#define POLYGONTYPE
Definition: liblwgeom.h:118
#define POLYHEDRALSURFACETYPE
Definition: liblwgeom.h:128
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:107
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:1080
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep-clone an LWGEOM object.
Definition: lwgeom.c:512
uint32_t lwgeom_count_vertices(const LWGEOM *geom)
Count points in an LWGEOM.
Definition: lwgeom.c:1229
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:1281
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:726
void lwgeom_free(LWGEOM *lwgeom)
Definition: lwgeom.c:1138
static void lwgeom_subdivide_recursive(const LWGEOM *geom, uint8_t dimension, uint32_t maxvertices, uint32_t depth, LWCOLLECTION *col, double gridSize)
Definition: lwgeom.c:2290
int lwgeom_simplify_in_place(LWGEOM *geom, double epsilon, int preserve_collapsed)
Definition: lwgeom.c:1743
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:371
double xmax
Definition: liblwgeom.h:369
double ymin
Definition: liblwgeom.h:370
double xmin
Definition: liblwgeom.h:368
uint32_t ngeoms
Definition: liblwgeom.h:594
LWGEOM ** geoms
Definition: liblwgeom.h:589
uint8_t type
Definition: liblwgeom.h:476
int32_t srid
Definition: liblwgeom.h:474
POINTARRAY ** rings
Definition: liblwgeom.h:533
uint32_t nrings
Definition: liblwgeom.h:538
double y
Definition: liblwgeom.h:404
double x
Definition: liblwgeom.h:404
uint32_t npoints
Definition: liblwgeom.h:441

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: