cmtkClassStreamAffineXform.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: 2402 $
00026 //
00027 //  $LastChangedDate: 2010-10-05 16:31:13 -0700 (Tue, 05 Oct 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #include "cmtkClassStreamAffineXform.h"
00034 
00035 namespace
00036 cmtk
00037 {
00038 
00041 
00042 ClassStream& 
00043 operator << ( ClassStream& stream, const AffineXform& affineXform )
00044 {
00045   stream.Begin( "affine_xform" );
00046   stream.WriteCoordinateArray( "xlate", affineXform.RetXlate(), 3 );
00047   stream.WriteCoordinateArray( "rotate", affineXform.RetAngles(), 3 );
00048   if ( affineXform.GetUseLogScaleFactors() )
00049     stream.WriteCoordinateArray( "log_scale", affineXform.RetScales(), 3 );
00050   else
00051     stream.WriteCoordinateArray( "scale", affineXform.RetScales(), 3 );
00052   stream.WriteCoordinateArray( "shear", affineXform.RetShears(), 3 );
00053   stream.WriteCoordinateArray( "center", affineXform.RetCenter(), 3 );
00054   stream.End();
00055 
00056   return stream;
00057 }
00058  
00059 ClassStream& 
00060 operator >> ( ClassStream& stream, AffineXform::SmartPtr& affineXform )
00061 {
00062   try
00063     {
00064     affineXform = AffineXform::SmartPtr( new AffineXform );
00065     stream >> (*affineXform);
00066     }
00067   catch (...)
00068     {
00069     affineXform = AffineXform::SmartPtr::Null;
00070     }
00071   return stream;
00072 }
00073 
00074 ClassStream& 
00075 operator >> ( ClassStream& stream, AffineXform& affineXform )
00076 {
00077   CoordinateVector pVector( 15 );
00078   Types::Coordinate* parameters = pVector.Elements;
00079 
00080   const char *referenceStudy = NULL;
00081   const char *floatingStudy = NULL;
00082 
00083   if ( stream.Seek( "affine_xform", true /*forward*/ ) != TYPEDSTREAM_OK )
00084     {
00085     stream.Rewind();
00086     if ( stream.Seek( "registration", true /*forward*/ ) != TYPEDSTREAM_OK )
00087       {
00088       throw Exception( "Did not find 'registration' section in affine xform archive" );
00089       }
00090     
00091     referenceStudy = stream.ReadString( "reference_study", NULL );
00092     floatingStudy = stream.ReadString( "floating_study", NULL );
00093 
00094     if ( stream.Seek( "affine_xform", false /*forward*/ ) != TYPEDSTREAM_OK )
00095       {
00096       throw Exception( "Did not find 'affine_xform' section in affine xform archive" );
00097       }
00098     }
00099 
00100   if ( stream.ReadCoordinateArray( "xlate", parameters, 3 ) != TYPEDSTREAM_OK )
00101     {
00102     parameters[0] = parameters[1] = parameters[2] = 0;
00103     }
00104   if ( stream.ReadCoordinateArray( "rotate", parameters+3, 3 ) != TYPEDSTREAM_OK )
00105     {
00106     parameters[3] = parameters[4] = parameters[5] = 0;
00107     }
00108   bool logScaleFactors = false;
00109   if ( stream.ReadCoordinateArray( "scale", parameters+6, 3 ) != TYPEDSTREAM_OK )
00110     {
00111     if ( stream.ReadCoordinateArray( "log_scale", parameters+6, 3 ) == TYPEDSTREAM_OK )
00112       {
00113       logScaleFactors = true;
00114       }
00115     else
00116       {
00117       parameters[6] = parameters[7] = parameters[8] = 1;
00118       }
00119     }
00120   if ( stream.ReadCoordinateArray( "shear", parameters+9, 3 ) != TYPEDSTREAM_OK )
00121     {
00122     parameters[9] = parameters[10] = parameters[11] = 0;
00123     }
00124   if ( stream.ReadCoordinateArray( "center", parameters+12, 3 ) != TYPEDSTREAM_OK )
00125     {
00126     parameters[12] = parameters[13] = parameters[14] = 0;
00127     }
00128   stream.End();
00129 
00130   affineXform.SetUseLogScaleFactors( logScaleFactors );
00131   affineXform.SetParamVector( pVector );
00132 
00133   affineXform.m_MetaInformation[META_SPACE] = AnatomicalOrientation::ORIENTATION_STANDARD;
00134 
00135   if ( referenceStudy )
00136     affineXform.SetMetaInfo( META_XFORM_FIXED_IMAGE_PATH, referenceStudy );
00137 
00138   if ( floatingStudy )
00139     affineXform.SetMetaInfo( META_XFORM_MOVING_IMAGE_PATH, floatingStudy );
00140 
00141   return stream;
00142 }
00143 
00144 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines