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://oncora1.lyon.fnclcc.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");
33 //--------------------------------------------------------------------
36 //--------------------------------------------------------------------
37 void clitk::AnatomicalFeatureDatabase::Write()
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;
49 //--------------------------------------------------------------------
52 //--------------------------------------------------------------------
53 //http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
55 static inline std::string <rim(std::string &s) {
56 s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
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());
64 // trim from both ends
65 static inline std::string &trim(std::string &s) {
66 return ltrim(rtrim(s));
68 //--------------------------------------------------------------------
71 //--------------------------------------------------------------------
72 void clitk::AnatomicalFeatureDatabase::Load()
77 openFileForReading(is, GetFilename());
78 // load line by line string/string
84 std::getline(is,value,'\n');
85 ltrim(value); // remove leading space
86 m_MapOfTag[tag] = value;
91 //--------------------------------------------------------------------
94 //--------------------------------------------------------------------
95 void clitk::AnatomicalFeatureDatabase::SetPoint3D(std::string tag, PointType3D & p)
97 ::itk::OStringStream value;
98 value << p[0] << " " << p[1] << " " << p[2];
99 m_MapOfTag[tag] = value.str();
101 //--------------------------------------------------------------------
104 //--------------------------------------------------------------------
105 double clitk::AnatomicalFeatureDatabase::GetPoint3D(std::string tag, int dim)
111 //--------------------------------------------------------------------
114 //--------------------------------------------------------------------
115 void clitk::AnatomicalFeatureDatabase::GetPoint3D(std::string tag, PointType3D & p)
117 if (m_MapOfTag.find(tag) == m_MapOfTag.end()) {
118 clitkExceptionMacro("Could not find the tag <" << tag << "> of type Point3D in the DB");
121 std::string s = m_MapOfTag[tag];
123 // construct a stream from the string
124 std::stringstream strstr(s);
126 // use stream iterators to copy the stream to the vector as
127 // whitespace separated strings
128 std::istream_iterator<std::string> it(strstr);
129 std::istream_iterator<std::string> end;
130 std::vector<std::string> results(it, end);
132 // parse the string into 3 doubles
133 for(int i=0; i<3; i++) {
135 if (!clitk::fromString<double>(p[i], results[i].c_str())) {
136 clitkExceptionMacro("Error while reading Point3D, could not convert '"
137 << results[i].c_str() << "' into double.");
140 // p[i] = atof(results[i].c_str());
145 #include <boost/foreach.hpp>
146 #include <boost/tokenizer.hpp>
147 // parse the string into 3 doubles
148 boost::char_separator<char> sep(", ");
149 boost::tokenizer<boost::char_separator<char> > tokens(s, sep);
151 BOOST_FOREACH(std::string t, tokens) {
152 std::cout << t << "." << std::endl;
153 p[i] = atof(t.c_str());
159 //--------------------------------------------------------------------
162 //--------------------------------------------------------------------
163 void clitk::AnatomicalFeatureDatabase::SetImageFilename(std::string tag, std::string f)
167 //--------------------------------------------------------------------