Debian Codegraph @

src/pip.c

raw symbols SCIP
1 # include <R.h>
2 # include <Rinternals.h>
3
4 #include "sp.h"
5
6 SEXP R_point_in_polygon_sp(const SEXP px, const SEXP py, const SEXP polx,
7 const SEXP poly) {
8 int i, pc=0;
9 PLOT_POINT p;
10 POLYGON pol;
11 SEXP ret, px1, py1, polx1, poly1;
12
13 if (MAYBE_REFERENCED(px)) {
14 PROTECT(px1 = Rf_duplicate(px));
15 pc++;
16 } else
17 px1 = px;
18 if (MAYBE_REFERENCED(py)) {
19 PROTECT(py1 = Rf_duplicate(py));
20 pc++;
21 } else
22 py1 = py;
23 if (MAYBE_REFERENCED(polx)) {
24 PROTECT(polx1 = Rf_duplicate(polx));
25 pc++;
26 } else
27 polx1 = polx;
28 if (MAYBE_REFERENCED(poly)) {
29 PROTECT(poly1 = Rf_duplicate(poly));
30 pc++;
31 } else
32 poly1 = poly;
33
34 pol.lines = LENGTH(polx); /* check later that first == last */
35 pol.p = (PLOT_POINT *) R_alloc((size_t) pol.lines, sizeof(PLOT_POINT));
36 /* transient; will be freed by R; freed by R on user interrupt */
37 for (i = 0; i < LENGTH(polx); i++) {
38 pol.p[i].x = NUMERIC_POINTER(polx)[i];
39 pol.p[i].y = NUMERIC_POINTER(poly)[i];
40 }
41 pol.close = (pol.p[0].x == pol.p[pol.lines - 1].x &&
42 pol.p[0].y == pol.p[pol.lines - 1].y);
43 setup_poly_minmax(&pol);
44
45 PROTECT(ret = NEW_INTEGER(LENGTH(px))); pc++;
46 for (i = 0; i < LENGTH(px); i++) {
47 p.x = NUMERIC_POINTER(px)[i];
48 p.y = NUMERIC_POINTER(py)[i];
49 /*
50 For each query point q, InPoly returns one of four char's:
51 i : q is strictly interior to P
52 o : q is strictly exterior to P
53 v : q is a vertex of P
54 e : q lies on the relative interior of an edge of P
55 */
56 switch (InPoly(p, &pol)) {
57 case 'i': INTEGER_POINTER(ret)[i] = 1; break;
58 case 'o': INTEGER_POINTER(ret)[i] = 0; break;
59 case 'v': INTEGER_POINTER(ret)[i] = 3; break;
60 case 'e': INTEGER_POINTER(ret)[i] = 2; break;
61 default: INTEGER_POINTER(ret)[i] = -1; break;
62 }
63 }
64 UNPROTECT(pc);
65 return(ret);
66 }
67
68 void setup_poly_minmax(POLYGON *pl) {
69 int i, n=pl->lines;
70 double minx,maxx,miny,maxy;
71
72 minx=miny=DBL_MAX;
73 maxx=maxy=-DBL_MAX;
74
75 for (i=0;i<n;i++) {
76 minx = MIN(minx, pl->p[i].x);
77 miny = MIN(miny, pl->p[i].y);
78 maxx = MAX(maxx, pl->p[i].x);
79 maxy = MAX(maxy, pl->p[i].y);
80 }
81 pl->mbr.min.x = minx;
82 pl->mbr.min.y = miny;
83 pl->mbr.max.x = maxx;
84 pl->mbr.max.y = maxy;
85 }
86
87 /*
88 This code is described in "Computational Geometry in C" (Second Edition),
89 Chapter 7. It is not written to be comprehensible without the
90 explanation in that book.
91
92 For each query point q, InPoly returns one of four char's:
93 i : q is strictly interior to P
94 o : q is strictly exterior to P
95 v : q is a vertex of P
96 e : q lies on the relative interior of an edge of P
97 These represent mutually exclusive categories.
98 For an explanation of the code, see Chapter 7 of
99 "Computational Geometry in C (Second Edition)."
100
101 Written by Joseph O'Rourke, contributions by Min Xu, June 1997.
102 Questions to [email protected].
103 --------------------------------------------------------------------
104 This code is Copyright 1998 by Joseph O'Rourke. It may be freely
105 redistributed in its entirety provided that this copyright notice is
106 not removed.
107 --------------------------------------------------------------------
108 */
109
110 /*
111 InPoly returns a char in {i,o,v,e}. See above for definitions.
112 */
113
114 char InPoly(PLOT_POINT q, POLYGON *Poly)
115 {
116 int n = Poly->lines;
117 PLOT_POINT *P=Poly->p;
118
119 int i, i1; /* point index; i1 = i-1 mod n */
120 double x; /* x intersection of e with ray */
121 double xx=q.x, yy=q.y;
122 int Rcross = 0; /* number of right edge/ray crossings */
123 int Lcross = 0; /* number of left edge/ray crossings */
124
125 /* For each edge e=(i-1,i), see if crosses ray. */
126 for( i = 0; i < n; i++ ) {
127 /* First see if q=(0,0) is a vertex. */
128 if (( P[i].x - xx )==0 &&( P[i].y - yy )==0 ) return 'v';
129 i1 = ( i + n - 1 ) % n;
130 /* printf("e=(%d,%d)\t", i1, i); */
131
132 /* if e "straddles" the x-axis... */
133 /* The commented-out statement is logically equivalent to the one
134 following. */
135 /* if( ( ( P[i].y > 0 ) && ( P[i1].y <= 0 ) ) ||
136 ( ( P[i1].y > 0 ) && ( P[i] .y <= 0 ) ) ) { }*/
137
138 if( (( P[i].y - yy ) > 0 ) != (( P[i1].y - yy ) > 0 ) ) {
139
140 /* e straddles ray, so compute intersection with ray. */
141 x = (( P[i].x - xx) *( P[i1].y - yy ) -( P[i1].x - xx ) *( P[i].y - yy )) /
142 (P[i1].y - P[i].y );
143 /* printf("straddles: x = %g\t", x); */
144
145 /* crosses ray if strictly positive intersection. */
146 if (x > 0) Rcross++;
147 }
148 /* printf("Right cross=%d\t", Rcross); */
149
150 /* if e straddles the x-axis when reversed... */
151 /* if( ( ( P[i] .y < 0 ) && ( P[i1].y >= 0 ) ) ||
152 ( ( P[i1].y < 0 ) && ( P[i] .y >= 0 ) ) ) { }*/
153
154 if ( (( P[i].y - yy ) < 0 ) != (( P[i1].y - yy ) < 0 ) ) {
155
156 /* e straddles ray, so compute intersection with ray. */
157 x = (( P[i].x - xx) *( P[i1].y - yy ) -( P[i1].x - xx ) *( P[i].y - yy )) /
158 (P[i1].y - P[i].y);
159 /* printf("straddles: x = %g\t", x); */
160
161 /* crosses ray if strictly positive intersection. */
162 if (x < 0) Lcross++;
163 }
164 /* printf("Left cross=%d\n", Lcross); */
165 }
166
167 /* q on the edge if left and right cross are not the same parity. */
168 if( ( Rcross % 2 ) != (Lcross % 2 ) )
169 return 'e';
170
171 /* q inside iff an odd number of crossings. */
172 if( (Rcross % 2) == 1 )
173 return 'i';
174 else
175 return 'o';
176 }