PostGIS  2.1.10dev-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 1438 of file measures.c.

References DIST_MIN, DISTPTS::distance, distance2d_pt_pt(), FP_EQUALS, lw_arc_center(), lw_arc_is_pt(), 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().

1441 {
1442  POINT2D CA, CB; /* Center points of arcs A and B */
1443  double radius_A, radius_B, d; /* Radii of arcs A and B */
1444  POINT2D P; /* Temporary point P */
1445  POINT2D D; /* Mid-point between the centers CA and CB */
1446  int pt_in_arc_A, pt_in_arc_B; /* Test whether potential intersection point is within the arc */
1447 
1448  if ( dl->mode != DIST_MIN )
1449  lwerror("lw_dist2d_arc_arc only supports mindistance");
1450 
1451  /* TODO: Handle case where arc is closed circle (A1 = A3) */
1452 
1453  /* What if one or both of our "arcs" is actually a point? */
1454  if ( lw_arc_is_pt(B1, B2, B3) && lw_arc_is_pt(A1, A2, A3) )
1455  return lw_dist2d_pt_pt(B1, A1, dl);
1456  else if ( lw_arc_is_pt(B1, B2, B3) )
1457  return lw_dist2d_pt_arc(B1, A1, A2, A3, dl);
1458  else if ( lw_arc_is_pt(A1, A2, A3) )
1459  return lw_dist2d_pt_arc(A1, B1, B2, B3, dl);
1460 
1461  /* Calculate centers and radii of circles. */
1462  radius_A = lw_arc_center(A1, A2, A3, &CA);
1463  radius_B = lw_arc_center(B1, B2, B3, &CB);
1464 
1465  /* Two co-linear arcs?!? That's two segments. */
1466  if ( radius_A < 0 && radius_B < 0 )
1467  return lw_dist2d_seg_seg(A1, A3, B1, B3, dl);
1468 
1469  /* A is co-linear, delegate to lw_dist_seg_arc here. */
1470  if ( radius_A < 0 )
1471  return lw_dist2d_seg_arc(A1, A3, B1, B2, B3, dl);
1472 
1473  /* B is co-linear, delegate to lw_dist_seg_arc here. */
1474  if ( radius_B < 0 )
1475  return lw_dist2d_seg_arc(B1, B3, A1, A2, A3, dl);
1476 
1477  /* Make sure that arc "A" has the bigger radius */
1478  if ( radius_B > radius_A )
1479  {
1480  const POINT2D *tmp;
1481  tmp = B1; B1 = A1; A1 = tmp;
1482  tmp = B2; B2 = A2; A2 = tmp;
1483  tmp = B3; B3 = A3; A3 = tmp;
1484  P = CB; CB = CA; CA = P;
1485  d = radius_B; radius_B = radius_A; radius_A = d;
1486  }
1487 
1488  /* Center-center distance */
1489  d = distance2d_pt_pt(&CA, &CB);
1490 
1491  /* Equal circles. Arcs may intersect at multiple points, or at none! */
1492  if ( FP_EQUALS(d, 0.0) && FP_EQUALS(radius_A, radius_B) )
1493  {
1494  lwerror("lw_dist2d_arc_arc can't handle cojoint circles, uh oh");
1495  }
1496 
1497  /* Circles touch at a point. Is that point within the arcs? */
1498  if ( d == (radius_A + radius_B) )
1499  {
1500  D.x = CA.x + (CB.x - CA.x) * radius_A / d;
1501  D.y = CA.y + (CB.y - CA.y) * radius_A / d;
1502 
1503  pt_in_arc_A = lw_pt_in_arc(&D, A1, A2, A3);
1504  pt_in_arc_B = lw_pt_in_arc(&D, B1, B2, B3);
1505 
1506  /* Arcs do touch at D, return it */
1507  if ( pt_in_arc_A && pt_in_arc_B )
1508  {
1509  dl->distance = 0.0;
1510  dl->p1 = D;
1511  dl->p2 = D;
1512  return LW_TRUE;
1513  }
1514  }
1515  /* Disjoint or contained circles don't intersect. Closest point may be on */
1516  /* the line joining CA to CB. */
1517  else if ( d > (radius_A + radius_B) /* Disjoint */ || d < (radius_A - radius_B) /* Contained */ )
1518  {
1519  POINT2D XA, XB; /* Points where the line from CA to CB cross their circle bounds */
1520 
1521  /* Calculate hypothetical nearest points, the places on the */
1522  /* two circles where the center-center line crosses. If both */
1523  /* arcs contain their hypothetical points, that's the crossing distance */
1524  XA.x = CA.x + (CB.x - CA.x) * radius_A / d;
1525  XA.y = CA.y + (CB.y - CA.y) * radius_A / d;
1526  XB.x = CB.x + (CA.x - CB.x) * radius_B / d;
1527  XB.y = CB.y + (CA.y - CB.y) * radius_B / d;
1528 
1529  pt_in_arc_A = lw_pt_in_arc(&XA, A1, A2, A3);
1530  pt_in_arc_B = lw_pt_in_arc(&XB, B1, B2, B3);
1531 
1532  /* If the nearest points are both within the arcs, that's our answer */
1533  /* the shortest distance is at the nearest points */
1534  if ( pt_in_arc_A && pt_in_arc_B )
1535  {
1536  return lw_dist2d_pt_pt(&XA, &XB, dl);
1537  }
1538  }
1539  /* Circles cross at two points, are either of those points in both arcs? */
1540  /* http://paulbourke.net/geometry/2circle/ */
1541  else if ( d < (radius_A + radius_B) )
1542  {
1543  POINT2D E, F; /* Points where circle(A) and circle(B) cross */
1544  /* Distance from CA to D */
1545  double a = (radius_A*radius_A - radius_B*radius_B + d*d) / (2*d);
1546  /* Distance from D to E or F */
1547  double h = sqrt(radius_A*radius_A - a*a);
1548 
1549  /* Location of D */
1550  D.x = CA.x + (CB.x - CA.x) * a / d;
1551  D.y = CA.y + (CB.y - CA.y) * a / d;
1552 
1553  /* Start from D and project h units perpendicular to CA-D to get E */
1554  E.x = D.x + (D.y - CA.y) * h / a;
1555  E.y = D.y + (D.x - CA.x) * h / a;
1556 
1557  /* Crossing point E contained in arcs? */
1558  pt_in_arc_A = lw_pt_in_arc(&E, A1, A2, A3);
1559  pt_in_arc_B = lw_pt_in_arc(&E, B1, B2, B3);
1560 
1561  if ( pt_in_arc_A && pt_in_arc_B )
1562  {
1563  dl->p1 = dl->p2 = E;
1564  dl->distance = 0.0;
1565  return LW_TRUE;
1566  }
1567 
1568  /* Start from D and project h units perpendicular to CA-D to get F */
1569  F.x = D.x - (D.y - CA.y) * h / a;
1570  F.y = D.y - (D.x - CA.x) * h / a;
1571 
1572  /* Crossing point F contained in arcs? */
1573  pt_in_arc_A = lw_pt_in_arc(&F, A1, A2, A3);
1574  pt_in_arc_B = lw_pt_in_arc(&F, B1, B2, B3);
1575 
1576  if ( pt_in_arc_A && pt_in_arc_B )
1577  {
1578  dl->p1 = dl->p2 = F;
1579  dl->distance = 0.0;
1580  return LW_TRUE;
1581  }
1582  }
1583  else
1584  {
1585  lwerror("lw_dist2d_arc_arc: arcs neither touch, intersect nor are disjoint! INCONCEIVABLE!");
1586  return LW_FALSE;
1587  }
1588 
1589  /* Closest point is in the arc A, but not in the arc B, so */
1590  /* one of the B end points must be the closest. */
1591  if ( pt_in_arc_A & ! pt_in_arc_B )
1592  {
1593  lw_dist2d_pt_arc(B1, A1, A2, A3, dl);
1594  lw_dist2d_pt_arc(B3, A1, A2, A3, dl);
1595  return LW_TRUE;
1596  }
1597  /* Closest point is in the arc B, but not in the arc A, so */
1598  /* one of the A end points must be the closest. */
1599  else if ( pt_in_arc_B && ! pt_in_arc_A )
1600  {
1601  lw_dist2d_pt_arc(A1, B1, B2, B3, dl);
1602  lw_dist2d_pt_arc(A3, B1, B2, B3, dl);
1603  return LW_TRUE;
1604  }
1605  /* Finally, one of the end-point to end-point combos is the closest. */
1606  else
1607  {
1608  lw_dist2d_pt_pt(A1, B1, dl);
1609  lw_dist2d_pt_pt(A1, B3, dl);
1610  lw_dist2d_pt_pt(A2, B1, dl);
1611  lw_dist2d_pt_pt(A2, B3, dl);
1612  return LW_TRUE;
1613  }
1614 
1615  return LW_TRUE;
1616 }
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
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:106
int mode
Definition: measures.h:26
#define DIST_MIN
POINT2D p1
Definition: measures.h:24
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:67
double x
Definition: liblwgeom.h:284
#define LW_FALSE
Definition: liblwgeom.h:52
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:51
int lw_dist2d_pt_arc(const POINT2D *P, const POINT2D *A1, const POINT2D *A2, const POINT2D *A3, DISTPTS *dl)
Definition: measures.c:1395
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:1624
POINT2D p2
Definition: measures.h:25
double y
Definition: liblwgeom.h:284
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:86
double distance
Definition: measures.h:23
double distance2d_pt_pt(const POINT2D *p1, const POINT2D *p2)
The old function nessecary for ptarray_segmentize2d in ptarray.c.
Definition: measures.c:2123
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:1248
#define FP_EQUALS(A, B)
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:2087

Here is the call graph for this function:

Here is the caller graph for this function: