PostGIS  3.0.6dev-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 1460 of file ptarray.c.

1461 {
1462  uint32_t i;
1463  double tolsq = tolerance * tolerance;
1464  const POINT2D *last = NULL;
1465  const POINT2D *pt;
1466  uint32_t n_points = pa->npoints;
1467  uint32_t n_points_out = 1;
1468  size_t pt_size = ptarray_point_size(pa);
1469 
1470  double dsq = FLT_MAX;
1471 
1472  /* No-op on short inputs */
1473  if ( n_points <= min_points ) return;
1474 
1475  last = getPoint2d_cp(pa, 0);
1476  void *p_to = ((char *)last) + pt_size;
1477  for (i = 1; i < n_points; i++)
1478  {
1479  int last_point = (i == n_points - 1);
1480 
1481  /* Look straight into the abyss */
1482  pt = getPoint2d_cp(pa, i);
1483 
1484  /* Don't drop points if we are running short of points */
1485  if (n_points + n_points_out > min_points + i)
1486  {
1487  if (tolerance > 0.0)
1488  {
1489  /* Only drop points that are within our tolerance */
1490  dsq = distance2d_sqr_pt_pt(last, pt);
1491  /* Allow any point but the last one to be dropped */
1492  if (!last_point && dsq <= tolsq)
1493  {
1494  continue;
1495  }
1496  }
1497  else
1498  {
1499  /* At tolerance zero, only skip exact dupes */
1500  if (memcmp((char*)pt, (char*)last, pt_size) == 0)
1501  continue;
1502  }
1503 
1504  /* Got to last point, and it's not very different from */
1505  /* the point that preceded it. We want to keep the last */
1506  /* point, not the second-to-last one, so we pull our write */
1507  /* index back one value */
1508  if (last_point && n_points_out > 1 && tolerance > 0.0 && dsq <= tolsq)
1509  {
1510  n_points_out--;
1511  p_to -= pt_size;
1512  }
1513  }
1514 
1515  /* Compact all remaining values to front of array */
1516  memcpy(p_to, pt, pt_size);
1517  n_points_out++;
1518  p_to += pt_size;
1519  last = pt;
1520  }
1521  /* Adjust array length */
1522  pa->npoints = n_points_out;
1523  return;
1524 }
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:91
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:48
uint32_t npoints
Definition: liblwgeom.h:413

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: