PostGIS  3.4.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 1576 of file measures.c.

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