Debian Codegraph @

closure-library/closure/goog/vec/quaternion_test.js

raw
1 // Copyright 2011 The Closure Library Authors. All Rights Reserved.
2 // Use of this source code is governed by the Apache License, Version 2.0.
3
4 goog.provide('goog.vec.QuaternionTest');
5 goog.setTestOnly('goog.vec.QuaternionTest');
6
7 goog.require('goog.vec.Float32Array');
8 goog.require('goog.vec.Mat3');
9 goog.require('goog.vec.Mat4');
10 goog.require('goog.vec.Quaternion');
11 goog.require('goog.vec.Vec3');
12 goog.require('goog.vec.vec3f');
13 goog.require('goog.vec.Vec4');
14 goog.require('goog.testing.jsunit');
15
16 function testCreateIdentityFloat32() {
17 var q = goog.vec.Quaternion.createIdentityFloat32();
18 assertElementsEquals([0, 0, 0, 1], q);
19 }
20
21 function testInvert() {
22 var q0 = goog.vec.Quaternion.createFloat32FromValues(1, 2, 3, 4);
23 var q1 = goog.vec.Quaternion.createFloat32();
24
25 goog.vec.Quaternion.invert(q0, q1);
26 assertElementsRoughlyEqual([1, 2, 3, 4], q0, goog.vec.EPSILON);
27 assertElementsRoughlyEqual([-0.033333, -0.066666, -0.1, 0.133333], q1,
28 goog.vec.EPSILON);
29 }
30
31 function testConjugate() {
32 var q0 = goog.vec.Quaternion.createFloat32FromValues(1, 2, 3, 4);
33 var q1 = goog.vec.Quaternion.createFloat32();
34
35 goog.vec.Quaternion.conjugate(q0, q1);
36 assertElementsEquals([1, 2, 3, 4], q0);
37 assertElementsEquals([-1, -2, -3, 4], q1);
38
39 goog.vec.Quaternion.conjugate(q1, q1);
40 assertElementsEquals([1, 2, 3, 4], q1);
41
42 // Conjugate and inverse of a normalized quaternion should be equal.
43 var q2 = goog.vec.Quaternion.createFloat32();
44 var q3 = goog.vec.Quaternion.createFloat32();
45
46 goog.vec.Quaternion.normalize(q0, q2);
47 goog.vec.Quaternion.conjugate(q2, q2);
48
49 goog.vec.Quaternion.normalize(q0, q3);
50 goog.vec.Quaternion.invert(q3, q3);
51
52 assertElementsRoughlyEqual(q2, q3, goog.vec.EPSILON);
53 }
54
55 function testConcat() {
56 var q0 = goog.vec.Quaternion.createFloat32FromValues(1, 2, 3, 4);
57 var q1 = goog.vec.Quaternion.createFloat32FromValues(2, 3, 4, 5);
58 var q2 = goog.vec.Quaternion.createFloat32();
59 goog.vec.Quaternion.concat(q0, q1, q2);
60 assertElementsEquals([12, 24, 30, 0], q2);
61
62 goog.vec.Quaternion.concat(q0, q1, q0);
63 assertElementsEquals([12, 24, 30, 0], q0);
64 }
65
66 function testMakeIdentity() {
67 var q = goog.vec.Quaternion.createFloat32FromValues(1, 2, 3, 4);
68 goog.vec.Quaternion.makeIdentity(q);
69 assertElementsEquals([0, 0, 0, 1], q);
70 }
71
72 function testRotateX() {
73 var q = goog.vec.Quaternion.createIdentityFloat32();
74 goog.vec.Quaternion.rotateX(q, Math.PI / 2, q);
75
76 var axis = goog.vec.Vec3.createFloat32();
77 var angle = goog.vec.Quaternion.toAngleAxis(q, axis);
78
79 assertElementsRoughlyEqual([1, 0, 0], axis, goog.vec.EPSILON);
80 assertRoughlyEquals(Math.PI / 2, angle, goog.vec.EPSILON);
81 }
82
83 function testRotateY() {
84 var q = goog.vec.Quaternion.createIdentityFloat32();
85 goog.vec.Quaternion.rotateY(q, Math.PI / 2, q);
86
87 var axis = goog.vec.Vec3.createFloat32();
88 var angle = goog.vec.Quaternion.toAngleAxis(q, axis);
89
90 assertElementsRoughlyEqual([0, 1, 0], axis, goog.vec.EPSILON);
91 assertRoughlyEquals(Math.PI / 2, angle, goog.vec.EPSILON);
92 }
93
94 function testRotateZ() {
95 var q = goog.vec.Quaternion.createIdentityFloat32();
96 goog.vec.Quaternion.rotateZ(q, Math.PI / 2, q);
97
98 var axis = goog.vec.Vec3.createFloat32();
99 var angle = goog.vec.Quaternion.toAngleAxis(q, axis);
100
101 assertElementsRoughlyEqual([0, 0, 1], axis, goog.vec.EPSILON);
102 assertRoughlyEquals(Math.PI / 2, angle, goog.vec.EPSILON);
103 }
104
105 function testTransformVec() {
106 var q = goog.vec.Quaternion.createIdentityFloat32();
107 goog.vec.Quaternion.rotateX(q, Math.PI / 2, q);
108
109 var v0 = goog.vec.vec3f.setFromArray(goog.vec.vec3f.create(), [0, 0, 1]);
110 var v1 = goog.vec.vec3f.create();
111
112 goog.vec.Quaternion.transformVec(v0, q, v1);
113 assertElementsRoughlyEqual([0, -1, 0], v1, goog.vec.EPSILON);
114 }
115
116 function testSlerp() {
117 var q0 = goog.vec.Quaternion.createFloat32FromValues(1, 2, 3, 4);
118 var q1 = goog.vec.Quaternion.createFloat32FromValues(5, -6, 7, -8);
119 var q2 = goog.vec.Quaternion.createFloat32();
120
121 goog.vec.Quaternion.slerp(q0, q1, 0, q2);
122 assertElementsEquals([5, -6, 7, -8], q2);
123
124 goog.vec.Quaternion.normalize(q0, q0);
125 goog.vec.Quaternion.normalize(q1, q1);
126
127 goog.vec.Quaternion.slerp(q0, q0, .5, q2);
128 assertElementsEquals(q0, q2);
129
130 goog.vec.Quaternion.slerp(q0, q1, 0, q2);
131 assertElementsEquals(q0, q2);
132
133 goog.vec.Quaternion.slerp(q0, q1, 1, q2);
134 if (q1[3] * q2[3] < 0) {
135 goog.vec.Quaternion.negate(q2, q2);
136 }
137 assertElementsEquals(q1, q2);
138
139 goog.vec.Quaternion.slerp(q0, q1, .3, q2);
140 assertElementsRoughlyEqual(
141 [-0.000501537327541, 0.4817612034640, 0.2398775270769, 0.842831337398],
142 q2, goog.vec.EPSILON);
143
144 goog.vec.Quaternion.slerp(q0, q1, .5, q2);
145 assertElementsRoughlyEqual(
146 [-0.1243045421171, 0.51879732466, 0.0107895780990, 0.845743047108],
147 q2, goog.vec.EPSILON);
148
149 goog.vec.Quaternion.slerp(q0, q1, .8, q0);
150 assertElementsRoughlyEqual(
151 [-0.291353561485, 0.506925588797, -0.3292443285721, 0.741442999653],
152 q0, goog.vec.EPSILON);
153 }
154
155 function testFromRotMatrix() {
156 var m0 = goog.vec.Mat3.createFloat32FromValues(
157 -0.408248, 0.8796528, -0.244016935,
158 -0.4082482, 0.06315623, 0.9106836,
159 0.8164965, 0.47140452, 0.3333333);
160 var q0 = goog.vec.Quaternion.createFloat32();
161 goog.vec.Quaternion.fromRotationMatrix3(m0, q0);
162 assertElementsRoughlyEqual(
163 [0.22094256606638, 0.53340203646030, 0.64777022739548, 0.497051689967954],
164 q0, goog.vec.EPSILON);
165
166 var m1 = goog.vec.Mat3.createFloat32FromValues(
167 -0.544310, 0, 0.838884, 0, 1, 0, -0.838884, 0, -0.544310);
168 var q1 = goog.vec.Quaternion.createFloat32();
169 goog.vec.Quaternion.fromRotationMatrix3(m1, q1);
170 assertElementsRoughlyEqual(
171 [0, -0.87872350215912, 0, 0.477331042289734], q1, goog.vec.EPSILON);
172
173 var m2 = goog.vec.Mat4.createFloat32FromValues(
174 -0.408248, 0.8796528, -0.244016935, 0, -0.4082482, 0.06315623, 0.9106836,
175 0, 0.8164965, 0.47140452, 0.3333333, 0, 0, 0, 0, 1);
176 var q2 = goog.vec.Quaternion.createFloat32();
177 goog.vec.Quaternion.fromRotationMatrix4(m2, q2);
178 assertElementsRoughlyEqual(
179 [0.22094256606638, 0.53340203646030, 0.64777022739548, 0.497051689967954],
180 q2, goog.vec.EPSILON);
181
182 var m3 = goog.vec.Mat4.createFloat32FromValues(
183 -0.544310, 0, 0.838884, 0, 0, 1, 0, 0, -0.838884, 0, -0.544310, 0, 0, 0,
184 0, 1);
185 var q3 = goog.vec.Quaternion.createFloat32();
186 goog.vec.Quaternion.fromRotationMatrix4(m3, q3);
187 assertElementsRoughlyEqual(
188 [0, -0.87872350215912, 0, 0.477331042289734], q3, goog.vec.EPSILON);
189
190 assertElementsRoughlyEqual(q0, q2, goog.vec.EPSILON);
191 assertElementsRoughlyEqual(q1, q3, goog.vec.EPSILON);
192 }
193
194 function testToRotMatrix() {
195 var q0 = goog.vec.Quaternion.createFloat32FromValues(
196 0.22094256606638, 0.53340203646030,
197 0.64777022739548, 0.497051689967954);
198 var m0 = goog.vec.Mat3.createFloat32();
199 goog.vec.Quaternion.toRotationMatrix3(q0, m0);
200
201 assertElementsRoughlyEqual(
202 [-0.408248, 0.8796528, -0.244016935,
203 -0.4082482, 0.06315623, 0.9106836,
204 0.8164965, 0.47140452, 0.3333333],
205 m0, goog.vec.EPSILON);
206
207 var m1 = goog.vec.Mat4.createFloat32();
208 goog.vec.Quaternion.toRotationMatrix4(q0, m1);
209
210 assertElementsRoughlyEqual(
211 [-0.408248, 0.8796528, -0.244016935, 0,
212 -0.4082482, 0.06315623, 0.9106836, 0,
213 0.8164965, 0.47140452, 0.3333333, 0,
214 0, 0, 0, 1],
215 m1, goog.vec.EPSILON);
216 }
217
218 function testToAngleAxis() {
219 // Test the identity rotation.
220 var q0 = goog.vec.Quaternion.createFloat32FromValues(0, 0, 0, 1);
221 var axis = goog.vec.Vec3.createFloat32();
222 var angle = goog.vec.Quaternion.toAngleAxis(q0, axis);
223 assertRoughlyEquals(0.0, angle, goog.vec.EPSILON);
224 assertElementsRoughlyEqual([1, 0, 0], axis, goog.vec.EPSILON);
225
226 // Check equivalent representations of the same rotation.
227 goog.vec.Quaternion.setFromValues(
228 q0, -0.288675032, 0.622008682, -0.17254543, 0.70710678);
229 angle = goog.vec.Quaternion.toAngleAxis(q0, axis);
230 assertRoughlyEquals(Math.PI / 2, angle, goog.vec.EPSILON);
231 assertElementsRoughlyEqual([-0.408248, 0.8796528, -0.244016],
232 axis, goog.vec.EPSILON);
233 // The polar opposite unit quaternion is the same rotation, so we
234 // check that the negated quaternion yields the negated angle and axis.
235 goog.vec.Quaternion.negate(q0, q0);
236 angle = goog.vec.Quaternion.toAngleAxis(q0, axis);
237 assertRoughlyEquals(-Math.PI / 2, angle, goog.vec.EPSILON);
238 assertElementsRoughlyEqual([0.408248, -0.8796528, 0.244016],
239 axis, goog.vec.EPSILON);
240
241 // Verify that the inverse rotation yields the inverse axis.
242 goog.vec.Quaternion.conjugate(q0, q0);
243 angle = goog.vec.Quaternion.toAngleAxis(q0, axis);
244 assertRoughlyEquals(-Math.PI / 2, angle, goog.vec.EPSILON);
245 assertElementsRoughlyEqual([-0.408248, 0.8796528, -0.244016],
246 axis, goog.vec.EPSILON);
247 }
248
249 function testFromAngleAxis() {
250 // Test identity rotation (zero angle or multiples of TWO_PI).
251 var angle = 0.0;
252 var axis = goog.vec.Vec3.createFloat32FromValues(-0.408248, 0.8796528,
253 -0.244016);
254 var q0 = goog.vec.Quaternion.createFloat32();
255 goog.vec.Quaternion.fromAngleAxis(angle, axis, q0);
256 assertElementsRoughlyEqual([0, 0, 0, 1], q0, goog.vec.EPSILON);
257 angle = 4 * Math.PI;
258 goog.vec.Quaternion.fromAngleAxis(angle, axis, q0);
259 assertElementsRoughlyEqual([0, 0, 0, 1], q0, goog.vec.EPSILON);
260
261 // General test of various rotations around axes of different lengths.
262 angle = Math.PI / 2;
263 goog.vec.Quaternion.fromAngleAxis(angle, axis, q0);
264 assertElementsRoughlyEqual(
265 [-0.288675032, 0.622008682, -0.17254543, 0.70710678],
266 q0, goog.vec.EPSILON);
267 // Angle multiples of TWO_PI with a scaled axis should be the same.
268 angle += 4 * Math.PI;
269 goog.vec.Vec3.scale(axis, 7.0, axis);
270 goog.vec.Quaternion.fromAngleAxis(angle, axis, q0);
271 assertElementsRoughlyEqual(
272 [-0.288675032, 0.622008682, -0.17254543, 0.70710678],
273 q0, goog.vec.EPSILON);
274 goog.vec.Vec3.setFromValues(axis, 1, 5, 8);
275 goog.vec.Quaternion.fromAngleAxis(angle, axis, q0);
276 assertElementsRoughlyEqual(
277 [0.074535599, 0.372677996, 0.596284794, 0.70710678],
278 q0, goog.vec.EPSILON);
279
280 // Check equivalent representations of the same rotation.
281 angle = Math.PI / 5;
282 goog.vec.Vec3.setFromValues(axis, 5, -2, -10);
283 goog.vec.Quaternion.fromAngleAxis(angle, axis, q0);
284 assertElementsRoughlyEqual(
285 [0.136037146, -0.0544148586, -0.27207429, 0.951056516],
286 q0, goog.vec.EPSILON);
287 // The negated angle and axis should yield the same rotation.
288 angle = -Math.PI / 5;
289 goog.vec.Vec3.negate(axis, axis);
290 goog.vec.Quaternion.fromAngleAxis(angle, axis, q0);
291 assertElementsRoughlyEqual(
292 [0.136037146, -0.0544148586, -0.27207429, 0.951056516],
293 q0, goog.vec.EPSILON);
294 }