PostGIS  3.4.0dev-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 1535 of file ptarray.c.

1536 {
1537  uint32_t i;
1538  double tolsq = tolerance * tolerance;
1539  const POINT2D *last = NULL;
1540  const POINT2D *pt;
1541  uint32_t n_points = pa->npoints;
1542  uint32_t n_points_out = 1;
1543  size_t pt_size = ptarray_point_size(pa);
1544 
1545  double dsq = FLT_MAX;
1546 
1547  /* No-op on short inputs */
1548  if ( n_points <= min_points ) return;
1549 
1550  last = getPoint2d_cp(pa, 0);
1551  void *p_to = ((char *)last) + pt_size;
1552  for (i = 1; i < n_points; i++)
1553  {
1554  int last_point = (i == n_points - 1);
1555 
1556  /* Look straight into the abyss */
1557  pt = getPoint2d_cp(pa, i);
1558 
1559  /* Don't drop points if we are running short of points */
1560  if (n_points + n_points_out > min_points + i)
1561  {
1562  if (tolerance > 0.0)
1563  {
1564  /* Only drop points that are within our tolerance */
1565  dsq = distance2d_sqr_pt_pt(last, pt);
1566  /* Allow any point but the last one to be dropped */
1567  if (!last_point && dsq <= tolsq)
1568  {
1569  continue;
1570  }
1571  }
1572  else
1573  {
1574  /* At tolerance zero, only skip exact dupes */
1575  if (memcmp((char*)pt, (char*)last, pt_size) == 0)
1576  continue;
1577  }
1578 
1579  /* Got to last point, and it's not very different from */
1580  /* the point that preceded it. We want to keep the last */
1581  /* point, not the second-to-last one, so we pull our write */
1582  /* index back one value */
1583  if (last_point && n_points_out > 1 && tolerance > 0.0 && dsq <= tolsq)
1584  {
1585  n_points_out--;
1586  p_to = (char*)p_to - pt_size;
1587  }
1588  }
1589 
1590  /* Compact all remaining values to front of array */
1591  memcpy(p_to, pt, pt_size);
1592  n_points_out++;
1593  p_to = (char*)p_to + pt_size;
1594  last = pt;
1595  }
1596  /* Adjust array length */
1597  pa->npoints = n_points_out;
1598  return;
1599 }
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:427

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: