]> Creatis software - clitk.git/blob - tools/clitkPointRigidRegistration.cxx
Replace "itk::NumericTraits<PixelType>::Zero" with explicit initialisation (for macos)
[clitk.git] / tools / clitkPointRigidRegistration.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 /* =================================================
20  * @file   clitkPointRigidRegistrationGenericFilter.txx
21  * @author xxx <xxx@creatis.insa-lyon.fr>
22  * @date   29 June 2029
23  *
24  * @brief PointRigidRegistration an image
25  *
26  ===================================================*/
27
28 // clitk
29 #include "clitkPointRigidRegistration_ggo.h"
30 #include "clitkPointRigidRegistrationGenericFilter.h"
31
32 //paste from RigidRegistration
33 #include "itkImageFileReader.h"
34 #include "itkImageFileWriter.h"
35 #include "itkImage.h"
36 #include "itkVector.h"
37 #include "itkResampleImageFilter.h"
38 #include "itkLandmarkBasedTransformInitializer.h"
39 #include "itkRigid2DTransform.h"
40 #include "itkVersorRigid3DTransform.h"
41 #include <iostream>
42
43 //paste from /home/dspinczyk/dev/clitk_superbuild_Agata/Source/clitk/common/clitkTransformUtilities.h
44 #include "itkMatrix.h"
45 #include "itkArray.h"
46 #include "itkPoint.h"
47 #include "clitkImageCommon.h"
48 #include "clitkCommon.h"
49 //#define VTK_EXCLUDE_STRSTREAM_HEADERS
50 #include <vtkMatrix4x4.h>
51 #include <vtkSmartPointer.h>
52
53 //for open file for reading
54 #include "clitkIO.h"
55 #include "clitkImageCommon.h"
56 #include "clitkCommon.h"
57
58
59 //--------------------------------------------------------------------
60 int main(int argc, char * argv[])
61 {
62
63   // Init command line
64   GGO(clitkPointRigidRegistration, args_info);
65   CLITK_INIT;
66
67
68
69
70   // Iinit itk
71   // read input file
72   //open stream to reading
73   std::ifstream is;
74   clitk::openFileForReading(is, args_info.input_arg);
75
76
77
78   //reading first line of input file to chck thw dimension of data
79   double x = 0;
80   //clitk::skipComment(is);
81   is >> x;
82
83     typedef   unsigned char  PixelType;
84
85
86
87
88
89     unsigned int Dimension_temp = (unsigned int)x;
90
91
92
93         if (Dimension_temp==2)
94         {
95             const     unsigned int   Dimension = 2;
96             typedef   itk::Image< PixelType, Dimension > ImageType;
97             typedef   float          VectorComponentType;
98             typedef   itk::Vector< VectorComponentType, Dimension >    VectorType;
99             //Typ LandmarkBasedTransormInitializer
100             typedef itk::Rigid2DTransform< double > Rigid2DTransformType;
101             typedef itk::LandmarkBasedTransformInitializer< Rigid2DTransformType,ImageType, ImageType>
102                 LandmarkBasedTransformInitializerType;
103
104             LandmarkBasedTransformInitializerType::Pointer landmarkBasedTransformInitializer = LandmarkBasedTransformInitializerType::New();
105
106             //  Create source and target landmarks.
107             typedef LandmarkBasedTransformInitializerType::LandmarkPointContainer     LandmarkContainerType;
108             typedef LandmarkBasedTransformInitializerType::LandmarkPointType          LandmarkPointType;
109
110             LandmarkContainerType imageLandmarks;
111             LandmarkContainerType trackerLandmarks;
112
113             LandmarkPointType imagePoint;
114             LandmarkPointType trackerPoint;
115
116             is >> x;
117
118
119             while (is && !is.eof()) {
120                 trackerPoint[0] = x;
121                 is >> trackerPoint[1];
122
123
124                 is >> imagePoint[0];
125                 is >> imagePoint[1];
126
127                 imageLandmarks.push_back(imagePoint );
128                 trackerLandmarks.push_back(trackerPoint );
129
130
131               is >> x;
132             }
133
134             is.close();
135
136             landmarkBasedTransformInitializer->SetFixedLandmarks( imageLandmarks);
137             landmarkBasedTransformInitializer->SetMovingLandmarks( trackerLandmarks);
138
139             Rigid2DTransformType::Pointer transform = Rigid2DTransformType::New();
140
141             transform->SetIdentity();
142
143             landmarkBasedTransformInitializer->SetTransform(transform);
144             landmarkBasedTransformInitializer->InitializeTransform();
145
146             Rigid2DTransformType::MatrixType matrix = transform->GetMatrix();
147             Rigid2DTransformType::OffsetType offset = transform->GetOffset();
148
149             // Write result
150               std::ofstream out;
151               clitk::openFileForWriting(out, args_info.output_arg);
152
153               out << matrix[0][0] << ' ' << matrix[0][1] << ' ' << offset[0] << std::endl;
154               out << matrix[1][0] << ' ' << matrix[1][1] << ' ' << offset[1] << std::endl;
155               out << 0.0  << ' ' << 0.0 << ' ' << 1.0;
156               out.close();
157
158
159         }
160         else if (Dimension_temp==3)
161         {
162
163             const     unsigned int   Dimension = 3;
164             typedef   itk::Image< PixelType, Dimension > ImageType;
165             typedef   float          VectorComponentType;
166             typedef   itk::Vector< VectorComponentType, Dimension >    VectorType;
167             //Typ LandmarkBasedTransormInitializer
168             typedef itk::VersorRigid3DTransform< double > Rigid3DTransformType;
169             typedef itk::LandmarkBasedTransformInitializer< Rigid3DTransformType,ImageType, ImageType>
170                 LandmarkBasedTransformInitializerType;
171
172             LandmarkBasedTransformInitializerType::Pointer landmarkBasedTransformInitializer = LandmarkBasedTransformInitializerType::New();
173
174             //  Create source and target landmarks.
175             typedef LandmarkBasedTransformInitializerType::LandmarkPointContainer     LandmarkContainerType;
176             typedef LandmarkBasedTransformInitializerType::LandmarkPointType          LandmarkPointType;
177
178             LandmarkContainerType imageLandmarks;
179             LandmarkContainerType trackerLandmarks;
180
181             LandmarkPointType imagePoint;
182             LandmarkPointType trackerPoint;
183
184             is >> x;
185
186             while (is && !is.eof()) {
187                 trackerPoint[0] = x;
188                 is >> trackerPoint[1];
189                 is >> trackerPoint[2];
190
191                 is >> imagePoint[0];
192                 is >> imagePoint[1];
193                 is >> imagePoint[2];
194
195                 imageLandmarks.push_back(imagePoint );
196                 trackerLandmarks.push_back(trackerPoint );
197
198                 is >> x;
199             }
200
201             is.close();
202
203             landmarkBasedTransformInitializer->SetFixedLandmarks( imageLandmarks);
204             landmarkBasedTransformInitializer->SetMovingLandmarks( trackerLandmarks);
205
206             Rigid3DTransformType::Pointer transform = Rigid3DTransformType::New();
207
208             transform->SetIdentity();
209
210             landmarkBasedTransformInitializer->SetTransform(transform);
211             landmarkBasedTransformInitializer->InitializeTransform();
212
213             Rigid3DTransformType::MatrixType matrix = transform->GetMatrix();
214             Rigid3DTransformType::OffsetType offset = transform->GetOffset();
215
216             // Write result
217               std::ofstream out;
218               clitk::openFileForWriting(out, args_info.output_arg);
219
220               out << matrix[0][0] << ' ' << matrix[0][1] << ' ' << matrix[0][2] << ' '<< offset[0] << std::endl;
221               out << matrix[1][0] << ' ' << matrix[1][1] << ' ' << matrix[1][2] << ' '<< offset[1] << std::endl;
222               out << matrix[2][0] << ' ' << matrix[2][1] << ' ' << matrix[2][2] << ' '<< offset[2] << std::endl;
223               out << 0.0  << ' ' << 0.0 << ' ' << 0.0 << ' ' << 1.0;
224               out.close();
225
226
227
228         }
229         else
230         {
231               is.close();
232               return EXIT_FAILURE;
233
234         }
235
236
237
238
239
240   return EXIT_SUCCESS;
241 }// end main
242 //--------------------------------------------------------------------