PostGIS  3.7.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 1373 of file measures.c.

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