#!/bin/sh
#
#  @(#)nuages	2.1 94/01/06
#
bin=../bin
#
#

usage='Usage: nuages <cnt-file> [-tri <surface-output>] [-obj] [-off] [-dxf] [-idx] [-act] [-vera] [-tetra <tetra-output>] [-pixel] [-no_int] [-s_hdr <string>] [-name <string>] [-t_hdr <string>] [-popt <opt>] [-ropt <opt>] [-r2vopt <opt>] [-r2topt <opt>] [-v]'

prepros_opt=""
repros_opt="-m"
repros2visu_opt=""
repros2tetra_opt=""
obj_name="object"
obj_header=""
tetra_header=""

internal=1
verbose=0
pixel_contours=0
prepros_error=0

tetra_out=0
tri_out=0
do_repros2visu=0
do_repros2tetra=0

triangle=""
tetra=""

pid=$$


if [ x$1 = x -o x$2 = x ]
then
      echo $usage
      exit 1
else
      coord=$1
fi

while [ x$2 != x ]; do
  case $2 in
    -v)     verbose=1
            shift
            continue;;
    -tetra) tetra=$3
            tetra_out=1 
            do_repros2tetra=1
            shift
            shift
            continue;;
    -tri)   triangle=$3
            tri_out=1
            do_repros2visu=1
            shift
            shift
            continue;;
    -obj)   repros2visu_opt="$repros2visu_opt -obj"
            shift
            continue;;
    -off)   repros2visu_opt="$repros2visu_opt -off"
            shift
            continue;;
    -dxf)   repros2visu_opt="$repros2visu_opt -dxf"
            shift
            continue;;
    -idx)   repros2visu_opt="$repros2visu_opt -idx"
            shift
            continue;;
    -act)   repros2visu_opt="$repros2visu_opt -act"
            shift
            continue;;
    -vera)  repros2visu_opt="$repros2visu_opt -vera"
            shift
            continue;;
   -no_int) internal=0;
            repros_opt="$repros_opt -no_int"
            shift
            continue;;
    -ropt)  repros_opt="$repros_opt $3"
            shift
            shift
            continue;;
    -popt)  prepros_opt="$prepros_opt $3"
            shift
            shift
            continue;;
    -r2vopt) repros2visu_opt="$repros2visu_opt $3"
            shift
            shift
            continue;;
    -r2topt)  repros2tetra_opt="$repros2tetra_opt $3"
            shift
            shift
            continue;;
    -name)  obj_name=$3
            shift
            shift
            continue;;
    -s_hdr) obj_header=$3
            shift
            shift
            continue;;
    -t_hdr) tetra_header=$3
            shift
            shift
            continue;;
    -pixel) pixel_contour=1
            prepros_opt="prepros_opt -pixel"
            shift
            continue;;
    *)      echo $usage
            exit 1 ;;
    esac
done
if [ $tri_out = 0 -a $tetra_out = 0 ]
then
  echo $usage
  exit 1
fi

if [ $tri_out = 1 -a x$triangle = x ]
then
  echo "-tri: filename missing"
  exit 1
fi
if [ $tetra_out = 1 -a  x$tetra = x ]
then
  echo "-tetra: filename missing"
  exit 1
fi

if [ $verbose = 1 ]
  then
    echo -n "reduce pixel_contours?  [y|n]: "
    read RESPONSE
    case "$RESPONSE" in
      "y" | "yes" )
         pixel_contours=1 
         prepros_opt="$prepros_opt -pixel"
       break;;
    esac
fi

prepros_args="$coord /tmp/coord$pid $prepros_opt"
echo "checking contours"

# CALL TO PREPROS
$bin/prepros $prepros_args > /tmp/prep_out$pid
if [ $? != 0 ]
  then
      cat /tmp/prep_out$pid 
      echo "prepros terminated in error"
      /bin/rm /tmp/prep_out$pid /tmp/coord*$pid
      exit 1
fi

nb_of_holes=`cat /tmp/prep_out$pid| grep inside | wc -l`
/bin/rm /tmp/prep_out$pid

if [ $do_repros2visu = 0 ]
then
      repros_args="/tmp/coord$pid /dev/null"
else
      repros_args="/tmp/coord$pid /tmp/tri$pid"
fi


if [ $verbose = 0 ]
then
    repros_opt="$repros_opt"
else
      if [ $nb_of_holes != 0 ]
      then
        echo "contours lying inside another"
      fi
      if [ $internal = 1 ]
      then
        echo -n "Do you want internal vertices?  [y|n]: "
        read RESPONSE
        case "$RESPONSE" in
          "n" | "no" )
           internal=0
           repros_opt="$repros_opt -no_int"
           break;;
        esac
      fi
fi

if [ $tetra_out = 1 ]
then
      repros_opt="$repros_opt -t /tmp/tetra$pid"
fi

repros_args="$repros_args $repros_opt"

echo "running reconstruction"

# CALL TO REPROS
$bin/repros $repros_args > /tmp/rep_out$pid
if [ $? != 0 ]
then
      cat /tmp/rep_out$pid 
      echo "repros terminated in error"
      /bin/rm /tmp/*$pid
      exit 1
fi
/bin/rm /tmp/rep_out$pid 


repros2visu_opt="$repros2visu_opt -o $obj_name"

repros2visu_args="/tmp/tri$pid /tmp/coord$pid.new"

repros2visu_args="$repros2visu_args $triangle"


if [ $do_repros2visu = 1 ]
then
     if [ $verbose = 0 ]
     then
           echo ""
     else
           echo -n "Do you want to delete all horizontal faces?  [y|n]: "
           read RESPONSE
           case "$RESPONSE" in
             "y" | "yes" )
                repros2visu_opt="$repros2visu_opt -h"
                break;;
             "n" | "no" )
                echo -n "Do you want to delete some horizontal faces?  [y|n]: "
                read RESPONSE
                case "$RESPONSE" in
                  "y" | "yes" )
                     nb_sections=`head -1 $coord | /bin/awk '{print $2}'`
                     i=0
                     while [ $i != $nb_sections ]; do
                       i=`expr $i + 1`
                       if [ $i = 1 ]
                       then
                         name="(first)"
                       else
                         if [ $i = $nb_sections ]
                         then
                           name="(last)"
                         else
                           name=""
                         fi
                       fi 
         
                       echo -n "Delete horizontal faces of xsection $i $name ? [y|n]: "
                       read RESPONSE
                       if [ $RESPONSE = y -o $RESPONSE = yes ]
                       then
                              repros2visu_opt="$repros2visu_opt -r $i"
                       fi
                     done
                    break;;
                esac
           esac
           echo -n "Do you want to interpolate all surface normals?  [y|n]: "
           read RESPONSE
           case "$RESPONSE" in
             "n" | "no" )
              nb_sections=`head -1 $coord | /bin/awk '{print $2}'`         
              i=0
              while [ $i != $nb_sections ]; do
                i=`expr $i + 1`
                if [ $i = 1 ]
                then
                  name="(first)"
                else
                  if [ $i = $nb_sections ]
                  then
                    name="(last)"
                  else
                    name=""
                  fi
                fi 
        
                echo -n "interpolate at xsection $i $name ?  [y|n]: "
                read RESPONSE
                if [ $RESPONSE = n -o $RESPONSE = no ]
                then
                       repros2visu_opt="$repros2visu_opt -n $i"
                fi
              done
             break;;
           esac
           echo -n "object header line : "
           read obj_header 
     fi
     repros2visu_args="$repros2visu_args $repros2visu_opt"
  
     echo "running repros2visu"
  
#CALL REPROS2VISU
     $bin/repros2visu $repros2visu_args -t "$obj_header" > /tmp/rep_out$pid
     if [ $? != 0 ]
     then
           cat /tmp/rep_out$pid
           echo "repros2visu terminated in error"
           /bin/rm /tmp/*$pid
           exit 1
     fi
     /bin/rm /tmp/tri$pid
fi

repros2tetra_args="/tmp/tetra$pid /tmp/coord$pid.new $tetra"

if [ $do_repros2tetra = 1 ]
then
     if [ $verbose = 0 ]
     then
           echo ""
     else
           echo -n "tetra file header line : "
           read tetra_header 
     fi
     repros2tetra_args="$repros2tetra_args $repros2tetra_opt"
  
     echo "running repros2tetra"
  
#CALL TO REPROS2TETRA
     $bin/repros2tetra $repros2tetra_args -t "$tetra_header" > /tmp/rep_out$pid
     if [ $? != 0 ]
     then
           cat /tmp/rep_out$pid
           echo "repros2tetra terminated in error"
           /bin/rm /tmp/*$pid
           exit 1
     fi
     /bin/rm /tmp/tetra$pid
fi
/bin/rm /tmp/rep_out$pid /tmp/coord*$pid*

echo "done"
exit 0
