PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ ptarray_simplify_in_place()

void ptarray_simplify_in_place ( POINTARRAY pa,
double  tolerance,
uint32_t  minpts 
)
Parameters
minptsminimum number of points to retain, if possible.

Definition at line 1587 of file ptarray.c.

1588 {
1589  /* Do not try to simplify really short things */
1590  if (pa->npoints < 3 || pa->npoints <= minpts)
1591  return;
1592 
1593  /* We use this array to keep track of the points we are keeping, so
1594  * we store just TRUE / FALSE in their position */
1595  uint8_t *kept_points = lwalloc(sizeof(uint8_t) * pa->npoints);
1596  memset(kept_points, LW_FALSE, sizeof(uint8_t) * pa->npoints);
1597  kept_points[0] = LW_TRUE;
1598  kept_points[pa->npoints - 1] = LW_TRUE;
1599  uint32_t keptn = 2;
1600 
1601  /* We use this array as a stack to store the iterators that we are going to need
1602  * in the following steps.
1603  * This is ~10% faster than iterating over @kept_points looking for them
1604  */
1605  uint32_t *iterator_stack = lwalloc(sizeof(uint32_t) * pa->npoints);
1606  iterator_stack[0] = 0;
1607  uint32_t iterator_stack_size = 1;
1608 
1609  uint32_t it_first = 0;
1610  uint32_t it_last = pa->npoints - 1;
1611 
1612  const double tolerance_sqr = tolerance * tolerance;
1613  /* For the first @minpts points we ignore the tolerance */
1614  double it_tol = keptn >= minpts ? tolerance_sqr : -1.0;
1615 
1616  while (iterator_stack_size)
1617  {
1618  uint32_t split = ptarray_dp_findsplit_in_place(pa, it_first, it_last, it_tol);
1619  if (split == it_first)
1620  {
1621  it_first = it_last;
1622  it_last = iterator_stack[--iterator_stack_size];
1623  }
1624  else
1625  {
1626  kept_points[split] = LW_TRUE;
1627  keptn++;
1628 
1629  iterator_stack[iterator_stack_size++] = it_last;
1630  it_last = split;
1631  it_tol = keptn >= minpts ? tolerance_sqr : -1.0;
1632  }
1633  }
1634 
1635  const size_t pt_size = ptarray_point_size(pa);
1636  /* The first point is already in place, so we don't need to copy it */
1637  size_t kept_it = 1;
1638  if (keptn == 2)
1639  {
1640  /* If there are 2 points remaining, it has to be first and last as
1641  * we added those at the start */
1642  memcpy(pa->serialized_pointlist + pt_size * kept_it,
1643  pa->serialized_pointlist + pt_size * (pa->npoints - 1),
1644  pt_size);
1645  }
1646  else
1647  {
1648  for (uint32_t i = 1; i < pa->npoints; i++)
1649  {
1650  if (kept_points[i])
1651  {
1652  memcpy(pa->serialized_pointlist + pt_size * kept_it,
1653  pa->serialized_pointlist + pt_size * i,
1654  pt_size);
1655  kept_it++;
1656  }
1657  }
1658  }
1659  pa->npoints = keptn;
1660 
1661  lwfree(kept_points);
1662  lwfree(iterator_stack);
1663 }
#define LW_FALSE
Definition: liblwgeom.h:108
void lwfree(void *mem)
Definition: lwutil.c:242
void * lwalloc(size_t size)
Definition: lwutil.c:227
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:107
static size_t ptarray_point_size(const POINTARRAY *pa)
Definition: lwinline.h:48
static uint32_t ptarray_dp_findsplit_in_place(const POINTARRAY *pts, uint32_t it_first, uint32_t it_last, double max_distance_sqr)
Definition: ptarray.c:1523
uint32_t npoints
Definition: liblwgeom.h:413
uint8_t * serialized_pointlist
Definition: liblwgeom.h:420

References LW_FALSE, LW_TRUE, lwalloc(), lwfree(), POINTARRAY::npoints, ptarray_dp_findsplit_in_place(), ptarray_point_size(), and POINTARRAY::serialized_pointlist.

Referenced by lwgeom_simplify_in_place().

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