]> Creatis software - creaImageIO.git/blob - src/creaImageIOOutputModel.cpp
additional document for output
[creaImageIO.git] / src / creaImageIOOutputModel.cpp
1 #include <creaImageIOOutputModel.h>
2
3 #include <boost/filesystem.hpp>
4 #include "boost/algorithm/string.hpp"
5
6 #if defined(USE_GDCM)
7 #include <gdcmGlobal.h>
8 #include <gdcmSerieHelper.h>
9 #endif
10
11 #if defined(USE_GDCM2)
12 #include <gdcmDict.h>
13 #include <gdcmDicts.h>
14 #include <gdcmGlobal.h>
15 #endif
16
17
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;
24 #endif
25
26
27 namespace creaImageIO
28 {
29
30
31         OutputModel::~OutputModel()
32         {
33         }
34
35                  #if defined(USE_GDCM)
36          double OutputModel::orderFilesWithZspacing(std::vector<std::string> &im)
37         {
38                 double spacing=1;
39                 typedef std::vector<GDCM_NAME_SPACE::File* > FileList;
40                 FileList fileVector;
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)
46                 {
47                         ///\TODO liberer les GDCM_NAME_SPACE::File a la fin!  // JPR
48                         GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
49                         f->SetFileName(*it);
50                         f->Load();
51                         if (f->IsReadable())
52                         {
53                                 fileVector.push_back(f);
54                         } else {
55                                 lstAux.push_back(*it);
56                         }
57                 } // for
58                         
59                 
60                 if ((fileVector.size()>1) && (sh->IsCoherent( &fileVector )))
61                 {
62                                 sh->OrderFileList(&fileVector);
63                                 spacing= sh->GetZSpacing();
64                                 im.clear();
65                                 int i;
66                                 for (i=0; i<fileVector.size(); i++)
67                                 {
68                                         im.push_back( (fileVector[i])->GetFileName() );
69                                 }
70                                 for (i=0; i<lstAux.size(); i++)
71                                 {
72                                         im.push_back( lstAux[i] );
73                                 }
74                 }else {
75                         std::sort( im.begin(), im.end() );
76                 }
77                                 
78            return spacing;
79         }
80
81         #endif
82
83 #if defined(USE_GDCM2)
84         // TO DO
85         double OutputModel::orderFilesWithZspacing(std::vector<std::string> &im)
86         {
87                 return 1;
88         }
89 #endif
90
91
92
93 #if defined(USE_XERCES)
94
95         OutputModel::OutputModel(OutputModelParser *i_outparser) : m_outparser(i_outparser)
96         {
97                 // Init
98                 b_db = false;
99         }
100
101         //OutputModel::setOutputModel(std::vector< std::map<std::string, std::string>> i_outputs)
102         //{
103         //      std::vector< std::map<std::string, std::string>>::iterator it = i_outputs.begin();
104         //      for(; it != i_outputs.end(); it++)
105         //      {
106         //              checkModel((*it));
107         //      }
108         //}
109
110          bool OutputModel::checkModel(std::map<std::string, std::string> i_model, const std::string i_val)
111         {
112                 bool bres = false;
113                 if( i_model.find(i_val) != i_model.end() )
114                 {
115                         bres = true;
116                 }
117
118                 return bres;
119
120         }
121
122         OutputModel::~OutputModel()
123         {
124         }
125         void OutputModel::setDB(const std::string i_db, const std::string i_table)
126         {
127                 b_db = true;
128                 m_db = i_db;
129                 m_table = i_table;
130         }
131
132         const std::string OutputModel::getTag()
133         {
134                 char key[12];
135                 if (!m_tag.empty())
136                 {
137                         return m_tag;
138                 }
139                 else
140                 {
141 #if defined(USE_GDCM)
142                 sprintf(key,"D%04x_%04x", m_tag1.GetGroup(), m_tag1.GetElement());
143 #endif
144 #if defined(USE_GDCM2)
145                 sprintf(key,"D%04x_%04x", m_tag2.GetGroup(), m_tag2.GetElement());
146 #endif
147                 return key;
148                 }
149         }
150 /*       void OutputModel::sort(const std::vector<std::string> i_filenames, std::vector<std::string> &o_sort)
151         {
152                 std::vector<int> values;
153                 if(bDicom)
154                 {
155                         if(b_db)
156                         {
157                                 getDBValues<int>(i_filenames, values);
158                         }
159                         else
160                         {
161                                 getValues<int>(i_filenames, values);
162                         }
163                 }
164                 else
165                 {
166                         for(int i = 0; i <i_filenames.size(); i++)
167                                 values.push_back(i);
168                 }
169
170                 for(int index = tag_begin; index <= tag_end; index += tag_step)
171                 {
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++)
175                         {
176                                 if((*it_val) == index)
177                                 {
178                                         o_sort.push_back((*it));
179                                         break;
180                                 }
181                         }
182                 }
183                 if(m_model != NULL)
184                         m_model->sort(i_filenames, o_sort);
185         }*/
186         template<typename T>
187         void OutputModel::getDBValues(const std::vector<std::string> i_filenames, const std::string i_stag, std::map<std::string , T> &o_val)
188         {
189         }
190
191
192          //template<typename T>
193          //T OutputModel::getTypeTag()
194          //{
195                 // GDCM_NAME_SPACE::DictEntry* entry =  GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->GetEntry(GetGroup(),GetElement());
196                 // entry->GetVR().GetVRType( entry->GetVR());
197                 // 
198          //}
199
200         template<typename T>
201         void OutputModel::getValues(const std::vector<std::string> i_filenames,const std::string i_tag, std::map< std::string, T> &o_val)
202          {
203 #if defined(USE_GDCM)
204                   getReadValues(i_filenames, o_val);
205 #endif
206 #if defined(USE_GDCM2)
207                   getScanValues(i_filenames, i_tag,o_val);
208 #endif
209          }
210
211 #if defined(USE_GDCM2)
212         // TO DO if some file don't provide this value, we are lost so return a map!
213         template<typename T>
214         void OutputModel::getScanValues(const std::vector<std :: string> i_filenames, const std::string i_stag, std::map<std::string,T> &o_val)
215         {
216                 uint16_t gr, el;
217                 sscanf(i_stag.c_str(),"D%04hx_%04hx ",&gr,&el); 
218                 gdcm::Tag tag(gr, el);
219                 if (!tag.IsIllegal())
220                 {
221                         std::vector<std :: string> names(i_filenames);
222                         gdcm::Scanner scan;
223                         scan.ClearTags();
224                         scan.AddTag(tag);
225                         
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++)
233                         {
234                                 if(     scan.GetMapping((*it_file).c_str()).begin() != scan.GetMapping((*it_file).c_str()).end())
235                                 {
236                                         o_val[(*it)] = scan.GetMapping((*it_file).c_str()).begin()->second;
237                                 }
238                                 else
239                                 {
240                                         o_val[(*it)] = "";
241                                 }
242                         }
243                 }
244                  
245         }
246
247         //const gdcm::VR::VRType OutputModel::getType(const std::string i_tag)
248         //{
249         //      uint16_t gr, el;
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();
256         //}
257 #endif
258
259
260
261 #if defined(USE_GDCM)
262         double OutputModel::orderFiles(std::vector<std::string> im, std::vector<std::string> &out)
263 {
264         double spacing = 1;
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)
270         {
271                 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
272                 f->SetFileName(*it);
273                 f->Load();
274                 if (f->IsReadable())
275                 {
276                         fileVector.push_back(f);
277                 } 
278                 else
279                 {
280                         lstAux.push_back((*it));
281                 }
282         } 
283         if ((fileVector.size()>1) && (sh->IsCoherent( &fileVector )))
284         {
285                         sh->OrderFileList(&fileVector);
286                         spacing= sh->GetZSpacing();
287                         out.clear();
288                         int i;
289                         for (i=0; i<fileVector.size(); i++)
290                         {
291                                 out.push_back( (fileVector[i])->GetFileName() );
292                         }
293                         for (i=0; i<lstAux.size(); i++)
294                         {
295                                 out.push_back( lstAux[i] );
296                         }
297         }
298         else
299         {
300                 std::sort( im.begin(), im.end() );
301         }
302      return spacing;
303         }
304 #endif 
305
306
307          void OutputModel::sort(const std::vector<std::string> i_filenames, std::vector<std::string> &o_sort, int level)
308         {
309 #if defined(USE_XERCES)
310                 int tags[3];
311                         tags[0] = 0;
312
313                 int tag_end = i_filenames.size();
314                 int tag_step = 1;
315                 std::map<std::string, std::string> model = m_outparser->getLevel(level);
316                 std::map<std::string, std::string> values;
317                 
318                 if( checkModel(model, OUTPUTMODEL_TAG(3)) && !b_db)
319                 {
320 #if defined (USE_GDCM2)
321                         
322                         //OutputSort< getType(model[OUTPUTMODEL_TAG(3)] > osort;
323                         
324                         getValues<std::string>(i_filenames, model[OUTPUTMODEL_TAG(3)], values);
325 #endif
326                 }
327                 else
328                 {
329                         OutputSort<std::string> osort;
330                         for(int i = 0; i < 3; i++)
331                         {
332                                 if ( checkModel(model, OUTPUTMODEL_TAG(i)))
333                                 {       
334                                         osort.setTag(model[OUTPUTMODEL_TAG(i)],OUTPUTMODEL_TAG(i));
335                                 }
336                         }
337
338                         if(b_db)
339                         {
340                                 getDBValues<std::string>(i_filenames,model[OUTPUTMODEL_TAG(3)], values);
341                         }
342                         else
343                         {
344                                 for(int i = 0; i <i_filenames.size(); i++)
345                                         values[i_filenames[i]] = i;
346                                 osort.sort(values,o_sort);
347                         }
348                 }
349
350
351
352
353                 for(int i = 1; i < 4; i++)
354                 {
355                         if ( checkModel(model, OUTPUTMODEL_TAG(i)))
356                         {       
357                                 sscanf(model[OUTPUTMODEL_TAG(i)].c_str(), "%d", tags[i]);
358                         }
359                 }
360                 
361                 if( checkModel(model, OUTPUTMODEL_TAG(3)) )
362                 {
363                         if(b_db)
364                         {
365                                 getDBValues<std::string>(i_filenames,model[OUTPUTMODEL_TAG(3)], values);
366                         }
367                         else
368                         {
369                                 getValues<std::string>(i_filenames, model[OUTPUTMODEL_TAG(3)], values);
370                         }
371                 }
372                 else
373                 {
374                         for(int i = 0; i <i_filenames.size(); i++)
375                                 values[i_filenames[i]] = i;
376                 }
377
378                 for(int index = tag_begin; index <= tag_end; index += tag_step)
379                 {
380                         std::map<std::string, std::string>::iterator it_val = values.begin();
381                         for(;it_val != values.end(); it_val++)
382                         {
383                                 if(it_val->second.c_str() )// == index)
384                                 {
385                                         o_sort.push_back(it_val->first.c_str());
386                                         break;
387                                 }
388                         }
389                 }
390                 if(checkModel(model, OUTPUTMODEL_TAG(4)))
391                 {
392                         int lv;
393                         sscanf(model[OUTPUTMODEL_TAG(4)].c_str(), "%d", lv);
394                         sort(i_filenames,o_sort,lv);
395                 }
396 #endif
397         }
398
399 #endif
400
401
402 }