PostGIS  2.5.0beta1dev-r@@SVN_REVISION@@

◆ ptarrayarc_contains_point_partial()

int ptarrayarc_contains_point_partial ( const POINTARRAY pa,
const POINT2D pt,
int  check_closed,
int *  winding_number 
)

Definition at line 832 of file ptarray.c.

References distance2d_pt_pt(), FP_EQUALS, FP_MAX, FP_MIN, getPoint2d_cp(), lw_arc_calculate_gbox_cartesian_2d(), lw_arc_center(), lw_arc_is_pt(), lw_arc_side(), LW_BOUNDARY, LW_INSIDE, LW_OUTSIDE, lw_pt_in_arc(), lwerror(), POINTARRAY::npoints, p2d_same(), POINT2D::x, GBOX::xmax, GBOX::xmin, POINT2D::y, GBOX::ymax, and GBOX::ymin.

Referenced by lwcompound_contains_point(), and ptarrayarc_contains_point().

833 {
834  int wn = 0;
835  uint32_t i;
836  int side;
837  const POINT2D *seg1;
838  const POINT2D *seg2;
839  const POINT2D *seg3;
840  GBOX gbox;
841 
842  /* Check for not an arc ring (always have odd # of points) */
843  if ( (pa->npoints % 2) == 0 )
844  {
845  lwerror("ptarrayarc_contains_point called with even number of points");
846  return LW_OUTSIDE;
847  }
848 
849  /* Check for not an arc ring (always have >= 3 points) */
850  if ( pa->npoints < 3 )
851  {
852  lwerror("ptarrayarc_contains_point called too-short pointarray");
853  return LW_OUTSIDE;
854  }
855 
856  /* Check for unclosed case */
857  seg1 = getPoint2d_cp(pa, 0);
858  seg3 = getPoint2d_cp(pa, pa->npoints-1);
859  if ( check_closed && ! p2d_same(seg1, seg3) )
860  {
861  lwerror("ptarrayarc_contains_point called on unclosed ring");
862  return LW_OUTSIDE;
863  }
864  /* OK, it's closed. Is it just one circle? */
865  else if ( p2d_same(seg1, seg3) && pa->npoints == 3 )
866  {
867  double radius, d;
868  POINT2D c;
869  seg2 = getPoint2d_cp(pa, 1);
870 
871  /* Wait, it's just a point, so it can't contain anything */
872  if ( lw_arc_is_pt(seg1, seg2, seg3) )
873  return LW_OUTSIDE;
874 
875  /* See if the point is within the circle radius */
876  radius = lw_arc_center(seg1, seg2, seg3, &c);
877  d = distance2d_pt_pt(pt, &c);
878  if ( FP_EQUALS(d, radius) )
879  return LW_BOUNDARY; /* Boundary of circle */
880  else if ( d < radius )
881  return LW_INSIDE; /* Inside circle */
882  else
883  return LW_OUTSIDE; /* Outside circle */
884  }
885  else if ( p2d_same(seg1, pt) || p2d_same(seg3, pt) )
886  {
887  return LW_BOUNDARY; /* Boundary case */
888  }
889 
890  /* Start on the ring */
891  seg1 = getPoint2d_cp(pa, 0);
892  for ( i=1; i < pa->npoints; i += 2 )
893  {
894  seg2 = getPoint2d_cp(pa, i);
895  seg3 = getPoint2d_cp(pa, i+1);
896 
897  /* Catch an easy boundary case */
898  if( p2d_same(seg3, pt) )
899  return LW_BOUNDARY;
900 
901  /* Skip arcs that have no size */
902  if ( lw_arc_is_pt(seg1, seg2, seg3) )
903  {
904  seg1 = seg3;
905  continue;
906  }
907 
908  /* Only test segments in our vertical range */
909  lw_arc_calculate_gbox_cartesian_2d(seg1, seg2, seg3, &gbox);
910  if ( pt->y > gbox.ymax || pt->y < gbox.ymin )
911  {
912  seg1 = seg3;
913  continue;
914  }
915 
916  /* Outside of horizontal range, and not between end points we also skip */
917  if ( (pt->x > gbox.xmax || pt->x < gbox.xmin) &&
918  (pt->y > FP_MAX(seg1->y, seg3->y) || pt->y < FP_MIN(seg1->y, seg3->y)) )
919  {
920  seg1 = seg3;
921  continue;
922  }
923 
924  side = lw_arc_side(seg1, seg2, seg3, pt);
925 
926  /* On the boundary */
927  if ( (side == 0) && lw_pt_in_arc(pt, seg1, seg2, seg3) )
928  {
929  return LW_BOUNDARY;
930  }
931 
932  /* Going "up"! Point to left of arc. */
933  if ( side < 0 && (seg1->y <= pt->y) && (pt->y < seg3->y) )
934  {
935  wn++;
936  }
937 
938  /* Going "down"! */
939  if ( side > 0 && (seg2->y <= pt->y) && (pt->y < seg1->y) )
940  {
941  wn--;
942  }
943 
944  /* Inside the arc! */
945  if ( pt->x <= gbox.xmax && pt->x >= gbox.xmin )
946  {
947  POINT2D C;
948  double radius = lw_arc_center(seg1, seg2, seg3, &C);
949  double d = distance2d_pt_pt(pt, &C);
950 
951  /* On the boundary! */
952  if ( d == radius )
953  return LW_BOUNDARY;
954 
955  /* Within the arc! */
956  if ( d < radius )
957  {
958  /* Left side, increment winding number */
959  if ( side < 0 )
960  wn++;
961  /* Right side, decrement winding number */
962  if ( side > 0 )
963  wn--;
964  }
965  }
966 
967  seg1 = seg3;
968  }
969 
970  /* Sent out the winding number for calls that are building on this as a primitive */
971  if ( winding_number )
972  *winding_number = wn;
973 
974  /* Outside */
975  if (wn == 0)
976  {
977  return LW_OUTSIDE;
978  }
979 
980  /* Inside */
981  return LW_INSIDE;
982 }
double lw_arc_center(const POINT2D *p1, const POINT2D *p2, const POINT2D *p3, POINT2D *result)
Determines the center of the circle defined by the three given points.
Definition: lwalgorithm.c:228
int lw_arc_is_pt(const POINT2D *A1, const POINT2D *A2, const POINT2D *A3)
Returns true if arc A is actually a point (all vertices are the same) .
Definition: lwalgorithm.c:105
int lw_arc_calculate_gbox_cartesian_2d(const POINT2D *A1, const POINT2D *A2, const POINT2D *A3, GBOX *gbox)
Definition: g_box.c:459
double xmax
Definition: liblwgeom.h:295
double distance2d_pt_pt(const POINT2D *p1, const POINT2D *p2)
Definition: measures.c:2313
#define FP_MIN(A, B)
unsigned int uint32_t
Definition: uthash.h:78
double x
Definition: liblwgeom.h:330
int p2d_same(const POINT2D *p1, const POINT2D *p2)
Definition: lwalgorithm.c:49
double ymin
Definition: liblwgeom.h:296
double xmin
Definition: liblwgeom.h:294
#define LW_INSIDE
Constants for point-in-polygon return values.
double ymax
Definition: liblwgeom.h:297
double y
Definition: liblwgeom.h:330
int lw_arc_side(const POINT2D *A1, const POINT2D *A2, const POINT2D *A3, const POINT2D *Q)
Definition: lwalgorithm.c:178
#define LW_BOUNDARY
int lw_pt_in_arc(const POINT2D *P, const POINT2D *A1, const POINT2D *A2, const POINT2D *A3)
Returns true if P is on the same side of the plane partition defined by A1/A3 as A2 is...
Definition: lwalgorithm.c:85
#define FP_EQUALS(A, B)
#define LW_OUTSIDE
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
#define FP_MAX(A, B)
const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from...
Definition: lwgeom_api.c:364
uint32_t npoints
Definition: liblwgeom.h:373
Here is the call graph for this function:
Here is the caller graph for this function: