creaImageIOOutputModel.cpp

Go to the documentation of this file.
00001 #include <creaImageIOOutputModel.h>
00002 
00003 #include <boost/filesystem.hpp>
00004 #include "boost/algorithm/string.hpp"
00005 
00006 #if defined(USE_GDCM)
00007 #include <gdcmGlobal.h>
00008 #include <gdcmFile.h>
00009 #include <gdcmSerieHelper.h>
00010 #include <gdcmFile.h>
00011 #endif
00012 
00013 #if defined(USE_GDCM2)
00014 #include <gdcmDict.h>
00015 #include <gdcmDicts.h>
00016 #include <gdcmGlobal.h>
00017 #endif
00018 
00019 #if defined(USE_XERCES)
00020 #include <xercesc/dom/DOM.hpp>
00021 #include <xercesc/dom/DOMDocument.hpp>
00022 #include <xercesc/util/XMLString.hpp>
00023 #include <xercesc/util/PlatformUtils.hpp>
00024 using namespace xercesc;
00025 #endif
00026 
00027 
00028 namespace creaImageIO
00029 {
00030 
00031         OutputModel::~OutputModel()
00032         {
00033         }
00034 
00035 #if defined(USE_GDCM)
00036          double OutputModel::orderFilesWithZspacing(std::vector<std::string> &im)
00037         {
00038                 double spacing=1;
00039                 typedef std::vector<GDCM_NAME_SPACE::File* > FileList;
00040                 FileList fileVector;
00041                 //GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
00042                 GDCM_NAME_SPACE::SerieHelper *sh = GDCM_NAME_SPACE::SerieHelper::New();
00043                 std::vector<std::string> lstAux;
00044                 std::vector<std::string>::iterator it;
00045                 for (it=im.begin(); it!=im.end(); ++it)
00046                 {
00048                         GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
00049                         f->SetFileName(*it);
00050                         f->Load();
00051                         if (f->IsReadable())
00052                         {
00053                                 fileVector.push_back(f);
00054                         } else {
00055                                 lstAux.push_back(*it);
00056                         }
00057                 } // for
00058 
00059                 if ((fileVector.size()>1) && (sh->IsCoherent( &fileVector )))
00060                 {
00061                                 sh->OrderFileList(&fileVector);
00062                                 spacing= sh->GetZSpacing();
00063                                 im.clear();
00064                                 int i;
00065                                 for (i=0; i<fileVector.size(); i++)
00066                                 {
00067                                         im.push_back( (fileVector[i])->GetFileName() );
00068                                 }
00069                                 for (i=0; i<lstAux.size(); i++)
00070                                 {
00071                                         im.push_back( lstAux[i] );
00072                                 }
00073                 }else {
00074                         std::sort( im.begin(), im.end() );
00075                 }
00076                 
00077            return spacing;
00078         }
00079 
00080 #endif
00081 
00082 #if defined(USE_GDCM2)
00083         // TO DO
00084         double OutputModel::orderFilesWithZspacing(std::vector<std::string> &im)
00085         {
00086                 return 1;
00087         }
00088 #endif
00089 
00090 #if defined(USE_XERCES)
00091 
00092         OutputModel::OutputModel(OutputModelParser *i_outparser) : m_outparser(i_outparser)
00093         {
00094                 // Init
00095                 b_db = false;
00096         }
00097 
00098         //OutputModel::setOutputModel(std::vector< std::map<std::string, std::string>> i_outputs)
00099         //{
00100         //      std::vector< std::map<std::string, std::string>>::iterator it = i_outputs.begin();
00101         //      for(; it != i_outputs.end(); it++)
00102         //      {
00103         //              checkModel((*it));
00104         //      }
00105         //}
00106 
00107          bool OutputModel::checkModel(std::map<std::string, std::string> i_model, const std::string i_val)
00108         {
00109                 bool bres = false;
00110                 if( i_model.find(i_val) != i_model.end() )
00111                 {
00112                         bres = true;
00113                 }
00114 
00115                 return bres;
00116         }
00117 
00118         OutputModel::~OutputModel()
00119         {
00120         }
00121         void OutputModel::setDB(const std::string i_db, const std::string i_table)
00122         {
00123                 b_db = true;
00124                 m_db = i_db;
00125                 m_table = i_table;
00126         }
00127 
00128         const std::string OutputModel::getTag()
00129         {
00130                 char key[12];
00131                 if (!m_tag.empty())
00132                 {
00133                         return m_tag;
00134                 }
00135                 else
00136                 {
00137 #if defined(USE_GDCM)
00138                 sprintf(key,"D%04x_%04x", m_tag1.GetGroup(), m_tag1.GetElement());
00139 #endif
00140 #if defined(USE_GDCM2)
00141                 sprintf(key,"D%04x_%04x", m_tag2.GetGroup(), m_tag2.GetElement());
00142 #endif
00143                 return key;
00144                 }
00145         }
00146 /*       void OutputModel::sort(const std::vector<std::string> i_filenames, std::vector<std::string> &o_sort)
00147         {
00148                 std::vector<int> values;
00149                 if(bDicom)
00150                 {
00151                         if(b_db)
00152                         {
00153                                 getDBValues<int>(i_filenames, values);
00154                         }
00155                         else
00156                         {
00157                                 getValues<int>(i_filenames, values);
00158                         }
00159                 }
00160                 else
00161                 {
00162                         for(int i = 0; i <i_filenames.size(); i++)
00163                                 values.push_back(i);
00164                 }
00165 
00166                 for(int index = tag_begin; index <= tag_end; index += tag_step)
00167                 {
00168                         std::vector<int>::iterator it_val = values.begin();
00169                         std::vector<std::string>::const_iterator it = i_filenames.begin();
00170                         for(;it != i_filenames.end(); it_val++, it++)
00171                         {
00172                                 if((*it_val) == index)
00173                                 {
00174                                         o_sort.push_back((*it));
00175                                         break;
00176                                 }
00177                         }
00178                 }
00179                 if(m_model != NULL)
00180                         m_model->sort(i_filenames, o_sort);
00181         }*/
00182         template<typename T>
00183         void OutputModel::getDBValues(const std::vector<std::string> i_filenames, const std::string i_stag, std::map<std::string , T> &o_val)
00184         {
00185         }
00186 
00187 
00188          //template<typename T>
00189          //T OutputModel::getTypeTag()
00190          //{
00191                 // GDCM_NAME_SPACE::DictEntry* entry =  GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->GetEntry(GetGroup(),GetElement());
00192                 // entry->GetVR().GetVRType( entry->GetVR());
00193                 // 
00194          //}
00195 
00196         template<typename T>
00197         void OutputModel::getValues(const std::vector<std::string> i_filenames,const std::string i_tag, std::map< std::string, T> &o_val)
00198          {
00199 #if defined(USE_GDCM)
00200                   getReadValues(i_filenames, o_val);
00201 #endif
00202 #if defined(USE_GDCM2)
00203                   getScanValues(i_filenames, i_tag,o_val);
00204 #endif
00205          }
00206 
00207 #if defined(USE_GDCM2)
00208         // TO DO if some file don't provide this value, we are lost so return a map!
00209         template<typename T>
00210         void OutputModel::getScanValues(const std::vector<std :: string> i_filenames, const std::string i_stag, std::map<std::string,T> &o_val)
00211         {
00212                 uint16_t gr, el;
00213                 sscanf(i_stag.c_str(),"D%04hx_%04hx ",&gr,&el); 
00214                 gdcm::Tag tag(gr, el);
00215                 if (!tag.IsIllegal())
00216                 {
00217                         std::vector<std :: string> names(i_filenames);
00218                         gdcm::Scanner scan;
00219                         scan.ClearTags();
00220                         scan.AddTag(tag);
00221                         
00222                         std::vector<std :: string>::iterator it = names.begin();
00223                         for(;it != names.end(); it++)
00224                                 boost::algorithm::replace_all((*it),"\\", "/");
00225                         scan.Scan(i_filenames);
00226 //                      const gdcm::Scanner::TagToValue &mapping = 
00227                         std::vector<std::string>::const_iterator it_file = i_filenames.begin();
00228                         for(; it_file != i_filenames.end(); it++)
00229                         {
00230                                 if(     scan.GetMapping((*it_file).c_str()).begin() != scan.GetMapping((*it_file).c_str()).end())
00231                                 {
00232                                         o_val[(*it)] = scan.GetMapping((*it_file).c_str()).begin()->second;
00233                                 }
00234                                 else
00235                                 {
00236                                         o_val[(*it)] = "";
00237                                 }
00238                         }
00239                 } 
00240         }
00241 
00242         //const gdcm::VR::VRType OutputModel::getType(const std::string i_tag)
00243         //{
00244         //      uint16_t gr, el;
00245         //      sscanf(itag.c_str(),"D%04hx_%04hx ",&gr,&el); 
00246         //      const gdcm::Global& g = gdcm::Global::GetInstance(); // sum of all knowledge !
00247         //      const gdcm::Dicts &dicts = g.GetDicts();
00248         //      const gdcm::Dict &dict = dicts.GetPublicDict(); // Part 6
00249         //    gdcm::DictEntry dictentry =  dict.GetDictEntry(gdcm::Tag(gr, el));
00250         //   return dictentry.GetVR();
00251         //}
00252 #endif
00253 
00254 
00255 #if defined(USE_GDCM)
00256         double OutputModel::orderFiles(std::vector<std::string> im, std::vector<std::string> &out)
00257 {
00258         double spacing = 1;
00259         std::vector<boost::shared_ptr<GDCM_NAME_SPACE::File> > fileVector;
00260         std::vector<std::string> lstAux;
00261         GDCM_NAME_SPACE::SerieHelper *sh = GDCM_NAME_SPACE::SerieHelper::New();
00262         std::vector<std::string>::iterator it = im.begin();
00263         for (; it!=im.end(); ++it)
00264         {
00265                 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
00266                 f->SetFileName(*it);
00267                 f->Load();
00268                 if (f->IsReadable())
00269                 {
00270                         fileVector.push_back(f);
00271                 } 
00272                 else
00273                 {
00274                         lstAux.push_back((*it));
00275                 }
00276         } 
00277         if ((fileVector.size()>1) && (sh->IsCoherent( &fileVector )))
00278         {
00279                         sh->OrderFileList(&fileVector);
00280                         spacing= sh->GetZSpacing();
00281                         out.clear();
00282                         int i;
00283                         for (i=0; i<fileVector.size(); i++)
00284                         {
00285                                 out.push_back( (fileVector[i])->GetFileName() );
00286                         }
00287                         for (i=0; i<lstAux.size(); i++)
00288                         {
00289                                 out.push_back( lstAux[i] );
00290                         }
00291         }
00292         else
00293         {
00294                 std::sort( im.begin(), im.end() );
00295         }
00296      return spacing;
00297         }
00298 #endif 
00299 
00300 
00301         void OutputModel::sort(const std::vector<std::string> i_filenames, std::vector<std::string> &o_sort, int level)
00302         {
00303 #if defined(USE_XERCES)
00304                 int tags[3];
00305                         tags[0] = 0;
00306 
00307                 int tag_end = i_filenames.size();
00308                 int tag_step = 1;
00309                 std::map<std::string, std::string> model = m_outparser->getLevel(level);
00310                 std::map<std::string, std::string> values;
00311                 
00312                 if( checkModel(model, OUTPUTMODEL_TAG(3)) && !b_db)
00313                 {
00314 #if defined (USE_GDCM2)
00315         
00316                         //OutputSort< getType(model[OUTPUTMODEL_TAG(3)] > osort;
00317                         
00318                         getValues<std::string>(i_filenames, model[OUTPUTMODEL_TAG(3)], values);
00319 #endif
00320                 }
00321                 else
00322                 {
00323                         OutputSort<std::string> osort;
00324                         for(int i = 0; i < 3; i++)
00325                         {
00326                                 if ( checkModel(model, OUTPUTMODEL_TAG(i)))
00327                                 {       
00328                                         osort.setTag(model[OUTPUTMODEL_TAG(i)],OUTPUTMODEL_TAG(i));
00329                                 }
00330                         }
00331 
00332                         if(b_db)
00333                         {
00334                                 getDBValues<std::string>(i_filenames,model[OUTPUTMODEL_TAG(3)], values);
00335                         }
00336                         else
00337                         {
00338                                 for(int i = 0; i <i_filenames.size(); i++)
00339                                         values[i_filenames[i]] = i;
00340                                 osort.sort(values,o_sort);
00341                         }
00342                 }
00343 
00344 
00345 
00346 
00347                 for(int i = 1; i < 4; i++)
00348                 {
00349                         if ( checkModel(model, OUTPUTMODEL_TAG(i)))
00350                         {       
00351                                 sscanf(model[OUTPUTMODEL_TAG(i)].c_str(), "%d", tags[i]);
00352                         }
00353                 }
00354                 
00355                 if( checkModel(model, OUTPUTMODEL_TAG(3)) )
00356                 {
00357                         if(b_db)
00358                         {
00359                                 getDBValues<std::string>(i_filenames,model[OUTPUTMODEL_TAG(3)], values);
00360                         }
00361                         else
00362                         {
00363                                 getValues<std::string>(i_filenames, model[OUTPUTMODEL_TAG(3)], values);
00364                         }
00365                 }
00366                 else
00367                 {
00368                         for(int i = 0; i <i_filenames.size(); i++)
00369                                 values[i_filenames[i]] = i;
00370                 }
00371 
00372                 for(int index = tag_begin; index <= tag_end; index += tag_step)
00373                 {
00374                         std::map<std::string, std::string>::iterator it_val = values.begin();
00375                         for(;it_val != values.end(); it_val++)
00376                         {
00377                                 if(it_val->second.c_str() )// == index)
00378                                 {
00379                                         o_sort.push_back(it_val->first.c_str());
00380                                         break;
00381                                 }
00382                         }
00383                 }
00384                 if(checkModel(model, OUTPUTMODEL_TAG(4)))
00385                 {
00386                         int lv;
00387                         sscanf(model[OUTPUTMODEL_TAG(4)].c_str(), "%d", lv);
00388                         sort(i_filenames,o_sort,lv);
00389                 }
00390 #endif
00391         }
00392 #endif
00393 
00394 }