PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ lw_dist2d_arc_arc()

int lw_dist2d_arc_arc ( const POINT2D A1,
const POINT2D A2,
const POINT2D A3,
const POINT2D B1,
const POINT2D B2,
const POINT2D B3,
DISTPTS dl 
)

Definition at line 1586 of file measures.c.

1593 {
1594  POINT2D CA, CB; /* Center points of arcs A and B */
1595  double radius_A, radius_B, d; /* Radii of arcs A and B */
1596  POINT2D D; /* Mid-point between the centers CA and CB */
1597  int pt_in_arc_A, pt_in_arc_B; /* Test whether potential intersection point is within the arc */
1598 
1599  if (dl->mode != DIST_MIN)
1600  lwerror("lw_dist2d_arc_arc only supports mindistance");
1601 
1602  /* TODO: Handle case where arc is closed circle (A1 = A3) */
1603 
1604  /* What if one or both of our "arcs" is actually a point? */
1605  if (lw_arc_is_pt(B1, B2, B3) && lw_arc_is_pt(A1, A2, A3))
1606  return lw_dist2d_pt_pt(B1, A1, dl);
1607  else if (lw_arc_is_pt(B1, B2, B3))
1608  return lw_dist2d_pt_arc(B1, A1, A2, A3, dl);
1609  else if (lw_arc_is_pt(A1, A2, A3))
1610  return lw_dist2d_pt_arc(A1, B1, B2, B3, dl);
1611 
1612  /* Calculate centers and radii of circles. */
1613  radius_A = lw_arc_center(A1, A2, A3, &CA);
1614  radius_B = lw_arc_center(B1, B2, B3, &CB);
1615 
1616  /* Two co-linear arcs?!? That's two segments. */
1617  if (radius_A < 0 && radius_B < 0)
1618  return lw_dist2d_seg_seg(A1, A3, B1, B3, dl);
1619 
1620  /* A is co-linear, delegate to lw_dist_seg_arc here. */
1621  if (radius_A < 0)
1622  return lw_dist2d_seg_arc(A1, A3, B1, B2, B3, dl);
1623 
1624  /* B is co-linear, delegate to lw_dist_seg_arc here. */
1625  if (radius_B < 0)
1626  return lw_dist2d_seg_arc(B1, B3, A1, A2, A3, dl);
1627 
1628  /* Center-center distance */
1629  d = distance2d_pt_pt(&CA, &CB);
1630 
1631  /* Concentric arcs */
1632  if (FP_EQUALS(d, 0.0))
1633  return lw_dist2d_arc_arc_concentric(A1, A2, A3, radius_A, B1, B2, B3, radius_B, &CA, dl);
1634 
1635  /* Make sure that arc "A" has the bigger radius */
1636  if (radius_B > radius_A)
1637  {
1638  const POINT2D *tmp;
1639  POINT2D TP; /* Temporary point P */
1640  double td;
1641  tmp = B1;
1642  B1 = A1;
1643  A1 = tmp;
1644  tmp = B2;
1645  B2 = A2;
1646  A2 = tmp;
1647  tmp = B3;
1648  B3 = A3;
1649  A3 = tmp;
1650  TP = CB;
1651  CB = CA;
1652  CA = TP;
1653  td = radius_B;
1654  radius_B = radius_A;
1655  radius_A = td;
1656  }
1657 
1658  /* Circles touch at a point. Is that point within the arcs? */
1659  if (d == (radius_A + radius_B))
1660  {
1661  D.x = CA.x + (CB.x - CA.x) * radius_A / d;
1662  D.y = CA.y + (CB.y - CA.y) * radius_A / d;
1663 
1664  pt_in_arc_A = lw_pt_in_arc(&D, A1, A2, A3);
1665  pt_in_arc_B = lw_pt_in_arc(&D, B1, B2, B3);
1666 
1667  /* Arcs do touch at D, return it */
1668  if (pt_in_arc_A && pt_in_arc_B)
1669  {
1670  dl->distance = 0.0;
1671  dl->p1 = D;
1672  dl->p2 = D;
1673  return LW_TRUE;
1674  }
1675  }
1676  /* Disjoint or contained circles don't intersect. Closest point may be on */
1677  /* the line joining CA to CB. */
1678  else if (d > (radius_A + radius_B) /* Disjoint */ || d < (radius_A - radius_B) /* Contained */)
1679  {
1680  POINT2D XA, XB; /* Points where the line from CA to CB cross their circle bounds */
1681 
1682  /* Calculate hypothetical nearest points, the places on the */
1683  /* two circles where the center-center line crosses. If both */
1684  /* arcs contain their hypothetical points, that's the crossing distance */
1685  XA.x = CA.x + (CB.x - CA.x) * radius_A / d;
1686  XA.y = CA.y + (CB.y - CA.y) * radius_A / d;
1687  XB.x = CB.x + (CA.x - CB.x) * radius_B / d;
1688  XB.y = CB.y + (CA.y - CB.y) * radius_B / d;
1689 
1690  pt_in_arc_A = lw_pt_in_arc(&XA, A1, A2, A3);
1691  pt_in_arc_B = lw_pt_in_arc(&XB, B1, B2, B3);
1692 
1693  /* If the nearest points are both within the arcs, that's our answer */
1694  /* the shortest distance is at the nearest points */
1695  if (pt_in_arc_A && pt_in_arc_B)
1696  {
1697  return lw_dist2d_pt_pt(&XA, &XB, dl);
1698  }
1699  }
1700  /* Circles cross at two points, are either of those points in both arcs? */
1701  /* http://paulbourke.net/geometry/2circle/ */
1702  else if (d < (radius_A + radius_B))
1703  {
1704  POINT2D E, F; /* Points where circle(A) and circle(B) cross */
1705  /* Distance from CA to D */
1706  double a = (radius_A * radius_A - radius_B * radius_B + d * d) / (2 * d);
1707  /* Distance from D to E or F */
1708  double h = sqrt(radius_A * radius_A - a * a);
1709 
1710  /* Location of D */
1711  D.x = CA.x + (CB.x - CA.x) * a / d;
1712  D.y = CA.y + (CB.y - CA.y) * a / d;
1713 
1714  /* Start from D and project h units perpendicular to CA-D to get E */
1715  E.x = D.x + (D.y - CA.y) * h / a;
1716  E.y = D.y + (D.x - CA.x) * h / a;
1717 
1718  /* Crossing point E contained in arcs? */
1719  pt_in_arc_A = lw_pt_in_arc(&E, A1, A2, A3);
1720  pt_in_arc_B = lw_pt_in_arc(&E, B1, B2, B3);
1721 
1722  if (pt_in_arc_A && pt_in_arc_B)
1723  {
1724  dl->p1 = dl->p2 = E;
1725  dl->distance = 0.0;
1726  return LW_TRUE;
1727  }
1728 
1729  /* Start from D and project h units perpendicular to CA-D to get F */
1730  F.x = D.x - (D.y - CA.y) * h / a;
1731  F.y = D.y - (D.x - CA.x) * h / a;
1732 
1733  /* Crossing point F contained in arcs? */
1734  pt_in_arc_A = lw_pt_in_arc(&F, A1, A2, A3);
1735  pt_in_arc_B = lw_pt_in_arc(&F, B1, B2, B3);
1736 
1737  if (pt_in_arc_A && pt_in_arc_B)
1738  {
1739  dl->p1 = dl->p2 = F;
1740  dl->distance = 0.0;
1741  return LW_TRUE;
1742  }
1743  }
1744  else
1745  {
1746  lwerror("lw_dist2d_arc_arc: arcs neither touch, intersect nor are disjoint! INCONCEIVABLE!");
1747  return LW_FALSE;
1748  }
1749 
1750  /* Closest point is in the arc A, but not in the arc B, so */
1751  /* one of the B end points must be the closest. */
1752  if (pt_in_arc_A && !pt_in_arc_B)
1753  {
1754  lw_dist2d_pt_arc(B1, A1, A2, A3, dl);
1755  lw_dist2d_pt_arc(B3, A1, A2, A3, dl);
1756  return LW_TRUE;
1757  }
1758  /* Closest point is in the arc B, but not in the arc A, so */
1759  /* one of the A end points must be the closest. */
1760  else if (pt_in_arc_B && !pt_in_arc_A)
1761  {
1762  lw_dist2d_pt_arc(A1, B1, B2, B3, dl);
1763  lw_dist2d_pt_arc(A3, B1, B2, B3, dl);
1764  return LW_TRUE;
1765  }
1766  /* Finally, one of the end-point to end-point combos is the closest. */
1767  else
1768  {
1769  lw_dist2d_pt_pt(A1, B1, dl);
1770  lw_dist2d_pt_pt(A1, B3, dl);
1771  lw_dist2d_pt_pt(A3, B1, dl);
1772  lw_dist2d_pt_pt(A3, B3, dl);
1773  return LW_TRUE;
1774  }
1775 
1776  return LW_TRUE;
1777 }
#define LW_FALSE
Definition: liblwgeom.h:94
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:93
double lw_arc_center(const POINT2D *p1, const POINT2D *p2, const POINT2D *p3, POINT2D *result)
Determines the center of the circle defined by the three given points.
Definition: lwalgorithm.c:234
#define FP_EQUALS(A, B)
int lw_arc_is_pt(const POINT2D *A1, const POINT2D *A2, const POINT2D *A3)
Returns true if arc A is actually a point (all vertices are the same) .
Definition: lwalgorithm.c:111
int lw_pt_in_arc(const POINT2D *P, const POINT2D *A1, const POINT2D *A2, const POINT2D *A3)
Returns true if P is on the same side of the plane partition defined by A1/A3 as A2 is.
Definition: lwalgorithm.c:91
void void lwerror(const char *fmt,...) __attribute__((format(printf
Write a notice out to the error handler.
double distance2d_pt_pt(const POINT2D *p1, const POINT2D *p2)
Definition: measures.c:2445
int lw_dist2d_pt_arc(const POINT2D *P, const POINT2D *A1, const POINT2D *A2, const POINT2D *A3, DISTPTS *dl)
Definition: measures.c:1523
int lw_dist2d_arc_arc_concentric(const POINT2D *A1, const POINT2D *A2, const POINT2D *A3, double radius_A, const POINT2D *B1, const POINT2D *B2, const POINT2D *B3, double radius_B, const POINT2D *CENTER, DISTPTS *dl)
Definition: measures.c:1780
int lw_dist2d_seg_seg(const POINT2D *A, const POINT2D *B, const POINT2D *C, const POINT2D *D, DISTPTS *dl)
Finds the shortest distance between two segments.
Definition: measures.c:1927
int lw_dist2d_seg_arc(const POINT2D *A1, const POINT2D *A2, const POINT2D *B1, const POINT2D *B2, const POINT2D *B3, DISTPTS *dl)
Calculate the shortest distance between an arc and an edge.
Definition: measures.c:1373
int lw_dist2d_pt_pt(const POINT2D *thep1, const POINT2D *thep2, DISTPTS *dl)
Compares incoming points and stores the points closest to each other or most far away from each other...
Definition: measures.c:2413
#define DIST_MIN
Definition: measures.h:44
POINT2D p1
Definition: measures.h:52
POINT2D p2
Definition: measures.h:53
int mode
Definition: measures.h:54
double distance
Definition: measures.h:51
double y
Definition: liblwgeom.h:390
double x
Definition: liblwgeom.h:390

References DIST_MIN, DISTPTS::distance, distance2d_pt_pt(), FP_EQUALS, lw_arc_center(), lw_arc_is_pt(), lw_dist2d_arc_arc_concentric(), lw_dist2d_pt_arc(), lw_dist2d_pt_pt(), lw_dist2d_seg_arc(), lw_dist2d_seg_seg(), LW_FALSE, lw_pt_in_arc(), LW_TRUE, lwerror(), DISTPTS::mode, DISTPTS::p1, DISTPTS::p2, POINT2D::x, and POINT2D::y.

Referenced by lw_dist2d_ptarrayarc_ptarrayarc(), rect_leaf_node_distance(), rect_leaf_node_intersects(), and test_lw_dist2d_arc_arc().

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