#!/bin/perl
#
# $Id: off2vrml,v 1.3 1995/06/02 19:13:54 mathews Exp $
#
# Convert ASCII files from indexed polygons in 3-D Object File Format (OFF)
# to simple mono-colored VRML format.
#
# OFF2VRML By Gary Jason Mathews
#
# NASA/Goddard Space Flight Center
# Mail stop 633.2
# Greenbelt, Maryland  20771 USA
# e-mail: mathews@nssdc.gsfc.nasa.gov
#
# See http://coney.gsfc.nasa.gov/Mathews/Objects/
# for sample VRML files of various objects.
#
# Modification History:
#   6-Jun-95 Original version.
##

# Default options

$color = '0 0 1'; 	# Default color is Blue.

# read first line which should be of the form:
# "[data]   no_of_pts   no_of_polygons  [no_of_edges]"
# some OFF files have a label of "data" first, others do not.

$_ = <>;
die "invalid object file" unless /^data\s+(\d+)\s+(\d+)/
	|| /^(\d+)\s+(\d+)/;

($sec,$min,$hour,$mday,$mon,$year,$wday) = localtime;
$Day = (Sun,Mon,Tue,Wed,Thu,Fri,Sat)[$wday];
$Month = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Nov,Dec)[$mon];
$date = sprintf "$Day $Month $mday $hour:%02d:%02d %d", $min, $sec, $year+1900;

print <<EOH;
#VRML V1.0 ascii

Separator {
    renderCaching	OFF
    renderCulling	OFF
    Callback {
    }
    Info {
	string "Created by OFF2VRML
contact: Jason Mathews
	 NASA/Goddard Space Flight Center
	 Greenbelt, MD 20771
	 Phone: (301) 286-6879
	 E-mail: mathews\@nssdc.gsfc.nasa.gov
date:	 $date
"
    }
    ShapeHints {
	vertexOrdering	COUNTERCLOCKWISE
	faceType	CONVEX
    }
    Separator {
	renderCulling	OFF
	Material {
	    diffuseColor	$color
	}
	Separator {
	    renderCulling	OFF
EOH

$n = $1;
$count = 0;
while ($n-- > 0) {
 chop($_ = <>);
 if (!$count++) {
  print "	    Coordinate3 {\n";
  print "		point	[ $_";
 }
 else { print ",\n\t\t\t ", $_; }
}
print " ]\n" if $count;
print "	    }\n";

$count = 0;
while (<>) {
 chop;
 s/\d+\s+//; # truncate the # of points in polygon
 if (!$count++) {
   print "\t   IndexedFaceSet {\n";
   print "\t\tcoordIndex\t[";
 }  else { print ",\n\t\t\t\t "; }

# print each point
 foreach $n (split()) { print ' ', $n-1, ','; }

# print polygon separator
 print " -1";
}

print " ] \n" if $count;

print <<EOH;
	    }
	}
    }
    Callback {
    }
}
EOH
