PostGIS  3.0.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 826 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().

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