PostGIS  2.5.7dev-r@@SVN_REVISION@@

## ◆ lwgeom_calculate_mbc()

 LWBOUNDINGCIRCLE* lwgeom_calculate_mbc ( const LWGEOM * g )

Definition at line 237 of file lwboundingcircle.c.

238 {
239  SUPPORTING_POINTS* support;
240  LWBOUNDINGCIRCLE* result;
241  LWPOINTITERATOR* it;
242  uint32_t num_points;
243  POINT2D** points;
244  POINT4D p;
245  uint32_t i;
246  int success;
247
248  if(g == NULL || lwgeom_is_empty(g))
249  return LW_FAILURE;
250
251  num_points = lwgeom_count_vertices(g);
252  it = lwpointiterator_create(g);
253  points = lwalloc(num_points * sizeof(POINT2D*));
254  for (i = 0; i < num_points; i++)
255  {
256  if(!lwpointiterator_next(it, &p))
257  {
258  uint32_t j;
259  for (j = 0; j < i; j++)
260  {
261  lwfree(points[j]);
262  }
264  lwfree(points);
265  return LW_FAILURE;
266  }
267
268  points[i] = lwalloc(sizeof(POINT2D));
269  points[i]->x = p.x;
270  points[i]->y = p.y;
271  }
273
274  support = supporting_points_create();
275  result = lwboundingcircle_create();
276  /* Technically, a randomized algorithm would demand that we shuffle the input points
277  * before we call calculate_mbc(). However, we make the (perhaps poor) assumption that
278  * the order we happen to find the points is as good as random, or close enough.
279  * */
280  success = calculate_mbc((const POINT2D**) points, num_points, support, result);
281
282  for (i = 0; i < num_points; i++)
283  {
284  lwfree(points[i]);
285  }
286  lwfree(points);
287  supporting_points_destroy(support);
288
289  if (!success)
290  return NULL;
291
292  return result;
293 }
LWPOINTITERATOR * lwpointiterator_create(const LWGEOM *g)
Create a new LWPOINTITERATOR over supplied LWGEOM*.
Definition: lwiterator.c:244
#define LW_FAILURE
Definition: liblwgeom.h:79
int lwpointiterator_next(LWPOINTITERATOR *s, POINT4D *p)
Attempts to assign the next point in the iterator to p, and advances the iterator to the next point.
Definition: lwiterator.c:212
void lwpointiterator_destroy(LWPOINTITERATOR *s)
Free all memory associated with the iterator.
Definition: lwiterator.c:269
uint32_t lwgeom_count_vertices(const LWGEOM *geom)
Count the total number of vertices in any LWGEOM.
Definition: lwgeom.c:1235
void lwfree(void *mem)
Definition: lwutil.c:244
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:1393
void * lwalloc(size_t size)
Definition: lwutil.c:229
static void supporting_points_destroy(SUPPORTING_POINTS *s)
static int calculate_mbc(const POINT2D **points, uint32_t max_n, SUPPORTING_POINTS *support, LWBOUNDINGCIRCLE *mbc)
static SUPPORTING_POINTS * supporting_points_create()
static LWBOUNDINGCIRCLE * lwboundingcircle_create()
double y
Definition: liblwgeom.h:331
double x
Definition: liblwgeom.h:331
double x
Definition: liblwgeom.h:355
double y
Definition: liblwgeom.h:355
unsigned int uint32_t
Definition: uthash.h:78

Referenced by mbc_test(), ST_MinimumBoundingCircle(), ST_MinimumBoundingRadius(), and test_empty().

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