#!/bin/csh -f

#
# make_symmetric
#
# Registers head/brian image to the left/right reversed into midspace
#   to make the image upright and straight, then mirror one hemisphere
#
# Original Author: Martin Reuter
# CVS Revision Info:
#    $Author: mreuter $
#    $Date: 2012/12/12 16:30:44 $
#    $Revision: 1.2.2.4 $
#
# 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_symmetric,v 1.2.2.4 2012/12/12 16:30:44 mreuter Exp $';


if ( $#argv < 4 ) then
	echo 
	echo "   Usage: make_symmetric hemi input.mgz output.mgz map.lta"
  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 mirrors selected"
  echo "    hemi ('lh' or 'rh') to the other. The transform map.lta is also"
  echo "    written that maps the input to the upright space."
  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 maplta   = $argv[4]
set outdir   = `dirname $outfile`;
set tmpupright = $outdir/tmp.make_upright.${$}.mgz

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

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

rm -rf $tmpupright

echo "make_symmetric done"
echo
