PostGIS  2.1.10dev-r@@SVN_REVISION@@
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 1750 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().

1751 {
1752  /*here we define two lists to hold our calculated "z"-values and the order number in the geometry*/
1753 
1754  double k, thevalue;
1755  float deltaX, deltaY, c1m, c2m;
1756  POINT2D c1, c2;
1757  const POINT2D *theP;
1758  float min1X, max1X, max1Y, min1Y,min2X, max2X, max2Y, min2Y;
1759  int t;
1760  int n1 = l1->npoints;
1761  int n2 = l2->npoints;
1762 
1763  LISTSTRUCT *list1, *list2;
1764  list1 = (LISTSTRUCT*)lwalloc(sizeof(LISTSTRUCT)*n1);
1765  list2 = (LISTSTRUCT*)lwalloc(sizeof(LISTSTRUCT)*n2);
1766 
1767  LWDEBUG(2, "lw_dist2d_fast_ptarray_ptarray is called");
1768 
1769  max1X = box1->xmax;
1770  min1X = box1->xmin;
1771  max1Y = box1->ymax;
1772  min1Y = box1->ymin;
1773  max2X = box2->xmax;
1774  min2X = box2->xmin;
1775  max2Y = box2->ymax;
1776  min2Y = box2->ymin;
1777  /*we want the center of the bboxes, and calculate the slope between the centerpoints*/
1778  c1.x = min1X + (max1X-min1X)/2;
1779  c1.y = min1Y + (max1Y-min1Y)/2;
1780  c2.x = min2X + (max2X-min2X)/2;
1781  c2.y = min2Y + (max2Y-min2Y)/2;
1782 
1783  deltaX=(c2.x-c1.x);
1784  deltaY=(c2.y-c1.y);
1785 
1786 
1787  /*Here we calculate where the line perpendicular to the center-center line crosses the axes for each vertex
1788  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 */
1789  if ((deltaX*deltaX)<(deltaY*deltaY)) /*North or South*/
1790  {
1791  k = -deltaX/deltaY;
1792  for (t=0; t<n1; t++) /*for each segment in L1 */
1793  {
1794  theP = getPoint2d_cp(l1, t);
1795  thevalue = theP->y - (k * theP->x);
1796  list1[t].themeasure=thevalue;
1797  list1[t].pnr=t;
1798 
1799  }
1800  for (t=0; t<n2; t++) /*for each segment in L2*/
1801  {
1802  theP = getPoint2d_cp(l2, t);
1803  thevalue = theP->y - (k * theP->x);
1804  list2[t].themeasure=thevalue;
1805  list2[t].pnr=t;
1806 
1807  }
1808  c1m = c1.y-(k*c1.x);
1809  c2m = c2.y-(k*c2.x);
1810  }
1811 
1812 
1813  /*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
1814  and we find it's crossing the X-axes with z = x-(1/k)y */
1815  else /*West or East*/
1816  {
1817  k = -deltaY/deltaX;
1818  for (t=0; t<n1; t++) /*for each segment in L1 */
1819  {
1820  theP = getPoint2d_cp(l1, t);
1821  thevalue = theP->x - (k * theP->y);
1822  list1[t].themeasure=thevalue;
1823  list1[t].pnr=t;
1824  /* lwnotice("l1 %d, measure=%f",t,thevalue ); */
1825  }
1826  for (t=0; t<n2; t++) /*for each segment in L2*/
1827  {
1828  theP = getPoint2d_cp(l2, t);
1829  thevalue = theP->x - (k * theP->y);
1830  list2[t].themeasure=thevalue;
1831  list2[t].pnr=t;
1832  /* lwnotice("l2 %d, measure=%f",t,thevalue ); */
1833  }
1834  c1m = c1.x-(k*c1.y);
1835  c2m = c2.x-(k*c2.y);
1836  }
1837 
1838  /*we sort our lists by the calculated values*/
1839  qsort(list1, n1, sizeof(LISTSTRUCT), struct_cmp_by_measure);
1840  qsort(list2, n2, sizeof(LISTSTRUCT), struct_cmp_by_measure);
1841 
1842  if (c1m < c2m)
1843  {
1844  if (!lw_dist2d_pre_seg_seg(l1,l2,list1,list2,k,dl))
1845  {
1846  lwfree(list1);
1847  lwfree(list2);
1848  return LW_FALSE;
1849  }
1850  }
1851  else
1852  {
1853  dl->twisted= ((dl->twisted) * (-1));
1854  if (!lw_dist2d_pre_seg_seg(l2,l1,list2,list1,k,dl))
1855  {
1856  lwfree(list1);
1857  lwfree(list2);
1858  return LW_FALSE;
1859  }
1860  }
1861  lwfree(list1);
1862  lwfree(list2);
1863  return LW_TRUE;
1864 }
void lwfree(void *mem)
Definition: lwutil.c:190
int npoints
Definition: liblwgeom.h:327
int struct_cmp_by_measure(const void *a, const void *b)
Definition: measures.c:1867
double xmax
Definition: liblwgeom.h:249
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:50
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:1878
double themeasure
Definition: measures.h:33
double x
Definition: liblwgeom.h:284
double ymin
Definition: liblwgeom.h:250
double xmin
Definition: liblwgeom.h:248
#define LW_FALSE
Definition: liblwgeom.h:52
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:458
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:51
double ymax
Definition: liblwgeom.h:251
double y
Definition: liblwgeom.h:284
int twisted
Definition: measures.h:27
void * lwalloc(size_t size)
Definition: lwutil.c:175
int pnr
Definition: measures.h:34

Here is the call graph for this function:

Here is the caller graph for this function: