PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ edge_contains_coplanar_point()

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 785 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.

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