#! /bin/bash 
# Last edited on 2006-04-28 23:41:06 by stolfi
 
PROG_NAME=${0##*/}
PROG_DESC="generate a fake test suite for photo_to_normal(1) from a slope map"
PROG_HELP=(
  "${PROG_NAME} \\"
  "\n    -radius {RADIUS} \\"
  "\n    -fields {NF} \\"
  "\n    [ -lamps {NS} ] \\"
  "\n    [ -channels {NC} ] \\"
  "\n    -prefix {PREFIX} \\"
  "\n    < INFILE.fni"
)
PROG_INFO=(
  "\nNAME"
  "\n  ${PROG_NAME} - ${PROG_DESC}."
  "\n"
  "\nSYNOPSIS"
  "\n  ${PROG_HELP[@]}"
  "\n"
  "\nDESCRIPTION"
  "\n  Reads a /slope map/ \"{INFILE}.fni\", a two-channel float-valued" \
  "\n image file (in FNI format), where the two samples of each pixel are" \
  "\n the average X and Y derivatives {dZdX} and {dZdY} of some height map" \
  " {Z(X,Y)} (the /scene/) within that pixel." \
  "\n" \
  "\n  Writes a set of FNI images that can be used to test the program" \
  "\n photo_to_normal(1):" \
  "\n" \
  "\n    \"{PREFIX}-ref-nrm.fni\"" \
  "\n        The normal map of the scene." \
  "\n    \"{PREFIX}-S-0.fni\", .. \"{PREFIX}-S-{NF}.fni\"" \
  "\n        Synthetic photos of the scene under {NF} different fields." \
  "\n    \"{PREFIX}-G-a-nrm.fni\"" \
  "\n        The normal map of an ideal spherical light gauge" \
  "\n        of given {RADIUS}." \
  "\n    \"{PREFIX}-G-a-0.fni\", .. \"{PREFIX}-G-a-{NF}.fni\"" \
  "\n        Synthetic photos of that light gauge under the same" \
  "\n        lighting conditions." \
  "\n" \
  "\n  The synthetic photos have {NC} color channels (default 1). Each of" \
  "\n the {NF} lightfields are generated by {NS} lamps of various colors" \
  "\n and intensities, placed in various positions." \
  "\n" \
  "\nAUTHOR"
  "\n  Created 2006-04-07 by Jorge Stolfi, Unicamp"
)

# Parse command line options: 

radius=
fields=
lamps=1
channels=1
prefix=
while [[ ( $# -ge 1 ) && ( "/$1" =~ "/-.*" ) ]]; do
  if [[ ( $# -ge 2 ) && ( "/$1" == "/-radius" ) ]]; then 
    radius=$2; shift; shift;
  elif [[ ( $# -ge 2 ) && ( "/$1" == "/-fields" ) ]]; then 
    fields=$2; shift; shift;
  elif [[ ( $# -ge 2 ) && ( "/$1" == "/-channels" ) ]]; then 
    channels=$2; shift; shift;
  elif [[ ( $# -ge 2 ) && ( "/$1" == "/-lamps" ) ]]; then 
    lamps=$2; shift; shift;
  elif [[ ( $# -ge 2 ) && ( "/$1" == "/-prefix" ) ]]; then 
    prefix=$2; shift; shift;
  elif [[ ( $# -ge 1 ) && ( ( "/$1" == "/-help" ) || ( "/$1" == "/--help" ) ) ]]; then 
    echo -e "usage:\n  ${PROG_HELP[@]}"; exit 0;
  elif [[ ( $# -ge 1 ) && ( ( "/$1" == "/-info" ) || ( "/$1" == "/--info" ) ) ]]; then 
    echo -e "${PROG_INFO[@]}"; exit 0;
  else
    echo "unknown option $1" 1>&2 ;
    echo -e "usage:\n  ${PROG_HELP[@]}" 1>&2 ; exit 1 
  fi
done 

if [[ $# -gt 0 ]]; then
  echo 'excess arguments "'"$1"'" ...' 1>&2 ;
  echo -e "usage:\n  ${PROG_HELP[@]}" 1>&2 ; exit 1 
fi

if [[ ! $?radius ]]; then
  echo 'must specify "-radius"' 1>&2 ;
  echo -e "usage:\n  ${PROG_HELP[@]}" 1>&2 ; exit 1 
fi

if [[ ! $?fields ]]; then
  echo 'must specify "-fields"' 1>&2 ;
  echo -e "usage:\n  ${PROG_HELP[@]}" 1>&2 ; exit 1 
fi

if [[ ! $?prefix ]]; then
  echo 'must specify "-prefix"' 1>&2 ;
  echo -e "usage:\n  ${PROG_HELP[@]}" 1>&2 ; exit 1 
fi

# Temporary file name prefix:
tmp=/tmp/$$

# Save input file:
cat > ${tmp}.fni

# Create gauge normal map:
make_sphere_normal_map \
    -v radius="${radius}" \
  > ${prefix}-G-a-nrm.fni
fni_to_pnm \
  -channels 0 1 2 \
  < ${prefix}-G-a-nrm.fni \
  > ${prefix}-G-a-nrm.ppm

# Create normal_to_photo call file:
make-normal_to_photo-calls \
  -v NF="${fields}" \
  -v NC="${channels}" \
  -v NS="${lamps}" \
  -v IN="${prefix}-G-a" \
  -v OT="${prefix}-G-a" \
  > ${tmp}.cmds
  
# printf "=== commands ================================\n"
# cat ${tmp}.cmds
# printf "=============================================\n"

source ${tmp}.cmds

# Create scene normal map:
slope_to_normal \
  < ${tmp}.fni \
  > ${prefix}-ref-nrm.fni
fni_to_pnm \
  -channels 0 1 2 \
  < ${prefix}-ref-nrm.fni \
  > ${prefix}-ref-nrm.ppm

# Create scene photos:
make-normal_to_photo-calls \
  -v NF="${fields}" \
  -v NC="${channels}" \
  -v NS="${lamps}" \
  -v IN="${prefix}-ref" \
  -v OT="${prefix}-S" \
  > ${tmp}.cmds
  
# printf "=== commands ================================\n"
# cat ${tmp}.cmds
# printf "=============================================\n"

source ${tmp}.cmds

rm ${tmp}.fni ${tmp}.cmds

exit 0

