Blame view

src/idl_misc/coyote_for_Dustemwrap/cgpolygon.pro 14.1 KB
6db3528a   Jean-Philippe Bernard   adding librairies...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
; docformat = 'rst'
;
; NAME:
;   cgPolygon
;
; PURPOSE:
;   Provides a device-independent and color-model-independent way to draw a 
;   filled or unfilled polygon.
;
;******************************************************************************************;
;                                                                                          ;
;  Copyright (c) 2010, by Fanning Software Consulting, Inc. All rights reserved.           ;
;                                                                                          ;
;  Redistribution and use in source and binary forms, with or without                      ;
;  modification, are permitted provided that the following conditions are met:             ;
;                                                                                          ;
;      * Redistributions of source code must retain the above copyright                    ;
;        notice, this list of conditions and the following disclaimer.                     ;
;      * Redistributions in binary form must reproduce the above copyright                 ;
;        notice, this list of conditions and the following disclaimer in the               ;
;        documentation and/or other materials provided with the distribution.              ;
;      * Neither the name of Fanning Software Consulting, Inc. nor the names of its        ;
;        contributors may be used to endorse or promote products derived from this         ;
;        software without specific prior written permission.                               ;
;                                                                                          ;
;  THIS SOFTWARE IS PROVIDED BY FANNING SOFTWARE CONSULTING, INC. ''AS IS'' AND ANY        ;
;  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES    ;
;  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT     ;
;  SHALL FANNING SOFTWARE CONSULTING, INC. BE LIABLE FOR ANY DIRECT, INDIRECT,             ;
;  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED    ;
;  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;         ;
;  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND             ;
;  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT              ;
;  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS           ;
;  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                            ;
;******************************************************************************************;
;
;+
;   Provides a device-independent and color-model-independent way to draw a 
;   filled or unfilled polygon.
; 
; The program requires the `Coyote Library <http://www.idlcoyote.com/documents/programs.php>`
; to be installed on your machine.
;
; :Categories:
;    Graphics
;    
; :Params:
;     x: in, required, type=number
4864ca05   Ilyes Choubani   updating Coyote d...
50
51
52
53
54
;        A vector argument providing the X coordinates of the points to be connected. 
;        The vector must contain at least three elements. If only one argument is 
;        specified, X must be an array of either two or three vectors (i.e., (2,*) 
;        or (3,*)). In this special case, the vector X[0,*] specifies the X values, 
;        X[1,*] specifies Y, and X[2,*] contain the Z values.       
6db3528a   Jean-Philippe Bernard   adding librairies...
55
;     y: in, required, type=number
4864ca05   Ilyes Choubani   updating Coyote d...
56
57
;        A vector argument providing the Y coordinates of the points to be connected. 
;        Y must contain at least three elements.
6db3528a   Jean-Philippe Bernard   adding librairies...
58
;     z: in, optional, type=number
4864ca05   Ilyes Choubani   updating Coyote d...
59
60
;        An optional vector argument providing the Z coordinates of the points to be 
;        connected. Z must contain at least three elements.
6db3528a   Jean-Philippe Bernard   adding librairies...
61
62
63
64
65
66
;
; :Keywords:
;     addcmd: in, optional, type=boolean, default=0
;        Set this keyword to add the command to an cgWindow. Setting this keyword
;        automatically sets the WINDOW keyword, but the command does not erase the
;        graphics window as it would normally.
4864ca05   Ilyes Choubani   updating Coyote d...
67
68
69
70
71
72
73
;     checkforfinite: in, optional, type=boolean, default=0
;        If the input data is not finite (i.e., it contains NaNs), then the drawing of
;        the polygons will be affected. This is particularly true if you are drawing polygons
;        on map projections using the `Map_Object` keyword. The program could check all input for NaNs, but
;        this would be quite slow when a great number of polygons are being drawn. For this reason,
;        the program only checks polygon input when this keyword is set. Only polygons containing all
;        finite values are drawn when this keyword is set.
6db3528a   Jean-Philippe Bernard   adding librairies...
74
;     color: in, optional, type=string, default='rose'
4864ca05   Ilyes Choubani   updating Coyote d...
75
76
77
;        The name of the polygon color. Color names are those used with cgColor. 
;        This value can also be a long integer or an index into the current color
;        table.
6db3528a   Jean-Philippe Bernard   adding librairies...
78
;     device: in, optional, type=boolean, default=0
4864ca05   Ilyes Choubani   updating Coyote d...
79
;        Set to indicate the polygon vertices are in device coordinates.
6db3528a   Jean-Philippe Bernard   adding librairies...
80
;     fcolor: in, optional, type=string/integer/long
4864ca05   Ilyes Choubani   updating Coyote d...
81
82
83
;        The name of the polygon fill color. Color names are those used with cgColor. 
;        This value can also be a long integer or an index into the current color
;        table. The default is the same as `Color`.
6db3528a   Jean-Philippe Bernard   adding librairies...
84
;     fill: in, optional, type=boolean, default=0
4864ca05   Ilyes Choubani   updating Coyote d...
85
86
87
88
89
90
91
;        Set this keyword to draw a filled polygon, rather than an open polygon.
;     map_object: in, optional, type=object
;        If you are drawing on a map projection set up with Map_Proj_Init
;        and using projected meter space, rather than lat/lon space, then you can use this
;        keyword to provide a cgMap object that will allow you to convert the `x` and `y`
;        parameters from longitude and latitude, respectively, to projected meter space
;        before drawing. X and Y must both be present.
6db3528a   Jean-Philippe Bernard   adding librairies...
92
;     normal: in, optional, type=boolean, default=0
4864ca05   Ilyes Choubani   updating Coyote d...
93
;        Set to indicate the polygon vertices are in normalized coordinates.
6db3528a   Jean-Philippe Bernard   adding librairies...
94
;     position: in, optional, type=fltarr(4)
4864ca05   Ilyes Choubani   updating Coyote d...
95
96
97
;        Set this keyword to a four-element position array of normalized coordinates
;        to fill a rectangular area on the graphics display. The normal input parameters
;        are ignored if this keyword is set.
6db3528a   Jean-Philippe Bernard   adding librairies...
98
;     window: in, optional, type=boolean, default=0
4864ca05   Ilyes Choubani   updating Coyote d...
99
;        Set this keyword to add the command to the current cgWindow application.
6db3528a   Jean-Philippe Bernard   adding librairies...
100
;     _ref_extra: in, optional, type=appropriate
4864ca05   Ilyes Choubani   updating Coyote d...
101
;        Any other keywords to the IDL POLYFILL command may be used.
6db3528a   Jean-Philippe Bernard   adding librairies...
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
;     
;          
; :Examples:
;    Used like the IDL Plots or Polyfill commands::
;       IDL> cgPolygon, [0.25, 0.25, 0.75, 0.75, 0.25], [0.25, 0.75, 0.75, 0.25, 0.25], $
;                 /NORMAL, COLOR='blue'
;       IDL> cgPolygon, [0.25, 0.25, 0.75, 0.75, 0.25], [0.25, 0.75, 0.75, 0.25, 0.25], $
;                 /NORMAL, COLOR='blue', FCOLOR='rose', /FILL
;       
; :Author:
;       FANNING SOFTWARE CONSULTING::
;           David W. Fanning 
;           1645 Sheely Drive
;           Fort Collins, CO 80526 USA
;           Phone: 970-221-0438
;           E-mail: david@idlcoyote.com
;           Coyote's Guide to IDL Programming: http://www.idlcoyote.com
;
; :History:
;     Change History::
;        Written, 26 March 2012. David Fanning.
;        Added Position keyword to allow a fill of a particular portion of the graphics 
;            window. 20 Apr 2012. DWF.
;        Added AddCmd keyword. 25 Oct 2012. DWF.
4864ca05   Ilyes Choubani   updating Coyote d...
126
127
128
;        Added a MAP_OBJECT keyword to allow polygon filling on maps. 16 Dec 2013. DWF.
;        Completely forgot to deconstruct a single parameter into component parts. 11 Jan 2014. DWF.
;        Added CheckForFinite keyword to check output for NaN values before display. 22 Jan 2014. DWF.
6db3528a   Jean-Philippe Bernard   adding librairies...
129
130
;
; :Copyright:
4864ca05   Ilyes Choubani   updating Coyote d...
131
;     Copyright (c) 2012-2014, Fanning Software Consulting, Inc.
6db3528a   Jean-Philippe Bernard   adding librairies...
132
133
134
;-
PRO cgPolygon, x, y, z, $
    ADDCMD=addcmd, $
