PostGIS  2.3.8dev-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 1942 of file measures.c.

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().

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