]> Creatis software - clitk.git/blob - itk/clitkRelativePositionDataBase.cxx
Remove warnings
[clitk.git] / itk / clitkRelativePositionDataBase.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://www.centreleonberard.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 #ifndef CLITKRELATIVEPOSITIONDATABASE_CXX
20 #define CLITKRELATIVEPOSITIONDATABASE_CXX
21
22 // clitk
23 #include "clitkRelativePositionDataBase.h"
24
25 namespace clitk {
26
27   //--------------------------------------------------------------------
28   void RelativePositionDataBase::ReadIndex(std::istream & is, IndexType & index)
29   {
30     is >> index.patient; 
31     is >> index.station;
32     is >> index.object;
33     is >> index.direction.angle1; 
34     index.direction.angle1 = clitk::deg2rad(index.direction.angle1);
35     is >> index.direction.angle2;
36     index.direction.angle2 = clitk::deg2rad(index.direction.angle2);
37     std::string s;
38     is >> s;
39     if (s=="true") index.direction.notFlag = true;
40     else index.direction.notFlag = false;
41   }
42   //--------------------------------------------------------------------
43  
44
45   //--------------------------------------------------------------------
46   void RelativePositionDataBase::ReadInformation(std::istream & is, RelativePositionInformationType & v)
47   {
48     is >> v.threshold;
49     is >> v.sizeBeforeThreshold;
50     is >> v.sizeAfterThreshold;
51     is >> v.sizeReference;
52   }
53   //--------------------------------------------------------------------
54   
55   //--------------------------------------------------------------------
56   void RelativePositionDataBase::Read(const std::string & filename)
57   {
58     std::ifstream is;
59     openFileForReading(is, filename);
60
61     std::string s;
62     IndexType index;
63     RelativePositionInformationType v;
64     while (is) {
65       skipComment(is); /* FIXME Read Index etc */ 
66       ReadIndex(is, index);
67       ReadInformation(is, v);      
68
69       if (is) {// FIXME INSERT
70         // Set in station
71         if (m_DB.find(index.station) == m_DB.end()) {
72           MapByObjectType s;
73           m_DB[index.station] = s;
74         }
75         MapByObjectType & s = m_DB[index.station];
76         
77         // Get Direction map from Object
78         if (s.find(index.object) == s.end()) {
79           MapByDirectionType r;
80           s[index.object] = r;
81         }
82         MapByDirectionType & r = s[index.object];
83         
84         // Get Patient map from Direction
85         if (r.find(index.direction) == r.end()) {
86           MapByPatientType q;
87           r[index.direction] = q;
88         }
89         MapByPatientType & q = r[index.direction];
90
91         // Set value by patient
92         q[index.patient] = v;
93         
94         // Debug
95         // index.Println(); 
96         // GetMapByPatient(index).find(index.patient)->second.Println();
97         
98       } // End insertion
99     } // end loop reading
100   }
101   //--------------------------------------------------------------------
102
103   
104   //--------------------------------------------------------------------
105   const RelativePositionDataBase::MapByDirectionType & 
106   RelativePositionDataBase::GetMapByDirection(const IndexType & index) const
107   {
108     const MapByObjectType & a = GetMapByObject(index.station);
109     if (a.find(index.object) == a.end()) {
110       clitkExceptionMacro("Could not find index in DB (object= '" << index.object << "' not found).");
111     }
112     return a.find(index.object)->second;    
113   }
114   //--------------------------------------------------------------------
115   
116
117   //--------------------------------------------------------------------
118   const RelativePositionDataBase::MapByObjectType & 
119   RelativePositionDataBase::GetMapByObject(const std::string & station) const
120   {
121     if (m_DB.find(station) == m_DB.end()) {
122       clitkExceptionMacro("Could not find index in DB (station= '" << station << "' not found).");
123     }
124     return m_DB.find(station)->second;    
125   }
126   //--------------------------------------------------------------------
127   
128
129   //--------------------------------------------------------------------
130   const RelativePositionDataBase::MapByPatientType & 
131   RelativePositionDataBase::GetMapByPatient(const IndexType & index) const
132   {
133     const MapByDirectionType & a = GetMapByDirection(index);
134     if (a.find(index.direction) == a.end()) {
135       std::ostringstream s;
136       index.direction.Print(s);
137       clitkExceptionMacro("Could not find index in DB (direction= '" << s.str() << "' not found).");
138     }
139     return a.find(index.direction)->second;
140   }
141   //--------------------------------------------------------------------
142   
143
144   //--------------------------------------------------------------------
145   const RelativePositionInformationType & 
146   RelativePositionDataBase::GetInformation(const IndexType & index) const
147   {
148     const RelativePositionDataBase::MapByPatientType & a = GetMapByPatient(index);
149     if (a.find(index.patient) == a.end()) {
150       clitkExceptionMacro("Could not find index in DB (patient= '" << index.patient << "' not found).");
151     }
152     return a.find(index.patient)->second;
153   }
154   //--------------------------------------------------------------------
155
156
157   //--------------------------------------------------------------------
158   int RelativePositionDataBase::GetNumberOfPatient(const IndexType & index) const
159   {
160     const MapByPatientType & o = GetMapByPatient(index);
161     return o.size();
162   }
163   //--------------------------------------------------------------------
164
165
166   //--------------------------------------------------------------------
167   std::vector<std::string> & RelativePositionDataBase::GetListOfPatients(const IndexType & index) const
168   {
169     const MapByPatientType & o = GetMapByPatient(index);
170     std::vector<std::string> * v = new std::vector<std::string>; 
171     MapToVecFirst(o, *v);
172     return *v;
173   }
174   //--------------------------------------------------------------------
175
176
177  //--------------------------------------------------------------------
178   double RelativePositionDataBase::GetAreaGain(const IndexType & index) const
179   {
180     // FIXME change name
181     const RelativePositionInformationType & v = GetInformation(index);
182     return v.sizeAfterThreshold/v.sizeBeforeThreshold;
183   }
184   //--------------------------------------------------------------------
185
186
187   //--------------------------------------------------------------------
188   double RelativePositionDataBase::GetThreshold(const IndexType & index) const
189   {
190     const RelativePositionInformationType & v = GetInformation(index);
191     return v.threshold;
192   }
193   //--------------------------------------------------------------------
194     
195
196   //--------------------------------------------------------------------
197   void
198   RelativePositionDataBase::GetListOfObjects(const std::string & station, std::vector<std::string> & objects) const
199   {
200     const MapByObjectType & a = GetMapByObject(station);
201     MapToVecFirst(a, objects);
202   }
203   //--------------------------------------------------------------------
204
205
206   //--------------------------------------------------------------------
207   void
208   RelativePositionDataBase::GetListOfDirections(const std::string & station, 
209                                                   const std::string & object, 
210                                                   std::vector<RelativePositionDirectionType> & directions) const
211   {
212     IndexType i;
213     i.station = station;
214     i.object = object;
215     const MapByDirectionType & n = GetMapByDirection(i);
216     MapToVecFirst(n, directions);    
217   }
218   //--------------------------------------------------------------------
219
220
221   //--------------------------------------------------------------------
222   bool RelativePositionDataBase::CheckIndex(const IndexType & index) const
223   {
224     try {
225       /*const RelativePositionInformationType & m = */ GetInformation(index);
226     } catch (clitk::ExceptionObject e) {
227       // std::cout << e.what() << std::endl;      
228       return false;
229     }
230     return true;
231   }
232   //--------------------------------------------------------------------
233
234   //--------------------------------------------------------------------
235   std::ostream& operator<<(std::ostream & os, const clitk::RelativePositionInformationType & rp)
236   {
237     rp.Print(os);
238     return os;
239   }
240   //--------------------------------------------------------------------
241
242 } // end namespace clitk
243 //--------------------------------------------------------------------
244
245 #endif