1 |
#!/usr/bin/env ruby |
2 |
# RCSid: $Id: objview.rb,v 2.1 2012/08/07 18:17:17 greg Exp $ |
3 |
# |
4 |
# objview.rb, v1.1 2012.07.04 RPG |
5 |
# Ruby port of objview.csh |
6 |
# |
7 |
# Make a nice view of an object, or luminaire |
8 |
# Arguments are scene input files |
9 |
# |
10 |
|
11 |
require 'optparse' |
12 |
require 'ostruct' |
13 |
require 'tempfile' |
14 |
|
15 |
class Optparse |
16 |
# |
17 |
# Return a structure describing the options. |
18 |
# |
19 |
def self.parse(args) |
20 |
# The options specified on the command line will be collected in *options*. |
21 |
options = OpenStruct.new |
22 |
|
23 |
opts = OptionParser.new do |opts| |
24 |
opts.banner = "Usage: (ruby) objview.rb [options] [radiance scene input files]" |
25 |
|
26 |
opts.separator "" |
27 |
opts.separator "Options:" |
28 |
|
29 |
options.radopt = [] |
30 |
options.glradopt = [] |
31 |
|
32 |
options.up = "Z" |
33 |
opts.on("-u, --up", String, [:X, :Y, :Z], "View up vector (X, Y, or Z)") do |n| |
34 |
options.up = n |
35 |
end |
36 |
|
37 |
#Specify display device (qt is only option for Windows) |
38 |
if /mswin/.match(RUBY_PLATFORM) or /mingw/.match(RUBY_PLATFORM) |
39 |
options.dev = "qt" |
40 |
else |
41 |
options.dev = "x11" |
42 |
end |
43 |
opts.on("-o, --odev", String, [:x11, :qt], "Output device (x11 or qt)") do |n| |
44 |
options.dev = n |
45 |
options.radopt << "-o #{options.dev}" |
46 |
end |
47 |
|
48 |
options.procs = "1" |
49 |
opts.on("-N, --nprocs", String, "Number of processors (UNIX ony)") do |n| |
50 |
options.procs = n |
51 |
options.radopt << "-N #{options.procs}" |
52 |
end |
53 |
|
54 |
options.vw = "XYZ" |
55 |
opts.on("-v", "--view", String, "View specification") do |n| |
56 |
options.view = n |
57 |
options.radopt << "-v #{options.view}" |
58 |
end |
59 |
|
60 |
options.ltv = false |
61 |
opts.on("-l", "--ltview", "Luminaire viewer mode") do |n| |
62 |
options.ltv = true |
63 |
options.vw = "y" |
64 |
end |
65 |
|
66 |
#set "room" size for ltview, default is 4' square box (in meters) |
67 |
options.rsiz = "0.6096" |
68 |
opts.on("-r", "--roomsize", String, "Room scaling factor for ltview") do |n| |
69 |
options.rsiz = n |
70 |
end |
71 |
|
72 |
opts.on("-d", "--debug", "Run verbosely") do |n| |
73 |
options.verbose = true |
74 |
end |
75 |
|
76 |
opts.on("-g", "--ogl", "Use OpenGL (glrad)") do |n| |
77 |
options.ogl = true |
78 |
end |
79 |
|
80 |
opts.on("-e", "--rad", "Set rad options") do |n| |
81 |
options.radopt << n |
82 |
end |
83 |
|
84 |
opts.on("-S", "--glrad", "Set glrad options") do |n| |
85 |
options.glradopt = "-S #{n}" |
86 |
end |
87 |
|
88 |
# No argument, shows at tail. This will print an options summary. |
89 |
opts.on_tail("-h", "--help", "Show this message") do |
90 |
puts opts |
91 |
exit |
92 |
end |
93 |
|
94 |
end |
95 |
|
96 |
opts.parse!(args) |
97 |
options |
98 |
|
99 |
end # parse() |
100 |
|
101 |
end # class Optparse |
102 |
|
103 |
options = Optparse.parse(ARGV) |
104 |
|
105 |
# utility function: print statement and execute as system call |
106 |
def exec_statement(s) |
107 |
if /mswin/.match(RUBY_PLATFORM) or /mingw/.match(RUBY_PLATFORM) |
108 |
s = s.gsub("/", "\\") |
109 |
end |
110 |
puts "'#{s}'" |
111 |
result = system(s) |
112 |
puts |
113 |
return result |
114 |
end |
115 |
|
116 |
exec = "system" |
117 |
if options.verbose == true |
118 |
puts options |
119 |
puts "Input file(s): #{ARGV}" |
120 |
exec = "exec_statement" |
121 |
end |
122 |
|
123 |
#not needed(?) |
124 |
#octree = Tempfile.new(['objview_', '.oct']) |
125 |
#ambf = Tempfile.new(['objview_', '.amb']) |
126 |
|
127 |
if options.ltv == true |
128 |
rendopts = "-ab 1 -ds .15" |
129 |
end |
130 |
|
131 |
# not sure how these options are supposed to work... RPG |
132 |
# |
133 |
# case -s: |
134 |
# case -w: |
135 |
# set opts=($opts $argv[1]) |
136 |
# breaksw |
137 |
# case -b*: |
138 |
# set rendopts=($rendopts -bv) |
139 |
# breaksw |
140 |
# case -V: |
141 |
# |
142 |
|
143 |
|
144 |
if not ARGV[0] |
145 |
puts "No input files specified" |
146 |
exit 1 |
147 |
end |
148 |
|
149 |
if options.ogl == true |
150 |
if rendopts |
151 |
puts "bad option for glrad" |
152 |
exit 1 |
153 |
else |
154 |
if options.glradopt == true |
155 |
puts "bad option for rad" |
156 |
exit 1 |
157 |
end |
158 |
end |
159 |
end |
160 |
|
161 |
if options.ltv == false |
162 |
# create lights input file for objview |
163 |
lights = Tempfile.new(['lt_', '.rad']) |
164 |
lights.write("void glow dim 0 0 4 .1 .1 .15 0\n") |
165 |
lights.write("dim source background 0 0 4 0 0 1 360\n") |
166 |
lights.write("void light bright 0 0 3 1000 1000 1000\n") |
167 |
lights.write("bright source sun1 0 0 4 1 .2 1 5\n") |
168 |
lights.write("bright source sun2 0 0 4 .3 1 1 5\n") |
169 |
lights.write("bright source sun3 0 0 4 -1 -.7 1 5\n") |
170 |
if options.verbose == true |
171 |
lights.rewind |
172 |
lightsread = lights.read |
173 |
puts "\nlights file:" |
174 |
puts lightsread |
175 |
end |
176 |
lights.close |
177 |
end |
178 |
|
179 |
if options.ltv == true |
180 |
#create room input file for ltview |
181 |
room = Tempfile.new(['lt_', '.rad']) |
182 |
room.write("void plastic surf\n0\n0\n5\n.2 .2 .2 0 0\n\n") |
183 |
room.write("surf polygon floor\n0\n0\n12\n") |
184 |
room.write("-#{options.rsiz} #{options.rsiz} -#{options.rsiz}\n") |
185 |
room.write("-#{options.rsiz} -#{options.rsiz} -#{options.rsiz}\n") |
186 |
room.write("#{options.rsiz} -#{options.rsiz} -#{options.rsiz}\n") |
187 |
room.write("#{options.rsiz} #{options.rsiz} -#{options.rsiz}\n\n") |
188 |
|
189 |
room.write("surf polygon clg\n0\n0\n12\n") |
190 |
room.write("-#{options.rsiz} #{options.rsiz} #{options.rsiz}\n") |
191 |
room.write("-#{options.rsiz} -#{options.rsiz} #{options.rsiz}\n") |
192 |
room.write("#{options.rsiz} -#{options.rsiz} #{options.rsiz}\n") |
193 |
room.write("#{options.rsiz} #{options.rsiz} #{options.rsiz}\n\n") |
194 |
|
195 |
room.write("surf polygon wall-north\n0\n0\n12\n") |
196 |
room.write("-#{options.rsiz} #{options.rsiz} -#{options.rsiz}\n") |
197 |
room.write("#{options.rsiz} #{options.rsiz} -#{options.rsiz}\n") |
198 |
room.write("#{options.rsiz} #{options.rsiz} #{options.rsiz}\n") |
199 |
room.write("-#{options.rsiz} #{options.rsiz} #{options.rsiz}\n\n") |
200 |
|
201 |
room.write("surf polygon wall-east\n0\n0\n12\n") |
202 |
room.write("#{options.rsiz} #{options.rsiz} -#{options.rsiz}\n") |
203 |
room.write("#{options.rsiz} -#{options.rsiz} -#{options.rsiz}\n") |
204 |
room.write("#{options.rsiz} -#{options.rsiz} #{options.rsiz}\n") |
205 |
room.write("#{options.rsiz} #{options.rsiz} #{options.rsiz}\n\n") |
206 |
|
207 |
room.write("surf polygon wall-west\n0\n0\n12\n") |
208 |
room.write("-#{options.rsiz} #{options.rsiz} -#{options.rsiz}\n") |
209 |
room.write("-#{options.rsiz} #{options.rsiz} #{options.rsiz}\n") |
210 |
room.write("-#{options.rsiz} -#{options.rsiz} #{options.rsiz}\n") |
211 |
room.write("-#{options.rsiz} -#{options.rsiz} -#{options.rsiz}\n\n") |
212 |
if options.verbose == true |
213 |
room.rewind |
214 |
roomread = room.read |
215 |
puts "\nroom file:" |
216 |
puts roomread |
217 |
end |
218 |
room.close |
219 |
end |
220 |
|
221 |
# create .rif file |
222 |
rif = Tempfile.new(['ov_', '.rif']) |
223 |
if options.ltv == true |
224 |
rif.write("scene= #{ARGV.join(' ')} #{room.path}\n") |
225 |
else |
226 |
rif.write("scene= #{ARGV.join(' ')} #{lights.path}\n") |
227 |
end |
228 |
rif.write("EXPOSURE= .5\n") |
229 |
rif.write("UP= #{options.up}\n") |
230 |
rif.write("view= #{options.vw}\n") |
231 |
rif.write("oconv= -f\n") |
232 |
rif.write("render= #{rendopts}\n") |
233 |
#doesn't rad handle the construction and destruction of these files (below) on its own? RPG |
234 |
#rif.write("OCTREE= #{octree.path}\n") |
235 |
#rif.write("AMBF= #{ambf.path}\n") |
236 |
if options.verbose == true |
237 |
rif.rewind |
238 |
rifread = rif.read |
239 |
puts "\nrif file:" |
240 |
puts rifread |
241 |
end |
242 |
rif.close |
243 |
|
244 |
puts "radopt = #{options.radopt.join(" ")}" |
245 |
|
246 |
#run executive control program of choice |
247 |
if options.ogl == true |
248 |
exec("glrad #{options.radopt.join(" ")} #{rif.path}") |
249 |
else |
250 |
exec("rad -o #{options.dev} #{options.radopt.join(" ")} #{rif.path}") |
251 |
end |
252 |
|
253 |
# clean up temp files |
254 |
if coptions.ltv == true |
255 |
room.unlink |
256 |
else |
257 |
lights.unlink |
258 |
end |
259 |
rif.unlink |
260 |
#ambf.unlink |
261 |
#octree.unlink |
262 |
|
263 |
exit 0 |
264 |
|