PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ ptarray_remove_repeated_points_in_place()

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

Definition at line 1674 of file ptarray.c.

1675{
1676 uint32_t i;
1677 double tolsq = tolerance * tolerance;
1678 const POINT2D *last = NULL;
1679 const POINT2D *pt;
1680 uint32_t n_points = pa->npoints;
1681 uint32_t n_points_out = 1;
1682 size_t pt_size = ptarray_point_size(pa);
1683
1684 double dsq = FLT_MAX;
1685
1686 /* No-op on short inputs */
1687 if ( n_points <= min_points ) return;
1688
1689 last = getPoint2d_cp(pa, 0);
1690 void *p_to = ((char *)last) + pt_size;
1691 for (i = 1; i < n_points; i++)
1692 {
1693 int last_point = (i == n_points - 1);
1694
1695 /* Look straight into the abyss */
1696 pt = getPoint2d_cp(pa, i);
1697
1698 /* Don't drop points if we are running short of points */
1699 if (n_points + n_points_out > min_points + i)
1700 {
1701 if (tolerance > 0.0)
1702 {
1703 /* Only drop points that are within our tolerance */
1704 dsq = distance2d_sqr_pt_pt(last, pt);
1705 /* Allow any point but the last one to be dropped */
1706 if (!last_point && dsq <= tolsq)
1707 {
1708 continue;
1709 }
1710 }
1711 else
1712 {
1713 /* At tolerance zero, only skip exact dupes */
1714 if (memcmp((char*)pt, (char*)last, pt_size) == 0)
1715 continue;
1716 }
1717
1718 /* Got to last point, and it's not very different from */
1719 /* the point that preceded it. We want to keep the last */
1720 /* point, not the second-to-last one, so we pull our write */
1721 /* index back one value */
1722 if (last_point && n_points_out > 1 && tolerance > 0.0 && dsq <= tolsq)
1723 {
1724 n_points_out--;
1725 p_to = (char*)p_to - pt_size;
1726 }
1727 }
1728
1729 /* Compact all remaining values to front of array */
1730 memcpy(p_to, pt, pt_size);
1731 n_points_out++;
1732 p_to = (char*)p_to + pt_size;
1733 last = pt;
1734 }
1735 /* Adjust array length */
1736 pa->npoints = n_points_out;
1737 return;
1738}
static double distance2d_sqr_pt_pt(const POINT2D *p1, const POINT2D *p2)
Definition lwinline.h:33
static size_t ptarray_point_size(const POINTARRAY *pa)
Definition lwinline.h:56
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:97
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: