EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AlignmentHelperTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file AlignmentHelperTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2020 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #include <boost/test/unit_test.hpp>
10 
14 
15 #include <cmath>
16 
17 namespace Acts {
18 namespace Test {
19 
22 BOOST_AUTO_TEST_CASE(alignment_helper_test) {
23  // (a) Test with non-identity rotation matrix
24  // Rotation angle parameters
25  const double alpha = M_PI;
26  const double beta = 0;
27  const double gamma = M_PI / 2;
28  // rotation around x axis
29  AngleAxis3D rotX(alpha, Vector3D(1., 0., 0.));
30  // rotation around y axis
31  AngleAxis3D rotY(beta, Vector3D(0., 1., 0.));
32  // rotation around z axis
33  AngleAxis3D rotZ(gamma, Vector3D(0., 0., 1.));
34  double sz = std::sin(gamma);
35  double cz = std::cos(gamma);
36  double sy = std::sin(beta);
37  double cy = std::cos(beta);
38  double sx = std::sin(alpha);
39  double cx = std::cos(alpha);
40 
41  // Calculate the expected rotation matrix for rotZ * rotY * rotX,
42  // (i.e. first rotation around x axis, then y axis, last z axis):
43  // [ cz*cy cz*sy*sx-cx*sz sz*sx+cz*cx*sy ]
44  // [ cy*sz cz*cx+sz*sy*sx cx*sz*sy-cz*sx ]
45  // [ -sy cy*sx cy*cx ]
46  RotationMatrix3D expRot = RotationMatrix3D::Zero();
47  expRot.col(0) = Vector3D(cz * cy, cy * sz, -sy);
48  expRot.col(1) =
49  Vector3D(cz * sy * sx - cx * sz, cz * cx + sz * sy * sx, cy * sx);
50  expRot.col(2) =
51  Vector3D(sz * sx + cz * cx * sy, cx * sz * sy - cz * sx, cy * cx);
52 
53  // Calculate the expected derivative of local x axis to its rotation
54  RotationMatrix3D expRotToXAxis = RotationMatrix3D::Zero();
55  expRotToXAxis.col(0) = Vector3D(0, 0, 0);
56  expRotToXAxis.col(1) = Vector3D(-cz * sy, -sz * sy, -cy);
57  expRotToXAxis.col(2) = Vector3D(-sz * cy, cz * cy, 0);
58 
59  // Calculate the expected derivative of local y axis to its rotation
60  RotationMatrix3D expRotToYAxis = RotationMatrix3D::Zero();
61  expRotToYAxis.col(0) =
62  Vector3D(cz * sy * cx + sz * sx, sz * sy * cx - cz * sx, cy * cx);
63  expRotToYAxis.col(1) = Vector3D(cz * cy * sx, sz * cy * sx, -sy * sx);
64  expRotToYAxis.col(2) =
65  Vector3D(-sz * sy * sx - cz * cx, cz * sy * sx - sz * cx, 0);
66 
67  // Calculate the expected derivative of local z axis to its rotation
68  RotationMatrix3D expRotToZAxis = RotationMatrix3D::Zero();
69  expRotToZAxis.col(0) =
70  Vector3D(sz * cx - cz * sy * sx, -sz * sy * sx - cz * cx, -cy * sx);
71  expRotToZAxis.col(1) = Vector3D(cz * cy * cx, sz * cy * cx, -sy * cx);
72  expRotToZAxis.col(2) =
73  Vector3D(cz * sx - sz * sy * cx, cz * sy * cx + sz * sx, 0);
74 
75  // Construct a transform
76  Translation3D translation(Vector3D(0., 0., 0.));
77  Transform3D transform(translation);
78  // Rotation with rotZ * rotY * rotX
79  transform *= rotZ;
80  transform *= rotY;
81  transform *= rotX;
82  // Get the rotation of the transform
83  const auto rotation = transform.rotation();
84 
85  // Check if the rotation matrix is as expected
86  CHECK_CLOSE_ABS(rotation, expRot, 1e-15);
87 
88  // Call the alignment helper to calculate the derivative of local frame axes
89  // w.r.t its rotation
90  const auto& [rotToLocalXAxis, rotToLocalYAxis, rotToLocalZAxis] =
92 
93  // Check if the derivative for local x axis is as expected
94  CHECK_CLOSE_ABS(rotToLocalXAxis, expRotToXAxis, 1e-15);
95 
96  // Check if the derivative for local y axis is as expected
97  CHECK_CLOSE_ABS(rotToLocalYAxis, expRotToYAxis, 1e-15);
98 
99  // Check if the derivative for local z axis is as expected
100  CHECK_CLOSE_ABS(rotToLocalZAxis, expRotToZAxis, 1e-15);
101 
102  // (b) Test with identity rotation matrix
103  RotationMatrix3D iRotation = RotationMatrix3D::Identity();
104 
105  // Call the alignment helper to calculate the derivative of local frame axes
106  // w.r.t its rotation
107  const auto& [irotToLocalXAxis, irotToLocalYAxis, irotToLocalZAxis] =
109 
110  // The expected derivatives
111  expRotToXAxis << 0, 0, 0, 0, 0, 1, 0, -1, 0;
112  expRotToYAxis << 0, 0, -1, 0, 0, 0, 1, 0, 0;
113  expRotToZAxis << 0, 1, 0, -1, 0, 0, 0, 0, 0;
114 
115  // Check if the derivative for local x axis is as expected
116  CHECK_CLOSE_ABS(irotToLocalXAxis, expRotToXAxis, 1e-15);
117 
118  // Check if the derivative for local y axis is as expected
119  CHECK_CLOSE_ABS(irotToLocalYAxis, expRotToYAxis, 1e-15);
120 
121  // Check if the derivative for local z axis is as expected
122  CHECK_CLOSE_ABS(irotToLocalZAxis, expRotToZAxis, 1e-15);
123 }
124 } // namespace Test
125 } // namespace Acts