Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:11:27

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2020 CERN for the benefit of the Acts project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
0008 
0009 #include <boost/test/unit_test.hpp>
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Surfaces/detail/AlignmentHelper.hpp"
0013 #include "Acts/Tests/CommonHelpers/FloatComparisons.hpp"
0014 
0015 #include <algorithm>
0016 #include <cmath>
0017 #include <utility>
0018 
0019 namespace Acts::Test {
0020 
0021 /// Test for rotation matrix and calculation of derivative of rotated x/y/z axis
0022 /// w.r.t. rotation parameters
0023 BOOST_AUTO_TEST_CASE(alignment_helper_test) {
0024   // (a) Test with non-identity rotation matrix
0025   // Rotation angle parameters
0026   const double alpha = M_PI;
0027   const double beta = 0;
0028   const double gamma = M_PI / 2;
0029   // rotation around x axis
0030   AngleAxis3 rotX(alpha, Vector3(1., 0., 0.));
0031   // rotation around y axis
0032   AngleAxis3 rotY(beta, Vector3(0., 1., 0.));
0033   // rotation around z axis
0034   AngleAxis3 rotZ(gamma, Vector3(0., 0., 1.));
0035   double sz = std::sin(gamma);
0036   double cz = std::cos(gamma);
0037   double sy = std::sin(beta);
0038   double cy = std::cos(beta);
0039   double sx = std::sin(alpha);
0040   double cx = std::cos(alpha);
0041 
0042   // Calculate the expected rotation matrix for rotZ * rotY * rotX,
0043   // (i.e. first rotation around x axis, then y axis, last z axis):
0044   // [ cz*cy  cz*sy*sx-cx*sz  sz*sx+cz*cx*sy ]
0045   // [ cy*sz  cz*cx+sz*sy*sx  cx*sz*sy-cz*sx ]
0046   // [ -sy    cy*sx           cy*cx          ]
0047   RotationMatrix3 expRot = RotationMatrix3::Zero();
0048   expRot.col(0) = Vector3(cz * cy, cy * sz, -sy);
0049   expRot.col(1) =
0050       Vector3(cz * sy * sx - cx * sz, cz * cx + sz * sy * sx, cy * sx);
0051   expRot.col(2) =
0052       Vector3(sz * sx + cz * cx * sy, cx * sz * sy - cz * sx, cy * cx);
0053 
0054   // Calculate the expected derivative of local x axis to its rotation
0055   RotationMatrix3 expRotToXAxis = RotationMatrix3::Zero();
0056   expRotToXAxis.col(0) = Vector3(0, 0, 0);
0057   expRotToXAxis.col(1) = Vector3(-cz * sy, -sz * sy, -cy);
0058   expRotToXAxis.col(2) = Vector3(-sz * cy, cz * cy, 0);
0059 
0060   // Calculate the expected derivative of local y axis to its rotation
0061   RotationMatrix3 expRotToYAxis = RotationMatrix3::Zero();
0062   expRotToYAxis.col(0) =
0063       Vector3(cz * sy * cx + sz * sx, sz * sy * cx - cz * sx, cy * cx);
0064   expRotToYAxis.col(1) = Vector3(cz * cy * sx, sz * cy * sx, -sy * sx);
0065   expRotToYAxis.col(2) =
0066       Vector3(-sz * sy * sx - cz * cx, cz * sy * sx - sz * cx, 0);
0067 
0068   // Calculate the expected derivative of local z axis to its rotation
0069   RotationMatrix3 expRotToZAxis = RotationMatrix3::Zero();
0070   expRotToZAxis.col(0) =
0071       Vector3(sz * cx - cz * sy * sx, -sz * sy * sx - cz * cx, -cy * sx);
0072   expRotToZAxis.col(1) = Vector3(cz * cy * cx, sz * cy * cx, -sy * cx);
0073   expRotToZAxis.col(2) =
0074       Vector3(cz * sx - sz * sy * cx, cz * sy * cx + sz * sx, 0);
0075 
0076   // Construct a transform
0077   Translation3 translation(Vector3(0., 0., 0.));
0078   Transform3 transform(translation);
0079   // Rotation with rotZ * rotY * rotX
0080   transform *= rotZ;
0081   transform *= rotY;
0082   transform *= rotX;
0083   // Get the rotation of the transform
0084   const auto rotation = transform.rotation();
0085 
0086   // Check if the rotation matrix is as expected
0087   CHECK_CLOSE_ABS(rotation, expRot, 1e-15);
0088 
0089   // Call the alignment helper to calculate the derivative of local frame axes
0090   // w.r.t its rotation
0091   const auto& [rotToLocalXAxis, rotToLocalYAxis, rotToLocalZAxis] =
0092       detail::rotationToLocalAxesDerivative(rotation);
0093 
0094   // Check if the derivative for local x axis is as expected
0095   CHECK_CLOSE_ABS(rotToLocalXAxis, expRotToXAxis, 1e-15);
0096 
0097   // Check if the derivative for local y axis is as expected
0098   CHECK_CLOSE_ABS(rotToLocalYAxis, expRotToYAxis, 1e-15);
0099 
0100   // Check if the derivative for local z axis is as expected
0101   CHECK_CLOSE_ABS(rotToLocalZAxis, expRotToZAxis, 1e-15);
0102 
0103   // (b) Test with identity rotation matrix
0104   RotationMatrix3 iRotation = RotationMatrix3::Identity();
0105 
0106   // Call the alignment helper to calculate the derivative of local frame axes
0107   // w.r.t its rotation
0108   const auto& [irotToLocalXAxis, irotToLocalYAxis, irotToLocalZAxis] =
0109       detail::rotationToLocalAxesDerivative(iRotation);
0110 
0111   // The expected derivatives
0112   expRotToXAxis << 0, 0, 0, 0, 0, 1, 0, -1, 0;
0113   expRotToYAxis << 0, 0, -1, 0, 0, 0, 1, 0, 0;
0114   expRotToZAxis << 0, 1, 0, -1, 0, 0, 0, 0, 0;
0115 
0116   // Check if the derivative for local x axis is as expected
0117   CHECK_CLOSE_ABS(irotToLocalXAxis, expRotToXAxis, 1e-15);
0118 
0119   // Check if the derivative for local y axis is as expected
0120   CHECK_CLOSE_ABS(irotToLocalYAxis, expRotToYAxis, 1e-15);
0121 
0122   // Check if the derivative for local z axis is as expected
0123   CHECK_CLOSE_ABS(irotToLocalZAxis, expRotToZAxis, 1e-15);
0124 }
0125 }  // namespace Acts::Test