]> Creatis software - creaRigidRegistration.git/blob - PackRecalage/src/bbPackRecalageTransform3D2PointsBox.cxx
Feature #1766 Add licence terms for all files.
[creaRigidRegistration.git] / PackRecalage / src / bbPackRecalageTransform3D2PointsBox.cxx
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 #
8 #  This software is governed by the CeCILL-B license under French law and 
9 #  abiding by the rules of distribution of free software. You can  use, 
10 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
11 #  license as circulated by CEA, CNRS and INRIA at the following URL 
12 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
13 #  or in the file LICENSE.txt.
14 #
15 #  As a counterpart to the access to the source code and  rights to copy,
16 #  modify and redistribute granted by the license, users are provided only
17 #  with a limited warranty  and the software's author,  the holder of the
18 #  economic rights,  and the successive licensors  have only  limited
19 #  liability. 
20 #
21 #  The fact that you are presently reading this means that you have had
22 #  knowledge of the CeCILL-B license and that you accept its terms.
23 # ------------------------------------------------------------------------   
24 */
25
26
27 #include "bbPackRecalageTransform3D2PointsBox.h"
28 #include "bbPackRecalagePackage.h"
29 namespace bbPackRecalage
30 {
31
32 BBTK_ADD_BLACK_BOX_TO_PACKAGE(PackRecalage,Transform3D2PointsBox)
33 BBTK_BLACK_BOX_IMPLEMENTATION(Transform3D2PointsBox,bbtk::AtomicBlackBox);
34 void Transform3D2PointsBox::Process()
35 {
36         if(!bbGetInputInX1().empty() && bbGetInputInX1().size() >= 2 && !bbGetInputInX2().empty() && bbGetInputInX2().size() >= 2)
37         {
38                 double A1[3];
39                 double O1[3];
40
41                 double A2[3];
42                 double O2[3];
43                 
44                 //In case there is an order in the points
45                 if(!bbGetInputLabels1().empty() && !bbGetInputLabels2().empty() && bbGetInputLabels1()[0].compare("1") == 0 && bbGetInputLabels1()[1].compare("2") == 0 && bbGetInputLabels2()[0].compare("1") == 0 && bbGetInputLabels2()[1].compare("2") == 0)
46                 {
47                         int i;
48                         for(i = 0; i < 2; i++)
49                         {
50                                 if(bbGetInputLabels1()[i].compare("1") == 0)
51                                 {       
52                                         O1[0] = bbGetInputInX1()[i];
53                                         O1[1] = bbGetInputInY1()[i];
54                                         O1[2] = bbGetInputInZ1()[i];
55                                 }
56                                 else if(bbGetInputLabels1()[i].compare("2") == 0)
57                                 {
58                                         A1[0] = bbGetInputInX1()[i];
59                                         A1[1] = bbGetInputInY1()[i];
60                                         A1[2] = bbGetInputInZ1()[i];
61                                 }                               
62                         }                       
63                         for(i = 0; i < 2; i++)
64                         {
65                                 if(bbGetInputLabels2()[i].compare("1") == 0)
66                                 {       
67                                         O2[0] = bbGetInputInX2()[i];
68                                         O2[1] = bbGetInputInY2()[i];
69                                         O2[2] = bbGetInputInZ2()[i];
70                                 }
71                                 else if(bbGetInputLabels2()[i].compare("2") == 0)
72                                 {
73                                         A2[0] = bbGetInputInX2()[i];
74                                         A2[1] = bbGetInputInY2()[i];
75                                         A2[2] = bbGetInputInZ2()[i];
76                                 }                               
77                         }                               
78                 }
79                 else
80                 {
81                         O1[0] = bbGetInputInX1()[0];
82                         O1[1] = bbGetInputInY1()[0];
83                         O1[2] = bbGetInputInZ1()[0];
84
85                         A1[0] = bbGetInputInX1()[1];
86                         A1[1] = bbGetInputInY1()[1];
87                         A1[2] = bbGetInputInZ1()[1];
88
89                         O2[0] = bbGetInputInX2()[0];
90                         O2[1] = bbGetInputInY2()[0];
91                         O2[2] = bbGetInputInZ2()[0];
92
93                         A2[0] = bbGetInputInX2()[1];
94                         A2[1] = bbGetInputInY2()[1];
95                         A2[2] = bbGetInputInZ2()[1];
96                 }
97
98                 //Create the new center point
99                 double * centerPoint;
100                 centerPoint = new double[3];
101
102                 centerPoint[0] = O2[0];
103                 centerPoint[1] = O2[1];
104                 centerPoint[2] = O2[2];
105
106                 transformer->SetFirstTranslation(centerPoint);
107
108                 centerPoint[0] = O1[0];
109                 centerPoint[1] = O1[1];
110                 centerPoint[2] = O1[2];
111
112                 transformer->SetSecondTranslation(centerPoint);
113
114                 //Create the first vector
115                 double* vector1;
116                 vector1 = planes->makeVector(O1, A1);
117                 
118                 //create the second vector
119                 double* vector2;
120                 vector2 = planes->makeVector(O2, A2);
121
122                 //The rotation axis
123                 double* axis = planes->getCrossProduct(vector2, vector1);
124
125                 //Normalize the axis
126                 axis = planes->getNormal(axis);
127
128                 //Normalize the 2 vectors for calculating the dot product
129                 vector1 = planes->getNormal(vector1);
130                 vector2 = planes->getNormal(vector2);
131
132                 //Dot product
133                 double angle = planes->getDotProduct(vector2, vector1);
134
135                 //Convert the dot product to radians
136                 angle = acos(angle);
137
138                 //Convert from Radians to Degrees (necesary for the transformation)
139                 angle = vtkMath::DegreesFromRadians(angle);
140
141                 //Set the cross product and dot product of the first rotation
142                 transformer->SetRotationAxis(axis);
143                 transformer->SetAngle(angle);
144
145                 //Sets the second rotation axis (defined by the first vector) and the second angle as input
146                 transformer->SetSecondRotationAxis(vector1);
147                 transformer->SetSecondAngle(bbGetInputSecondAngle());
148
149                 // The calculation of the transformations are made
150                 transformer->Run();
151
152                 // We get the results of transformer and set it as result of this box
153                 bbSetOutputOut( transformer->GetResult() );
154         }
155         else
156         {
157                 bbSetOutputOut( NULL );
158         }
159 }
160 void Transform3D2PointsBox::bbUserSetDefaultValues()
161 {
162         std::vector<int> empty;
163         std::vector<std::string> emptyString;
164         bbSetInputInX1(empty);
165         bbSetInputInY1(empty);
166         bbSetInputInZ1(empty);
167         bbSetInputInX2(empty);
168         bbSetInputInY2(empty);
169         bbSetInputInZ2(empty);
170         bbSetInputLabels1(emptyString);
171         bbSetInputLabels2(emptyString);
172         bbSetInputSecondAngle(0);
173         bbSetOutputOut(NULL);
174 }
175 void Transform3D2PointsBox::bbUserInitializeProcessing()
176 {
177         //We initialize the transformer
178         transformer=new Transformer3D(); 
179
180         //We initialize the plane operator
181         planes=new PlanesOperations(); 
182 }
183 void Transform3D2PointsBox::bbUserFinalizeProcessing()
184 {
185         //We delete the transformer
186         delete transformer;
187
188         //We delete the plane operator
189         delete planes;
190 }
191 }
192 // EO namespace bbPackRecalage