1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
20 #include "clitkList.h"
28 //---------------------------------------------------------------------
30 //---------------------------------------------------------------------
31 template<class ListItemType>
32 void List< ListItemType>::Read(const string& filename, bool verbose)
35 openFileForReading(is, filename);
41 if(verbose)std::cout<<"Reading "<<filename<<"..."<<std::endl;
42 for (unsigned int dim=0; dim<Dimension; dim++)
51 this->push_back(item);
52 for (unsigned int dim=0; dim<Dimension; dim++)
62 //---------------------------------------------------------------------
63 // ReadPointPairs (specific for DARS- IX point list format)
64 //---------------------------------------------------------------------
65 template<class ListItemType>
67 List<ListItemType>::ReadAndConvertPointPairs(const std::string& fileName, List& correspList, const typename ImageType::Pointer referenceImage, bool verbose)
71 correspList.resize(0);
74 IndexType item1, item2;
75 itk::Point<ValueType, Dimension> point1,point2;
76 unsigned int pointNumber=0;
77 bool veryUnsure=false;
80 bool pointWasFound=ReadPointPair(fileName, pointNumber, item1, item2, veryUnsure);
82 // Loop over all points
88 if (verbose) std::cout<<"Omitting item "<<pointNumber<<" (very unsure): "<<item1<<" ; "<<item2<<std::endl;
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);
102 pointWasFound=ReadPointPair(fileName, pointNumber, item1, item2, veryUnsure);
106 //---------------------------------------------------------------------
108 //---------------------------------------------------------------------
109 template<class ListItemType>
111 List< ListItemType>::ReadPointPair(const std::string& fileName, const unsigned int& pointNumber, IndexType& item1, IndexType& item2, bool& veryUnsure)
114 bool pointWasFound=false;
120 ifstream listStream(fileName.c_str());
122 if(!listStream.is_open()){
123 std::cerr << "ERROR: Couldn't open file " << fileName << " in List::Read" << std::endl;
127 // Skip the configuration lines
128 while ( !listStream.eof())
131 skipComment(listStream);
132 getline(listStream, line);
135 unsigned int endIndexItemType= line.find_first_of ("_",0)-1;
136 string typeString= line.substr(0 , endIndexItemType+1);
139 if (typeString=="Point")
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;
150 // FF to the pointNumber
151 while ( (itemNumber != pointNumber) && (!listStream.eof()) )
154 skipComment(listStream);
157 getline(listStream, line);
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;
166 // Go over all fields of the pointNumber
167 while (itemNumber == pointNumber && (!listStream.eof()) )
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);
177 // Get the point Value
178 unsigned int beginIndexItemValue= line.find_first_of ("=",0)+1;
179 stringstream valueString( line.substr(beginIndexItemValue) );
181 valueString>> itemValue;
190 if (itemTag=="X_Corresp")
192 if (itemTag=="Y_Corresp")
194 if (itemTag=="Z_Corresp")
196 if (itemTag=="VeryUnsure")
197 veryUnsure=itemValue;
200 skipComment(listStream);
201 getline(listStream, line);
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;
210 return pointWasFound;
214 //---------------------------------------------------------------------
216 //---------------------------------------------------------------------
217 template<class ListItemType>
218 void List< ListItemType>::Print()
220 for (unsigned int i=0; i< this->size(); i++)
221 std::cout<<this->at(i)<<std::endl;
225 //---------------------------------------------------------------------
227 //---------------------------------------------------------------------
228 template<class ListItemType>
229 void List<ListItemType>::Write(const string fileName, const bool verbose)
231 ofstream listStream(fileName.c_str());
232 if(!listStream.is_open()){
233 cerr << "ERROR: Couldn't open file " << fileName << " in List::Write" << endl;
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];
248 //---------------------------------------------------------------------
250 //---------------------------------------------------------------------
251 template<class ListItemType>
252 List<itk::FixedArray<double, 1> > List< ListItemType>::Norm()
254 List<itk::FixedArray<double, 1> > norm;
255 itk::FixedArray<double, 1> n;
257 for (unsigned int i=0; i< this->size(); i++)
260 for (d=0; d< Dimension; d++)
261 n[0]+=this->at(i)[d] * this->at(i)[d];
273 #endif //#define CLITKLIST
277 // ifstream listStream(fileName.c_str());
278 // ListItemType item;
280 // if(!listStream.is_open()){
281 // std::cerr << "ERROR: Couldn't open file " << fileName << " in List::Read" << std::endl;
284 // skipComment(listStream);
286 // // Skip the configuration lines
290 // skipComment(listStream);
291 // getline(listStream, line);
293 // // Get the line type
294 // unsigned int endIndexItemType= line.find_first_of ("_",0)-1;
295 // string typeString= line.substr(0 , endIndexItemType+1);
298 // if (typeString=="Point")
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;
309 // // Go over all the lines
310 // while(!listStream.eof())
313 // itk::Point<ValueType, Dimension> point1, point2;
316 // bool veryUnsure=false;
319 // itk::ContinuousIndex<ValueType, Dimension> item1, item2;
323 // // Go over all lines corresponding to this point
324 // unsigned int currentNumber=itemNumber;
325 // while (itemNumber == currentNumber && (!listStream.eof()) )
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);
332 // // Get the point Value
333 // unsigned int beginIndexItemValue= line.find_first_of ("=",0)+1;
334 // stringstream valueString( line.substr(beginIndexItemValue) );
336 // valueString>> itemValue;
340 // item1[0]=itemValue;
342 // item1[1]=itemValue;
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;
354 // // Get the next line
355 // skipComment(listStream);
356 // getline(listStream, line);
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;