cmtkSplineWarpXformITKIO.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 "cmtkSplineWarpXformITKIO.h"
00032 
00033 #include <IO/cmtkAffineXformITKIO.h>
00034 #include <Base/cmtkTransformChangeToSpaceAffine.h>
00035 
00036 #include <fstream>
00037 #include <string>
00038 #include <typeinfo>
00039 
00040 void
00041 cmtk::SplineWarpXformITKIO
00042 ::Write( const std::string& filename, const SplineWarpXform& xform, const UniformVolume& refVolume, const UniformVolume& fltVolume )
00043 {
00044   std::ofstream stream( filename.c_str() );
00045   if ( stream.good() )
00046     {
00047     // write header
00048     stream << "#Insight Transform File V1.0\n"
00049            << "# Transform 0\n";
00050     
00051     // write ID depending on whether CMTK is using single or double precision floats for coordinates
00052     if ( typeid( Types::Coordinate ) == typeid( double ) )
00053       {
00054       stream << "Transform: BSplineDeformableTransform_double_3_3\n";
00055       }
00056     else
00057       {
00058       stream << "Transform: BSplineDeformableTransform_float_3_3\n";
00059       }
00060 
00061     // write parameters
00062     stream << "Parameters:";
00063 
00064     Vector3D v, vx;
00065     const AffineXform::SmartPtr bulkXform = xform.GetInitialAffineXform();
00066 
00067     for ( size_t cp = 0; cp < xform.GetNumberOfControlPoints(); ++cp )
00068       {
00069       xform.GetOriginalControlPointPositionByOffset( v, cp );
00070       if ( bulkXform )
00071         bulkXform->ApplyInPlace( v );
00072       xform.GetShiftedControlPointPositionByOffset( vx, cp );
00073 
00074       vx -= v;
00075       stream << " " << vx[0] << " " << vx[1] << " " << vx[2];
00076       }
00077     stream << "\n";
00078 
00079     // Origin of the control point grid must be transformed into physical coordinates of the reference image
00080     Vector3D origin( xform.m_Offset * refVolume.GetImageToPhysicalMatrix() );
00081     
00082     // Fixed parameters:
00083     // * Grid Size
00084     // * Grid Origin
00085     // * Grid Spacing
00086     // * Grid Direction
00087     stream << "FixedParameters: "
00088            << xform.m_Dims[0] << " " << xform.m_Dims[1] << " " << xform.m_Dims[2] << " "
00089            << origin[0] << " " << origin[1] << " " << origin[2] << " "
00090            << xform.Spacing[0] << " " << xform.Spacing[1] << " " << xform.Spacing[2] << " "
00091            << "1 0 0 0 1 0 0 0 1\n";
00092 
00093     if ( bulkXform )
00094       {
00095       TransformChangeToSpaceAffine toNative( *(bulkXform), refVolume, fltVolume, AnatomicalOrientationBase::SPACE_ITK );
00096       AffineXformITKIO::Write( stream, toNative.GetTransformation(), 1 /*idx*/ );
00097       }
00098 
00099     stream.close();
00100     }
00101 }
00102 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines