PostGIS  3.4.0dev-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 858 of file ptarray.c.

859 {
860  int wn = 0;
861  uint32_t i;
862  int side;
863  const POINT2D *seg1;
864  const POINT2D *seg2;
865  const POINT2D *seg3;
866  GBOX gbox;
867 
868  /* Check for not an arc ring (always have odd # of points) */
869  if ( (pa->npoints % 2) == 0 )
870  {
871  lwerror("ptarrayarc_contains_point called with even number of points");
872  return LW_OUTSIDE;
873  }
874 
875  /* Check for not an arc ring (always have >= 3 points) */
876  if ( pa->npoints < 3 )
877  {
878  lwerror("ptarrayarc_contains_point called too-short pointarray");
879  return LW_OUTSIDE;
880  }
881 
882  /* Check for unclosed case */
883  seg1 = getPoint2d_cp(pa, 0);
884  seg3 = getPoint2d_cp(pa, pa->npoints-1);
885  if ( check_closed && ! p2d_same(seg1, seg3) )
886  {
887  lwerror("ptarrayarc_contains_point called on unclosed ring");
888  return LW_OUTSIDE;
889  }
890  /* OK, it's closed. Is it just one circle? */
891  else if ( p2d_same(seg1, seg3) && pa->npoints == 3 )
892  {
893  double radius, d;
894  POINT2D c;
895  seg2 = getPoint2d_cp(pa, 1);
896 
897  /* Wait, it's just a point, so it can't contain anything */
898  if ( lw_arc_is_pt(seg1, seg2, seg3) )
899  return LW_OUTSIDE;
900 
901  /* See if the point is within the circle radius */
902  radius = lw_arc_center(seg1, seg2, seg3, &c);
903  d = distance2d_pt_pt(pt, &c);
904  if ( FP_EQUALS(d, radius) )
905  return LW_BOUNDARY; /* Boundary of circle */
906  else if ( d < radius )
907  return LW_INSIDE; /* Inside circle */
908  else
909  return LW_OUTSIDE; /* Outside circle */
910  }
911  else if ( p2d_same(seg1, pt) || p2d_same(seg3, pt) )
912  {
913  return LW_BOUNDARY; /* Boundary case */
914  }
915 
916  /* Start on the ring */
917  seg1 = getPoint2d_cp(pa, 0);
918  for ( i=1; i < pa->npoints; i += 2 )
919  {
920  seg2 = getPoint2d_cp(pa, i);
921  seg3 = getPoint2d_cp(pa, i+1);
922 
923  /* Catch an easy boundary case */
924  if( p2d_same(seg3, pt) )
925  return LW_BOUNDARY;
926 
927  /* Skip arcs that have no size */
928  if ( lw_arc_is_pt(seg1, seg2, seg3) )
929  {
930  seg1 = seg3;
931  continue;
932  }
933 
934  /* Only test segments in our vertical range */
935  lw_arc_calculate_gbox_cartesian_2d(seg1, seg2, seg3, &gbox);
936  if ( pt->y > gbox.ymax || pt->y < gbox.ymin )
937  {
938  seg1 = seg3;
939  continue;
940  }
941 
942  /* Outside of horizontal range, and not between end points we also skip */
943  if ( (pt->x > gbox.xmax || pt->x < gbox.xmin) &&
944  (pt->y > FP_MAX(seg1->y, seg3->y) || pt->y < FP_MIN(seg1->y, seg3->y)) )
945  {
946  seg1 = seg3;
947  continue;
948  }
949 
950  side = lw_arc_side(seg1, seg2, seg3, pt);
951 
952  /* On the boundary */
953  if ( (side == 0) && lw_pt_in_arc(pt, seg1, seg2, seg3) )
954  {
955  return LW_BOUNDARY;
956  }
957 
958  /* Going "up"! Point to left of arc. */
959  if ( side < 0 && (seg1->y <= pt->y) && (pt->y < seg3->y) )
960  {
961  wn++;
962  }
963 
964  /* Going "down"! */
965  if ( side > 0 && (seg3->y <= pt->y) && (pt->y < seg1->y) )
966  {
967  wn--;
968  }
969 
970  /* Inside the arc! */
971  if ( pt->x <= gbox.xmax && pt->x >= gbox.xmin )
972  {
973  POINT2D C;
974  double radius = lw_arc_center(seg1, seg2, seg3, &C);
975  double d = distance2d_pt_pt(pt, &C);
976 
977  /* On the boundary! */
978  if ( d == radius )
979  return LW_BOUNDARY;
980 
981  /* Within the arc! */
982  if ( d < radius )
983  {
984  /* Left side, increment winding number */
985  if ( side < 0 )
986  wn++;
987  /* Right side, decrement winding number */
988  if ( side > 0 )
989  wn--;
990  }
991  }
992 
993  seg1 = seg3;
994  }
995 
996  /* Sent out the winding number for calls that are building on this as a primitive */
997  if ( winding_number )
998  *winding_number = wn;
999 
1000  /* Outside */
1001  if (wn == 0)
1002  {
1003  return LW_OUTSIDE;
1004  }
1005 
1006  /* Inside */
1007  return LW_INSIDE;
1008 }
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:2398
#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:226
int lw_arc_side(const POINT2D *A1, const POINT2D *A2, const POINT2D *A3, const POINT2D *Q)
Definition: lwalgorithm.c:176
#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:103
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:83
int p2d_same(const POINT2D *p1, const POINT2D *p2)
Definition: lwalgorithm.c:49
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:101
double ymax
Definition: liblwgeom.h:357
double xmax
Definition: liblwgeom.h:355
double ymin
Definition: liblwgeom.h:356
double xmin
Definition: liblwgeom.h:354
double y
Definition: liblwgeom.h:390
double x
Definition: liblwgeom.h:390
uint32_t npoints
Definition: liblwgeom.h:427

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: