]> Creatis software - clitk.git/blob - segmentation/clitkAnatomicalFeatureDatabase.cxx
Add output dicom filename to clitkImage2Dicom
[clitk.git] / segmentation / clitkAnatomicalFeatureDatabase.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to: 
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://www.centreleonberard.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17   ===========================================================================**/
18
19 // clitk
20 #include "clitkAnatomicalFeatureDatabase.h"
21
22 // std
23 #include <iterator>
24 #include <sstream>
25 #include <cctype>
26 #include <functional>
27
28 //--------------------------------------------------------------------
29 clitk::AnatomicalFeatureDatabase::AnatomicalFeatureDatabase() 
30
31   SetFilename("default.afdb");
32   SetPath("./");
33 }
34 //--------------------------------------------------------------------
35
36
37 //--------------------------------------------------------------------
38 clitk::AnatomicalFeatureDatabase::Pointer clitk::AnatomicalFeatureDatabase::New(std::string filename) 
39
40   Pointer a = AnatomicalFeatureDatabase::New();
41   a->SetFilename(filename);
42   a->Load();
43   return a;
44 }
45 //--------------------------------------------------------------------
46
47
48 //--------------------------------------------------------------------
49 void clitk::AnatomicalFeatureDatabase::Write() 
50 {
51   // open file
52   std::ofstream os;
53   openFileForWriting(os, GetFilename());
54   MapTagType::const_iterator iter = m_MapOfTag.begin();
55   while (iter != m_MapOfTag.end()) {
56     os << iter->first << " " << iter->second << std::endl;
57     iter++;
58   }
59   os.close();
60 }
61 //--------------------------------------------------------------------
62
63
64 //--------------------------------------------------------------------
65 //http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
66 // trim from start
67 static inline std::string &ltrim(std::string &s) {
68   s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
69   return s;
70 }
71 // trim from end
72 static inline std::string &rtrim(std::string &s) {
73   s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
74   return s;
75 }
76 // trim from both ends
77 static inline std::string &trim(std::string &s) {
78   return ltrim(rtrim(s));
79 }
80 //--------------------------------------------------------------------
81
82
83 //--------------------------------------------------------------------
84 void clitk::AnatomicalFeatureDatabase::Load() 
85 {
86   m_MapOfTag.clear();
87   // open file
88   std::ifstream is;
89   openFileForReading(is, GetFilename());
90   // load line by line string/string
91   while (!is.fail()) {
92     std::string tag;
93     is >> tag; 
94     if (tag != "") {
95       std::string value;
96       std::getline(is,value,'\n');
97       ltrim(value); // remove leading space
98       m_MapOfTag[tag] = value;
99     }
100   }
101   is.close();
102 }
103 //--------------------------------------------------------------------
104
105    
106 //--------------------------------------------------------------------
107 void clitk::AnatomicalFeatureDatabase::SetPoint3D(std::string tag, PointType3D & p)
108 {
109   std::ostringstream value;
110   value << p[0] << " " << p[1] << " " << p[2];
111   m_MapOfTag[tag] = value.str();
112 }
113 //--------------------------------------------------------------------
114
115
116 //--------------------------------------------------------------------
117 double clitk::AnatomicalFeatureDatabase::GetPoint3D(std::string tag, int dim)
118 {
119   PointType3D p;
120   GetPoint3D(tag, p);
121   return p[dim];
122 }
123 //--------------------------------------------------------------------
124
125
126 //--------------------------------------------------------------------
127 std::string clitk::AnatomicalFeatureDatabase::GetTagValue(std::string tag)
128 {
129   if (!TagExist(tag)) {
130     clitkExceptionMacro("Could not find the tag <" << tag << "> in the DB");
131     return "";
132   }
133   std::string s = m_MapOfTag[tag];
134   return s;
135 }
136 //--------------------------------------------------------------------
137
138
139 //--------------------------------------------------------------------
140 void clitk::AnatomicalFeatureDatabase::GetPoint3D(std::string tag, PointType3D & p)
141 {
142   if (!TagExist(tag)) {
143     clitkExceptionMacro("Could not find the tag <" << tag << "> of type Point3D in the DB");
144     return;
145   }
146
147   std::string s = m_MapOfTag[tag];
148     
149   // construct a stream from the string
150   std::stringstream strstr(s);
151
152   // use stream iterators to copy the stream to the vector as
153   // whitespace separated strings
154   std::istream_iterator<std::string> it(strstr);
155   std::istream_iterator<std::string> end;
156   std::vector<std::string> results(it, end);
157
158   // parse the string into 3 doubles
159   for(int i=0; i<3; i++) {
160
161     if (!clitk::fromString<double>(p[i], results[i].c_str())) {
162       clitkExceptionMacro("Error while reading Point3D, could not convert '" 
163                           << results[i].c_str() << "' into double.");
164     }
165   }
166 }
167 //--------------------------------------------------------------------
168
169
170 //--------------------------------------------------------------------
171 void clitk::AnatomicalFeatureDatabase::SetImageFilename(std::string tag, std::string f)
172 {
173   m_MapOfTag[tag] = f;
174 }
175 //--------------------------------------------------------------------
176
177
178 //--------------------------------------------------------------------
179 bool clitk::AnatomicalFeatureDatabase::TagExist(std::string tag)
180 {
181   return (m_MapOfTag.find(tag) != m_MapOfTag.end());
182 }
183 //--------------------------------------------------------------------
184
185
186 //-------------------------------------------------------------------- 
187 void clitk::AnatomicalFeatureDatabase::SetDouble(std::string tag, double value)
188 {
189   m_MapOfTag[tag] = clitk::toString(value);
190 }
191 //-------------------------------------------------------------------- 
192
193
194 //-------------------------------------------------------------------- 
195 double clitk::AnatomicalFeatureDatabase::GetDouble(std::string tag)
196 {
197   if (!TagExist(tag)) {
198     clitkExceptionMacro("Could not find the tag <" << tag << "> of type Double in the DB");
199     return -1;
200   }
201
202   double a;
203   if (!clitk::fromString<double>(a, m_MapOfTag[tag])) {
204     clitkExceptionMacro("Error while reading Double (tag='" << tag << "'), could not convert '" 
205                         << m_MapOfTag[tag] << "' into double.");
206   }
207   return a;  
208 }
209 //-------------------------------------------------------------------- 
210
211
212 //-------------------------------------------------------------------- 
213 void clitk::AnatomicalFeatureDatabase::RemoveTag(TagType tag)
214 {
215   if (TagExist(tag)) {
216     m_MapOfTag.erase(m_MapOfTag.find(tag));
217   }
218 }
219 //--------------------------------------------------------------------