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