aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/vec/quaternion.js
blob: 35d40b937e3b5f53a7f2807dc17f765e76ff6a55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// Copyright 2011 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


/**
 * @fileoverview Implements quaternions and their conversion functions. In this
 * implementation, quaternions are represented as 4 element vectors with the
 * first 3 elements holding the imaginary components and the 4th element holding
 * the real component.
 *
 */
goog.provide('goog.vec.Quaternion');

goog.require('goog.vec');
goog.require('goog.vec.Vec3');
goog.require('goog.vec.Vec4');


/** @typedef {goog.vec.Float32} */ goog.vec.Quaternion.Float32;
/** @typedef {goog.vec.Float64} */ goog.vec.Quaternion.Float64;
/** @typedef {goog.vec.Number} */ goog.vec.Quaternion.Number;
/** @typedef {goog.vec.AnyType} */ goog.vec.Quaternion.AnyType;


/**
 * Creates a Float32 quaternion, initialized to zero.
 *
 * @return {!goog.vec.Quaternion.Float32} The new quaternion.
 */
goog.vec.Quaternion.createFloat32 = goog.vec.Vec4.createFloat32;


/**
 * Creates a Float64 quaternion, initialized to zero.
 *
 * @return {goog.vec.Quaternion.Float64} The new quaternion.
 */
goog.vec.Quaternion.createFloat64 = goog.vec.Vec4.createFloat64;


/**
 * Creates a Number quaternion, initialized to zero.
 *
 * @return {goog.vec.Quaternion.Number} The new quaternion.
 */
goog.vec.Quaternion.createNumber = goog.vec.Vec4.createNumber;


/**
 * Creates a new Float32 quaternion initialized with the values from the
 * supplied array.
 *
 * @param {goog.vec.AnyType} vec The source 4 element array.
 * @return {!goog.vec.Quaternion.Float32} The new quaternion.
 */
goog.vec.Quaternion.createFloat32FromArray =
    goog.vec.Vec4.createFloat32FromArray;


/**
 * Creates a new Float64 quaternion initialized with the values from the
 * supplied array.
 *
 * @param {goog.vec.AnyType} vec The source 4 element array.
 * @return {!goog.vec.Quaternion.Float64} The new quaternion.
 */
goog.vec.Quaternion.createFloat64FromArray =
    goog.vec.Vec4.createFloat64FromArray;


/**
 * Creates a new Float32 quaternion initialized with the supplied values.
 *
 * @param {number} v0 The value for element at index 0.
 * @param {number} v1 The value for element at index 1.
 * @param {number} v2 The value for element at index 2.
 * @param {number} v3 The value for element at index 3.
 * @return {!goog.vec.Quaternion.Float32} The new quaternion.
 */
goog.vec.Quaternion.createFloat32FromValues =
    goog.vec.Vec4.createFloat32FromValues;


/**
 * Creates a new Float64 quaternion initialized with the supplied values.
 *
 * @param {number} v0 The value for element at index 0.
 * @param {number} v1 The value for element at index 1.
 * @param {number} v2 The value for element at index 2.
 * @param {number} v3 The value for element at index 3.
 * @return {!goog.vec.Quaternion.Float64} The new quaternion.
 */
goog.vec.Quaternion.createFloat64FromValues =
    goog.vec.Vec4.createFloat64FromValues;


/**
 * Creates a clone of the given Float32 quaternion.
 *
 * @param {goog.vec.Quaternion.Float32} q The source quaternion.
 * @return {goog.vec.Quaternion.Float32} The new quaternion.
 */
goog.vec.Quaternion.cloneFloat32 = goog.vec.Vec4.cloneFloat32;


/**
 * Creates a clone of the given Float64 quaternion.
 *
 * @param {goog.vec.Quaternion.Float64} q The source quaternion.
 * @return {goog.vec.Quaternion.Float64} The new quaternion.
 */
goog.vec.Quaternion.cloneFloat64 = goog.vec.Vec4.cloneFloat64;


/**
 * Initializes the quaternion with the given values.
 *
 * @param {goog.vec.Quaternion.AnyType} q The quaternion to receive
 *     the values.
 * @param {number} v0 The value for element at index 0.
 * @param {number} v1 The value for element at index 1.
 * @param {number} v2 The value for element at index 2.
 * @param {number} v3 The value for element at index 3.
 * @return {!goog.vec.Vec4.AnyType} return q so that operations can be
 *     chained together.
 */
