#!/usr/bin/env python # Copyright (c) 2008, Peter Eschler [peschler [at] googlemail.com] # All rights reserved. # Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: # * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. # * Neither the name of the nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. #THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import sys from optparse import OptionParser from os import system, listdir, remove #, getcwd, chdir, path, , waitpid, getenv, mkdir X3D_TEMPLATE = """ %(inline_switches)s """ X3D_COORD_TEMPLATE = """ """ def main(): usage = \ """%prog [options] Convert Radiohead csv data to X3D.""" parser = OptionParser(usage) parser.set_defaults(data_dir='csv', target_dir='x3d', start_frame=1, end_frame=90, globalfile=False, intensity_sizefactor=1.0) parser.add_option("-d", "--dir", dest="data_dir", help="The directory containing the original csv data files.") parser.add_option("-x", "--targetdir", dest="target_dir", help="The target directory containing the converted X3D files.") parser.add_option("-s", "--startframe", type='int', dest="start_frame", help="The index of the frame to start with.") parser.add_option("-e", "--endframe", type='int', dest="end_frame", help="The index of the frame to end with.") #parser.add_option("-c", "--color", #action="store_true", dest="intensity_color", #help="Use the intensity data (4th parameter) as the color of a point.") #parser.add_option("-s", "--size", #action="store_true", dest="intensity_size", #help="Use the intensity data (4th parameter) as the size of a point.") parser.add_option("-f", "--sizefactor", type='float', dest="intensity_sizefactor", help="Intensity value (4th parameter) is multiplied by this (for scaling).") parser.add_option("-b", "--binary", action="store_true", dest="binary", help="When given the inline files will be written as x3db (binary files). Requires aopt tool from instantReality.") parser.add_option("-g", "--globalfile", action="store_true", dest="globalfile", help="When given the main file containing the inlines will be created (using frames between STARTFRAME and ENDFRAME reading from DATA_DIR writing to TARGET_DIR).") (options, args) = parser.parse_args() print "data_dir:", options.data_dir print "start_frame:", options.start_frame print "end_frame:", options.end_frame print "globalfile:", options.globalfile if options.globalfile: print "Creating global file..." main_filename = "main.x3d" main_file = open(main_filename, 'w') inline_line = '' for i in range(options.start_frame, options.end_frame+1): filename = '%s/%s.x3d' % (options.target_dir, i) if options.binary: filename += 'b' inline_line += '\n' % filename main_file.write( X3D_TEMPLATE % {'inline_switches': inline_line} ) main_file.close() sys.exit() # Variables for the fourth data parameter (depth?) max_intensity = min_intensity = 0 # Iterate over all csv files in the data dir csv_files = listdir(options.data_dir) for i in range(options.start_frame, options.end_frame+1): # Build the filename filename = '%s/%s.csv' % (options.data_dir, i) print "Converting csv file %s ..." % filename # Open the file csv_file = open(filename, 'r') # And read all lines lines = csv_file.readlines() csv_file.close() # Now write converted coords to X3D Coordinate file x3d_filename = "%s/%s.x3d" % (options.target_dir, i) x3d_file = open(x3d_filename, 'w') coord_str = '' color_str = '' size_str = '' for line in lines: coords = line.split(',') coord_str += '%s, %s, %s, ' % (coords[0], coords[1], coords[2]) intensity = float(coords[3]) min_intensity = min(min_intensity, intensity) max_intensity = max(max_intensity, intensity) norm_intensity = intensity / 255.0 size = norm_intensity * options.intensity_sizefactor # Calc a color from the intensity value # Adjust colors here... color_str += '%s, %s, %s, ' % (norm_intensity, 1.0-norm_intensity, 0) # Calc a size value from the intensity value (2/3 uniform scale) size_str += '%s %s %s, ' % (size, size, size) x3d_file.write(X3D_COORD_TEMPLATE % {'points': coord_str, 'colors': color_str, 'sizes': size_str}) x3d_file.close() # Check if binary option was given. If so convert file from .x3d to .x3db if options.binary: x3db_filename = x3d_filename + 'b' cmd = 'aopt -i %s -b %s' % (x3d_filename, x3db_filename) system(cmd) remove(x3d_filename) print "min_intensity:", min_intensity print "max_intensity:", max_intensity if __name__ == "__main__": main()