PostGIS  2.5.7dev-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 839 of file ptarray.c.

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

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: