PostGIS  2.5.0dev-r@@SVN_REVISION@@
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 1493 of file measures.c.

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(), and test_lw_dist2d_arc_arc().

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

Here is the call graph for this function:

Here is the caller graph for this function: