PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ lw_dist2d_seg_arc()

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.

Line/circle approach from http://stackoverflow.com/questions/1073336/circle-line-collision-detection

Definition at line 1363 of file measures.c.

1369 {
1370  POINT2D C; /* center of arc circle */
1371  double radius_C; /* radius of arc circle */
1372  POINT2D D; /* point on A closest to C */
1373  double dist_C_D; /* distance from C to D */
1374  int pt_in_arc, pt_in_seg;
1375  DISTPTS dltmp;
1376 
1377  /* Bail out on crazy modes */
1378  if (dl->mode < 0)
1379  lwerror("lw_dist2d_seg_arc does not support maxdistance mode");
1380 
1381  /* What if the "arc" is a point? */
1382  if (lw_arc_is_pt(B1, B2, B3))
1383  return lw_dist2d_pt_seg(B1, A1, A2, dl);
1384 
1385  /* Calculate center and radius of the circle. */
1386  radius_C = lw_arc_center(B1, B2, B3, &C);
1387 
1388  /* This "arc" is actually a line (B2 is collinear with B1,B3) */
1389  if (radius_C < 0.0)
1390  return lw_dist2d_seg_seg(A1, A2, B1, B3, dl);
1391 
1392  /* Calculate distance between the line and circle center */
1394  if (lw_dist2d_pt_seg(&C, A1, A2, &dltmp) == LW_FALSE)
1395  lwerror("lw_dist2d_pt_seg failed in lw_dist2d_seg_arc");
1396 
1397  D = dltmp.p1;
1398  dist_C_D = dltmp.distance;
1399 
1400  /* Line intersects circle, maybe arc intersects edge? */
1401  /* If so, that's the closest point. */
1402  /* If not, the closest point is one of the end points of A */
1403  if (dist_C_D < radius_C)
1404  {
1405  double length_A; /* length of the segment A */
1406  POINT2D E, F; /* points of intersection of edge A and circle(B) */
1407  double dist_D_EF; /* distance from D to E or F (same distance both ways) */
1408 
1409  dist_D_EF = sqrt(radius_C * radius_C - dist_C_D * dist_C_D);
1410  length_A = sqrt((A2->x - A1->x) * (A2->x - A1->x) + (A2->y - A1->y) * (A2->y - A1->y));
1411 
1412  /* Point of intersection E */
1413  E.x = D.x - (A2->x - A1->x) * dist_D_EF / length_A;
1414  E.y = D.y - (A2->y - A1->y) * dist_D_EF / length_A;
1415  /* Point of intersection F */
1416  F.x = D.x + (A2->x - A1->x) * dist_D_EF / length_A;
1417  F.y = D.y + (A2->y - A1->y) * dist_D_EF / length_A;
1418 
1419  /* If E is within A and within B then it's an intersection point */
1420  pt_in_arc = lw_pt_in_arc(&E, B1, B2, B3);
1421  pt_in_seg = lw_pt_in_seg(&E, A1, A2);
1422 
1423  if (pt_in_arc && pt_in_seg)
1424  {
1425  dl->distance = 0.0;
1426  dl->p1 = E;
1427  dl->p2 = E;
1428  return LW_TRUE;
1429  }
1430 
1431  /* If F is within A and within B then it's an intersection point */
1432  pt_in_arc = lw_pt_in_arc(&F, B1, B2, B3);
1433  pt_in_seg = lw_pt_in_seg(&F, A1, A2);
1434 
1435  if (pt_in_arc && pt_in_seg)
1436  {
1437  dl->distance = 0.0;
1438  dl->p1 = F;
1439  dl->p2 = F;
1440  return LW_TRUE;
1441  }
1442  }
1443 
1444  /* Line grazes circle, maybe arc intersects edge? */
1445  /* If so, grazing point is the closest point. */
1446  /* If not, the closest point is one of the end points of A */
1447  else if (dist_C_D == radius_C)
1448  {
1449  /* Closest point D is also the point of grazing */
1450  pt_in_arc = lw_pt_in_arc(&D, B1, B2, B3);
1451  pt_in_seg = lw_pt_in_seg(&D, A1, A2);
1452 
1453  /* Is D contained in both A and B? */
1454  if (pt_in_arc && pt_in_seg)
1455  {
1456  dl->distance = 0.0;
1457  dl->p1 = D;
1458  dl->p2 = D;
1459  return LW_TRUE;
1460  }
1461  }
1462  /* Line misses circle. */
1463  /* If closest point to A on circle is within B, then that's the closest */
1464  /* Otherwise, the closest point will be an end point of A */
1465  else
1466  {
1467  POINT2D G; /* Point on circle closest to A */
1468  G.x = C.x + (D.x - C.x) * radius_C / dist_C_D;
1469  G.y = C.y + (D.y - C.y) * radius_C / dist_C_D;
1470 
1471  pt_in_arc = lw_pt_in_arc(&G, B1, B2, B3);
1472  pt_in_seg = lw_pt_in_seg(&D, A1, A2);
1473 
1474  /* Closest point is on the interior of A and B */
1475  if (pt_in_arc && pt_in_seg)
1476  return lw_dist2d_pt_pt(&D, &G, dl);
1477  }
1478 
1479  /* Now we test the many combinations of end points with either */
1480  /* arcs or edges. Each previous check determined if the closest */
1481  /* potential point was within the arc/segment inscribed on the */
1482  /* line/circle holding the arc/segment. */
1483 
1484  /* Closest point is in the arc, but not in the segment, so */
1485  /* one of the segment end points must be the closest. */
1486  if (pt_in_arc && !pt_in_seg)
1487  {
1488  lw_dist2d_pt_arc(A1, B1, B2, B3, dl);
1489  lw_dist2d_pt_arc(A2, B1, B2, B3, dl);
1490  return LW_TRUE;
1491  }
1492  /* or, one of the arc end points is the closest */
1493  else if (pt_in_seg && !pt_in_arc)
1494  {
1495  lw_dist2d_pt_seg(B1, A1, A2, dl);
1496  lw_dist2d_pt_seg(B3, A1, A2, dl);
1497  return LW_TRUE;
1498  }
1499  /* Finally, one of the end-point to end-point combos is the closest. */
1500  else
1501  {
1502  lw_dist2d_pt_pt(A1, B1, dl);
1503  lw_dist2d_pt_pt(A1, B3, dl);
1504  lw_dist2d_pt_pt(A2, B1, dl);
1505  lw_dist2d_pt_pt(A2, B3, dl);
1506  return LW_TRUE;
1507  }
1508 
1509  return LW_FALSE;
1510 }
#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
int lw_pt_in_seg(const POINT2D *P, const POINT2D *A1, const POINT2D *A2)
Returns true if P is between A1/A2.
Definition: lwalgorithm.c:93
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
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_pt_seg(const POINT2D *p, const POINT2D *A, const POINT2D *B, DISTPTS *dl)
lw_dist2d_comp from p to line A->B This one is now sending every occasion to lw_dist2d_pt_pt Before i...
Definition: measures.c:2306
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
void lw_dist2d_distpts_init(DISTPTS *dl, int mode)
Definition: measures.c:64
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
Structure used in distance-calculations.
Definition: measures.h:50
double y
Definition: liblwgeom.h:390
double x
Definition: liblwgeom.h:390

References DIST_MIN, DISTPTS::distance, lw_arc_center(), lw_arc_is_pt(), lw_dist2d_distpts_init(), lw_dist2d_pt_arc(), lw_dist2d_pt_pt(), lw_dist2d_pt_seg(), lw_dist2d_seg_seg(), LW_FALSE, lw_pt_in_arc(), lw_pt_in_seg(), LW_TRUE, lwerror(), DISTPTS::mode, DISTPTS::p1, DISTPTS::p2, POINT2D::x, and POINT2D::y.

Referenced by lw_dist2d_arc_arc(), lw_dist2d_ptarray_ptarrayarc(), rect_leaf_node_distance(), rect_leaf_node_intersects(), and test_lw_dist2d_seg_arc().

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