cmtkAnatomicalOrientation.cxx

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2010 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2694 $
00026 //
00027 //  $LastChangedDate: 2011-01-07 16:16:36 -0800 (Fri, 07 Jan 2011) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #include "cmtkAnatomicalOrientation.h"
00034 
00035 #include <System/cmtkConsole.h>
00036 
00037 #include <math.h>
00038 
00039 namespace
00040 cmtk
00041 {
00042 
00045 
00046 void
00047 AnatomicalOrientation
00048 ::GetOrientationFromDirections( char* orientation,  const AffineXform::MatrixType& directions, const char* spaceAxes )
00049 {
00050   const Types::Coordinate spacing[3] =
00051     {
00052       sqrt( directions[0][0]*directions[0][0] + directions[0][1]*directions[0][1] + directions[0][2]*directions[0][2] ),
00053       sqrt( directions[1][0]*directions[1][0] + directions[1][1]*directions[1][1] + directions[1][2]*directions[1][2] ),
00054       sqrt( directions[2][0]*directions[2][0] + directions[2][1]*directions[2][1] + directions[2][2]*directions[2][2] )
00055     };
00056 
00057   // keep track of which axes are already used in the direction code
00058   bool axisUsed[3] = { false, false, false };
00059 
00060   for ( int axis = 0; axis < 3; ++axis )
00061     {
00062     // skip axes already used
00063     int maxDim = 0;
00064     while ( axisUsed[maxDim] ) ++maxDim;
00065     
00066     // get closest aligned of remaining axes
00067     Types::Coordinate max = fabs( directions[axis][0] / spacing[axis] );
00068     for ( int dim = 1; dim < 3; ++dim )
00069       {
00070       const Types::Coordinate positive = fabs( directions[axis][dim] / spacing[axis] );
00071       if ( (positive > max) && !axisUsed[dim] )
00072         {
00073         max = positive;
00074         maxDim = dim;
00075         }
00076       else
00077         {
00078         if ( positive == max )
00079           {
00080           maxDim = 3;
00081           }
00082         }
00083       }
00084     
00085     if ( maxDim == 3 )
00086       {
00087       StdErr << "WARNING: image seems to have an oblique orientation. This is not going to end well...\n";
00088       }
00089     
00090     orientation[axis] = (directions[axis][maxDim] > 0) ? spaceAxes[maxDim] : Self::OppositeDirection( spaceAxes[maxDim] );
00091     axisUsed[maxDim] = true;
00092     }
00093   orientation[3] = 0;
00094 }
00095 
00096 void
00097 AnatomicalOrientation
00098 ::GetImageToSpaceAxesPermutation( int (&imageToSpaceAxesPermutation)[3][3], const char* orientation, const char* spaceAxes )
00099 {
00100   for ( int j = 0; j < 3; ++j )
00101     {
00102     for ( int i = 0; i < 3; ++i )
00103       {
00104       if ( orientation[j] == spaceAxes[i] )
00105          imageToSpaceAxesPermutation[j][i] = 1;
00106       else 
00107         if ( Self::OnSameAxis( orientation[j], spaceAxes[i] ) )
00108           imageToSpaceAxesPermutation[j][i] = -1;
00109         else
00110           imageToSpaceAxesPermutation[j][i] = 0;
00111       }
00112     }
00113 }
00114 
00115 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines