Debian Codegraph @

src/ImathTest/testQuatSlerp.cpp

raw
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
4 //
5
6 #ifdef NDEBUG
7 # undef NDEBUG
8 #endif
9
10 #include <ImathQuat.h>
11 #include <ImathRandom.h>
12 #include <assert.h>
13 #include <iostream>
14 #include <math.h>
15 #include "testQuatSlerp.h"
16
17 using namespace std;
18 using namespace IMATH_INTERNAL_NAMESPACE;
19
20 namespace
21 {
22
23 void
24 compareQuats (const Quatf& q1, const Quatf& q2, float e)
25 {
26 assert (equalWithAbsError (q1.v.x, q2.v.x, e));
27 assert (equalWithAbsError (q1.v.y, q2.v.y, e));
28 assert (equalWithAbsError (q1.v.z, q2.v.z, e));
29 assert (equalWithAbsError (q1.r, q2.r, e));
30 }
31
32 Quatd
33 pow (const Quatd q, int n)
34 {
35 Quatd result;
36
37 for (int i = 0; i < n; ++i)
38 result *= q;
39
40 return result;
41 }
42
43 void
44 testSlerp (const Quatf q1, const Quatf q2, int m, int n)
45 {
46 //
47 // For two quaternions, q1 and q2, and the identity quaternion, qi,
48 //
49 // slerp (q1, q2, f) == q1 * slerp (qi, q1.inverse() * q2, f); (1)
50 //
51 // In addition, for integers m and n, with m >= 0, n > 0,
52 //
53 // pow (slerp (qi, q3, m/n), n) == pow (q3, m) (2)
54 //
55 // This allows us to test if slerp (q1, q2, m/n) works correctly.
56 // Thanks to Dan Piponi for pointing this out.
57 //
58 // Note that e2, our upper bound for the numerical error in (2) is
59 // fairly large. The reason for this is that testSlerp() will be
60 // called with m and n up to 16. Taking quaternions to the 16th
61 // power amplifies any inaccuracies.
62 //
63
64 Quatf qi;
65 Quatf q3 = q1.inverse() * q2;
66 Quatf q1q2 = slerp (q1, q2, float (m) / float (n));
67 Quatf qiq3 = slerp (qi, q3, float (m) / float (n));
68 float e1 = 60 * std::numeric_limits<float>::epsilon();
69 float e2 = 600 * std::numeric_limits<float>::epsilon();
70
71 compareQuats (q1q2, q1 * qiq3, e1);
72 compareQuats (pow (qiq3, n), pow (q3, m), e2);
73 }
74
75 void
76 testSlerp (const Quatf q1, const Quatf q2)
77 {
78 const int n = 16;
79
80 for (int m = 0; m <= n; ++m)
81 testSlerp (q1, q2, m, n);
82 }
83
84 void
85 specificRotations()
86 {
87 cout << " combinations of 90-degree rotations around x, y and z" << endl;
88
89 for (int x1 = 0; x1 < 3; ++x1)
90 {
91 V3f axis1 (0, 0, 0);
92 axis1[x1] = 1;
93
94 for (int n1 = 0; n1 < 4; ++n1)
95 {
96 float angle1 = n1 * float(M_PI / 2);
97
98 Quatf q1;
99 q1.setAxisAngle (axis1, angle1);
100
101 for (int x2 = 0; x2 < 3; ++x2)
102 {
103 V3f axis2 (0, 0, 0);
104 axis2[x2] = 1;
105
106 for (int n2 = 0; n2 < 4; ++n2)
107 {
108 float angle2 = n2 * float(M_PI / 2);
109
110 Quatf q2;
111 q2.setAxisAngle (axis2, angle2);
112
113 testSlerp (q1, q2);
114 testSlerp (-q1, -q2);
115
116 if ((q1 ^ q2) < 0.99)
117 {
118 testSlerp (q1, -q2);
119 testSlerp (-q1, q2);
120 }
121 }
122 }
123 }
124 }
125 }
126
127 void
128 randomRotations()
129 {
130 cout << " random rotations" << endl;
131
132 Rand48 rand (53);
133
134 for (int i = 0; i < 10000; ++i)
135 {
136 V3f axis1 = hollowSphereRand<V3f> (rand);
137 V3f axis2 = hollowSphereRand<V3f> (rand);
138 float angle1 = float(rand.nextf (0, M_PI));
139 float angle2 = float(rand.nextf (0, M_PI));
140
141 Quatf q1, q2;
142 q1.setAxisAngle (axis1, angle1);
143 q2.setAxisAngle (axis2, angle2);
144
145 testSlerp (q1, q2);
146 testSlerp (-q1, -q2);
147
148 if ((q1 ^ q2) < 0.99)
149 {
150 testSlerp (q1, -q2);
151 testSlerp (-q1, q2);
152 }
153 }
154 }
155
156 } // namespace
157
158 void
159 testQuatSlerp()
160 {
161 cout << "Testing quaternion spherical linear interpolation" << endl;
162
163 specificRotations();
164 randomRotations();
165
166 cout << "ok\n" << endl;
167 }