| 1 |
########################## |
| 2 |
## |
| 3 |
## Title : writeoogl, a package for converting maple PLOT3D |
| 4 |
## objects to Geomview data objects. |
| 5 |
## gvplot, interactive pipe to Geomview from Maple |
| 6 |
## |
| 7 |
## Created : Dec 5 1993 |
| 8 |
## |
| 9 |
## Authors : Frederick Wicklin and |
| 10 |
## Stuart Levy |
| 11 |
## The Geometry Center |
| 12 |
## |
| 13 |
|
| 14 |
## Documentation : currently ?gvplot or ?writeoogl or ?geomview |
| 15 |
## |
| 16 |
## History |
| 17 |
## fjw 9/21/93 initial maple->geomview for Maple VR2 |
| 18 |
## slevy 11/27/93 interactive geomview pipe; |
| 19 |
## backwards compatable to Maple V |
| 20 |
## fjw 11/29/93 debugging; added color options |
| 21 |
## |
| 22 |
## |
| 23 |
## Usage: readlib(gvplot); |
| 24 |
## (optional) gvcommand := `geomview initial-options ...`; |
| 25 |
## (optional) gvdirectories := `/some/dir/ectory:/other/dir...`; |
| 26 |
## 3dplot_struct := plot3d( ... ): |
| 27 |
## writeoogl(`filename`, 3dplot_struct); |
| 28 |
## gvplot(3dplot_struct); |
| 29 |
|
| 30 |
# To search for "geomview" and "togeomview" in directories which might not |
| 31 |
# be on the default UNIX search path, list those director(ies) here, as in |
| 32 |
# default_gvdirectories := `/u/geom/bin:/usr/local/bin:` |
| 33 |
# or, set the "gvdirectories" variable before invoking gvplot(). |
| 34 |
|
| 35 |
default_gvdirectories := ``: |
| 36 |
|
| 37 |
# To start geomview with non-default options, or to start another program |
| 38 |
# via gvplot, put its name and initial arguments here in place of `geomview`, |
| 39 |
# or set the variable "gvcommand" before invoking gvplot(). |
| 40 |
# Changing default_gvcommand, then resaving gvplot.m, alters the default for |
| 41 |
# all users; setting the gvcommand variable changes it for just your session. |
| 42 |
|
| 43 |
default_gvcommand := `geomview`: |
| 44 |
|
| 45 |
|
| 46 |
### Upon invoking the geomview package, maple creates a global |
| 47 |
### variable UsEr_ID__ which is assigned a "random" number. |
| 48 |
### This variable is used to create user-specific files in /tmp. |
| 49 |
################################################################## |
| 50 |
# There doesn't seem to be any explicit test for Maple V1 vs. V2, |
| 51 |
# but all V1 plot objects seem to begin with FUNCTION, so use that fact |
| 52 |
# to detect that we must be careful not to print invalid float constants. |
| 53 |
################################################################## |
| 54 |
`oogl/lprn` := `oogl/lprnV2`: |
| 55 |
|
| 56 |
writeoogl := proc() |
| 57 |
global `oogl/lprn`; |
| 58 |
local header, item, ps, zlist, plist, llist, ppoint, pcolor, appear, |
| 59 |
i, j, totl, xrange, yrange, nx, ny, oldgc, |
| 60 |
coloron,colorlist,ccnt, totalverts; |
| 61 |
options `Copyright 1993 by Frederick Wicklin and Stuart Levy, Geometry Center`; |
| 62 |
|
| 63 |
if nargs > 2 or nargs = 0 then |
| 64 |
ERROR(`Usage:writeoogl(``filename``,3dplot_struct); OR writeoogl(3dplot_struct);`) |
| 65 |
fi; |
| 66 |
ps := args[nargs]; |
| 67 |
if not type(ps, PLOT3D) then ERROR(`Invalid plot structure`,ps, |
| 68 |
`; must be of type PLOT3D, as from plot3d(), tubeplot(), spacecurve(), etc.`) |
| 69 |
fi; |
| 70 |
if nargs = 2 and not type(args[1], string) then |
| 71 |
ERROR(`Invalid file name (not string)`, args[1]) |
| 72 |
fi; |
| 73 |
if nops(ps) < 1 then ERROR(`Empty 3D plot structure!`) fi; |
| 74 |
|
| 75 |
################################################################## |
| 76 |
# When run in command-line mode, Garbage Collection (GC) # |
| 77 |
# messages printed to terminal can interfere with the data flow. # |
| 78 |
# Therefore suppress GC during writing of GeomView data # |
| 79 |
################################################################## |
| 80 |
if nargs = 2 then |
| 81 |
oldgc := status[5]; # words increment for automatic garbage collection |
| 82 |
gc(0); # suppress garbage collection |
| 83 |
print(`Saving Maple 3D structs to Geomview file`, args[1]); |
| 84 |
writeto(args[1]); |
| 85 |
fi; |
| 86 |
xrange := 'xrange'; # nullify this local variable |
| 87 |
appear := find_appearance(1, ps); |
| 88 |
lprint( appear, `{LIST` ); |
| 89 |
|
| 90 |
################################################################## |
| 91 |
### BEGIN main loop over all 3D plots (may be a list of plots) ### |
| 92 |
################################################################## |
| 93 |
for item in ps do |
| 94 |
coloron := FALSE; # default is no color info |
| 95 |
header := op(0,item); |
| 96 |
|
| 97 |
if header = FUNCTION then |
| 98 |
# Maple V1 only: FUNCTION( f(x,y), xrange, yrange ) |
| 99 |
# or FUNCTION( x(u,v), y(u,v), z(u,v), u = u0..u1, v = v0..v1 ) |
| 100 |
xrange := op(nops(item)-1, item); |
| 101 |
if type(xrange, `=`) then xrange := op(2, xrange) fi; |
| 102 |
yrange := op(nops(item), item); |
| 103 |
if type(yrange, `=`) then yrange := op(2, yrange) fi; |
| 104 |
# Maple V1 -- don't trust lprint() |
| 105 |
`oogl/lprn` := `oogl/lprnV1`; |
| 106 |
|
| 107 |
########################### GRID ############################ |
| 108 |
# EX: plot3d( f(x,y), x=xmin..xmax, y=ymin..ymax] |
| 109 |
# Saving Maple GRID struct to geomview ZMESH struct |
| 110 |
# Maple V1: FUNCTION( f(x,y), xrange,yrange ) followed by GRID( zlist ) |
| 111 |
# In Maple V1, we might see either ZMESH-style data (only Z per vertex) |
| 112 |
# or a full 3-D mesh (x,y,z per vertex). Just check the first |
| 113 |
# vertex to see. |
| 114 |
# Maple V2: GRID( xrange, yrange, zlist ) |
| 115 |
# Maple V2 uses GRID only for ZMESH-style data, |
| 116 |
# and uses a MESH header for meshes with general 3-D verts. |
| 117 |
############################################################### |
| 118 |
elif header = GRID then |
| 119 |
if type(xrange, `..`) then |
| 120 |
zlist := item |
| 121 |
else # this is typical VR2 route |
| 122 |
xrange := op(1, item); |
| 123 |
yrange := op(2, item); |
| 124 |
zlist := op(3, item); |
| 125 |
####################################################### |
| 126 |
# if color, then there are 4 possibilities: |
| 127 |
# colorlist = COLOR(HUE, h[1],...,h[nops[colorlist]) |
| 128 |
# colorlist = COLOR(HUE, h) |
| 129 |
# colorlist = COLOR(RGB, r[1],g[1],b[1],...) |
| 130 |
# colorlist = COLOR(RGB, r,g,b) |
| 131 |
# Convert HUES to an RGB value using HSV2RGB and |
| 132 |
# (assume that Maple uses s=v=1). Add a 1 at the end of |
| 133 |
# the list for geomview's "alpha": 0<=>transparent, 1<=>opaque |
| 134 |
####################################################### |
| 135 |
if( nops(item)>3 ) then |
| 136 |
colorlist := find_colorlist(4, item); |
| 137 |
coloron := op(1,colorlist); # either RGB, HUE, or FALSE |
| 138 |
fi; |
| 139 |
fi; |
| 140 |
nx := nops(zlist); |
| 141 |
ny := nops(op(1,zlist)); |
| 142 |
if type(op(1, op(1,zlist)), list) then |
| 143 |
lprint(`{ { MESH`, ny, nx) |
| 144 |
else # scale this data by wrapping it in transformation matrix |
| 145 |
lprint(`{ INST transform { `); |
| 146 |
`oogl/lprn`( [0, (op(2,yrange)-op(1,yrange)) / (ny-1), 0, 0] ); |
| 147 |
`oogl/lprn`( [(op(2,xrange)-op(1,xrange)) / (nx-1), 0, 0, 0] ); |
| 148 |
lprint(0, 0, 1, 0); |
| 149 |
`oogl/lprn`( [op(1,xrange), op(1,yrange), 0, 1] ); |
| 150 |
if(coloron = FALSE) then |
| 151 |
lprint(` } geom { ZMESH`, ny, nx); # gv type |
| 152 |
else |
| 153 |
lprint(` } geom { CZMESH`, ny, nx); # color gv type |
| 154 |
fi; |
| 155 |
fi; |
| 156 |
ccnt := 2; # set color counter to 2 |
| 157 |
for plist in zlist do |
| 158 |
for ppoint in plist do |
| 159 |
`oogl/lprn`(ppoint); |
| 160 |
if (coloron = HUE) and nops(colorlist)>2 then |
| 161 |
lprint(HSV2RGB(op(ccnt,colorlist), 1,1), 1); |
| 162 |
ccnt := ccnt + 1; |
| 163 |
elif (coloron = HUE) and nops(colorlist)=2 then |
| 164 |
lprint(HSV2RGB(op(2,colorlist), 1,1), 1); |
| 165 |
elif (coloron = RGB) and nops(colorlist)>4 then |
| 166 |
lprint(op(ccnt,colorlist),op(ccnt+1,colorlist),op(ccnt+2,colorlist),1); |
| 167 |
ccnt := ccnt + 3; |
| 168 |
elif (coloron = RGB) and nops(colorlist)=4 then |
| 169 |
lprint(op(2,colorlist),op(3,colorlist),op(4,colorlist), 1); |
| 170 |
fi; |
| 171 |
od; |
| 172 |
od; |
| 173 |
lprint( `} }`); |
| 174 |
|
| 175 |
########################### MESH ########################### |
| 176 |
# EX: plot3d( [x(s,t), y(s,t), z(s,t)], s=smin..smax, t=tmin..tmax] |
| 177 |
# Saving Maple MESH struct to geomview MESH struct |
| 178 |
############################################################### |
| 179 |
elif header = MESH then |
| 180 |
llist := op(1, item): |
| 181 |
if( nops(item)>1 ) then |
| 182 |
colorlist := find_colorlist(2, item); |
| 183 |
coloron := op(1,colorlist); # either RGB, HUE, or FALSE |
| 184 |
fi; |
| 185 |
if(coloron=FALSE) then |
| 186 |
lprint(`{ MESH ` ); # gv type |
| 187 |
else |
| 188 |
lprint(`{ CMESH ` ); # color gv type |
| 189 |
fi; |
| 190 |
lprint( nops(llist)); # num x elements |
| 191 |
lprint( nops(op(1,llist)) ); # num y elements |
| 192 |
# Maple store points in row-dominant manner |
| 193 |
# But geomview want column-dominant, so need to |
| 194 |
# print out the transpose of the matrix in the plot3d struct |
| 195 |
nx := nops(llist); # maple rows |
| 196 |
ny := nops(op(1,llist)); # maple cols |
| 197 |
for j from 1 to ny do |
| 198 |
for i from 1 to nx do |
| 199 |
ppoint := op(j, op(i,llist)); |
| 200 |
`oogl/lprn`(ppoint); |
| 201 |
if (coloron = HUE) and nops(colorlist)>2 then |
| 202 |
lprint(HSV2RGB(op((i-1)*ny+j +1,colorlist), 1,1), 1); |
| 203 |
elif (coloron = HUE) and nops(colorlist)=2 then |
| 204 |
lprint(HSV2RGB(op(2,colorlist), 1,1), 1); |
| 205 |
elif (coloron = RGB) and nops(colorlist)>4 then |
| 206 |
lprint(op(3*((i-1)*ny+j-1)+2,colorlist),op(3*((i-1)*ny+j-1)+3,colorlist),op(3*((i-1)*ny+j-1)+4,colorlist),1); |
| 207 |
elif (coloron = RGB) and nops(colorlist)=4 then |
| 208 |
lprint(op(2,colorlist),op(3,colorlist),op(4,colorlist), 1); |
| 209 |
fi; |
| 210 |
od; |
| 211 |
od; |
| 212 |
lprint( `}`); |
| 213 |
|
| 214 |
########################### CURVES ########################## |
| 215 |
# EX: spacecurve( [x(t), y(t), z(t)], t=tmin..tmax] |
| 216 |
# Saving Maple CURVES struct to geomview VECT struct |
| 217 |
############################################################### |
| 218 |
elif header = CURVES then |
| 219 |
llist := select(type, item, list); |
| 220 |
if( nops(item)>1 ) then |
| 221 |
colorlist := find_colorlist(2, item); |
| 222 |
coloron := op(1,colorlist); # either RGB, HUE, or FALSE |
| 223 |
fi; |
| 224 |
lprint(`{ VECT `); # gv type |
| 225 |
# Number of polylines, total vertices. |
| 226 |
totalverts := sum('nops(op(i,llist))', 'i'=1..nops(llist)); |
| 227 |
lprint(nops(llist), totalverts); |
| 228 |
if coloron=FALSE then |
| 229 |
lprint(1); # One color in all |
| 230 |
lprint(seq(nops(op(i,llist)), i=1..nops(llist))); # Vertex counts |
| 231 |
lprint(1, 0 $ 'i'=2..nops(llist)); # Color counts. |
| 232 |
else |
| 233 |
lprint(totalverts); # Total number of colors, 1 per vert |
| 234 |
lprint(seq(nops(op(i,llist)), i=1..nops(llist))); # Vertex counts |
| 235 |
lprint(seq(nops(op(i,llist)), i=1..nops(llist))); # color per vert |
| 236 |
fi; |
| 237 |
for plist in llist do |
| 238 |
for ppoint in plist do # print all vertices |
| 239 |
`oogl/lprn`(ppoint); |
| 240 |
od |
| 241 |
od; |
| 242 |
if coloron=FALSE then |
| 243 |
lprint(1,1,1,1); # color RGBA = white and opaque |
| 244 |
elif (coloron = HUE) and nops(colorlist)>2 then |
| 245 |
for ccnt from 2 to nops(colorlist) do |
| 246 |
lprint(HSV2RGB(op(ccnt,colorlist), 1,1), 1); |
| 247 |
od; |
| 248 |
elif (coloron = HUE) and nops(colorlist)=2 then |
| 249 |
for ccnt from 1 to nops(llist) do |
| 250 |
lprint(HSV2RGB(op(2,colorlist), 1,1), 1); |
| 251 |
od; |
| 252 |
elif (coloron = RGB) and nops(colorlist)>4 then |
| 253 |
for ccnt from 2 by 3 to nops(colorlist) do |
| 254 |
lprint(op(ccnt,colorlist),op(ccnt+1,colorlist),op(ccnt+2,color\list),1); |
| 255 |
od; |
| 256 |
elif (coloron = RGB) and nops(colorlist)=4 then |
| 257 |
for ccnt from 1 to nops(llist) do |
| 258 |
lprint(op(2,colorlist),op(3,colorlist),op(4,colorlist),1); |
| 259 |
od; |
| 260 |
fi; |
| 261 |
lprint( `}`); |
| 262 |
|
| 263 |
######################### POLYGONS ########################## |
| 264 |
#EX:polygonplot3d([seq([seq([x(s,t),y(s,t),z(s,t)],s=smin..smax],tmin..tmax])): |
| 265 |
# Polygons are handled differently than the other objects w/r/t |
| 266 |
# color. Leave color alone and let user change color using geomview. |
| 267 |
############################################################### |
| 268 |
elif header = POLYGONS then |
| 269 |
pcolor := i $ i=1..0; # Empty sequence |
| 270 |
totl := 0: nx := 0: # use nx to count number of polygons |
| 271 |
for llist in item do # sequence of lists POLY(),COLOR(), |
| 272 |
if type(llist,list) then |
| 273 |
nx := nx + 1; # number of polygons |
| 274 |
totl := totl + nops(llist); # compute total number of vertices |
| 275 |
fi |
| 276 |
od; |
| 277 |
|
| 278 |
lprint(find_appearance(1, item)); |
| 279 |
|
| 280 |
# if coloron=FALSE then |
| 281 |
lprint(`{ OFF `); # gv type |
| 282 |
# else |
| 283 |
# lprint(`{ COFF `); # color |
| 284 |
# fi; |
| 285 |
lprint(totl, nx, 0); # total verts,polygons,"edges"(not used!) |
| 286 |
for llist in item do |
| 287 |
if type(llist,list) then |
| 288 |
for ppoint in llist do |
| 289 |
`oogl/lprn`(ppoint); # List of vertices |
| 290 |
od; |
| 291 |
fi; |
| 292 |
od; |
| 293 |
lprint(``); |
| 294 |
totl := 0; |
| 295 |
for llist in item do |
| 296 |
if type(llist,function) |
| 297 |
and (op(0,llist)=COLOR or op(0,llist)=COLOUR) then |
| 298 |
colorlist := llist; |
| 299 |
if op(1,llist)=HUE and nops(colorlist)=2 then |
| 300 |
pcolor := HSV2RGB(op(2,colorlist),1,1); # use ppoint for RGB |
| 301 |
elif op(1,llist)=RGB and nops(colorlist)=4 then |
| 302 |
pcolor := op(2..4,colorlist); |
| 303 |
fi; |
| 304 |
elif type(llist,list) then |
| 305 |
## There are only two possibilities for POLYGONS ## |
| 306 |
## In both cases, it means entire polygon is same color ## |
| 307 |
lprint( nops(llist), i $ i=totl..totl+nops(llist)-1, ` `, pcolor); |
| 308 |
totl := totl + nops(llist); |
| 309 |
fi; |
| 310 |
od; |
| 311 |
lprint( `}`); |
| 312 |
|
| 313 |
######################### POINTS ########################## |
| 314 |
#EX:PLOT3D(POINTS([0,0,1],[1,0,0],[0,1,0])); |
| 315 |
############################################################### |
| 316 |
elif header = POINTS then |
| 317 |
lprint(`{ VECT`, nops(item), nops(item), 0); |
| 318 |
for plist in item do lprint(`1`) od; |
| 319 |
lprint(``); |
| 320 |
for plist in item do lprint(`0`) od; |
| 321 |
lprint(``); |
| 322 |
for ppoint in item do |
| 323 |
`oogl/lprn`(ppoint) |
| 324 |
od; |
| 325 |
lprint(`}`); |
| 326 |
|
| 327 |
########################### TEXT ############################ |
| 328 |
# 3D plots using the "plots" package may include TEXT |
| 329 |
# This is not supported |
| 330 |
############################################################### |
| 331 |
elif header = TEXT then |
| 332 |
print(` `); |
| 333 |
fi; |
| 334 |
od; # END main for loop |
| 335 |
|
| 336 |
lprint( `}`); |
| 337 |
if nargs = 2 then |
| 338 |
writeto(terminal); |
| 339 |
gc(oldgc); |
| 340 |
fi; |
| 341 |
end: |
| 342 |
|
| 343 |
|
| 344 |
gvplot := proc() |
| 345 |
global UsEr_ID__, gvcommand, default_gvcommand, gvdirectories, default_gvdirectories; |
| 346 |
local gvname, gvcmd, gvdirs, ps, oldgc, tmp_fname; |
| 347 |
options `Copyright 1993 by Frederick Wicklin and Stuart Levy, Geometry Center`; |
| 348 |
### Set up filename to use as temporary site for data. ### |
| 349 |
### Take file in /tmp and postfix number which is (hopefully) ### |
| 350 |
### independent of users. Eg, if user1 and user2 are both ### |
| 351 |
### using writeoogl, then they will be writing to different ### |
| 352 |
### files. This is UNIX specific (but so is Geomview). ### |
| 353 |
if UsEr_ID__ = 'UsEr_ID__' then |
| 354 |
UsEr_ID__ := (round( rand() * (time()+1) ) mod 9999) + 1; |
| 355 |
fi; |
| 356 |
tmp_fname := `Maple`.(UsEr_ID__); |
| 357 |
|
| 358 |
### Let user specify, via "gvcommand" and "gvdirectories" global vars, |
| 359 |
### which program/args to run when invoking geomview, |
| 360 |
### and how to set the search path to find it and togeomview. |
| 361 |
|
| 362 |
if gvcommand <> 'gvcommand' |
| 363 |
then gvcmd := gvcommand |
| 364 |
else gvcmd := default_gvcommand |
| 365 |
fi; |
| 366 |
if gvdirectories <> 'gvdirectories' |
| 367 |
then gvdirs := gvdirectories |
| 368 |
else gvdirs := default_gvdirectories |
| 369 |
fi; |
| 370 |
|
| 371 |
ps := args[nargs]; |
| 372 |
gvname := `Maple`; |
| 373 |
|
| 374 |
if nargs < 1 or nargs > 2 then |
| 375 |
ERROR(`Usage: gvplot(3dplot_structure) -or- gvplot(``name``, 3dplot_structure)`); |
| 376 |
fi; |
| 377 |
if nargs > 1 then gvname := args[1] fi; |
| 378 |
if not type( ps, PLOT3D) then |
| 379 |
ERROR(`Invalid plot structure`,ps, |
| 380 |
`; must be of type PLOT3D, as from plot3d(), tubeplot(), spacecurve(), etc.`) |
| 381 |
fi; |
| 382 |
# start geomview reading from stdin |
| 383 |
if system( `PATH=` . gvdirs . `:$PATH togeomview -Mcp `.tmp_fname.` `.gvcmd.`</dev/null` ) <> 0 then |
| 384 |
ERROR(`gvplot: togeomview: Can't start a copy of geomview. `. |
| 385 |
`If "togeomview" or "geomview" were not found `. |
| 386 |
`try setting the variable "gvdirectories" to the name of the directory where `. |
| 387 |
`they're installed (or to a colon-separated list of directories).`); |
| 388 |
fi; |
| 389 |
oldgc := status[5]; # words increment for automatic garbage collection |
| 390 |
gc(0); # suppress garbage collection messages |
| 391 |
writeto(`/tmp/geomview/`.tmp_fname); |
| 392 |
lprint(`(geometry`, gvname); |
| 393 |
writeoogl( ps ); |
| 394 |
lprint(`)`); |
| 395 |
writeto(terminal); |
| 396 |
gc(oldgc); # return to previous garbage collection state |
| 397 |
|
| 398 |
end: |
| 399 |
|
| 400 |
gvcommand := proc() |
| 401 |
global UsEr_ID__, gvcommand, default_gvcommand, gvdirectories, default_gvdirectories; |
| 402 |
local gvname, gvcmd, gvdirs, ps, oldgc, tmp_fname; |
| 403 |
options `Copyright 1993 by Frederick Wicklin and Stuart Levy, Geometry Center`; |
| 404 |
### Set up filename to use as temporary site for data. ### |
| 405 |
### Take file in /tmp and postfix number which is (hopefully) ### |
| 406 |
### independent of users. Eg, if user1 and user2 are both ### |
| 407 |
### using writeoogl, then they will be writing to different ### |
| 408 |
### files. This is UNIX specific (but so is Geomview). ### |
| 409 |
if UsEr_ID__ = 'UsEr_ID__' then |
| 410 |
UsEr_ID__ := (round( rand() * (time()+1) ) mod 9999) + 1; |
| 411 |
fi; |
| 412 |
tmp_fname := `Maple`.(UsEr_ID__); |
| 413 |
|
| 414 |
### Let user specify, via "gvcommand" and "gvdirectories" global vars, |
| 415 |
### which program/args to run when invoking geomview, |
| 416 |
### and how to set the search path to find it and togeomview. |
| 417 |
|
| 418 |
if gvcommand <> 'gvcommand' |
| 419 |
then gvcmd := gvcommand |
| 420 |
else gvcmd := default_gvcommand |
| 421 |
fi; |
| 422 |
if gvdirectories <> 'gvdirectories' |
| 423 |
then gvdirs := gvdirectories |
| 424 |
else gvdirs := default_gvdirectories |
| 425 |
fi; |
| 426 |
|
| 427 |
# start geomview reading from stdin |
| 428 |
if system( `PATH=` . gvdirs . `:$PATH togeomview -Mcp `.tmp_fname.` `.gvcmd.`</dev/null` ) <> 0 then |
| 429 |
ERROR(`gvplot: togeomview: Can't start a copy of geomview. `. |
| 430 |
`If "togeomview" or "geomview" were not found `. |
| 431 |
`try setting the variable "gvdirectories" to the name of the directory where `. |
| 432 |
`they're installed (or to a colon-separated list of directories).`); |
| 433 |
fi; |
| 434 |
oldgc := status[5]; # words increment for automatic garbage collection |
| 435 |
gc(0); # suppress garbage collection messages |
| 436 |
writeto(`/tmp/geomview/`.tmp_fname); |
| 437 |
lprint(args); |
| 438 |
writeto(terminal); |
| 439 |
gc(oldgc); # return to previous garbage collection state |
| 440 |
|
| 441 |
end: |
| 442 |
|
| 443 |
# possible gvplot enhancements: |
| 444 |
# allow remote display? |
| 445 |
# Maybe use a two-step Rube Goldberg hookup with an external converter |
| 446 |
# that directly reads lprint() format. Likely to be faster for large objects. |
| 447 |
# Then gvplot would read |
| 448 |
# gvplot := proc() |
| 449 |
# ... |
| 450 |
# if system(`togeomview -Mcp Maple.raw maple2oogl -togeomview -Mcp Maple`)<>0 |
| 451 |
# then ERROR(...) fi; |
| 452 |
# writeto(`/tmp/geomview/Maple.raw`); |
| 453 |
# lprint(`(geometry`, gvname, `<<`); |
| 454 |
# lprint(3dplot_struct); |
| 455 |
# lprint(`>>)`); |
| 456 |
# writeto(terminal); |
| 457 |
# end: |
| 458 |
# and writeoogl would read |
| 459 |
# writeoogl := proc() |
| 460 |
# ... |
| 461 |
# if system(cat(`togeomview -Mcp Maple.rawdata maple2oogl -o `, fname)) <> 0 |
| 462 |
# then ERROR(...) fi; |
| 463 |
# writeto(`/tmp/geomview/Maple.rawdata`); |
| 464 |
# lprint(`<<', 3dplot_string, `>>'); |
| 465 |
# writeto(terminal); |
| 466 |
# end: |
| 467 |
# |
| 468 |
# Here 'togeomview' is actually being used to start 'maple2oogl' rather |
| 469 |
# than geomview itself. In the first case, maple2oogl -togeomview would |
| 470 |
# invoke code to start a copy of geomview. |
| 471 |
|
| 472 |
# Help information for writeoogl |
| 473 |
`help/text/writeoogl` := TEXT( |
| 474 |
` `, |
| 475 |
`HELP FOR: writeoogl, gvplot `, |
| 476 |
` `, |
| 477 |
`CALLING SEQUENCE: `, |
| 478 |
` readlib(gvplot): `, |
| 479 |
` writeoogl( ``filename``, 3dplot_struct ); `, |
| 480 |
` writeoogl( 3dplot_struct ); `, |
| 481 |
` gvplot( 3dplot_struct ); `, |
| 482 |
` gvplot( ``geomview_name``, 3dplot_struct ) `, |
| 483 |
` `, |
| 484 |
`PARAMETERS: `, |
| 485 |
` filename the file which will contain the Geomview data `, |
| 486 |
` 3dplot_struct a Maple PLOT3D data structure `, |
| 487 |
` geomview_name name of the object in the Geomview browser `, |
| 488 |
` `, |
| 489 |
`SYNOPSIS: `, |
| 490 |
` writeoogl() accepts Maple PLOT3D structures and writes files `, |
| 491 |
` readable by Geomview. gvplot() converts Maple PLOT3D structures`, |
| 492 |
` and displays them immediately using Geomview, starting a copy of`, |
| 493 |
` Geomview if necessary.`, |
| 494 |
``, |
| 495 |
` Supported data types include the Maple structures MESH, GRID, `, |
| 496 |
` CURVES, POINTS, and POLYGONS. `, |
| 497 |
` See ?plot3d[structure] for more information on Maple PLOT3D types.`, |
| 498 |
` `, |
| 499 |
` The Maple MESH and GRID structures are translated into Geomview's`, |
| 500 |
` MESH and ZMESH objects, respectively, Maple CURVES and POINTS`, |
| 501 |
` become VECT objects, and Maple POLYGONS becomes an OFF object.`, |
| 502 |
` `, |
| 503 |
` The Maple TEXT structure is not supported. Color information is`, |
| 504 |
` supported for all objects.`, |
| 505 |
``, |
| 506 |
` The form writeoogl(filename, plot_struct) writes data in OOGL`, |
| 507 |
` (Geomview) form to the given file; writeoogl(plot) writes to the`, |
| 508 |
` current output stream as selected by writeto().`, |
| 509 |
``, |
| 510 |
` gvplot() normally starts the program "geomview", with no options;`, |
| 511 |
` to specify something else, set the variable "gvcommand", as in:`, |
| 512 |
` gvcommand := ``/usr/local/bin/gv -wpos 200x200 -c startup.gv``;`, |
| 513 |
` before invoking gvplot(). Also, if the programs "geomview" or`, |
| 514 |
` "togeomview" are not on your UNIX search path, set the variable`, |
| 515 |
` gvdirectories := ``/some/dir/ectory:/some/other/directory``;`, |
| 516 |
` to the appropriate directory (or directories separated by colons).`, |
| 517 |
` `, |
| 518 |
`EXAMPLES: `, |
| 519 |
` readlib(gvplot): `, |
| 520 |
` my_plot := plot3d(sin(x+y), x=-Pi..Pi, y=-Pi..Pi): `, |
| 521 |
` writeoogl( ``sinxy.mesh``, my_plot ); `, |
| 522 |
` Saving Maple 3D structs to Geomview file, sinxy.mesh `, |
| 523 |
` steiner := plot3d([ sin(2*x)*(cos(y))^2, sin(x)*sin(2*y), `, |
| 524 |
` cos(x)*sin(2*y)], x=-Pi..Pi, y=-Pi..Pi): `, |
| 525 |
` writeoogl( ``steiner.mesh``, steiner); `, |
| 526 |
` Saving Maple 3D structs to Geomview file, steiner.mesh`, |
| 527 |
` # Display it in geomview directly `, |
| 528 |
` gvplot( steiner ); `, |
| 529 |
` # load in the plots package `, |
| 530 |
` with(plots): `, |
| 531 |
` trefoil := spacecurve([ -2*cos(t)- 1/2*cos(5*t)+ 3*sin(2*t), `, |
| 532 |
` -3*cos(2*t)+ 2*sin(t)- 1/2*sin(5*t), 2*cos(3*t), t=0..2*Pi]):`, |
| 533 |
` writeoogl( ``trefoil.vect``, trefoil); `, |
| 534 |
` Saving Maple 3D structs to Geomview file, trefoil.vect`, |
| 535 |
` tetrahedra := [[1,0,0],[-1,0,0],[0,0,1]], [[1,0,0],[0,0,1],[0,1,0]], `, |
| 536 |
` [[0,1,0],[-1,0,0],[0,0,1]], [[1,0,0],[0,1,0],[-1,0,0]]:`, |
| 537 |
` Tplot :=polygonplot3d([tetrahedra]): `, |
| 538 |
` writeoogl( ``tetrahedra.off``, Tplot); `, |
| 539 |
` Saving Maple 3D structs to Geomview file, tetrahedra.off`, |
| 540 |
` # you can even create a single file with multiple objects `, |
| 541 |
` all_objs := display3d( {my_plot, steiner, trefoil, Tplot} ): `, |
| 542 |
` writeoogl( ``all.list``, all_objs ); `, |
| 543 |
` Saving Maple 3D structs to Geomview file, all.list `, |
| 544 |
` `, |
| 545 |
`SEE ALSO: plot3d[structure] ` |
| 546 |
|
| 547 |
): |
| 548 |
|
| 549 |
`help/text/gvplot` := `help/text/writeoogl`: |
| 550 |
`help/text/geomview` := `help/text/writeoogl`: |
| 551 |
|
| 552 |
|
| 553 |
# Convert number to string, avoiding excess digits and avoiding Maple V1's |
| 554 |
# propensity to emit Float(mantissa,exponent) rather than ordinary exponential |
| 555 |
# notation! |
| 556 |
|
| 557 |
`oogl/cvs` := proc(v) |
| 558 |
local sign, absmant; |
| 559 |
options `Copyright 1993 by Stuart Levy, Geometry Center`; |
| 560 |
if type(v, float) then |
| 561 |
absmant := cat(abs(op(1,v))); |
| 562 |
sign := substring(`-`, 1..1-signum(v)); |
| 563 |
if abs(v) >= 1 then |
| 564 |
if(op(2,v) >= 0) then |
| 565 |
cat(op(1,v), '0' $ op(2,v)) |
| 566 |
else |
| 567 |
cat( |
| 568 |
sign, |
| 569 |
substring(absmant, 1..length(absmant)+op(2,v)), |
| 570 |
`.`, |
| 571 |
substring(absmant, length(absmant)+op(2,v)+1..8) |
| 572 |
) |
| 573 |
fi |
| 574 |
elif abs(v) > .000001 then |
| 575 |
cat( sign, |
| 576 |
substring( `.0000000`, 1 .. 1-length(absmant)-op(2,v) ), |
| 577 |
substring(absmant, 1..7) |
| 578 |
) |
| 579 |
else |
| 580 |
cat( sign, |
| 581 |
`.`, substring( absmant, 1..7), |
| 582 |
`e`, op(2,v) + length(absmant) |
| 583 |
) |
| 584 |
fi |
| 585 |
elif type(v, realcons) then |
| 586 |
cat(v) |
| 587 |
fi; |
| 588 |
end: |
| 589 |
|
| 590 |
# |
| 591 |
# lprint a number, or a list of numbers. |
| 592 |
# |
| 593 |
`oogl/lprnV1` := proc (v) |
| 594 |
local i; |
| 595 |
options `Copyright 1993 by Stuart Levy, Geometry Center`; |
| 596 |
if type(v, list) then |
| 597 |
lprint( ' `oogl/cvs`(v[i]) ' $ i=1..nops(v) ); |
| 598 |
elif type(v,float) and abs(v) <= .1 then |
| 599 |
lprint(`oogl/cvs`(v)); |
| 600 |
elif type(v, realcons) then |
| 601 |
lprint(v); |
| 602 |
else |
| 603 |
lprint(`0 #`); |
| 604 |
fi; |
| 605 |
end: |
| 606 |
|
| 607 |
|
| 608 |
# Ditto, but assume we're Maple V2, which prints floating constants |
| 609 |
# in acceptable form. Don't bother to reformat them. |
| 610 |
|
| 611 |
`oogl/lprnV2` := proc(v) |
| 612 |
local i; |
| 613 |
options `Copyright 1993 by Stuart Levy, Geometry Center`; |
| 614 |
if type(v, list) then |
| 615 |
lprint( 'v[i]' $ i = 1..nops(v) ); # Print on one line |
| 616 |
elif type(v, realcons) then |
| 617 |
lprint(v); |
| 618 |
else |
| 619 |
lprint(`0 #`); |
| 620 |
fi |
| 621 |
end: |
| 622 |
|
| 623 |
############################################################### |
| 624 |
## HSV2RGB converts Maple Hues to RGB colors. |
| 625 |
## The HUEs used are in [0,1] so assume that S=V=1 (this is close) |
| 626 |
## The following algorithm is from Computer Graphics |
| 627 |
## By Foley, vanDam, Feiner, Hughes, 2nd Ed. Addison-Wesley |
| 628 |
## p 593 |
| 629 |
## |
| 630 |
## Adapted by fjw 11/29/93 |
| 631 |
## Input: (h,s,v) in [0,1]^3 |
| 632 |
## Output: sequence r,b,g each in [0,1] |
| 633 |
############################################################### |
| 634 |
HSV2RGB := proc( h,s,v ) |
| 635 |
local hh,ip,fp,p,q,t; |
| 636 |
options `Copyright 1990 by Foley, vanDam, Feiner, Hughes`; |
| 637 |
if (s = 0) then # color on B/W center line |
| 638 |
if (h<0) then # There is no hue,ie, hue undefined |
| 639 |
RETURN( v,v,v ); |
| 640 |
else |
| 641 |
ERROR(`Undefined color`); |
| 642 |
fi; |
| 643 |
else |
| 644 |
if (h>1) then hh:=1 fi; |
| 645 |
hh := 6*h; # hh now in [0,6] |
| 646 |
ip := floor(hh); # ip is integer part of h |
| 647 |
fp := hh - ip; # fp is fractional part of h |
| 648 |
p := v*(1-s); |
| 649 |
q := v*(1-(s*fp)); |
| 650 |
t := v*(1-(s*(1-fp))); |
| 651 |
if (ip=0) then |
| 652 |
RETURN( v,t,p ); |
| 653 |
elif (ip=1) then |
| 654 |
RETURN( q,v,p ); |
| 655 |
elif (ip=2) then |
| 656 |
RETURN( p,v,t ); |
| 657 |
elif (ip=3) then |
| 658 |
RETURN( p,q,v ); |
| 659 |
elif (ip=4) then |
| 660 |
RETURN( t,p,v ); |
| 661 |
else |
| 662 |
RETURN( v,p,q ); |
| 663 |
fi; |
| 664 |
fi; |
| 665 |
end: |
| 666 |
|
| 667 |
|
| 668 |
######################################################## |
| 669 |
# find_colorlist |
| 670 |
# input: n where to start looking for color data |
| 671 |
# item what list to search |
| 672 |
# output: list of color information or NULL list |
| 673 |
######################################################## |
| 674 |
find_colorlist := proc(n,item) |
| 675 |
local i; |
| 676 |
options `Copyright 1993 by Frederick Wicklin, Geometry Center`; |
| 677 |
for i from n to nops(item) do |
| 678 |
if type(op(i,item),function) and (op(0,op(i,item))=COLOR or op(0,op(i,item))=COLOUR) then |
| 679 |
RETURN(op(i,item)) |
| 680 |
fi; |
| 681 |
od; |
| 682 |
RETURN([FALSE]); # no color info found |
| 683 |
end: |
| 684 |
|
| 685 |
######################################################## |
| 686 |
# find_appearance |
| 687 |
# input: n index to start seeking color data |
| 688 |
# list list to search |
| 689 |
# output: argument of STYLE option (e.g. `PATCH`) or [] list. |
| 690 |
######################################################## |
| 691 |
find_appearance := proc(n, list) |
| 692 |
local i, part, ap, color, colortype; |
| 693 |
ap := ``; color := ``; |
| 694 |
for i from n to nops(list) do |
| 695 |
part := op(i,list); |
| 696 |
if type(part,function) and (op(0,part)=COLOR or op(0,part)=COLOUR) then |
| 697 |
colortype := op(1,part); |
| 698 |
if colortype = HUE and nops(part) = 2 then |
| 699 |
color := [HSV2RGB(op(2,part), 1, 1)] |
| 700 |
elif colortype = RGB and nops(part) = 4 then |
| 701 |
color := [op(2..4,part)] |
| 702 |
fi |
| 703 |
# Otherwise, this "color" tag isn't for us |
| 704 |
elif type(part,function) and op(0,part) = STYLE then |
| 705 |
if op(1,part) = PATCH then |
| 706 |
ap := cat(ap, ` +face -edge`) |
| 707 |
elif op(1,part) = LINE or op(1,part) = WIREFRAME then |
| 708 |
ap := cat(ap, ` -face +edge`) |
| 709 |
fi |
| 710 |
elif type(part,function) and op(0,part) = THICKNESS and type(op(1,part),integer) then |
| 711 |
ap := cat(ap, ` linewidth `, op(1,part)); |
| 712 |
fi |
| 713 |
od; |
| 714 |
if color <> `` then |
| 715 |
color := cat( op(1..3, map(v -> cat(convert(v,string),` `), color) ) ); |
| 716 |
ap := cat(ap, `\n material { edgecolor `, color, `\n diffuse `, color, `}\n`); |
| 717 |
fi; |
| 718 |
if ap <> `` then |
| 719 |
ap := cat(` appearance { `, ap, `}`) |
| 720 |
fi; |
| 721 |
RETURN(ap); |
| 722 |
end: |
| 723 |
|
| 724 |
[`writeoogl`, `gvplot`]; |