PostGIS  2.5.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 1446 of file ptarray.c.

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

Referenced by lwgeom_remove_repeated_points_in_place(), and ptarray_remove_repeated_points_minpoints().

1447 {
1448  uint32_t i;
1449  double tolsq = tolerance * tolerance;
1450  const POINT2D *last = NULL;
1451  const POINT2D *pt;
1452  uint32_t n_points = pa->npoints;
1453  uint32_t n_points_out = 1;
1454  size_t pt_size = ptarray_point_size(pa);
1455 
1456  double dsq = FLT_MAX;
1457 
1458  /* No-op on short inputs */
1459  if ( n_points <= min_points ) return;
1460 
1461  last = getPoint2d_cp(pa, 0);
1462  for (i = 1; i < n_points; i++)
1463  {
1464  int last_point = (i == n_points-1);
1465 
1466  /* Look straight into the abyss */
1467  pt = getPoint2d_cp(pa, i);
1468 
1469  /* Don't drop points if we are running short of points */
1470  if (n_points + n_points_out > min_points + i)
1471  {
1472  if (tolerance > 0.0)
1473  {
1474  /* Only drop points that are within our tolerance */
1475  dsq = distance2d_sqr_pt_pt(last, pt);
1476  /* Allow any point but the last one to be dropped */
1477  if (!last_point && dsq <= tolsq)
1478  {
1479  continue;
1480  }
1481  }
1482  else
1483  {
1484  /* At tolerance zero, only skip exact dupes */
1485  if (memcmp((char*)pt, (char*)last, pt_size) == 0)
1486  continue;
1487  }
1488 
1489  /* Got to last point, and it's not very different from */
1490  /* the point that preceded it. We want to keep the last */
1491  /* point, not the second-to-last one, so we pull our write */
1492  /* index back one value */
1493  if (last_point && n_points_out > 1 && tolerance > 0.0 && dsq <= tolsq)
1494  {
1495  n_points_out--;
1496  }
1497  }
1498 
1499  /* Compact all remaining values to front of array */
1500  ptarray_copy_point(pa, i, n_points_out++);
1501  last = pt;
1502  }
1503  /* Adjust array length */
1504  pa->npoints = n_points_out;
1505  return;
1506 }
size_t ptarray_point_size(const POINTARRAY *pa)
Definition: ptarray.c:54
void ptarray_copy_point(POINTARRAY *pa, uint32_t from, uint32_t to)
Definition: lwgeom_api.c:450
unsigned int uint32_t
Definition: uthash.h:78
double distance2d_sqr_pt_pt(const POINT2D *p1, const POINT2D *p2)
Definition: measures.c:2322
const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from...
Definition: lwgeom_api.c:364
uint32_t npoints
Definition: liblwgeom.h:373
Here is the call graph for this function:
Here is the caller graph for this function: