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

1293 {
1294  POINT2D C; /* center of arc circle */
1295  double radius_C; /* radius of arc circle */
1296  POINT2D D; /* point on A closest to C */
1297  double dist_C_D; /* distance from C to D */
1298  int pt_in_arc, pt_in_seg;
1299  DISTPTS dltmp;
1300 
1301  /* Bail out on crazy modes */
1302  if ( dl->mode < 0 )
1303  lwerror("lw_dist2d_seg_arc does not support maxdistance mode");
1304 
1305  /* What if the "arc" is a point? */
1306  if ( lw_arc_is_pt(B1, B2, B3) )
1307  return lw_dist2d_pt_seg(B1, A1, A2, dl);
1308 
1309  /* Calculate center and radius of the circle. */
1310  radius_C = lw_arc_center(B1, B2, B3, &C);
1311 
1312  /* This "arc" is actually a line (B2 is collinear with B1,B3) */
1313  if ( radius_C < 0.0 )
1314  return lw_dist2d_seg_seg(A1, A2, B1, B3, dl);
1315 
1316  /* Calculate distance between the line and circle center */
1318  if ( lw_dist2d_pt_seg(&C, A1, A2, &dltmp) == LW_FALSE )
1319  lwerror("lw_dist2d_pt_seg failed in lw_dist2d_seg_arc");
1320 
1321  D = dltmp.p1;
1322  dist_C_D = dltmp.distance;
1323 
1324  /* Line intersects circle, maybe arc intersects edge? */
1325  /* If so, that's the closest point. */
1326  /* If not, the closest point is one of the end points of A */
1327  if ( dist_C_D < radius_C )
1328  {
1329  double length_A; /* length of the segment A */
1330  POINT2D E, F; /* points of intersection of edge A and circle(B) */
1331  double dist_D_EF; /* distance from D to E or F (same distance both ways) */
1332 
1333  dist_D_EF = sqrt(radius_C*radius_C - dist_C_D*dist_C_D);
1334  length_A = sqrt((A2->x-A1->x)*(A2->x-A1->x)+(A2->y-A1->y)*(A2->y-A1->y));
1335 
1336  /* Point of intersection E */
1337  E.x = D.x - (A2->x-A1->x) * dist_D_EF / length_A;
1338  E.y = D.y - (A2->y-A1->y) * dist_D_EF / length_A;
1339  /* Point of intersection F */
1340  F.x = D.x + (A2->x-A1->x) * dist_D_EF / length_A;
1341  F.y = D.y + (A2->y-A1->y) * dist_D_EF / length_A;
1342 
1343 
1344  /* If E is within A and within B then it's an intersection point */
1345  pt_in_arc = lw_pt_in_arc(&E, B1, B2, B3);
1346  pt_in_seg = lw_pt_in_seg(&E, A1, A2);
1347 
1348  if ( pt_in_arc && pt_in_seg )
1349  {
1350  dl->distance = 0.0;
1351  dl->p1 = E;
1352  dl->p2 = E;
1353  return LW_TRUE;
1354  }
1355 
1356  /* If F is within A and within B then it's an intersection point */
1357  pt_in_arc = lw_pt_in_arc(&F, B1, B2, B3);
1358  pt_in_seg = lw_pt_in_seg(&F, A1, A2);
1359 
1360  if ( pt_in_arc && pt_in_seg )
1361  {
1362  dl->distance = 0.0;
1363  dl->p1 = F;
1364  dl->p2 = F;
1365  return LW_TRUE;
1366  }
1367  }
1368 
1369  /* Line grazes circle, maybe arc intersects edge? */
1370  /* If so, grazing point is the closest point. */
1371  /* If not, the closest point is one of the end points of A */
1372  else if ( dist_C_D == radius_C )
1373  {
1374  /* Closest point D is also the point of grazing */
1375  pt_in_arc = lw_pt_in_arc(&D, B1, B2, B3);
1376  pt_in_seg = lw_pt_in_seg(&D, A1, A2);
1377 
1378  /* Is D contained in both A and B? */
1379  if ( pt_in_arc && pt_in_seg )
1380  {
1381  dl->distance = 0.0;
1382  dl->p1 = D;
1383  dl->p2 = D;
1384  return LW_TRUE;
1385  }
1386  }
1387  /* Line misses circle. */
1388  /* If closest point to A on circle is within B, then that's the closest */
1389  /* Otherwise, the closest point will be an end point of A */
1390  else
1391  {
1392  POINT2D G; /* Point on circle closest to A */
1393  G.x = C.x + (D.x-C.x) * radius_C / dist_C_D;
1394  G.y = C.y + (D.y-C.y) * radius_C / dist_C_D;
1395 
1396  pt_in_arc = lw_pt_in_arc(&G, B1, B2, B3);
1397  pt_in_seg = lw_pt_in_seg(&D, A1, A2);
1398 
1399  /* Closest point is on the interior of A and B */
1400  if ( pt_in_arc && pt_in_seg )
1401  return lw_dist2d_pt_pt(&D, &G, dl);
1402 
1403  }
1404 
1405  /* Now we test the many combinations of end points with either */
1406  /* arcs or edges. Each previous check determined if the closest */
1407  /* potential point was within the arc/segment inscribed on the */
1408  /* line/circle holding the arc/segment. */
1409 
1410  /* Closest point is in the arc, but not in the segment, so */
1411  /* one of the segment end points must be the closest. */
1412  if (pt_in_arc && !pt_in_seg)
1413  {
1414  lw_dist2d_pt_arc(A1, B1, B2, B3, dl);
1415  lw_dist2d_pt_arc(A2, B1, B2, B3, dl);
1416  return LW_TRUE;
1417  }
1418  /* or, one of the arc end points is the closest */
1419  else if (pt_in_seg && !pt_in_arc)
1420  {
1421  lw_dist2d_pt_seg(B1, A1, A2, dl);
1422  lw_dist2d_pt_seg(B3, A1, A2, dl);
1423  return LW_TRUE;
1424  }
1425  /* Finally, one of the end-point to end-point combos is the closest. */
1426  else
1427  {
1428  lw_dist2d_pt_pt(A1, B1, dl);
1429  lw_dist2d_pt_pt(A1, B3, dl);
1430  lw_dist2d_pt_pt(A2, B1, dl);
1431  lw_dist2d_pt_pt(A2, B3, dl);
1432  return LW_TRUE;
1433  }
1434 
1435  return LW_FALSE;
1436 }
#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
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:95
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
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_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:2206
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
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: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
Structure used in distance-calculations.
Definition: measures.h:50
double y
Definition: liblwgeom.h:331
double x
Definition: liblwgeom.h:331

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: