1
## Automatically adapted for scipy Oct 31, 2005 by
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.
9
# Simple "painter's algorithm"-class routine for making 3-D wire frames
12
# $Id: plwf.py 2182 2006-08-29 07:22:11Z oliphant $
15
## execfile ("pl3d.py")
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,
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
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
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
58
SEE ALSO: lightwf, plm, plf, orient3, light3, fma3, window3
61
_draw3 = get_draw3_ ( )
62
_square = get_square_ ( )
63
[_xfactor, _yfactor] = get_factors_ ( )
65
if (type (z) == ListType) :
75
xyz1 = get3_xy(xyz, 1)
76
x = xyz [0] # the original x
77
y = xyz [1] # the original y
80
# rotate (x,y,0) into on-screen orientation to determine order
81
# just use four corners for this
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)
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])
96
# compute shading if necessary
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.
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) :
111
x = transpose (array (xyz1 [0]))
112
y = transpose (array (xyz1 [1]))
114
fill = transpose (fill)
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
127
fill = reverse (fill, 0)
132
fill = reverse (fill, 1)
138
xmax = xmax + (_xfactor - 1) * (xmax - xmin) / 2.0
139
xmin = xmin - (_xfactor - 1) * (xmax - xmin) / 2.0
141
ymax = ymax + (_yfactor - 1) * (ymax - ymin) / 2.0
142
ymin = ymin - (_yfactor - 1) * (ymax - ymin) / 2.0
147
dif = (xdif - ydif) / 2.
151
dif = (ydif - xdif) / 2.
155
if len (fill.shape) == 1:
160
fill = reshape ( bytscl (ravel (fill)), (k, l))
161
if cull == 0 : #transparent mesh
163
plm (y, x, color = ecolor)
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 = "")
185
plf (fill, y, x, edges = edges,
186
cmin = 0.0, cmax = cmax, legend = "")
188
plf (fill, y, x, edges = edges, cmin = 0.0, legend = "")
189
return [xmin, xmax, ymin, ymax]
191
xyz = xyz_wf (z, y, x, scale = scale)
195
set3_object (plwf, [xyz, fill, shade, edges, ecolor, ewidth, cull, cmax])
197
call_idler ( ) # This will traverse and execute the drawing list
198
# if the default idler has been set.
200
_LightwfError = "LightwfError"
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=
212
SEE ALSO: plwf, light3
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"
221
undo3_set_ (lightwf, list)
224
_Xyz_wfError = "Xyz_wfError"
226
def xyz_wf (z, y, x, scale = 1.0) :
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);
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.
255
if len (shape (z)) < 2 :
256
raise _Xyz_wfError, "impossible dimensions for z array"
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))
269
xyscl = xyscl * scale
270
dz = maxelt_ (z) - minelt_ (z)
271
zscl= dz + (dz == 0.0)
273
z = z * 0.5 * xyscl /zscl
277
xyz = array ( [x - xbar, y - ybar, z - zbar], Float)