Debian Codegraph @

src/ImathTest/testQuatSetRotation.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 "testQuatSetRotation.h"
15
16 using namespace std;
17 using namespace IMATH_INTERNAL_NAMESPACE;
18
19 namespace
20 {
21
22 void
23 testRotation (const V3f& from, const V3f& to)
24 {
25 //
26 // Build a quaternion.
27 //
28
29 Quatf Q;
30 Q.setRotation (from, to);
31 M44f M = Q.toMatrix44();
32
33 //
34 // Verify that the quaternion rotates vector from into vector to.
35 //
36
37 float e = 20 * std::numeric_limits<float>::epsilon();
38
39 V3f fromM = from * M;
40 V3f fromQ = from * Q;
41 V3f t0 = to.normalized();
42 V3f fM0 = fromM.normalized();
43 V3f fQ0 = fromQ.normalized();
44
45 assert (t0.equalWithAbsError (fM0, e));
46 assert (t0.equalWithAbsError (fQ0, e));
47
48 //
49 // Verify that the rotation axis is the cross product of from and to.
50 //
51
52 V3f f0 = from.normalized();
53
54 if (abs (f0 ^ t0) < 0.9)
55 {
56 V3f n0 = (from % to).normalized();
57 V3f n0M = n0 * M;
58
59 assert (n0.equalWithAbsError (n0M, e));
60 }
61 }
62
63 void
64 specificVectors()
65 {
66 cout << " exact 90-degree rotations" << endl;
67
68 testRotation (V3f (1, 0, 0), V3f (0, 1, 0));
69 testRotation (V3f (1, 0, 0), V3f (0, 0, 1));
70 testRotation (V3f (0, 1, 0), V3f (1, 0, 0));
71 testRotation (V3f (0, 1, 0), V3f (0, 0, 1));
72 testRotation (V3f (0, 0, 1), V3f (1, 0, 0));
73 testRotation (V3f (0, 0, 1), V3f (0, 1, 0));
74
75 cout << " exact zero-degree rotations" << endl;
76
77 testRotation (V3f (1, 0, 0), V3f (1, 0, 0));
78 testRotation (V3f (0, 1, 0), V3f (0, 1, 0));
79 testRotation (V3f (0, 0, 1), V3f (0, 0, 1));
80 testRotation (V3f (1, 2, 3), V3f (2, 4, 6));
81
82 cout << " exact 180-degree rotations" << endl;
83
84 testRotation (V3f (1, 0, 0), V3f (-1, 0, 0));
85 testRotation (V3f (0, 1, 0), V3f (0, -1, 0));
86 testRotation (V3f (0, 0, 1), V3f (0, 0, -1));
87 testRotation (V3f (1, 2, 3), V3f (-2, -4, -6));
88 testRotation (V3f (1, 3, 2), V3f (-2, -6, -4));
89 testRotation (V3f (2, 1, 3), V3f (-4, -2, -6));
90 testRotation (V3f (3, 1, 2), V3f (-6, -2, -4));
91 testRotation (V3f (2, 3, 1), V3f (-4, -6, -2));
92 testRotation (V3f (3, 2, 1), V3f (-6, -4, -2));
93
94 cout << " other angles" << endl;
95
96 testRotation (V3f (1, 2, 3), V3f (4, 5, 6));
97 testRotation (V3f (1, 2, 3), V3f (4, 6, 5));
98 testRotation (V3f (1, 2, 3), V3f (5, 4, 6));
99 testRotation (V3f (1, 2, 3), V3f (6, 4, 5));
100 testRotation (V3f (1, 2, 3), V3f (5, 6, 4));
101 testRotation (V3f (1, 2, 3), V3f (6, 5, 4));
102 testRotation (V3f (1, 2, 3), V3f (-4, -5, -6));
103 testRotation (V3f (1, 2, 3), V3f (-4, -6, -5));
104 testRotation (V3f (1, 2, 3), V3f (-5, -4, -6));
105 testRotation (V3f (1, 2, 3), V3f (-6, -4, -5));
106 testRotation (V3f (1, 2, 3), V3f (-5, -6, -4));
107 testRotation (V3f (1, 2, 3), V3f (-6, -5, -4));
108 }
109
110 void
111 randomVectors()
112 {
113 cout << " random from and to vectors" << endl;
114
115 Rand48 rand (17);
116
117 for (int i = 0; i < 500000; ++i)
118 {
119 V3f from = hollowSphereRand<V3f> (rand) * float(rand.nextf (0.1, 10.0));
120 V3f to = hollowSphereRand<V3f> (rand) * float(rand.nextf (0.1, 10.0));
121 testRotation (from, to);
122 }
123 }
124
125 void
126 nearlyEqualVectors()
127 {
128 cout << " nearly equal from and to vectors" << endl;
129
130 Rand48 rand (19);
131 float e = 100 * std::numeric_limits<float>::epsilon();
132
133 for (int i = 0; i < 500000; ++i)
134 {
135 V3f from = hollowSphereRand<V3f> (rand);
136 V3f to = from + e * hollowSphereRand<V3f> (rand);
137 testRotation (from, to);
138 }
139 }
140
141 void
142 nearlyOppositeVectors()
143 {
144 cout << " nearly opposite from and to vectors" << endl;
145
146 Rand48 rand (19);
147 float e = 100 * std::numeric_limits<float>::epsilon();
148
149 for (int i = 0; i < 500000; ++i)
150 {
151 V3f from = hollowSphereRand<V3f> (rand);
152 V3f to = -from + e * hollowSphereRand<V3f> (rand);
153 testRotation (from, to);
154 }
155 }
156
157 } // namespace
158
159 void
160 testQuatSetRotation()
161 {
162 cout << "Testing quaternion rotations" << endl;
163
164 specificVectors();
165 randomVectors();
166 nearlyEqualVectors();
167 nearlyOppositeVectors();
168
169 cout << "ok\n" << endl;
170 }