1 /*=========================================================================
4 Module: $RCSfile: gdcmSerieHelper.cxx,v $
6 Date: $Date: 2005/07/19 09:04:58 $
7 Version: $Revision: 1.13 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
19 #include "gdcmSerieHelper.h"
20 #include "gdcmDirList.h"
22 #include "gdcmDebug.h"
32 //-----------------------------------------------------------------------------
33 // Constructor / Destructor
35 * \brief Constructor from a given SerieHelper
37 SerieHelper::SerieHelper()
39 // For all the File lists of the gdcm::Serie
40 FileList *l = GetFirstCoherentFileList();
43 // For all the files of a File list
44 for (gdcm::FileList::iterator it = l->begin();
52 l = GetNextCoherentFileList();
57 * \brief Canonical destructor.
59 SerieHelper::~SerieHelper()
61 // For all the Coherent File lists of the gdcm::Serie
62 FileList *l = GetFirstCoherentFileList();
65 // For all the files of a Coherent File list
66 for (FileList::iterator it = l->begin();
74 l = GetNextCoherentFileList();
78 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
84 * \brief add a gdcm::File to the list corresponding to its Serie UID
85 * @param filename Name of the file to deal with
87 void SerieHelper::AddFileName(std::string const &filename)
89 // Create a DICOM file
90 File *header = new File ();
91 header->SetLoadMode(LoadMode);
92 header->SetFileName( filename );
94 //File *header = new File( filename ); // Deprecated old style
95 if ( header->IsReadable() )
98 // First step the user has defined a set of rules for the DICOM
100 // make sure the file correspond to his set of rules:
101 for(SerieRestrictions::iterator it = Restrictions.begin();
102 it != Restrictions.end();
106 const std::string s;// = header->GetEntryValue( r.first );
107 if ( !Util::DicomStringEqual(s, r.second.c_str()) )
109 // Argh ! This rule is unmatch let's just quit
114 if ( allrules ) // all rules are respected:
116 // Allright ! we have a found a DICOM that match the user expectation.
119 // 0020 000e UI REL Series Instance UID
120 const std::string &uid = header->GetEntryValue (0x0020, 0x000e);
121 // if uid == GDCM_UNFOUND then consistently we should find GDCM_UNFOUND
122 // no need here to do anything special
124 if ( CoherentFileListHT.count(uid) == 0 )
126 gdcmDebugMacro(" New Serie UID :[" << uid << "]");
127 // create a std::list in 'uid' position
128 CoherentFileListHT[uid] = new FileList;
130 // Current Serie UID and DICOM header seems to match; add the file:
131 CoherentFileListHT[uid]->push_back( header );
135 // at least one rule was unmatch we need to deallocate the file:
141 gdcmWarningMacro("Could not read file: " << filename );
147 * \brief add a gdcm::File to the first (and supposed to be unique) list
148 * of the gdcm::SerieHelper.
149 * \warning : this method should be used by aware users only!
150 * User is supposed to know the files he want to deal with
151 * and consider them they belong to the same Serie
152 * (even if their Serie UID is different)
153 * user will probabely OrderFileList() this list (actually, ordering
154 * user choosen gdm::File is the sole interest of this method)
155 * Moreover, using vtkGdcmReader::SetCoherentFileList() will avoid
156 * vtkGdcmReader parsing twice the same files.
157 * *no* coherence check is performed, but those specified
158 * by SerieHelper::AddRestriction()
159 * @param header gdcm::File* of the file to deal with
161 void SerieHelper::AddGdcmFile(File *header)
164 // First step the user has defined a set of rules for the DICOM
165 // he is looking for.
166 // make sure the file correspond to his set of rules:
167 for(SerieRestrictions::iterator it = Restrictions.begin();
168 it != Restrictions.end();
172 const std::string s;// = header->GetEntryValue( r.first );
173 if ( !Util::DicomStringEqual(s, r.second.c_str()) )
175 // Argh ! This rule is unmatch let's just quit
180 if ( allrules ) // all rules are respected:
182 // Allright ! we have a found a DICOM that match the user expectation.
185 const std::string &uid = "0";
186 // Serie UID of the gdcm::File* may be different.
187 // User is supposed to know what he wants
189 if ( CoherentFileListHT.count(uid) == 0 )
191 gdcmDebugMacro(" New Serie UID :[" << uid << "]");
192 // create a std::list in 'uid' position
193 CoherentFileListHT[uid] = new FileList;
195 // Current Serie UID and DICOM header seems to match; add the file:
196 CoherentFileListHT[uid]->push_back( header );
198 // Even if a rule was unmatch we don't deallocate the gdcm::File:
201 * \brief add a rules for restricting a DICOM file to be in the serie we are
202 * trying to find. For example you can select only the DICOM file from a
203 * directory which would have a particular EchoTime==4.0.
204 * This method is a user level, value is not required to be formatted as a DICOM
207 void SerieHelper::AddRestriction(TagKey const &key, std::string const &value)
212 Restrictions.push_back( r );
216 * \brief Sets the root Directory
217 * @param dir Name of the directory to deal with
218 * @param recursive whether we want explore recursively the Directory
220 void SerieHelper::SetDirectory(std::string const &dir, bool recursive)
222 DirList dirList(dir, recursive); // OS specific
224 DirListType filenames_list = dirList.GetFilenames();
225 for( DirListType::const_iterator it = filenames_list.begin();
226 it != filenames_list.end(); ++it)
233 * \brief Sorts the given File List
234 * \warning This could be implemented in a 'Strategy Pattern' approach
235 * But as I don't know how to do it, I leave it this way
236 * BTW, this is also a Strategy, I don't know this is the best approach :)
238 void SerieHelper::OrderFileList(FileList *coherentFileList)
240 if ( ImagePositionPatientOrdering( coherentFileList ) )
244 else if ( ImageNumberOrdering(coherentFileList ) )
250 FileNameOrdering(coherentFileList );
255 * \brief Get the first List while visiting the CoherentFileListHT
256 * @return The first FileList if found, otherwhise NULL
258 FileList *SerieHelper::GetFirstCoherentFileList()
260 ItListHt = CoherentFileListHT.begin();
261 if ( ItListHt != CoherentFileListHT.end() )
262 return ItListHt->second;
267 * \brief Get the next List while visiting the CoherentFileListHT
268 * \note : meaningfull only if GetFirstCoherentFileList() already called
269 * @return The next FileList if found, otherwhise NULL
271 FileList *SerieHelper::GetNextCoherentFileList()
273 gdcmAssertMacro (ItListHt != CoherentFileListHT.end());
276 if ( ItListHt != CoherentFileListHT.end() )
277 return ItListHt->second;
282 * \brief Get the Coherent Files list according to its Serie UID
283 * @param SerieUID SerieUID
284 * \return pointer to the Coherent Files list if found, otherwhise NULL
286 FileList *SerieHelper::GetCoherentFileList(std::string SerieUID)
288 if ( CoherentFileListHT.count(SerieUID) == 0 )
290 return CoherentFileListHT[SerieUID];
293 //-----------------------------------------------------------------------------
296 //-----------------------------------------------------------------------------
299 * \brief sorts the images, according to their Patient Position
300 * We may order, considering :
301 * -# Image Position Patient
303 * -# More to come :-)
304 * WARNING : FileList = std::vector<File* >
305 * @param fileList Coherent File list (same Serie UID) to sort
306 * @return false only if the header is bugged !
308 bool SerieHelper::ImagePositionPatientOrdering( FileList *fileList )
309 //based on Jolinda Smith's algorithm
311 //iop is calculated based on the file file
316 float min = 0, max = 0;
319 std::vector<float> distlist;
321 //!\todo rewrite this for loop.
322 for ( FileList::const_iterator
323 it = fileList->begin();
324 it != fileList->end(); ++it )
328 (*it)->GetImageOrientationPatient( cosines );
330 // You only have to do this once for all slices in the volume. Next,
331 // for each slice, calculate the distance along the slice normal
332 // using the IPP tag ("dist" is initialized to zero before reading
333 // the first slice) :
334 normal[0] = cosines[1]*cosines[5] - cosines[2]*cosines[4];
335 normal[1] = cosines[2]*cosines[3] - cosines[0]*cosines[5];
336 normal[2] = cosines[0]*cosines[4] - cosines[1]*cosines[3];
338 ipp[0] = (*it)->GetXOrigin();
339 ipp[1] = (*it)->GetYOrigin();
340 ipp[2] = (*it)->GetZOrigin();
343 for ( int i = 0; i < 3; ++i )
345 dist += normal[i]*ipp[i];
348 distlist.push_back( dist );
355 ipp[0] = (*it)->GetXOrigin();
356 ipp[1] = (*it)->GetYOrigin();
357 ipp[2] = (*it)->GetZOrigin();
360 for ( int i = 0; i < 3; ++i )
362 dist += normal[i]*ipp[i];
365 distlist.push_back( dist );
367 min = (min < dist) ? min : dist;
368 max = (max > dist) ? max : dist;
373 // Then I order the slices according to the value "dist". Finally, once
374 // I've read in all the slices, I calculate the z-spacing as the difference
375 // between the "dist" values for the first two slices.
376 FileVector CoherentFileVector(n);
377 // CoherentFileVector.reserve( n );
378 CoherentFileVector.resize( n );
379 // gdcmAssertMacro( CoherentFileVector.capacity() >= n );
381 // Find out if min/max are coherent
384 gdcmWarningMacro( "Looks like all images have the exact same image position."
385 << "No PositionPatientOrdering sort performed" );
389 float step = (max - min)/(n - 1);
393 //VC++ don't understand what scope is !! it -> it2
394 for (FileList::const_iterator it2 = fileList->begin();
395 it2 != fileList->end(); ++it2, ++n)
398 //Assumption: all files are present (no one missing)
399 pos = (int)( fabs( (distlist[n]-min)/step) + .5 );
401 // a Dicom 'Serie' may contain scout views
402 // and images may have differents directions
403 // -> More than one may have the same 'pos'
404 // Sorting has then NO meaning !
405 if (CoherentFileVector[pos]==NULL)
406 CoherentFileVector[pos] = *it2;
409 gdcmWarningMacro( "At least 2 files with same position. No PositionPatientOrdering sort performed");
414 fileList->clear(); // doesn't delete list elements, only nodes
416 //VC++ don't understand what scope is !! it -> it3
417 for (FileVector::const_iterator it3 = CoherentFileVector.begin();
418 it3 != CoherentFileVector.end(); ++it3)
420 fileList->push_back( *it3 );
424 CoherentFileVector.clear();
429 bool SerieHelper::ImageNumberLessThan(File *file1, File *file2)
431 return file1->GetImageNumber() < file2->GetImageNumber();
435 * \brief sorts the images, according to their Image Number
436 * \note Works only on bona fide files (i.e image number is a character string
437 * corresponding to an integer)
438 * within a bona fide serie (i.e image numbers are consecutive)
439 * @param fileList Coherent File list (same Serie UID) to sort
440 * @return false if non bona fide stuff encountered
442 bool SerieHelper::ImageNumberOrdering(FileList *fileList)
445 int n = fileList->size();
447 FileList::const_iterator it = fileList->begin();
448 min = max = (*it)->GetImageNumber();
450 for (; it != fileList->end(); ++it, ++n)
452 pos = (*it)->GetImageNumber();
453 min = (min < pos) ? min : pos;
454 max = (max > pos) ? max : pos;
457 // Find out if image numbers are coherent (consecutive)
458 if ( min == max || max == 0 || max >= (n+min) )
460 gdcmWarningMacro( " 'Image numbers' not coherent. No ImageNumberOrdering sort performed.");
463 std::sort(fileList->begin(), fileList->end(), SerieHelper::ImageNumberLessThan );
468 bool SerieHelper::FileNameLessThan(File *file1, File *file2)
470 return file1->GetFileName() < file2->GetFileName();
474 * \brief sorts the images, according to their File Name
475 * @param fileList Coherent File list (same Serie UID) to sort
476 * @return false only if the header is bugged !
478 bool SerieHelper::FileNameOrdering(FileList *fileList)
480 std::sort(fileList->begin(), fileList->end(), SerieHelper::FileNameLessThan);
484 //-----------------------------------------------------------------------------
487 * \brief Canonical printer.
489 void SerieHelper::Print(std::ostream &os, std::string const &indent)
491 // For all the Coherent File lists of the gdcm::Serie
492 CoherentFileListmap::iterator itl = CoherentFileListHT.begin();
493 if ( itl == CoherentFileListHT.end() )
495 gdcmWarningMacro( "No Coherent File list found" );
498 while (itl != CoherentFileListHT.end())
500 os << "Serie UID :[" << itl->first << "]" << std::endl;
502 // For all the files of a Coherent File list
503 for (FileList::iterator it = (itl->second)->begin();
504 it != (itl->second)->end();
507 os << indent << " --- " << (*it)->GetFileName() << std::endl;
513 //-----------------------------------------------------------------------------
514 } // end namespace gdcm