PostGIS  3.0.6dev-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 837 of file ptarray.c.

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

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().

Here is the call graph for this function:
Here is the caller graph for this function: