1 /*=========================================================================
4 Module: $RCSfile: gdcmSerieHelper.cxx,v $
6 Date: $Date: 2005/07/30 18:13:24 $
7 Version: $Revision: 1.17 $
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 "gdcmDictEntry.h" // for TranslateToKey
23 #include "gdcmDebug.h"
33 //-----------------------------------------------------------------------------
34 // Constructor / Destructor
36 * \brief Constructor from a given SerieHelper
38 SerieHelper::SerieHelper()
40 // For all the File lists that may already exist within the gdcm::Serie
41 FileList *l = GetFirstCoherentFileList();
44 // For all the files of a File list
45 for (gdcm::FileList::iterator it = l->begin();
49 delete *it; // remove entry
52 delete l; // remove the list
53 l = GetNextCoherentFileList();
59 * \brief Canonical destructor.
61 SerieHelper::~SerieHelper()
63 // For all the Coherent File lists of the gdcm::Serie
64 FileList *l = GetFirstCoherentFileList();
67 // For all the files of a Coherent File list
68 for (FileList::iterator it = l->begin();
72 delete *it; // remove entry
75 delete l; // remove the list
76 l = GetNextCoherentFileList();
80 //-----------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
86 * \brief add a gdcm::File to the list corresponding to its Serie UID
87 * @param filename Name of the file to deal with
89 void SerieHelper::AddFileName(std::string const &filename)
91 // Create a DICOM file
92 File *header = new File ();
93 header->SetLoadMode(LoadMode);
94 header->SetFileName( filename );
97 if ( header->IsReadable() )
100 // First step the user has defined a set of rules for the DICOM file
101 // he is looking for.
102 // make sure the file correspond to his set of rules:
105 for(SerieRestrictions::iterator it = Restrictions.begin();
106 it != Restrictions.end();
110 // doesn't compile (no matching function...).
111 const std::string s;// = header->GetEntryValue( r.first );
112 if ( !Util::DicomStringEqual(s, r.second.c_str()) )
114 // Argh ! This rule is unmatch let's just quit
120 // Just keep 'new style' for Rules
122 for(SerieExRestrictions::iterator it2 = ExRestrictions.begin();
123 it2 != ExRestrictions.end();
126 const ExRule &r = *it2;
127 s = header->GetEntryValue( r.group, r.elem );
128 if ( !Util::CompareDicomString(s, r.value.c_str(), r.op) )
130 // Argh ! This rule is unmatch let's just quit
136 if ( allrules ) // all rules are respected:
138 // Allright ! we have a found a DICOM that match the user expectation.
141 // 0020 000e UI REL Series Instance UID
142 const std::string &uid = header->GetEntryValue (0x0020, 0x000e);
143 // if uid == GDCM_UNFOUND then consistently we should find GDCM_UNFOUND
144 // no need here to do anything special
146 if ( CoherentFileListHT.count(uid) == 0 )
148 gdcmDebugMacro(" New Serie UID :[" << uid << "]");
149 // create a std::list in 'uid' position
150 CoherentFileListHT[uid] = new FileList;
152 // Current Serie UID and DICOM header seems to match; add the file:
153 CoherentFileListHT[uid]->push_back( header );
157 // at least one rule was unmatch we need to deallocate the file:
163 gdcmWarningMacro("Could not read file: " << filename );
169 * \brief add a gdcm::File to the first (and supposed to be unique) list
170 * of the gdcm::SerieHelper.
171 * \warning : this method should be used by aware users only!
172 * User is supposed to know the files he want to deal with
173 * and consider them they belong to the same Serie
174 * (even if their Serie UID is different)
175 * user will probabely OrderFileList() this list (actually, ordering
176 * user choosen gdm::File is the sole interest of this method)
177 * Moreover, using vtkGdcmReader::SetCoherentFileList() will avoid
178 * vtkGdcmReader parsing twice the same files.
179 * *no* coherence check is performed, but those specified
180 * by SerieHelper::AddRestriction()
181 * @param header gdcm::File* of the file to deal with
183 void SerieHelper::AddGdcmFile(File *header)
186 // First step the user has defined a set of rules for the DICOM
187 // he is looking for.
188 // make sure the file correspond to his set of rules:
189 for(SerieRestrictions::iterator it = Restrictions.begin();
190 it != Restrictions.end();
194 const std::string s;// = header->GetEntryValue( r.first );
195 if ( !Util::DicomStringEqual(s, r.second.c_str()) )
197 // Argh ! This rule is unmatch let's just quit
202 if ( allrules ) // all rules are respected:
204 // Allright ! we have a found a DICOM that match the user expectation.
207 const std::string &uid = "0";
208 // Serie UID of the gdcm::File* may be different.
209 // User is supposed to know what he wants
211 if ( CoherentFileListHT.count(uid) == 0 )
213 gdcmDebugMacro(" New Serie UID :[" << uid << "]");
214 // create a std::list in 'uid' position
215 CoherentFileListHT[uid] = new FileList;
217 // Current Serie UID and DICOM header seems to match; add the file:
218 CoherentFileListHT[uid]->push_back( header );
220 // Even if a rule was unmatch we don't deallocate the gdcm::File:
224 * \brief add a rules for restricting a DICOM file to be in the serie we are
225 * trying to find. For example you can select only the DICOM file from a
226 * directory which would have a particular EchoTime==4.0.
227 * This method is a user level, value is not required to be formatted as a DICOM
230 void SerieHelper::AddRestriction(uint16_t group, uint16_t elem,
231 std::string const &value, int op)
238 ExRestrictions.push_back( r );
241 #ifndef GDCM_LEGACY_REMOVE
243 * \brief add a rules for restricting a DICOM file to be in the serie we are
244 * trying to find. For example you can select only the DICOM file from a
245 * directory which would have a particular EchoTime==4.0.
246 * This method is a user level, value is not required to be formatted as a DICOM
248 * @deprecated use : AddRestriction(uint16_t group, uint16_t elem,
249 * std::string const &value, int op);
251 void SerieHelper::AddRestriction(TagKey const &key, std::string const &value)
256 Restrictions.push_back( r );
261 * \brief Sets the root Directory
262 * @param dir Name of the directory to deal with
263 * @param recursive whether we want explore recursively the Directory
265 void SerieHelper::SetDirectory(std::string const &dir, bool recursive)
267 DirList dirList(dir, recursive); // OS specific
269 DirListType filenames_list = dirList.GetFilenames();
270 for( DirListType::const_iterator it = filenames_list.begin();
271 it != filenames_list.end(); ++it)
278 * \brief Sorts the given File List
279 * \warning This could be implemented in a 'Strategy Pattern' approach
280 * But as I don't know how to do it, I leave it this way
281 * BTW, this is also a Strategy, I don't know this is the best approach :)
283 void SerieHelper::OrderFileList(FileList *coherentFileList)
285 if ( ImagePositionPatientOrdering( coherentFileList ) )
289 else if ( ImageNumberOrdering(coherentFileList ) )
295 FileNameOrdering(coherentFileList );
300 * \brief Elementary coherence checking of the files with the same Serie UID
301 * Only sizes and pixel type are checked right now ...
303 bool SerieHelper::IsCoherent(FileList *coherentFileList)
305 if(coherentFileList->size() == 1)
308 FileList::const_iterator it = coherentFileList->begin();
310 int nX = (*it)->GetXSize();
311 int nY = (*it)->GetYSize();
312 int pixelSize = (*it)->GetPixelSize();
316 it != coherentFileList->end();
319 if ( (*it)->GetXSize() != nX )
321 if ( (*it)->GetYSize() != nY )
323 if ( (*it)->GetPixelSize() != pixelSize )
325 // probabely more is to be checked (?)
330 * \brief Get the first List while visiting the CoherentFileListHT
331 * @return The first FileList if found, otherwhise NULL
333 FileList *SerieHelper::GetFirstCoherentFileList()
335 ItListHt = CoherentFileListHT.begin();
336 if ( ItListHt != CoherentFileListHT.end() )
337 return ItListHt->second;
342 * \brief Get the next List while visiting the CoherentFileListHT
343 * \note : meaningfull only if GetFirstCoherentFileList() already called
344 * @return The next FileList if found, otherwhise NULL
346 FileList *SerieHelper::GetNextCoherentFileList()
348 gdcmAssertMacro (ItListHt != CoherentFileListHT.end());
351 if ( ItListHt != CoherentFileListHT.end() )
352 return ItListHt->second;
357 * \brief Get the Coherent Files list according to its Serie UID
358 * @param SerieUID SerieUID
359 * \return pointer to the Coherent Files list if found, otherwhise NULL
361 FileList *SerieHelper::GetCoherentFileList(std::string SerieUID)
363 if ( CoherentFileListHT.count(SerieUID) == 0 )
365 return CoherentFileListHT[SerieUID];
368 //-----------------------------------------------------------------------------
371 //-----------------------------------------------------------------------------
374 * \brief sorts the images, according to their Patient Position
375 * We may order, considering :
376 * -# Image Position Patient
378 * -# More to come :-)
379 * WARNING : FileList = std::vector<File* >
380 * @param fileList Coherent File list (same Serie UID) to sort
381 * @return false only if the header is bugged !
383 bool SerieHelper::ImagePositionPatientOrdering( FileList *fileList )
384 //based on Jolinda Smith's algorithm
386 //iop is calculated based on the file file
391 float min = 0, max = 0;
394 std::vector<float> distlist;
396 //!\todo rewrite this for loop.
397 for ( FileList::const_iterator
398 it = fileList->begin();
399 it != fileList->end(); ++it )
403 (*it)->GetImageOrientationPatient( cosines );
405 // You only have to do this once for all slices in the volume. Next,
406 // for each slice, calculate the distance along the slice normal
407 // using the IPP tag ("dist" is initialized to zero before reading
408 // the first slice) :
409 normal[0] = cosines[1]*cosines[5] - cosines[2]*cosines[4];
410 normal[1] = cosines[2]*cosines[3] - cosines[0]*cosines[5];
411 normal[2] = cosines[0]*cosines[4] - cosines[1]*cosines[3];
413 ipp[0] = (*it)->GetXOrigin();
414 ipp[1] = (*it)->GetYOrigin();
415 ipp[2] = (*it)->GetZOrigin();
418 for ( int i = 0; i < 3; ++i )
420 dist += normal[i]*ipp[i];
423 distlist.push_back( dist );
430 ipp[0] = (*it)->GetXOrigin();
431 ipp[1] = (*it)->GetYOrigin();
432 ipp[2] = (*it)->GetZOrigin();
435 for ( int i = 0; i < 3; ++i )
437 dist += normal[i]*ipp[i];
440 distlist.push_back( dist );
442 min = (min < dist) ? min : dist;
443 max = (max > dist) ? max : dist;
448 // Then I order the slices according to the value "dist". Finally, once
449 // I've read in all the slices, I calculate the z-spacing as the difference
450 // between the "dist" values for the first two slices.
451 FileVector CoherentFileVector(n);
452 // CoherentFileVector.reserve( n );
453 CoherentFileVector.resize( n );
454 // gdcmAssertMacro( CoherentFileVector.capacity() >= n );
456 // Find out if min/max are coherent
459 gdcmWarningMacro( "Looks like all images have the exact same image position."
460 << "No PositionPatientOrdering sort performed" );
464 float step = (max - min)/(n - 1);
468 //VC++ don't understand what scope is !! it -> it2
469 for (FileList::const_iterator it2 = fileList->begin();
470 it2 != fileList->end(); ++it2, ++n)
473 //Assumption: all files are present (no one missing)
474 pos = (int)( fabs( (distlist[n]-min)/step) + .5 );
476 // a Dicom 'Serie' may contain scout views
477 // and images may have differents directions
478 // -> More than one may have the same 'pos'
479 // Sorting has then NO meaning !
480 if (CoherentFileVector[pos]==NULL)
481 CoherentFileVector[pos] = *it2;
484 gdcmWarningMacro( "At least 2 files with same position. No PositionPatientOrdering sort performed");
489 fileList->clear(); // doesn't delete list elements, only nodes
493 //VC++ don't understand what scope is !! it -> it3
494 for (FileVector::const_iterator it3 = CoherentFileVector.begin();
495 it3 != CoherentFileVector.end(); ++it3)
497 fileList->push_back( *it3 );
500 else // user asked for reverse order
502 FileVector::const_iterator it4;
503 it4 = CoherentFileVector.end();
507 fileList->push_back( *it4 );
508 } while (it4 != CoherentFileVector.begin() );
512 CoherentFileVector.clear();
517 bool SerieHelper::ImageNumberLessThan(File *file1, File *file2)
519 return file1->GetImageNumber() < file2->GetImageNumber();
522 bool SerieHelper::ImageNumberGreaterThan(File *file1, File *file2)
524 return file1->GetImageNumber() > file2->GetImageNumber();
527 * \brief sorts the images, according to their Image Number
528 * \note Works only on bona fide files (i.e image number is a character string
529 * corresponding to an integer)
530 * within a bona fide serie (i.e image numbers are consecutive)
531 * @param fileList Coherent File list (same Serie UID) to sort
532 * @return false if non bona fide stuff encountered
534 bool SerieHelper::ImageNumberOrdering(FileList *fileList)
537 int n = fileList->size();
539 FileList::const_iterator it = fileList->begin();
540 min = max = (*it)->GetImageNumber();
542 for (; it != fileList->end(); ++it, ++n)
544 pos = (*it)->GetImageNumber();
545 min = (min < pos) ? min : pos;
546 max = (max > pos) ? max : pos;
549 // Find out if image numbers are coherent (consecutive)
550 if ( min == max || max == 0 || max >= (n+min) )
552 gdcmWarningMacro( " 'Image numbers' not coherent. No ImageNumberOrdering sort performed.");
556 std::sort(fileList->begin(), fileList->end(), SerieHelper::ImageNumberLessThan );
558 std::sort(fileList->begin(), fileList->end(), SerieHelper::ImageNumberGreaterThan );
563 bool SerieHelper::FileNameLessThan(File *file1, File *file2)
565 return file1->GetFileName() < file2->GetFileName();
568 bool SerieHelper::FileNameGreaterThan(File *file1, File *file2)
570 return file1->GetFileName() > file2->GetFileName();
573 * \brief sorts the images, according to their File Name
574 * @param fileList Coherent File list (same Serie UID) to sort
575 * @return false only if the header is bugged !
577 bool SerieHelper::FileNameOrdering(FileList *fileList)
580 std::sort(fileList->begin(), fileList->end(), SerieHelper::FileNameLessThan);
582 std::sort(fileList->begin(), fileList->end(), SerieHelper::FileNameGreaterThan);
587 //-----------------------------------------------------------------------------
590 * \brief Canonical printer.
592 void SerieHelper::Print(std::ostream &os, std::string const &indent)
594 // For all the Coherent File lists of the gdcm::Serie
595 CoherentFileListmap::iterator itl = CoherentFileListHT.begin();
596 if ( itl == CoherentFileListHT.end() )
598 gdcmWarningMacro( "No Coherent File list found" );
601 while (itl != CoherentFileListHT.end())
603 os << "Serie UID :[" << itl->first << "]" << std::endl;
605 // For all the files of a Coherent File list
606 for (FileList::iterator it = (itl->second)->begin();
607 it != (itl->second)->end();
610 os << indent << " --- " << (*it)->GetFileName() << std::endl;
616 //-----------------------------------------------------------------------------
617 } // end namespace gdcm