cmtkAffineXformITKIO.cxx

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 2009-2010 SRI International
00004 //
00005 //  This file is part of the Computational Morphometry Toolkit.
00006 //
00007 //  http://www.nitrc.org/projects/cmtk/
00008 //
00009 //  The Computational Morphometry Toolkit is free software: you can
00010 //  redistribute it and/or modify it under the terms of the GNU General Public
00011 //  License as published by the Free Software Foundation, either version 3 of
00012 //  the License, or (at your option) any later version.
00013 //
00014 //  The Computational Morphometry Toolkit is distributed in the hope that it
00015 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00016 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 //  GNU General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU General Public License along
00020 //  with the Computational Morphometry Toolkit.  If not, see
00021 //  <http://www.gnu.org/licenses/>.
00022 //
00023 //  $Revision: 2479 $
00024 //
00025 //  $LastChangedDate: 2010-10-20 15:35:37 -0700 (Wed, 20 Oct 2010) $
00026 //
00027 //  $LastChangedBy: torstenrohlfing $
00028 //
00029 */
00030 
00031 #include "cmtkAffineXformITKIO.h"
00032 
00033 #include <fstream>
00034 #include <string>
00035 #include <typeinfo>
00036 
00037 void
00038 cmtk::AffineXformITKIO
00039 ::Write( const std::string& filename, const AffineXform& affineXform )
00040 {
00041   std::ofstream stream( filename.c_str() );
00042   if ( stream.good() )
00043     {
00044     // write header
00045     stream << "#Insight Transform File V1.0\n";
00046     Self::Write( stream, affineXform, 0 );
00047     stream.close();
00048     }
00049 }
00050 
00051 void
00052 cmtk::AffineXformITKIO
00053 ::Write( std::ofstream& stream, const AffineXform& affineXform, const size_t idx )
00054 {
00055   stream << "# Transform " << idx << "\n";
00056   
00057   // write ID depending on whether CMTK is using single or double precision floats for coordinates
00058   if ( typeid( Types::Coordinate ) == typeid( double ) )
00059     {
00060     stream << "Transform: AffineTransform_double_3_3\n";
00061     }
00062   else
00063     {
00064     stream << "Transform: AffineTransform_float_3_3\n";
00065     }
00066   
00067   // write parameters, 3x3 transformation matrix first
00068   stream << "Parameters: ";
00069   for ( int i = 0; i < 3; ++i )
00070     {
00071     for ( int j = 0; j < 3; ++j )
00072       {
00073       stream << affineXform.Matrix[j][i] << " ";
00074       }
00075     }
00076   
00077   // write translations
00078   for ( int i = 0; i < 3; ++i )
00079     {
00080     stream << affineXform.Matrix[3][i] << " ";
00081     }
00082   
00083   // finish up with (all-zero) fixed parameters
00084   stream << "\n"
00085          << "FixedParameters: 0 0 0\n";
00086 }
00087 
00088 cmtk::AffineXform::SmartPtr
00089 cmtk::AffineXformITKIO
00090 ::Read( const std::string& filename )
00091 {
00092   std::ifstream stream( filename.c_str() );
00093   if ( stream.good() )
00094     {
00095     std::string line;
00096     std::getline( stream, line );
00097     if ( line != "#Insight Transform File V1.0" )
00098       return AffineXform::SmartPtr( NULL );
00099 
00100     std::getline( stream, line );
00101     if ( line != "# Transform 0" )
00102       return AffineXform::SmartPtr( NULL );
00103 
00104     std::getline( stream, line );
00105     if ( line == "Transform: AffineTransform_double_3_3" || line == "Transform: AffineTransform_float_3_3" )
00106       {
00107       std::getline( stream, line, ' ' );
00108       Types::Coordinate matrix[4][4] = { {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,1} };
00109       
00110       for ( int i = 0; i < 3; ++i )
00111         {
00112         for ( int j = 0; j < 3; ++j )
00113           {
00114           stream >> matrix[j][i];
00115           }
00116         }
00117       for ( int i = 0; i < 3; ++i )
00118         {
00119         stream >> matrix[3][i];
00120         }
00121       AffineXform::SmartPtr xform( new AffineXform( matrix ) );
00122       xform->SetMetaInfo( META_SPACE, AnatomicalOrientationBase::SPACE_ITK );
00123       return xform;
00124       }
00125     }
00126   
00127   return AffineXform::SmartPtr( NULL );
00128 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines