#!/usr/bin/perl # converts a Geomview OFF file to a v file # Usage: off2v [OFFfile] # if no OFFfile specified, reads from stdin # The first two integers of a v file are number of vertices # and dimension, respectively. After these two numbers, # the vertices are listed. Whitespace, newlines are irrelevant. # Can deal with "#" comments. use strict ; my($tok,$keyword,$nvert,$dim,$stoff,$coff,$noff,$i,$j,$k,@token_line) ; #goto begin of file format while( defined($tok = get_tok()) ) { last if $tok =~ /OFF$/ ; } defined($tok) || die "couldn't understand file.\n" ; $keyword = $tok ; $dim = 3 ; $dim = 4 if $keyword =~ /4.*OFF/ ; $dim = get_tok() if $keyword =~ /n.*OFF/ ; $nvert = get_tok() ; get_tok() ; #no. faces -- don't care get_tok() ; #no. edges -- don't care $stoff = 1 if $keyword =~ /ST.*OFF/ ; $coff = 1 if $keyword =~ /C.*OFF/ ; $noff = 1 if $keyword =~ /N.*OFF/ ; #print output print "$nvert $dim\n" ; for( $i = 0 ; $i < $nvert ; $i++ ) { for( $j = 0 ; $j < $dim ; $j++ ) { print get_tok()," " ; } if( $stoff ) { for($k=0;$k<8;$k++) { get_tok(); } } if( $coff ) { get_tok();get_tok();get_tok();get_tok(); } if( $noff ) { for($k=0;$k<$dim;$k++) { get_tok(); } } print "\n" ; } ############################################# # parses out "#" comments # returns one token at a time # uses global variable @token_line sub get_tok { my($line,$tok) ; return shift(@token_line) if @token_line ; do { $line = <> ; return undef if !defined($line) ; @token_line = get_line_tokens($line) ; } while(@token_line == 0) ; return shift(@token_line) ; } ############################################# # parses out "#" comments # returns list of tokens from input string sub get_line_tokens { my($line) ; $line = $_[0] ; chomp($line) ; $line =~ s/^\s+// ; #split fn doesn't ignore leading white. perl bug? if($line =~ /#/) { return split(/\s+/, $`) ; } else { return split(/\s+/, $line) ; } }