| 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); |
| 35 |
pol.p = (PLOT_POINT *) R_alloc((size_t) pol.lines, sizeof(PLOT_POINT)); |
| 36 |
|
| 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 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 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 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 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; |
| 120 |
double x; |
| 121 |
double xx=q.x, yy=q.y; |
| 122 |
int Rcross = 0; |
| 123 |
int Lcross = 0; |
| 124 |
|
| 125 |
|
| 126 |
for( i = 0; i < n; i++ ) { |
| 127 |
|
| 128 |
if (( P[i].x - xx )==0 &&( P[i].y - yy )==0 ) return 'v'; |
| 129 |
i1 = ( i + n - 1 ) % n; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
if( (( P[i].y - yy ) > 0 ) != (( P[i1].y - yy ) > 0 ) ) { |
| 139 |
|
| 140 |
|
| 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 |
|
| 144 |
|
| 145 |
|
| 146 |
if (x > 0) Rcross++; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
if ( (( P[i].y - yy ) < 0 ) != (( P[i1].y - yy ) < 0 ) ) { |
| 155 |
|
| 156 |
|
| 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 |
|
| 160 |
|
| 161 |
|
| 162 |
if (x < 0) Lcross++; |
| 163 |
} |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
if( ( Rcross % 2 ) != (Lcross % 2 ) ) |
| 169 |
return 'e'; |
| 170 |
|
| 171 |
|
| 172 |
if( (Rcross % 2) == 1 ) |
| 173 |
return 'i'; |
| 174 |
else |
| 175 |
return 'o'; |
| 176 |
} |