PostGIS  2.5.0dev-r@@SVN_REVISION@@
int edge_contains_coplanar_point ( const GEOGRAPHIC_EDGE e,
const GEOGRAPHIC_POINT p 
)

True if the longitude of p is within the range of the longitude of the ends of e.

Definition at line 800 of file lwgeodetic.c.

References GEOGRAPHIC_EDGE::end, FP_EQUALS, FP_MAX, FP_MIN, GEOGRAPHIC_POINT::lat, GEOGRAPHIC_POINT::lon, LW_FALSE, LW_TRUE, LWDEBUG, LWDEBUGF, SIGNUM, and GEOGRAPHIC_EDGE::start.

801 {
802  GEOGRAPHIC_EDGE g;
804  double slon = fabs((e->start).lon) + fabs((e->end).lon);
805  double dlon = fabs(fabs((e->start).lon) - fabs((e->end).lon));
806  double slat = (e->start).lat + (e->end).lat;
807 
808  LWDEBUGF(4, "e.start == GPOINT(%.6g %.6g) ", (e->start).lat, (e->start).lon);
809  LWDEBUGF(4, "e.end == GPOINT(%.6g %.6g) ", (e->end).lat, (e->end).lon);
810  LWDEBUGF(4, "p == GPOINT(%.6g %.6g) ", p->lat, p->lon);
811 
812  /* Copy values into working registers */
813  g = *e;
814  q = *p;
815 
816  /* Vertical plane, we need to do this calculation in latitude */
817  if ( FP_EQUALS( g.start.lon, g.end.lon ) )
818  {
819  LWDEBUG(4, "vertical plane, we need to do this calculation in latitude");
820  /* Supposed to be co-planar... */
821  if ( ! FP_EQUALS( q.lon, g.start.lon ) )
822  return LW_FALSE;
823 
824  if ( ( g.start.lat <= q.lat && q.lat <= g.end.lat ) ||
825  ( g.end.lat <= q.lat && q.lat <= g.start.lat ) )
826  {
827  return LW_TRUE;
828  }
829  else
830  {
831  return LW_FALSE;
832  }
833  }
834 
835  /* Over the pole, we need normalize latitude and do this calculation in latitude */
836  if ( FP_EQUALS( slon, M_PI ) && ( SIGNUM(g.start.lon) != SIGNUM(g.end.lon) || FP_EQUALS(dlon, M_PI) ) )
837  {
838  LWDEBUG(4, "over the pole...");
839  /* Antipodal, everything (or nothing?) is inside */
840  if ( FP_EQUALS( slat, 0.0 ) )
841  return LW_TRUE;
842 
843  /* Point *is* the north pole */
844  if ( slat > 0.0 && FP_EQUALS(q.lat, M_PI_2 ) )
845  return LW_TRUE;
846 
847  /* Point *is* the south pole */
848  if ( slat < 0.0 && FP_EQUALS(q.lat, -1.0 * M_PI_2) )
849  return LW_TRUE;
850 
851  LWDEBUG(4, "coplanar?...");
852 
853  /* Supposed to be co-planar... */
854  if ( ! FP_EQUALS( q.lon, g.start.lon ) )
855  return LW_FALSE;
856 
857  LWDEBUG(4, "north or south?...");
858 
859  /* Over north pole, test based on south pole */
860  if ( slat > 0.0 )
861  {
862  LWDEBUG(4, "over the north pole...");
863  if ( q.lat > FP_MIN(g.start.lat, g.end.lat) )
864  return LW_TRUE;
865  else
866  return LW_FALSE;
867  }
868  else
869  /* Over south pole, test based on north pole */
870  {
871  LWDEBUG(4, "over the south pole...");
872  if ( q.lat < FP_MAX(g.start.lat, g.end.lat) )
873  return LW_TRUE;
874  else
875  return LW_FALSE;
876  }
877  }
878 
879  /* Dateline crossing, flip everything to the opposite hemisphere */
880  else if ( slon > M_PI && ( SIGNUM(g.start.lon) != SIGNUM(g.end.lon) ) )
881  {
882  LWDEBUG(4, "crosses dateline, flip longitudes...");
883  if ( g.start.lon > 0.0 )
884  g.start.lon -= M_PI;
885  else
886  g.start.lon += M_PI;
887  if ( g.end.lon > 0.0 )
888  g.end.lon -= M_PI;
889  else
890  g.end.lon += M_PI;
891 
892  if ( q.lon > 0.0 )
893  q.lon -= M_PI;
894  else
895  q.lon += M_PI;
896  }
897 
898  if ( ( g.start.lon <= q.lon && q.lon <= g.end.lon ) ||
899  ( g.end.lon <= q.lon && q.lon <= g.start.lon ) )
900  {
901  LWDEBUG(4, "true, this edge contains point");
902  return LW_TRUE;
903  }
904 
905  LWDEBUG(4, "false, this edge does not contain point");
906  return LW_FALSE;
907 }
Two-point great circle segment from a to b.
Definition: lwgeodetic.h:56
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
#define FP_MIN(A, B)
Point in spherical coordinates on the world.
Definition: lwgeodetic.h:47
#define LW_FALSE
Definition: liblwgeom.h:76
GEOGRAPHIC_POINT start
Definition: lwgeodetic.h:58
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:75
GEOGRAPHIC_POINT end
Definition: lwgeodetic.h:59
#define FP_EQUALS(A, B)
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
#define SIGNUM(n)
Macro that returns: -1 if n < 0, 1 if n > 0, 0 if n == 0.
#define FP_MAX(A, B)