PostGIS  2.2.7dev-r@@SVN_REVISION@@
int edge_intersection ( const GEOGRAPHIC_EDGE e1,
const GEOGRAPHIC_EDGE e2,
GEOGRAPHIC_POINT g 
)

Returns true if an intersection can be calculated, and places it in *g.

Returns false otherwise.

Definition at line 1075 of file lwgeodetic.c.

References dot_product(), edge_contains_point(), GEOGRAPHIC_EDGE::end, FP_EQUALS, geographic_point_equals(), GEOGRAPHIC_POINT::lat, GEOGRAPHIC_POINT::lon, LW_FALSE, LW_TRUE, LWDEBUG, LWDEBUGF, normalize(), rad2deg, robust_cross_product(), GEOGRAPHIC_EDGE::start, unit_normal(), POINT3D::x, POINT3D::y, and POINT3D::z.

Referenced by circ_tree_distance_tree_internal(), and test_edge_intersection().

1076 {
1077  POINT3D ea, eb, v;
1078  LWDEBUGF(4, "e1 start(%.20g %.20g) end(%.20g %.20g)", e1->start.lat, e1->start.lon, e1->end.lat, e1->end.lon);
1079  LWDEBUGF(4, "e2 start(%.20g %.20g) end(%.20g %.20g)", e2->start.lat, e2->start.lon, e2->end.lat, e2->end.lon);
1080 
1081  LWDEBUGF(4, "e1 start(%.20g %.20g) end(%.20g %.20g)", rad2deg(e1->start.lon), rad2deg(e1->start.lat), rad2deg(e1->end.lon), rad2deg(e1->end.lat));
1082  LWDEBUGF(4, "e2 start(%.20g %.20g) end(%.20g %.20g)", rad2deg(e2->start.lon), rad2deg(e2->start.lat), rad2deg(e2->end.lon), rad2deg(e2->end.lat));
1083 
1084  if ( geographic_point_equals(&(e1->start), &(e2->start)) )
1085  {
1086  *g = e1->start;
1087  return LW_TRUE;
1088  }
1089  if ( geographic_point_equals(&(e1->end), &(e2->end)) )
1090  {
1091  *g = e1->end;
1092  return LW_TRUE;
1093  }
1094  if ( geographic_point_equals(&(e1->end), &(e2->start)) )
1095  {
1096  *g = e1->end;
1097  return LW_TRUE;
1098  }
1099  if ( geographic_point_equals(&(e1->start), &(e2->end)) )
1100  {
1101  *g = e1->start;
1102  return LW_TRUE;
1103  }
1104 
1105  robust_cross_product(&(e1->start), &(e1->end), &ea);
1106  normalize(&ea);
1107  robust_cross_product(&(e2->start), &(e2->end), &eb);
1108  normalize(&eb);
1109  LWDEBUGF(4, "e1 cross product == POINT(%.12g %.12g %.12g)", ea.x, ea.y, ea.z);
1110  LWDEBUGF(4, "e2 cross product == POINT(%.12g %.12g %.12g)", eb.x, eb.y, eb.z);
1111  LWDEBUGF(4, "fabs(dot_product(ea, eb)) == %.14g", fabs(dot_product(&ea, &eb)));
1112  if ( FP_EQUALS(fabs(dot_product(&ea, &eb)), 1.0) )
1113  {
1114  LWDEBUGF(4, "parallel edges found! dot_product = %.12g", dot_product(&ea, &eb));
1115  /* Parallel (maybe equal) edges! */
1116  /* Hack alert, only returning ONE end of the edge right now, most do better later. */
1117  /* Hack alart #2, returning a value of 2 to indicate a co-linear crossing event. */
1118  if ( edge_contains_point(e1, &(e2->start)) )
1119  {
1120  *g = e2->start;
1121  return 2;
1122  }
1123  if ( edge_contains_point(e1, &(e2->end)) )
1124  {
1125  *g = e2->end;
1126  return 2;
1127  }
1128  if ( edge_contains_point(e2, &(e1->start)) )
1129  {
1130  *g = e1->start;
1131  return 2;
1132  }
1133  if ( edge_contains_point(e2, &(e1->end)) )
1134  {
1135  *g = e1->end;
1136  return 2;
1137  }
1138  }
1139  unit_normal(&ea, &eb, &v);
1140  LWDEBUGF(4, "v == POINT(%.12g %.12g %.12g)", v.x, v.y, v.z);
1141  g->lat = atan2(v.z, sqrt(v.x * v.x + v.y * v.y));
1142  g->lon = atan2(v.y, v.x);
1143  LWDEBUGF(4, "g == GPOINT(%.12g %.12g)", g->lat, g->lon);
1144  LWDEBUGF(4, "g == POINT(%.12g %.12g)", rad2deg(g->lon), rad2deg(g->lat));
1145  if ( edge_contains_point(e1, g) && edge_contains_point(e2, g) )
1146  {
1147  return LW_TRUE;
1148  }
1149  else
1150  {
1151  LWDEBUG(4, "flipping point to other side of sphere");
1152  g->lat = -1.0 * g->lat;
1153  g->lon = g->lon + M_PI;
1154  if ( g->lon > M_PI )
1155  {
1156  g->lon = -1.0 * (2.0 * M_PI - g->lon);
1157  }
1158  if ( edge_contains_point(e1, g) && edge_contains_point(e2, g) )
1159  {
1160  return LW_TRUE;
1161  }
1162  }
1163  return LW_FALSE;
1164 }
void robust_cross_product(const GEOGRAPHIC_POINT *p, const GEOGRAPHIC_POINT *q, POINT3D *a)
Computes the cross product of two vectors using their lat, lng representations.
Definition: lwgeodetic.c:584
void normalize(POINT3D *p)
Normalize to a unit vector.
Definition: lwgeodetic.c:565
int edge_contains_point(const GEOGRAPHIC_EDGE *e, const GEOGRAPHIC_POINT *p)
Returns true if the point p is on the minor edge defined by the end points of e.
Definition: lwgeodetic.c:982
double y
Definition: liblwgeom.h:324
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:50
double x
Definition: liblwgeom.h:324
double z
Definition: liblwgeom.h:324
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:491
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:396
#define LW_FALSE
Definition: liblwgeom.h:62
#define rad2deg(r)
Definition: lwgeodetic.h:60
GEOGRAPHIC_POINT start
Definition: lwgeodetic.h:43
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:61
GEOGRAPHIC_POINT end
Definition: lwgeodetic.h:44
int geographic_point_equals(const GEOGRAPHIC_POINT *g1, const GEOGRAPHIC_POINT *g2)
Definition: lwgeodetic.c:146
#define FP_EQUALS(A, B)
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:55

Here is the call graph for this function:

Here is the caller graph for this function: