PostGIS  2.4.9dev-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 841 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().

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