#! /bin/bash

# The Npic library and tools
#
# Copyright (C) 2003 Edouard Thiel <Edouard.Thiel@@lif.univ-mrs.fr>
#
# This program is free software under the terms of the 
# GNU Lesser General Public License (LGPL) version 2.1.
#

#
# npic-anim4d.sh - 16/11/2008
#
#

affiUsage () {
cat << EOT
npic-anim4d.sh - Show a hyper-volume slice by slice with geomview.
Usage:
  npic-anim4d.sh  -h | -help | --help   : print help
  npic-anim4d.sh  in1  [options]        : load in1 and run geomview

in1  : image in .npz, .pan[.gz] or other format.

Options:
  -time t         : time interval between 2 slices in seconds (default is 0.5).
  -x|-y|-z|-t ... : show a sequence of slices for x, y, z, t
  -all            : equivalent to -x -y -z -t
  -cont           : show sequence continuously

Example: show a 4-dimensionnal ball for weighted distance <3,4,5,6>
  ./npic-new tmp1.npz -4l 41:41:41:41
  ./npic-draw tmp1.npz tmp1.npz -point 20:20:20:20 60
  ./npic-wdt -rdt ../masks/d-3-4-5-6_4l.nmask tmp1.npz tmp2.npz
  ./npic-anim4d.sh tmp2.npz -x -cont

EOT
}

getVal () {
    npic-info "$1" | grep "$2" | { read a b ; echo "$b" ;}
}

argcExit () {
    if [ "$1" -lt "$2" ]; then
        echo "ERROR: $(($2-$1)) argument(s) missing, type \"npic-anim4d.sh -h\" to get help." 1>&2
        exit 1
        fi
}

#
# Main program
#

# Add here the path to npic tools
export PATH="$PATH:."

argcExit $# 1
case "$1" in
    -h|-help|--help) affiUsage ; exit 0 ;;
esac

in1="$1"
shift

ze_time=0.5
ze_x=
ze_y=
ze_z=
ze_t=
ze_all=false
ze_cont=false
ze_seq=

while [ $# -ge 1 ]; do
    case "$1" in
        -time) argcExit $# 2 ; ze_time="$2" ; shift 2 ;;
        -x) ze_x=x; ze_seq="$ze_seq x" ; shift 1 ;;
        -y) ze_y=y; ze_seq="$ze_seq y" ; shift 1 ;;
        -z) ze_z=z; ze_seq="$ze_seq z" ; shift 1 ;;
        -t) ze_t=t; ze_seq="$ze_seq t" ; shift 1 ;;
        -all) ze_x=x ze_y=y ze_z=z ze_t=t ze_seq="x y z t" ; shift 1 ;;
        -cont) ze_cont=true; shift 1 ;;
        *) break ;;
    esac
done


echo "Checking image $in1 ..."
if [ ! -r "$in1" ]; then
    echo "ERROR: can't read file $in1"
    exit 1
fi

if (( $(getVal "$in1" DIM) != 4 )); then
    echo "ERROR: image has not dimension 4" 1>&2
    exit 1
fi

xmax=$(getVal "$in1" XMAX)
ymax=$(getVal "$in1" YMAX)
zmax=$(getVal "$in1" ZMAX)
tmax=$(getVal "$in1" TMAX)

# If the script is interrupted, clean files
trap 'rm -f /tmp/tmp-npic-$$*.* ; exit 1' 1 2 3 15

if [ -z "$ze_seq" ]; then
    echo "ERROR: need -x|-y|-z|-t|-all" 1>&2
    exit 1
fi

# Creating all slices
for k in $ze_x $ze_y $ze_z $ze_t 
do
    echo "Creating slices for $k ..."
    for ((i = 0; i < ${k}max; i++)); do
        printf "%d\\r" $i
        npic-slice "$in1" "/tmp/tmp-npic-$$.npz" -$k $i  > /dev/null
        npic-geomv "/tmp/tmp-npic-$$.npz" "/tmp/tmp-npic-$$-$k=$i.geom" -vobb  > /dev/null
    done
done
echo "           "

# Showing animation in geomview
# To get more info on geomview langage, type: info geomview gcl
{
    while true 
    do
        for k in $ze_seq 
        do
            for ((i = 0; i < ${k}max; i++)); do
                echo "(! echo \"Slice $k = $i\")"
                echo "(delete allgeoms)"
                echo "(load \"/tmp/tmp-npic-$$-$k=$i.geom\")"
                echo "(sleep-for $ze_time)"
            done
        done
        $ze_cont || break
    done
    echo "(exit)"
} | geomview -c -

# Cleaning
echo "Cleaning slices ..."
rm -f /tmp/tmp-npic-$$*.*

exit 0


