]> Creatis software - clitk.git/blob - registration/clitkList.txx
Moved from repository clitk to clitk.private/tests_dav
[clitk.git] / registration / clitkList.txx
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 #ifndef CLITKLIST_TXX
19 #define CLITKLIST_TXX
20 #include "clitkList.h"
21
22 using namespace std; 
23
24 namespace clitk 
25 {
26   
27
28   //---------------------------------------------------------------------
29   // Read
30   //---------------------------------------------------------------------
31   template<class ListItemType>
32   void List< ListItemType>::Read(const string& filename, bool verbose)
33   {
34     std::ifstream is;
35     openFileForReading(is, filename);
36
37     this->resize(0);
38     ListItemType item;
39     ValueType x;
40    
41     if(verbose)std::cout<<"Reading "<<filename<<"..."<<std::endl;
42     for (unsigned int dim=0; dim<Dimension; dim++)
43       {
44         skipComment(is);
45         is >> x;
46         item[dim]=x;
47       }
48    
49     while (!is.eof()) 
50       {
51         this->push_back(item);
52         for (unsigned int dim=0; dim<Dimension; dim++)
53           {
54             skipComment(is);
55             is >> x;
56             item[dim]=x;
57           }
58       }
59   }
60
61
62   //---------------------------------------------------------------------
63   // ReadPointPairs (specific for DARS- IX point list format)
64   //---------------------------------------------------------------------
65   template<class ListItemType>
66   void 
67   List<ListItemType>::ReadAndConvertPointPairs(const std::string& fileName, List& correspList, const typename ImageType::Pointer referenceImage, bool verbose)
68   {
69     // Init
70     this->resize(0);
71     correspList.resize(0);
72
73     // Tags
74     IndexType item1, item2;
75     itk::Point<ValueType, Dimension> point1,point2;
76     unsigned int pointNumber=0;
77     bool veryUnsure=false;
78
79     // Read first point
80     bool pointWasFound=ReadPointPair(fileName, pointNumber, item1, item2, veryUnsure);
81
82     // Loop over all points 
83     while (pointWasFound)
84       {
85         // Store the values
86         if(veryUnsure)
87           {
88             if (verbose) std::cout<<"Omitting item "<<pointNumber<<" (very unsure): "<<item1<<" ; "<<item2<<std::endl;
89           }
90         else
91           {
92             if (verbose) std::cout<<"Adding item "<<pointNumber<<": "<<item1<<" ; "<<item2<<std::endl;
93             referenceImage->TransformContinuousIndexToPhysicalPoint(item1, point1);
94             referenceImage->TransformContinuousIndexToPhysicalPoint(item2, point2);
95             if (verbose) std::cout<<"Converted items "<<pointNumber<<" to points: "<<point1<<" ; "<<point2<<std::endl;
96             this->push_back(point1);
97             correspList.push_back(point2);
98           }
99
100         // Next point
101         pointNumber++;
102         pointWasFound=ReadPointPair(fileName, pointNumber, item1, item2, veryUnsure);
103       }
104   }
105   
106   //---------------------------------------------------------------------
107   // Read one point
108   //---------------------------------------------------------------------
109   template<class ListItemType>
110   bool
111   List< ListItemType>::ReadPointPair(const std::string& fileName, const unsigned int& pointNumber, IndexType& item1, IndexType& item2, bool& veryUnsure)
112   {
113     // bool
114     bool pointWasFound=false;
115     veryUnsure=false;
116     item1.Fill(0.0);
117     item2.Fill(0.0);
118     
119     // Open the file
120     ifstream listStream(fileName.c_str());
121     string line;
122     if(!listStream.is_open()){
123       std::cerr << "ERROR: Couldn't open file " << fileName << " in List::Read" << std::endl;
124       return false;
125     }
126     
127     // Skip the configuration lines
128     while ( !listStream.eof())
129       {
130         // Get the line
131         skipComment(listStream);
132         getline(listStream, line);
133         
134         // Get the line type
135         unsigned int endIndexItemType= line.find_first_of ("_",0)-1;
136         string typeString= line.substr(0 , endIndexItemType+1);
137         
138         // Check
139         if  (typeString=="Point")
140           break;
141       }
142
143     // Get the point number
144     unsigned int beginIndexItemNumber= line.find_first_of ("_",0)+1; 
145     unsigned int endIndexItemNumber= line.find_first_of ("->",0)-1;
146     stringstream numberString( line.substr(beginIndexItemNumber , endIndexItemNumber-beginIndexItemNumber+1) );
147     unsigned int itemNumber;
148     numberString>> itemNumber;
149
150     // FF to the pointNumber
151     while ( (itemNumber != pointNumber) && (!listStream.eof()) )
152       {
153         // Skipcomment
154         skipComment(listStream);
155         
156         // Get Line
157         getline(listStream, line);
158         
159         // Get the point number
160         beginIndexItemNumber= line.find_first_of ("_",0)+1; 
161         endIndexItemNumber= line.find_first_of ("->",0)-1;
162         stringstream numberString( line.substr(beginIndexItemNumber , endIndexItemNumber-beginIndexItemNumber+1) );
163         numberString>> itemNumber;
164       }
165     
166     // Go over all fields of the pointNumber
167     while (itemNumber == pointNumber && (!listStream.eof()) )
168       {
169         // point found
170         pointWasFound=true;
171
172         // Get the tag
173         unsigned int beginIndexItemTag= line.find_first_of ("->",0)+2; 
174         unsigned int endIndexItemTag= line.find_first_of ("=",0)-1;
175         string itemTag= line.substr(beginIndexItemTag , endIndexItemTag-beginIndexItemTag+1);
176         
177         // Get the point Value
178         unsigned int beginIndexItemValue= line.find_first_of ("=",0)+1; 
179         stringstream valueString( line.substr(beginIndexItemValue) );
180         double itemValue;
181         valueString>> itemValue;
182         
183         // Fill the items
184         if (itemTag=="X")
185           item1[0]=itemValue;
186         if (itemTag=="Y")
187           item1[1]=itemValue;
188         if (itemTag=="Z")
189           item1[2]=itemValue;
190         if (itemTag=="X_Corresp")
191           item2[0]=itemValue;
192         if (itemTag=="Y_Corresp")
193           item2[1]=itemValue;
194         if (itemTag=="Z_Corresp")
195           item2[2]=itemValue;
196         if (itemTag=="VeryUnsure")
197           veryUnsure=itemValue;
198         
199         // Get the next line
200         skipComment(listStream);
201         getline(listStream, line);
202         
203         // Get the point number
204         unsigned int beginIndexItemNumber= line.find_first_of ("_",0)+1; 
205         unsigned int endIndexItemNumber= line.find_first_of ("->",0)-1;
206         stringstream numberString( line.substr(beginIndexItemNumber , endIndexItemNumber-beginIndexItemNumber+1) );
207         numberString >> itemNumber;
208       }
209
210     return pointWasFound;
211   }
212
213
214  //---------------------------------------------------------------------
215   // Print
216   //---------------------------------------------------------------------
217   template<class ListItemType>
218   void List< ListItemType>::Print()
219   {
220     for (unsigned int i=0; i< this->size(); i++)
221       std::cout<<this->at(i)<<std::endl;
222   }
223
224
225   //---------------------------------------------------------------------
226   // Write
227   //---------------------------------------------------------------------
228   template<class ListItemType>
229   void List<ListItemType>::Write(const string fileName, const bool verbose)
230   {
231     ofstream listStream(fileName.c_str());
232     if(!listStream.is_open()){
233       cerr << "ERROR: Couldn't open file " << fileName << " in List::Write" << endl;
234       return;
235     }
236
237     typename ListType::iterator it=this->begin();
238     while(it!=this->end()) {
239       listStream << (*it)[0];
240       for (unsigned int i=1; i< this->at(0).Size(); i++)
241         listStream <<" "<< (*it)[i];
242       listStream<< endl;      
243       it++;
244     }
245     listStream.close();
246   }
247
248   //---------------------------------------------------------------------
249   // Norm
250   //---------------------------------------------------------------------
251   template<class ListItemType>
252   List<itk::FixedArray<double, 1> > List< ListItemType>::Norm()
253   {
254     List<itk::FixedArray<double, 1> > norm;
255     itk::FixedArray<double, 1> n;
256     unsigned int d;
257     for (unsigned int i=0; i< this->size(); i++)
258       {
259         n[0]=0;
260         for (d=0; d< Dimension; d++)
261           n[0]+=this->at(i)[d] * this->at(i)[d];
262         n[0]=sqrt(n[0]);
263         norm.push_back(n);
264       }
265     return norm;
266   }
267  
268
269   
270
271 }
272
273 #endif //#define CLITKLIST
274
275
276     //    // Open the file
277     //     ifstream listStream(fileName.c_str());
278     //     ListItemType item;
279     //     string line;
280     //     if(!listStream.is_open()){
281     //       std::cerr << "ERROR: Couldn't open file " << fileName << " in List::Read" << std::endl;
282     //       return;
283     //     }
284     //     skipComment(listStream);
285     
286     //     // Skip the configuration lines
287     //     while (true)
288     //       {
289     //  // Get the line
290     //  skipComment(listStream);
291     //  getline(listStream, line);
292     
293     //  // Get the line type
294     //  unsigned int endIndexItemType= line.find_first_of ("_",0)-1;
295     //  string typeString= line.substr(0 , endIndexItemType+1);
296     
297     //  // Check
298     //  if  (typeString=="Point")
299     //    break;
300     //       }
301     
302     //     // Get the point number
303     //     unsigned int beginIndexItemNumber= line.find_first_of ("_",0)+1; 
304     //     unsigned int endIndexItemNumber= line.find_first_of ("->",0)-1;
305     //     stringstream numberString( line.substr(beginIndexItemNumber , endIndexItemNumber-beginIndexItemNumber+1) );
306     //     unsigned int itemNumber;
307     //     numberString>> itemNumber;
308     
309     //     // Go over all the lines
310     //     while(!listStream.eof()) 
311     //       {
312     //  // Stored data
313     //  itk::Point<ValueType, Dimension> point1, point2;
314     //  point1.Fill(0.);
315     //  point2.Fill(0.);
316     //  bool veryUnsure=false;
317     
318     //  // Retrieved data
319     //  itk::ContinuousIndex<ValueType, Dimension> item1, item2;
320     //  item1.Fill(0.);
321     //  item2.Fill(0.);
322     
323     //  // Go over all lines corresponding to this point
324     //  unsigned int currentNumber=itemNumber;
325     //  while (itemNumber == currentNumber && (!listStream.eof()) )
326     //    {
327     //      // Get the tag
328     //      unsigned int beginIndexItemTag= line.find_first_of ("->",0)+2; 
329     //      unsigned int endIndexItemTag= line.find_first_of ("=",0)-1;
330     //      string itemTag= line.substr(beginIndexItemTag , endIndexItemTag-beginIndexItemTag+1);
331     
332     //      // Get the point Value
333     //      unsigned int beginIndexItemValue= line.find_first_of ("=",0)+1; 
334     //      stringstream valueString( line.substr(beginIndexItemValue) );
335     //      double itemValue;
336     //      valueString>> itemValue;
337     
338     //      // Fill the items
339     //      if (itemTag=="X")
340     //        item1[0]=itemValue;
341     //      if (itemTag=="Y")
342     //        item1[1]=itemValue;
343     //      if (itemTag=="Z")
344     //        item1[2]=itemValue;
345     //      if (itemTag=="X_Corresp")
346     //        item2[0]=itemValue;
347     //      if (itemTag=="Y_Corresp")
348     //        item2[1]=itemValue;
349     //      if (itemTag=="Z_Corresp")
350     //         item2[2]=itemValue;
351     //      if (itemTag=="VeryUnsure")
352     //        veryUnsure=itemValue;
353     
354     //      // Get the next line
355     //      skipComment(listStream);
356     //      getline(listStream, line);
357     
358     //      // Get the point number
359     //      unsigned int beginIndexItemNumber= line.find_first_of ("_",0)+1; 
360     //      unsigned int endIndexItemNumber= line.find_first_of ("->",0)-1;
361     //      stringstream numberString( line.substr(beginIndexItemNumber , endIndexItemNumber-beginIndexItemNumber+1) );
362     //      numberString>> itemNumber;
363     //    }