]> Creatis software - clitk.git/blob - tools/clitkPointRigidRegistration.cxx
First version of PointRigidRegistration
[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   //pojedyncza zaczytana wartosc
79   double x = 0;
80   //clitk::skipComment(is);
81   is >> x;
82
83   //typ piksela - bo wykorzystujemy strukture itk::Image
84   typedef   unsigned char  PixelType;
85
86
87
88
89
90     //Ddefinicja typu obrazu: zawiera rozmiar danych, typ piksela
91     //rozmiarowosc danych
92     unsigned int Dimension_temp = (unsigned int)x;
93
94
95
96         if (Dimension_temp==2)
97         {
98             const     unsigned int   Dimension = 2;
99             typedef   itk::Image< PixelType, Dimension > ImageType;
100             typedef   float          VectorComponentType;
101             typedef   itk::Vector< VectorComponentType, Dimension >    VectorType;
102             //Typ LandmarkBasedTransormInitializer
103             typedef itk::Rigid2DTransform< double > Rigid2DTransformType;
104             typedef itk::LandmarkBasedTransformInitializer< Rigid2DTransformType,ImageType, ImageType>
105                 LandmarkBasedTransformInitializerType;
106
107             LandmarkBasedTransformInitializerType::Pointer landmarkBasedTransformInitializer = LandmarkBasedTransformInitializerType::New();
108
109             //  Create source and target landmarks.
110             typedef LandmarkBasedTransformInitializerType::LandmarkPointContainer     LandmarkContainerType;
111             typedef LandmarkBasedTransformInitializerType::LandmarkPointType          LandmarkPointType;
112
113             //bufory na zaczyt wspolrzedncyh
114             LandmarkContainerType imageLandmarks;
115             LandmarkContainerType trackerLandmarks;
116
117             //bufory na pojedyncze punkty
118             LandmarkPointType imagePoint; // dane z CT
119             LandmarkPointType trackerPoint; //dane z tracking system
120
121             is >> x;
122
123
124             while (is && !is.eof()) {
125                 trackerPoint[0] = x;
126                 is >> trackerPoint[1];
127                 //is >> trackerPoint[2];
128
129                 is >> imagePoint[0];
130                 is >> imagePoint[1];
131                 //is >> imagePoint[2];
132
133                 imageLandmarks.push_back(imagePoint );
134                 trackerLandmarks.push_back(trackerPoint );
135
136
137               is >> x;
138             }
139
140             is.close();
141
142             landmarkBasedTransformInitializer->SetFixedLandmarks( imageLandmarks);
143             landmarkBasedTransformInitializer->SetMovingLandmarks( trackerLandmarks);
144
145             Rigid2DTransformType::Pointer transform = Rigid2DTransformType::New();
146
147             transform->SetIdentity();
148
149             // Versor rotacji i macierz translacji
150             landmarkBasedTransformInitializer->SetTransform(transform);
151             landmarkBasedTransformInitializer->InitializeTransform();
152
153             Rigid2DTransformType::MatrixType matrix = transform->GetMatrix();
154             Rigid2DTransformType::OffsetType offset = transform->GetOffset();
155
156             // Write result
157               std::ofstream out;
158               clitk::openFileForWriting(out, args_info.output_arg);
159
160               out << matrix[0][0] << ' ' << matrix[0][1] << ' ' << offset[0] << std::endl;
161               out << matrix[1][0] << ' ' << matrix[1][1] << ' ' << offset[1] << std::endl;
162               out << 0.0  << ' ' << 0.0 << ' ' << 1.0;
163               out.close();
164
165
166         }
167         else if (Dimension_temp==3)
168         {
169
170             std::cout << "to ja";
171             const     unsigned int   Dimension = 3;
172             typedef   itk::Image< PixelType, Dimension > ImageType;
173             typedef   float          VectorComponentType;
174             typedef   itk::Vector< VectorComponentType, Dimension >    VectorType;
175             //Typ LandmarkBasedTransormInitializer
176             typedef itk::VersorRigid3DTransform< double > Rigid3DTransformType;
177             typedef itk::LandmarkBasedTransformInitializer< Rigid3DTransformType,ImageType, ImageType>
178                 LandmarkBasedTransformInitializerType;
179
180             LandmarkBasedTransformInitializerType::Pointer landmarkBasedTransformInitializer = LandmarkBasedTransformInitializerType::New();
181
182             //  Create source and target landmarks.
183             typedef LandmarkBasedTransformInitializerType::LandmarkPointContainer     LandmarkContainerType;
184             typedef LandmarkBasedTransformInitializerType::LandmarkPointType          LandmarkPointType;
185
186             //bufory na zaczyt wspolrzedncyh
187             LandmarkContainerType imageLandmarks;
188             LandmarkContainerType trackerLandmarks;
189
190             //bufory na pojedyncze punkty
191             LandmarkPointType imagePoint; // dane z CT
192             LandmarkPointType trackerPoint; //dane z tracking system
193
194             is >> x;
195
196             while (is && !is.eof()) {
197                 trackerPoint[0] = x;
198                 is >> trackerPoint[1];
199                 is >> trackerPoint[2];
200
201                 is >> imagePoint[0];
202                 is >> imagePoint[1];
203                 is >> imagePoint[2];
204
205                 imageLandmarks.push_back(imagePoint );
206                 trackerLandmarks.push_back(trackerPoint );
207
208                 is >> x;
209             }
210
211             is.close();
212
213             landmarkBasedTransformInitializer->SetFixedLandmarks( imageLandmarks);
214             landmarkBasedTransformInitializer->SetMovingLandmarks( trackerLandmarks);
215
216             Rigid3DTransformType::Pointer transform = Rigid3DTransformType::New();
217
218             transform->SetIdentity();
219
220             // Versor rotacji i macierz translacji
221             landmarkBasedTransformInitializer->SetTransform(transform);
222             landmarkBasedTransformInitializer->InitializeTransform();
223
224             Rigid3DTransformType::MatrixType matrix = transform->GetMatrix();
225             Rigid3DTransformType::OffsetType offset = transform->GetOffset();
226
227             // Write result
228               std::ofstream out;
229               clitk::openFileForWriting(out, args_info.output_arg);
230
231               out << matrix[0][0] << ' ' << matrix[0][1] << ' ' << matrix[0][2] << ' '<< offset[0] << std::endl;
232               out << matrix[1][0] << ' ' << matrix[1][1] << ' ' << matrix[1][2] << ' '<< offset[1] << std::endl;
233               out << matrix[2][0] << ' ' << matrix[2][1] << ' ' << matrix[2][2] << ' '<< offset[2] << std::endl;
234               out << 0.0  << ' ' << 0.0 << ' ' << 0.0 << ' ' << 1.0;
235               out.close();
236
237
238
239         }
240         else
241         {
242               is.close();
243               return EXIT_FAILURE;
244
245         }
246
247
248
249
250
251   return EXIT_SUCCESS;
252 }// end main
253 //--------------------------------------------------------------------