#!/bin/tcsh -f
#
# This converts an ascii text file of 3D vectors into an AC3D data file.
# Each line of the text file should have the  x y z  coordinates of a point.
# The coordinates should be space separated
# By Scott D. Nelson - Lawrence Livermore National Laboratory http://www-dsed.llnl.gov/


set closed=0
if ($# == 2) then
   set infile=($1)
   set outfile=($2)
else if ($# == 3) then
   set closed=1
   set infile=($2)
   set outfile=($3)
else
   echo "Usage: vector2ac  [-c]  inputfile  outputfile"
   echo "       The inputfile is an ASCII file composed of  x y z  coordinates"
   echo "          for each point.  There can only be one polyline in a file"
   echo "       The resulting output file is an AC3D .ac data file"
   echo "       The optional '-c' will form a closed curve.  The default is"
   echo "          to make an open curve -- i.e. just connect the dots"
   exit(0)
endif

set numlines=`wc -l ${infile} | awk '{print $1}'`
if ("$numlines" > 0) then
   # everything is OK
else
   # oops
   echo "Error: no data in '"${infile}"'"
   exit(1)
endif   

echo 'AC3Db' > ${outfile}
echo 'MATERIAL "" rgb 1 1 1  amb 0.2 0.2 0.2  emis 0 0 0  spec 0.5 0.5 0.5  shi 10  trans 0' >> ${outfile}
echo 'OBJECT world' >> ${outfile}
echo 'kids 1' >> ${outfile}
echo 'OBJECT poly' >> ${outfile}
echo 'name "'${infile}'"' >> ${outfile}
echo 'loc 0 0 0' >> ${outfile}

echo 'numvert' $numlines >> ${outfile}
cat ${infile} >> ${outfile}

echo 'numsurf 1' >> ${outfile}

if ( $closed == 1) then
   echo 'SURF 0x21' >> ${outfile}
else
   echo 'SURF 0x22' >> ${outfile}
endif

echo 'mat 0' >> ${outfile}

echo 'refs ' $numlines >> ${outfile}
set i=0
while ($i < $numlines)
   echo $i 0 0 >> ${outfile}
   set i=`expr $i + 1`
end

echo 'kids 0' >> ${outfile}