PostGIS  3.4.0dev-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 2036 of file measures.c.

2037 {
2038  /*here we define two lists to hold our calculated "z"-values and the order number in the geometry*/
2039 
2040  double k, thevalue;
2041  float deltaX, deltaY, c1m, c2m;
2042  POINT2D c1, c2;
2043  const POINT2D *theP;
2044  float min1X, max1X, max1Y, min1Y, min2X, max2X, max2Y, min2Y;
2045  int t;
2046  int n1 = l1->npoints;
2047  int n2 = l2->npoints;
2048 
2049  LISTSTRUCT *list1, *list2;
2050  list1 = (LISTSTRUCT *)lwalloc(sizeof(LISTSTRUCT) * n1);
2051  list2 = (LISTSTRUCT *)lwalloc(sizeof(LISTSTRUCT) * n2);
2052 
2053  LWDEBUG(2, "lw_dist2d_fast_ptarray_ptarray is called");
2054 
2055  max1X = box1->xmax;
2056  min1X = box1->xmin;
2057  max1Y = box1->ymax;
2058  min1Y = box1->ymin;
2059  max2X = box2->xmax;
2060  min2X = box2->xmin;
2061  max2Y = box2->ymax;
2062  min2Y = box2->ymin;
2063  /*we want the center of the bboxes, and calculate the slope between the centerpoints*/
2064  c1.x = min1X + (max1X - min1X) / 2;
2065  c1.y = min1Y + (max1Y - min1Y) / 2;
2066  c2.x = min2X + (max2X - min2X) / 2;
2067  c2.y = min2Y + (max2Y - min2Y) / 2;
2068 
2069  deltaX = (c2.x - c1.x);
2070  deltaY = (c2.y - c1.y);
2071 
2072  /*Here we calculate where the line perpendicular to the center-center line crosses the axes for each vertex
2073  if the center-center line is vertical the perpendicular line will be horizontal and we find it's crossing the
2074  Y-axes with z = y-kx */
2075  if ((deltaX * deltaX) < (deltaY * deltaY)) /*North or South*/
2076  {
2077  k = -deltaX / deltaY;
2078  for (t = 0; t < n1; t++) /*for each segment in L1 */
2079  {
2080  theP = getPoint2d_cp(l1, t);
2081  thevalue = theP->y - (k * theP->x);
2082  list1[t].themeasure = thevalue;
2083  list1[t].pnr = t;
2084  }
2085  for (t = 0; t < n2; t++) /*for each segment in L2*/
2086  {
2087  theP = getPoint2d_cp(l2, t);
2088  thevalue = theP->y - (k * theP->x);
2089  list2[t].themeasure = thevalue;
2090  list2[t].pnr = t;
2091  }
2092  c1m = c1.y - (k * c1.x);
2093  c2m = c2.y - (k * c2.x);
2094  }
2095 
2096  /*if the center-center line is horizontal the perpendicular line will be vertical. To eliminate problems with
2097  dividing by zero we are here mirroring the coordinate-system and we find it's crossing the X-axes with z =
2098  x-(1/k)y */
2099  else /*West or East*/
2100  {
2101  k = -deltaY / deltaX;
2102  for (t = 0; t < n1; t++) /*for each segment in L1 */
2103  {
2104  theP = getPoint2d_cp(l1, t);
2105  thevalue = theP->x - (k * theP->y);
2106  list1[t].themeasure = thevalue;
2107  list1[t].pnr = t;
2108  /* lwnotice("l1 %d, measure=%f",t,thevalue ); */
2109  }
2110  for (t = 0; t < n2; t++) /*for each segment in L2*/
2111  {
2112  theP = getPoint2d_cp(l2, t);
2113  thevalue = theP->x - (k * theP->y);
2114  list2[t].themeasure = thevalue;
2115  list2[t].pnr = t;
2116  /* lwnotice("l2 %d, measure=%f",t,thevalue ); */
2117  }
2118  c1m = c1.x - (k * c1.y);
2119  c2m = c2.x - (k * c2.y);
2120  }
2121 
2122  /*we sort our lists by the calculated values*/
2123  qsort(list1, n1, sizeof(LISTSTRUCT), struct_cmp_by_measure);
2124  qsort(list2, n2, sizeof(LISTSTRUCT), struct_cmp_by_measure);
2125 
2126  if (c1m < c2m)
2127  {
2128  if (!lw_dist2d_pre_seg_seg(l1, l2, list1, list2, k, dl))
2129  {
2130  lwfree(list1);
2131  lwfree(list2);
2132  return LW_FALSE;
2133  }
2134  }
2135  else
2136  {
2137  dl->twisted = ((dl->twisted) * (-1));
2138  if (!lw_dist2d_pre_seg_seg(l2, l1, list2, list1, k, dl))
2139  {
2140  lwfree(list1);
2141  lwfree(list2);
2142  return LW_FALSE;
2143  }
2144  }
2145  lwfree(list1);
2146  lwfree(list2);
2147  return LW_TRUE;
2148 }
#define LW_FALSE
Definition: liblwgeom.h:94
void lwfree(void *mem)
Definition: lwutil.c:242
void * lwalloc(size_t size)
Definition: lwutil.c:227
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:93
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
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
int struct_cmp_by_measure(const void *a, const void *b)
Definition: measures.c:2151
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:2160
int twisted
Definition: measures.h:55
double ymax
Definition: liblwgeom.h:357
double xmax
Definition: liblwgeom.h:355
double ymin
Definition: liblwgeom.h:356
double xmin
Definition: liblwgeom.h:354
int pnr
Definition: measures.h:62
double themeasure
Definition: measures.h:61
double y
Definition: liblwgeom.h:390
double x
Definition: liblwgeom.h:390
uint32_t npoints
Definition: liblwgeom.h:427

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: