1 /*=========================================================================
4 Module: $RCSfile: vtkGdcm4DSplitter.cxx,v $
6 Date: $Date: 2011/03/30 14:49:04 $
7 Version: $Revision: 1.5 $
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 (despite its name, 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 ==> WARNING : This one has troubles; do NOT use it, right now!
72 void setSplitOnTag(unsigned short splitGroup, unsigned short splitElem);
73 void setSplitConvertToFloat(bool conv);
74 - UserDefined Function
75 void setSortOnUserFunction (FoncComp f);
77 . Choose 'sort' criterion :
78 --------------------------
80 - ImagePositionPatient
81 void setSortOnPosition();
83 void setSortOnTag(unsigned short sortGroup, unsigned short sortElem);
84 void setSortConvertToFloat(bool conv)
93 -a single vtkImageData:
94 vtkImageData *GetImageData();
95 - a vector of vtkImageData
96 std::vector<vtkImageData*> *GetImageDataVector();
98 ===================================================================== */
100 #include "gdcmSerieHelper.h"
102 #include "vtkGdcmReader.h"
103 #include "vtkGdcm4DSplitter.h"
105 #include "gdcmSerieHelper.h" // for ImagePositionPatientOrdering()
106 #include <stdlib.h> // for atof
108 vtkGdcm4DSplitter::vtkGdcm4DSplitter() :
109 SplitOnPosition(false), SplitOnOrientation(false), SplitOnTag(false),
110 SplitGroup(0), SplitElem(0),
112 SortOnPosition(false), SortOnOrientation(false), SortOnTag(false),
113 SortGroup(0), SortElem(0), SortConvertToFloat(false),
115 Recursive(false), TypeDir(0),
121 std::vector<vtkImageData*> * vtkGdcm4DSplitter::GetImageDataVector()
123 if (verbose) std::cout << "TypeDir " << TypeDir << std::endl;
125 return ImageDataVector;
129 std::vector<vtkImageData*> *t = new std::vector<vtkImageData*>;
130 t->push_back( ImageData );
134 return (std::vector<vtkImageData*>*) NULL;
137 vtkImageData *vtkGdcm4DSplitter::GetImageData()
144 return (*ImageDataVector)[0];
147 return (vtkImageData*) NULL;
151 bool vtkGdcm4DSplitter::setDirName(std::string &dirName)
153 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(dirName) )
155 std::cout << "[" << dirName << "] is NOT a directory" << std::endl;
163 bool vtkGdcm4DSplitter::setVectDirName(std::vector<std::string> &vectDirName)
165 int nbDir = vectDirName.size();
166 for (int iDir=0; iDir<nbDir; iDir++)
168 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(vectDirName[iDir]) )
170 std::cout << "[" << vectDirName[iDir] << "] is NOT a directory" << std::endl;
175 VectDirName = vectDirName;
180 bool vtkGdcm4DSplitter::setVectFileName(std::vector<std::string> &vectFileName)
182 if ( vectFileName.size() == 0)
184 std::cout << "[ vectFileName ] : empty list" << std::endl;
187 VectFileName = vectFileName;
192 /*static */bool vtkGdcm4DSplitter::CompareOnSortTagConvertToFloat(GDCM_NAME_SPACE::File *file1, GDCM_NAME_SPACE::File *file2)
194 /* if (verbose) printf ("%04x %04x\n", this->SortGroup,this->SortElem);
195 if (verbose) std :: cout << file1->GetEntryString(SortGroup,SortElem).c_str() << " : "
196 << atof(file1->GetEntryString(SortGroup,SortElem).c_str())
199 // return atof(file1->GetEntryString(vtkGdcm4DSplitter::SortGroup,vtkGdcm4DSplitter::SortElem).c_str()) < atof(file2->GetEntryString(vtkGdcm4DSplitter::SortGroup,vtkGdcm4DSplitter::SortElem).c_str());
200 return atof(file1->GetEntryString(SortGroup,SortElem).c_str()) < atof(file2->GetEntryString(SortGroup,SortElem).c_str());
203 /*static */bool vtkGdcm4DSplitter::CompareOnSortTag(GDCM_NAME_SPACE::File *file1, GDCM_NAME_SPACE::File *file2)
205 return file1->GetEntryString(vtkGdcm4DSplitter::SortGroup,vtkGdcm4DSplitter::SortElem) < file2->GetEntryString(vtkGdcm4DSplitter::SortGroup,vtkGdcm4DSplitter::SortElem);
208 bool vtkGdcm4DSplitter::Go()
210 if (!SplitOnPosition && !SplitOnOrientation && !SplitOnTag )
212 ///\TODO (?) Throw an exception "Choose Splitting mode before!"
213 std::cout << "Choose Splitting mode before!" << std::endl;
219 entree nom de directory / Vecteur de noms?
221 recuperer la liste des gdcm::File*
222 passer a SerieHelper (pas de check du Serie UID)
225 trier chaque Coherent file set
226 passer chacun a un vtkGcdmReader
227 retourner le (vecteur de) vtkImageData
230 GDCM_NAME_SPACE::SerieHelper *s;
231 s = GDCM_NAME_SPACE::SerieHelper::New();
233 GDCM_NAME_SPACE::File *f;
234 GDCM_NAME_SPACE::DirListType fileNames;
238 ///\TODO (?) Throw an exception "Set input Directory name(s) / file names before!"
239 std::cout << "Set input Directory name(s) / file names before!" << std::endl;
242 else if (TypeDir == 1)
244 GDCM_NAME_SPACE::DirList dirlist(DirName, Recursive); // NO recursive exploration
245 fileNames = dirlist.GetFilenames(); // all the file names
248 else if (TypeDir == 2)
250 int nbDir = VectDirName.size();
251 GDCM_NAME_SPACE::DirListType tmpFileNames;
252 for (int iDir=0; iDir<nbDir; iDir++)
254 GDCM_NAME_SPACE::DirList dirlist(VectDirName[iDir], Recursive);
255 tmpFileNames = dirlist.GetFilenames();
256 // Concat two std::vector
257 //vector1.insert( vector1.end(), vector2.begin(), vector2.end() );
258 fileNames.insert( fileNames.end(), tmpFileNames.begin(), tmpFileNames.end() );
261 else if (TypeDir == 3)
263 fileNames=VectFileName;
266 GDCM_NAME_SPACE::FileList *l = new GDCM_NAME_SPACE::FileList; // (set of gdcm::File)
267 double floatTagvalue;
268 // Loop on all the gdcm-readable files
269 for (GDCM_NAME_SPACE::DirListType::iterator it = fileNames.begin();
270 it != fileNames.end();
273 int maxSize = 0x7fff; // load Elements of any length
274 f = GDCM_NAME_SPACE::File::New();
275 f->SetMaxSizeLoadEntry(maxSize);
276 f->SetFileName( *it );
280 std::cout << " Fail to load [" << *it << "]" << std::endl;
283 GDCM_NAME_SPACE::XCoherentFileSetmap xcm;
285 if (SplitOnOrientation)
287 s->SetDropDuplicatePositions(false);
288 xcm = s->SplitOnOrientation(l);
290 else if (SplitOnPosition)
292 s->SetDropDuplicatePositions(true);
293 xcm = s->SplitOnPosition(l);
297 s->SetDropDuplicatePositions(false);
299 // Crashes if DataElement not found
300 //std:: cout << GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->GetEntry(groupelem[0],groupelem[1])->GetName() << std::endl;
301 if ( ! SplitConvertToFloat )
302 xcm = s->SplitOnTagValue(l, SplitGroup, SplitElem);
305 xcm = s->SplitOnTagValueConvertToFloat(l, SplitGroup, SplitElem);
311 if(verbose) std::cout << "Empty XCoherent File Set after 'split' ?!?" << std::endl;
314 else if (xcm.size() == 1)
319 ImageDataVector = new std::vector<vtkImageData*>;
320 // vtkGdcmReader *reader = vtkGdcmReader::New(); // move inside the loop, or be clever using vtk!
322 for (GDCM_NAME_SPACE::XCoherentFileSetmap::iterator i = xcm.begin();
327 std::cout << "--- xCoherentName = [" << (*i).first << "]" << std::endl;
329 // XCoherentFileSetmap map < critère de split, FileList (= std::vector de gdcm::File*) >
331 for (GDCM_NAME_SPACE::XCoherentFileSetmap::iterator i = xcm.begin();
336 vtkGdcmReader *reader = vtkGdcmReader::New(); /// \FIXME : unable to delete!
339 std::cout << "==========================================xCoherentName = [" << (*i).first << "]" << std::endl;
343 if (verbose) std::cout << "SortOnPosition" << std::endl;
344 // (will be IPPSorter, in GDCM2)
345 s->ImagePositionPatientOrdering((*i).second);
346 if (verbose) std::cout << "out of SortOnPosition" << std::endl;
349 else if (SortOnOrientation)
351 if (verbose) std::cout << "SortOnOrientation" << std::endl;
352 /// \TODO SortOnOrientation()
353 // Within a 'just to see' program,
354 // OrderFileList() causes trouble, since some files
355 // (eg:MIP views) don't have 'Position', now considered as mandatory
356 // --> Activated on user demand.
358 // Information is in :
359 // 0020,0032 : Image Position Patient
360 // 0020,0030 : Image Position (RET)
362 // we still miss an algo to sort an Orientation, given by 6 cosines!
363 // Anything like this, in GDCM2?
364 std::cout << "SortOnOrientation : not so easy - I(mage)O(rientation)P(atient)Sorter still missing! -" << std::endl;
365 // have a look at SerieHelper::SplitOnOrientation() to have an idea of the mess!
367 //Better sort on the file name, right now...
368 s->FileNameOrdering((*i).second);
371 else if (SortOnFileName)
373 if (verbose) std::cout << "SortOnFileName" << std::endl;
374 if (verbose) std::cout << "taille " << ((*i).second)->size() << std::endl;
376 s->FileNameOrdering((*i).second);
377 if (verbose) std::cout << "Out of SortOnFileName" << std::endl;
382 if (verbose) std::cout << "SortOnTag" << std::endl;
383 printf ("--> %04x %04x\n", SortGroup,SortElem);
384 std::cout << "Sorry, troubles not solved yet; use SortOnUserFunction, right now!" << std::endl;
386 /* ==> WARNING : This one has troubles; do NOT use it, right now!
387 if ( SortConvertToFloat )
388 s->SetUserLessThanFunction( reinterpret_cast<bool (*)(gdcm13::File*, gdcm13::File*)>
389 ( &vtkGdcm4DSplitter::CompareOnSortTagConvertToFloat));
391 s->SetUserLessThanFunction( reinterpret_cast<bool (*)(gdcm13::File*, gdcm13::File*)>
392 ( &vtkGdcm4DSplitter::CompareOnSortTag));
394 // Anything like this, in GDCM2?
395 s->UserOrdering((*i).second);
398 //if (verbose) std::cout << "Out of SortOnTag" << std::endl;
399 std::cout << "NO ordering performed :-( " << std::endl;
402 else if (SortOnUserFunction)
404 if (verbose) std::cout << "SortOnUserFunction" << std::endl;
405 s->SetUserLessThanFunction( UserCompareFunction );
406 // Anything like this, in GDCM2?
407 s->UserOrdering((*i).second);
408 if (verbose) std::cout << "Out of SortOnUserFunction" << std::endl;
411 reader->SetCoherentFileList((*i).second);
414 /// \TODO : remove the following
415 //if (verbose) reader->GetOutput()->PrintSelf(std::cout, vtkIndent(2));
417 ImageDataVector->push_back(reader->GetOutput() );
419 std::cout << std::endl;
422 //reader->Delete(); // \TODO : fix