PostGIS  2.5.0dev-r@@SVN_REVISION@@
int ptarrayarc_contains_point_partial ( const POINTARRAY pa,
const POINT2D pt,
int  check_closed,
int *  winding_number 
)

Definition at line 834 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().

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

Here is the call graph for this function:

Here is the caller graph for this function: