Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
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
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
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
00078 for ( int i = 0; i < 3; ++i )
00079 {
00080 stream << affineXform.Matrix[3][i] << " ";
00081 }
00082
00083
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 }