PostGIS  2.5.0dev-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 1288 of file measures.c.

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(), and test_lw_dist2d_seg_arc().

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

Here is the call graph for this function:

Here is the caller graph for this function: