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