cmtkStudyList.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: 2398 $
00026 //
00027 //  $LastChangedDate: 2010-10-05 14:54:37 -0700 (Tue, 05 Oct 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #include "cmtkStudyList.h"
00034 
00035 #include <System/cmtkConsole.h>
00036 #include <Base/cmtkSplineWarpXform.h>
00037 
00038 namespace
00039 cmtk
00040 {
00041 
00044 
00045 const Study*
00046 StudyList::GetStudy( const unsigned int studyIndex ) const
00047 {
00048   if ( studyIndex < this->size() ) {
00049     const_iterator it = this->begin();
00050     for ( unsigned int i = 0; i < studyIndex; ++i ) ++it;
00051     return it->first;
00052   } else
00053     return NULL;
00054 }
00055 
00056 Study::SmartPtr
00057 StudyList::GetStudy( const unsigned int studyIndex )
00058 {
00059   if ( studyIndex < this->size() ) {
00060     const_iterator it = this->begin();
00061     for ( unsigned int i = 0; i < studyIndex; ++i ) ++it;
00062     return it->first;
00063   } else
00064     return Study::SmartPtr::Null;
00065 }
00066 
00067 const Study*
00068 StudyList::FindStudyPath( const char *fileSystemPath ) const
00069 {
00070   if ( ! fileSystemPath ) return NULL;
00071 
00072   const_iterator it = this->begin();
00073   while ( it != this->end() ) {
00074     if ( ! strcmp( it->first->GetFileSystemPath(), fileSystemPath ) )
00075       return it->first;
00076     ++it;
00077   }
00078   
00079   // not found: return NULL;
00080   return NULL;
00081 }
00082 
00083 Study::SmartPtr
00084 StudyList::FindStudyPath( const char *fileSystemPath, const bool create )
00085 {
00086   if ( ! fileSystemPath ) return Study::SmartPtr::Null;
00087 
00088   iterator it = this->begin();
00089   while ( it != this->end() ) {
00090     if ( ! strcmp( it->first->GetFileSystemPath(), fileSystemPath ) )
00091       return it->first;
00092     ++it;
00093   }
00094   
00095   // not found: return NULL or create;
00096   if ( !create )
00097     return Study::SmartPtr::Null;
00098   
00099   Study::SmartPtr newStudy;
00100   newStudy->SetFileSystemPath( fileSystemPath );
00101   this->AddStudy( newStudy );
00102   return newStudy;
00103 }
00104 
00105 const Study*
00106 StudyList::FindStudyName( const char *name ) const
00107 {
00108   if ( ! name ) return NULL;
00109 
00110   const_iterator it = this->begin();
00111   while ( it != this->end() ) {
00112     if ( ! strcmp( it->first->GetName(), name ) )
00113       return it->first;
00114     ++it;
00115   }
00116   
00117   // not found: return NULL;
00118   return NULL;
00119 }
00120 
00121 Study::SmartPtr
00122 StudyList::FindStudyName( const char *name )
00123 {
00124   if ( ! name ) return Study::SmartPtr::Null;
00125 
00126   iterator it = this->begin();
00127   while ( it != this->end() ) {
00128     if ( ! strcmp( it->first->GetName(), name ) )
00129       return it->first;
00130     ++it;
00131   }
00132   
00133   // not found: return NULL;
00134   return Study::SmartPtr::Null;
00135 }
00136 
00137 Study::SmartPtr
00138 StudyList::AddStudy( const char *fileSystemPath )
00139 {
00140   if ( ! fileSystemPath ) return Study::SmartPtr::Null;
00141 
00142   const_iterator it = this->begin();
00143   while ( it != this->end() ) 
00144     {
00145     // if this study is already in the list, we're done.
00146     if ( ! strcmp( it->first->GetFileSystemPath(), fileSystemPath ) )
00147       return Study::SmartPtr::Null;
00148     ++it;
00149     }
00150   
00151   Study::SmartPtr newStudy( Study::Read( fileSystemPath ) );
00152   if ( newStudy ) 
00153     {
00154     int suffix = 0;
00155     while ( this->FindStudyName( newStudy->GetName() ) ) {
00156     newStudy->SetMakeName( NULL, suffix++ );
00157     }
00158     
00159     (*this)[newStudy];
00160   }
00161 
00162   return newStudy;
00163 }
00164 
00165 void 
00166 StudyList::AddStudy( Study::SmartPtr& study )
00167 {
00168   if ( !study ) return;
00169 
00170   const char* newStudyPath = study->GetFileSystemPath();
00171 
00172   const_iterator it = this->begin();
00173   while ( it != this->end() ) {
00174     // if this study is already in the list, we're done.
00175     if ( ! strcmp( it->first->GetFileSystemPath(), newStudyPath ) )
00176       return;
00177     ++it;
00178   }
00179   
00180   // insert new study into map.
00181   (*this)[study];
00182 }
00183 
00184 void 
00185 StudyList::AddXform
00186 ( const char *fromStudyPath, const char *toStudyPath, AffineXform::SmartPtr& affineXform, WarpXform::SmartPtr& warpXform )
00187 {
00188   Study::SmartPtr fromStudy = this->FindStudyPath( fromStudyPath, true /*create*/ );
00189   Study::SmartPtr toStudy = this->FindStudyPath( toStudyPath, true /*create*/ );
00190 
00191   this->AddXform( fromStudy, toStudy, affineXform, warpXform );
00192 }
00193 
00194 void 
00195 StudyList::AddXform
00196 ( Study::SmartPtr& fromStudy, Study::SmartPtr& toStudy, AffineXform::SmartPtr& affineXform, WarpXform::SmartPtr& warpXform )
00197 {
00198   if ( !fromStudy || !toStudy ) return;
00199 
00200   if ( affineXform ) 
00201     {
00202     Xform::SmartPtr xform = affineXform;
00203     (*this)[fromStudy].insert( std::multimap<Study::SmartPtr,Xform::SmartPtr>::value_type( toStudy, xform ) );
00204     }
00205   if ( warpXform ) 
00206     {
00207     Xform::SmartPtr xform = warpXform;
00208     (*this)[fromStudy].insert( std::multimap<Study::SmartPtr,Xform::SmartPtr>::value_type( toStudy, xform ) );
00209     }
00210 }
00211 
00212 void 
00213 StudyList::DeleteStudy( const Study* study )
00214 {
00215   iterator it = this->begin();
00216   while ( it != this->end() ) {
00217     if ( it->first == study ) {
00218       this->erase( it );
00219     }
00220     break;
00221   }
00222 }
00223 
00224 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines