Line data Source code
1 : #include "crpropa/Vector3.h"
2 : #include "gtest/gtest.h"
3 :
4 : namespace crpropa {
5 :
6 2 : TEST(Vector3, comparison) {
7 : EXPECT_TRUE(Vector3d(1, 2, 3) == Vector3d(1, 2, 3));
8 : EXPECT_FALSE(Vector3d(1, 2, 3) == Vector3d(2, 3, 4));
9 1 : }
10 :
11 1 : TEST(Vector3, multiplication) {
12 : Vector3d v(1);
13 : v *= 10;
14 1 : EXPECT_DOUBLE_EQ(v.x, 10);
15 1 : EXPECT_DOUBLE_EQ(v.y, 10);
16 1 : EXPECT_DOUBLE_EQ(v.z, 10);
17 : v = Vector3d(1) * Vector3d(2); // element-wise multiplication
18 1 : EXPECT_DOUBLE_EQ(v.x, 2);
19 1 : EXPECT_DOUBLE_EQ(v.y, 2);
20 1 : EXPECT_DOUBLE_EQ(v.z, 2);
21 1 : }
22 :
23 1 : TEST(Vector3, division) {
24 : Vector3d v(10);
25 : v /= 10;
26 1 : EXPECT_DOUBLE_EQ(v.x, 1);
27 1 : EXPECT_DOUBLE_EQ(v.y, 1);
28 1 : EXPECT_DOUBLE_EQ(v.z, 1);
29 : v = Vector3d(10) / Vector3d(2); // element-wise division
30 1 : EXPECT_DOUBLE_EQ(v.x, 5);
31 1 : EXPECT_DOUBLE_EQ(v.y, 5);
32 1 : EXPECT_DOUBLE_EQ(v.z, 5);
33 1 : }
34 :
35 2 : TEST(Vector3, mod) {
36 : Vector3d v(10.1, 10.2, 10.3);
37 1 : v %= 10.2;
38 1 : EXPECT_NEAR(v.x, 10.1, 1e-10); // mod doesn't preserve double precision
39 1 : EXPECT_NEAR(v.y, 0, 1e-10);
40 1 : EXPECT_NEAR(v.z, 0.1, 1e-10);
41 1 : }
42 :
43 1 : TEST(Vector3, dot) {
44 : double a = Vector3d(1, 0, 0).dot(Vector3d(0, 1, 0));
45 1 : EXPECT_DOUBLE_EQ(a, 0);
46 : double b = Vector3d(-1, 10, 2).dot(Vector3d(5, 1, -3));
47 1 : EXPECT_DOUBLE_EQ(b, -1);
48 1 : }
49 :
50 2 : TEST(Vector3, cross) {
51 : Vector3d v = Vector3d(1, 0, 0).cross(Vector3d(0, 1, 0));
52 : EXPECT_TRUE(v == Vector3d(0, 0, 1));
53 1 : }
54 :
55 2 : TEST(Vector3, angle) {
56 : // 45 degrees
57 1 : double a = Vector3d(1, 1, 0).getAngleTo(Vector3d(1, 0, 0));
58 1 : EXPECT_DOUBLE_EQ(a, 45 * M_PI / 180);
59 : // perpendicular vectors
60 1 : double b = Vector3d(0, 0, 1).getAngleTo(Vector3d(0, 0, 1));
61 1 : EXPECT_DOUBLE_EQ(b, 0);
62 1 : }
63 :
64 2 : TEST(Vector3, unitVectors) {
65 : Vector3d v = Vector3d(2, 0, 0);
66 1 : Vector3d er = v.getUnitVector();
67 1 : Vector3d et = v.getUnitVectorTheta();
68 1 : Vector3d ep = v.getUnitVectorPhi();
69 :
70 : // trigonometrical functions don't preserve double precision
71 : double eps = 1e-16;
72 1 : EXPECT_NEAR(er.x, 1, eps);
73 1 : EXPECT_NEAR(er.y, 0, eps);
74 1 : EXPECT_NEAR(er.z, 0, eps);
75 :
76 1 : EXPECT_NEAR(et.x, 0, eps);
77 1 : EXPECT_NEAR(et.y, 0, eps);
78 1 : EXPECT_NEAR(et.z, -1, eps);
79 :
80 1 : EXPECT_NEAR(ep.x, 0, eps);
81 1 : EXPECT_NEAR(ep.y, 1, eps);
82 1 : EXPECT_NEAR(ep.z, 0, eps);
83 1 : }
84 :
85 1 : TEST(Vector3, magnitude) {
86 : Vector3d v = Vector3d(1, 2, -2);
87 1 : EXPECT_DOUBLE_EQ(v.getR(), 3);
88 1 : EXPECT_DOUBLE_EQ(v.getR2(), 9);
89 1 : }
90 :
91 2 : TEST(Vector3, distance) {
92 1 : double a = Vector3d(10, 0, 10).getDistanceTo(Vector3d(10, 0, 0));
93 1 : EXPECT_DOUBLE_EQ(a, 10);
94 1 : }
95 :
96 2 : TEST(Vector3, rotation) {
97 : Vector3d v, w;
98 :
99 : // rotation by 180 degrees
100 : v = Vector3d(10, 0, 0);
101 1 : w = v.getRotated(Vector3d(0, 2, 0), M_PI);
102 1 : EXPECT_NEAR(w.x, -10, 1e-9);
103 1 : EXPECT_NEAR(w.y, 0, 1e-9);
104 1 : EXPECT_NEAR(w.z, 0, 1e-9);
105 :
106 : // rotation axis parallel to vector --> no rotation
107 : v = Vector3d(10, 0, 0);
108 1 : w = v.getRotated(Vector3d(1, 0, 0), M_PI);
109 1 : EXPECT_NEAR(w.x, 10, 1e-9);
110 1 : EXPECT_NEAR(w.y, 0, 1e-9);
111 1 : EXPECT_NEAR(w.z, 0, 1e-9);
112 :
113 : // rotation by 360 degrees
114 : v = Vector3d(5, 2, 7);
115 1 : w = v.getRotated(Vector3d(1, 8, -4), 2 * M_PI);
116 1 : EXPECT_NEAR(w.x, 5, 1e-9);
117 1 : EXPECT_NEAR(w.y, 2, 1e-9);
118 1 : EXPECT_NEAR(w.z, 7, 1e-9);
119 :
120 : // rotation by zero degrees
121 : v = Vector3d(1, 0, 0);
122 1 : w = v.getRotated(Vector3d(0,1,0), 0);
123 :
124 1 : EXPECT_NEAR(w.x, 1, 1e-9);
125 1 : EXPECT_NEAR(w.y, 0, 1e-9);
126 1 : EXPECT_NEAR(w.z, 0, 1e-9);
127 :
128 : // rotation around zero axis
129 : v = Vector3d(1, 0, 0);
130 : Vector3d a = v.cross(v);
131 1 : w = v.getRotated(a, 0);
132 :
133 1 : EXPECT_NEAR(w.x, 1, 1e-9);
134 1 : EXPECT_NEAR(w.y, 0, 1e-9);
135 1 : EXPECT_NEAR(w.z, 0, 1e-9);
136 :
137 :
138 :
139 :
140 1 : }
141 :
142 2 : TEST(Vector3, parallelPerpendicular) {
143 : Vector3d v(3, 2, 1);
144 : Vector3d w(0, 1, 0);
145 :
146 1 : Vector3d vpara = v.getParallelTo(w);
147 1 : Vector3d vperp = v.getPerpendicularTo(w);
148 :
149 1 : EXPECT_DOUBLE_EQ(vpara.x, 0);
150 1 : EXPECT_DOUBLE_EQ(vpara.y, 2);
151 1 : EXPECT_DOUBLE_EQ(vpara.z, 0);
152 1 : EXPECT_DOUBLE_EQ(vperp.x, 3);
153 1 : EXPECT_DOUBLE_EQ(vperp.y, 0);
154 1 : EXPECT_DOUBLE_EQ(vperp.z, 1);
155 1 : }
156 :
157 0 : int main(int argc, char **argv) {
158 0 : ::testing::InitGoogleTest(&argc, argv);
159 0 : return RUN_ALL_TESTS();
160 : }
161 :
162 : } // namespace crpropa
|