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