PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ edge_intersects()

uint32_t edge_intersects ( const POINT3D A1,
const POINT3D A2,
const POINT3D B1,
const POINT3D B2 
)

Returns non-zero if edges A and B interact.

The type of interaction is given in the return value with the bitmask elements defined above.

Definition at line 3381 of file lwgeodetic.c.

3382 {
3383  POINT3D AN, BN, VN; /* Normals to plane A and plane B */
3384  double ab_dot;
3385  int a1_side, a2_side, b1_side, b2_side;
3386  int rv = PIR_NO_INTERACT;
3387 
3388  /* Normals to the A-plane and B-plane */
3389  unit_normal(A1, A2, &AN);
3390  unit_normal(B1, B2, &BN);
3391 
3392  /* Are A-plane and B-plane basically the same? */
3393  ab_dot = dot_product(&AN, &BN);
3394 
3395  /*
3396  * https://trac.osgeo.org/postgis/ticket/5765
3397  * Failure because the colinearity check was
3398  * triggering due to an overly loose equality
3399  * check here.
3400  * if ( FP_EQUALS(fabs(ab_dot), 1.0) )
3401  */
3402  if ( 1.0 - fabs(ab_dot) <= 10e-16 )
3403  {
3404  /* Co-linear case */
3405  if ( point_in_cone(A1, A2, B1) || point_in_cone(A1, A2, B2) ||
3406  point_in_cone(B1, B2, A1) || point_in_cone(B1, B2, A2) )
3407  {
3408  rv |= PIR_INTERSECTS;
3409  rv |= PIR_COLINEAR;
3410  }
3411  return rv;
3412  }
3413 
3414  /* What side of plane-A and plane-B do the end points */
3415  /* of A and B fall? */
3416  a1_side = dot_product_side(&BN, A1);
3417  a2_side = dot_product_side(&BN, A2);
3418  b1_side = dot_product_side(&AN, B1);
3419  b2_side = dot_product_side(&AN, B2);
3420 
3421  /* Both ends of A on the same side of plane B. */
3422  if ( a1_side == a2_side && a1_side != 0 )
3423  {
3424  /* No intersection. */
3425  return PIR_NO_INTERACT;
3426  }
3427 
3428  /* Both ends of B on the same side of plane A. */
3429  if ( b1_side == b2_side && b1_side != 0 )
3430  {
3431  /* No intersection. */
3432  return PIR_NO_INTERACT;
3433  }
3434 
3435  /* A straddles B and B straddles A, so... */
3436  if ( a1_side != a2_side && (a1_side + a2_side) == 0 &&
3437  b1_side != b2_side && (b1_side + b2_side) == 0 )
3438  {
3439  /* Have to check if intersection point is inside both arcs */
3440  unit_normal(&AN, &BN, &VN);
3441  if ( point_in_cone(A1, A2, &VN) && point_in_cone(B1, B2, &VN) )
3442  {
3443  return PIR_INTERSECTS;
3444  }
3445 
3446  /* Have to check if intersection point is inside both arcs */
3447  vector_scale(&VN, -1);
3448  if ( point_in_cone(A1, A2, &VN) && point_in_cone(B1, B2, &VN) )
3449  {
3450  return PIR_INTERSECTS;
3451  }
3452 
3453  return PIR_NO_INTERACT;
3454  }
3455 
3456  /* The rest are all intersects variants... */
3457  rv |= PIR_INTERSECTS;
3458 
3459  /* A touches B */
3460  if ( a1_side == 0 )
3461  {
3462  /* Touches at A1, A2 is on what side? */
3463  rv |= (a2_side < 0 ? PIR_A_TOUCH_RIGHT : PIR_A_TOUCH_LEFT);
3464  }
3465  else if ( a2_side == 0 )
3466  {
3467  /* Touches at A2, A1 is on what side? */
3468  rv |= (a1_side < 0 ? PIR_A_TOUCH_RIGHT : PIR_A_TOUCH_LEFT);
3469  }
3470 
3471  /* B touches A */
3472  if ( b1_side == 0 )
3473  {
3474  /* Touches at B1, B2 is on what side? */
3475  rv |= (b2_side < 0 ? PIR_B_TOUCH_RIGHT : PIR_B_TOUCH_LEFT);
3476  }
3477  else if ( b2_side == 0 )
3478  {
3479  /* Touches at B2, B1 is on what side? */
3480  rv |= (b1_side < 0 ? PIR_B_TOUCH_RIGHT : PIR_B_TOUCH_LEFT);
3481  }
3482 
3483  return rv;
3484 }
static int point_in_cone(const POINT3D *A1, const POINT3D *A2, const POINT3D *P)
Utility function for checking if P is within the cone defined by A1/A2.
Definition: lwgeodetic.c:3299
void vector_scale(POINT3D *n, double scale)
Scale a vector out by a factor.
Definition: lwgeodetic.c:487
static int dot_product_side(const POINT3D *p, const POINT3D *q)
Utility function for edge_intersects(), signum with a tolerance in determining if the value is zero.
Definition: lwgeodetic.c:3366
void unit_normal(const POINT3D *P1, const POINT3D *P2, POINT3D *normal)
Calculates the unit normal to two vectors, trying to avoid problems with over-narrow or over-wide cas...
Definition: lwgeodetic.c:541
static double dot_product(const POINT3D *p1, const POINT3D *p2)
Convert cartesian coordinates on unit sphere to lon/lat coordinates static void cart2ll(const POINT3D...
Definition: lwgeodetic.c:446
#define PIR_A_TOUCH_LEFT
Definition: lwgeodetic.h:91
#define PIR_COLINEAR
Definition: lwgeodetic.h:89
#define PIR_INTERSECTS
Definition: lwgeodetic.h:88
#define PIR_A_TOUCH_RIGHT
Definition: lwgeodetic.h:90
#define PIR_B_TOUCH_RIGHT
Definition: lwgeodetic.h:92
#define PIR_B_TOUCH_LEFT
Definition: lwgeodetic.h:93
#define PIR_NO_INTERACT
Bitmask elements for edge_intersects() return value.
Definition: lwgeodetic.h:87

References dot_product(), dot_product_side(), PIR_A_TOUCH_LEFT, PIR_A_TOUCH_RIGHT, PIR_B_TOUCH_LEFT, PIR_B_TOUCH_RIGHT, PIR_COLINEAR, PIR_INTERSECTS, PIR_NO_INTERACT, point_in_cone(), unit_normal(), and vector_scale().

Referenced by circ_tree_contains_point(), circ_tree_distance_tree_internal(), lwpoly_intersects_line(), ptarray_contains_point_sphere(), ptarray_distance_spheroid(), and test_edge_intersects().

Here is the call graph for this function:
Here is the caller graph for this function: