1 /*=========================================================================
4 Module: $RCSfile: gdcmSerieHelper.cxx,v $
6 Date: $Date: 2005/06/03 16:08:16 $
7 Version: $Revision: 1.9 $
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 (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( filename );
91 if( header->IsReadable() )
94 // First step the user has defined s set of rules for the DICOM he is looking for
95 // make sure the file correspond to his set of rules:
96 for(SerieRestrictions::iterator it = Restrictions.begin();
97 it != Restrictions.end();
101 const std::string s;// = header->GetEntryValue( r.first );
102 if( !Util::DicomStringEqual(s, r.second.c_str()))
104 // Argh ! This rule is unmatch let's just quit
109 if( allrules ) // all rules are respected:
111 // Alright ! we have a found a DICOM that match the user expectation.
114 // 0020 000e UI REL Series Instance UID
115 const std::string &uid = header->GetEntryValue (0x0020, 0x000e);
116 // if uid == GDCM_UNFOUND then consistently we should find GDCM_UNFOUND
117 // no need here to do anything special
119 if ( CoherentFileListHT.count(uid) == 0 )
121 gdcmDebugMacro(" New Serie UID :[" << uid << "]");
122 // create a std::list in 'uid' position
123 CoherentFileListHT[uid] = new FileList;
125 // Current Serie UID and DICOM header seems to match add the file:
126 CoherentFileListHT[uid]->push_back( header );
130 // at least one rule was unmatch we need to deallocate the file:
136 gdcmWarningMacro("Could not read file: " << filename );
141 * \brief add a rules for restricting a DICOM file to be in the serie we are
142 * trying to find. For example you can select only the DICOM file from a
143 * directory which would have a particular EchoTime==4.0.
144 * This method is a user level, value is not required to be formatted as a DICOM
147 void SerieHelper::AddRestriction(TagKey const &key, std::string const &value)
152 Restrictions.push_back( r );
156 * \brief Sets the root Directory
157 * @param dir Name of the directory to deal with
158 * @param recursive whether we want explore recursively the Directory
160 void SerieHelper::SetDirectory(std::string const &dir, bool recursive)
162 DirList dirList(dir, recursive); // OS specific
164 DirListType filenames_list = dirList.GetFilenames();
165 for( DirListType::const_iterator it = filenames_list.begin();
166 it != filenames_list.end(); ++it)
173 * \brief Sorts the given File List
174 * \warning This could be implemented in a 'Strategy Pattern' approach
175 * But as I don't know how to do it, I leave it this way
176 * BTW, this is also a Strategy, I don't know this is the best approach :)
178 void SerieHelper::OrderFileList(FileList *coherentFileList)
180 if( ImagePositionPatientOrdering( coherentFileList ) )
184 else if( ImageNumberOrdering(coherentFileList ) )
190 FileNameOrdering(coherentFileList );
195 * \brief Get the first List while visiting the CoherentFileListHT
196 * @return The first FileList if found, otherwhise NULL
198 FileList *SerieHelper::GetFirstCoherentFileList()
200 ItListHt = CoherentFileListHT.begin();
201 if( ItListHt != CoherentFileListHT.end() )
202 return ItListHt->second;
207 * \brief Get the next List while visiting the CoherentFileListHT
208 * \note : meaningfull only if GetFirstCoherentFileList already called
209 * @return The next FileList if found, otherwhise NULL
211 FileList *SerieHelper::GetNextCoherentFileList()
213 gdcmAssertMacro (ItListHt != CoherentFileListHT.end());
216 if ( ItListHt != CoherentFileListHT.end() )
217 return ItListHt->second;
222 * \brief Get the Coherent Files list according to its Serie UID
223 * @param SerieUID SerieUID
224 * \return pointer to the Coherent Files list if found, otherwhise NULL
226 FileList *SerieHelper::GetCoherentFileList(std::string SerieUID)
228 if ( CoherentFileListHT.count(SerieUID) == 0 )
230 return CoherentFileListHT[SerieUID];
233 //-----------------------------------------------------------------------------
236 //-----------------------------------------------------------------------------
239 * \brief sorts the images, according to their Patient Position
240 * We may order, considering :
241 * -# Image Position Patient
243 * -# More to come :-)
244 * WARNING : FileList = std::vector<File* >
245 * @param fileList Coherent File list (same Serie UID) to sort
246 * @return false only if the header is bugged !
248 bool SerieHelper::ImagePositionPatientOrdering( FileList *fileList )
249 //based on Jolinda's algorithm
251 //iop is calculated based on the file file
256 float min = 0, max = 0;
259 std::vector<float> distlist;
261 //!\todo rewrite this for loop.
262 for ( FileList::const_iterator
263 it = fileList->begin();
264 it != fileList->end(); ++it )
268 (*it)->GetImageOrientationPatient( cosines );
270 // You only have to do this once for all slices in the volume. Next,
271 // for each slice, calculate the distance along the slice normal
272 // using the IPP tag ("dist" is initialized to zero before reading
273 // the first slice) :
274 normal[0] = cosines[1]*cosines[5] - cosines[2]*cosines[4];
275 normal[1] = cosines[2]*cosines[3] - cosines[0]*cosines[5];
276 normal[2] = cosines[0]*cosines[4] - cosines[1]*cosines[3];
278 ipp[0] = (*it)->GetXOrigin();
279 ipp[1] = (*it)->GetYOrigin();
280 ipp[2] = (*it)->GetZOrigin();
283 for ( int i = 0; i < 3; ++i )
285 dist += normal[i]*ipp[i];
288 distlist.push_back( dist );
295 ipp[0] = (*it)->GetXOrigin();
296 ipp[1] = (*it)->GetYOrigin();
297 ipp[2] = (*it)->GetZOrigin();
300 for ( int i = 0; i < 3; ++i )
302 dist += normal[i]*ipp[i];
305 distlist.push_back( dist );
307 min = (min < dist) ? min : dist;
308 max = (max > dist) ? max : dist;
313 // Then I order the slices according to the value "dist". Finally, once
314 // I've read in all the slices, I calculate the z-spacing as the difference
315 // between the "dist" values for the first two slices.
316 FileVector CoherentFileVector(n);
317 // CoherentFileVector.reserve( n );
318 CoherentFileVector.resize( n );
319 // gdcmAssertMacro( CoherentFileVector.capacity() >= n );
321 // Find out if min/max are coherent
324 gdcmWarningMacro( "Looks like all images have the exact same image position...");
328 float step = (max - min)/(n - 1);
332 //VC++ don't understand what scope is !! it -> it2
333 for (FileList::const_iterator it2 = fileList->begin();
334 it2 != fileList->end(); ++it2, ++n)
337 //Assumption: all files are present (no one missing)
338 pos = (int)( fabs( (distlist[n]-min)/step) + .5 );
340 // a Dicom 'Serie' may contain scout views
341 // and images may have differents directions
342 // -> More than one may have the same 'pos'
343 // Sorting has then NO meaning !
344 if (CoherentFileVector[pos]==NULL)
345 CoherentFileVector[pos] = *it2;
348 gdcmWarningMacro( "2 files same position");
353 fileList->clear(); // doesn't delete list elements, only node
355 //VC++ don't understand what scope is !! it -> it3
356 for (FileVector::const_iterator it3 = CoherentFileVector.begin();
357 it3 != CoherentFileVector.end(); ++it3)
359 fileList->push_back( *it3 );
363 CoherentFileVector.clear();
368 bool SerieHelper::ImageNumberLessThan(File *file1, File *file2)
370 return file1->GetImageNumber() < file2->GetImageNumber();
374 * \brief sorts the images, according to their Image Number
375 * \note Works only on bona fide files (i.e image number is a character string
376 * corresponding to an integer)
377 * within a bona fide serie (i.e image numbers are consecutive)
378 * @param fileList Coherent File list (same Serie UID) to sort
379 * @return false if non nona fide stuff encountered
381 bool SerieHelper::ImageNumberOrdering(FileList *fileList)
384 int n = fileList->size();
386 FileList::const_iterator it = fileList->begin();
387 min = max = (*it)->GetImageNumber();
389 for (; it != fileList->end(); ++it, ++n)
391 pos = (*it)->GetImageNumber();
392 min = (min < pos) ? min : pos;
393 max = (max > pos) ? max : pos;
396 // Find out if image numbers are coherent (consecutive)
397 if( min == max || max == 0 || max >= (n+min))
400 std::sort(fileList->begin(), fileList->end(), SerieHelper::ImageNumberLessThan );
405 bool SerieHelper::FileNameLessThan(File *file1, File *file2)
407 return file1->GetFileName() < file2->GetFileName();
411 * \brief sorts the images, according to their File Name
412 * @param fileList Coherent File list (same Serie UID) to sort
413 * @return false only if the header is bugged !
415 bool SerieHelper::FileNameOrdering(FileList *fileList)
417 std::sort(fileList->begin(), fileList->end(), SerieHelper::FileNameLessThan);
421 //-----------------------------------------------------------------------------
424 * \brief Canonical printer.
426 void SerieHelper::Print(std::ostream &os, std::string const &indent)
428 // For all the Coherent File lists of the gdcm::Serie
429 CoherentFileListmap::iterator itl = CoherentFileListHT.begin();
430 if ( itl == CoherentFileListHT.end() )
432 gdcmWarningMacro( "No Coherent File list found" );
435 while (itl != CoherentFileListHT.end())
437 os << "Serie UID :[" << itl->first << "]" << std::endl;
439 // For all the files of a Coherent File list
440 for (FileList::iterator it = (itl->second)->begin();
441 it != (itl->second)->end();
444 os << indent << " --- " << (*it)->GetFileName() << std::endl;
450 //-----------------------------------------------------------------------------
451 } // end namespace gdcm