goog.vec.Quaternion.setFromValues = goog.vec.Vec4.setFromValues;


/**
 * Initializes the quaternion with the given array of values.
 *
 * @param {goog.vec.Quaternion.AnyType} q The quaternion to receive
 *     the values.
 * @param {goog.vec.AnyType} values The array of values.
 * @return {!goog.vec.Quaternion.AnyType} return q so that operations can be
 *     chained together.
 */
goog.vec.Quaternion.setFromArray = goog.vec.Vec4.setFromArray;


/**
 * Adds the two quaternions.
 *
 * @param {goog.vec.Quaternion.AnyType} quat0 The first addend.
 * @param {goog.vec.Quaternion.AnyType} quat1 The second addend.
 * @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
 *     receive the result. May be quat0 or quat1.
 */
goog.vec.Quaternion.add = goog.vec.Vec4.add;


/**
 * Negates a quaternion, storing the result into resultQuat.
 *
 * @param {goog.vec.Quaternion.AnyType} quat0 The quaternion to negate.
 * @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
 *     receive the result. May be quat0.
 */
goog.vec.Quaternion.negate = goog.vec.Vec4.negate;


/**
 * Multiplies each component of quat0 with scalar storing the product into
 * resultVec.
 *
 * @param {goog.vec.Quaternion.AnyType} quat0 The source quaternion.
 * @param {number} scalar The value to multiply with each component of quat0.
 * @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
 *     receive the result. May be quat0.
 */
goog.vec.Quaternion.scale = goog.vec.Vec4.scale;


/**
 * Returns the square magnitude of the given quaternion.
 *
 * @param {goog.vec.Quaternion.AnyType} quat0 The quaternion.
 * @return {number} The magnitude of the quaternion.
 */
goog.vec.Quaternion.magnitudeSquared =
    goog.vec.Vec4.magnitudeSquared;


/**
 * Returns the magnitude of the given quaternion.
 *
 * @param {goog.vec.Quaternion.AnyType} quat0 The quaternion.
 * @return {number} The magnitude of the quaternion.
 */
goog.vec.Quaternion.magnitude =
    goog.vec.Vec4.magnitude;


/**
 * Normalizes the given quaternion storing the result into resultVec.
 *
 * @param {goog.vec.Quaternion.AnyType} quat0 The quaternion to
 *     normalize.
 * @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
 *     receive the result. May be quat0.
 */
goog.vec.Quaternion.normalize = goog.vec.Vec4.normalize;


/**
 * Computes the dot (scalar) product of two quaternions.
 *
 * @param {goog.vec.Quaternion.AnyType} q0 The first quaternion.
 * @param {goog.vec.Quaternion.AnyType} q1 The second quaternion.
 * @return {number} The scalar product.
 */
goog.vec.Quaternion.dot = goog.vec.Vec4.dot;


/**
 * Computes the conjugate of the quaternion in quat storing the result into
 * resultQuat.
 *
 * @param {goog.vec.Quaternion.AnyType} quat The source quaternion.
 * @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
 *     receive the result.
 * @return {!goog.vec.Quaternion.AnyType} Return q so that
 *     operations can be chained together.
 */
goog.vec.Quaternion.conjugate = function(quat, resultQuat) {
  resultQuat[0] = -quat[0];
  resultQuat[1] = -quat[1];
  resultQuat[2] = -quat[2];
  resultQuat[3] = quat[3];
  return resultQuat;
};


/**
 * Concatenates the two quaternions storing the result into resultQuat.
 *
 * @param {goog.vec.Quaternion.AnyType} quat0 The first quaternion.
 * @param {goog.vec.Quaternion.AnyType} quat1 The second quaternion.
 * @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
 *     receive the result.
 * @return {!goog.vec.Quaternion.AnyType} Return q so that
 *     operations can be chained together.
 */
goog.vec.Quaternion.concat = function(quat0, quat1, resultQuat) {
  var x0 = quat0[0], y0 = quat0[1], z0 = quat0[2], w0 = quat0[3];
  var x1 = quat1[0], y1 = quat1[1], z1 = quat1[2], w1 = quat1[3];
  resultQuat[0] = w0 * x1 + x0 * w1 + y0 * z1 - z0 * y1;
  resultQuat[1] = w0 * y1 - x0 * z1 + y0 * w1 + z0 * x1;
  resultQuat[2] = w0 * z1 + x0 * y1 - y0 * x1 + z0 * w1;
  resultQuat[3] = w0 * w1 - x0 * x1 - y0 * y1 - z0 * z1;
  return resultQuat;
};


/**
 * Generates a unit quaternion from the given angle-axis rotation pair.
 * The rotation axis is not required to be a unit vector, but should
 * have non-zero length.  The angle should be specified in radians.
 *
 * @param {number} angle The angle (in radians) to rotate about the axis.
 * @param {goog.vec.Quaternion.AnyType} axis Unit vector specifying the
 *     axis of rotation.
 * @param {goog.vec.Quaternion.AnyType} quat Unit quaternion to store the
 *     result.
 * @return {goog.vec.Quaternion.AnyType} Return q so that
 *     operations can be chained together.
 */
goog.vec.Quaternion.fromAngleAxis = function(angle, axis, quat) {
  // Normalize the axis of rotation.
  goog.vec.Vec3.normalize(axis, axis);

  var halfAngle = 0.5 * angle;
  var sin = Math.sin(halfAngle);
  goog.vec.Quaternion.setFromValues(
      quat, sin * axis[0], sin * axis[1], sin * axis[2], Math.cos(halfAngle));

  // Normalize the resulting quaternion.
  goog.vec.Quaternion.normalize(quat, quat);
  return quat;
};


/**
 * Generates an angle-axis rotation pair from a unit quaternion.
 * The quaternion is assumed to be of unit length.  The calculated
 * values are returned via the passed 'axis' object and the 'angle'
 * number returned by the function itself. The returned rotation axis
 * is a non-zero length unit vector, and the returned angle is in
 * radians in the range of [-PI, +PI].
 *
 * @param {goog.vec.Quaternion.AnyType} quat Unit quaternion to convert.
 * @param {goog.vec.Quaternion.AnyType} axis Vector to store the returned
 *     rotation axis.
 * @return {number} angle Angle (in radians) to rotate about 'axis'.
 *     The range of the returned angle is [-PI, +PI].
 */
goog.vec.Quaternion.toAngleAxis = function(quat, axis) {
  var angle = 2 * Math.acos(quat[3]);
  var magnitude = Math.min(Math.max(1 - quat[3] * quat[3], 0), 1);
  if (magnitude < goog.vec.EPSILON) {
    // This is nearly an identity rotation, so just use a fixed +X axis.
    goog.vec.Vec3.setFromValues(axis, 1, 0, 0);
  } else {
    // Compute the proper rotation axis.
    goog.vec.Vec3.setFromValues(axis, quat[0], quat[1], quat[2]);
    // Make sure the rotation axis is of unit length.
    goog.vec.Vec3.normalize(axis, axis);
  }
  // Adjust the range of the returned angle to [-PI, +PI].
  if (angle > Math.PI) {
    angle -= 2 * Math.PI;
  }
  return angle;
};


/**
 * Generates the quaternion from the given rotation matrix.
 *
 * @param {goog.vec.Quaternion.AnyType} matrix The source matrix.
 * @param {goog.vec.Quaternion.AnyType} quat The resulting quaternion.
 * @return {!goog.vec.Quaternion.AnyType} Return q so that
 *     operations can be chained together.
 */
goog.vec.Quaternion.fromRotationMatrix4 = function(matrix, quat) {
  var sx = matrix[0], sy = matrix[5], sz = matrix[10];
  quat[3] = Math.sqrt(Math.max(0, 1 + sx + sy + sz)) / 2;
  quat[0] = Math.sqrt(Math.max(0, 1 + sx - sy - sz)) / 2;
  quat[1] = Math.sqrt(Math.max(0, 1 - sx + sy - sz)) / 2;
  quat[2] = Math.sqrt(Math.max(0, 1 - sx - sy + sz)) / 2;

  quat[0] = (matrix[6] - matrix[9] < 0) != (quat[0] < 0) ? -quat[0] : quat[0];
  quat[1] = (matrix[8] - matrix[2] < 0) != (quat[1] < 0) ? -quat[1] : quat[1];
  quat[2] = (matrix[1] - matrix[4] < 0) != (quat[2] < 0) ? -quat[2] : quat[2];
  return quat;
};


