PostGIS  3.3.9dev-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 1593 of file ptarray.c.

1594 {
1595  uint32_t split = it_first;
1596  if ((it_first - it_last) < 2)
1597  return it_first;
1598 
1599  const POINT2D *A = getPoint2d_cp(pts, it_first);
1600  const POINT2D *B = getPoint2d_cp(pts, it_last);
1601 
1602  if (distance2d_sqr_pt_pt(A, B) < DBL_EPSILON)
1603  {
1604  /* If p1 == p2, we can just calculate the distance from each point to A */
1605  for (uint32_t itk = it_first + 1; itk < it_last; itk++)
1606  {
1607  const POINT2D *pk = getPoint2d_cp(pts, itk);
1608  double distance_sqr = distance2d_sqr_pt_pt(pk, A);
1609  if (distance_sqr > max_distance_sqr)
1610  {
1611  split = itk;
1612  max_distance_sqr = distance_sqr;
1613  }
1614  }
1615  return split;
1616  }
1617 
1618  /* This is based on distance2d_sqr_pt_seg, but heavily inlined here to avoid recalculations */
1619  double ba_x = (B->x - A->x);
1620  double ba_y = (B->y - A->y);
1621  double ab_length_sqr = (ba_x * ba_x + ba_y * ba_y);
1622  /* To avoid the division by ab_length_sqr in the 3rd path, we normalize here
1623  * and multiply in the first two paths [(dot_ac_ab < 0) and (> ab_length_sqr)] */
1624  max_distance_sqr *= ab_length_sqr;
1625  for (uint32_t itk = it_first + 1; itk < it_last; itk++)
1626  {
1627  const POINT2D *C = getPoint2d_cp(pts, itk);
1628  double distance_sqr;
1629  double ca_x = (C->x - A->x);
1630  double ca_y = (C->y - A->y);
1631  double dot_ac_ab = (ca_x * ba_x + ca_y * ba_y);
1632 
1633  if (dot_ac_ab <= 0.0)
1634  {
1635  distance_sqr = distance2d_sqr_pt_pt(C, A) * ab_length_sqr;
1636  }
1637  else if (dot_ac_ab >= ab_length_sqr)
1638  {
1639  distance_sqr = distance2d_sqr_pt_pt(C, B) * ab_length_sqr;
1640  }
1641  else
1642  {
1643  double s_numerator = ca_x * ba_y - ca_y * ba_x;
1644  distance_sqr = s_numerator * s_numerator; /* Missing division by ab_length_sqr on purpose */
1645  }
1646 
1647  if (distance_sqr > max_distance_sqr)
1648  {
1649  split = itk;
1650  max_distance_sqr = distance_sqr;
1651  }
1652  }
1653  return split;
1654 }
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:101
static double distance2d_sqr_pt_pt(const POINT2D *p1, const POINT2D *p2)
Definition: lwinline.h:35
double y
Definition: liblwgeom.h:405
double x
Definition: liblwgeom.h:405

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: