X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=vtk%2FvtkGdcm4DSplitter.cxx;h=e3e27d813c4eb72ee04cbe72971da146689d38be;hb=refs%2Fheads%2Fmaster;hp=69079de8e72469feb9abe55a08c31368428eed51;hpb=a13370a2d5abfe74b91cedf9512bd644c80737e1;p=gdcm.git diff --git a/vtk/vtkGdcm4DSplitter.cxx b/vtk/vtkGdcm4DSplitter.cxx index 69079de8..e3e27d81 100644 --- a/vtk/vtkGdcm4DSplitter.cxx +++ b/vtk/vtkGdcm4DSplitter.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: vtkGdcm4DSplitter.cxx,v $ Language: C++ - Date: $Date: 2011/04/15 15:14:40 $ - Version: $Revision: 1.11 $ + Date: $Date: 2011/10/25 20:36:21 $ + Version: $Revision: 1.16 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -49,7 +49,7 @@ vtkGdcm4DSplitter class does the job for him PrintFile dirin=Root_Directory_holding_the_images rec > 1.txt -open it with any test editor, and/or (Linux users) +open it with any text editor, and/or (Linux users) grep a_supposed_to_be_string_of_interest 1.txt @@ -112,10 +112,10 @@ Aware user will have to specify some points : - Choose 'flip' option ---------------------- -VTK reader 'flop upside down' the images to display them correctly ?!? +VTK reader 'flips upside down' the images to display them correctly ?!? When a 'math guy' gets the image, the first thing he does is flipping it again. Avoid loosing CPU time using : - bool SetFlipY(bool); // Wrong = no flip + void SetFlipY(bool); // Wrong = no flip . Execute : ----------- @@ -143,12 +143,12 @@ Avoid loosing CPU time using : */ vtkGdcm4DSplitter::vtkGdcm4DSplitter() : SplitOnPosition(false), SplitOnOrientation(false), SplitOnTag(false), - SplitGroup(0), SplitElem(0), SplitConvertToFloat(false), + SplitGroup(0), SplitElem(0), SplitConvertToFloat(false), SplitOnly(false), - SortOnPosition(false), SortOnOrientation(false), SortOnTag(false), SortOnFileName(false), SortOnUserFunction(false), - SortGroup(0), SortElem(0), SortConvertToFloat(false), + SortOnPosition(false), SortOnOrientation(false), /* SortOnTag(false),*/ SortOnFileName(false), SortOnUserFunction(false), + /*SortGroup(0), SortElem(0), SortConvertToFloat(false),*/ - Recursive(false), TypeDir(0), + Recursive(false),FlipY(true), TypeDir(0), verbose(false) { @@ -159,7 +159,26 @@ Avoid loosing CPU time using : */ vtkGdcm4DSplitter::~vtkGdcm4DSplitter() { - /// \TODO : delete everything that must be! + /// \TODO : delete everything that must be! + + GDCM_NAME_SPACE::XCoherentFileSetmap::iterator it; + std::vector::iterator it2; + + for ( it = xcm.begin(); // for each std::vector + it != xcm.end(); + ++it) + { + for ( it2 = (*it).second->begin(); // for each gdcm::File* + it2 != (*it).second->end(); + ++it2) + { + (*it2)->Delete(); // delete gdcm::File + } + delete (*it).second; // delete the now empty std::vector + } + + // VectGdcmFileLists is a vector of pointers on elements of xcm : nothing to do! + } // Locate Data to process @@ -211,7 +230,7 @@ void vtkGdcm4DSplitter::setRecursive(bool recursive) } /** - * \brief Sets a list of files read + * \brief Sets a list of files names to read * @param vectFileName vector of file names to deal with */ bool vtkGdcm4DSplitter::setVectFileName(std::vector &vectFileName) @@ -227,8 +246,8 @@ void vtkGdcm4DSplitter::setRecursive(bool recursive) } /** - * \brief Sets a vector of gdcm::File * - * @param vectGdcmFileName vector of gdcm::File * + * \brief Sets an already filled std::vector of gdcm::File * + * @param vectGdcmFileName std::vector of gdcm::File * */ bool vtkGdcm4DSplitter::setVectGdcmFile(GDCM_NAME_SPACE::FileList *vectGdcmFile) @@ -243,28 +262,30 @@ void vtkGdcm4DSplitter::setRecursive(bool recursive) } - // Split - // ===== + // Split criterion + // =============== + // /** * \brief asks for splitting Filesets according to the Position - */ - + */ void vtkGdcm4DSplitter::setSplitOnPosition() { SplitOnPosition=true; SplitOnOrientation=false; SplitOnTag=false; } + /** * \brief asks for splitting Filesets according to the Orientation - */ + */ void vtkGdcm4DSplitter::setSplitOnOrientation() { SplitOnPosition=false; SplitOnOrientation=true; SplitOnTag=false; } + /** * \brief asks for splitting Filesets according to the value of a given Tag * @param group group number of the target Element @@ -278,6 +299,7 @@ void vtkGdcm4DSplitter::setRecursive(bool recursive) SplitGroup=splitGroup; SplitElem=splitElem; } + /** * \brief asks for converting to 'float' the tag values used as a splitting criteria (lexicographic order may not be suitable) */ @@ -290,29 +312,34 @@ void vtkGdcm4DSplitter::setRecursive(bool recursive) { SplitOnly = s; } - // Sort - // ==== - + + // Sort criterion + // ============== + // + +/** + * \brief asks for IPP sorting each XCoherent gdcm::FILE set + */ void vtkGdcm4DSplitter::setSortOnPosition() { SortOnPosition=true; SortOnOrientation=false; - SortOnTag=false; + //SortOnTag=false; SortOnFileName=false; SortOnUserFunction=false; SortOnPosition=true; } - // use setSortOnUserFunction, instead! - // void setSortOnTag(unsigned short int sortGroup, unsigned short int sortElem) - // { - // SortOnPosition=false; - // SortOnOrientation=false; - // SortOnTag=true; - // SortOnFileName=false; - // SortOnUserFunction=false; - // SortGroup=sortGroup; SortElem=sortElem; - // } + // use setSortOnUserFunction, instead! + // void setSortOnTag(unsigned short int sortGroup, unsigned short int sortElem) + // { + // SortOnPosition=false; + // SortOnOrientation=false; + // SortOnTag=true; + // SortOnFileName=false; + // SortOnUserFunction=false; + // SortGroup=sortGroup; SortElem=sortElem; + // } /** @@ -324,11 +351,10 @@ void vtkGdcm4DSplitter::setRecursive(bool recursive) UserCompareFunction=f; SortOnPosition=false; SortOnOrientation=false; - SortOnTag=false; + //SortOnTag=false; SortOnFileName=false; SortOnUserFunction=true; - } - + } // void setSortConvertToFloat(bool conv) // { @@ -336,30 +362,24 @@ void vtkGdcm4DSplitter::setRecursive(bool recursive) // } /** - * \brief asks for sorting the images, according to their File Name + * \brief asks for sorting each XCoherent gdcm::FILE set, according to the File names */ void vtkGdcm4DSplitter::setSortOnFileName() { SortOnPosition=false; SortOnOrientation=false; - SortOnTag=false; + //SortOnTag=false; SortOnFileName=true; SortOnUserFunction=false; } - - std::vector * vtkGdcm4DSplitter::GetImageDataVector() - { - if (SplitOnly) - return NULL; - - return ImageDataVector; - } - +/** + * \brief returns a std::vector of gdcm::FileList* (gdcm::FileList : actually, a std::vector of gdcm::File*) + */ std::vector *vtkGdcm4DSplitter::GetVectGdcmFileLists() { if (SplitOnly) - return NULL; + return &VectGdcmFileLists; GDCM_NAME_SPACE::XCoherentFileSetmap::iterator it; for ( it = xcm.begin(); @@ -370,42 +390,42 @@ void vtkGdcm4DSplitter::setRecursive(bool recursive) } return &VectGdcmFileLists; } - +/** + * \brief returns a std::vector of [2D/3D, depending on what was passed] vtkImageData* + */ + std::vector * vtkGdcm4DSplitter::GetImageDataVector() + { + if (SplitOnly) + return NULL; + return ImageDataVector; + } + +/** + * \brief when user _knows_ only _one_ vtkImageData* is returned he may be interested in not getting a vector... + */ vtkImageData *vtkGdcm4DSplitter::GetImageData() { if (SplitOnly) return NULL; - return (*ImageDataVector)[0]; + return (*ImageDataVector)[0]; } - bool vtkGdcm4DSplitter::CompareOnSortTagConvertToFloat(GDCM_NAME_SPACE::File *file1, GDCM_NAME_SPACE::File *file2) - { - /* if (verbose) printf ("%04x %04x\n", this->SortGroup,this->SortElem); - if (verbose) std :: cout << file1->GetEntryString(SortGroup,SortElem).c_str() << " : " - << atof(file1->GetEntryString(SortGroup,SortElem).c_str()) - << std::endl; -*/ +// bool vtkGdcm4DSplitter::CompareOnSortTagConvertToFloat(GDCM_NAME_SPACE::File *file1, GDCM_NAME_SPACE::File *file2) +// { // return atof(file1->GetEntryString(vtkGdcm4DSplitter::SortGroup,vtkGdcm4DSplitter::SortElem).c_str()) < atof(file2->GetEntryString(vtkGdcm4DSplitter::SortGroup,vtkGdcm4DSplitter::SortElem).c_str()); - return atof(file1->GetEntryString(SortGroup,SortElem).c_str()) < atof(file2->GetEntryString(SortGroup,SortElem).c_str()); - } +// return atof(file1->GetEntryString(SortGroup,SortElem).c_str()) < atof(file2->GetEntryString(SortGroup,SortElem).c_str()); +// } - bool vtkGdcm4DSplitter::CompareOnSortTag(GDCM_NAME_SPACE::File *file1, GDCM_NAME_SPACE::File *file2) - { - return file1->GetEntryString(vtkGdcm4DSplitter::SortGroup,vtkGdcm4DSplitter::SortElem) < file2->GetEntryString(vtkGdcm4DSplitter::SortGroup,vtkGdcm4DSplitter::SortElem); - } +// bool vtkGdcm4DSplitter::CompareOnSortTag(GDCM_NAME_SPACE::File *file1, GDCM_NAME_SPACE::File *file2) +// { +// return file1->GetEntryString(vtkGdcm4DSplitter::SortGroup,vtkGdcm4DSplitter::SortElem) < file2->GetEntryString(vtkGdcm4DSplitter::SortGroup,vtkGdcm4DSplitter::SortElem); +// } bool vtkGdcm4DSplitter::Go() { - if (!SplitOnPosition && !SplitOnOrientation && !SplitOnTag) - { - ///\TODO (?) Throw an exception "Choose Splitting mode before!" - std::cout << "Choose Splitting mode before!" << std::endl; - return false; - } - /// How To : /* entree nom de directory / Vecteur de noms? @@ -417,80 +437,26 @@ void vtkGdcm4DSplitter::setRecursive(bool recursive) trier chaque Coherent file set passer chacun a un vtkGcdmReader retourner le (vecteur de) vtkImageData - */ - - GDCM_NAME_SPACE::SerieHelper *s; - s = GDCM_NAME_SPACE::SerieHelper::New(); + */ - GDCM_NAME_SPACE::File *f; - GDCM_NAME_SPACE::DirListType fileNames; - - // - // Fill fileNames with the user supplied file names (in any) - // -------------------------------------- - // - if (TypeDir == 0 ) - { - ///\TODO (?) Throw an exception "Set input Directory name(s) / file names before!" - std::cout << "Set input Directory name(s) / file names before!" << std::endl; - return false; - } - else if (TypeDir == 1) - { - GDCM_NAME_SPACE::DirList dirlist(DirName, Recursive); // NO recursive exploration - fileNames = dirlist.GetFilenames(); // all the file names - } - - else if (TypeDir == 2) + if (!SplitOnPosition && !SplitOnOrientation && !SplitOnTag) { - int nbDir = VectDirName.size(); - GDCM_NAME_SPACE::DirListType tmpFileNames; - for (int iDir=0; iDirSetMaxSizeLoadEntry(maxSize); - f->SetFileName( *it ); - if (f->Load()) - l->push_back(f); - else - std::cout << " Fail to load [" << *it << "]" << std::endl; - } - } + GDCM_NAME_SPACE::FileList *l; + GDCM_NAME_SPACE::SerieHelper *s; + s = GDCM_NAME_SPACE::SerieHelper::New(); +// +// Load the gdcm::File* set, according to user's requierements +// ------------------------------------------------------------ +// + l = getGdcmFileList(); +std::cout << l->size() << " gdcm::File read" << std::endl; // // Split the gdcm::File* set, according to user's requierements // ------------------------------------------------------------ @@ -521,39 +487,43 @@ void vtkGdcm4DSplitter::setRecursive(bool recursive) xcm = s->SplitOnTagValue(l, SplitGroup, SplitElem); else { - xcm = s->SplitOnTagValueConvertToFloat(l, SplitGroup, SplitElem); + xcm = s->SplitOnTagValueConvertToFloat(l, SplitGroup, SplitElem); } - } - + } + if (xcm.size() == 0) { - if(verbose) std::cout << "Empty XCoherent File Set after 'split' ?!?" << std::endl; + if(verbose) + std::cout << "Empty XCoherent File Set after 'split' ?!?" << std::endl; return false; } -/* - else if (xcm.size() == 1) - TypeResult=1; else - TypeResult=2; -*/ + { + std::cout << xcm.size() << " XCoherent entries found" << std::endl; + } + + // put here, to avoid segfault when unaware user sets SplitOnly to true, and the asks for ImageDataVector + ImageDataVector = new std::vector; + std::cout <<"SplitOnly " << SplitOnly << std::endl; if(SplitOnly) return true; // // // ------------------------------------------------------------ // - ImageDataVector = new std::vector; +// ImageDataVector = new std::vector; + /// \TODO move inside the loop, or be clever using vtk! - // vtkGdcmReader *reader = vtkGdcmReader::New(); // move inside the loop, or be clever using vtk! - - // XCoherentFileSetmap map < critère de split, FileList (= std::vector de gdcm::File*) > + vtkGdcmReader *reader = vtkGdcmReader::New(); // move inside the loop, or be clever using vtk! + + // XCoherentFileSetmap map < critère de split, FileList (= std::vector de gdcm::File*) > for (GDCM_NAME_SPACE::XCoherentFileSetmap::iterator i = xcm.begin(); - i != xcm.end(); - ++i) + i != xcm.end(); + ++i) { - vtkGdcmReader *reader = vtkGdcmReader::New(); /// \TODO FIXME : unable to delete! + //vtkGdcmReader *reader = vtkGdcmReader::New(); /// \TODO FIXME : unable to delete! reader->SetFlipY(FlipY); // better user SetFileLowerLeft() @@ -592,30 +562,30 @@ void vtkGdcm4DSplitter::setRecursive(bool recursive) if (verbose) std::cout << "Out of SortOnFileName" << std::endl; } - else if (SortOnTag) - { - if (verbose) std::cout << "SortOnTag" << std::endl; - printf ("--> %04x %04x\n", SortGroup,SortElem); - std::cout << "Sorry, troubles not solved yet; use SortOnUserFunction, right now!" << std::endl; +// else if (SortOnTag) +// { +// if (verbose) std::cout << "SortOnTag" << std::endl; +// printf ("--> %04x %04x\n", SortGroup,SortElem); +// std::cout << "Sorry, troubles not solved yet; use SortOnUserFunction, right now!" << std::endl; /* ==> WARNING : This one has troubles; do NOT use it, right now! // a pointer to fonction cannot be casted as a pointer to member function! // Use SortOnUserFunction, instead! // if ( SortConvertToFloat ) - // s->SetUserLessThanFunction( reinterpret_cast - ( &vtkGdcm4DSplitter::CompareOnSortTagConvertToFloat)); + // s->SetUserLessThanFunction( reinterpret_cast + // ( &vtkGdcm4DSplitter::CompareOnSortTagConvertToFloat)); // else // s->SetUserLessThanFunction( reinterpret_cast - ( &vtkGdcm4DSplitter::CompareOnSortTag)); + ( &vtkGdcm4DSplitter::CompareOnSortTag)); // Anything like this, in GDCM2? // s->UserOrdering((*i).second); */ - //if (verbose) std::cout << "Out of SortOnTag" << std::endl; - std::cout << "NO ordering performed :-( " << std::endl; - } +// //if (verbose) std::cout << "Out of SortOnTag" << std::endl; +// std::cout << "NO ordering performed :-( " << std::endl; +// } else if (SortOnUserFunction) { @@ -631,30 +601,112 @@ void vtkGdcm4DSplitter::setRecursive(bool recursive) /// \TODO : remove the following if (verbose) { - std::cout << "reader->GetOutput() :" << std::endl; - reader->GetOutput()->PrintSelf(std::cout, vtkIndent(2)); + // std::cout << "reader->GetOutput() :" << std::endl; + // reader->GetOutput()->PrintSelf(std::cout, vtkIndent(2)); } ImageDataVector->push_back(reader->GetOutput() ); std::vector::iterator it; - if (verbose) + //if (verbose) // JPR + for(it=ImageDataVector->begin(); it!=ImageDataVector->end(); ++it) { - std::cout << "-in vtkGdcm4DSplitter--------------------------" << std::endl; - (*it)->PrintSelf(std::cout, vtkIndent(2)); + // std::cout << "-in vtkGdcm4DSplitter --------------------------" << std::endl; + // (*it)->PrintSelf(std::cout, vtkIndent(2)); } - std::cout << std::endl; + //std::cout << std::endl; } - //reader->Delete(); // \TODO : fix + reader->Delete(); // \TODO : fix s->Delete(); - f->Delete(); + // f->Delete(); delete l; return true; } +/** + * \brief Load the gdcm::File* set, according to user's requierements + * returns a std::vector of gdcm::File* (gdcm::FileList : actually, a std::vector of gdcm::File*) + */ + + +GDCM_NAME_SPACE::FileList *vtkGdcm4DSplitter::getGdcmFileList() +{ + + GDCM_NAME_SPACE::File *f; + GDCM_NAME_SPACE::DirListType fileNames; + + // + // Fill fileNames with the user supplied file names (in any) + // ------------------------------------------------ + // + if (TypeDir == 0 ) // Nothing was set as input... + { + ///\TODO (?) Throw an exception "Set input Directory name(s) / file names before!" + std::cout << "Set input Directory name(s) / file names before!" << std::endl; + return false; + } + else if (TypeDir == 1) // A root directory name was set as input + { + GDCM_NAME_SPACE::DirList dirlist(DirName, Recursive); // NO recursive exploration + fileNames = dirlist.GetFilenames(); // all the file names + } + + else if (TypeDir == 2) // a std::vector of directory names was set as input + { + int nbDir = VectDirName.size(); + GDCM_NAME_SPACE::DirListType tmpFileNames; + for (int iDir=0; iDirSetMaxSizeLoadEntry(maxSize); + f->SetFileName( *it ); + if (f->Load()) + l->push_back(f); + else + std::cout << " Fail to load [" << *it << "]" << std::endl; + } + } + return l; +} + + + void vtkGdcm4DSplitter::reorgXCoherentFileSetmap(GDCM_NAME_SPACE::XCoherentFileSetmap &xcm) { /* @@ -670,7 +722,7 @@ void vtkGdcm4DSplitter::setRecursive(bool recursive) ELEM e; std::vector vectElem; -/* remenber : +/* just to remember : typedef struct { std::string strIPP; @@ -730,6 +782,7 @@ void vtkGdcm4DSplitter::setRecursive(bool recursive) bool vtkGdcm4DSplitter::sortVectElem(std::vector *fileList) { //based on Jolinda Smith's algorithm +// // NOTE : if you need to use Jolinda Smith's algorithm, get the one inside gdcm::SerieHelper // this one is a light version. @@ -737,12 +790,12 @@ bool vtkGdcm4DSplitter::sortVectElem(std::vector *fileList) //to right, "y" is posterior to anterior, and "z" is foot to head (RAH). //iop is calculated based on the file file - float cosines[6]; + float cosines[6]; double normal[3]; double ipp[3]; double dist; double min = 0, max = 0; - bool first = true; + bool first = true; //double ZSpacing; // useless here! // JPR bool DirectOrder = true; // remove it!