[59] | 1 | ;+ |
---|
[232] | 2 | ; |
---|
[136] | 3 | ; @file_comments |
---|
[125] | 4 | ; to find if an (x,y) point is in a quadrilateral (x1,x2,x3,x4) |
---|
[59] | 5 | ; |
---|
[226] | 6 | ; @categories |
---|
[157] | 7 | ; Grid |
---|
[59] | 8 | ; |
---|
[118] | 9 | ; @param x {in}{required} |
---|
| 10 | ; @param y {in}{required} |
---|
[136] | 11 | ; the coordinates of the point we want to know where it is. |
---|
| 12 | ; Must be a scalar if /ONSPHERE activated else can be scalar or array. |
---|
[59] | 13 | ; |
---|
[118] | 14 | ; @param x1 {in}{required} |
---|
| 15 | ; @param y1 {in}{required} |
---|
| 16 | ; @param x2 {in}{required} |
---|
| 17 | ; @param y2 {in}{required} |
---|
| 18 | ; @param x3 {in}{required} |
---|
| 19 | ; @param y3 {in}{required} |
---|
| 20 | ; @param x4 {in}{required} |
---|
| 21 | ; @param y4 {in}{required} |
---|
[136] | 22 | ; the coordinates of the quadrilateral given in the CLOCKWISE order. |
---|
| 23 | ; Scalar or array. |
---|
[59] | 24 | ; |
---|
[136] | 25 | ; @keyword DOUBLE |
---|
| 26 | ; use double precision to perform the computation |
---|
[59] | 27 | ; |
---|
[136] | 28 | ; @keyword ONSPHERE |
---|
[163] | 29 | ; to specify that the quadrilateral are on a sphere and |
---|
| 30 | ; that their coordinates are longitude-latitude coordinates. In this |
---|
[297] | 31 | ; case, east-west periodicity, poles singularity and other pbs |
---|
[136] | 32 | ; related to longitude-latitude coordinates are managed |
---|
| 33 | ; automatically. |
---|
[59] | 34 | ; |
---|
[303] | 35 | ; @keyword DELTA {default=4} |
---|
| 36 | ; to speed up the program, we reduce the aera where we look for potential |
---|
| 37 | ; quadrilaterals containing (x,y). Delta defines the limit of the box |
---|
| 38 | ; centred on (x,y) with a zonal and meridional extent of delta degrees. |
---|
[59] | 39 | ; |
---|
[136] | 40 | ; @keyword NOPRINT |
---|
| 41 | ; to suppress the print messages. |
---|
[125] | 42 | ; |
---|
[118] | 43 | ; @keyword NEWCOORD |
---|
| 44 | ; |
---|
[101] | 45 | ; @returns |
---|
[242] | 46 | ; a n elements vector where n is the number of elements of |
---|
[136] | 47 | ; x. res[i]=j means that the point number i is located in the |
---|
| 48 | ; quadrilateral number j with (0 <= j <= n_elements(x0)-1) |
---|
[59] | 49 | ; |
---|
[125] | 50 | ; @restrictions |
---|
| 51 | ; I think degenerated quadrilateral (e.g. flat of twisted) is not work. |
---|
| 52 | ; This has to be tested. |
---|
[59] | 53 | ; |
---|
[125] | 54 | ; @examples |
---|
[59] | 55 | ; |
---|
[118] | 56 | ; IDL> x = 1.*[1, 2, 6, 7, 3] |
---|
| 57 | ; IDL> y = 1.*[1, 3, 3, 4, 7] |
---|
| 58 | ; IDL> x1 = 1.*[0,4,2] |
---|
| 59 | ; IDL> y1 = 1.*[1,4,8] |
---|
| 60 | ; IDL> x2 = 1.*[1,6,4] |
---|
| 61 | ; IDL> y2 = 1.*[5,6,8] |
---|
| 62 | ; IDL> x3 = 1.*[3,8,4] |
---|
| 63 | ; IDL> y3 = 1.*[4,4,6] |
---|
| 64 | ; IDL> x4 = 1.*[2,6,2] |
---|
| 65 | ; IDL> y4 = 1.*[0,2,6] |
---|
| 66 | ; IDL> splot, [0,10], [0,10], xstyle = 1, ystyle = 1,/nodata |
---|
| 67 | ; IDL> for i=0,2 do oplot, [x4[i],x1[i],x2[i],x3[i],x4[i]],[y4[i],y1[i],y2[i],y3[i],y4[i]] |
---|
| 68 | ; IDL> oplot, x, y, color = 20, psym = 1, thick = 2 |
---|
| 69 | ; IDL> print, inquad(x, y, x1, y1, x2, y2, x3, y3, x4, y4) |
---|
[59] | 70 | ; |
---|
[231] | 71 | ; On a sphere see |
---|
[232] | 72 | ; <pro>clickincell</pro> ... |
---|
[59] | 73 | ; |
---|
[101] | 74 | ; @history |
---|
| 75 | ; Sebastien Masson (smasson\@lodyc.jussieu.fr) |
---|
[59] | 76 | ; August 2003 |
---|
[125] | 77 | ; Based on Convert_clic_ij.pro written by Gurvan Madec |
---|
[59] | 78 | ; |
---|
[231] | 79 | ; @version |
---|
| 80 | ; $Id$ |
---|
[118] | 81 | ; |
---|
[59] | 82 | ;- |
---|
[231] | 83 | ; |
---|
[303] | 84 | FUNCTION inquad, x, y, x1, y1, x2, y2, x3, y3, x4, y4, ONSPHERE = onsphere, DOUBLE = double, DELTA = delta, NOPRINT = noprint, NEWCOORD = newcoord |
---|
[59] | 85 | ; |
---|
[114] | 86 | compile_opt idl2, strictarrsubs |
---|
[59] | 87 | ; |
---|
| 88 | ntofind = n_elements(x) |
---|
| 89 | nquad = n_elements(x2) |
---|
| 90 | ; |
---|
| 91 | IF keyword_set(onsphere) THEN BEGIN |
---|
| 92 | ; save the inputs parameters |
---|
| 93 | xin = x |
---|
| 94 | yin = y |
---|
| 95 | x1in = x1 |
---|
| 96 | y1in = y1 |
---|
| 97 | x2in = x2 |
---|
| 98 | y2in = y2 |
---|
| 99 | x3in = x3 |
---|
| 100 | y3in = y3 |
---|
| 101 | x4in = x4 |
---|
| 102 | y4in = y4 |
---|
| 103 | ; for map_set |
---|
| 104 | x = x MOD 360 |
---|
| 105 | x1 = x1 MOD 360 |
---|
| 106 | x2 = x2 MOD 360 |
---|
| 107 | x3 = x3 MOD 360 |
---|
| 108 | x4 = x4 MOD 360 |
---|
[125] | 109 | ; save !map |
---|
[59] | 110 | save = {map:!map, x:!x, y:!y, z:!z, p:!p} |
---|
[125] | 111 | ; do a satellite projection |
---|
[303] | 112 | IF NOT keyword_set(delta) THEN delta = 4 |
---|
| 113 | map_set, y[0], x[0], 0, /stereo, limit = [y[0]-delta/2, x[0]-delta/2, y[0]+delta/2, x[0]+delta/2], /noerase, /iso, /noborder |
---|
[59] | 114 | ; use normal coordinates to reject cells which are out of the projection. |
---|
[125] | 115 | tmp = convert_coord(x, y, /DATA, /TO_NORMAL, DOUBLE = double) |
---|
| 116 | tmp1 = convert_coord(x1, y1, /DATA, /TO_NORMAL, DOUBLE = double) |
---|
| 117 | tmp2 = convert_coord(x2, y2, /DATA, /TO_NORMAL, DOUBLE = double) |
---|
| 118 | tmp3 = convert_coord(x3, y3, /DATA, /TO_NORMAL, DOUBLE = double) |
---|
| 119 | tmp4 = convert_coord(x4, y4, /DATA, /TO_NORMAL, DOUBLE = double) |
---|
[59] | 120 | ; remove cell which have one corner with coordinates equal to NaN |
---|
| 121 | test = finite(tmp1[0, *]+tmp1[1, *]+tmp2[0, *]+tmp2[1, *] $ |
---|
| 122 | +tmp3[0, *]+tmp3[1, *]+tmp4[0, *]+tmp4[1, *]) |
---|
| 123 | good = where(temporary(test) EQ 1) |
---|
| 124 | ; |
---|
| 125 | IF good[0] EQ -1 THEN BEGIN |
---|
[240] | 126 | IF NOT keyword_set(noprint) THEN BEGIN |
---|
| 127 | ras = report('The point is out of the cells') |
---|
| 128 | ENDIF |
---|
[59] | 129 | ; restore the input parameters |
---|
| 130 | x = temporary(xin) |
---|
| 131 | y = temporary(yin) |
---|
| 132 | x1 = temporary(x1in) |
---|
| 133 | y1 = temporary(y1in) |
---|
| 134 | x2 = temporary(x2in) |
---|
| 135 | y2 = temporary(y2in) |
---|
| 136 | x3 = temporary(x3in) |
---|
| 137 | y3 = temporary(y3in) |
---|
| 138 | x4 = temporary(x4in) |
---|
| 139 | y4 = temporary(y4in) |
---|
| 140 | ; restore old !map... |
---|
| 141 | !map = save.map |
---|
| 142 | !x = save.x |
---|
| 143 | !y = save.y |
---|
| 144 | !z = save.z |
---|
| 145 | !p = save.p |
---|
| 146 | RETURN, -1 |
---|
| 147 | ENDIF |
---|
| 148 | ; |
---|
| 149 | x = tmp[0] |
---|
| 150 | y = tmp[1] |
---|
| 151 | x1 = tmp1[0, good] |
---|
| 152 | y1 = tmp1[1, good] |
---|
| 153 | x2 = tmp2[0, good] |
---|
| 154 | y2 = tmp2[1, good] |
---|
| 155 | x3 = tmp3[0, good] |
---|
| 156 | y3 = tmp3[1, good] |
---|
| 157 | x4 = tmp4[0, good] |
---|
| 158 | y4 = tmp4[1, good] |
---|
| 159 | ; |
---|
| 160 | tmp1 = -1 & tmp2 = -1 & tmp3 = -1 & tmp4 = -1 |
---|
[125] | 161 | ; remove cells which are obviously bad |
---|
[59] | 162 | test = (x1 GT x AND x2 GT x AND x3 GT x AND x4 GT x) $ |
---|
| 163 | OR (x1 LT x AND x2 LT x AND x3 LT x AND x4 LT x) $ |
---|
| 164 | OR (y1 GT y AND y2 GT y AND y3 GT y AND y4 GT y) $ |
---|
| 165 | OR (y1 LT y AND y2 LT y AND y3 LT y AND y4 LT y) |
---|
| 166 | good2 = where(temporary(test) EQ 0) |
---|
| 167 | ; |
---|
| 168 | IF good2[0] EQ -1 THEN BEGIN |
---|
[240] | 169 | IF NOT keyword_set(noprint) THEN BEGIN |
---|
| 170 | ras = report('The point is out of the cells') |
---|
| 171 | ENDIF |
---|
[59] | 172 | ; restore the input parameters |
---|
| 173 | x = temporary(xin) |
---|
| 174 | y = temporary(yin) |
---|
| 175 | x1 = temporary(x1in) |
---|
| 176 | y1 = temporary(y1in) |
---|
| 177 | x2 = temporary(x2in) |
---|
| 178 | y2 = temporary(y2in) |
---|
| 179 | x3 = temporary(x3in) |
---|
| 180 | y3 = temporary(y3in) |
---|
| 181 | x4 = temporary(x4in) |
---|
| 182 | y4 = temporary(y4in) |
---|
| 183 | ; restore old !map... |
---|
| 184 | !map = save.map |
---|
| 185 | !x = save.x |
---|
| 186 | !y = save.y |
---|
| 187 | !z = save.z |
---|
| 188 | !p = save.p |
---|
| 189 | RETURN, -1 |
---|
| 190 | ENDIF |
---|
| 191 | ; |
---|
[125] | 192 | nquad = n_elements(good2) |
---|
[59] | 193 | x1 = x1[good2] |
---|
| 194 | y1 = y1[good2] |
---|
| 195 | x2 = x2[good2] |
---|
| 196 | y2 = y2[good2] |
---|
| 197 | x3 = x3[good2] |
---|
| 198 | y3 = y3[good2] |
---|
| 199 | x4 = x4[good2] |
---|
| 200 | y4 = y4[good2] |
---|
| 201 | ENDIF |
---|
| 202 | ; |
---|
| 203 | ; |
---|
[163] | 204 | ; the point is inside the quadrilateral if test eq 1 |
---|
[59] | 205 | ; with test equal to: |
---|
[101] | 206 | ; test = ((x-x1)*(y2-y1) GE (x2-x1)*(y-y1)) $ |
---|
[59] | 207 | ; *((x-x2)*(y3-y2) GT (x3-x2)*(y-y2)) $ |
---|
| 208 | ; *((x-x3)*(y4-y3) GT (x4-x3)*(y-y3)) $ |
---|
[125] | 209 | ; *((x-x4)*(y1-y4) GE (x1-x4)*(y-y4)) |
---|
[59] | 210 | ; |
---|
| 211 | ; computation of test without any do loop for ntofind points (x,y) and |
---|
[226] | 212 | ; nquad quadrilateral((x1,x2,x3,x4),(y1,y2,y3,y4)) |
---|
[59] | 213 | ; test dimensions are (ntofind, nquad) |
---|
| 214 | ; column i of test corresponds to the intersection of point i with all |
---|
[226] | 215 | ; quadrilateral. |
---|
[125] | 216 | ; row j of test corresponds to all the points localized in cell j |
---|
[303] | 217 | |
---|
| 218 | IF keyword_set(double) THEN one = 1.d ELSE one = 1. |
---|
| 219 | nquad_1 = replicate(one, nquad) |
---|
| 220 | ntofind_1 = replicate(one, ntofind) |
---|
| 221 | x_nquad = x[*]#replicate(one, nquad) |
---|
| 222 | y_nquad = y[*]#replicate(one, nquad) |
---|
| 223 | |
---|
[59] | 224 | test = $ |
---|
[303] | 225 | ; (x-x1)*(y2-y1) GE (x2-x1)*(y-y1) |
---|
| 226 | ( (x_nquad - ntofind_1#x1[*]) * (ntofind_1#(y2-y1)[*]) ) GE ( (ntofind_1#(x2-x1)[*]) * (y_nquad - ntofind_1#y1[*]) ) AND $ |
---|
| 227 | ; *(x-x2)*(y3-y2) GE (x3-x2)*(y-y2) |
---|
| 228 | ( (x_nquad - ntofind_1#x2[*]) * (ntofind_1#(y3-y2)[*]) ) GE ( (ntofind_1#(x3-x2)[*]) * (y_nquad - ntofind_1#y2[*]) ) AND $ |
---|
| 229 | ; *(x-x3)*(y4-y3) GE (x4-x3)*(y-y3) |
---|
| 230 | ( (x_nquad - ntofind_1#x3[*]) * (ntofind_1#(y4-y3)[*]) ) GE ( (ntofind_1#(x4-x3)[*]) * (y_nquad - ntofind_1#y3[*]) ) AND $ |
---|
| 231 | ; *(x-x4)*(y1-y4) GE (x1-x4)*(y-y4) |
---|
| 232 | ( (x_nquad - ntofind_1#x4[*]) * (ntofind_1#(y1-y4)[*]) ) GE ( (ntofind_1#(x1-x4)[*]) * (y_nquad - ntofind_1#y4[*]) ) |
---|
[59] | 233 | ; |
---|
[303] | 234 | nquad_1 = 1 & ntofind_1 = 1 & x_nquad = 1 & y_nquad = 1 ; free memory |
---|
| 235 | ; |
---|
[59] | 236 | ; check test if ntofind gt 1 |
---|
| 237 | ; if ntofind gt 1, each point must be localised in one uniq cell. |
---|
| 238 | IF ntofind GT 1 THEN BEGIN |
---|
| 239 | ; each column of test must have only 1 position equal to one |
---|
| 240 | chtest = total(test, 2) |
---|
| 241 | ; points out of the cells |
---|
| 242 | IF (where(chtest EQ 0))[0] NE -1 THEN BEGIN |
---|
[236] | 243 | IF NOT keyword_set(noprint) THEN BEGIN |
---|
| 244 | ras = report('Points number '+strjoin(strtrim(where(chtest EQ 0), 1), ', ')+' are out of the grid') |
---|
| 245 | ENDIF |
---|
[59] | 246 | stop |
---|
| 247 | ENDIF |
---|
| 248 | ; points in more than one cell |
---|
| 249 | IF (where(chtest GT 1))[0] NE -1 THEN BEGIN |
---|
[236] | 250 | IF NOT keyword_set(noprint) THEN BEGIN |
---|
| 251 | ras = report('Points number '+strjoin(strtrim(where(chtest GT 1), 1), ', ')+' are in more than one cell') |
---|
| 252 | ENDIF |
---|
[59] | 253 | stop |
---|
| 254 | ENDIF |
---|
| 255 | ENDIF |
---|
| 256 | ; find the points for which test eq 1 |
---|
| 257 | found = where(temporary(test) EQ 1) |
---|
| 258 | ; if ntofind eq 1, the point may be localised in more than one grid |
---|
| 259 | ; cell ou may also be out of the cells |
---|
| 260 | IF ntofind EQ 1 THEN BEGIN |
---|
| 261 | CASE 1 OF |
---|
| 262 | found[0] EQ -1:BEGIN |
---|
[236] | 263 | IF NOT keyword_set(noprint) THEN BEGIN |
---|
| 264 | ras = report('The point is out of the cells') |
---|
| 265 | ENDIF |
---|
[59] | 266 | IF keyword_set(onsphere) THEN BEGIN |
---|
| 267 | ; restore old !map... |
---|
| 268 | !map = save.map |
---|
| 269 | !x = save.x |
---|
| 270 | !y = save.y |
---|
| 271 | !z = save.z |
---|
| 272 | !p = save.p |
---|
| 273 | ENDIF |
---|
| 274 | return, -1 |
---|
[125] | 275 | END |
---|
[59] | 276 | n_elements(found) GT ntofind:BEGIN |
---|
[236] | 277 | IF NOT keyword_set(noprint) THEN BEGIN |
---|
| 278 | ras = report('The point is in more than one cell') |
---|
| 279 | ENDIF |
---|
[125] | 280 | END |
---|
[59] | 281 | ELSE: |
---|
| 282 | ENDCASE |
---|
| 283 | ENDIF ELSE BEGIN |
---|
[125] | 284 | ; if ntofind GT 1, found must be sorted |
---|
[59] | 285 | ; i position of found. this corresponds to one x,y point |
---|
| 286 | forsort = found MOD ntofind |
---|
| 287 | ; j position of found. this corresponds to cell in which is one x,y |
---|
| 288 | ; point |
---|
| 289 | found = temporary(found)/ntofind |
---|
[136] | 290 | ; found must be sorted according to forsort |
---|
[59] | 291 | found = found[sort(forsort)] |
---|
| 292 | ENDELSE |
---|
| 293 | ; |
---|
| 294 | IF keyword_set(onsphere) THEN BEGIN |
---|
| 295 | IF arg_present(newcoord) THEN BEGIN |
---|
[110] | 296 | found = found[0] |
---|
[59] | 297 | newcoord = [[x1[found], y1[found]] $ |
---|
| 298 | , [x2[found], y2[found]] $ |
---|
| 299 | , [x3[found], y3[found]] $ |
---|
| 300 | , [x4[found], y4[found]] $ |
---|
| 301 | , [x, y]] |
---|
| 302 | ENDIF |
---|
| 303 | ; |
---|
| 304 | found = good[good2[found]] |
---|
| 305 | ; restore the input parameters |
---|
| 306 | x = temporary(xin) |
---|
| 307 | y = temporary(yin) |
---|
| 308 | x1 = temporary(x1in) |
---|
| 309 | y1 = temporary(y1in) |
---|
| 310 | x2 = temporary(x2in) |
---|
| 311 | y2 = temporary(y2in) |
---|
| 312 | x3 = temporary(x3in) |
---|
| 313 | y3 = temporary(y3in) |
---|
| 314 | x4 = temporary(x4in) |
---|
| 315 | y4 = temporary(y4in) |
---|
| 316 | ; restore old !map... |
---|
| 317 | !map = save.map |
---|
| 318 | !x = save.x |
---|
| 319 | !y = save.y |
---|
| 320 | !z = save.z |
---|
| 321 | !p = save.p |
---|
| 322 | ENDIF |
---|
[297] | 323 | ; |
---|
[59] | 324 | RETURN, found |
---|
| 325 | END |
---|