source: trunk/SRC/Interpolation/inquad.pro @ 325

Last change on this file since 325 was 325, checked in by pinsard, 17 years ago

modification of some headers (+some corrections) to prepare usage of the new idldoc

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