]> Creatis software - clitk.git/blob - segmentation/clitkAnatomicalFeatureDatabase.cxx
a23deb40e86924f05451f0f478cf4f47704fec3d
[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://oncora1.lyon.fnclcc.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 }
33 //--------------------------------------------------------------------
34
35
36 //--------------------------------------------------------------------
37 void clitk::AnatomicalFeatureDatabase::Write() 
38 {
39   // open file
40   std::ofstream os;
41   openFileForWriting(os, GetFilename());
42   MapTagType::const_iterator iter = m_MapOfTag.begin();
43   while (iter != m_MapOfTag.end()) {
44     os << iter->first << " " << iter->second << std::endl;
45     iter++;
46   }
47   os.close();
48 }
49 //--------------------------------------------------------------------
50
51
52 //--------------------------------------------------------------------
53 //http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
54 // trim from start
55 static inline std::string &ltrim(std::string &s) {
56   s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
57   return s;
58 }
59 // trim from end
60 static inline std::string &rtrim(std::string &s) {
61   s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
62   return s;
63 }
64 // trim from both ends
65 static inline std::string &trim(std::string &s) {
66   return ltrim(rtrim(s));
67 }
68 //--------------------------------------------------------------------
69
70
71 //--------------------------------------------------------------------
72 void clitk::AnatomicalFeatureDatabase::Load() 
73 {
74   m_MapOfTag.clear();
75   // open file
76   std::ifstream is;
77   openFileForReading(is, GetFilename());
78   // load line by line string/string
79   while (!is.fail()) {
80     std::string tag;
81     is >> tag; 
82     if (tag != "") {
83       std::string value;
84       std::getline(is,value,'\n');
85       ltrim(value); // remove leading space
86       m_MapOfTag[tag] = value;
87     }
88   }
89   is.close();
90 }
91 //--------------------------------------------------------------------
92
93    
94 //--------------------------------------------------------------------
95 void clitk::AnatomicalFeatureDatabase::SetPoint3D(std::string tag, PointType3D & p)
96 {
97   ::itk::OStringStream value;
98   value << p[0] << " " << p[1] << " " << p[2];
99   m_MapOfTag[tag] = value.str();
100 }
101 //--------------------------------------------------------------------
102
103
104 //--------------------------------------------------------------------
105 double clitk::AnatomicalFeatureDatabase::GetPoint3D(std::string tag, int dim)
106 {
107   PointType3D p;
108   GetPoint3D(tag, p);
109   return p[dim];
110 }
111 //--------------------------------------------------------------------
112
113
114 //--------------------------------------------------------------------
115 void clitk::AnatomicalFeatureDatabase::GetPoint3D(std::string tag, PointType3D & p)
116 {
117   if (!TagExist(tag)) {
118     clitkExceptionMacro("Could not find the tag <" << tag << "> of type Point3D in the DB");
119     return;
120   }
121
122   std::string s = m_MapOfTag[tag];
123     
124   // construct a stream from the string
125   std::stringstream strstr(s);
126
127   // use stream iterators to copy the stream to the vector as
128   // whitespace separated strings
129   std::istream_iterator<std::string> it(strstr);
130   std::istream_iterator<std::string> end;
131   std::vector<std::string> results(it, end);
132
133   // parse the string into 3 doubles
134   for(int i=0; i<3; i++) {
135
136     if (!clitk::fromString<double>(p[i], results[i].c_str())) {
137       clitkExceptionMacro("Error while reading Point3D, could not convert '" 
138                           << results[i].c_str() << "' into double.");
139     }
140   }
141 }
142 //--------------------------------------------------------------------
143
144
145 //--------------------------------------------------------------------
146 void clitk::AnatomicalFeatureDatabase::SetImageFilename(std::string tag, std::string f)
147 {
148   m_MapOfTag[tag] = f;
149 }
150 //--------------------------------------------------------------------
151
152
153 //--------------------------------------------------------------------
154 bool clitk::AnatomicalFeatureDatabase::TagExist(std::string tag)
155 {
156   return (m_MapOfTag.find(tag) != m_MapOfTag.end());
157 }
158 //--------------------------------------------------------------------
159
160 //-------------------------------------------------------------------- 
161 void clitk::AnatomicalFeatureDatabase::SetDouble(std::string tag, double value)
162 {
163   m_MapOfTag[tag] = clitk::toString(value);
164 }
165 //-------------------------------------------------------------------- 
166
167 //-------------------------------------------------------------------- 
168 double clitk::AnatomicalFeatureDatabase::GetDouble(std::string tag)
169 {
170   if (!TagExist(tag)) {
171     clitkExceptionMacro("Could not find the tag <" << tag << "> of type Double in the DB");
172     return -1;
173   }
174
175   double a;
176   if (!clitk::fromString<double>(a, m_MapOfTag[tag])) {
177     clitkExceptionMacro("Error while reading Double (tag='" << tag << "'), could not convert '" 
178                         << m_MapOfTag[tag] << "' into double.");
179   }
180   return a;  
181 }
182 //--------------------------------------------------------------------