ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UnitVectorsTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file UnitVectorsTests.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 
11 #include <limits>
12 
15 
16 using Acts::Vector3D;
17 
18 namespace {
19 constexpr auto eps = std::numeric_limits<double>::epsilon();
20 }
21 
22 BOOST_AUTO_TEST_SUITE(UnitVectors)
23 
24 BOOST_AUTO_TEST_CASE(DirectionPhiEta) {
26 
27  // along positive x
28  const auto xPos1 = makeDirectionUnitFromPhiEta(0.0, 0.0);
29  CHECK_CLOSE_REL(xPos1.norm(), 1, eps);
30  CHECK_CLOSE_REL(xPos1.dot(Vector3D(1, 0, 0)), 1, eps);
31  const auto xPos2 = makeDirectionUnitFromPhiEta(2 * M_PI, 0.0);
32  CHECK_CLOSE_REL(xPos2.norm(), 1, eps);
33  CHECK_CLOSE_REL(xPos2.dot(Vector3D(1, 0, 0)), 1, eps);
34  // along negative x
35  const auto xNeg1 = makeDirectionUnitFromPhiEta(M_PI, 0.0);
36  CHECK_CLOSE_REL(xNeg1.norm(), 1, eps);
37  CHECK_CLOSE_REL(xNeg1.dot(Vector3D(-1, 0, 0)), 1, eps);
38  const auto xNeg2 = makeDirectionUnitFromPhiEta(-M_PI, 0.0);
39  CHECK_CLOSE_REL(xNeg2.norm(), 1, eps);
40  CHECK_CLOSE_REL(xNeg2.dot(Vector3D(-1, 0, 0)), 1, eps);
41  // along positive y
42  const auto yPos1 = makeDirectionUnitFromPhiEta(M_PI_2, 0.0);
43  CHECK_CLOSE_REL(yPos1.norm(), 1, eps);
44  CHECK_CLOSE_REL(yPos1.dot(Vector3D(0, 1, 0)), 1, eps);
45  const auto yPos2 = makeDirectionUnitFromPhiEta(-3 * M_PI_2, 0.0);
46  CHECK_CLOSE_REL(yPos2.norm(), 1, eps);
47  CHECK_CLOSE_REL(yPos2.dot(Vector3D(0, 1, 0)), 1, eps);
48  // along negative y
49  const auto yNeg1 = makeDirectionUnitFromPhiEta(-M_PI_2, 0.0);
50  CHECK_CLOSE_REL(yNeg1.norm(), 1, eps);
51  CHECK_CLOSE_REL(yNeg1.dot(Vector3D(0, -1, 0)), 1, eps);
52  const auto yNeg2 = makeDirectionUnitFromPhiEta(3 * M_PI_2, 0.0);
53  CHECK_CLOSE_REL(yNeg2.norm(), 1, eps);
54  CHECK_CLOSE_REL(yNeg2.dot(Vector3D(0, -1, 0)), 1, eps);
55 
56  const auto inf = std::numeric_limits<double>::infinity();
57  // along positive z
58  const auto zPos1 = makeDirectionUnitFromPhiEta(0.0, inf);
59  CHECK_CLOSE_REL(zPos1.norm(), 1, eps);
60  CHECK_CLOSE_REL(zPos1.dot(Vector3D(0, 0, 1)), 1, eps);
61  const auto zPos2 = makeDirectionUnitFromPhiEta(M_PI_2, inf);
62  CHECK_CLOSE_REL(zPos2.norm(), 1, eps);
63  CHECK_CLOSE_REL(zPos2.dot(Vector3D(0, 0, 1)), 1, eps);
64  // along negative z
65  const auto zNeg1 = makeDirectionUnitFromPhiEta(0.0, -inf);
66  CHECK_CLOSE_REL(zNeg1.norm(), 1, eps);
67  CHECK_CLOSE_REL(zNeg1.dot(Vector3D(0, 0, -1)), 1, eps);
68  const auto zNeg2 = makeDirectionUnitFromPhiEta(M_PI_2, -inf);
69  CHECK_CLOSE_REL(zNeg2.norm(), 1, eps);
70  CHECK_CLOSE_REL(zNeg2.dot(Vector3D(0, 0, -1)), 1, eps);
71 
72  // mixed direction
73  const auto mixed1 = makeDirectionUnitFromPhiEta(M_PI_4, 1.0);
74  CHECK_CLOSE_REL(mixed1.norm(), 1, eps);
76  mixed1.dot(Vector3D(1, 1, M_SQRT2 * std::sinh(1.0)).normalized()), 1,
77  eps);
78  const auto mixed2 = makeDirectionUnitFromPhiEta(M_PI_4, -1.0);
79  CHECK_CLOSE_REL(mixed2.norm(), 1, eps);
81  mixed2.dot(Vector3D(1, 1, M_SQRT2 * std::sinh(-1.0)).normalized()), 1,
82  eps);
83  const auto mixed3 = makeDirectionUnitFromPhiEta(-M_PI_4, -1.0);
84  CHECK_CLOSE_REL(mixed3.norm(), 1, eps);
86  mixed3.dot(Vector3D(1, -1, M_SQRT2 * std::sinh(-1.0)).normalized()), 1,
87  eps);
88 }
89 
90 BOOST_AUTO_TEST_CASE(DirectionPhiTheta) {
92 
93  // along positive x
94  const auto xPos1 = makeDirectionUnitFromPhiTheta(0.0, M_PI_2);
95  CHECK_CLOSE_REL(xPos1.norm(), 1, eps);
96  CHECK_CLOSE_REL(xPos1.dot(Vector3D(1, 0, 0)), 1, eps);
97  const auto xPos2 = makeDirectionUnitFromPhiTheta(2 * M_PI, M_PI_2);
98  CHECK_CLOSE_REL(xPos2.norm(), 1, eps);
99  CHECK_CLOSE_REL(xPos2.dot(Vector3D(1, 0, 0)), 1, eps);
100  // along negative x
101  const auto xNeg1 = makeDirectionUnitFromPhiTheta(M_PI, M_PI_2);
102  CHECK_CLOSE_REL(xNeg1.norm(), 1, eps);
103  CHECK_CLOSE_REL(xNeg1.dot(Vector3D(-1, 0, 0)), 1, eps);
104  const auto xNeg2 = makeDirectionUnitFromPhiTheta(-M_PI, M_PI_2);
105  CHECK_CLOSE_REL(xNeg2.norm(), 1, eps);
106  CHECK_CLOSE_REL(xNeg2.dot(Vector3D(-1, 0, 0)), 1, eps);
107  // along positive y
108  const auto yPos1 = makeDirectionUnitFromPhiTheta(M_PI_2, M_PI_2);
109  CHECK_CLOSE_REL(yPos1.norm(), 1, eps);
110  CHECK_CLOSE_REL(yPos1.dot(Vector3D(0, 1, 0)), 1, eps);
111  const auto yPos2 = makeDirectionUnitFromPhiTheta(-3 * M_PI_2, M_PI_2);
112  CHECK_CLOSE_REL(yPos2.norm(), 1, eps);
113  CHECK_CLOSE_REL(yPos2.dot(Vector3D(0, 1, 0)), 1, eps);
114  // along negative y
115  const auto yNeg1 = makeDirectionUnitFromPhiTheta(-M_PI_2, M_PI_2);
116  CHECK_CLOSE_REL(yNeg1.norm(), 1, eps);
117  CHECK_CLOSE_REL(yNeg1.dot(Vector3D(0, -1, 0)), 1, eps);
118  const auto yNeg2 = makeDirectionUnitFromPhiTheta(3 * M_PI_2, M_PI_2);
119  CHECK_CLOSE_REL(yNeg2.norm(), 1, eps);
120  CHECK_CLOSE_REL(yNeg2.dot(Vector3D(0, -1, 0)), 1, eps);
121 
122  // along positive z
123  const auto zPos1 = makeDirectionUnitFromPhiTheta(0.0, 0.0);
124  CHECK_CLOSE_REL(zPos1.norm(), 1, eps);
125  CHECK_CLOSE_REL(zPos1.dot(Vector3D(0, 0, 1)), 1, eps);
126  const auto zPos2 = makeDirectionUnitFromPhiTheta(M_PI_2, 0.0);
127  CHECK_CLOSE_REL(zPos2.norm(), 1, eps);
128  CHECK_CLOSE_REL(zPos2.dot(Vector3D(0, 0, 1)), 1, eps);
129  // along negative z
130  const auto zNeg1 = makeDirectionUnitFromPhiTheta(0.0, M_PI);
131  CHECK_CLOSE_REL(zNeg1.norm(), 1, eps);
132  CHECK_CLOSE_REL(zNeg1.dot(Vector3D(0, 0, -1)), 1, eps);
133  const auto zNeg2 = makeDirectionUnitFromPhiTheta(M_PI_2, M_PI);
134  CHECK_CLOSE_REL(zNeg2.norm(), 1, eps);
135  CHECK_CLOSE_REL(zNeg2.dot(Vector3D(0, 0, -1)), 1, eps);
136 
137  // mixed direction
138  const auto mixed1 = makeDirectionUnitFromPhiTheta(M_PI_4, M_PI_4);
139  CHECK_CLOSE_REL(mixed1.norm(), 1, eps);
140  CHECK_CLOSE_REL(mixed1.dot(Vector3D(1, 1, M_SQRT2).normalized()), 1, eps);
141  const auto mixed2 = makeDirectionUnitFromPhiTheta(M_PI_4, 3 * M_PI_4);
142  CHECK_CLOSE_REL(mixed2.norm(), 1, eps);
143  CHECK_CLOSE_REL(mixed2.dot(Vector3D(1, 1, -M_SQRT2).normalized()), 1, eps);
144  const auto mixed3 = makeDirectionUnitFromPhiTheta(-M_PI_4, 3 * M_PI_4);
145  CHECK_CLOSE_REL(mixed3.norm(), 1, eps);
146  CHECK_CLOSE_REL(mixed3.dot(Vector3D(1, -1, -M_SQRT2).normalized()), 1, eps);
147 }
148 
149 namespace {
150 template <typename Direction, typename RefUnitU, typename RefUnitV>
151 void testCurvilinear(const Eigen::MatrixBase<Direction>& direction,
152  const Eigen::MatrixBase<RefUnitU>& refU,
153  const Eigen::MatrixBase<RefUnitV>& refV) {
154  const auto u = Acts::makeCurvilinearUnitU(direction);
155  const auto uv = Acts::makeCurvilinearUnitVectors(direction);
156  // verify normalization
157  CHECK_CLOSE_ABS(u.norm(), 1, eps);
158  CHECK_CLOSE_ABS(uv.first.norm(), 1, eps);
159  CHECK_CLOSE_ABS(uv.second.norm(), 1, eps);
160  // verify orthonormality
161  CHECK_SMALL(u.dot(direction), eps);
162  CHECK_SMALL(uv.first.dot(direction), eps);
163  CHECK_SMALL(uv.second.dot(direction), eps);
164  CHECK_SMALL(uv.first.dot(uv.second), eps);
165  // verify u is in the x-y plane
166  CHECK_SMALL(u[2], eps);
167  CHECK_SMALL(uv.first[2], eps);
168  // verify references. do not use element-wise comparison to avoid issues with
169  // small, epsilon-like differences.
170  CHECK_CLOSE_ABS(u.dot(refU), 1, eps);
171  CHECK_CLOSE_ABS(uv.first.dot(refU), 1, eps);
172  CHECK_CLOSE_ABS(uv.second.dot(refV), 1, eps);
173 }
174 } // namespace
175 
176 BOOST_AUTO_TEST_CASE(CurvilinearTransverse) {
177  // curvilinear system w/ direction in the transverse plane
178  testCurvilinear(Vector3D(1, 1, 0), Vector3D(-1, 1, 0).normalized(),
179  Vector3D(0, 0, 1).normalized());
180 }
181 
182 BOOST_AUTO_TEST_CASE(CurvilinearPositiveZ) {
183  // curvilinear system w/ direction along z
184  testCurvilinear(Vector3D(0, 0, 1), Vector3D(1, 0, 0), Vector3D(0, 1, 0));
185 }
186 
187 BOOST_AUTO_TEST_CASE(CurvilinearNegativeZ) {
188  // curvilinear system w/ direction along z
189  testCurvilinear(Vector3D(0, 0, -1), Vector3D(1, 0, 0), Vector3D(0, -1, 0));
190 }
191 
192 BOOST_AUTO_TEST_CASE(CurvilinearCloseToZ) {
193  // curvilinear system w/ direction close to z
194  testCurvilinear(Vector3D(0, 32 * eps, 1 - 32 * eps), Vector3D(-1, 0, 0),
195  Vector3D(0, -1, 32 * eps));
196 }
197 
198 BOOST_AUTO_TEST_SUITE_END()