#!/bin/sh
#
# Detect and visualize alignments and vanishing points

usage() {
    echo "megawave_vp_demo [-h] [-fftdequant] [-no_align] [-quant q] [-masked_vps] [-all_vps] [image]"
    echo "input:  the image to be analyzed"
    echo
    echo "options for alignment detection:"
    echo " -fftdequant	dequantize gradient orientation by"
    echo "              translating image by half a pixel"
    echo " -quant q	ignore orientation of small gradients"
    echo "              knowing that quantization noise is q (default q=1)"
    echo " -no_align    use precomputed alignments image.segs image.ksegs"
    echo
    echo "options for vanishing point detection:"
    echo " -masked_vps	compute and show \"masked\" vps that become"
    echo "              meaningful only when removing alignments"
    echo "              contributing to other meaningful vps"
    echo " -all_vps     compute and show all maximal vps"
    echo "              (before MDL)"
    echo
    echo "output: visualization of alignments and vanishing points"
}

IMAGE=building.tif
# other images:
# route.rim ciup_b1.tif ciup_b2.tif ciup_fdm_appel1.tif ciup_fdm_central1.tif ciup_fdm_central2.tif

TMP=/tmp/megawave_vp_demo.$$
mkdir $TMP
FFTDEQUANT=0
QUANT=1
COMPUTE_ALIGNMENTS=1
VP_OPTS=""

#----- Parse command line -----
while [ $# -ge 1 ]; do
    case $1 in
	-h )
	    usage
	    exit
	    ;;

	-fftdequant )
	    FFTDEQUANT=1
	    ;;
	
	-quant )
	    shift
            if [ $# -lt 1 ]; then
		echo "-quant option requires an argument"
		exit 1
	    else
		QUANT=$1
	    fi
	    ;;

	-no_align )
	    COMPUTE_ALIGNMENTS=0
	    ;;

	-masked_vps )
	    VP_OPTS="$VP_OPTS -m"
	    ;;

	-all_vps )
	    VP_OPTS="$VP_OPTS -a"
	    ;;

	* )
	    IMAGE=$1
	    ;;
    esac
    shift
done

IMG=`basename $IMAGE`

if [ $COMPUTE_ALIGNMENTS -eq 1 ]; then
    if [ $FFTDEQUANT -eq 1 ]; then
	megawave fzrt -o 7 $IMAGE $TMP/$IMG 1 0 0.5 0.5
    else
	megawave fcopy $IMAGE $TMP/$IMG
    fi
    SEGS=$TMP/${IMG}.segs
    KSEGA=$TMP/${IMG}.ksegs
    megawave align_mdl -n 256 -d 8 -l 3 -g $QUANT -c $KSEGS $TMP/$IMG $SEGS
else
    megawave fcopy $IMAGE $TMP/$IMG
    SEGS=${IMG}.segs
    KSEGS=${IMG}.ksegs
    if [ ! -r $SEGS ]; then
	echo "${IMG}.segs: file not found or unreadable. Call megawave_vp_demo without the -no_align option."
	exit 1
    fi
    if [ ! -r $KSEGS ]; then
	echo "${IMG}.ksegs: file not found or unreadable. Call megawave_vp_demo without the -no_align option."
	exit 1
    fi
fi
megawave fkview -s -b $IMAGE $KSEGS &

# Vanishing points
megawave vpoint $VP_OPTS -v -s $TMP/${IMG}.csegs $TMP/$IMG $SEGS $TMP/${IMG}.vpoints > $TMP/${IMG}.nvpoints

# Number of maximal vanishing points
NVP0=`awk -F= '(match($1,/NVP /)){ print $2}' $TMP/${IMG}.nvpoints`
# Number of masked maximal vanishing points
NVP1=`awk -F= '(match($1,/NVP_masked /)){ print $2}' $TMP/${IMG}.nvpoints`
# Total number of maximal vanishing points
NVP=$(( $NVP0 + $NVP1 ))

# Visualize vanishing points and corresponding segments
I=0
while [ $I -le $NVP ]; do
    if [ $I -lt $NVP0 ]; then
	KVP="$TMP/${IMG}.vp${i}"
    elif [ $I -lt $NVP ]; then
	KVP="$TMP/${IMG}.vp${i}masked"
    else
	KVP="$TMP/${IMG}.vp-none"
    fi
    if megawave vpsegplot $TMP/${IMG} $SEGS $TMP/${IMG}.vpoints $TMP/${IMG}.csegs $I $KVP; then
	megawave fkview -s -b ${outdir}/${IMG} $kvp &
    else
        break
    fi
    I=$(( $I + 1 ))
done

rm -rf $TMP
