]> Creatis software - clitk.git/blob - segmentation/clitkAnatomicalFeatureDatabase.cxx
separate airway tracking from extract lung
[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 void clitk::AnatomicalFeatureDatabase::Load() 
52 {
53   // open file
54   std::ifstream is;
55   openFileForReading(is, GetFilename());
56   // load line by line string/string
57   while (!is.fail()) {
58     std::string tag;
59     is >> tag; 
60     std::string value;
61     std::getline(is,value,'\n');
62     m_MapOfTag[tag] = value;
63   }
64 }
65 //--------------------------------------------------------------------
66
67    
68 //--------------------------------------------------------------------
69 void clitk::AnatomicalFeatureDatabase::SetPoint3D(std::string tag, PointType3D & p)
70 {
71   ::itk::OStringStream value;
72   value << p[0] << " " << p[1] << " " << p[2];
73   m_MapOfTag[tag] = value.str();
74 }
75 //--------------------------------------------------------------------
76
77
78 //--------------------------------------------------------------------
79 void clitk::AnatomicalFeatureDatabase::GetPoint3D(std::string tag, PointType3D & p)
80 {
81   if (m_MapOfTag.find(tag) == m_MapOfTag.end()) {
82     clitkExceptionMacro("Could not find the tag <" << tag << "> of type Point3D in the DB");
83   }
84   else {
85     std::string s = m_MapOfTag[tag];
86     
87     // construct a stream from the string
88     std::stringstream strstr(s);
89
90     // use stream iterators to copy the stream to the vector as
91     // whitespace separated strings
92     std::istream_iterator<std::string> it(strstr);
93     std::istream_iterator<std::string> end;
94     std::vector<std::string> results(it, end);
95
96     // parse the string into 3 doubles
97     for(int i=0; i<3; i++) {
98       DD(results[i]);
99       p[i] = atof(results[i].c_str());
100       DD(p[i]);
101     }
102
103     /*
104     // boost 
105     #include <boost/foreach.hpp>
106     #include <boost/tokenizer.hpp>
107     // parse the string into 3 doubles
108     boost::char_separator<char> sep(", ");
109     boost::tokenizer<boost::char_separator<char> > tokens(s, sep);
110     int i=0;
111     BOOST_FOREACH(std::string t, tokens) {
112       std::cout << t << "." << std::endl;
113       p[i] = atof(t.c_str());
114       i++;
115     }
116     */
117   }
118 }
119 //--------------------------------------------------------------------
120