1 #include <creaImageIOOutputModel.h>
3 #include <boost/filesystem.hpp>
4 #include "boost/algorithm/string.hpp"
7 #include <gdcmGlobal.h>
9 #include <gdcmSerieHelper.h>
13 #if defined(USE_GDCM2)
15 #include <gdcmDicts.h>
16 #include <gdcmGlobal.h>
19 #if defined(USE_XERCES)
20 #include <xercesc/dom/DOM.hpp>
21 #include <xercesc/dom/DOMDocument.hpp>
22 #include <xercesc/util/XMLString.hpp>
23 #include <xercesc/util/PlatformUtils.hpp>
24 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);
59 if ((fileVector.size()>1) && (sh->IsCoherent( &fileVector )))
61 sh->OrderFileList(&fileVector);
62 spacing= sh->GetZSpacing();
65 for (i=0; i<fileVector.size(); i++)
67 im.push_back( (fileVector[i])->GetFileName() );
69 for (i=0; i<lstAux.size(); i++)
71 im.push_back( lstAux[i] );
74 std::sort( im.begin(), im.end() );
82 #if defined(USE_GDCM2)
84 double OutputModel::orderFilesWithZspacing(std::vector<std::string> &im)
90 #if defined(USE_XERCES)
92 OutputModel::OutputModel(OutputModelParser *i_outparser) : m_outparser(i_outparser)
98 //OutputModel::setOutputModel(std::vector< std::map<std::string, std::string>> i_outputs)
100 // std::vector< std::map<std::string, std::string>>::iterator it = i_outputs.begin();
101 // for(; it != i_outputs.end(); it++)
103 // checkModel((*it));
107 bool OutputModel::checkModel(std::map<std::string, std::string> i_model, const std::string i_val)
110 if( i_model.find(i_val) != i_model.end() )
118 OutputModel::~OutputModel()
121 void OutputModel::setDB(const std::string i_db, const std::string i_table)
128 const std::string OutputModel::getTag()
137 #if defined(USE_GDCM)
138 sprintf(key,"D%04x_%04x", m_tag1.GetGroup(), m_tag1.GetElement());
140 #if defined(USE_GDCM2)
141 sprintf(key,"D%04x_%04x", m_tag2.GetGroup(), m_tag2.GetElement());
146 /* void OutputModel::sort(const std::vector<std::string> i_filenames, std::vector<std::string> &o_sort)
148 std::vector<int> values;
153 getDBValues<int>(i_filenames, values);
157 getValues<int>(i_filenames, values);
162 for(int i = 0; i <i_filenames.size(); i++)
166 for(int index = tag_begin; index <= tag_end; index += tag_step)
168 std::vector<int>::iterator it_val = values.begin();
169 std::vector<std::string>::const_iterator it = i_filenames.begin();
170 for(;it != i_filenames.end(); it_val++, it++)
172 if((*it_val) == index)
174 o_sort.push_back((*it));
180 m_model->sort(i_filenames, o_sort);
183 void OutputModel::getDBValues(const std::vector<std::string> i_filenames, const std::string i_stag, std::map<std::string , T> &o_val)
188 //template<typename T>
189 //T OutputModel::getTypeTag()
191 // GDCM_NAME_SPACE::DictEntry* entry = GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->GetEntry(GetGroup(),GetElement());
192 // entry->GetVR().GetVRType( entry->GetVR());
197 void OutputModel::getValues(const std::vector<std::string> i_filenames,const std::string i_tag, std::map< std::string, T> &o_val)
199 #if defined(USE_GDCM)
200 getReadValues(i_filenames, o_val);
202 #if defined(USE_GDCM2)
203 getScanValues(i_filenames, i_tag,o_val);
207 #if defined(USE_GDCM2)
208 // TO DO if some file don't provide this value, we are lost so return a map!
210 void OutputModel::getScanValues(const std::vector<std :: string> i_filenames, const std::string i_stag, std::map<std::string,T> &o_val)
213 sscanf(i_stag.c_str(),"D%04hx_%04hx ",&gr,&el);
214 gdcm::Tag tag(gr, el);
215 if (!tag.IsIllegal())
217 std::vector<std :: string> names(i_filenames);
222 std::vector<std :: string>::iterator it = names.begin();
223 for(;it != names.end(); it++)
224 boost::algorithm::replace_all((*it),"\\", "/");
225 scan.Scan(i_filenames);
226 // const gdcm::Scanner::TagToValue &mapping =
227 std::vector<std::string>::const_iterator it_file = i_filenames.begin();
228 for(; it_file != i_filenames.end(); it++)
230 if( scan.GetMapping((*it_file).c_str()).begin() != scan.GetMapping((*it_file).c_str()).end())
232 o_val[(*it)] = scan.GetMapping((*it_file).c_str()).begin()->second;
242 //const gdcm::VR::VRType OutputModel::getType(const std::string i_tag)
245 // sscanf(itag.c_str(),"D%04hx_%04hx ",&gr,&el);
246 // const gdcm::Global& g = gdcm::Global::GetInstance(); // sum of all knowledge !
247 // const gdcm::Dicts &dicts = g.GetDicts();
248 // const gdcm::Dict &dict = dicts.GetPublicDict(); // Part 6
249 // gdcm::DictEntry dictentry = dict.GetDictEntry(gdcm::Tag(gr, el));
250 // return dictentry.GetVR();
255 #if defined(USE_GDCM)
256 double OutputModel::orderFiles(std::vector<std::string> im, std::vector<std::string> &out)
259 std::vector<boost::shared_ptr<GDCM_NAME_SPACE::File> > fileVector;
260 std::vector<std::string> lstAux;
261 GDCM_NAME_SPACE::SerieHelper *sh = GDCM_NAME_SPACE::SerieHelper::New();
262 std::vector<std::string>::iterator it = im.begin();
263 for (; it!=im.end(); ++it)
265 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
270 fileVector.push_back(f);
274 lstAux.push_back((*it));
277 if ((fileVector.size()>1) && (sh->IsCoherent( &fileVector )))
279 sh->OrderFileList(&fileVector);
280 spacing= sh->GetZSpacing();
283 for (i=0; i<fileVector.size(); i++)
285 out.push_back( (fileVector[i])->GetFileName() );
287 for (i=0; i<lstAux.size(); i++)
289 out.push_back( lstAux[i] );
294 std::sort( im.begin(), im.end() );
301 void OutputModel::sort(const std::vector<std::string> i_filenames, std::vector<std::string> &o_sort, int level)
303 #if defined(USE_XERCES)
307 int tag_end = i_filenames.size();
309 std::map<std::string, std::string> model = m_outparser->getLevel(level);
310 std::map<std::string, std::string> values;
312 if( checkModel(model, OUTPUTMODEL_TAG(3)) && !b_db)
314 #if defined (USE_GDCM2)
316 //OutputSort< getType(model[OUTPUTMODEL_TAG(3)] > osort;
318 getValues<std::string>(i_filenames, model[OUTPUTMODEL_TAG(3)], values);
323 OutputSort<std::string> osort;
324 for(int i = 0; i < 3; i++)
326 if ( checkModel(model, OUTPUTMODEL_TAG(i)))
328 osort.setTag(model[OUTPUTMODEL_TAG(i)],OUTPUTMODEL_TAG(i));
334 getDBValues<std::string>(i_filenames,model[OUTPUTMODEL_TAG(3)], values);
338 for(int i = 0; i <i_filenames.size(); i++)
339 values[i_filenames[i]] = i;
340 osort.sort(values,o_sort);
347 for(int i = 1; i < 4; i++)
349 if ( checkModel(model, OUTPUTMODEL_TAG(i)))
351 sscanf(model[OUTPUTMODEL_TAG(i)].c_str(), "%d", tags[i]);
355 if( checkModel(model, OUTPUTMODEL_TAG(3)) )
359 getDBValues<std::string>(i_filenames,model[OUTPUTMODEL_TAG(3)], values);
363 getValues<std::string>(i_filenames, model[OUTPUTMODEL_TAG(3)], values);
368 for(int i = 0; i <i_filenames.size(); i++)
369 values[i_filenames[i]] = i;
372 for(int index = tag_begin; index <= tag_end; index += tag_step)
374 std::map<std::string, std::string>::iterator it_val = values.begin();
375 for(;it_val != values.end(); it_val++)
377 if(it_val->second.c_str() )// == index)
379 o_sort.push_back(it_val->first.c_str());
384 if(checkModel(model, OUTPUTMODEL_TAG(4)))
387 sscanf(model[OUTPUTMODEL_TAG(4)].c_str(), "%d", lv);
388 sort(i_filenames,o_sort,lv);