1 /*=========================================================================
4 Module: $RCSfile: vtkGdcm4DSplitter.cxx,v $
6 Date: $Date: 2011/04/20 15:03:54 $
7 Version: $Revision: 1.13 $
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 it
44 User needs to be aware, *only him* knows want he wants to do.
45 vtkGdcm4DSplitter class does the job for him
46 (despite its name, it works on 3D or 2D+T images too)
48 ==> To (try to) understand how the file store is organised, user is deeply encourage to use something like:
50 PrintFile dirin=Root_Directory_holding_the_images rec > 1.txt
52 open it with any test editor, and/or (Linux users)
53 grep a_supposed_to_be_string_of_interest 1.txt
56 Aware user will have to specify some points :
62 bool setDirName(std::string &dirName);
63 - a list of directories
64 bool setVectDirName(std::vector<std::string> &vectDirName);
66 bool setVectFileName(std::vector<std::string> &vectFileName);
67 - a list of gdcm::File*
68 bool setVectGdcmFile(std::vector<GDCM_NAME_SPACE::File *> &vectGdcmFile);
70 - Recursive directory exploration
71 void setRecursive(bool recursive);
73 . Choose 'split' criterion :
74 ---------------------------
76 - ImagePositionPatient
77 void setSplitOnPosition();
78 - ImageOrientationPatient
79 void setSplitOnOrientation();
81 void setSplitOnTag(unsigned short splitGroup, unsigned short splitElem);
82 void setSplitConvertToFloat(bool conv);
83 - UserDefined Function
84 void setSortOnUserFunction (FoncComp f);
86 for 'true' 3D image sets :
87 - if you want to get a single 3D vtkImageData, use SplitOnOrientation -i.e. no split-
88 - if you want to get a vector of 2D vtkImageData, use SplitOnPosition -i.e. one slice in each 'XCoherent fileset'-
90 for 'true' 4D multi-orientation image sets (i.e. a stack of n axial + m sagital + p coronal images, at different instants ...)
91 --> this is 5D, right?
92 (almost) nothing done, yet :
94 . Use a first time vtkGdcm4DSplitter with setSplitOnOrientation();
95 . Get the VectGdcmFileLists (a std::vector of 'XCoherent fileset')
96 . use vtkGdcm4DSplitter, with as many setVectGdcmFile(std::vector<GDCM_NAME_SPACE::File *> &vectGdcmFile) you need
97 one per element of std::vector<GDCM_NAME_SPACE::File *>
98 think on 'spliting' and 'sorting' it, according to your needs.
100 . Choose 'sort' criterion :
101 --------------------------
103 - ImagePositionPatient
104 void setSortOnPosition();
105 - ImageOrientationPatient
106 ==> Only in your dreams!
107 ==> or, please, write a IOP sorter ...
108 - UserDefined Function
109 void setSortOnUserFunction (FoncComp f);
111 void setSortOnFileName()
113 - Choose 'flip' option
114 ----------------------
115 VTK reader 'flop upside down' the images to display them correctly ?!?
116 When a 'math guy' gets the image, the first thing he does is flipping it again.
117 Avoid loosing CPU time using :
118 void SetFlipY(bool); // Wrong = no flip
127 // -a single vtkImageData:
128 // vtkImageData *GetImageData();
129 - a vector of vtkImageData
130 std::vector<vtkImageData*> *GetImageDataVector();
132 ===================================================================== */
134 #include "vtkGdcmReader.h"
135 #include "vtkGdcm4DSplitter.h"
137 #include "gdcmSerieHelper.h" // for ImagePositionPatientOrdering()
138 #include <stdlib.h> // for atof
140 // Constructor / Destructor
142 * \brief Constructor from a given vtkGdcm4DSplitter
144 vtkGdcm4DSplitter::vtkGdcm4DSplitter() :
145 SplitOnPosition(false), SplitOnOrientation(false), SplitOnTag(false),
146 SplitGroup(0), SplitElem(0), SplitConvertToFloat(false), SplitOnly(false),
148 SortOnPosition(false), SortOnOrientation(false), /* SortOnTag(false),*/ SortOnFileName(false), SortOnUserFunction(false),
149 /*SortGroup(0), SortElem(0), SortConvertToFloat(false),*/
151 Recursive(false),FlipY(true), TypeDir(0),
158 * \brief Canonical destructor.
160 vtkGdcm4DSplitter::~vtkGdcm4DSplitter()
162 /// \TODO : delete everything that must be!
165 // Locate Data to process
166 // ======================
168 * \brief sets the directories exploration mode
169 * @param recursive whether we want explore recursively the root Directory
171 void vtkGdcm4DSplitter::setRecursive(bool recursive)
177 * \brief Sets the root Directory to get the images from
178 * @param dirName name of the directory to deal with
180 bool vtkGdcm4DSplitter::setDirName(std::string &dirName)
182 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(dirName) )
184 std::cout << "[" << dirName << "] is NOT a directory" << std::endl;
193 * \brief Sets a list of Directories to get the images from
194 * @param vectDirName vector of directory names to deal with
196 bool vtkGdcm4DSplitter::setVectDirName(std::vector<std::string> &vectDirName)
198 int nbDir = vectDirName.size();
199 for (int iDir=0; iDir<nbDir; iDir++)
201 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(vectDirName[iDir]) )
203 std::cout << "[" << vectDirName[iDir] << "] is NOT a directory" << std::endl;
208 VectDirName = vectDirName;
214 * \brief Sets a list of files names to read
215 * @param vectFileName vector of file names to deal with
217 bool vtkGdcm4DSplitter::setVectFileName(std::vector<std::string> &vectFileName)
219 if ( vectFileName.size() == 0)
221 std::cout << "[ vectFileName ] : empty list" << std::endl;
224 VectFileName = vectFileName;
230 * \brief Sets an already filled std::vector of gdcm::File *
231 * @param vectGdcmFileName std::vector of gdcm::File *
234 bool vtkGdcm4DSplitter::setVectGdcmFile(GDCM_NAME_SPACE::FileList *vectGdcmFile)
236 if ( vectGdcmFile->size() == 0)
238 std::cout << "[ vectGdcmFile ] : empty list" << std::endl;
242 VectGdcmFile = vectGdcmFile;
251 * \brief asks for splitting Filesets according to the Position
253 void vtkGdcm4DSplitter::setSplitOnPosition()
255 SplitOnPosition=true;
256 SplitOnOrientation=false;
261 * \brief asks for splitting Filesets according to the Orientation
263 void vtkGdcm4DSplitter::setSplitOnOrientation()
265 SplitOnPosition=false;
266 SplitOnOrientation=true;
271 * \brief asks for splitting Filesets according to the value of a given Tag
272 * @param group group number of the target Element
273 * @param element element number of the target Element
275 void vtkGdcm4DSplitter::setSplitOnTag(unsigned short int splitGroup, unsigned short int splitElem)
277 SplitOnPosition=false;
278 SplitOnOrientation=false;
280 SplitGroup=splitGroup;
285 * \brief asks for converting to 'float' the tag values used as a splitting criteria (lexicographic order may not be suitable)
287 void vtkGdcm4DSplitter::setSplitConvertToFloat(bool conv) {SplitConvertToFloat=conv;}
290 * \brief asks for splitting Filesets according to what was asked for (no sorting, no reading data)
292 void vtkGdcm4DSplitter::setSplitOnly(bool s)
302 * \brief asks for IPP sorting each XCoherent gdcm::FILE set
304 void vtkGdcm4DSplitter::setSortOnPosition()
307 SortOnOrientation=false;
309 SortOnFileName=false;
310 SortOnUserFunction=false;
314 // use setSortOnUserFunction, instead!
315 // void setSortOnTag(unsigned short int sortGroup, unsigned short int sortElem)
317 // SortOnPosition=false;
318 // SortOnOrientation=false;
320 // SortOnFileName=false;
321 // SortOnUserFunction=false;
322 // SortGroup=sortGroup; SortElem=sortElem;
327 * \brief sets a user supplied function (comparison)
328 * @param f comparison function
330 void vtkGdcm4DSplitter::setSortOnUserFunction (FoncComp f)
332 UserCompareFunction=f;
333 SortOnPosition=false;
334 SortOnOrientation=false;
336 SortOnFileName=false;
337 SortOnUserFunction=true;
340 // void setSortConvertToFloat(bool conv)
342 // SortConvertToFloat=conv;
346 * \brief asks for sorting each XCoherent gdcm::FILE set, according to the File names
348 void vtkGdcm4DSplitter::setSortOnFileName()
350 SortOnPosition=false;
351 SortOnOrientation=false;
354 SortOnUserFunction=false;
358 * \brief returns a std::vector of gdcm::FileList* (gdcm::FileList : actually, a std::vector of gdcm::File*)
360 std::vector<GDCM_NAME_SPACE::FileList *> *vtkGdcm4DSplitter::GetVectGdcmFileLists()
365 GDCM_NAME_SPACE::XCoherentFileSetmap::iterator it;
366 for ( it = xcm.begin();
370 VectGdcmFileLists.push_back((*it).second);
372 return &VectGdcmFileLists;
376 * \brief returns a std::vector of [2D/3D, depending on what was passed] vtkImageData*
378 std::vector<vtkImageData*> * vtkGdcm4DSplitter::GetImageDataVector()
382 return ImageDataVector;
386 * \brief when user _knows_ only _one_ vtkImageData* is returned he may be interested in not getting a vector...
388 vtkImageData *vtkGdcm4DSplitter::GetImageData()
392 return (*ImageDataVector)[0];
396 // bool vtkGdcm4DSplitter::CompareOnSortTagConvertToFloat(GDCM_NAME_SPACE::File *file1, GDCM_NAME_SPACE::File *file2)
398 // return atof(file1->GetEntryString(vtkGdcm4DSplitter::SortGroup,vtkGdcm4DSplitter::SortElem).c_str()) < atof(file2->GetEntryString(vtkGdcm4DSplitter::SortGroup,vtkGdcm4DSplitter::SortElem).c_str());
399 // return atof(file1->GetEntryString(SortGroup,SortElem).c_str()) < atof(file2->GetEntryString(SortGroup,SortElem).c_str());
402 // bool vtkGdcm4DSplitter::CompareOnSortTag(GDCM_NAME_SPACE::File *file1, GDCM_NAME_SPACE::File *file2)
404 // return file1->GetEntryString(vtkGdcm4DSplitter::SortGroup,vtkGdcm4DSplitter::SortElem) < file2->GetEntryString(vtkGdcm4DSplitter::SortGroup,vtkGdcm4DSplitter::SortElem);
408 bool vtkGdcm4DSplitter::Go()
412 entree nom de directory / Vecteur de noms?
414 recuperer la liste des gdcm::File*
415 passer a SerieHelper (pas de check du Serie UID)
418 trier chaque Coherent file set
419 passer chacun a un vtkGcdmReader
420 retourner le (vecteur de) vtkImageData
423 if (!SplitOnPosition && !SplitOnOrientation && !SplitOnTag)
425 ///\TODO (?) Throw an exception "Choose Splitting mode before!"
426 std::cout << "Choose Splitting mode before!" << std::endl;
430 GDCM_NAME_SPACE::FileList *l;
432 GDCM_NAME_SPACE::SerieHelper *s;
433 s = GDCM_NAME_SPACE::SerieHelper::New();
436 // Load the gdcm::File* set, according to user's requierements
437 // ------------------------------------------------------------
439 l = getGdcmFileList();
440 std::cout << l->size() << " gdcm::File read" << std::endl;
442 // Split the gdcm::File* set, according to user's requierements
443 // ------------------------------------------------------------
445 if (SplitOnOrientation)
447 s->SetDropDuplicatePositions(false);
448 xcm = s->SplitOnOrientation(l);
450 else if (SplitOnPosition)
452 s->SetDropDuplicatePositions(true);
453 xcm = s->SplitOnPosition(l);
455 // the key of xcm follows lexicographical order
456 // (that may be different than the 'distance' order)
457 // we have to reorganize it!
459 reorgXCoherentFileSetmap(xcm);
463 s->SetDropDuplicatePositions(false);
465 // Crashes if DataElement not found
466 //std:: cout << GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->GetEntry(groupelem[0],groupelem[1])->GetName() << std::endl;
467 if ( ! SplitConvertToFloat )
468 xcm = s->SplitOnTagValue(l, SplitGroup, SplitElem);
471 xcm = s->SplitOnTagValueConvertToFloat(l, SplitGroup, SplitElem);
478 std::cout << "Empty XCoherent File Set after 'split' ?!?" << std::endl;
483 std::cout << xcm.size() << " XCoherent entries found" << std::endl;
486 std::cout <<"SplitOnly " << SplitOnly << std::endl;
491 // ------------------------------------------------------------
493 ImageDataVector = new std::vector<vtkImageData*>;
495 /// \TODO move inside the loop, or be clever using vtk!
496 // vtkGdcmReader *reader = vtkGdcmReader::New(); // move inside the loop, or be clever using vtk!
498 // XCoherentFileSetmap map < critère de split, FileList (= std::vector de gdcm::File*) >
500 for (GDCM_NAME_SPACE::XCoherentFileSetmap::iterator i = xcm.begin();
504 vtkGdcmReader *reader = vtkGdcmReader::New(); /// \TODO FIXME : unable to delete!
506 reader->SetFlipY(FlipY);
507 // better user SetFileLowerLeft()
508 /// \TODO : modify vtkGdcmReader !
510 std::cout << " --- xCoherentName = [" << (*i).first << "]" << std::endl;
514 if (verbose) std::cout << "SortOnPosition" << std::endl;
515 // (will be IPPSorter, in GDCM2)
516 s->ImagePositionPatientOrdering((*i).second);
517 if (verbose) std::cout << "out of SortOnPosition" << std::endl;
520 else if (SortOnOrientation)
522 if (verbose) std::cout << "SortOnOrientation" << std::endl;
523 /// \TODO (?) SortOnOrientation()
525 // we still miss an algo to sort an Orientation, given by 6 cosines!
526 // Anything like this, in GDCM2?
527 std::cout << "SortOnOrientation : not so easy - I(mage)O(rientation)P(atient)Sorter still missing! -" << std::endl;
528 // have a look at SerieHelper::SplitOnOrientation() to have an idea of the mess!
530 //Better sort on the file name, right now...
531 s->FileNameOrdering((*i).second);
534 else if (SortOnFileName)
536 if (verbose) std::cout << "SortOnFileName" << std::endl;
537 if (verbose) std::cout << "taille " << ((*i).second)->size() << std::endl;
539 s->FileNameOrdering((*i).second);
540 if (verbose) std::cout << "Out of SortOnFileName" << std::endl;
543 // else if (SortOnTag)
545 // if (verbose) std::cout << "SortOnTag" << std::endl;
546 // printf ("--> %04x %04x\n", SortGroup,SortElem);
547 // std::cout << "Sorry, troubles not solved yet; use SortOnUserFunction, right now!" << std::endl;
549 /* ==> WARNING : This one has troubles; do NOT use it, right now!
550 // a pointer to fonction cannot be casted as a pointer to member function!
551 // Use SortOnUserFunction, instead!
553 // if ( SortConvertToFloat )
554 // s->SetUserLessThanFunction( reinterpret_cast<bool (*)(gdcm13::File*, gdcm13::File*)>
555 // ( &vtkGdcm4DSplitter::CompareOnSortTagConvertToFloat));
557 // s->SetUserLessThanFunction( reinterpret_cast<bool (*)(gdcm13::File*, gdcm13::File*)>
558 ( &vtkGdcm4DSplitter::CompareOnSortTag));
560 // Anything like this, in GDCM2?
561 // s->UserOrdering((*i).second);
564 // //if (verbose) std::cout << "Out of SortOnTag" << std::endl;
565 // std::cout << "NO ordering performed :-( " << std::endl;
568 else if (SortOnUserFunction)
570 if (verbose) std::cout << "SortOnUserFunction" << std::endl;
571 s->SetUserLessThanFunction( UserCompareFunction );
572 // Anything like this, in GDCM2?
573 s->UserOrdering((*i).second);
574 if (verbose) std::cout << "Out of SortOnUserFunction" << std::endl;
577 reader->SetCoherentFileList((*i).second);
580 /// \TODO : remove the following
582 // std::cout << "reader->GetOutput() :" << std::endl;
583 // reader->GetOutput()->PrintSelf(std::cout, vtkIndent(2));
586 ImageDataVector->push_back(reader->GetOutput() );
588 std::vector<vtkImageData*>::iterator it;
589 //if (verbose) // JPR
591 for(it=ImageDataVector->begin(); it!=ImageDataVector->end(); ++it) {
592 // std::cout << "-in vtkGdcm4DSplitter --------------------------" << std::endl;
593 // (*it)->PrintSelf(std::cout, vtkIndent(2));
595 //std::cout << std::endl;
598 //reader->Delete(); // \TODO : fix
608 * \brief Load the gdcm::File* set, according to user's requierements
609 * returns a std::vector of gdcm::File* (gdcm::FileList : actually, a std::vector of gdcm::File*)
613 GDCM_NAME_SPACE::FileList *vtkGdcm4DSplitter::getGdcmFileList()
616 GDCM_NAME_SPACE::File *f;
617 GDCM_NAME_SPACE::DirListType fileNames;
620 // Fill fileNames with the user supplied file names (in any)
621 // ------------------------------------------------
623 if (TypeDir == 0 ) // Nothing was set as input...
625 ///\TODO (?) Throw an exception "Set input Directory name(s) / file names before!"
626 std::cout << "Set input Directory name(s) / file names before!" << std::endl;
629 else if (TypeDir == 1) // A root directory name was set as input
631 GDCM_NAME_SPACE::DirList dirlist(DirName, Recursive); // NO recursive exploration
632 fileNames = dirlist.GetFilenames(); // all the file names
635 else if (TypeDir == 2) // a std::vector of directory names was set as input
637 int nbDir = VectDirName.size();
638 GDCM_NAME_SPACE::DirListType tmpFileNames;
639 for (int iDir=0; iDir<nbDir; iDir++)
641 GDCM_NAME_SPACE::DirList dirlist(VectDirName[iDir], Recursive);
642 tmpFileNames = dirlist.GetFilenames();
643 // Concat two std::vector
644 //vector1.insert( vector1.end(), vector2.begin(), vector2.end() );
645 fileNames.insert( fileNames.end(), tmpFileNames.begin(), tmpFileNames.end() );
648 else if (TypeDir == 3) // a list of files names was set as input
650 fileNames=VectFileName;
654 // Fill l with the gdcm::File* corresponding to the files
655 // --------------------------------------
658 GDCM_NAME_SPACE::FileList *l = new GDCM_NAME_SPACE::FileList; // (set of gdcm::File*)
660 if (TypeDir == 4) // an already existing std::vector of gdcm::File* was set as input
666 double floatTagvalue;
667 // Loop on all the gdcm-readable files
668 for (GDCM_NAME_SPACE::DirListType::iterator it = fileNames.begin();
669 it != fileNames.end();
672 int maxSize = 0x7fff; // load Elements of any length
673 f = GDCM_NAME_SPACE::File::New();
674 f->SetMaxSizeLoadEntry(maxSize);
675 f->SetFileName( *it );
679 std::cout << " Fail to load [" << *it << "]" << std::endl;
687 void vtkGdcm4DSplitter::reorgXCoherentFileSetmap(GDCM_NAME_SPACE::XCoherentFileSetmap &xcm)
690 the key of the 'XCoherentFileSetmap', is a std::string, used as if it was found in the Dicom header
691 Normaly(?), it's suitable for almost everything ...
692 ... but the 'Image Position Patient'.
693 We need to order the 'XCoherentFileSetmap' (NOT the content of each XCoherentFileSet!) according to the IPP,
694 using Jolinda Smith's algorithm.
695 (we use a subset of the one defined in gdcm::SerieHelper)
700 std::vector<ELEM> vectElem;
702 /* just to remember :
707 GDCM_NAME_SPACE::File *file;
713 for (GDCM_NAME_SPACE::XCoherentFileSetmap::iterator i = xcm.begin();
718 std::cout << "--- xCoherentName = [" << (*i).first << "]" << std::endl;
720 e.strIPP = (*i).first;
721 e.file = *(((*i).second)->begin()); // all the gdcm::File of a given xcm item *have* the same IPP; first one is enough
723 vectElem.push_back(e);
725 sortVectElem(&vectElem);
727 // now, create the final std::map !
728 // final_xcm<to_str(e.dist , xcm[e.strIPP]>
730 // check what we need to free !
734 std::string str_dist;
735 int lgr=vectElem.size();
736 GDCM_NAME_SPACE::XCoherentFileSetmap final_xcm;
737 for (int i2=0; i2<lgr; i2++)
739 dist = (vectElem[i2].dist*1000);
740 sprintf(c_dist,"%010d",dist);
743 std::cout << "dist " << vectElem[i2].dist
744 << " str_dist " << str_dist
745 << " IPP " << vectElem[i2].strIPP
749 final_xcm[str_dist] = xcm[vectElem[i2].strIPP];
752 /// \TODO : check what needs to be cleared // JPR
759 bool vtkGdcm4DSplitter::sortVectElem(std::vector<ELEM> *fileList)
761 //based on Jolinda Smith's algorithm
763 // NOTE : if you need to use Jolinda Smith's algorithm, get the one inside gdcm::SerieHelper
764 // this one is a light version.
766 //Tags always use the same coordinate system, where "x" is left
767 //to right, "y" is posterior to anterior, and "z" is foot to head (RAH).
769 //iop is calculated based on the file file
774 double min = 0, max = 0;
777 //double ZSpacing; // useless here! // JPR
778 bool DirectOrder = true; // remove it!
780 // ZSpacing = -1.0; // will be updated if process doesn't fail
782 //std::multimap<double,File *> distmultimap; // JPR
783 std::multimap<double,ELEM> distmultimap; // JPR
785 // Use a multimap to sort the distances from 0,0,0
786 //for ( FileList::const_iterator // JPR
787 for ( std::vector<ELEM>::iterator // JPR
788 it = fileList->begin();
789 it != fileList->end(); ++it )
791 //gdcmDebugMacro("deal with " << (*it)->file->GetFileName() );
794 (*it).file->GetImageOrientationPatient( cosines );
796 // The "Image Orientation Patient" tag gives the direction cosines
797 // for the rows and columns for the three axes defined above.
798 // Typical axial slices will have a value 1/0/0/0/1/0:
799 // rows increase from left to right,
800 // columns increase from posterior to anterior. This is your everyday
801 // "looking up from the bottom of the head with the eyeballs up" image.
803 // The "Image Position Patient" tag gives the coordinates of the first
804 // voxel in the image in the "RAH" coordinate system, relative to some
807 // First, calculate the slice normal from IOP :
809 // You only have to do this once for all slices in the volume. Next,
810 // for each slice, calculate the distance along the slice normal
811 // using the IPP ("Image Position Patient") tag.
812 // ("dist" is initialized to zero before reading the first slice) :
814 normal[0] = cosines[1]*cosines[5] - cosines[2]*cosines[4];
815 normal[1] = cosines[2]*cosines[3] - cosines[0]*cosines[5];
816 normal[2] = cosines[0]*cosines[4] - cosines[1]*cosines[3];
818 // For each slice (here : the first), calculate the distance along
819 // the slice normal using the IPP tag
821 ipp[0] = (*it).file->GetXOrigin();
822 ipp[1] = (*it).file->GetYOrigin();
823 ipp[2] = (*it).file->GetZOrigin();
826 for ( int i = 0; i < 3; ++i )
828 dist += normal[i]*ipp[i];
831 //gdcmDebugMacro("dist : " << dist);
832 distmultimap.insert(std::pair<const double,ELEM>(dist, *it));
839 // Next, for each slice, calculate the distance along the slice normal
841 ipp[0] = (*it).file->GetXOrigin();
842 ipp[1] = (*it).file->GetYOrigin();
843 ipp[2] = (*it).file->GetZOrigin();
846 for ( int i = 0; i < 3; ++i )
848 dist += normal[i]*ipp[i];
851 (*it).dist = dist; // JPR
853 distmultimap.insert(std::pair<const double,ELEM>(dist, *it));
854 //gdcmDebugMacro("dist : " << dist);
855 min = (min < dist) ? min : dist;
856 max = (max > dist) ? max : dist;
860 // gdcmDebugMacro("After parsing vector, nb of elements : " << fileList->size() );
862 /*Useless here. // JPR
864 // Find out if min/max are coherent
867 gdcmWarningMacro("Looks like all images have the exact same image position. "
868 << "No PositionPatientOrdering sort performed. "
869 << "No 'ZSpacing' calculated! ");
874 /* Useless here, 'split' already done. // JPR
876 // Check to see if image shares a common position
878 for (std::multimap<double, File *>::iterator it2 = distmultimap.begin();
879 it2 != distmultimap.end();
883 gdcmDebugMacro("Check if image shares a common position : " << ((*it2).second).file->GetFileName() );
885 if (distmultimap.count((*it2).first) != 1)
887 gdcmWarningMacro("File: ["
888 << ((*it2).second->GetFileName())
889 << "] : more than ONE file at distance: '"
891 << " (position is not unique!) "
892 << "No PositionPatientOrdering sort performed. "
893 << "No 'ZSpacing' calculated! ");
900 if (! DropDuplicatePositions)
906 // Now, we can calculate Z Spacing as the difference
907 // between the "dist" values for the first two slices.
909 // The following (un)-commented out code is let here
910 // to be re-used by whomsoever is interested...
912 //std::multimap<double, ELEM>::iterator it5 = distmultimap.begin();
913 //double d1 = (*it5).first;
915 //double d2 = (*it5).first;
917 //if (ZSpacing < 0.0)
918 // ZSpacing = - ZSpacing;
920 fileList->clear(); // doesn't delete list elements, only nodes
922 // Acording to user requierement, we sort direct order or reverse order.
925 for (std::multimap<double, ELEM>::iterator it3 = distmultimap.begin();
926 it3 != distmultimap.end();
929 fileList->push_back( (*it3).second );
931 /* useless here! // JPR
933 if (DropDuplicatePositions)
935 // ImagePositionPatientOrdering wrong duplicates are found ???
936 // --> fixed. See comment
938 it3 = distmultimap.upper_bound((*it3).first); // skip all duplicates
939 // the upper_bound function increments the iterator to the next non-duplicate entry
940 // The for loop iteration also increments the iterator, which causes the code to skip every other image
941 // --> decrement the iterator after the upper_bound function call
943 if (it3 == distmultimap.end() ) // if last image, stop iterate
949 else // user asked for reverse order
951 std::multimap<double, ELEM>::const_iterator it4;
952 it4 = distmultimap.end();
956 fileList->push_back( (*it4).second );
958 /* useless here // JPR
960 if (DropDuplicatePositions) // skip all duplicates
962 // lower_bound finds the next element that is
963 // less than or *equal to* the current value!
964 //it4 = distmultimap.lower_bound((*it4).first);
967 std::multimap<double, ELEM>::const_iterator itPrev = it4;
968 while (itPrev->first == it4->first)
972 if (it4 == distmultimap.begin() ) // if first image, stop iterate
976 } while (it4 != distmultimap.begin() );
979 distmultimap.clear();