1 /*=========================================================================
4 Module: $RCSfile: vtkGdcm4DSplitter.cxx,v $
6 Date: $Date: 2011/03/29 13:33:48 $
7 Version: $Revision: 1.3 $
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 /* Raisons ne pas utiliser itkImageSeriesReader:
21 On Wed, Feb 16, 2011 at 11:51 AM, Roger Bramon Feixas <rogerbramon@gmail.com>
23 I'm developing with ITK 3.20 + GDCM 2.0.17 + VTK 5.6 and I've noticed
24 itkImageSeriesReader is ~2x slower than vtkGDCMImageReader (from GDCM2).
25 I compared both codes and I think the difference is the extra copy which
26 itkImageSeriesReader makes from ImageFileReader's output to its own output
27 (ImageSeriesReader::GenerateData() line 393).
31 /* ====================================================================
34 3D, 2D+T, 3D+T, n*2D+T, 4D images are not always stored the same way :
35 a single 'Dicom Serie',
36 several 'Dicom series' within a single directory
37 several 'Dicom series' within several directories
38 A 'Dicom Serie' doesn't mean always the same thing :
39 a given Slice along the time
40 a given Volume at a given time
41 Sometimes, an image within a serie is so artefacted than user decides to replace
44 User needs to be aware, *only him* knows want he wants to do.
45 vtkGdcm4DSplitter class does the job for hom
46 (it works on 3D or 2D+T images too)
48 User will have to specify some points
54 bool setDirName(std::string &dirName);
55 - a list of directories
56 bool setVectDirName(std::vector<std::string> &vectDirName);
58 bool setVectFileName(std::vector<std::string> &vectFileName);
60 - Recursive directory exploration
61 void setRecursive(bool recursive);
63 . Choose 'split' criterion :
64 ---------------------------
66 - ImagePositionPatient
67 void setSplitOnPosition();
68 - ImageOrientationPatient
69 void setSplitOnOrientation();
71 void setSplitOnTag(unsigned short splitGroup, unsigned short splitElem);
72 void setSplitConvertToFloat(bool conv);
74 . Choose 'sort' criterion :
75 --------------------------
77 - ImagePositionPatient
78 void setSortOnPosition();
80 void setSortOnTag(unsigned short sortGroup, unsigned short sortElem);
81 void setSortConvertToFloat(bool conv)
90 -a single vtkImageData:
91 vtkImageData *GetImageData();
92 - a vector of vtkImageData
93 std::vector<vtkImageData*> *GetImageDataVector();
95 ===================================================================== */
97 #include "gdcmSerieHelper.h"
99 #include "vtkGdcmReader.h"
100 #include "vtkGdcm4DSplitter.h"
102 #include "gdcmSerieHelper.h" // for ImagePositionPatientOrdering()
103 #include <stdlib.h> // for atof
105 vtkGdcm4DSplitter::vtkGdcm4DSplitter() :
106 SplitOnPosition(false), SplitOnOrientation(false), SplitOnTag(false),
107 SplitGroup(0), SplitElem(0),
109 SortOnPosition(false), SortOnOrientation(false), SortOnTag(false),
110 SortGroup(0), SortElem(0), SortConvertToFloat(false),
112 Recursive(false), TypeDir(0),
118 std::vector<vtkImageData*> * vtkGdcm4DSplitter::GetImageDataVector()
120 if (verbose) std::cout << "TypeDir " << TypeDir << std::endl;
122 return ImageDataVector;
126 std::vector<vtkImageData*> *t = new std::vector<vtkImageData*>;
127 t->push_back( ImageData );
131 return (std::vector<vtkImageData*>*) NULL;
134 vtkImageData *vtkGdcm4DSplitter::GetImageData()
141 return (*ImageDataVector)[0];
144 return (vtkImageData*) NULL;
148 bool vtkGdcm4DSplitter::setDirName(std::string &dirName)
150 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(dirName) )
152 std::cout << "[" << dirName << "] is NOT a directory" << std::endl;
160 bool vtkGdcm4DSplitter::setVectDirName(std::vector<std::string> &vectDirName)
162 int nbDir = vectDirName.size();
163 for (int iDir=0; iDir<nbDir; iDir++)
165 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(vectDirName[iDir]) )
167 std::cout << "[" << vectDirName[iDir] << "] is NOT a directory" << std::endl;
172 VectDirName = vectDirName;
177 bool vtkGdcm4DSplitter::setVectFileName(std::vector<std::string> &vectFileName)
179 if ( vectFileName.size() == 0)
181 std::cout << "[ vectFileName ] : empty list" << std::endl;
184 VectFileName = vectFileName;
189 bool vtkGdcm4DSplitter::CompareOnSortTagConvertToFloat(GDCM_NAME_SPACE::File *file1, GDCM_NAME_SPACE::File *file2)
191 if (verbose) printf ("%04x %04x\n", this->SortGroup,this->SortElem);
192 if (verbose) std :: cout << file1->GetEntryString(SortGroup,SortElem).c_str() << " : "
193 << atof(file1->GetEntryString(SortGroup,SortElem).c_str())
195 return atof(file1->GetEntryString(SortGroup,SortElem).c_str()) < atof(file2->GetEntryString(SortGroup,SortElem).c_str());
198 bool vtkGdcm4DSplitter::CompareOnSortTag(GDCM_NAME_SPACE::File *file1, GDCM_NAME_SPACE::File *file2)
200 return file1->GetEntryString(SortGroup,SortElem) < file2->GetEntryString(SortGroup,SortElem);
203 bool vtkGdcm4DSplitter::Go()
205 if (!SplitOnPosition && !SplitOnOrientation && !SplitOnTag )
207 ///\TODO (?) Throw an exception "Choose Splitting mode before!"
208 std::cout << "Choose Splitting mode before!" << std::endl;
214 entree nom de directory / Vecteur de noms?
216 recuperer la liste des gdcm::File*
217 passer a SerieHelper (pas de check du Serie UID)
220 trier chaque Coherent file set
221 passer chacun a un vtkGcdmReader
222 retourner le (vecteur de) vtkImageData
225 GDCM_NAME_SPACE::SerieHelper *s;
226 s = GDCM_NAME_SPACE::SerieHelper::New();
228 GDCM_NAME_SPACE::File *f;
229 GDCM_NAME_SPACE::DirListType fileNames;
233 ///\TODO (?) Throw an exception "Set input Directory name(s) / file names before!"
234 std::cout << "Set input Directory name(s) / file names before!" << std::endl;
237 else if (TypeDir == 1)
239 GDCM_NAME_SPACE::DirList dirlist(DirName, Recursive); // NO recursive exploration
240 fileNames = dirlist.GetFilenames(); // all the file names
243 else if (TypeDir == 2)
245 int nbDir = VectDirName.size();
246 GDCM_NAME_SPACE::DirListType tmpFileNames;
247 for (int iDir=0; iDir<nbDir; iDir++)
249 GDCM_NAME_SPACE::DirList dirlist(VectDirName[iDir], Recursive);
250 tmpFileNames = dirlist.GetFilenames();
251 // Concat two std::vector
252 //vector1.insert( vector1.end(), vector2.begin(), vector2.end() );
253 fileNames.insert( fileNames.end(), tmpFileNames.begin(), tmpFileNames.end() );
256 else if (TypeDir == 3)
258 fileNames=VectFileName;
261 GDCM_NAME_SPACE::FileList *l = new GDCM_NAME_SPACE::FileList; // (set of gdcm::File)
262 double floatTagvalue;
263 // Loop on all the gdcm-readable files
264 for (GDCM_NAME_SPACE::DirListType::iterator it = fileNames.begin();
265 it != fileNames.end();
268 int maxSize = 0x7fff; // load Elements of any length
269 f = GDCM_NAME_SPACE::File::New();
270 f->SetMaxSizeLoadEntry(maxSize);
271 f->SetFileName( *it );
275 std::cout << " Fail to load [" << *it << "]" << std::endl;
278 GDCM_NAME_SPACE::XCoherentFileSetmap xcm;
280 if (SplitOnOrientation)
282 s->SetDropDuplicatePositions(false);
283 xcm = s->SplitOnOrientation(l);
285 else if (SplitOnPosition)
287 s->SetDropDuplicatePositions(true);
288 xcm = s->SplitOnPosition(l);
292 s->SetDropDuplicatePositions(false);
294 // Crashes if DataElement not found
295 //std:: cout << GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->GetEntry(groupelem[0],groupelem[1])->GetName() << std::endl;
296 if ( ! SplitConvertToFloat )
297 xcm = s->SplitOnTagValue(l, SplitGroup, SplitElem);
300 xcm = s->SplitOnTagValueConvertToFloat(l, SplitGroup, SplitElem);
306 if(verbose) std::cout << "Empty XCoherent File Set after 'split' ?!?" << std::endl;
309 else if (xcm.size() == 1)
314 ImageDataVector = new std::vector<vtkImageData*>;
315 vtkGdcmReader *reader = vtkGdcmReader::New();
316 for (GDCM_NAME_SPACE::XCoherentFileSetmap::iterator i = xcm.begin();
321 std::cout << "--- xCoherentName = [" << (*i).first << "]" << std::endl;
323 // XCoherentFileSetmap map < critère de split, FileList (= std::vector de gdcm::File*) >
325 for (GDCM_NAME_SPACE::XCoherentFileSetmap::iterator i = xcm.begin();
330 std::cout << "==========================================xCoherentName = [" << (*i).first << "]" << std::endl;
334 if (verbose) std::cout << "SortOnPosition" << std::endl;
335 // (will be IPPSorter, in GDCM2)
336 s->ImagePositionPatientOrdering((*i).second);
337 if (verbose) std::cout << "out of SortOnPosition" << std::endl;
340 if (SortOnOrientation)
342 if (verbose) std::cout << "SortOnOrientation" << std::endl;
343 /// \TODO SortOnOrientation()
344 // Within a 'just to see' program,
345 // OrderFileList() causes trouble, since some files
346 // (eg:MIP views) don't have 'Position', now considered as mandatory
347 // --> Activated on user demand.
349 // Information is in :
350 // 0020,0032 : Image Position Patient
351 // 0020,0030 : Image Position (RET)
353 // we still miss an algo to sort an Orientation, given by 6 cosines!
354 // Anything like this, in GDCM2?
355 std::cout << "SortOnOrientation : not so easy - I(mage)O(rientation)P(atient)Sorter still missing! -" << std::endl;
356 // have a look at SerieHelper::SplitOnPosition() to have an idea of the mess!
358 //Better sort on the file name, right now...
359 s->FileNameOrdering((*i).second);
364 if (verbose) std::cout << "SortOnFileName" << std::endl;
365 if (verbose) std::cout << "taille " << ((*i).second)->size() << std::endl;
366 s->FileNameOrdering((*i).second);
367 if (verbose) std::cout << "Out of SortOnFileName" << std::endl;
372 if (verbose) std::cout << "SortOnTag" << std::endl;
373 printf ("--> %04x %04x\n", SortGroup,SortElem);
374 if ( SortConvertToFloat )
375 s->SetUserLessThanFunction( reinterpret_cast<bool (*)(gdcm13::File*, gdcm13::File*)>
376 ( &vtkGdcm4DSplitter::CompareOnSortTagConvertToFloat));
378 s->SetUserLessThanFunction( reinterpret_cast<bool (*)(gdcm13::File*, gdcm13::File*)>
379 ( &vtkGdcm4DSplitter::CompareOnSortTag));
381 // Anything like this, in GDCM2?
382 s->UserOrdering((*i).second);
383 if (verbose) std::cout << "Out of SortOnTag" << std::endl;
386 reader->SetCoherentFileList((*i).second);
388 ImageDataVector->push_back(reader->GetOutput() );
390 std::cout << std::endl;