GCC Code Coverage Report


Directory: ./
File: openvdb/openvdb/unittest/TestQuat.cc
Date: 2022-07-25 17:40:05
Exec Total Coverage
Lines: 69 69 100.0%
Functions: 7 7 100.0%
Branches: 18 174 10.3%

Line Branch Exec Source
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3
4 #include <openvdb/Exceptions.h>
5 #include <openvdb/math/Math.h>
6 #include <openvdb/math/Quat.h>
7 #include <openvdb/math/Mat4.h>
8
9 #include <gtest/gtest.h>
10
11
12 using namespace openvdb::math;
13
14 7 class TestQuat: public ::testing::Test
15 {
16 };
17
18
19
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 TEST_F(TestQuat, testConstructor)
20 {
21 {
22 Quat<float> qq(1.23f, 2.34f, 3.45f, 4.56f);
23 EXPECT_TRUE( isExactlyEqual(qq.x(), 1.23f) );
24 EXPECT_TRUE( isExactlyEqual(qq.y(), 2.34f) );
25 EXPECT_TRUE( isExactlyEqual(qq.z(), 3.45f) );
26 EXPECT_TRUE( isExactlyEqual(qq.w(), 4.56f) );
27 }
28
29 {
30 float a[] = { 1.23f, 2.34f, 3.45f, 4.56f };
31 Quat<float> qq(a);
32 EXPECT_TRUE( isExactlyEqual(qq.x(), 1.23f) );
33 EXPECT_TRUE( isExactlyEqual(qq.y(), 2.34f) );
34 EXPECT_TRUE( isExactlyEqual(qq.z(), 3.45f) );
35 EXPECT_TRUE( isExactlyEqual(qq.w(), 4.56f) );
36 }
37 1 }
38
39
40
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 TEST_F(TestQuat, testAxisAngle)
41 {
42 float TOL = 1e-6f;
43
44 Quat<float> q1(1.0f, 2.0f, 3.0f, 4.0f);
45 Quat<float> q2(1.2f, 2.3f, 3.4f, 4.5f);
46
47 Vec3s v(1, 2, 3);
48 1 v.normalize();
49 float a = float(M_PI / 4.f);
50
51 1 Quat<float> q(v,a);
52 1 float b = q.angle();
53 1 Vec3s vv = q.axis();
54
55
1/16
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
1 EXPECT_TRUE( isApproxEqual(a, b, TOL) );
56
1/16
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
1 EXPECT_TRUE( v.eq(vv, TOL) );
57
58 1 q1.setAxisAngle(v,a);
59 1 b = q1.angle();
60 1 vv = q1.axis();
61
1/16
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
1 EXPECT_TRUE( isApproxEqual(a, b, TOL) );
62
1/16
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
1 EXPECT_TRUE( v.eq(vv, TOL) );
63 1 }
64
65
66
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 TEST_F(TestQuat, testOpPlus)
67 {
68 Quat<float> q1(1.0f, 2.0f, 3.0f, 4.0f);
69 Quat<float> q2(1.2f, 2.3f, 3.4f, 4.5f);
70
71 Quat<float> q = q1 + q2;
72
73 float
74 x=q1.x()+q2.x(), y=q1.y()+q2.y(), z=q1.z()+q2.z(), w=q1.w()+q2.w();
75 EXPECT_TRUE( isExactlyEqual(q.x(), x) );
76 EXPECT_TRUE( isExactlyEqual(q.y(), y) );
77 EXPECT_TRUE( isExactlyEqual(q.z(), z) );
78 EXPECT_TRUE( isExactlyEqual(q.w(), w) );
79
80 q = q1;
81 q += q2;
82 EXPECT_TRUE( isExactlyEqual(q.x(), x) );
83 EXPECT_TRUE( isExactlyEqual(q.y(), y) );
84 EXPECT_TRUE( isExactlyEqual(q.z(), z) );
85 EXPECT_TRUE( isExactlyEqual(q.w(), w) );
86
87 q.add(q1,q2);
88 EXPECT_TRUE( isExactlyEqual(q.x(), x) );
89 EXPECT_TRUE( isExactlyEqual(q.y(), y) );
90 EXPECT_TRUE( isExactlyEqual(q.z(), z) );
91 EXPECT_TRUE( isExactlyEqual(q.w(), w) );
92 1 }
93
94
95
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 TEST_F(TestQuat, testOpMinus)
96 {
97 Quat<float> q1(1.0f, 2.0f, 3.0f, 4.0f);
98 Quat<float> q2(1.2f, 2.3f, 3.4f, 4.5f);
99
100 Quat<float> q = q1 - q2;
101
102 float
103 x=q1.x()-q2.x(), y=q1.y()-q2.y(), z=q1.z()-q2.z(), w=q1.w()-q2.w();
104 EXPECT_TRUE( isExactlyEqual(q.x(), x) );
105 EXPECT_TRUE( isExactlyEqual(q.y(), y) );
106 EXPECT_TRUE( isExactlyEqual(q.z(), z) );
107 EXPECT_TRUE( isExactlyEqual(q.w(), w) );
108
109 q = q1;
110 q -= q2;
111 EXPECT_TRUE( isExactlyEqual(q.x(), x) );
112 EXPECT_TRUE( isExactlyEqual(q.y(), y) );
113 EXPECT_TRUE( isExactlyEqual(q.z(), z) );
114 EXPECT_TRUE( isExactlyEqual(q.w(), w) );
115
116 q.sub(q1,q2);
117 EXPECT_TRUE( isExactlyEqual(q.x(), x) );
118 EXPECT_TRUE( isExactlyEqual(q.y(), y) );
119 EXPECT_TRUE( isExactlyEqual(q.z(), z) );
120 EXPECT_TRUE( isExactlyEqual(q.w(), w) );
121 1 }
122
123
124
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 TEST_F(TestQuat, testOpMultiply)
125 {
126 Quat<float> q1(1.0f, 2.0f, 3.0f, 4.0f);
127 Quat<float> q2(1.2f, 2.3f, 3.4f, 4.5f);
128
129 Quat<float> q = q1 * 1.5f;
130
131 EXPECT_TRUE( isExactlyEqual(q.x(), float(1.5f)*q1.x()) );
132 EXPECT_TRUE( isExactlyEqual(q.y(), float(1.5f)*q1.y()) );
133 EXPECT_TRUE( isExactlyEqual(q.z(), float(1.5f)*q1.z()) );
134 EXPECT_TRUE( isExactlyEqual(q.w(), float(1.5f)*q1.w()) );
135
136 q = q1;
137 q *= 1.5f;
138 EXPECT_TRUE( isExactlyEqual(q.x(), float(1.5f)*q1.x()) );
139 EXPECT_TRUE( isExactlyEqual(q.y(), float(1.5f)*q1.y()) );
140 EXPECT_TRUE( isExactlyEqual(q.z(), float(1.5f)*q1.z()) );
141 EXPECT_TRUE( isExactlyEqual(q.w(), float(1.5f)*q1.w()) );
142
143 q.scale(1.5f, q1);
144 EXPECT_TRUE( isExactlyEqual(q.x(), float(1.5f)*q1.x()) );
145 EXPECT_TRUE( isExactlyEqual(q.y(), float(1.5f)*q1.y()) );
146 EXPECT_TRUE( isExactlyEqual(q.z(), float(1.5f)*q1.z()) );
147 EXPECT_TRUE( isExactlyEqual(q.w(), float(1.5f)*q1.w()) );
148 1 }
149
150
151
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 TEST_F(TestQuat, testInvert)
152 {
153 float TOL = 1e-6f;
154
155 Quat<float> q1(1.0f, 2.0f, 3.0f, 4.0f);
156 Quat<float> q2(1.2f, 2.3f, 3.4f, 4.5f);
157
158
159 1 q1 = q2;
160 1 q2 = q2.inverse();
161
162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 Quat<float> q = q1*q2;
163
164
1/16
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
1 EXPECT_TRUE( q.eq( Quat<float>(0,0,0,1), TOL ) );
165
166 1 q1.normalize();
167 1 q2 = q1.conjugate();
168 1 q = q1*q2;
169
1/16
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
1 EXPECT_TRUE( q.eq( Quat<float>(0,0,0,1), TOL ) );
170 1 }
171
172
173
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 TEST_F(TestQuat, testEulerAngles)
174 {
175
176 {
177 double TOL = 1e-7;
178
179 Mat4d rx, ry, rz;
180 const double angle1 = 20. * M_PI / 180.;
181 const double angle2 = 64. * M_PI / 180.;
182 const double angle3 = 125. *M_PI / 180.;
183 1 rx.setToRotation(Vec3d(1,0,0), angle1);
184 1 ry.setToRotation(Vec3d(0,1,0), angle2);
185 1 rz.setToRotation(Vec3d(0,0,1), angle3);
186
187 1 Mat4d r = rx * ry * rz;
188
189 1 const Quat<double> rot(r.getMat3());
190 1 Vec3d result = rot.eulerAngles(ZYX_ROTATION);
191
192 1 rx.setToRotation(Vec3d(1,0,0), result[0]);
193 1 ry.setToRotation(Vec3d(0,1,0), result[1]);
194 1 rz.setToRotation(Vec3d(0,0,1), result[2]);
195
196 1 Mat4d rtest = rx * ry * rz;
197
198
1/16
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
1 EXPECT_TRUE(r.eq(rtest, TOL));
199 }
200
201 {
202 double TOL = 1e-7;
203
204 Mat4d rx, ry, rz;
205 const double angle1 = 20. * M_PI / 180.;
206 const double angle2 = 64. * M_PI / 180.;
207 const double angle3 = 125. *M_PI / 180.;
208 1 rx.setToRotation(Vec3d(1,0,0), angle1);
209 1 ry.setToRotation(Vec3d(0,1,0), angle2);
210 1 rz.setToRotation(Vec3d(0,0,1), angle3);
211
212 1 Mat4d r = rz * ry * rx;
213
214 1 const Quat<double> rot(r.getMat3());
215 1 Vec3d result = rot.eulerAngles(XYZ_ROTATION);
216
217 1 rx.setToRotation(Vec3d(1,0,0), result[0]);
218 1 ry.setToRotation(Vec3d(0,1,0), result[1]);
219 1 rz.setToRotation(Vec3d(0,0,1), result[2]);
220
221 1 Mat4d rtest = rz * ry * rx;
222
223
1/16
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
1 EXPECT_TRUE(r.eq(rtest, TOL));
224 }
225
226 {
227 double TOL = 1e-7;
228
229 Mat4d rx, ry, rz;
230 const double angle1 = 20. * M_PI / 180.;
231 const double angle2 = 64. * M_PI / 180.;
232 const double angle3 = 125. *M_PI / 180.;
233 1 rx.setToRotation(Vec3d(1,0,0), angle1);
234 1 ry.setToRotation(Vec3d(0,1,0), angle2);
235 1 rz.setToRotation(Vec3d(0,0,1), angle3);
236
237 1 Mat4d r = rz * rx * ry;
238
239 1 const Quat<double> rot(r.getMat3());
240 1 Vec3d result = rot.eulerAngles(YXZ_ROTATION);
241
242 1 rx.setToRotation(Vec3d(1,0,0), result[0]);
243 1 ry.setToRotation(Vec3d(0,1,0), result[1]);
244 1 rz.setToRotation(Vec3d(0,0,1), result[2]);
245
246 1 Mat4d rtest = rz * rx * ry;
247
248
1/16
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
1 EXPECT_TRUE(r.eq(rtest, TOL));
249 }
250
251 {
252 const Quat<float> rot(X_AXIS, 1.0);
253 1 Vec3s result = rot.eulerAngles(XZY_ROTATION);
254
1/14
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
1 EXPECT_EQ(result, Vec3s(1,0,0));
255 }
256
257 1 }
258