PostGIS  3.2.2dev-r@@SVN_REVISION@@

◆ ptarray_remove_repeated_points_in_place()

void ptarray_remove_repeated_points_in_place ( POINTARRAY pa,
double  tolerance,
uint32_t  min_points 
)

Definition at line 1802 of file ptarray.c.

1803 {
1804  uint32_t i;
1805  double tolsq = tolerance * tolerance;
1806  const POINT2D *last = NULL;
1807  const POINT2D *pt;
1808  uint32_t n_points = pa->npoints;
1809  uint32_t n_points_out = 1;
1810  size_t pt_size = ptarray_point_size(pa);
1811 
1812  double dsq = FLT_MAX;
1813 
1814  /* No-op on short inputs */
1815  if ( n_points <= min_points ) return;
1816 
1817  last = getPoint2d_cp(pa, 0);
1818  void *p_to = ((char *)last) + pt_size;
1819  for (i = 1; i < n_points; i++)
1820  {
1821  int last_point = (i == n_points - 1);
1822 
1823  /* Look straight into the abyss */
1824  pt = getPoint2d_cp(pa, i);
1825 
1826  /* Don't drop points if we are running short of points */
1827  if (n_points + n_points_out > min_points + i)
1828  {
1829  if (tolerance > 0.0)
1830  {
1831  /* Only drop points that are within our tolerance */
1832  dsq = distance2d_sqr_pt_pt(last, pt);
1833  /* Allow any point but the last one to be dropped */
1834  if (!last_point && dsq <= tolsq)
1835  {
1836  continue;
1837  }
1838  }
1839  else
1840  {
1841  /* At tolerance zero, only skip exact dupes */
1842  if (memcmp((char*)pt, (char*)last, pt_size) == 0)
1843  continue;
1844  }
1845 
1846  /* Got to last point, and it's not very different from */
1847  /* the point that preceded it. We want to keep the last */
1848  /* point, not the second-to-last one, so we pull our write */
1849  /* index back one value */
1850  if (last_point && n_points_out > 1 && tolerance > 0.0 && dsq <= tolsq)
1851  {
1852  n_points_out--;
1853  p_to = (char*)p_to - pt_size;
1854  }
1855  }
1856 
1857  /* Compact all remaining values to front of array */
1858  memcpy(p_to, pt, pt_size);
1859  n_points_out++;
1860  p_to = (char*)p_to + pt_size;
1861  last = pt;
1862  }
1863  /* Adjust array length */
1864  pa->npoints = n_points_out;
1865  return;
1866 }
static const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from.
Definition: lwinline.h:101
static double distance2d_sqr_pt_pt(const POINT2D *p1, const POINT2D *p2)
Definition: lwinline.h:35
static size_t ptarray_point_size(const POINTARRAY *pa)
Definition: lwinline.h:58
uint32_t npoints
Definition: liblwgeom.h:441

References distance2d_sqr_pt_pt(), getPoint2d_cp(), POINTARRAY::npoints, and ptarray_point_size().

Referenced by lwcompound_linearize(), lwgeom_remove_repeated_points_in_place(), and ptarray_remove_repeated_points_minpoints().

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