PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ edge_point_in_cone()

int edge_point_in_cone ( const GEOGRAPHIC_EDGE e,
const GEOGRAPHIC_POINT p 
)

Returns true if the point p is inside the cone defined by the two ends of the edge e.

Definition at line 784 of file lwgeodetic.c.

References dot_product(), GEOGRAPHIC_EDGE::end, geog2cart(), LW_FALSE, LW_TRUE, LWDEBUG, LWDEBUGF, normalize(), GEOGRAPHIC_EDGE::start, vector_sum(), POINT3D::x, POINT3D::y, and POINT3D::z.

Referenced by edge_contains_point().

785 {
786  POINT3D vcp, vs, ve, vp;
787  double vs_dot_vcp, vp_dot_vcp;
788  geog2cart(&(e->start), &vs);
789  geog2cart(&(e->end), &ve);
790  /* Antipodal case, everything is inside. */
791  if ( vs.x == -1.0 * ve.x && vs.y == -1.0 * ve.y && vs.z == -1.0 * ve.z )
792  return LW_TRUE;
793  geog2cart(p, &vp);
794  /* The normalized sum bisects the angle between start and end. */
795  vector_sum(&vs, &ve, &vcp);
796  normalize(&vcp);
797  /* The projection of start onto the center defines the minimum similarity */
798  vs_dot_vcp = dot_product(&vs, &vcp);
799  LWDEBUGF(4,"vs_dot_vcp %.19g",vs_dot_vcp);
800  /* The projection of candidate p onto the center */
801  vp_dot_vcp = dot_product(&vp, &vcp);
802  LWDEBUGF(4,"vp_dot_vcp %.19g",vp_dot_vcp);
803  /* If p is more similar than start then p is inside the cone */
804  LWDEBUGF(4,"fabs(vp_dot_vcp - vs_dot_vcp) %.39g",fabs(vp_dot_vcp - vs_dot_vcp));
805 
806  /*
807  ** We want to test that vp_dot_vcp is >= vs_dot_vcp but there are
808  ** numerical stability issues for values that are very very nearly
809  ** equal. Unfortunately there are also values of vp_dot_vcp that are legitimately
810  ** very close to but still less than vs_dot_vcp which we also need to catch.
811  ** The tolerance of 10-17 seems to do the trick on 32-bit and 64-bit architectures,
812  ** for the test cases here.
813  ** However, tuning the tolerance value feels like a dangerous hack.
814  ** Fundamentally, the problem is that this test is so sensitive.
815  */
816 
817  /* 1.1102230246251565404236316680908203125e-16 */
818 
819  if ( vp_dot_vcp > vs_dot_vcp || fabs(vp_dot_vcp - vs_dot_vcp) < 2e-16 )
820  {
821  LWDEBUG(4, "point is in cone");
822  return LW_TRUE;
823  }
824  LWDEBUG(4, "point is not in cone");
825  return LW_FALSE;
826 }
void normalize(POINT3D *p)
Normalize to a unit vector.
Definition: lwgeodetic.c:611
double y
Definition: liblwgeom.h:340
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
double x
Definition: liblwgeom.h:340
double z
Definition: liblwgeom.h:340
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:442
#define LW_FALSE
Definition: liblwgeom.h:77
GEOGRAPHIC_POINT start
Definition: lwgeodetic.h:63
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:76
GEOGRAPHIC_POINT end
Definition: lwgeodetic.h:64
void geog2cart(const GEOGRAPHIC_POINT *g, POINT3D *p)
Convert spherical coordinates to cartesion coordinates on unit sphere.
Definition: lwgeodetic.c:400
void vector_sum(const POINT3D *a, const POINT3D *b, POINT3D *n)
Calculate the sum of two vectors.
Definition: lwgeodetic.c:461
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
Here is the call graph for this function:
Here is the caller graph for this function: