PostGIS  2.5.7dev-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 1497 of file measures.c.

1500 {
1501  POINT2D CA, CB; /* Center points of arcs A and B */
1502  double radius_A, radius_B, d; /* Radii of arcs A and B */
1503  POINT2D D; /* Mid-point between the centers CA and CB */
1504  int pt_in_arc_A, pt_in_arc_B; /* Test whether potential intersection point is within the arc */
1505 
1506  if ( dl->mode != DIST_MIN )
1507  lwerror("lw_dist2d_arc_arc only supports mindistance");
1508 
1509  /* TODO: Handle case where arc is closed circle (A1 = A3) */
1510 
1511  /* What if one or both of our "arcs" is actually a point? */
1512  if ( lw_arc_is_pt(B1, B2, B3) && lw_arc_is_pt(A1, A2, A3) )
1513  return lw_dist2d_pt_pt(B1, A1, dl);
1514  else if ( lw_arc_is_pt(B1, B2, B3) )
1515  return lw_dist2d_pt_arc(B1, A1, A2, A3, dl);
1516  else if ( lw_arc_is_pt(A1, A2, A3) )
1517  return lw_dist2d_pt_arc(A1, B1, B2, B3, dl);
1518 
1519  /* Calculate centers and radii of circles. */
1520  radius_A = lw_arc_center(A1, A2, A3, &CA);
1521  radius_B = lw_arc_center(B1, B2, B3, &CB);
1522 
1523  /* Two co-linear arcs?!? That's two segments. */
1524  if ( radius_A < 0 && radius_B < 0 )
1525  return lw_dist2d_seg_seg(A1, A3, B1, B3, dl);
1526 
1527  /* A is co-linear, delegate to lw_dist_seg_arc here. */
1528  if ( radius_A < 0 )
1529  return lw_dist2d_seg_arc(A1, A3, B1, B2, B3, dl);
1530 
1531  /* B is co-linear, delegate to lw_dist_seg_arc here. */
1532  if ( radius_B < 0 )
1533  return lw_dist2d_seg_arc(B1, B3, A1, A2, A3, dl);
1534 
1535  /* Center-center distance */
1536  d = distance2d_pt_pt(&CA, &CB);
1537 
1538  /* Concentric arcs */
1539  if ( FP_EQUALS(d, 0.0) )
1540  return lw_dist2d_arc_arc_concentric(A1, A2, A3, radius_A,
1541  B1, B2, B3, radius_B,
1542  &CA, dl);
1543 
1544  /* Make sure that arc "A" has the bigger radius */
1545  if ( radius_B > radius_A )
1546  {
1547  const POINT2D *tmp;
1548  POINT2D TP; /* Temporary point P */
1549  double td;
1550  tmp = B1; B1 = A1; A1 = tmp;
1551  tmp = B2; B2 = A2; A2 = tmp;
1552  tmp = B3; B3 = A3; A3 = tmp;
1553  TP = CB; CB = CA; CA = TP;
1554  td = radius_B; radius_B = radius_A; radius_A = td;
1555  }
1556 
1557  /* Circles touch at a point. Is that point within the arcs? */
1558  if ( d == (radius_A + radius_B) )
1559  {
1560  D.x = CA.x + (CB.x - CA.x) * radius_A / d;
1561  D.y = CA.y + (CB.y - CA.y) * radius_A / d;
1562 
1563  pt_in_arc_A = lw_pt_in_arc(&D, A1, A2, A3);
1564  pt_in_arc_B = lw_pt_in_arc(&D, B1, B2, B3);
1565 
1566  /* Arcs do touch at D, return it */
1567  if ( pt_in_arc_A && pt_in_arc_B )
1568  {
1569  dl->distance = 0.0;
1570  dl->p1 = D;
1571  dl->p2 = D;
1572  return LW_TRUE;
1573  }
1574  }
1575  /* Disjoint or contained circles don't intersect. Closest point may be on */
1576  /* the line joining CA to CB. */
1577  else if ( d > (radius_A + radius_B) /* Disjoint */ || d < (radius_A - radius_B) /* Contained */ )
1578  {
1579  POINT2D XA, XB; /* Points where the line from CA to CB cross their circle bounds */
1580 
1581  /* Calculate hypothetical nearest points, the places on the */
1582  /* two circles where the center-center line crosses. If both */
1583  /* arcs contain their hypothetical points, that's the crossing distance */
1584  XA.x = CA.x + (CB.x - CA.x) * radius_A / d;
1585  XA.y = CA.y + (CB.y - CA.y) * radius_A / d;
1586  XB.x = CB.x + (CA.x - CB.x) * radius_B / d;
1587  XB.y = CB.y + (CA.y - CB.y) * radius_B / d;
1588 
1589  pt_in_arc_A = lw_pt_in_arc(&XA, A1, A2, A3);
1590  pt_in_arc_B = lw_pt_in_arc(&XB, B1, B2, B3);
1591 
1592  /* If the nearest points are both within the arcs, that's our answer */
1593  /* the shortest distance is at the nearest points */
1594  if ( pt_in_arc_A && pt_in_arc_B )
1595  {
1596  return lw_dist2d_pt_pt(&XA, &XB, dl);
1597  }
1598  }
1599  /* Circles cross at two points, are either of those points in both arcs? */
1600  /* http://paulbourke.net/geometry/2circle/ */
1601  else if ( d < (radius_A + radius_B) )
1602  {
1603  POINT2D E, F; /* Points where circle(A) and circle(B) cross */
1604  /* Distance from CA to D */
1605  double a = (radius_A*radius_A - radius_B*radius_B + d*d) / (2*d);
1606  /* Distance from D to E or F */
1607  double h = sqrt(radius_A*radius_A - a*a);
1608 
1609  /* Location of D */
1610  D.x = CA.x + (CB.x - CA.x) * a / d;
1611  D.y = CA.y + (CB.y - CA.y) * a / d;
1612 
1613  /* Start from D and project h units perpendicular to CA-D to get E */
1614  E.x = D.x + (D.y - CA.y) * h / a;
1615  E.y = D.y + (D.x - CA.x) * h / a;
1616 
1617  /* Crossing point E contained in arcs? */
1618  pt_in_arc_A = lw_pt_in_arc(&E, A1, A2, A3);
1619  pt_in_arc_B = lw_pt_in_arc(&E, B1, B2, B3);
1620 
1621  if ( pt_in_arc_A && pt_in_arc_B )
1622  {
1623  dl->p1 = dl->p2 = E;
1624  dl->distance = 0.0;
1625  return LW_TRUE;
1626  }
1627 
1628  /* Start from D and project h units perpendicular to CA-D to get F */
1629  F.x = D.x - (D.y - CA.y) * h / a;
1630  F.y = D.y - (D.x - CA.x) * h / a;
1631 
1632  /* Crossing point F contained in arcs? */
1633  pt_in_arc_A = lw_pt_in_arc(&F, A1, A2, A3);
1634  pt_in_arc_B = lw_pt_in_arc(&F, B1, B2, B3);
1635 
1636  if ( pt_in_arc_A && pt_in_arc_B )
1637  {
1638  dl->p1 = dl->p2 = F;
1639  dl->distance = 0.0;
1640  return LW_TRUE;
1641  }
1642  }
1643  else
1644  {
1645  lwerror("lw_dist2d_arc_arc: arcs neither touch, intersect nor are disjoint! INCONCEIVABLE!");
1646  return LW_FALSE;
1647  }
1648 
1649  /* Closest point is in the arc A, but not in the arc B, so */
1650  /* one of the B end points must be the closest. */
1651  if (pt_in_arc_A && !pt_in_arc_B)
1652  {
1653  lw_dist2d_pt_arc(B1, A1, A2, A3, dl);
1654  lw_dist2d_pt_arc(B3, A1, A2, A3, dl);
1655  return LW_TRUE;
1656  }
1657  /* Closest point is in the arc B, but not in the arc A, so */
1658  /* one of the A end points must be the closest. */
1659  else if ( pt_in_arc_B && ! pt_in_arc_A )
1660  {
1661  lw_dist2d_pt_arc(A1, B1, B2, B3, dl);
1662  lw_dist2d_pt_arc(A3, B1, B2, B3, dl);
1663  return LW_TRUE;
1664  }
1665  /* Finally, one of the end-point to end-point combos is the closest. */
1666  else
1667  {
1668  lw_dist2d_pt_pt(A1, B1, dl);
1669  lw_dist2d_pt_pt(A1, B3, dl);
1670  lw_dist2d_pt_pt(A3, B1, dl);
1671  lw_dist2d_pt_pt(A3, B3, dl);
1672  return LW_TRUE;
1673  }
1674 
1675  return LW_TRUE;
1676 }
#define LW_FALSE
Definition: liblwgeom.h:77
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:76
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:228
#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:105
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:85
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:2314
int lw_dist2d_pt_arc(const POINT2D *P, const POINT2D *A1, const POINT2D *A2, const POINT2D *A3, DISTPTS *dl)
Definition: measures.c:1439
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:1679
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:1821
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:1292
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:2282
#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:331
double x
Definition: liblwgeom.h:331

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: