]> Creatis software - creaRigidRegistration.git/blob - PackRecalage/src/bbPackRecalageTransform3D2PointsBox.cxx
Renamed Recalage Boxes
[creaRigidRegistration.git] / PackRecalage / src / bbPackRecalageTransform3D2PointsBox.cxx
1 #include "bbPackRecalageTransform3D2PointsBox.h"
2 #include "bbPackRecalagePackage.h"
3 namespace bbPackRecalage
4 {
5
6 BBTK_ADD_BLACK_BOX_TO_PACKAGE(PackRecalage,Transform3D2PointsBox)
7 BBTK_BLACK_BOX_IMPLEMENTATION(Transform3D2PointsBox,bbtk::AtomicBlackBox);
8 void Transform3D2PointsBox::Process()
9 {
10         if(!bbGetInputInX1().empty() && bbGetInputInX1().size() >= 2 && !bbGetInputInX2().empty() && bbGetInputInX2().size() >= 2)
11         {
12                 double A1[3];
13                 double O1[3];
14
15                 double A2[3];
16                 double O2[3];
17                 
18                 //In case there is an order in the points
19                 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)
20                 {
21                         int i;
22                         for(i = 0; i < 2; i++)
23                         {
24                                 if(bbGetInputLabels1()[i].compare("1") == 0)
25                                 {       
26                                         O1[0] = bbGetInputInX1()[i];
27                                         O1[1] = bbGetInputInY1()[i];
28                                         O1[2] = bbGetInputInZ1()[i];
29                                 }
30                                 else if(bbGetInputLabels1()[i].compare("2") == 0)
31                                 {
32                                         A1[0] = bbGetInputInX1()[i];
33                                         A1[1] = bbGetInputInY1()[i];
34                                         A1[2] = bbGetInputInZ1()[i];
35                                 }                               
36                         }                       
37                         for(i = 0; i < 2; i++)
38                         {
39                                 if(bbGetInputLabels2()[i].compare("1") == 0)
40                                 {       
41                                         O2[0] = bbGetInputInX2()[i];
42                                         O2[1] = bbGetInputInY2()[i];
43                                         O2[2] = bbGetInputInZ2()[i];
44                                 }
45                                 else if(bbGetInputLabels2()[i].compare("2") == 0)
46                                 {
47                                         A2[0] = bbGetInputInX2()[i];
48                                         A2[1] = bbGetInputInY2()[i];
49                                         A2[2] = bbGetInputInZ2()[i];
50                                 }                               
51                         }                               
52                 }
53                 else
54                 {
55                         O1[0] = bbGetInputInX1()[0];
56                         O1[1] = bbGetInputInY1()[0];
57                         O1[2] = bbGetInputInZ1()[0];
58
59                         A1[0] = bbGetInputInX1()[1];
60                         A1[1] = bbGetInputInY1()[1];
61                         A1[2] = bbGetInputInZ1()[1];
62
63                         O2[0] = bbGetInputInX2()[0];
64                         O2[1] = bbGetInputInY2()[0];
65                         O2[2] = bbGetInputInZ2()[0];
66
67                         A2[0] = bbGetInputInX2()[1];
68                         A2[1] = bbGetInputInY2()[1];
69                         A2[2] = bbGetInputInZ2()[1];
70                 }
71
72                 //Create the new center point
73                 double * centerPoint;
74                 centerPoint = new double[3];
75
76                 centerPoint[0] = O2[0];
77                 centerPoint[1] = O2[1];
78                 centerPoint[2] = O2[2];
79
80                 transformer->SetFirstTranslation(centerPoint);
81
82                 centerPoint[0] = O1[0];
83                 centerPoint[1] = O1[1];
84                 centerPoint[2] = O1[2];
85
86                 transformer->SetSecondTranslation(centerPoint);
87
88                 //Create the first vector
89                 double* vector1;
90                 vector1 = planes->makeVector(O1, A1);
91                 
92                 //create the second vector
93                 double* vector2;
94                 vector2 = planes->makeVector(O2, A2);
95
96                 //The rotation axis
97                 double* axis = planes->getCrossProduct(vector2, vector1);
98
99                 //Normalize the axis
100                 axis = planes->getNormal(axis);
101
102                 //Normalize the 2 vectors for calculating the dot product
103                 vector1 = planes->getNormal(vector1);
104                 vector2 = planes->getNormal(vector2);
105
106                 //Dot product
107                 double angle = planes->getDotProduct(vector2, vector1);
108
109                 //Convert the dot product to radians
110                 angle = acos(angle);
111
112                 //Convert from Radians to Degrees (necesary for the transformation)
113                 angle = vtkMath::DegreesFromRadians(angle);
114
115                 //Set the cross product and dot product of the first rotation
116                 transformer->SetRotationAxis(axis);
117                 transformer->SetAngle(angle);
118
119                 //Sets the second rotation axis (defined by the first vector) and the second angle as input
120                 transformer->SetSecondRotationAxis(vector1);
121                 transformer->SetSecondAngle(bbGetInputSecondAngle());
122
123                 // The calculation of the transformations are made
124                 transformer->Run();
125
126                 // We get the results of transformer and set it as result of this box
127                 bbSetOutputOut( transformer->GetResult() );
128         }
129         else
130         {
131                 bbSetOutputOut( NULL );
132         }
133 }
134 void Transform3D2PointsBox::bbUserSetDefaultValues()
135 {
136         std::vector<int> empty;
137         std::vector<std::string> emptyString;
138         bbSetInputInX1(empty);
139         bbSetInputInY1(empty);
140         bbSetInputInZ1(empty);
141         bbSetInputInX2(empty);
142         bbSetInputInY2(empty);
143         bbSetInputInZ2(empty);
144         bbSetInputLabels1(emptyString);
145         bbSetInputLabels2(emptyString);
146         bbSetInputSecondAngle(0);
147         bbSetOutputOut(NULL);
148 }
149 void Transform3D2PointsBox::bbUserInitializeProcessing()
150 {
151         //We initialize the transformer
152         transformer=new Transformer3D(); 
153
154         //We initialize the plane operator
155         planes=new PlanesOperations(); 
156 }
157 void Transform3D2PointsBox::bbUserFinalizeProcessing()
158 {
159         //We delete the transformer
160         delete transformer;
161
162         //We delete the plane operator
163         delete planes;
164 }
165 }
166 // EO namespace bbPackRecalage