#!/bin/csh -f

#
# make_hemi_mask
#
# Registers head/brian image to the left/right reversed into midspace
#   to make the image upright and straight, then keep only specified
#   hemi and map back to original space
#
# Original Author: Martin Reuter
# CVS Revision Info:
#    $Author: mreuter $
#    $Date: 2012/08/31 21:37:53 $
#    $Revision: 1.2.2.3 $
#
# Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH"
#
# Terms and conditions for use, reproduction, distribution and contribution
# are found in the 'FreeSurfer Software License Agreement' contained
# in the file 'LICENSE' found in the FreeSurfer distribution, and here:
#
# https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense
#
# Reporting: freesurfer@nmr.mgh.harvard.edu
#
#

set VERSION = '$Id: make_hemi_mask,v 1.2.2.3 2012/08/31 21:37:53 mreuter Exp $';


if ( $#argv < 3 ) then
	echo 
	echo "   Usage: make_hemi_mask hemi input.mgz output.mgz"
  echo  
  echo "   Description: "
  echo "    Registers input to the left/right reversed version using "
  echo "    mri_robust_register making use of the half-way space. This results"
  echo "    in an upright, forward facing head position. Then keep only selected"
  echo "    hemi ('lh' or 'rh'), map it back to original space and apply as mask."
  echo  
  echo "   Reference:"
  echo "    Highly Accurate Inverse Consistent Registration: A Robust Approach, M."
  echo "    Reuter, H.D. Rosas, B. Fischl.  NeuroImage 53(4):1181-1196, 2010."
  echo "    DOI-Link http://dx.doi.org/10.1016/j.neuroimage.2010.07.020 "
  echo "    Pre-Print http://reuter.mit.edu/papers/reuter-robreg10.pdf "
	echo
  echo $VERSION
  echo
	exit 1;
endif

set hemi     = $argv[1]
set infile   = $argv[2]
set outfile  = $argv[3]
set tmpupright = `mktemp -p ./`
rm $tmpupright
set tmpupright = ${tmpupright}.mgz
set maplta   = ${tmpupright}.lta

set cmd = "make_upright $infile $tmpupright $maplta"
eval $cmd

set cmd = "mri_convert $tmpupright $tmpupright --left-right-keep $hemi "
eval $cmd


set cmd = "mri_convert -rt nearest -ait $maplta $tmpupright $tmpupright"
eval $cmd

set cmd = "mri_mask $infile $tmpupright $outfile"
eval $cmd

rm -rf $tmpupright
rm -rf $maplta

echo "make_hemi_mask done"
echo
