PostGIS  2.3.8dev-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 845 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().

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