PostGIS  3.6.1dev-r@@SVN_REVISION@@

◆ ptarray_dp_findsplit_in_place()

static uint32_t ptarray_dp_findsplit_in_place ( const POINTARRAY pts,
uint32_t  it_first,
uint32_t  it_last,
double  max_distance_sqr 
)
static

Definition at line 1733 of file ptarray.c.

1734 {
1735  uint32_t split = it_first;
1736  if ((it_first - it_last) < 2)
1737  return it_first;
1738 
1739  const POINT2D *A = getPoint2d_cp(pts, it_first);
1740  const POINT2D *B = getPoint2d_cp(pts, it_last);
1741 
1742  if (distance2d_sqr_pt_pt(A, B) < DBL_EPSILON)
1743  {
1744  /* If p1 == p2, we can just calculate the distance from each point to A */
1745  for (uint32_t itk = it_first + 1; itk < it_last; itk++)
1746  {
1747  const POINT2D *pk = getPoint2d_cp(pts, itk);
1748  double distance_sqr = distance2d_sqr_pt_pt(pk, A);
1749  if (distance_sqr > max_distance_sqr)
1750  {
1751  split = itk;
1752  max_distance_sqr = distance_sqr;
1753  }
1754  }
1755  return split;
1756  }
1757 
1758  /* This is based on distance2d_sqr_pt_seg, but heavily inlined here to avoid recalculations */
1759  double ba_x = (B->x - A->x);
1760  double ba_y = (B->y - A->y);
1761  double ab_length_sqr = (ba_x * ba_x + ba_y * ba_y);
1762  /* To avoid the division by ab_length_sqr in the 3rd path, we normalize here
1763  * and multiply in the first two paths [(dot_ac_ab < 0) and (> ab_length_sqr)] */
1764  max_distance_sqr *= ab_length_sqr;
1765  for (uint32_t itk = it_first + 1; itk < it_last; itk++)
1766  {
1767  const POINT2D *C = getPoint2d_cp(pts, itk);
1768  double distance_sqr;
1769  double ca_x = (C->x - A->x);
1770  double ca_y = (C->y - A->y);
1771  double dot_ac_ab = (ca_x * ba_x + ca_y * ba_y);
1772 
1773  if (dot_ac_ab <= 0.0)
1774  {
1775  distance_sqr = distance2d_sqr_pt_pt(C, A) * ab_length_sqr;
1776  }
1777  else if (dot_ac_ab >= ab_length_sqr)
1778  {
1779  distance_sqr = distance2d_sqr_pt_pt(C, B) * ab_length_sqr;
1780  }
1781  else
1782  {
1783  double s_numerator = ca_x * ba_y - ca_y * ba_x;
1784  distance_sqr = s_numerator * s_numerator; /* Missing division by ab_length_sqr on purpose */
1785  }
1786 
1787  if (distance_sqr > max_distance_sqr)
1788  {
1789  split = itk;
1790  max_distance_sqr = distance_sqr;
1791  }
1792  }
1793  return split;
1794 }
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
static double distance2d_sqr_pt_pt(const POINT2D *p1, const POINT2D *p2)
Definition: lwinline.h:33
double y
Definition: liblwgeom.h:390
double x
Definition: liblwgeom.h:390

References distance2d_sqr_pt_pt(), getPoint2d_cp(), POINT2D::x, and POINT2D::y.

Referenced by ptarray_simplify_in_place().

Here is the call graph for this function:
Here is the caller graph for this function: