PostGIS  2.4.9dev-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 1601 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().

1602 {
1603  int *stack; /* recursion stack */
1604  int sp=-1; /* recursion stack pointer */
1605  int p1, split;
1606  double dist;
1607  POINTARRAY *outpts;
1608  POINT4D pt;
1609 
1610  double eps_sqr = epsilon * epsilon;
1611 
1612  /* Allocate recursion stack */
1613  stack = lwalloc(sizeof(int)*inpts->npoints);
1614 
1615  p1 = 0;
1616  stack[++sp] = inpts->npoints-1;
1617 
1618  LWDEBUGF(2, "Input has %d pts and %d dims", inpts->npoints,
1619  FLAGS_NDIMS(inpts->flags));
1620 
1621  /* Allocate output POINTARRAY, and add first point. */
1622  outpts = ptarray_construct_empty(FLAGS_GET_Z(inpts->flags), FLAGS_GET_M(inpts->flags), inpts->npoints);
1623  getPoint4d_p(inpts, 0, &pt);
1624  ptarray_append_point(outpts, &pt, LW_FALSE);
1625 
1626  LWDEBUG(3, "Added P0 to simplified point array (size 1)");
1627 
1628  do
1629  {
1630 
1631  ptarray_dp_findsplit(inpts, p1, stack[sp], &split, &dist);
1632 
1633  LWDEBUGF(3, "Farthest point from P%d-P%d is P%d (dist. %g)", p1, stack[sp], split, dist);
1634 
1635  if (dist > eps_sqr || ( outpts->npoints+sp+1 < minpts && dist >= 0 ) )
1636  {
1637  LWDEBUGF(4, "Added P%d to stack (outpts:%d)", split, sp);
1638  stack[++sp] = split;
1639  }
1640  else
1641  {
1642  getPoint4d_p(inpts, stack[sp], &pt);
1643  LWDEBUGF(4, "npoints , minpoints %d %d", outpts->npoints, minpts);
1644  ptarray_append_point(outpts, &pt, LW_FALSE);
1645 
1646  LWDEBUGF(4, "Added P%d to simplified point array (size: %d)", stack[sp], outpts->npoints);
1647 
1648  p1 = stack[sp--];
1649  }
1650 
1651  LWDEBUGF(4, "stack pointer = %d", sp);
1652  }
1653  while (! (sp<0) );
1654 
1655  lwfree(stack);
1656  return outpts;
1657 }
void lwfree(void *mem)
Definition: lwutil.c:244
int npoints
Definition: liblwgeom.h:371
#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:77
uint8_t flags
Definition: liblwgeom.h:369
#define FLAGS_GET_Z(flags)
Macros for manipulating the &#39;flags&#39; byte.
Definition: liblwgeom.h:140
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:141
void * lwalloc(size_t size)
Definition: lwutil.c:229
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
#define FLAGS_NDIMS(flags)
Definition: liblwgeom.h:152
int getPoint4d_p(const POINTARRAY *pa, int n, POINT4D *point)
Definition: lwgeom_api.c:122
static void ptarray_dp_findsplit(POINTARRAY *pts, int p1, int p2, int *split, double *dist)
Definition: ptarray.c:1552
Here is the call graph for this function:
Here is the caller graph for this function: