]> Creatis software - clitk.git/blob - segmentation/clitkAnatomicalFeatureDatabase.cxx
small improvement
[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
26 //--------------------------------------------------------------------
27 clitk::AnatomicalFeatureDatabase::AnatomicalFeatureDatabase() 
28
29   SetFilename("noname.afdb"); 
30 }
31 //--------------------------------------------------------------------
32
33
34 //--------------------------------------------------------------------
35 void clitk::AnatomicalFeatureDatabase::Write() 
36 {
37   // open file
38   std::ofstream os;
39   openFileForWriting(os, GetFilename());
40   MapTagType::const_iterator iter = m_MapOfTag.begin();
41   while (iter != m_MapOfTag.end()) {
42     os << iter->first << " " << iter->second << std::endl;
43     iter++;
44   }
45   os.close();
46 }
47 //--------------------------------------------------------------------
48
49
50 //--------------------------------------------------------------------
51 //http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
52 // trim from start
53 static inline std::string &ltrim(std::string &s) {
54   s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
55   return s;
56 }
57 // trim from end
58 static inline std::string &rtrim(std::string &s) {
59   s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
60   return s;
61 }
62 // trim from both ends
63 static inline std::string &trim(std::string &s) {
64   return ltrim(rtrim(s));
65 }
66 //--------------------------------------------------------------------
67
68
69 //--------------------------------------------------------------------
70 void clitk::AnatomicalFeatureDatabase::Load() 
71 {
72   // open file
73   std::ifstream is;
74   openFileForReading(is, GetFilename());
75   // load line by line string/string
76   while (!is.fail()) {
77     std::string tag;
78     is >> tag; 
79     std::string value;
80     std::getline(is,value,'\n');
81     ltrim(value); // remove leading space
82     m_MapOfTag[tag] = value;
83   }
84 }
85 //--------------------------------------------------------------------
86
87    
88 //--------------------------------------------------------------------
89 void clitk::AnatomicalFeatureDatabase::SetPoint3D(std::string tag, PointType3D & p)
90 {
91   ::itk::OStringStream value;
92   value << p[0] << " " << p[1] << " " << p[2];
93   m_MapOfTag[tag] = value.str();
94 }
95 //--------------------------------------------------------------------
96
97
98 //--------------------------------------------------------------------
99 void clitk::AnatomicalFeatureDatabase::GetPoint3D(std::string tag, PointType3D & p)
100 {
101   if (m_MapOfTag.find(tag) == m_MapOfTag.end()) {
102     clitkExceptionMacro("Could not find the tag <" << tag << "> of type Point3D in the DB");
103   }
104   else {
105     std::string s = m_MapOfTag[tag];
106     
107     // construct a stream from the string
108     std::stringstream strstr(s);
109
110     // use stream iterators to copy the stream to the vector as
111     // whitespace separated strings
112     std::istream_iterator<std::string> it(strstr);
113     std::istream_iterator<std::string> end;
114     std::vector<std::string> results(it, end);
115
116     // parse the string into 3 doubles
117     for(int i=0; i<3; i++) {
118       p[i] = atof(results[i].c_str());
119     }
120
121     /*
122     // boost 
123     #include <boost/foreach.hpp>
124     #include <boost/tokenizer.hpp>
125     // parse the string into 3 doubles
126     boost::char_separator<char> sep(", ");
127     boost::tokenizer<boost::char_separator<char> > tokens(s, sep);
128     int i=0;
129     BOOST_FOREACH(std::string t, tokens) {
130     std::cout << t << "." << std::endl;
131     p[i] = atof(t.c_str());
132     i++;
133     }
134     */
135   }
136 }
137 //--------------------------------------------------------------------
138
139
140 //--------------------------------------------------------------------
141 void clitk::AnatomicalFeatureDatabase::SetImageFilename(std::string tag, std::string f)
142 {
143   m_MapOfTag[tag] = f;
144 }
145 //--------------------------------------------------------------------
146