cmtkClassStreamStudyList.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: 2399 $
00026 //
00027 //  $LastChangedDate: 2010-10-05 15:39:13 -0700 (Tue, 05 Oct 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #include "cmtkClassStreamStudyList.h"
00034 
00035 #include <System/cmtkMountPoints.h>
00036 
00037 #include <IO/cmtkClassStream.h>
00038 #include <IO/cmtkClassStreamAffineXform.h>
00039 
00040 namespace
00041 cmtk
00042 {
00043 
00046 
00047 StudyList* 
00048 ClassStreamStudyList::Read
00049 ( const char *path )
00050 {
00051   return Merge( new StudyList, path );
00052 }
00053 
00054 StudyList* 
00055 ClassStreamStudyList::Merge
00056 ( const StudyList& studyList, const char *path )
00057 {
00058   return Merge( new StudyList( studyList ), path );
00059 }
00060 
00061 StudyList* 
00062 ClassStreamStudyList::Merge
00063 ( StudyList* const studyList, const char *path )
00064 {
00065   ClassStream classStream( MountPoints::Translate( path ), "studylist", ClassStream::READ );
00066   if ( ! classStream.IsValid() ) 
00067     {
00068     return NULL;
00069     }
00070   
00071   StudyList *newStudyList = studyList;
00072   if ( ! newStudyList ) 
00073     newStudyList = new StudyList;
00074   
00075   while ( classStream.Seek ( "source" ) ) 
00076     {
00077     char *fileSystemPath = classStream.ReadString( "studyname", NULL );
00078     if ( fileSystemPath ) 
00079       {
00080       newStudyList->AddStudy( fileSystemPath );
00081       }
00082     }
00083   classStream.Close();
00084   
00085   classStream.Open( MountPoints::Translate( path ), "registration", ClassStream::READ );
00086   if ( ! classStream.IsValid() ) 
00087     {
00088     return newStudyList;
00089     }
00090   
00091   while ( classStream.Seek ( "registration" ) ) 
00092     {
00093     char *referenceStudy = classStream.ReadString( "reference_study", NULL );
00094     
00095     bool legacy = false;
00096     char *floatingStudy = classStream.ReadString( "floating_study", NULL );
00097     if ( !floatingStudy ) 
00098       {
00099       // check outdated "model_study" entry.
00100       floatingStudy = classStream.ReadString( "model_study", NULL );
00101       legacy = true;
00102       }
00103     
00104     if ( referenceStudy && floatingStudy ) 
00105       {
00106       AffineXform::SmartPtr affineXform;
00107       classStream >> affineXform;      
00108 
00109       affineXform->SetMetaInfo( META_XFORM_FIXED_IMAGE_PATH, referenceStudy );
00110       affineXform->SetMetaInfo( META_XFORM_MOVING_IMAGE_PATH, floatingStudy );
00111       
00112       WarpXform::SmartPtr warpXform;
00113       classStream.Get( warpXform, affineXform );
00114       
00115       warpXform->SetMetaInfo( META_XFORM_FIXED_IMAGE_PATH, referenceStudy );
00116       warpXform->SetMetaInfo( META_XFORM_MOVING_IMAGE_PATH, floatingStudy );
00117       
00118       AffineXform::SmartPtr inverse = affineXform->GetInverse();
00119       
00120       WarpXform::SmartPtr nullWarp( NULL );
00121       if ( !legacy )
00122         {
00123         newStudyList->AddXform( referenceStudy, floatingStudy, affineXform, warpXform );
00124         newStudyList->AddXform( floatingStudy, referenceStudy, inverse, nullWarp );
00125         } 
00126       else
00127         {
00128         newStudyList->AddXform( referenceStudy, floatingStudy, inverse, warpXform );
00129         newStudyList->AddXform( floatingStudy, referenceStudy, affineXform, nullWarp );
00130         }
00131       }
00132     }
00133   classStream.Close();
00134   
00135   return newStudyList;
00136 }
00137 
00138 void 
00139 ClassStreamStudyList::Write
00140 ( const char *path, const StudyList* studyList )
00141 {
00142   ClassStream stream;
00143 
00144   stream.Open( path, "studylist", ClassStream::WRITE );
00145 
00146   StudyList::const_iterator it = studyList->begin();
00147   while ( it != studyList->end() ) 
00148     {
00149     stream.Begin( "source" );
00150     stream.WriteString( "studyname", it->first->GetFileSystemPath() );
00151     stream.End();
00152     ++it;
00153     }
00154   
00155   stream.Close();
00156   
00157   stream.Open( path, "registration", ClassStream::WRITE );
00158 
00159   it = studyList->begin();
00160   while ( it != studyList->end() ) 
00161     {
00162     StudyToXform targetList = it->second;
00163     
00164     std::map<Study::SmartPtr,bool> seen;
00165 
00166     StudyToXform::const_iterator tit;
00167     for ( tit = targetList.begin(); tit != targetList.end(); ++tit ) 
00168       {
00169       if ( seen.find( tit->first ) == seen.end() )
00170         {
00171         seen[tit->first] = true;
00172         
00173         stream.Begin( "registration" );
00174         stream.WriteString( "reference_study", it->first->GetFileSystemPath() );
00175         stream.WriteString( "floating_study", tit->first->GetFileSystemPath() );
00176         
00177         StudyToXform::const_iterator tit2;
00178         for ( tit2 = targetList.begin(); tit2 != targetList.end(); ++tit2 ) 
00179           {
00180           if ( tit2->first == tit->first )
00181             {
00182             Xform::SmartPtr xform = tit2->second;
00183             
00184             AffineXform::SmartPtr affine = AffineXform::SmartPtr::DynamicCastFrom( xform );
00185             if ( affine )
00186               {
00187               stream << (*affine);
00188               }
00189             
00190             WarpXform::SmartPtr warp = WarpXform::SmartPtr::DynamicCastFrom( xform );
00191             if ( warp ) 
00192               {
00193               stream << warp;
00194               }
00195             }
00196           }
00197         stream.End();
00198         }
00199       }
00200     ++it;
00201     }
00202   
00203   stream.Close();
00204 }
00205 
00206 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines