PostGIS 3.0.6dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ 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 1362 of file measures.c.

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

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: