1 #include <creaImageIOOutputModel.h>
3 #include <boost/filesystem.hpp>
4 #include "boost/algorithm/string.hpp"
7 #include <gdcmGlobal.h>
8 #include <gdcmSerieHelper.h>
11 #if defined(USE_GDCM2)
13 #include <gdcmDicts.h>
14 #include <gdcmGlobal.h>
18 #if defined(USE_XERCES)
19 #include <xercesc/dom/DOM.hpp>
20 #include <xercesc/dom/DOMDocument.hpp>
21 #include <xercesc/util/XMLString.hpp>
22 #include <xercesc/util/PlatformUtils.hpp>
23 using namespace xercesc;
31 OutputModel::~OutputModel()
36 double OutputModel::orderFilesWithZspacing(std::vector<std::string> &im)
39 typedef std::vector<GDCM_NAME_SPACE::File* > FileList;
41 //GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
42 GDCM_NAME_SPACE::SerieHelper *sh = GDCM_NAME_SPACE::SerieHelper::New();
43 std::vector<std::string> lstAux;
44 std::vector<std::string>::iterator it;
45 for (it=im.begin(); it!=im.end(); ++it)
47 ///\TODO liberer les GDCM_NAME_SPACE::File a la fin! // JPR
48 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
53 fileVector.push_back(f);
55 lstAux.push_back(*it);
60 if ((fileVector.size()>1) && (sh->IsCoherent( &fileVector )))
62 sh->OrderFileList(&fileVector);
63 spacing= sh->GetZSpacing();
66 for (i=0; i<fileVector.size(); i++)
68 im.push_back( (fileVector[i])->GetFileName() );
70 for (i=0; i<lstAux.size(); i++)
72 im.push_back( lstAux[i] );
75 std::sort( im.begin(), im.end() );
83 #if defined(USE_GDCM2)
85 double OutputModel::orderFilesWithZspacing(std::vector<std::string> &im)
93 #if defined(USE_XERCES)
95 OutputModel::OutputModel(OutputModelParser *i_outparser) : m_outparser(i_outparser)
101 //OutputModel::setOutputModel(std::vector< std::map<std::string, std::string>> i_outputs)
103 // std::vector< std::map<std::string, std::string>>::iterator it = i_outputs.begin();
104 // for(; it != i_outputs.end(); it++)
106 // checkModel((*it));
110 bool OutputModel::checkModel(std::map<std::string, std::string> i_model, const std::string i_val)
113 if( i_model.find(i_val) != i_model.end() )
122 OutputModel::~OutputModel()
125 void OutputModel::setDB(const std::string i_db, const std::string i_table)
132 const std::string OutputModel::getTag()
141 #if defined(USE_GDCM)
142 sprintf(key,"D%04x_%04x", m_tag1.GetGroup(), m_tag1.GetElement());
144 #if defined(USE_GDCM2)
145 sprintf(key,"D%04x_%04x", m_tag2.GetGroup(), m_tag2.GetElement());
150 /* void OutputModel::sort(const std::vector<std::string> i_filenames, std::vector<std::string> &o_sort)
152 std::vector<int> values;
157 getDBValues<int>(i_filenames, values);
161 getValues<int>(i_filenames, values);
166 for(int i = 0; i <i_filenames.size(); i++)
170 for(int index = tag_begin; index <= tag_end; index += tag_step)
172 std::vector<int>::iterator it_val = values.begin();
173 std::vector<std::string>::const_iterator it = i_filenames.begin();
174 for(;it != i_filenames.end(); it_val++, it++)
176 if((*it_val) == index)
178 o_sort.push_back((*it));
184 m_model->sort(i_filenames, o_sort);
187 void OutputModel::getDBValues(const std::vector<std::string> i_filenames, const std::string i_stag, std::map<std::string , T> &o_val)
192 //template<typename T>
193 //T OutputModel::getTypeTag()
195 // GDCM_NAME_SPACE::DictEntry* entry = GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->GetEntry(GetGroup(),GetElement());
196 // entry->GetVR().GetVRType( entry->GetVR());
201 void OutputModel::getValues(const std::vector<std::string> i_filenames,const std::string i_tag, std::map< std::string, T> &o_val)
203 #if defined(USE_GDCM)
204 getReadValues(i_filenames, o_val);
206 #if defined(USE_GDCM2)
207 getScanValues(i_filenames, i_tag,o_val);
211 #if defined(USE_GDCM2)
212 // TO DO if some file don't provide this value, we are lost so return a map!
214 void OutputModel::getScanValues(const std::vector<std :: string> i_filenames, const std::string i_stag, std::map<std::string,T> &o_val)
217 sscanf(i_stag.c_str(),"D%04hx_%04hx ",&gr,&el);
218 gdcm::Tag tag(gr, el);
219 if (!tag.IsIllegal())
221 std::vector<std :: string> names(i_filenames);
226 std::vector<std :: string>::iterator it = names.begin();
227 for(;it != names.end(); it++)
228 boost::algorithm::replace_all((*it),"\\", "/");
229 scan.Scan(i_filenames);
230 // const gdcm::Scanner::TagToValue &mapping =
231 std::vector<std::string>::const_iterator it_file = i_filenames.begin();
232 for(; it_file != i_filenames.end(); it++)
234 if( scan.GetMapping((*it_file).c_str()).begin() != scan.GetMapping((*it_file).c_str()).end())
236 o_val[(*it)] = scan.GetMapping((*it_file).c_str()).begin()->second;
247 //const gdcm::VR::VRType OutputModel::getType(const std::string i_tag)
250 // sscanf(itag.c_str(),"D%04hx_%04hx ",&gr,&el);
251 // const gdcm::Global& g = gdcm::Global::GetInstance(); // sum of all knowledge !
252 // const gdcm::Dicts &dicts = g.GetDicts();
253 // const gdcm::Dict &dict = dicts.GetPublicDict(); // Part 6
254 // gdcm::DictEntry dictentry = dict.GetDictEntry(gdcm::Tag(gr, el));
255 // return dictentry.GetVR();
261 #if defined(USE_GDCM)
262 double OutputModel::orderFiles(std::vector<std::string> im, std::vector<std::string> &out)
265 std::vector<boost::shared_ptr<GDCM_NAME_SPACE::File> > fileVector;
266 std::vector<std::string> lstAux;
267 GDCM_NAME_SPACE::SerieHelper *sh = GDCM_NAME_SPACE::SerieHelper::New();
268 std::vector<std::string>::iterator it = im.begin();
269 for (; it!=im.end(); ++it)
271 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
276 fileVector.push_back(f);
280 lstAux.push_back((*it));
283 if ((fileVector.size()>1) && (sh->IsCoherent( &fileVector )))
285 sh->OrderFileList(&fileVector);
286 spacing= sh->GetZSpacing();
289 for (i=0; i<fileVector.size(); i++)
291 out.push_back( (fileVector[i])->GetFileName() );
293 for (i=0; i<lstAux.size(); i++)
295 out.push_back( lstAux[i] );
300 std::sort( im.begin(), im.end() );
307 void OutputModel::sort(const std::vector<std::string> i_filenames, std::vector<std::string> &o_sort, int level)
309 #if defined(USE_XERCES)
313 int tag_end = i_filenames.size();
315 std::map<std::string, std::string> model = m_outparser->getLevel(level);
316 std::map<std::string, std::string> values;
318 if( checkModel(model, OUTPUTMODEL_TAG(3)) && !b_db)
320 #if defined (USE_GDCM2)
322 //OutputSort< getType(model[OUTPUTMODEL_TAG(3)] > osort;
324 getValues<std::string>(i_filenames, model[OUTPUTMODEL_TAG(3)], values);
329 OutputSort<std::string> osort;
330 for(int i = 0; i < 3; i++)
332 if ( checkModel(model, OUTPUTMODEL_TAG(i)))
334 osort.setTag(model[OUTPUTMODEL_TAG(i)],OUTPUTMODEL_TAG(i));
340 getDBValues<std::string>(i_filenames,model[OUTPUTMODEL_TAG(3)], values);
344 for(int i = 0; i <i_filenames.size(); i++)
345 values[i_filenames[i]] = i;
346 osort.sort(values,o_sort);
353 for(int i = 1; i < 4; i++)
355 if ( checkModel(model, OUTPUTMODEL_TAG(i)))
357 sscanf(model[OUTPUTMODEL_TAG(i)].c_str(), "%d", tags[i]);
361 if( checkModel(model, OUTPUTMODEL_TAG(3)) )
365 getDBValues<std::string>(i_filenames,model[OUTPUTMODEL_TAG(3)], values);
369 getValues<std::string>(i_filenames, model[OUTPUTMODEL_TAG(3)], values);
374 for(int i = 0; i <i_filenames.size(); i++)
375 values[i_filenames[i]] = i;
378 for(int index = tag_begin; index <= tag_end; index += tag_step)
380 std::map<std::string, std::string>::iterator it_val = values.begin();
381 for(;it_val != values.end(); it_val++)
383 if(it_val->second.c_str() )// == index)
385 o_sort.push_back(it_val->first.c_str());
390 if(checkModel(model, OUTPUTMODEL_TAG(4)))
393 sscanf(model[OUTPUTMODEL_TAG(4)].c_str(), "%d", lv);
394 sort(i_filenames,o_sort,lv);