PostGIS  3.2.2dev-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 1873 of file ptarray.c.

1874 {
1875  uint32_t split = it_first;
1876  if ((it_first - it_last) < 2)
1877  return it_first;
1878 
1879  const POINT2D *A = getPoint2d_cp(pts, it_first);
1880  const POINT2D *B = getPoint2d_cp(pts, it_last);
1881 
1882  if (distance2d_sqr_pt_pt(A, B) < DBL_EPSILON)
1883  {
1884  /* If p1 == p2, we can just calculate the distance from each point to A */
1885  for (uint32_t itk = it_first + 1; itk < it_last; itk++)
1886  {
1887  const POINT2D *pk = getPoint2d_cp(pts, itk);
1888  double distance_sqr = distance2d_sqr_pt_pt(pk, A);
1889  if (distance_sqr > max_distance_sqr)
1890  {
1891  split = itk;
1892  max_distance_sqr = distance_sqr;
1893  }
1894  }
1895  return split;
1896  }
1897 
1898  /* This is based on distance2d_sqr_pt_seg, but heavily inlined here to avoid recalculations */
1899  double ba_x = (B->x - A->x);
1900  double ba_y = (B->y - A->y);
1901  double ab_length_sqr = (ba_x * ba_x + ba_y * ba_y);
1902  /* To avoid the division by ab_length_sqr in the 3rd path, we normalize here
1903  * and multiply in the first two paths [(dot_ac_ab < 0) and (> ab_length_sqr)] */
1904  max_distance_sqr *= ab_length_sqr;
1905  for (uint32_t itk = it_first + 1; itk < it_last; itk++)
1906  {
1907  const POINT2D *C = getPoint2d_cp(pts, itk);
1908  double distance_sqr;
1909  double ca_x = (C->x - A->x);
1910  double ca_y = (C->y - A->y);
1911  double dot_ac_ab = (ca_x * ba_x + ca_y * ba_y);
1912 
1913  if (dot_ac_ab <= 0.0)
1914  {
1915  distance_sqr = distance2d_sqr_pt_pt(C, A) * ab_length_sqr;
1916  }
1917  else if (dot_ac_ab >= ab_length_sqr)
1918  {
1919  distance_sqr = distance2d_sqr_pt_pt(C, B) * ab_length_sqr;
1920  }
1921  else
1922  {
1923  double s_numerator = ca_x * ba_y - ca_y * ba_x;
1924  distance_sqr = s_numerator * s_numerator; /* Missing division by ab_length_sqr on purpose */
1925  }
1926 
1927  if (distance_sqr > max_distance_sqr)
1928  {
1929  split = itk;
1930  max_distance_sqr = distance_sqr;
1931  }
1932  }
1933  return split;
1934 }
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:404
double x
Definition: liblwgeom.h:404

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: