cmtkTypedStreamStudylist.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: 2584 $
00026 //
00027 //  $LastChangedDate: 2010-12-02 14:37:48 -0800 (Thu, 02 Dec 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #include "cmtkTypedStreamStudylist.h"
00034 
00035 #include <Base/cmtkTypes.h>
00036 
00037 #include <System/cmtkConsole.h>
00038 #include <System/cmtkStrUtility.h>
00039 #include <System/cmtkConsole.h>
00040 #include <System/cmtkCompressedStream.h>
00041 #include <System/cmtkMountPoints.h>
00042 
00043 #include <IO/cmtkTypedStream.h>
00044 #include <IO/cmtkClassStream.h>
00045 #include <IO/cmtkClassStreamAffineXform.h>
00046 
00047 namespace
00048 cmtk
00049 {
00050 
00053 
00054 TypedStreamStudylist::TypedStreamStudylist()
00055 {
00056   this->Clear();
00057 }
00058 
00059 void TypedStreamStudylist::Clear()
00060 {
00061   StudyPath[0] = StudyPath[1] = NULL;
00062   ReferenceStudyIndex = 0;
00063   this->m_AffineXform = AffineXform::SmartPtr( NULL );
00064   this->m_WarpXform = WarpXform::SmartPtr( NULL );
00065 }
00066 
00067 TypedStreamStudylist::~TypedStreamStudylist()
00068 {
00069   if ( StudyPath[0] ) free( StudyPath[0] );
00070   if ( StudyPath[1] ) free( StudyPath[1] );
00071 }
00072 
00073 bool
00074 TypedStreamStudylist::Read( const char *studylistpath )
00075 {
00076   char archive[PATH_MAX];
00077 
00078   snprintf( archive, sizeof( archive ), "%s/studylist", MountPoints::Translate( studylistpath ) );
00079   ClassStream classStream( archive, ClassStream::READ );
00080   if ( ! classStream.IsValid() ) 
00081     {
00082     StdErr.printf( "Could not open studylist archive %s.\n", archive );
00083     return false;
00084     }
00085   
00086   if ( StudyPath[0] ) free( StudyPath[0] );
00087   classStream.Seek ( "source" );
00088   StudyPath[0] = classStream.ReadString( "studyname", "<unknown>" );
00089   
00090   if ( StudyPath[1] ) free( StudyPath[1] );
00091   classStream.Seek ( "source" );
00092   StudyPath[1] = classStream.ReadString( "studyname", "<unknown>" );
00093   classStream.Close();
00094   
00095   snprintf( archive, sizeof( archive ), "%s/registration", MountPoints::Translate(studylistpath) );
00096   classStream.Open( archive, ClassStream::READ );
00097   if ( ! classStream.IsValid() ) 
00098     {
00099     StdErr.printf( "Could not open studylist archive %s.\n", archive );
00100     return false;
00101     }
00102   
00103   classStream.Seek ( "registration" );
00104   char *referenceStudy = classStream.ReadString( "reference_study" );
00105   ReferenceStudyIndex = ( StrCmp( referenceStudy, StudyPath[0] ) ) ? 1 : 0;
00106 
00107   bool legacy = false;
00108   char *floatingStudy = classStream.ReadString( "floating_study" );
00109   if ( !floatingStudy )
00110     {
00111     classStream.Begin();
00112     floatingStudy = classStream.ReadString( "model_study" );
00113     if ( floatingStudy )
00114       {
00115       legacy = true;
00116       }
00117     else
00118       {
00119       StdErr.printf( "WARNING: Studylist %s/registration apparently has neither new 'floating_study' nor old 'model_study' entry\n", archive );
00120       }
00121     }
00122   
00123   classStream >> this->m_AffineXform;
00124 
00125   if ( referenceStudy )
00126     {
00127     this->m_AffineXform->SetMetaInfo( META_XFORM_FIXED_IMAGE_PATH, referenceStudy );
00128     }
00129   if ( floatingStudy )
00130     {
00131     this->m_AffineXform->SetMetaInfo( META_XFORM_MOVING_IMAGE_PATH, floatingStudy );
00132     }
00133 
00134   if ( legacy )
00135     {
00136     this->m_AffineXform = AffineXform::SmartPtr( this->m_AffineXform->MakeInverse() );
00137     }
00138 
00139   classStream.Get( this->m_WarpXform );
00140   if ( this->m_WarpXform )
00141     {
00142     if ( referenceStudy )
00143       {
00144       this->m_WarpXform->SetMetaInfo( META_XFORM_FIXED_IMAGE_PATH, referenceStudy );
00145       }
00146     if ( floatingStudy )
00147       {
00148       this->m_WarpXform->SetMetaInfo( META_XFORM_MOVING_IMAGE_PATH, floatingStudy );
00149       }
00150     }
00151   
00152   classStream.Close();
00153   return true;
00154 }
00155 
00156 void
00157 TypedStreamStudylist::Write
00158 ( const char *path, const char* referenceStudy, const char* floatingStudy, const Xform* xform )
00159 {
00160   ClassStream classStream( path, "studylist", ClassStream::WRITE );
00161   if ( ! classStream.IsValid() ) return;
00162   
00163   classStream.Begin( "studylist" );
00164   classStream.WriteInt( "num_sources", 2 );
00165   classStream.End();
00166 
00167   classStream.Begin( "source" );
00168   classStream.WriteString( "studyname", CompressedStream::GetBaseName( referenceStudy ) );
00169   classStream.End();
00170 
00171   classStream.Begin( "source" );
00172   classStream.WriteString( "studyname", CompressedStream::GetBaseName( floatingStudy ) );
00173   classStream.End();
00174 
00175   classStream.Close();
00176 
00177   classStream.Open( path, "registration", ClassStream::WRITE );
00178   if ( classStream.IsValid() ) 
00179     {    
00180     classStream.Begin( "registration" );
00181     classStream.WriteString( "reference_study", CompressedStream::GetBaseName( referenceStudy ) );
00182     classStream.WriteString( "floating_study", CompressedStream::GetBaseName( floatingStudy ) );
00183     
00184     const WarpXform* warp = dynamic_cast<const WarpXform*>( xform );
00185     if ( warp ) 
00186       {
00187       if ( warp->GetInitialAffineXform() ) 
00188         {
00189         classStream << (*warp->GetInitialAffineXform()->GetInverse());
00190         }
00191       classStream << warp;
00192       } 
00193     else 
00194       {
00195       const AffineXform* affine = dynamic_cast<const AffineXform*>( xform );
00196       if ( affine )
00197         classStream << (*affine->GetInverse());
00198       }
00199     
00200     classStream.End();
00201   }
00202   classStream.Close();
00203 }
00204 
00205 } // namespace
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines