PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ ptarray_remove_repeated_points_in_place()

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

Definition at line 1468 of file ptarray.c.

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

Referenced by ptarray_remove_repeated_points_minpoints().

1469 {
1470  uint32_t i;
1471  double tolsq = tolerance * tolerance;
1472  const POINT2D *last = NULL;
1473  const POINT2D *pt;
1474  uint32_t n_points = pa->npoints;
1475  uint32_t n_points_out = 1;
1476  size_t pt_size = ptarray_point_size(pa);
1477 
1478  double dsq = FLT_MAX;
1479 
1480  /* No-op on short inputs */
1481  if ( n_points <= min_points ) return;
1482 
1483  last = getPoint2d_cp(pa, 0);
1484  for (i = 1; i < n_points; i++)
1485  {
1486  int last_point = (i == n_points-1);
1487 
1488  /* Look straight into the abyss */
1489  pt = getPoint2d_cp(pa, i);
1490 
1491  /* Don't drop points if we are running short of points */
1492  if (n_points + n_points_out > min_points + i)
1493  {
1494  if (tolerance > 0.0)
1495  {
1496  /* Only drop points that are within our tolerance */
1497  dsq = distance2d_sqr_pt_pt(last, pt);
1498  /* Allow any point but the last one to be dropped */
1499  if (!last_point && dsq <= tolsq)
1500  {
1501  continue;
1502  }
1503  }
1504  else
1505  {
1506  /* At tolerance zero, only skip exact dupes */
1507  if (memcmp((char*)pt, (char*)last, pt_size) == 0)
1508  continue;
1509  }
1510 
1511  /* Got to last point, and it's not very different from */
1512  /* the point that preceded it. We want to keep the last */
1513  /* point, not the second-to-last one, so we pull our write */
1514  /* index back one value */
1515  if (last_point && n_points_out > 1 && tolerance > 0.0 && dsq <= tolsq)
1516  {
1517  n_points_out--;
1518  }
1519  }
1520 
1521  /* Compact all remaining values to front of array */
1522  ptarray_copy_point(pa, i, n_points_out++);
1523  last = pt;
1524  }
1525  /* Adjust array length */
1526  pa->npoints = n_points_out;
1527  return;
1528 }
int ptarray_point_size(const POINTARRAY *pa)
Definition: ptarray.c:54
int npoints
Definition: liblwgeom.h:371
unsigned int uint32_t
Definition: uthash.h:78
const POINT2D * getPoint2d_cp(const POINTARRAY *pa, int n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from...
Definition: lwgeom_api.c:373
static void ptarray_copy_point(POINTARRAY *pa, uint32_t from, uint32_t to)
Definition: ptarray.c:1432
double distance2d_sqr_pt_pt(const POINT2D *p1, const POINT2D *p2)
Definition: measures.c:2327
Here is the call graph for this function:
Here is the caller graph for this function: