#! /bin/tcsh -f

#
# bblabel
#
# applies a bounding box to a label
#
# Original Author: REPLACE_WITH_FULL_NAME_OF_CREATING_AUTHOR
# CVS Revision Info:
#    $Author: nicks $
#    $Date: 2008/03/10 14:16:07 $
#    $Revision: 1.5.2.1 $
#
# Copyright (C) 2002-2007,
# The General Hospital Corporation (Boston, MA).
# All rights reserved.
#
# Distribution, usage and copying of this software is covered under the
# terms found in the License Agreement file named 'COPYING' found in the
# FreeSurfer source code root directory, and duplicated here:
# https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferOpenSourceLicense
#
# General inquiries: freesurfer@nmr.mgh.harvard.edu
# Bug reports: analysis-bugs@nmr.mgh.harvard.edu
#


set VERSION = '$Id: bblabel,v 1.5.2.1 2008/03/10 14:16:07 nicks Exp $';

set cmdargs = ($argv);
set PrintHelp = 0;

set labelfile = ();
set xmin = ();
set xmax = ();
set ymin = ();
set ymax = ();
set zmin = ();
set zmax = ();
set rev = 0;
set outlabelfile = ();

if($#argv == 0)  goto usage_exit;
set n = `echo $argv | egrep -e -help | wc -l`
if($n != 0) then
  set PrintHelp = 1;
  goto usage_exit;
  exit 1;
endif

goto parse_args;
parse_args_return:

goto check_params;
check_params_return:

if(-e $outlabelfile) mv $outlabelfile $outlabelfile.bak

set tmpfile =  $outlabelfile.tmp$$
touch $tmpfile

set firstline = `head -n 1 $labelfile`;
set lno = `tail -n +3 $labelfile | awk '{print $1}'`;
set x = `tail -n +3 $labelfile | awk '{print $2}'`;
set y = `tail -n +3 $labelfile | awk '{print $3}'`;
set z = `tail -n +3 $labelfile | awk '{print $4}'`;
set s = `tail -n +3 $labelfile | awk '{print $5}'`;

echo "Finding points within bounds"
@ nth = 1;
foreach p ($lno)

  set bound = 1
  if($#xmin) set bound = ($bound && $x[$nth] \> $xmin)
  if($#xmax) set bound = ($bound && $x[$nth] \< $xmax)
  if($#ymin) set bound = ($bound && $y[$nth] \> $ymin)
  if($#ymax) set bound = ($bound && $y[$nth] \< $ymax)
  if($#zmin) set bound = ($bound && $z[$nth] \> $zmin)
  if($#zmax) set bound = ($bound && $z[$nth] \< $zmax)

  if(`echo $bound | bc -l`) then
    echo $lno[$nth] $x[$nth] $y[$nth] $z[$nth] $s[$nth] >> $tmpfile
  endif

  @ nth = $nth + 1;
end

echo "Writing new label to $outlabelfile"
echo $firstline > $outlabelfile
set npoints = `wc -l $tmpfile`
echo $npoints[1] >> $outlabelfile
cat $tmpfile >> $outlabelfile
rm -f $tmpfile

echo "bblabel done"

exit 0;
###############################################


############--------------##################
parse_args:
set cmdline = "$argv";
while( $#argv != 0 )

  set flag = $argv[1]; shift;

  switch($flag)

    case "--l":
      if ( $#argv == 0) goto arg1err;
      set labelfile = $argv[1]; shift;
      breaksw

    case "--o":
      if ( $#argv == 0) goto arg1err;
      set outlabelfile = $argv[1]; shift;
      breaksw

    case "--xmin":
      if ( $#argv == 0) goto arg1err;
      set xmin = $argv[1]; shift;
      breaksw

    case "--xmax":
      if ( $#argv == 0) goto arg1err;
      set xmax = $argv[1]; shift;
      breaksw

    case "--ymin":
      if ( $#argv == 0) goto arg1err;
      set ymin = $argv[1]; shift;
      breaksw

    case "--ymax":
      if ( $#argv == 0) goto arg1err;
      set ymax = $argv[1]; shift;
      breaksw

    case "--zmin":
      if ( $#argv == 0) goto arg1err;
      set zmin = $argv[1]; shift;
      breaksw

    case "--zmax":
      if ( $#argv == 0) goto arg1err;
      set zmax = $argv[1]; shift;
      breaksw

    case "--rev":
      set rev = 1;
      breaksw

    case "--debug":
      set verbose = 1;
      set echo = 1;
      breaksw

    case "--umask":
      if ( $#argv == 0) goto arg1err;
      setenv MRI_UMASK $1; shift;
      breaksw

    default:
      echo ERROR: Flag $flag unrecognized.
      echo $cmdline
      exit 1
      breaksw
  endsw

end

goto parse_args_return;
############--------------##################

############--------------##################
check_params:

if($#labelfile == 0) then
  echo "ERROR: no label file specified"
  exit 1;
endif

if(! -e $labelfile) then
  echo "ERROR: cannot find label file $labelfile"
  exit 1;
endif

if($#outlabelfile == 0) then
  echo "ERROR: no output label file specified"
  exit 1;
endif

goto check_params_return;
############--------------##################

############--------------##################
arg1err:
  echo "ERROR: flag $flag requires one argument"
  exit 1
############--------------##################

############--------------##################
usage_exit:
  echo ""
  echo "USAGE: bblabel"
  echo ""
  echo "   --l labelfile "
  echo "   --xmin xmin"
  echo "   --xmax xmax"
  echo "   --ymin ymin"
  echo "   --ymax ymax"
  echo "   --zmin zmin"
  echo "   --zmax zmax"
#  echo "   --rev"
  echo "   --o outlabelfile "
  echo ""
  echo "   --debug"
  echo "   --umask umask    : set unix file permission mask"
  echo "   --version        : print version and exit"
  echo "   --help           : print help and exit"
  echo ""

  if(! $PrintHelp) exit 1;

  echo $VERSION

  echo "------------------------------------------------------------"
  cat $0 | awk 'BEGIN{prt=0}{if(prt) print $0; if($1 == "BEGINHELP") prt = 1 }'

  echo "------------------------------------------------------------"

exit 1;

#---- Everything below here is printed out as part of help -----#
BEGINHELP

bblabel

Applies a bounding box to a label. The bounding box is specified by
six coordinates (xmin,xmax,ymin,ymax,zmin,zmax). Only those label
points within this box are copied to the output. If a min is not
specified, then -infinity is used. If a max is not specified, then
+infinity is used.

Example:

bblabel --l lh.G_cuneus.label --o lh.out.label \
  --xmin 0 --ymax -90 --zmin 10 --zmax 20

Keeps label points from lh.G_cuneus.label that have x > 0, y > -90,
and z between 10 and 20. The result is stored in lh.out.label.