/**
 * Generates the rotation matrix from the given quaternion.
 *
 * @param {goog.vec.Quaternion.AnyType} quat The source quaternion.
 * @param {goog.vec.AnyType} matrix The resulting matrix.
 * @return {!goog.vec.AnyType} Return resulting matrix so that
 *     operations can be chained together.
 */
goog.vec.Quaternion.toRotationMatrix4 = function(quat, matrix) {
  var x = quat[0], y = quat[1], z = quat[2], w = quat[3];
  var x2 = 2 * x, y2 = 2 * y, z2 = 2 * z;
  var wx = x2 * w;
  var wy = y2 * w;
  var wz = z2 * w;
  var xx = x2 * x;
  var xy = y2 * x;
  var xz = z2 * x;
  var yy = y2 * y;
  var yz = z2 * y;
  var zz = z2 * z;

  matrix[0] = 1 - (yy + zz);
  matrix[1] = xy + wz;
  matrix[2] = xz - wy;
  matrix[3] = 0;
  matrix[4] = xy - wz;
  matrix[5] = 1 - (xx + zz);
  matrix[6] = yz + wx;
  matrix[7] = 0;
  matrix[8] = xz + wy;
  matrix[9] = yz - wx;
  matrix[10] = 1 - (xx + yy);
  matrix[11] = 0;
  matrix[12] = 0;
  matrix[13] = 0;
  matrix[14] = 0;
  matrix[15] = 1;
  return matrix;
};


/**
 * Computes the spherical linear interpolated value from the given quaternions
 * q0 and q1 according to the coefficient t. The resulting quaternion is stored
 * in resultQuat.
 *
 * @param {goog.vec.Quaternion.AnyType} q0 The first quaternion.
 * @param {goog.vec.Quaternion.AnyType} q1 The second quaternion.
 * @param {number} t The interpolating coefficient.
 * @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
 *     receive the result.
 * @return {goog.vec.Quaternion.AnyType} Return q so that
 *     operations can be chained together.
 */
goog.vec.Quaternion.slerp = function(q0, q1, t, resultQuat) {
  // Compute the dot product between q0 and q1 (cos of the angle between q0 and
  // q1). If it's outside the interval [-1,1], then the arccos is not defined.
  // The usual reason for this is that q0 and q1 are colinear. In this case
  // the angle between the two is zero, so just return q1.
  var cosVal = goog.vec.Quaternion.dot(q0, q1);
  if (cosVal > 1 || cosVal < -1) {
    goog.vec.Vec4.setFromArray(resultQuat, q1);
    return resultQuat;
  }

  // Quaternions are a double cover on the space of rotations. That is, q and -q
  // represent the same rotation. Thus we have two possibilities when
  // interpolating between q0 and q1: going the short way or the long way. We
  // prefer the short way since that is the likely expectation from users.
  var factor = 1;
  if (cosVal < 0) {
    factor = -1;
    cosVal = -cosVal;
  }

  // Compute the angle between q0 and q1. If it's very small, then just return
  // q1 to avoid a very large denominator below.
  var angle = Math.acos(cosVal);
  if (angle <= goog.vec.EPSILON) {
    goog.vec.Vec4.setFromArray(resultQuat, q1);
    return resultQuat;
  }

  // Compute the coefficients and interpolate.
  var invSinVal = 1 / Math.sin(angle);
  var c0 = Math.sin((1 - t) * angle) * invSinVal;
  var c1 = factor * Math.sin(t * angle) * invSinVal;

  resultQuat[0] = q0[0] * c0 + q1[0] * c1;
  resultQuat[1] = q0[1] * c0 + q1[1] * c1;
  resultQuat[2] = q0[2] * c0 + q1[2] * c1;
  resultQuat[3] = q0[3] * c0 + q1[3] * c1;
  return resultQuat;
};


/**
 * Compute the simple linear interpolation of the two quaternions q0 and q1
 * according to the coefficient t. The resulting quaternion is stored in
 * resultVec.
 *
 * @param {goog.vec.Quaternion.AnyType} q0 The first quaternion.
 * @param {goog.vec.Quaternion.AnyType} q1 The second quaternion.
 * @param {number} t The interpolation factor.
 * @param {goog.vec.Quaternion.AnyType} resultQuat The quaternion to
 *     receive the results (may be q0 or q1).
 */
goog.vec.Quaternion.nlerp = goog.vec.Vec4.lerp;