PostGIS  3.3.9dev-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 1700 of file ptarray.c.

1701 {
1702  /* Do not try to simplify really short things */
1703  if (pa->npoints < 3 || pa->npoints <= minpts)
1704  return;
1705 
1706  if (tolerance == 0 && minpts <= 2)
1707  {
1709  return;
1710  }
1711 
1712  /* We use this array to keep track of the points we are keeping, so
1713  * we store just TRUE / FALSE in their position */
1714  uint8_t *kept_points = lwalloc(sizeof(uint8_t) * pa->npoints);
1715  memset(kept_points, LW_FALSE, sizeof(uint8_t) * pa->npoints);
1716  kept_points[0] = LW_TRUE;
1717  kept_points[pa->npoints - 1] = LW_TRUE;
1718  uint32_t keptn = 2;
1719 
1720  /* We use this array as a stack to store the iterators that we are going to need
1721  * in the following steps.
1722  * This is ~10% faster than iterating over @kept_points looking for them
1723  */
1724  uint32_t *iterator_stack = lwalloc(sizeof(uint32_t) * pa->npoints);
1725  iterator_stack[0] = 0;
1726  uint32_t iterator_stack_size = 1;
1727 
1728  uint32_t it_first = 0;
1729  uint32_t it_last = pa->npoints - 1;
1730 
1731  const double tolerance_sqr = tolerance * tolerance;
1732  /* For the first @minpts points we ignore the tolerance */
1733  double it_tol = keptn >= minpts ? tolerance_sqr : -1.0;
1734 
1735  while (iterator_stack_size)
1736  {
1737  uint32_t split = ptarray_dp_findsplit_in_place(pa, it_first, it_last, it_tol);
1738  if (split == it_first)
1739  {
1740  it_first = it_last;
1741  it_last = iterator_stack[--iterator_stack_size];
1742  }
1743  else
1744  {
1745  kept_points[split] = LW_TRUE;
1746  keptn++;
1747 
1748  iterator_stack[iterator_stack_size++] = it_last;
1749  it_last = split;
1750  it_tol = keptn >= minpts ? tolerance_sqr : -1.0;
1751  }
1752  }
1753 
1754  const size_t pt_size = ptarray_point_size(pa);
1755  /* The first point is already in place, so we don't need to copy it */
1756  size_t kept_it = 1;
1757  if (keptn == 2)
1758  {
1759  /* If there are 2 points remaining, it has to be first and last as
1760  * we added those at the start */
1761  memcpy(pa->serialized_pointlist + pt_size * kept_it,
1762  pa->serialized_pointlist + pt_size * (pa->npoints - 1),
1763  pt_size);
1764  }
1765  else if (pa->npoints != keptn) /* We don't need to move any points if we are keeping them all */
1766  {
1767  for (uint32_t i = 1; i < pa->npoints; i++)
1768  {
1769  if (kept_points[i])
1770  {
1771  memcpy(pa->serialized_pointlist + pt_size * kept_it,
1772  pa->serialized_pointlist + pt_size * i,
1773  pt_size);
1774  kept_it++;
1775  }
1776  }
1777  }
1778  pa->npoints = keptn;
1779 
1780  lwfree(kept_points);
1781  lwfree(iterator_stack);
1782 }
#define LW_FALSE
Definition: liblwgeom.h:109
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:108
static size_t ptarray_point_size(const POINTARRAY *pa)
Definition: lwinline.h:58
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:1593
static void ptarray_simplify_in_place_tolerance0(POINTARRAY *pa)
Definition: ptarray.c:1658
uint32_t npoints
Definition: liblwgeom.h:442
uint8_t * serialized_pointlist
Definition: liblwgeom.h:449

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().

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