1 #include <creaImageIOOutputModel.h>
3 #include <boost/filesystem.hpp>
4 #include "boost/algorithm/string.hpp"
7 #include <gdcmGlobal.h>
8 #include <gdcmSerieHelper.h>
12 #if defined(USE_GDCM2)
14 #include <gdcmDicts.h>
15 #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;
30 OutputModel::~OutputModel()
35 double OutputModel::orderFilesWithZspacing(std::vector<std::string> &im)
38 typedef std::vector<GDCM_NAME_SPACE::File* > FileList;
40 //GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
41 GDCM_NAME_SPACE::SerieHelper *sh = GDCM_NAME_SPACE::SerieHelper::New();
42 std::vector<std::string> lstAux;
43 std::vector<std::string>::iterator it;
44 for (it=im.begin(); it!=im.end(); ++it)
46 ///\TODO liberer les GDCM_NAME_SPACE::File a la fin! // JPR
47 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
52 fileVector.push_back(f);
54 lstAux.push_back(*it);
58 if ((fileVector.size()>1) && (sh->IsCoherent( &fileVector )))
60 sh->OrderFileList(&fileVector);
61 spacing= sh->GetZSpacing();
64 for (i=0; i<fileVector.size(); i++)
66 im.push_back( (fileVector[i])->GetFileName() );
68 for (i=0; i<lstAux.size(); i++)
70 im.push_back( lstAux[i] );
73 std::sort( im.begin(), im.end() );
81 #if defined(USE_GDCM2)
83 double OutputModel::orderFilesWithZspacing(std::vector<std::string> &im)
89 #if defined(USE_XERCES)
91 OutputModel::OutputModel(OutputModelParser *i_outparser) : m_outparser(i_outparser)
97 //OutputModel::setOutputModel(std::vector< std::map<std::string, std::string>> i_outputs)
99 // std::vector< std::map<std::string, std::string>>::iterator it = i_outputs.begin();
100 // for(; it != i_outputs.end(); it++)
102 // checkModel((*it));
106 bool OutputModel::checkModel(std::map<std::string, std::string> i_model, const std::string i_val)
109 if( i_model.find(i_val) != i_model.end() )
117 OutputModel::~OutputModel()
120 void OutputModel::setDB(const std::string i_db, const std::string i_table)
127 const std::string OutputModel::getTag()
136 #if defined(USE_GDCM)
137 sprintf(key,"D%04x_%04x", m_tag1.GetGroup(), m_tag1.GetElement());
139 #if defined(USE_GDCM2)
140 sprintf(key,"D%04x_%04x", m_tag2.GetGroup(), m_tag2.GetElement());
145 /* void OutputModel::sort(const std::vector<std::string> i_filenames, std::vector<std::string> &o_sort)
147 std::vector<int> values;
152 getDBValues<int>(i_filenames, values);
156 getValues<int>(i_filenames, values);
161 for(int i = 0; i <i_filenames.size(); i++)
165 for(int index = tag_begin; index <= tag_end; index += tag_step)
167 std::vector<int>::iterator it_val = values.begin();
168 std::vector<std::string>::const_iterator it = i_filenames.begin();
169 for(;it != i_filenames.end(); it_val++, it++)
171 if((*it_val) == index)
173 o_sort.push_back((*it));
179 m_model->sort(i_filenames, o_sort);
182 void OutputModel::getDBValues(const std::vector<std::string> i_filenames, const std::string i_stag, std::map<std::string , T> &o_val)
187 //template<typename T>
188 //T OutputModel::getTypeTag()
190 // GDCM_NAME_SPACE::DictEntry* entry = GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->GetEntry(GetGroup(),GetElement());
191 // entry->GetVR().GetVRType( entry->GetVR());
196 void OutputModel::getValues(const std::vector<std::string> i_filenames,const std::string i_tag, std::map< std::string, T> &o_val)
198 #if defined(USE_GDCM)
199 getReadValues(i_filenames, o_val);
201 #if defined(USE_GDCM2)
202 getScanValues(i_filenames, i_tag,o_val);
206 #if defined(USE_GDCM2)
207 // TO DO if some file don't provide this value, we are lost so return a map!
209 void OutputModel::getScanValues(const std::vector<std :: string> i_filenames, const std::string i_stag, std::map<std::string,T> &o_val)
212 sscanf(i_stag.c_str(),"D%04hx_%04hx ",&gr,&el);
213 gdcm::Tag tag(gr, el);
214 if (!tag.IsIllegal())
216 std::vector<std :: string> names(i_filenames);
221 std::vector<std :: string>::iterator it = names.begin();
222 for(;it != names.end(); it++)
223 boost::algorithm::replace_all((*it),"\\", "/");
224 scan.Scan(i_filenames);
225 // const gdcm::Scanner::TagToValue &mapping =
226 std::vector<std::string>::const_iterator it_file = i_filenames.begin();
227 for(; it_file != i_filenames.end(); it++)
229 if( scan.GetMapping((*it_file).c_str()).begin() != scan.GetMapping((*it_file).c_str()).end())
231 o_val[(*it)] = scan.GetMapping((*it_file).c_str()).begin()->second;
241 //const gdcm::VR::VRType OutputModel::getType(const std::string i_tag)
244 // sscanf(itag.c_str(),"D%04hx_%04hx ",&gr,&el);
245 // const gdcm::Global& g = gdcm::Global::GetInstance(); // sum of all knowledge !
246 // const gdcm::Dicts &dicts = g.GetDicts();
247 // const gdcm::Dict &dict = dicts.GetPublicDict(); // Part 6
248 // gdcm::DictEntry dictentry = dict.GetDictEntry(gdcm::Tag(gr, el));
249 // return dictentry.GetVR();
254 #if defined(USE_GDCM)
255 double OutputModel::orderFiles(std::vector<std::string> im, std::vector<std::string> &out)
258 std::vector<boost::shared_ptr<GDCM_NAME_SPACE::File> > fileVector;
259 std::vector<std::string> lstAux;
260 GDCM_NAME_SPACE::SerieHelper *sh = GDCM_NAME_SPACE::SerieHelper::New();
261 std::vector<std::string>::iterator it = im.begin();
262 for (; it!=im.end(); ++it)
264 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
269 fileVector.push_back(f);
273 lstAux.push_back((*it));
276 if ((fileVector.size()>1) && (sh->IsCoherent( &fileVector )))
278 sh->OrderFileList(&fileVector);
279 spacing= sh->GetZSpacing();
282 for (i=0; i<fileVector.size(); i++)
284 out.push_back( (fileVector[i])->GetFileName() );
286 for (i=0; i<lstAux.size(); i++)
288 out.push_back( lstAux[i] );
293 std::sort( im.begin(), im.end() );
300 void OutputModel::sort(const std::vector<std::string> i_filenames, std::vector<std::string> &o_sort, int level)
302 #if defined(USE_XERCES)
306 int tag_end = i_filenames.size();
308 std::map<std::string, std::string> model = m_outparser->getLevel(level);
309 std::map<std::string, std::string> values;
311 if( checkModel(model, OUTPUTMODEL_TAG(3)) && !b_db)
313 #if defined (USE_GDCM2)
315 //OutputSort< getType(model[OUTPUTMODEL_TAG(3)] > osort;
317 getValues<std::string>(i_filenames, model[OUTPUTMODEL_TAG(3)], values);
322 OutputSort<std::string> osort;
323 for(int i = 0; i < 3; i++)
325 if ( checkModel(model, OUTPUTMODEL_TAG(i)))
327 osort.setTag(model[OUTPUTMODEL_TAG(i)],OUTPUTMODEL_TAG(i));
333 getDBValues<std::string>(i_filenames,model[OUTPUTMODEL_TAG(3)], values);
337 for(int i = 0; i <i_filenames.size(); i++)
338 values[i_filenames[i]] = i;
339 osort.sort(values,o_sort);
346 for(int i = 1; i < 4; i++)
348 if ( checkModel(model, OUTPUTMODEL_TAG(i)))
350 sscanf(model[OUTPUTMODEL_TAG(i)].c_str(), "%d", tags[i]);
354 if( checkModel(model, OUTPUTMODEL_TAG(3)) )
358 getDBValues<std::string>(i_filenames,model[OUTPUTMODEL_TAG(3)], values);
362 getValues<std::string>(i_filenames, model[OUTPUTMODEL_TAG(3)], values);
367 for(int i = 0; i <i_filenames.size(); i++)
368 values[i_filenames[i]] = i;
371 for(int index = tag_begin; index <= tag_end; index += tag_step)
373 std::map<std::string, std::string>::iterator it_val = values.begin();
374 for(;it_val != values.end(); it_val++)
376 if(it_val->second.c_str() )// == index)
378 o_sort.push_back(it_val->first.c_str());
383 if(checkModel(model, OUTPUTMODEL_TAG(4)))
386 sscanf(model[OUTPUTMODEL_TAG(4)].c_str(), "%d", lv);
387 sort(i_filenames,o_sort,lv);