]> Creatis software - clitk.git/blob - itk/clitkRelativePositionDataBase.cxx
1512e4c9059cfa1c9466d308473960cdfb4214bf
[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     MapByPatientType::const_iterator iter = o.begin();
171     std::vector<std::string> * v = new std::vector<std::string>; 
172     MapToVecFirst(o, *v);
173     return *v;
174   }
175   //--------------------------------------------------------------------
176
177
178  //--------------------------------------------------------------------
179   double RelativePositionDataBase::GetAreaGain(const IndexType & index) const
180   {
181     // FIXME change name
182     const RelativePositionInformationType & v = GetInformation(index);
183     return v.sizeAfterThreshold/v.sizeBeforeThreshold;
184   }
185   //--------------------------------------------------------------------
186
187
188   //--------------------------------------------------------------------
189   double RelativePositionDataBase::GetThreshold(const IndexType & index) const
190   {
191     const RelativePositionInformationType & v = GetInformation(index);
192     return v.threshold;
193   }
194   //--------------------------------------------------------------------
195     
196
197   //--------------------------------------------------------------------
198   void
199   RelativePositionDataBase::GetListOfObjects(const std::string & station, std::vector<std::string> & objects) const
200   {
201     const MapByObjectType & a = GetMapByObject(station);
202     MapToVecFirst(a, objects);
203   }
204   //--------------------------------------------------------------------
205
206
207   //--------------------------------------------------------------------
208   void
209   RelativePositionDataBase::GetListOfDirections(const std::string & station, 
210                                                   const std::string & object, 
211                                                   std::vector<RelativePositionDirectionType> & directions) const
212   {
213     IndexType i;
214     i.station = station;
215     i.object = object;
216     const MapByDirectionType & n = GetMapByDirection(i);
217     MapToVecFirst(n, directions);    
218   }
219   //--------------------------------------------------------------------
220
221
222   //--------------------------------------------------------------------
223   bool RelativePositionDataBase::CheckIndex(const IndexType & index) const
224   {
225     try {
226       const RelativePositionInformationType & m =  GetInformation(index);
227     } catch (clitk::ExceptionObject e) {
228       // std::cout << e.what() << std::endl;      
229       return false;
230     }
231     return true;
232   }
233   //--------------------------------------------------------------------
234
235   //--------------------------------------------------------------------
236   std::ostream& operator<<(std::ostream & os, const clitk::RelativePositionInformationType & rp)
237   {
238     rp.Print(os);
239     return os;
240   }
241   //--------------------------------------------------------------------
242
243 } // end namespace clitk
244 //--------------------------------------------------------------------
245
246 #endif