1 #include "bbPackRecalageTransform3D3PointsBox.h"
2 #include "bbPackRecalagePackage.h"
3 namespace bbPackRecalage
6 BBTK_ADD_BLACK_BOX_TO_PACKAGE(PackRecalage,Transform3D3PointsBox)
7 BBTK_BLACK_BOX_IMPLEMENTATION(Transform3D3PointsBox,bbtk::AtomicBlackBox);
8 void Transform3D3PointsBox::Process()
10 if(!bbGetInputInX1().empty() && bbGetInputInX1().size() >= 3 && !bbGetInputInX2().empty() && bbGetInputInX2().size() >= 3)
20 //In case there is an order in the points
21 if(!bbGetInputLabels1().empty() && !bbGetInputLabels2().empty() && bbGetInputLabels1()[0].compare("1") == 0 && bbGetInputLabels1()[1].compare("2") == 0 && bbGetInputLabels1()[2].compare("3") == 0)
24 for(i = 0; i < 3; i++)
26 if(bbGetInputLabels1()[i].compare("1") == 0)
28 O1[0] = bbGetInputInX1()[i];
29 O1[1] = bbGetInputInY1()[i];
30 O1[2] = bbGetInputInZ1()[i];
32 else if(bbGetInputLabels1()[i].compare("2") == 0)
34 A1[0] = bbGetInputInX1()[i];
35 A1[1] = bbGetInputInY1()[i];
36 A1[2] = bbGetInputInZ1()[i];
38 else if(bbGetInputLabels1()[i].compare("3") == 0)
40 B1[0] = bbGetInputInX1()[i];
41 B1[1] = bbGetInputInY1()[i];
42 B1[2] = bbGetInputInZ1()[i];
45 for(i = 0; i < 3; i++)
47 if(bbGetInputLabels2()[i].compare("1") == 0)
49 O2[0] = bbGetInputInX2()[i];
50 O2[1] = bbGetInputInY2()[i];
51 O2[2] = bbGetInputInZ2()[i];
53 else if(bbGetInputLabels2()[i].compare("2") == 0)
55 A2[0] = bbGetInputInX2()[i];
56 A2[1] = bbGetInputInY2()[i];
57 A2[2] = bbGetInputInZ2()[i];
59 else if(bbGetInputLabels2()[i].compare("3") == 0)
61 B2[0] = bbGetInputInX2()[i];
62 B2[1] = bbGetInputInY2()[i];
63 B2[2] = bbGetInputInZ2()[i];
69 O1[0] = bbGetInputInX1()[0];
70 O1[1] = bbGetInputInY1()[0];
71 O1[2] = bbGetInputInZ1()[0];
73 A1[0] = bbGetInputInX1()[1];
74 A1[1] = bbGetInputInY1()[1];
75 A1[2] = bbGetInputInZ1()[1];
77 B1[0] = bbGetInputInX1()[2];
78 B1[1] = bbGetInputInY1()[2];
79 B1[2] = bbGetInputInZ1()[2];
81 O2[0] = bbGetInputInX2()[0];
82 O2[1] = bbGetInputInY2()[0];
83 O2[2] = bbGetInputInZ2()[0];
85 A2[0] = bbGetInputInX2()[1];
86 A2[1] = bbGetInputInY2()[1];
87 A2[2] = bbGetInputInZ2()[1];
89 B2[0] = bbGetInputInX2()[2];
90 B2[1] = bbGetInputInY2()[2];
91 B2[2] = bbGetInputInZ2()[2];
94 //Create the new center point
95 double centerPoint[3]; // JPR
96 centerPoint[0] = O2[0];
97 centerPoint[1] = O2[1];
98 centerPoint[2] = O2[2];
100 transformer->SetSecondTranslation(centerPoint);
102 centerPoint[0] = O1[0];
103 centerPoint[1] = O1[1];
104 centerPoint[2] = O1[2];
106 transformer->SetFirstTranslation(centerPoint);
108 //Create the first vector
110 vector1 = planes->makeVector(O1, A1);
112 //create the second vector
114 vector2 = planes->makeVector(O2, A2);
117 double* axis = planes->getCrossProduct(vector2, vector1);
120 axis = planes->getNormal(axis);
122 //Normalize the 2 vectors for calculating the dot product
123 double* vector1N = planes->getNormal(vector1);
124 double* vector2N = planes->getNormal(vector2);
127 double angle = planes->getDotProduct(vector2N, vector1N);
129 //Convert the dot product to radians
132 //Convert from Radians to Degrees (necesary for the transformation)
133 angle = vtkMath::DegreesFromRadians(angle);
135 //Set the cross product and dot product of the first rotation
136 transformer->SetRotationAxis(axis);
137 transformer->SetAngle(angle);
139 //Sets the second rotation axis (defined by the first vector) and the second angle as input
140 transformer->SetSecondRotationAxis(vector1);
142 ///////////////////////////////
143 //Second Rotation calculations start now (adjust the angle between the planes)
144 /////////////////////////////////
146 //Obtain the resultant transformation without moving the image first
147 vtkMatrix4x4* matrix = transformer->GetFirstResult()->GetMatrix();
149 double newMatrix[3][3];
152 for(i = 0; i < 3; i ++)
154 for(j = 0; j < 3; j ++)
156 newMatrix[i][j] = matrix->GetElement(i,j);
160 //Calculates the new position of the points after the first transformation
162 A2N[0] = A2[0]*newMatrix[0][0] + A2[0]*newMatrix[0][1] + A2[0]*newMatrix[0][2];
163 A2N[1] = A2[1]*newMatrix[1][0] + A2[0]*newMatrix[1][1] + A2[0]*newMatrix[1][2];
164 A2N[2] = A2[2]*newMatrix[2][0] + A2[0]*newMatrix[2][1] + A2[0]*newMatrix[2][2];
167 B2N[0] = B2[0]*newMatrix[0][0] + B2[0]*newMatrix[0][1] + B2[0]*newMatrix[0][2];
168 B2N[1] = B2[1]*newMatrix[1][0] + B2[0]*newMatrix[1][1] + B2[0]*newMatrix[1][2];
169 B2N[2] = B2[2]*newMatrix[2][0] + B2[0]*newMatrix[2][1] + B2[0]*newMatrix[2][2];
172 O2N[0] = O2[0]*newMatrix[0][0] + O2[0]*newMatrix[0][1] + O2[0]*newMatrix[0][2];
173 O2N[1] = O2[1]*newMatrix[1][0] + O2[0]*newMatrix[1][1] + O2[0]*newMatrix[1][2];
174 O2N[2] = O2[2]*newMatrix[2][0] + O2[0]*newMatrix[2][1] + O2[0]*newMatrix[2][2];
176 ///////////////////////////////////////////////////////////////////////////////
177 //Creation of the two planes for the second transformation
178 ///////////////////////////////////////////////////////////////////////////////
181 vector1A = planes->makeVector(O1, A1);
184 vector1B = planes->makeVector(O1, B1);
187 vector2AN = planes->makeVector(O2N, A2N);
190 vector2BN = planes->makeVector(O2N, B2N);
193 double* crossPlaneA = planes->getCrossProduct(vector1A, vector1B);
196 double* crossPlaneB = planes->getCrossProduct(vector2AN, vector2BN);
198 //Normalize the vectors
199 crossPlaneA = planes->getNormal(crossPlaneA);
200 crossPlaneB = planes->getNormal(crossPlaneB);
202 //Angle between the planes
203 double anglePlanes = planes->getDotProduct(crossPlaneB, crossPlaneA);
205 //Make necessary changes to the angle
206 anglePlanes = acos(anglePlanes);
208 anglePlanes = vtkMath::DegreesFromRadians(anglePlanes);
210 transformer->SetSecondAngle(-anglePlanes);
212 // The calculation of the transformations are made
215 // We get the results of transformer and set it as result of this box
216 bbSetOutputOut( transformer->GetResult() );
220 bbSetOutputOut( NULL );
223 void Transform3D3PointsBox::bbUserSetDefaultValues()
225 std::vector<int> empty;
226 std::vector<std::string> emptyString;
227 bbSetInputInX1(empty);
228 bbSetInputInY1(empty);
229 bbSetInputInZ1(empty);
230 bbSetInputInX2(empty);
231 bbSetInputInY2(empty);
232 bbSetInputInZ2(empty);
233 bbSetInputLabels1(emptyString);
234 bbSetInputLabels2(emptyString);
235 bbSetOutputOut(NULL);
237 void Transform3D3PointsBox::bbUserInitializeProcessing()
239 //We initialize the transformer
240 transformer=new Transformer3D();
242 //We initialize the plane operator
243 planes=new PlanesOperations();
245 void Transform3D3PointsBox::bbUserFinalizeProcessing()
247 //We delete the transformer
250 //We delete the plane operator
254 // EO namespace bbPackRecalage