PostGIS  2.3.8dev-r@@SVN_REVISION@@

◆ edge_intersects()

int 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 3110 of file lwgeodetic.c.

References dot_product(), dot_product_side(), FP_EQUALS, 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(), ptarray_contains_point_sphere(), ptarray_distance_spheroid(), and test_edge_intersects().

3111 {
3112  POINT3D AN, BN, VN; /* Normals to plane A and plane B */
3113  double ab_dot;
3114  int a1_side, a2_side, b1_side, b2_side;
3115  int rv = PIR_NO_INTERACT;
3116 
3117  /* Normals to the A-plane and B-plane */
3118  unit_normal(A1, A2, &AN);
3119  unit_normal(B1, B2, &BN);
3120 
3121  /* Are A-plane and B-plane basically the same? */
3122  ab_dot = dot_product(&AN, &BN);
3123  if ( FP_EQUALS(fabs(ab_dot), 1.0) )
3124  {
3125  /* Co-linear case */
3126  if ( point_in_cone(A1, A2, B1) || point_in_cone(A1, A2, B2) ||
3127  point_in_cone(B1, B2, A1) || point_in_cone(B1, B2, A2) )
3128  {
3129  rv |= PIR_INTERSECTS;
3130  rv |= PIR_COLINEAR;
3131  }
3132  return rv;
3133  }
3134 
3135  /* What side of plane-A and plane-B do the end points */
3136  /* of A and B fall? */
3137  a1_side = dot_product_side(&BN, A1);
3138  a2_side = dot_product_side(&BN, A2);
3139  b1_side = dot_product_side(&AN, B1);
3140  b2_side = dot_product_side(&AN, B2);
3141 
3142  /* Both ends of A on the same side of plane B. */
3143  if ( a1_side == a2_side && a1_side != 0 )
3144  {
3145  /* No intersection. */
3146  return PIR_NO_INTERACT;
3147  }
3148 
3149  /* Both ends of B on the same side of plane A. */
3150  if ( b1_side == b2_side && b1_side != 0 )
3151  {
3152  /* No intersection. */
3153  return PIR_NO_INTERACT;
3154  }
3155 
3156  /* A straddles B and B straddles A, so... */
3157  if ( a1_side != a2_side && (a1_side + a2_side) == 0 &&
3158  b1_side != b2_side && (b1_side + b2_side) == 0 )
3159  {
3160  /* Have to check if intersection point is inside both arcs */
3161  unit_normal(&AN, &BN, &VN);
3162  if ( point_in_cone(A1, A2, &VN) && point_in_cone(B1, B2, &VN) )
3163  {
3164  return PIR_INTERSECTS;
3165  }
3166 
3167  /* Have to check if intersection point is inside both arcs */
3168  vector_scale(&VN, -1);
3169  if ( point_in_cone(A1, A2, &VN) && point_in_cone(B1, B2, &VN) )
3170  {
3171  return PIR_INTERSECTS;
3172  }
3173 
3174  return PIR_NO_INTERACT;
3175  }
3176 
3177  /* The rest are all intersects variants... */
3178  rv |= PIR_INTERSECTS;
3179 
3180  /* A touches B */
3181  if ( a1_side == 0 )
3182  {
3183  /* Touches at A1, A2 is on what side? */
3184  rv |= (a2_side < 0 ? PIR_A_TOUCH_RIGHT : PIR_A_TOUCH_LEFT);
3185  }
3186  else if ( a2_side == 0 )
3187  {
3188  /* Touches at A2, A1 is on what side? */
3189  rv |= (a1_side < 0 ? PIR_A_TOUCH_RIGHT : PIR_A_TOUCH_LEFT);
3190  }
3191 
3192  /* B touches A */
3193  if ( b1_side == 0 )
3194  {
3195  /* Touches at B1, B2 is on what side? */
3196  rv |= (b2_side < 0 ? PIR_B_TOUCH_RIGHT : PIR_B_TOUCH_LEFT);
3197  }
3198  else if ( b2_side == 0 )
3199  {
3200  /* Touches at B2, B1 is on what side? */
3201  rv |= (b1_side < 0 ? PIR_B_TOUCH_RIGHT : PIR_B_TOUCH_LEFT);
3202  }
3203 
3204  return rv;
3205 }
#define PIR_B_TOUCH_RIGHT
Definition: lwgeodetic.h:86
#define PIR_A_TOUCH_RIGHT
Definition: lwgeodetic.h:84
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:3095
static void vector_scale(POINT3D *n, double scale)
Scale a vector out by a factor.
Definition: lwgeodetic.c:452
#define PIR_A_TOUCH_LEFT
Definition: lwgeodetic.h:85
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:506
static double dot_product(const POINT3D *p1, const POINT3D *p2)
Convert cartesion coordinates on unit sphere to lon/lat coordinates static void cart2ll(const POINT3D...
Definition: lwgeodetic.c:411
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:3057
#define FP_EQUALS(A, B)
#define PIR_NO_INTERACT
Bitmask elements for edge_intersects() return value.
Definition: lwgeodetic.h:81
#define PIR_B_TOUCH_LEFT
Definition: lwgeodetic.h:87
#define PIR_COLINEAR
Definition: lwgeodetic.h:83
#define PIR_INTERSECTS
Definition: lwgeodetic.h:82
Here is the call graph for this function:
Here is the caller graph for this function: