PostGIS  3.7.0dev-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 1721 of file ptarray.c.

1722 {
1723  /* Do not try to simplify really short things */
1724  if (pa->npoints < 3 || pa->npoints <= minpts)
1725  return;
1726 
1727  if (tolerance == 0 && minpts <= 2)
1728  {
1730  return;
1731  }
1732 
1733  /* We use this array to keep track of the points we are keeping, so
1734  * we store just TRUE / FALSE in their position */
1735  uint8_t *kept_points = lwalloc(sizeof(uint8_t) * pa->npoints);
1736  memset(kept_points, LW_FALSE, sizeof(uint8_t) * pa->npoints);
1737  kept_points[0] = LW_TRUE;
1738  kept_points[pa->npoints - 1] = LW_TRUE;
1739  uint32_t keptn = 2;
1740 
1741  /* We use this array as a stack to store the iterators that we are going to need
1742  * in the following steps.
1743  * This is ~10% faster than iterating over @kept_points looking for them
1744  */
1745  uint32_t *iterator_stack = lwalloc(sizeof(uint32_t) * pa->npoints);
1746  iterator_stack[0] = 0;
1747  uint32_t iterator_stack_size = 1;
1748 
1749  uint32_t it_first = 0;
1750  uint32_t it_last = pa->npoints - 1;
1751 
1752  const double tolerance_sqr = tolerance * tolerance;
1753  /* For the first @minpts points we ignore the tolerance */
1754  double it_tol = keptn >= minpts ? tolerance_sqr : -1.0;
1755 
1756  while (iterator_stack_size)
1757  {
1758  uint32_t split = ptarray_dp_findsplit_in_place(pa, it_first, it_last, it_tol);
1759  if (split == it_first)
1760  {
1761  it_first = it_last;
1762  it_last = iterator_stack[--iterator_stack_size];
1763  }
1764  else
1765  {
1766  kept_points[split] = LW_TRUE;
1767  keptn++;
1768 
1769  iterator_stack[iterator_stack_size++] = it_last;
1770  it_last = split;
1771  it_tol = keptn >= minpts ? tolerance_sqr : -1.0;
1772  }
1773  }
1774 
1775  const size_t pt_size = ptarray_point_size(pa);
1776  /* The first point is already in place, so we don't need to copy it */
1777  size_t kept_it = 1;
1778  if (keptn == 2)
1779  {
1780  /* If there are 2 points remaining, it has to be first and last as
1781  * we added those at the start */
1782  memcpy(pa->serialized_pointlist + pt_size * kept_it,
1783  pa->serialized_pointlist + pt_size * (pa->npoints - 1),
1784  pt_size);
1785  }
1786  else if (pa->npoints != keptn) /* We don't need to move any points if we are keeping them all */
1787  {
1788  for (uint32_t i = 1; i < pa->npoints; i++)
1789  {
1790  if (kept_points[i])
1791  {
1792  memcpy(pa->serialized_pointlist + pt_size * kept_it,
1793  pa->serialized_pointlist + pt_size * i,
1794  pt_size);
1795  kept_it++;
1796  }
1797  }
1798  }
1799  pa->npoints = keptn;
1800 
1801  lwfree(kept_points);
1802  lwfree(iterator_stack);
1803 }
#define LW_FALSE
Definition: liblwgeom.h:94
void lwfree(void *mem)
Definition: lwutil.c:248
void * lwalloc(size_t size)
Definition: lwutil.c:227
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:93
static size_t ptarray_point_size(const POINTARRAY *pa)
Definition: lwinline.h:56
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:1610
static void ptarray_simplify_in_place_tolerance0(POINTARRAY *pa)
Definition: ptarray.c:1675
uint32_t npoints
Definition: liblwgeom.h:427
uint8_t * serialized_pointlist
Definition: liblwgeom.h:434

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

Referenced by lwgeom_simplify_in_place(), and test_ptarray_simplify().

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