PostGIS  2.3.8dev-r@@SVN_REVISION@@

◆ ptarray_simplify()

POINTARRAY* ptarray_simplify ( POINTARRAY inpts,
double  epsilon,
unsigned int  minpts 
)
Parameters
minptsminimun number of points to retain, if possible.

Definition at line 1561 of file ptarray.c.

References POINTARRAY::flags, FLAGS_GET_M, FLAGS_GET_Z, FLAGS_NDIMS, getPoint4d_p(), LW_FALSE, lwalloc(), LWDEBUG, LWDEBUGF, lwfree(), POINTARRAY::npoints, ptarray_append_point(), ptarray_construct_empty(), and ptarray_dp_findsplit().

Referenced by lwline_simplify(), and lwpoly_simplify().

1562 {
1563  int *stack; /* recursion stack */
1564  int sp=-1; /* recursion stack pointer */
1565  int p1, split;
1566  double dist;
1567  POINTARRAY *outpts;
1568  POINT4D pt;
1569 
1570  double eps_sqr = epsilon * epsilon;
1571 
1572  /* Allocate recursion stack */
1573  stack = lwalloc(sizeof(int)*inpts->npoints);
1574 
1575  p1 = 0;
1576  stack[++sp] = inpts->npoints-1;
1577 
1578  LWDEBUGF(2, "Input has %d pts and %d dims", inpts->npoints,
1579  FLAGS_NDIMS(inpts->flags));
1580 
1581  /* Allocate output POINTARRAY, and add first point. */
1582  outpts = ptarray_construct_empty(FLAGS_GET_Z(inpts->flags), FLAGS_GET_M(inpts->flags), inpts->npoints);
1583  getPoint4d_p(inpts, 0, &pt);
1584  ptarray_append_point(outpts, &pt, LW_FALSE);
1585 
1586  LWDEBUG(3, "Added P0 to simplified point array (size 1)");
1587 
1588  do
1589  {
1590 
1591  ptarray_dp_findsplit(inpts, p1, stack[sp], &split, &dist);
1592 
1593  LWDEBUGF(3, "Farthest point from P%d-P%d is P%d (dist. %g)", p1, stack[sp], split, dist);
1594 
1595  if (dist > eps_sqr || ( outpts->npoints+sp+1 < minpts && dist >= 0 ) )
1596  {
1597  LWDEBUGF(4, "Added P%d to stack (outpts:%d)", split, sp);
1598  stack[++sp] = split;
1599  }
1600  else
1601  {
1602  getPoint4d_p(inpts, stack[sp], &pt);
1603  LWDEBUGF(4, "npoints , minpoints %d %d", outpts->npoints, minpts);
1604  ptarray_append_point(outpts, &pt, LW_FALSE);
1605 
1606  LWDEBUGF(4, "Added P%d to simplified point array (size: %d)", stack[sp], outpts->npoints);
1607 
1608  p1 = stack[sp--];
1609  }
1610 
1611  LWDEBUGF(4, "stack pointer = %d", sp);
1612  }
1613  while (! (sp<0) );
1614 
1615  lwfree(stack);
1616  return outpts;
1617 }
void lwfree(void *mem)
Definition: lwutil.c:242
int npoints
Definition: liblwgeom.h:370
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
int ptarray_append_point(POINTARRAY *pa, const POINT4D *pt, int repeated_points)
Append a point to the end of an existing POINTARRAY If allow_duplicate is LW_FALSE, then a duplicate point will not be added.
Definition: ptarray.c:156
#define LW_FALSE
Definition: liblwgeom.h:76
uint8_t flags
Definition: liblwgeom.h:368
#define FLAGS_GET_Z(flags)
Macros for manipulating the &#39;flags&#39; byte.
Definition: liblwgeom.h:139
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:70
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:140
void * lwalloc(size_t size)
Definition: lwutil.c:227
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
#define FLAGS_NDIMS(flags)
Definition: liblwgeom.h:151
int getPoint4d_p(const POINTARRAY *pa, int n, POINT4D *point)
Definition: lwgeom_api.c:244
static void ptarray_dp_findsplit(POINTARRAY *pts, int p1, int p2, int *split, double *dist)
Definition: ptarray.c:1512
Here is the call graph for this function:
Here is the caller graph for this function: