##############################################################################
# @file  rigidlyregisterdt.sh
# @brief Rigid registration of diffusion tensor images.
#
# Copyright (c) 2008, 2009, 2012 University of Pennsylvania.
#
# This file is part of DTI-DROID.
#
# DTI-DROID is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DTI-DROID is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with DTI-DROID.  If not, see <http://www.gnu.org/licenses/>.
#
# Contact: SBIA Group <sbia-software at uphs.upenn.edu>
##############################################################################

@BASIS_BASH_UTILITIES@


if [ $# -lt 3 ]; then
    echo ''
    echo 'USAGE:' 
    echo "$0 <Subj_DT_Img> <Mdl_DT_Img_With_Path> <Warped SubjDT> <DOF> <Subj SizeX> <Subj SizeY> <Subj SizeZ> <Subj ResX> <Subj ResY> <Subj ResZ> <Mdl SizeX> <Mdl SizeY> <Mdl SizeZ> <Mdl ResX> <Mdl ResY> <Mdl ResZ> <useFsl>"
    echo ''
    echo 'Linearly warp Subj_DT_Img to Mdl_DT_Img using the FA maps. Transformation is saved as GlobalTransform.Affine'
    echo '<Subj_DT_Img>            : subject DT image, in *.dt format, in current directory.'
    echo '<Mdl_DT_Img_With_Path>   : model DT image, in *.dt format, with specified folder.'
    echo '<Warped SubjDT>          : warped subject DT image.'
    echo '<DOF>                    : degree of freedom, 6 for rigid, 12 for affine, default 12.'
    echo 'Subj SizeX, SizeY, SizeZ : Subj Image size for Subj_DT_Img in X, Y, Z dimension, default 256,256,70'
    echo 'Subj ResX, ResY, ResZ    : Subj Image resolution for both image A and B, default 1,1,1.'
    echo 'Mdl SizeX, SizeY, SizeZ  : Mdl Image size for Subj_DT_Img in X, Y, Z dimension, default 256,256,70'
    echo 'Mdl ResX, ResY, ResZ     : Mdl Image resolution for both image A and B, default 1,1,1.'
    echo 'useFsl Y or N            : should i Use fsl or Not, default to Y.'
    echo ''
    exit
fi

SubjImg=$1
MdlImg=$2
WarpedImg=$3

SubjSizeX=256
SubjSizeY=256
SubjSizeZ=70

dof=12
if [ $# -ge 4 ]; then
    dof=$4
fi

if [ $# -ge 7 ]; then
    SubjSizeX=$5
    SubjSizeY=$6
    SubjSizeZ=$7
fi

SubjVoxelX=1
SubjVoxelY=1
SubjVoxelZ=1

if [ $# -ge 10 ]; then
    SubjVoxelX=$8
    SubjVoxelY=$9
    SubjVoxelZ=${10}
fi

MdlSizeX=256
MdlSizeY=256
MdlSizeZ=70

if [ $# -ge 13 ]; then
    MdlSizeX=${11}
    MdlSizeY=${12}
    MdlSizeZ=${13}
fi

MdlVoxelX=1
MdlVoxelY=1
MdlVoxelZ=1

if [ $# -ge 16 ]; then
    MdlVoxelX=${14}
    MdlVoxelY=${15}
    MdlVoxelZ=${16}
fi

useFsl=1
if [ $# -ge 17 ]; then
    if [ "${17}" = "N" ]; then
        useFsl=0
    fi
fi

execute_process -v -- dtiFA $SubjSizeX $SubjSizeY $SubjSizeZ $SubjImg _subj.temp.fa.img 0
execute_process -v -- FloatFA2Byte _subj.temp.fa.img _subj.temp.fa.img
execute_process -v -- extractventricle _subj.temp.fa.img _subj.temp.fa.img -x$SubjSizeX,$SubjSizeY -b 1 > NoUse

execute_process -v -- dtiFA $MdlSizeX $MdlSizeY $MdlSizeZ $MdlImg _mdl.temp.fa.img 0
execute_process -v -- FloatFA2Byte -v -- _mdl.temp.fa.img _mdl.temp.fa.img
execute_process -v -- extractventricle _mdl.temp.fa.img _mdl.temp.fa.img -x$MdlSizeX,$MdlSizeY -b 1 > NoUse

if [ $useFsl = 1 ]; then
    execute_process -v -- linearlyregisterbyfsl _subj.temp.fa.img _mdl.temp.fa.img _subj.temp.fa.rig _subj.temp.fa.img $dof $SubjVoxelX $SubjVoxelY $SubjVoxelZ $MdlVoxelX $MdlVoxelY $MdlVoxelZ $MdlSizeX

    \rm -f _subj.temp.fa.img
    \rm -f _mdl.temp.fa.img
    \rm -f _subj.temp.fa.rig
    \rm -f _subj.temp.fa.img.rig
else
    execute_process -v -- makeavwheader _subj.temp.fa.hdr -d$SubjSizeX,$SubjSizeY,$SubjSizeZ -r$SubjVoxelX,$SubjVoxelY,$SubjVoxelZ -tCHAR
    execute_process -v -- makeavwheader _mdl.temp.fa.hdr -d$MdlSizeX,$MdlSizeY,$MdlSizeZ -r$MdlVoxelX,$MdlVoxelY,$MdlVoxelZ -tCHAR
    execute_process -v -- rigidtransform _subj.temp.fa.img _mdl.temp.fa.img _subj.temp.fa.rig GlobalTransformation.Affine -X $MdlSizeX

    \rm -f _mdl.temp.fa.hdr
    \rm -f _subj.temp.fa.hdr
    \rm -f _subj.temp.fa.img
    \rm -f _mdl.temp.fa.img
    \rm -f _subj.temp.fa.rig
fi

execute_process -v -- LnWarpDT $SubjImg GlobalTransformation.Affine -O $WarpedImg -X $SubjSizeX -Z $SubjSizeZ -R $SubjVoxelX,$SubjVoxelY,$SubjVoxelZ > NoUse

\rm -f NoUse
