]> Creatis software - clitk.git/blob - registration/clitkLists.txx
Merge branch 'VTK6_Qt5_Overlay4D' into VTK6_Qt5_Binarize
[clitk.git] / registration / clitkLists.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 clitkLists_txx
19 #define clitkLists_txx
20 #include "clitkLists.h"
21
22 using namespace std; 
23
24 namespace clitk 
25 {
26   
27   //---------------------------------------------------------------------
28   // Read
29   //---------------------------------------------------------------------
30   template<class ListItemType>
31   void Lists< ListItemType>::Read(const std::vector<string>& filenames, bool verbose)
32   {
33
34     // Make the lists
35     this->resize(filenames.size());
36
37     // Read the lists   
38     for (unsigned int i=0; i<filenames.size();i++)
39       this->at(i).Read(filenames[i],verbose);
40     
41   }
42
43   //---------------------------------------------------------------------
44   // Norm
45   //---------------------------------------------------------------------
46   template<class ListItemType>
47   Lists<itk::FixedArray<double,1> > Lists<ListItemType>::Norm(void)
48   {
49     Lists<itk::FixedArray<double,1> > norm;
50
51     // Normalize the lists   
52     for (unsigned int i=0; i<this->size();i++)
53       norm.push_back(this->at(i).Norm());
54   
55     return norm;
56   }
57
58   //---------------------------------------------------------------------
59   // ReadPointPairs (specific for DARS- IX point list format)
60   //---------------------------------------------------------------------
61   template<class ListItemType>
62   void 
63   Lists<ListItemType>::ReadAndConvertPointPairs(const std::vector<std::string>& fileNames, ListType& refList, const typename ImageType::Pointer referenceImage, bool verbose)
64   {
65
66     // Init
67     this->resize(fileNames.size());
68     refList.resize(0);
69     bool pointWasFound=true;
70     unsigned int pointNumber=0;
71     itk::ContinuousIndex<ValueType, Dimension> item1;
72     std::vector<itk::ContinuousIndex<ValueType, Dimension> > item2(fileNames.size());
73     itk::Point<ValueType, Dimension> point1, point2;
74     unsigned int totalNumberOfPoints=0;
75     std::vector<unsigned int> addedNumberOfPoints,ommittedNumberOfPoints;
76     std::vector<bool> pointIsUnsure(fileNames.size());
77     std::vector<std::vector<bool> > ommittedNumberOfPointsUnsure;
78     
79     // Loop over the points
80     while (pointWasFound)
81       {
82
83         if (verbose) std::cout<<std::endl<<"Processing point number: "<<pointNumber<<"..."<<std::endl;
84         if (verbose) std::cout<<"Point number: "<<pointNumber<<" was found in list number: ";
85         
86         // Loop over lists
87         bool veryUnsure=false;
88         for (unsigned int i =0; i< fileNames.size(); i++)
89           {
90             // Get the point
91             pointWasFound=this->at(i).ReadPointPair(fileNames[i],pointNumber,item1,item2[i],veryUnsure);
92             if (verbose && pointWasFound) std::cout<<" "<<i;
93                     
94             // Set unsure
95             pointIsUnsure[i]= veryUnsure;
96
97             // point not found
98             if(!pointWasFound) break;
99           }
100         if (verbose) std::cout<<std::endl;
101
102         // The point was found
103         if (pointWasFound)
104           {
105             totalNumberOfPoints++;
106
107             // Point unsure?
108             bool unsure=false;
109             for (unsigned int i =0; i< fileNames.size(); i++)
110               if (pointIsUnsure[i])
111                 {
112                   unsure=true;
113                   break;
114                 }
115
116             // At least one of the lists had it tagged as veryUnsure
117             if (unsure)
118               {
119                 ommittedNumberOfPoints.push_back(pointNumber);
120                 ommittedNumberOfPointsUnsure.push_back(pointIsUnsure);
121                 if (verbose) std::cout<<"Omitting item "<<pointNumber<<" (very unsure): "<<item1<<" ; "<<item2[0]<<std::endl;
122               }
123             else 
124               {
125                 addedNumberOfPoints.push_back(pointNumber);
126                 // Add the point to all the lists 
127                 for (unsigned int i =0; i< fileNames.size(); i++)
128                   {
129                     if (verbose) std::cout<<"Adding item "<<pointNumber<<": "<<item1<<" ; "<<item2[i]<<std::endl;
130                     if (i==0)
131                       {
132                         referenceImage->TransformContinuousIndexToPhysicalPoint(item1, point1);
133                         refList.push_back(point1);
134                       }
135                     referenceImage->TransformContinuousIndexToPhysicalPoint(item2[i], point2);
136                     if (verbose) std::cout<<"Converted items "<<pointNumber<<" to points: "<<point1<<" ; "<<point2<<std::endl;
137                     this->at(i).push_back(point2);
138                   }
139               }
140           }
141         
142         // Next point 
143         pointNumber++;
144
145       }
146
147     if (verbose)
148       {
149         std::cout<<std::endl<<totalNumberOfPoints<<" points processed, "<< ommittedNumberOfPoints.size() <<" points omitted, "<< addedNumberOfPoints.size()<<" points added."<<std::endl;
150         std::cout<<"Added point numbers: "<< std::endl;
151         for (unsigned int i=0; i< addedNumberOfPoints.size();i++)
152           std::cout<<addedNumberOfPoints[i] <<std::endl;
153         std::cout<<"Omitted point numbers: "<< std::endl;
154             for (unsigned int i=0; i< ommittedNumberOfPoints.size();i++)
155               {
156                 std::cout<<ommittedNumberOfPoints[i] <<"\t";
157                 for (unsigned int j=0; j< fileNames.size();j++)
158                   std::cout<<ommittedNumberOfPointsUnsure[i][j]<<" ";
159                 std::cout<<std::endl;
160               }
161       }
162   }
163
164     
165
166
167   //---------------------------------------------------------------------
168   // Write
169   //---------------------------------------------------------------------
170   template<class ListItemType>
171   void Lists<ListItemType>::Write(const std::vector<std::string> fileNames, const bool verbose)
172   {
173     for (unsigned int i =0; i< fileNames.size(); i++)
174       this->at(i).Write(fileNames[i], verbose);
175   }
176
177
178 } // namespace 
179
180 #endif