~ubuntu-branches/ubuntu/saucy/python-scipy/saucy

« back to all changes in this revision

Viewing changes to Lib/sandbox/xplt/plwf.py

  • Committer: Bazaar Package Importer
  • Author(s): Ondrej Certik
  • Date: 2008-06-16 22:58:01 UTC
  • mfrom: (2.1.24 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080616225801-irdhrpcwiocfbcmt
Tags: 0.6.0-12
* The description updated to match the current SciPy (Closes: #489149).
* Standards-Version bumped to 3.8.0 (no action needed)
* Build-Depends: netcdf-dev changed to libnetcdf-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
## Automatically adapted for scipy Oct 31, 2005 by
2
 
 
3
 
# $Id: plwf.py 2182 2006-08-29 07:22:11Z oliphant $
4
 
# Copyright (c) 1996, 1997, The Regents of the University of California.
5
 
# All rights reserved.  See Legal.htm for full text and disclaimer.
6
 
 
7
 
#
8
 
#  PLWF.PY
9
 
#  Simple "painter's algorithm"-class routine for making 3-D wire frames
10
 
#  and related models.
11
 
#
12
 
#  $Id: plwf.py 2182 2006-08-29 07:22:11Z oliphant $
13
 
#
14
 
 
15
 
## execfile ("pl3d.py")
16
 
from types import *
17
 
from pl3d import *
18
 
 
19
 
def plwf (z, y = None, x = None, fill = None, shade = 0, edges = 1,
20
 
   ecolor =  None, ewidth = None, cull = None, scale = None, cmax = None,
21
 
   clear = 1) :
22
 
 
23
 
    """
24
 
    plwf (z)
25
 
    or plwf (z, y, x)
26
 
 
27
 
      plots a 3-D wire frame of the given Z array, which must have the
28
 
      same dimensions as the mesh (X, Y).  If X and Y are not given, they
29
 
      default to the first and second indices of Z, respectively.
30
 
      The drawing order of the zones is determined by a simple "painter's
31
 
      algorithm", which works fairly well if the mesh is reasonably near
32
 
      rectilinear, but can fail even then if the viewpoint is chosen to
33
 
      produce extreme fisheye perspective effects.  Look at the resulting
34
 
      plot carefully to be sure the algorithm has correctly rendered the
35
 
      model in each case.
36
 
 
37
 
    KEYWORDS: fill   -- optional colors to use (default is to make zones
38
 
                        have background color), same dimension options as
39
 
                        for z argument to plf function
40
 
              shade  -- set non-zero to compute shading from current
41
 
                        3D lighting sources
42
 
              edges  -- default is 1 (draw edges), but if you provide fill
43
 
                        colors, you may set to 0 to supress the edges
44
 
              ecolor, ewidth  -- color and width of edges
45
 
              cull   -- default is 1 (cull back surfaces), but if you want
46
 
                        to see the "underside" of the model, set to 0
47
 
              scale  -- by default, Z is scaled to "reasonable" maximum
48
 
                        and minimum values related to the scale of (X,Y).
49
 
                        This keyword alters the default scaling factor, in
50
 
                        the sense that scale=2.0 will produce twice the
51
 
                        Z-relief of the default scale=1.0.
52
 
              cmax   -- the ambient= keyword in light3 can be used to
53
 
                        control how dark the darkest surface is; use this
54
 
                        to control how light the lightest surface is
55
 
                        the lightwf routine can change this parameter
56
 
                        interactively
57
 
 
58
 
    SEE ALSO: lightwf, plm, plf, orient3, light3, fma3, window3
59
 
    """
60
 
 
61
 
    _draw3 = get_draw3_ ( )
62
 
    _square = get_square_ ( )
63
 
    [_xfactor, _yfactor] = get_factors_ ( )
64
 
 
65
 
    if (type (z) == ListType) :
66
 
        xyz = z [0]
67
 
        fill = z [1]
68
 
        shade = z [2]
69
 
        edges = z [3]
70
 
        ecolor = z [4]
71
 
        ewidth = z [5]
72
 
        cull = z [6]
73
 
        cmax = z [7]
74
 
 
75
 
        xyz1 = get3_xy(xyz, 1)
76
 
        x = xyz [0] # the original x
77
 
        y = xyz [1] # the original y
78
 
 
79
 
 
80
 
        # rotate (x,y,0) into on-screen orientation to determine order
81
 
        # just use four corners for this
82
 
        nx = shape (x)
83
 
        ny = nx [1]
84
 
        nx = nx [0]
85
 
        xx = array([[x [0, 0], x[nx - 1, 0]],
86
 
                    [x [0, ny - 1] , x[nx - 1, ny - 1]]])
87
 
        yy = array([[y [0, 0], y[nx - 1, 0]],
88
 
                    [y [0, ny - 1] , y[nx - 1, ny - 1]]])
89
 
        xyzc = array ( [ xx , yy, array ( [ [0., 0.], [0., 0.]])])
90
 
        xyzc = get3_xy(xyzc, 1)
91
 
 
92
 
        # compute mean i-edge and j-edge vector z-components
93
 
        iedge = avg_ (xyzc [2, :, -1] - xyzc [2, :, 0])
94
 
        jedge = avg_ (xyzc [2, -1] - xyzc [2, 0])
95
 
 
96
 
        # compute shading if necessary
97
 
        if (shade) :
98
 
            xyz = xyz1
99
 
            fill = get3_light (xyz)
100
 
        # The order either requires a transpose or not, reversal of the
101
 
        # order of the first dimension or not, and reversal of the order
102
 
        # of the second dimension or not.
103
 
 
104
 
        # The direction with the minimum magnitude average z-component must
105
 
        # vary fastest in the painting order.  If this is the j-direction,
106
 
        # a transpose will be required to make this the i-direction.
107
 
        if abs (iedge) < abs (jedge) :
108
 
            tmp = iedge
109
 
            iedge = jedge
110
 
            jedge = tmp
111
 
            x = transpose (array (xyz1 [0]))
112
 
            y = transpose (array (xyz1 [1]))
113
 
            if fill != None :
114
 
                fill = transpose (fill)
115
 
        else :
116
 
            x = xyz1 [0]
117
 
            y = xyz1 [1]
118
 
 
119
 
        # Zones must be drawn from back to front, which means that the
120
 
        # average z-component of the edge vectors must be positive.  This
121
 
        # can be arranged by reversing the order of the elements if
122
 
        # necessary.
123
 
        if iedge < 0.0 :
124
 
            x = reverse (x, 0)
125
 
            y = reverse (y, 0)
126
 
            if fill != None :
127
 
                fill = reverse (fill, 0)
128
 
        if jedge < 0.0 :
129
 
            x = reverse (x, 1)
130
 
            y = reverse (y, 1)
131
 
            if fill != None :
132
 
                fill = reverse (fill, 1)
133
 
        xmax = maxelt_ (x)
134
 
        xmin = minelt_ (x)
135
 
        ymax = maxelt_ (y)
136
 
        ymin = minelt_ (y)
137
 
        if _xfactor != 1. :
138
 
            xmax = xmax + (_xfactor - 1) * (xmax - xmin) / 2.0
139
 
            xmin = xmin - (_xfactor - 1) * (xmax - xmin) / 2.0
140
 
        if _yfactor != 1. :
141
 
            ymax = ymax + (_yfactor - 1) * (ymax - ymin) / 2.0
142
 
            ymin = ymin - (_yfactor - 1) * (ymax - ymin) / 2.0
143
 
        if _square :
144
 
            xdif = xmax - xmin
145
 
            ydif = ymax - ymin
146
 
            if xdif > ydif :
147
 
                dif = (xdif - ydif) / 2.
148
 
                ymin = ymin - dif
149
 
                ymax = ymax + dif
150
 
            elif ydif > xdif :
151
 
                dif = (ydif - xdif) / 2.
152
 
                xmin = xmin - dif
153
 
                xmax = xmax + dif
154
 
        if fill != None :
155
 
            if len (fill.shape) == 1:
156
 
                fill = bytscl (fill)
157
 
            else:
158
 
                k = fill.shape [0]
159
 
                l = fill.shape [1]
160
 
                fill = reshape ( bytscl (ravel (fill)), (k, l))
161
 
        if cull == 0 : #transparent mesh
162
 
            if ecolor != None :
163
 
                plm (y, x, color = ecolor)
164
 
            else :
165
 
                plm (y, x)
166
 
        elif ecolor != None and ewidth != None and cmax != None :
167
 
            plf (fill, y, x, edges = edges, ecolor = ecolor,
168
 
                 ewidth = ewidth, cmin = 0.0, cmax = cmax, legend = "")
169
 
        elif ecolor != None and ewidth != None :
170
 
            plf (fill, y, x, edges = edges, ewidth = ewidth,
171
 
                 cmin = 0.0, ecolor = ecolor, legend = "")
172
 
        elif ecolor != None and cmax != None :
173
 
            plf (fill, y, x, edges = edges, ecolor = ecolor,
174
 
                 cmin = 0.0, cmax = cmax, legend = "")
175
 
        elif ewidth != None and cmax != None :
176
 
            plf (fill, y, x, edges = edges,  ewidth = ewidth,
177
 
                 cmin = 0.0, cmax = cmax, legend = "")
178
 
        elif ecolor != None :
179
 
            plf (fill, y, x, edges = edges, ecolor = ecolor,
180
 
                 cmin = 0.0, legend = "")
181
 
        elif ewidth != None :
182
 
            plf (fill, y, x, edges = edges, ewidth = ewidth,
183
 
                 cmin = 0.0, legend = "")
184
 
        elif cmax != None :
185
 
            plf (fill, y, x, edges = edges,
186
 
                 cmin = 0.0, cmax = cmax, legend = "")
187
 
        else :
188
 
            plf (fill, y, x, edges = edges, cmin = 0.0, legend = "")
189
 
        return [xmin, xmax, ymin, ymax]
190
 
 
191
 
    xyz = xyz_wf (z, y, x, scale = scale)
192
 
 
193
 
    if clear :
194
 
        clear3 ( )
195
 
    set3_object (plwf, [xyz, fill, shade, edges, ecolor, ewidth, cull, cmax])
196
 
    if ( _draw3 ) :
197
 
        call_idler ( ) # This will traverse and execute the drawing list
198
 
                       # if the default idler has been set.
199
 
 
200
 
_LightwfError = "LightwfError"
201
 
 
202
 
def lightwf (cmax) :
203
 
 
204
 
    """
205
 
    lightwf (cmax)
206
 
      Sets the cmax= parameter interactively, assuming the current
207
 
      3D display list contains the result of a previous plwf call.
208
 
      This changes the color of the brightest surface in the picture.
209
 
      The darkest surface color can be controlled using the ambient=
210
 
      keyword to light3.
211
 
 
212
 
    SEE ALSO: plwf, light3
213
 
    """
214
 
 
215
 
    _draw3_list = get_draw3_list_ ()
216
 
    _draw3_n = get_draw3_n_ ()
217
 
    list = _draw3_list [_draw3_n:]
218
 
    if list [0] != plwf :
219
 
        raise _LightwfError, "current 3D display list is not a plwf"
220
 
    list [1] [7] = cmax
221
 
    undo3_set_ (lightwf, list)
222
 
 
223
 
 
224
 
_Xyz_wfError = "Xyz_wfError"
225
 
 
226
 
def xyz_wf (z, y, x, scale = 1.0) :
227
 
 
228
 
    """
229
 
    xyz_wf (z, [y, x] [,scale = 1.0])
230
 
       returns a 3-by-ni-by-nj array whose 0th entry is x, 1th entry
231
 
       is y, and 2th entry is z. z is ni-by-nj. x and y, if present,
232
 
       must be the same shape. If not present, integer ranges will
233
 
       be used to create an equally spaced coordinate grid in x and y.
234
 
       The function which scales the "topography" of z(x,y) is
235
 
       potentially useful apart from plwf.
236
 
       For example, the xyz array used by plwf can be converted from
237
 
       a quadrilateral mesh plotted using plf to a polygon list plotted
238
 
       using plfp like this:
239
 
         xyz= xyz_wf(z,y,x,scale=scale);
240
 
         ni= shape(z)[1];
241
 
         nj= shape(z)[2];
242
 
         list = ravel (add.outer (
243
 
            ravel(add.outer (adders,zeros(nj-1, Int))) +
244
 
            arange((ni-1)*(nj-1), dtype = Int),
245
 
            array ( [[0, 1], [nj + 1, nj]])))
246
 
         xyz=array([take(ravel(xyz[0]),list,0),
247
 
            take(ravel(xyz[1]),list,0),
248
 
            take(ravel(xyz[2]),list,0)])
249
 
         nxyz= ones((ni-1)*(nj-1)) * 4;
250
 
       The resulting array xyz is 3-by-(4*(nj-1)*(ni-1)).
251
 
       xyz[0:3,4*i:4*(i+1)] are the clockwise coordinates of the
252
 
       vertices of cell number i.
253
 
    """
254
 
 
255
 
    if len (shape (z)) < 2 :
256
 
        raise _Xyz_wfError, "impossible dimensions for z array"
257
 
    nx = shape (z) [0]
258
 
    ny = shape (z) [1]
259
 
    if y == None or x == None :
260
 
        if x != None or y != None :
261
 
            raise _Xyz_wfError, "either give y,x both or neither"
262
 
        x = span (0, ny - 1, ny, nx)
263
 
        y = transpose (span (0, nx - 1, nx, ny))
264
 
    elif shape (x) != shape (z) or shape (y) != shape (z) :
265
 
        raise _Xyz_wfError, "x, y, and z must all have same dimensions"
266
 
    xyscl = max (maxelt_ (x) - minelt_ (x),
267
 
                 maxelt_ (y) - minelt_ (y))
268
 
    if scale != None:
269
 
        xyscl = xyscl * scale
270
 
    dz = maxelt_ (z) - minelt_ (z)
271
 
    zscl= dz + (dz == 0.0)
272
 
    if zscl :
273
 
        z = z * 0.5 * xyscl /zscl
274
 
    xbar = avg_ (x)
275
 
    ybar = avg_ (y)
276
 
    zbar = avg_ (z)
277
 
    xyz = array ( [x - xbar, y - ybar, z - zbar], Float)
278
 
    return (xyz)