4864ca05   Ilyes Choubani   updating Coyote d...
135
    CHECKFORFINITE=checkForFinite, $
6db3528a   Jean-Philippe Bernard   adding librairies...
136
137
138
    COLOR=color, $
    FCOLOR=fcolor, $
    FILL=fill, $
4864ca05   Ilyes Choubani   updating Coyote d...
139
    MAP_OBJECT=map_object, $
6db3528a   Jean-Philippe Bernard   adding librairies...
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
    NORMAL=normal, $
    DEVICE=device, $
    POSITION=position, $
    WINDOW=window, $
     _REF_EXTRA=extra

    Compile_Opt idl2

    Catch, theError
    IF theError NE 0 THEN BEGIN
        Catch, /CANCEL
        void = cgErrorMsg()
        IF N_Elements(currentState) NE 0 THEN cgSetColorState, currentState
        RETURN
    ENDIF

    ; Did user pass parameters?
    IF (N_Params() EQ 0) AND (N_Elements(position) EQ 0) THEN BEGIN
        Print, 'USE SYNTAX: cgPolygon, x, y, [z]'
        RETURN
    ENDIF
    
    ; Do they want this plot in a resizeable graphics window?
    IF Keyword_Set(addcmd) THEN window = 1
    IF Keyword_Set(window) AND ((!D.Flags AND 256) NE 0) THEN BEGIN
    
        currentWindow = cgQuery(/CURRENT, COUNT=wincnt)
        IF wincnt EQ 0 THEN replaceCmd = 0 ELSE replaceCmd=1
        
        ; If adding a command, have to do this differently.
        IF Keyword_Set(addcmd) THEN BEGIN
           cgWindow, 'cgPolygon', x, y, z, $
4864ca05   Ilyes Choubani   updating Coyote d...
172
              CHECKFORFINITE=checkForFinite, $
6db3528a   Jean-Philippe Bernard   adding librairies...
173
174
175
              COLOR=color, $
              FCOLOR=fcolor, $
              FILL=fill, $
4864ca05   Ilyes Choubani   updating Coyote d...
176
              MAP_OBJECT=map_object, $
6db3528a   Jean-Philippe Bernard   adding librairies...
177
178
179
180
181
182
183
184
185
186
187
188
              NORMAL=normal, $
              DEVICE=device, $
              POSITION=position, $
              ADDCMD=1, $
              _EXTRA=extra
                
            RETURN
            
        ENDIF ELSE BEGIN
        
           ; Otherwise, we are just replacing the commands in a new or existing window.
           cgWindow, 'cgPolygon', x, y, z, $
4864ca05   Ilyes Choubani   updating Coyote d...
189
              CHECKFORFINITE=checkForFinite, $
6db3528a   Jean-Philippe Bernard   adding librairies...
190
191
192
              COLOR=color, $
              FCOLOR=fcolor, $
              FILL=fill, $
4864ca05   Ilyes Choubani   updating Coyote d...
193
              MAP_OBJECT=map_object, $
6db3528a   Jean-Philippe Bernard   adding librairies...
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
              NORMAL=normal, $
              DEVICE=device, $
              POSITION=position, $
              REPLACECMD=replaceCmd, $
              _Extra=extra
            
           RETURN
         
         ENDELSE
    ENDIF
    
    ; Set up PostScript device for working with colors.
    IF !D.Name EQ 'PS' THEN Device, COLOR=1, BITS_PER_PIXEL=8
    
    ; We are going to draw in decomposed color, if possible.
    cgSetColorState, 1, Current=currentState
       
4864ca05   Ilyes Choubani   updating Coyote d...
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
    ; Check for NaNs in the data?
    SetDefaultValue, checkForFinite, 0

    ; If we only have a single parameter, see if it can be deconstructed.
    IF N_Params() EQ 1 THEN BEGIN
        dims = Size(x, /Dimensions)
        ndims = Size(x, /N_Dimensions)
        IF ndims EQ 1 THEN Message, 'If a single argument is supplied, it must be a 2D array.'
        IF (dims[0] NE 2) && (dims[0] NE 3) THEN Message, 'Input argument is not dimensioned correctly.'
        CASE dims[0] OF
            2: BEGIN
                y = Reform(x[1,*])
                x = Reform(x[0,*])
            END
            3: BEGIN
                z = Reform(x[2,*])
                y = Reform(x[1,*])
                x = Reform(x[0,*])
            END
        ENDCASE
        
    ENDIF
    
6db3528a   Jean-Philippe Bernard   adding librairies...
234
235
236
237
238
239
240
241
242
243
244
245
246
247
    ; If current state is "indexed color" and colors are represented as long integers then "fix" them.
    IF (currentState EQ 0) THEN BEGIN
      IF Size(color, /TNAME) EQ 'LONG' THEN color = Fix(color)
      IF Size(fcolor, /TNAME) EQ 'LONG' THEN fcolor = Fix(fcolor)
    ENDIF
    
    ; Need a color?
    thisColor = cgDefaultColor(color, DEFAULT='rose')
    IF N_Elements(fColor) EQ 0 THEN BEGIN
       fillColor = thisColor 
    ENDIF ELSE BEGIN
       fillColor = cgDefaultColor(fcolor)
    ENDELSE
    
6db3528a   Jean-Philippe Bernard   adding librairies...
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
    ; Do you need a window?
    IF ((!D.Flags AND 256) NE 0) && (!D.Window LT 0) THEN cgDisplay
    
    ; Are the colors strings? 
    IF Size(thisColor, /TNAME) EQ 'STRING' THEN thisColor = cgColor(thisColor)
    IF Size(fillColor, /TNAME) EQ 'STRING' THEN fillColor = cgColor(fillColor)
    
    ; Are we filling a POSITION in the window?
    IF N_Elements(position) NE 0 THEN BEGIN
       p = position
       x = [p[0], p[0], p[2], p[2], p[0]]
       y = [p[1], p[3], p[3], p[1], p[1]]
       normal = 1
       device = 0
    ENDIF
    
4864ca05   Ilyes Choubani   updating Coyote d...
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
    ; Get the current color vectors.
    TVLCT, rr, gg, bb, /Get
    
    ; Do you have a map object? If so, assume lon/lat conversion to projected XY space.
    ; Check for NaNs, if requested, because some projections can contain a lot of them.
    IF Obj_Valid(map_object) THEN BEGIN
        xy = map_object -> Forward(x, y, /NoForwardFix)
        xr = map_object.xrange
        yr = map_object.yrange
        _x = Reform(xy[0,*])
        _y =  Reform(xy[1,*])
        IF checkForFinite THEN BEGIN
           void = Where(Finite(_x) EQ 0, lonNaNCnt)
           void = Where(Finite(_y) EQ 0, latNaNCnt)
        ENDIF ELSE BEGIN
            lonNaNCnt = 0
            latNaNCnt = 0
        ENDELSE
    ENDIF ELSE BEGIN
        _x = x
        _y = y
        IF checkForFinite THEN BEGIN
           void = Where(Finite(_x) EQ 0, lonNaNCnt)
           void = Where(Finite(_y) EQ 0, latNaNCnt)
        ENDIF ELSE BEGIN
            lonNaNCnt = 0
            latNaNCnt = 0
        ENDELSE
    ENDELSE
    
6db3528a   Jean-Philippe Bernard   adding librairies...
294
    ; Fill the polygon.
4864ca05   Ilyes Choubani   updating Coyote d...
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
    IF (lonNaNCnt EQ 0) && (latNaNCnt EQ 0) THEN BEGIN
        CASE N_Elements(z) OF
            0: BEGIN
                  IF Keyword_Set(fill) THEN BEGIN
                     PolyFill, _x, _y, COLOR=fillColor, NORMAL=normal, DEVICE=device, _EXTRA=extra
                     PlotS, _x, _y, COLOR=thisColor, NORMAL=normal, DEVICE=device, _EXTRA=extra
                  ENDIF ELSE BEGIN
                     PlotS, _x, _y, COLOR=thisColor, NORMAL=normal, DEVICE=device, _EXTRA=extra
                  ENDELSE
               END
            ELSE: BEGIN
                  IF Keyword_Set(fill) THEN BEGIN
                     PolyFill, _x, _y, z, COLOR=fillColor, NORMAL=normal, DEVICE=device, _EXTRA=extra
                     PlotS, _x, _y, z, COLOR=thisColor, NORMAL=normal, DEVICE=device, _EXTRA=extra
                  ENDIF ELSE BEGIN
                     PlotS, _x, _y, z, COLOR=thisColor, NORMAL=normal, DEVICE=device, _EXTRA=extra
                  ENDELSE
               END
        ENDCASE
    ENDIF
6db3528a   Jean-Philippe Bernard   adding librairies...
315
316
317
318
319
320
    
    ; Clean up.
    cgSetColorState, currentState
    IF !D.Name NE 'Z' THEN TVLCT, rr, gg, bb
   
END