1 /*=========================================================================
4 Module: $RCSfile: gdcmSerieHelper.cxx,v $
6 Date: $Date: 2005/07/21 06:39:24 $
7 Version: $Revision: 1.15 $
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 of the gdcm::Serie
41 FileList *l = GetFirstCoherentFileList();
44 // For all the files of a File list
45 for (gdcm::FileList::iterator it = l->begin();
53 l = GetNextCoherentFileList();
58 * \brief Canonical destructor.
60 SerieHelper::~SerieHelper()
62 // For all the Coherent File lists of the gdcm::Serie
63 FileList *l = GetFirstCoherentFileList();
66 // For all the files of a Coherent File list
67 for (FileList::iterator it = l->begin();
75 l = GetNextCoherentFileList();
79 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
85 * \brief add a gdcm::File to the list corresponding to its Serie UID
86 * @param filename Name of the file to deal with
88 void SerieHelper::AddFileName(std::string const &filename)
90 // Create a DICOM file
91 File *header = new File ();
92 header->SetLoadMode(LoadMode);
93 header->SetFileName( filename );
96 if ( header->IsReadable() )
99 // First step the user has defined a set of rules for the DICOM file
100 // he is looking for.
101 // make sure the file correspond to his set of rules:
104 for(SerieRestrictions::iterator it = Restrictions.begin();
105 it != Restrictions.end();
109 // doesn't compile (no matching function...).
110 const std::string s;// = header->GetEntryValue( r.first );
111 if ( !Util::DicomStringEqual(s, r.second.c_str()) )
113 // Argh ! This rule is unmatch let's just quit
119 // Just keep 'new style' for Rules
121 for(SerieExRestrictions::iterator it2 = ExRestrictions.begin();
122 it2 != ExRestrictions.end();
125 const ExRule &r = *it2;
126 s = header->GetEntryValue( r.group, r.elem );
127 if ( !Util::CompareDicomString(s, r.value.c_str(), r.op) )
129 // Argh ! This rule is unmatch let's just quit
135 if ( allrules ) // all rules are respected:
137 // Allright ! we have a found a DICOM that match the user expectation.
140 // 0020 000e UI REL Series Instance UID
141 const std::string &uid = header->GetEntryValue (0x0020, 0x000e);
142 // if uid == GDCM_UNFOUND then consistently we should find GDCM_UNFOUND
143 // no need here to do anything special
145 if ( CoherentFileListHT.count(uid) == 0 )
147 gdcmDebugMacro(" New Serie UID :[" << uid << "]");
148 // create a std::list in 'uid' position
149 CoherentFileListHT[uid] = new FileList;
151 // Current Serie UID and DICOM header seems to match; add the file:
152 CoherentFileListHT[uid]->push_back( header );
156 // at least one rule was unmatch we need to deallocate the file:
162 gdcmWarningMacro("Could not read file: " << filename );
168 * \brief add a gdcm::File to the first (and supposed to be unique) list
169 * of the gdcm::SerieHelper.
170 * \warning : this method should be used by aware users only!
171 * User is supposed to know the files he want to deal with
172 * and consider them they belong to the same Serie
173 * (even if their Serie UID is different)
174 * user will probabely OrderFileList() this list (actually, ordering
175 * user choosen gdm::File is the sole interest of this method)
176 * Moreover, using vtkGdcmReader::SetCoherentFileList() will avoid
177 * vtkGdcmReader parsing twice the same files.
178 * *no* coherence check is performed, but those specified
179 * by SerieHelper::AddRestriction()
180 * @param header gdcm::File* of the file to deal with
182 void SerieHelper::AddGdcmFile(File *header)
185 // First step the user has defined a set of rules for the DICOM
186 // he is looking for.
187 // make sure the file correspond to his set of rules:
188 for(SerieRestrictions::iterator it = Restrictions.begin();
189 it != Restrictions.end();
193 const std::string s;// = header->GetEntryValue( r.first );
194 if ( !Util::DicomStringEqual(s, r.second.c_str()) )
196 // Argh ! This rule is unmatch let's just quit
201 if ( allrules ) // all rules are respected:
203 // Allright ! we have a found a DICOM that match the user expectation.
206 const std::string &uid = "0";
207 // Serie UID of the gdcm::File* may be different.
208 // User is supposed to know what he wants
210 if ( CoherentFileListHT.count(uid) == 0 )
212 gdcmDebugMacro(" New Serie UID :[" << uid << "]");
213 // create a std::list in 'uid' position
214 CoherentFileListHT[uid] = new FileList;
216 // Current Serie UID and DICOM header seems to match; add the file:
217 CoherentFileListHT[uid]->push_back( header );
219 // 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 Get the first List while visiting the CoherentFileListHT
301 * @return The first FileList if found, otherwhise NULL
303 FileList *SerieHelper::GetFirstCoherentFileList()
305 ItListHt = CoherentFileListHT.begin();
306 if ( ItListHt != CoherentFileListHT.end() )
307 return ItListHt->second;
312 * \brief Get the next List while visiting the CoherentFileListHT
313 * \note : meaningfull only if GetFirstCoherentFileList() already called
314 * @return The next FileList if found, otherwhise NULL
316 FileList *SerieHelper::GetNextCoherentFileList()
318 gdcmAssertMacro (ItListHt != CoherentFileListHT.end());
321 if ( ItListHt != CoherentFileListHT.end() )
322 return ItListHt->second;
327 * \brief Get the Coherent Files list according to its Serie UID
328 * @param SerieUID SerieUID
329 * \return pointer to the Coherent Files list if found, otherwhise NULL
331 FileList *SerieHelper::GetCoherentFileList(std::string SerieUID)
333 if ( CoherentFileListHT.count(SerieUID) == 0 )
335 return CoherentFileListHT[SerieUID];
338 //-----------------------------------------------------------------------------
341 //-----------------------------------------------------------------------------
344 * \brief sorts the images, according to their Patient Position
345 * We may order, considering :
346 * -# Image Position Patient
348 * -# More to come :-)
349 * WARNING : FileList = std::vector<File* >
350 * @param fileList Coherent File list (same Serie UID) to sort
351 * @return false only if the header is bugged !
353 bool SerieHelper::ImagePositionPatientOrdering( FileList *fileList )
354 //based on Jolinda Smith's algorithm
356 //iop is calculated based on the file file
361 float min = 0, max = 0;
364 std::vector<float> distlist;
366 //!\todo rewrite this for loop.
367 for ( FileList::const_iterator
368 it = fileList->begin();
369 it != fileList->end(); ++it )
373 (*it)->GetImageOrientationPatient( cosines );
375 // You only have to do this once for all slices in the volume. Next,
376 // for each slice, calculate the distance along the slice normal
377 // using the IPP tag ("dist" is initialized to zero before reading
378 // the first slice) :
379 normal[0] = cosines[1]*cosines[5] - cosines[2]*cosines[4];
380 normal[1] = cosines[2]*cosines[3] - cosines[0]*cosines[5];
381 normal[2] = cosines[0]*cosines[4] - cosines[1]*cosines[3];
383 ipp[0] = (*it)->GetXOrigin();
384 ipp[1] = (*it)->GetYOrigin();
385 ipp[2] = (*it)->GetZOrigin();
388 for ( int i = 0; i < 3; ++i )
390 dist += normal[i]*ipp[i];
393 distlist.push_back( dist );
400 ipp[0] = (*it)->GetXOrigin();
401 ipp[1] = (*it)->GetYOrigin();
402 ipp[2] = (*it)->GetZOrigin();
405 for ( int i = 0; i < 3; ++i )
407 dist += normal[i]*ipp[i];
410 distlist.push_back( dist );
412 min = (min < dist) ? min : dist;
413 max = (max > dist) ? max : dist;
418 // Then I order the slices according to the value "dist". Finally, once
419 // I've read in all the slices, I calculate the z-spacing as the difference
420 // between the "dist" values for the first two slices.
421 FileVector CoherentFileVector(n);
422 // CoherentFileVector.reserve( n );
423 CoherentFileVector.resize( n );
424 // gdcmAssertMacro( CoherentFileVector.capacity() >= n );
426 // Find out if min/max are coherent
429 gdcmWarningMacro( "Looks like all images have the exact same image position."
430 << "No PositionPatientOrdering sort performed" );
434 float step = (max - min)/(n - 1);
438 //VC++ don't understand what scope is !! it -> it2
439 for (FileList::const_iterator it2 = fileList->begin();
440 it2 != fileList->end(); ++it2, ++n)
443 //Assumption: all files are present (no one missing)
444 pos = (int)( fabs( (distlist[n]-min)/step) + .5 );
446 // a Dicom 'Serie' may contain scout views
447 // and images may have differents directions
448 // -> More than one may have the same 'pos'
449 // Sorting has then NO meaning !
450 if (CoherentFileVector[pos]==NULL)
451 CoherentFileVector[pos] = *it2;
454 gdcmWarningMacro( "At least 2 files with same position. No PositionPatientOrdering sort performed");
459 fileList->clear(); // doesn't delete list elements, only nodes
461 //VC++ don't understand what scope is !! it -> it3
462 for (FileVector::const_iterator it3 = CoherentFileVector.begin();
463 it3 != CoherentFileVector.end(); ++it3)
465 fileList->push_back( *it3 );
469 CoherentFileVector.clear();
474 bool SerieHelper::ImageNumberLessThan(File *file1, File *file2)
476 return file1->GetImageNumber() < file2->GetImageNumber();
480 * \brief sorts the images, according to their Image Number
481 * \note Works only on bona fide files (i.e image number is a character string
482 * corresponding to an integer)
483 * within a bona fide serie (i.e image numbers are consecutive)
484 * @param fileList Coherent File list (same Serie UID) to sort
485 * @return false if non bona fide stuff encountered
487 bool SerieHelper::ImageNumberOrdering(FileList *fileList)
490 int n = fileList->size();
492 FileList::const_iterator it = fileList->begin();
493 min = max = (*it)->GetImageNumber();
495 for (; it != fileList->end(); ++it, ++n)
497 pos = (*it)->GetImageNumber();
498 min = (min < pos) ? min : pos;
499 max = (max > pos) ? max : pos;
502 // Find out if image numbers are coherent (consecutive)
503 if ( min == max || max == 0 || max >= (n+min) )
505 gdcmWarningMacro( " 'Image numbers' not coherent. No ImageNumberOrdering sort performed.");
508 std::sort(fileList->begin(), fileList->end(), SerieHelper::ImageNumberLessThan );
513 bool SerieHelper::FileNameLessThan(File *file1, File *file2)
515 return file1->GetFileName() < file2->GetFileName();
519 * \brief sorts the images, according to their File Name
520 * @param fileList Coherent File list (same Serie UID) to sort
521 * @return false only if the header is bugged !
523 bool SerieHelper::FileNameOrdering(FileList *fileList)
525 std::sort(fileList->begin(), fileList->end(), SerieHelper::FileNameLessThan);
529 //-----------------------------------------------------------------------------
532 * \brief Canonical printer.
534 void SerieHelper::Print(std::ostream &os, std::string const &indent)
536 // For all the Coherent File lists of the gdcm::Serie
537 CoherentFileListmap::iterator itl = CoherentFileListHT.begin();
538 if ( itl == CoherentFileListHT.end() )
540 gdcmWarningMacro( "No Coherent File list found" );
543 while (itl != CoherentFileListHT.end())
545 os << "Serie UID :[" << itl->first << "]" << std::endl;
547 // For all the files of a Coherent File list
548 for (FileList::iterator it = (itl->second)->begin();
549 it != (itl->second)->end();
552 os << indent << " --- " << (*it)->GetFileName() << std::endl;
558 //-----------------------------------------------------------------------------
559 } // end namespace gdcm