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

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

1275 {
1276  POINT2D C; /* center of arc circle */
1278  POINT2D D; /* point on A closest to C */
1279  double dist_C_D; /* distance from C to D */
1280  int pt_in_arc, pt_in_seg;
1281  DISTPTS dltmp;
1282
1283  /* Bail out on crazy modes */
1284  if ( dl->mode < 0 )
1285  lwerror("lw_dist2d_seg_arc does not support maxdistance mode");
1286
1287  /* What if the "arc" is a point? */
1288  if ( lw_arc_is_pt(B1, B2, B3) )
1289  return lw_dist2d_pt_seg(B1, A1, A2, dl);
1290
1291  /* Calculate center and radius of the circle. */
1292  radius_C = lw_arc_center(B1, B2, B3, &C);
1293
1294  /* This "arc" is actually a line (B2 is colinear with B1,B3) */
1295  if ( radius_C < 0.0 )
1296  return lw_dist2d_seg_seg(A1, A2, B1, B3, dl);
1297
1298  /* Calculate distance between the line and circle center */
1300  if ( lw_dist2d_pt_seg(&C, A1, A2, &dltmp) == LW_FALSE )
1301  lwerror("lw_dist2d_pt_seg failed in lw_dist2d_seg_arc");
1302
1303  D = dltmp.p1;
1304  dist_C_D = dltmp.distance;
1305
1306  /* Line intersects circle, maybe arc intersects edge? */
1307  /* If so, that's the closest point. */
1308  /* If not, the closest point is one of the end points of A */
1309  if ( dist_C_D < radius_C )
1310  {
1311  double length_A; /* length of the segment A */
1312  POINT2D E, F; /* points of interection of edge A and circle(B) */
1313  double dist_D_EF; /* distance from D to E or F (same distance both ways) */
1314
1316  length_A = sqrt((A2->x-A1->x)*(A2->x-A1->x)+(A2->y-A1->y)*(A2->y-A1->y));
1317
1318  /* Point of intersection E */
1319  E.x = D.x - (A2->x-A1->x) * dist_D_EF / length_A;
1320  E.y = D.y - (A2->y-A1->y) * dist_D_EF / length_A;
1321  /* Point of intersection F */
1322  F.x = D.x + (A2->x-A1->x) * dist_D_EF / length_A;
1323  F.y = D.y + (A2->y-A1->y) * dist_D_EF / length_A;
1324
1325
1326  /* If E is within A and within B then it's an interesction point */
1327  pt_in_arc = lw_pt_in_arc(&E, B1, B2, B3);
1328  pt_in_seg = lw_pt_in_seg(&E, A1, A2);
1329
1330  if ( pt_in_arc && pt_in_seg )
1331  {
1332  dl->distance = 0.0;
1333  dl->p1 = E;
1334  dl->p2 = E;
1335  return LW_TRUE;
1336  }
1337
1338  /* If F is within A and within B then it's an interesction point */
1339  pt_in_arc = lw_pt_in_arc(&F, B1, B2, B3);
1340  pt_in_seg = lw_pt_in_seg(&F, A1, A2);
1341
1342  if ( pt_in_arc && pt_in_seg )
1343  {
1344  dl->distance = 0.0;
1345  dl->p1 = F;
1346  dl->p2 = F;
1347  return LW_TRUE;
1348  }
1349  }
1350
1351  /* Line grazes circle, maybe arc intersects edge? */
1352  /* If so, grazing point is the closest point. */
1353  /* If not, the closest point is one of the end points of A */
1354  else if ( dist_C_D == radius_C )
1355  {
1356  /* Closest point D is also the point of grazing */
1357  pt_in_arc = lw_pt_in_arc(&D, B1, B2, B3);
1358  pt_in_seg = lw_pt_in_seg(&D, A1, A2);
1359
1360  /* Is D contained in both A and B? */
1361  if ( pt_in_arc && pt_in_seg )
1362  {
1363  dl->distance = 0.0;
1364  dl->p1 = D;
1365  dl->p2 = D;
1366  return LW_TRUE;
1367  }
1368  }
1369  /* Line misses circle. */
1370  /* If closest point to A on circle is within B, then that's the closest */
1371  /* Otherwise, the closest point will be an end point of A */
1372  else
1373  {
1374  POINT2D G; /* Point on circle closest to A */
1375  G.x = C.x + (D.x-C.x) * radius_C / dist_C_D;
1376  G.y = C.y + (D.y-C.y) * radius_C / dist_C_D;
1377
1378  pt_in_arc = lw_pt_in_arc(&G, B1, B2, B3);
1379  pt_in_seg = lw_pt_in_seg(&D, A1, A2);
1380
1381  /* Closest point is on the interior of A and B */
1382  if ( pt_in_arc && pt_in_seg )
1383  return lw_dist2d_pt_pt(&D, &G, dl);
1384
1385  }
1386
1387  /* Now we test the many combinations of end points with either */
1388  /* arcs or edges. Each previous check determined if the closest */
1389  /* potential point was within the arc/segment inscribed on the */
1390  /* line/circle holding the arc/segment. */
1391
1392  /* Closest point is in the arc, but not in the segment, so */
1393  /* one of the segment end points must be the closest. */
1394  if ( pt_in_arc & ! pt_in_seg )
1395  {
1396  lw_dist2d_pt_arc(A1, B1, B2, B3, dl);
1397  lw_dist2d_pt_arc(A2, B1, B2, B3, dl);
1398  return LW_TRUE;
1399  }
1400  /* or, one of the arc end points is the closest */
1401  else if ( pt_in_seg && ! pt_in_arc )
1402  {
1403  lw_dist2d_pt_seg(B1, A1, A2, dl);
1404  lw_dist2d_pt_seg(B3, A1, A2, dl);
1405  return LW_TRUE;
1406  }
1407  /* Finally, one of the end-point to end-point combos is the closest. */
1408  else
1409  {
1410  lw_dist2d_pt_pt(A1, B1, dl);
1411  lw_dist2d_pt_pt(A1, B3, dl);
1412  lw_dist2d_pt_pt(A2, B1, dl);
1413  lw_dist2d_pt_pt(A2, B3, dl);
1414  return LW_TRUE;
1415  }
1416
1417  return LW_FALSE;
1418 }
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:213
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:91
int mode
Definition: measures.h:27
POINT2D p1
Definition: measures.h:25
double x
Definition: liblwgeom.h:312
#define DIST_MIN
Definition: measures.h:17
#define LW_FALSE
Definition: liblwgeom.h:62
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:61
int lw_dist2d_pt_arc(const POINT2D *P, const POINT2D *A1, const POINT2D *A2, const POINT2D *A3, DISTPTS *dl)
Definition: measures.c:1421
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 occation to lw_dist2d_pt_pt Before i...
Definition: measures.c:2189
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:1802
POINT2D p2
Definition: measures.h:26
double y
Definition: liblwgeom.h:312
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:71
double distance
Definition: measures.h:24
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:81
Structure used in distance-calculations.
Definition: measures.h:22
void lw_dist2d_distpts_init(DISTPTS *dl, int mode)
Definition: measures.c:53
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:74
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:2265

Here is the call graph for this function:

Here is the caller graph for this function: