PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ lw_dist2d_fast_ptarray_ptarray()

int lw_dist2d_fast_ptarray_ptarray ( POINTARRAY l1,
POINTARRAY l2,
DISTPTS dl,
GBOX box1,
GBOX box2 
)

The new faster calculation comparing pointarray to another pointarray the arrays can come from both polygons and linestrings.

The naming is not good but comes from that it compares a chosen selection of the points not all of them

Definition at line 1944 of file measures.c.

1945 {
1946  /*here we define two lists to hold our calculated "z"-values and the order number in the geometry*/
1947 
1948  double k, thevalue;
1949  float deltaX, deltaY, c1m, c2m;
1950  POINT2D c1, c2;
1951  const POINT2D *theP;
1952  float min1X, max1X, max1Y, min1Y,min2X, max2X, max2Y, min2Y;
1953  int t;
1954  int n1 = l1->npoints;
1955  int n2 = l2->npoints;
1956 
1957  LISTSTRUCT *list1, *list2;
1958  list1 = (LISTSTRUCT*)lwalloc(sizeof(LISTSTRUCT)*n1);
1959  list2 = (LISTSTRUCT*)lwalloc(sizeof(LISTSTRUCT)*n2);
1960 
1961  LWDEBUG(2, "lw_dist2d_fast_ptarray_ptarray is called");
1962 
1963  max1X = box1->xmax;
1964  min1X = box1->xmin;
1965  max1Y = box1->ymax;
1966  min1Y = box1->ymin;
1967  max2X = box2->xmax;
1968  min2X = box2->xmin;
1969  max2Y = box2->ymax;
1970  min2Y = box2->ymin;
1971  /*we want the center of the bboxes, and calculate the slope between the centerpoints*/
1972  c1.x = min1X + (max1X-min1X)/2;
1973  c1.y = min1Y + (max1Y-min1Y)/2;
1974  c2.x = min2X + (max2X-min2X)/2;
1975  c2.y = min2Y + (max2Y-min2Y)/2;
1976 
1977  deltaX=(c2.x-c1.x);
1978  deltaY=(c2.y-c1.y);
1979 
1980 
1981  /*Here we calculate where the line perpendicular to the center-center line crosses the axes for each vertex
1982  if the center-center line is vertical the perpendicular line will be horizontal and we find it's crossing the Y-axes with z = y-kx */
1983  if ((deltaX*deltaX)<(deltaY*deltaY)) /*North or South*/
1984  {
1985  k = -deltaX/deltaY;
1986  for (t=0; t<n1; t++) /*for each segment in L1 */
1987  {
1988  theP = getPoint2d_cp(l1, t);
1989  thevalue = theP->y - (k * theP->x);
1990  list1[t].themeasure=thevalue;
1991  list1[t].pnr=t;
1992 
1993  }
1994  for (t=0; t<n2; t++) /*for each segment in L2*/
1995  {
1996  theP = getPoint2d_cp(l2, t);
1997  thevalue = theP->y - (k * theP->x);
1998  list2[t].themeasure=thevalue;
1999  list2[t].pnr=t;
2000 
2001  }
2002  c1m = c1.y-(k*c1.x);
2003  c2m = c2.y-(k*c2.x);
2004  }
2005 
2006 
2007  /*if the center-center line is horizontal the perpendicular line will be vertical. To eliminate problems with dividing by zero we are here mirroring the coordinate-system
2008  and we find it's crossing the X-axes with z = x-(1/k)y */
2009  else /*West or East*/
2010  {
2011  k = -deltaY/deltaX;
2012  for (t=0; t<n1; t++) /*for each segment in L1 */
2013  {
2014  theP = getPoint2d_cp(l1, t);
2015  thevalue = theP->x - (k * theP->y);
2016  list1[t].themeasure=thevalue;
2017  list1[t].pnr=t;
2018  /* lwnotice("l1 %d, measure=%f",t,thevalue ); */
2019  }
2020  for (t=0; t<n2; t++) /*for each segment in L2*/
2021  {
2022  theP = getPoint2d_cp(l2, t);
2023  thevalue = theP->x - (k * theP->y);
2024  list2[t].themeasure=thevalue;
2025  list2[t].pnr=t;
2026  /* lwnotice("l2 %d, measure=%f",t,thevalue ); */
2027  }
2028  c1m = c1.x-(k*c1.y);
2029  c2m = c2.x-(k*c2.y);
2030  }
2031 
2032  /*we sort our lists by the calculated values*/
2033  qsort(list1, n1, sizeof(LISTSTRUCT), struct_cmp_by_measure);
2034  qsort(list2, n2, sizeof(LISTSTRUCT), struct_cmp_by_measure);
2035 
2036  if (c1m < c2m)
2037  {
2038  if (!lw_dist2d_pre_seg_seg(l1,l2,list1,list2,k,dl))
2039  {
2040  lwfree(list1);
2041  lwfree(list2);
2042  return LW_FALSE;
2043  }
2044  }
2045  else
2046  {
2047  dl->twisted= ((dl->twisted) * (-1));
2048  if (!lw_dist2d_pre_seg_seg(l2,l1,list2,list1,k,dl))
2049  {
2050  lwfree(list1);
2051  lwfree(list2);
2052  return LW_FALSE;
2053  }
2054  }
2055  lwfree(list1);
2056  lwfree(list2);
2057  return LW_TRUE;
2058 }
#define LW_FALSE
Definition: liblwgeom.h:77
void lwfree(void *mem)
Definition: lwutil.c:244
void * lwalloc(size_t size)
Definition: lwutil.c:229
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:76
const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from.
Definition: lwgeom_api.c:374
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
int struct_cmp_by_measure(const void *a, const void *b)
Definition: measures.c:2061
int lw_dist2d_pre_seg_seg(POINTARRAY *l1, POINTARRAY *l2, LISTSTRUCT *list1, LISTSTRUCT *list2, double k, DISTPTS *dl)
preparation before lw_dist2d_seg_seg.
Definition: measures.c:2073
int twisted
Definition: measures.h:55
double ymax
Definition: liblwgeom.h:298
double xmax
Definition: liblwgeom.h:296
double ymin
Definition: liblwgeom.h:297
double xmin
Definition: liblwgeom.h:295
int pnr
Definition: measures.h:62
double themeasure
Definition: measures.h:61
double y
Definition: liblwgeom.h:331
double x
Definition: liblwgeom.h:331
uint32_t npoints
Definition: liblwgeom.h:374

References getPoint2d_cp(), lw_dist2d_pre_seg_seg(), LW_FALSE, LW_TRUE, lwalloc(), LWDEBUG, lwfree(), POINTARRAY::npoints, LISTSTRUCT::pnr, struct_cmp_by_measure(), LISTSTRUCT::themeasure, DISTPTS::twisted, POINT2D::x, GBOX::xmax, GBOX::xmin, POINT2D::y, GBOX::ymax, and GBOX::ymin.

Referenced by lw_dist2d_distribute_fast().

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