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