#! /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-ball.sh - 26/01/2009
#
#

affiUsage () {
cat << EOT
npic-ball.sh - Generate a Distance Ball.
Usage:
  npic-ball.sh  -h | -help | --help : print help
  npic-ball.sh  mask out1 options   : compute a distance ball with WD mask or SED

mask : weighted distance mask in .nmask[.gz] format
       or "sed" for Squared Euclidean Distance

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

Options:
  -2|-3|-4|-5|-6 : dimension d, if it cannot be guessed from mask
  -ir N  : image radius N, will create a (2N+1)^d image
  -dr R  : distance radius R of the closed ball
  -dt    : fill the center point and make a DT on the ball

Example 1: a 2-d Euclidean ball, without and with option -dt
  ./npic-ball.sh sed tmp1.npz  -2 -dr 100 -ir 12
  ./npic-print tmp1.npz | sed -e 's/ 0/ ./g'
  ./npic-ball.sh sed tmp1.npz  -2 -dr 100 -ir 12 -dt
  ./npic-print tmp1.npz | sed -e 's/ 0/ ./g'

Example 2: a 2-d Montanari's ball
  ./npic-ball.sh ../masks/d-M2_2d.nmask tmp1.npz -dr 254.3 -ir 280
  ./npic-conv tmp1.npz tmp1.pgm -c
  gimp tmp1.pgm &

Example 3: a 3-d chamfer ball
  ./npic-ball.sh ../masks/d-7-10-13-18_3l.nmask tmp1.npz -dr 210 -ir 40
  ./npic-geomv tmp1.npz tmp1.geom -show &

EOT
}


execCom ()
{
    echo "$@"
    "$@" > /dev/null || exit 1
}

dimCoord () {
    local d="$1" a="$2" b="$2" i
    for ((i = 2; i <= d ; i++)); do 
        b="$b:$a"
    done
    echo "$b"
}

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-ball.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

argcExit $# 2
mask="$1"
out1="$2"
shift 2

dim=2
irN=100
drR=1
dt=false

while [ $# -ge 1 ]; do
    case "$1" in
        -2) dim=2 ; shift ;;
        -3) dim=3 ; shift ;;
        -4) dim=4 ; shift ;;
        -5) dim=5 ; shift ;;
        -6) dim=6 ; shift ;;
        -ir) argcExit $# 2 ; irN="$2" ; shift 2 ;;
        -dr) argcExit $# 2 ; drR="$2" ; shift 2 ;;
        -dt) dt=true ; shift ;;
        *) echo "ERROR: bad argument \"$1\"." 1>&2 ; exit 1 ;;
    esac
done


pix=l
if [ "$mask" != "sed" ]
then
    maty=$(getVal "$mask" TYPE)
    case "$maty" in
        NPIC_MASK_2L) pix=l ; dim=2 ;;
        NPIC_MASK_3L) pix=l ; dim=3 ;;
        NPIC_MASK_4L) pix=l ; dim=4 ;;
        NPIC_MASK_5L) pix=l ; dim=5 ;;
        NPIC_MASK_6L) pix=l ; dim=6 ;;
        NPIC_MASK_2D) pix=d ; dim=2 ;;
        NPIC_MASK_3D) pix=d ; dim=3 ;;
        NPIC_MASK_4D) pix=d ; dim=4 ;;
        NPIC_MASK_5D) pix=d ; dim=5 ;;
        NPIC_MASK_6D) pix=d ; dim=6 ;;
        *) echo "ERROR: bad mask type" 1>&2 ; exit 1 ;;
    esac
fi


idiam=$(dimCoord $dim $((2*irN+1)))
execCom npic-new "$out1" "-${dim}${pix}" "$idiam"

qa=$(dimCoord $dim 0)
qb=$(dimCoord $dim $((2*irN)))
qc=$(dimCoord $dim $((irN)))
execCom npic-draw "$out1" "$out1" -rect "$qa" "$qb" 1 -point "$qc" 0

if [ "$mask" = "sed" ]
then execCom npic-sedt -idt -hirata "$out1" "$out1"
else execCom npic-wdt  -idt "$mask" "$out1" "$out1"
fi

execCom npic-val "$out1" "$out1" -thr-gt "$drR" 0

if "$dt" ; then
    execCom npic-draw "$out1" "$out1" -point "$qc" 1
    if [ "$mask" = "sed" ]
    then execCom npic-sedt -dt -hirata "$out1" "$out1"
    else execCom npic-wdt  -dt "$mask" "$out1" "$out1"
    fi
fi

exit 0


