1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
20 #include "clitkAnatomicalFeatureDatabase.h"
28 //--------------------------------------------------------------------
29 clitk::AnatomicalFeatureDatabase::AnatomicalFeatureDatabase()
31 SetFilename("default.afdb");
34 //--------------------------------------------------------------------
37 //--------------------------------------------------------------------
38 clitk::AnatomicalFeatureDatabase::Pointer clitk::AnatomicalFeatureDatabase::New(std::string filename)
40 Pointer a = AnatomicalFeatureDatabase::New();
41 a->SetFilename(filename);
45 //--------------------------------------------------------------------
48 //--------------------------------------------------------------------
49 void clitk::AnatomicalFeatureDatabase::Write()
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;
61 //--------------------------------------------------------------------
64 //--------------------------------------------------------------------
65 //http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
67 static inline std::string <rim(std::string &s) {
68 s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
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());
76 // trim from both ends
77 static inline std::string &trim(std::string &s) {
78 return ltrim(rtrim(s));
80 //--------------------------------------------------------------------
83 //--------------------------------------------------------------------
84 void clitk::AnatomicalFeatureDatabase::Load()
89 openFileForReading(is, GetFilename());
90 // load line by line string/string
96 std::getline(is,value,'\n');
97 ltrim(value); // remove leading space
98 m_MapOfTag[tag] = value;
103 //--------------------------------------------------------------------
106 //--------------------------------------------------------------------
107 void clitk::AnatomicalFeatureDatabase::SetPoint3D(std::string tag, PointType3D & p)
109 #if ITK_VERSION_MAJOR > 3
110 std::ostringstream value;
112 ::itk::OStringStream value;
114 value << p[0] << " " << p[1] << " " << p[2];
115 m_MapOfTag[tag] = value.str();
117 //--------------------------------------------------------------------
120 //--------------------------------------------------------------------
121 double clitk::AnatomicalFeatureDatabase::GetPoint3D(std::string tag, int dim)
127 //--------------------------------------------------------------------
130 //--------------------------------------------------------------------
131 std::string clitk::AnatomicalFeatureDatabase::GetTagValue(std::string tag)
133 if (!TagExist(tag)) {
134 clitkExceptionMacro("Could not find the tag <" << tag << "> in the DB");
137 std::string s = m_MapOfTag[tag];
140 //--------------------------------------------------------------------
143 //--------------------------------------------------------------------
144 void clitk::AnatomicalFeatureDatabase::GetPoint3D(std::string tag, PointType3D & p)
146 if (!TagExist(tag)) {
147 clitkExceptionMacro("Could not find the tag <" << tag << "> of type Point3D in the DB");
151 std::string s = m_MapOfTag[tag];
153 // construct a stream from the string
154 std::stringstream strstr(s);
156 // use stream iterators to copy the stream to the vector as
157 // whitespace separated strings
158 std::istream_iterator<std::string> it(strstr);
159 std::istream_iterator<std::string> end;
160 std::vector<std::string> results(it, end);
162 // parse the string into 3 doubles
163 for(int i=0; i<3; i++) {
165 if (!clitk::fromString<double>(p[i], results[i].c_str())) {
166 clitkExceptionMacro("Error while reading Point3D, could not convert '"
167 << results[i].c_str() << "' into double.");
171 //--------------------------------------------------------------------
174 //--------------------------------------------------------------------
175 void clitk::AnatomicalFeatureDatabase::SetImageFilename(std::string tag, std::string f)
179 //--------------------------------------------------------------------
182 //--------------------------------------------------------------------
183 bool clitk::AnatomicalFeatureDatabase::TagExist(std::string tag)
185 return (m_MapOfTag.find(tag) != m_MapOfTag.end());
187 //--------------------------------------------------------------------
190 //--------------------------------------------------------------------
191 void clitk::AnatomicalFeatureDatabase::SetDouble(std::string tag, double value)
193 m_MapOfTag[tag] = clitk::toString(value);
195 //--------------------------------------------------------------------
198 //--------------------------------------------------------------------
199 double clitk::AnatomicalFeatureDatabase::GetDouble(std::string tag)
201 if (!TagExist(tag)) {
202 clitkExceptionMacro("Could not find the tag <" << tag << "> of type Double in the DB");
207 if (!clitk::fromString<double>(a, m_MapOfTag[tag])) {
208 clitkExceptionMacro("Error while reading Double (tag='" << tag << "'), could not convert '"
209 << m_MapOfTag[tag] << "' into double.");
213 //--------------------------------------------------------------------
216 //--------------------------------------------------------------------
217 void clitk::AnatomicalFeatureDatabase::RemoveTag(TagType tag)
220 m_MapOfTag.erase(m_MapOfTag.find(tag));
223 //--------------------------------------------------------------------