Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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
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 }