PostGIS  2.2.7dev-r@@SVN_REVISION@@
POINTARRAY* ptarray_simplify ( POINTARRAY inpts,
double  epsilon,
unsigned int  minpts 
)
Parameters
minptsminimun number of points to retain, if possible.

Definition at line 1554 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().

1555 {
1556  int *stack; /* recursion stack */
1557  int sp=-1; /* recursion stack pointer */
1558  int p1, split;
1559  double dist;
1560  POINTARRAY *outpts;
1561  POINT4D pt;
1562 
1563  double eps_sqr = epsilon * epsilon;
1564 
1565  /* Allocate recursion stack */
1566  stack = lwalloc(sizeof(int)*inpts->npoints);
1567 
1568  p1 = 0;
1569  stack[++sp] = inpts->npoints-1;
1570 
1571  LWDEBUGF(2, "Input has %d pts and %d dims", inpts->npoints,
1572  FLAGS_NDIMS(inpts->flags));
1573 
1574  /* Allocate output POINTARRAY, and add first point. */
1575  outpts = ptarray_construct_empty(FLAGS_GET_Z(inpts->flags), FLAGS_GET_M(inpts->flags), inpts->npoints);
1576  getPoint4d_p(inpts, 0, &pt);
1577  ptarray_append_point(outpts, &pt, LW_FALSE);
1578 
1579  LWDEBUG(3, "Added P0 to simplified point array (size 1)");
1580 
1581  do
1582  {
1583 
1584  ptarray_dp_findsplit(inpts, p1, stack[sp], &split, &dist);
1585 
1586  LWDEBUGF(3, "Farthest point from P%d-P%d is P%d (dist. %g)", p1, stack[sp], split, dist);
1587 
1588  if (dist > eps_sqr || ( outpts->npoints+sp+1 < minpts && dist >= 0 ) )
1589  {
1590  LWDEBUGF(4, "Added P%d to stack (outpts:%d)", split, sp);
1591  stack[++sp] = split;
1592  }
1593  else
1594  {
1595  getPoint4d_p(inpts, stack[sp], &pt);
1596  LWDEBUGF(4, "npoints , minpoints %d %d", outpts->npoints, minpts);
1597  ptarray_append_point(outpts, &pt, LW_FALSE);
1598 
1599  LWDEBUGF(4, "Added P%d to simplified point array (size: %d)", stack[sp], outpts->npoints);
1600 
1601  p1 = stack[sp--];
1602  }
1603 
1604  LWDEBUGF(4, "stack pointer = %d", sp);
1605  }
1606  while (! (sp<0) );
1607 
1608  lwfree(stack);
1609  return outpts;
1610 }
void lwfree(void *mem)
Definition: lwutil.c:214
int npoints
Definition: liblwgeom.h:355
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:50
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_TRUE, then a duplicate point will not be added.
Definition: ptarray.c:156
#define LW_FALSE
Definition: liblwgeom.h:62
uint8_t flags
Definition: liblwgeom.h:353
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:124
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:125
void * lwalloc(size_t size)
Definition: lwutil.c:199
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:55
#define FLAGS_NDIMS(flags)
Definition: liblwgeom.h:136
int getPoint4d_p(const POINTARRAY *pa, int n, POINT4D *point)
Definition: lwgeom_api.c:231
static void ptarray_dp_findsplit(POINTARRAY *pts, int p1, int p2, int *split, double *dist)
Definition: ptarray.c:1505

Here is the call graph for this function:

Here is the caller graph for this function: