| 1 |
// Copyright 2011 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
|
| 16 |
/** |
| 17 |
* @fileoverview Implements quaternions and their conversion functions. In this |
| 18 |
* implementation, quaternions are represented as 4 element vectors with the |
| 19 |
* first 3 elements holding the imaginary components and the 4th element holding |
| 20 |
* the real component. |
| 21 |
* |
| 22 |
*/ |
| 23 |
goog.provide('goog.vec.Quaternion'); |
| 24 |
goog.provide('goog.vec.Quaternion.AnyType'); |
| 25 |
|
| 26 |
goog.require('goog.vec'); |
| 27 |
goog.require('goog.vec.Vec3'); |
| 28 |
goog.require('goog.vec.Vec4'); |
| 29 |
|
| 30 |
|
| 31 |
/** @typedef {goog.vec.Float32} */ goog.vec.Quaternion.Float32; |
| 32 |
/** @typedef {goog.vec.Float64} */ goog.vec.Quaternion.Float64; |
| 33 |
/** @typedef {goog.vec.Number} */ goog.vec.Quaternion.Number; |
| 34 |
/** @typedef {goog.vec.AnyType} */ goog.vec.Quaternion.AnyType; |
| 35 |
|
| 36 |
|
| 37 |
/** |
| 38 |
* Creates a Float32 quaternion, initialized to zero. |
| 39 |
* |
| 40 |
* @return {!goog.vec.Quaternion.Float32} The new quaternion. |
| 41 |
*/ |
| 42 |
goog.vec.Quaternion.createFloat32 = goog.vec.Vec4.createFloat32; |
| 43 |
|
| 44 |
|
| 45 |
/** |
| 46 |
* Creates a Float64 quaternion, initialized to zero. |
| 47 |
* |
| 48 |
* @return {!goog.vec.Quaternion.Float64} The new quaternion. |
| 49 |
*/ |
| 50 |
goog.vec.Quaternion.createFloat64 = goog.vec.Vec4.createFloat64; |
| 51 |
|
| 52 |
|
| 53 |
/** |
| 54 |
* Creates a Number quaternion, initialized to zero. |
| 55 |
* |
| 56 |
* @return {goog.vec.Quaternion.Number} The new quaternion. |
| 57 |
*/ |
| 58 |
goog.vec.Quaternion.createNumber = goog.vec.Vec4.createNumber; |
| 59 |
|
| 60 |
|
| 61 |
/** |
| 62 |
* Creates a new Float32 quaternion initialized with the values from the |
| 63 |
* supplied array. |
| 64 |
* |
| 65 |
* @param {!goog.vec.AnyType} vec The source 4 element array. |
| 66 |
* @return {!goog.vec.Quaternion.Float32} The new quaternion. |
| 67 |
*/ |
| 68 |
goog.vec.Quaternion.createFloat32FromArray = |
| 69 |
goog.vec.Vec4.createFloat32FromArray; |
| 70 |
|
| 71 |
|
| 72 |
/** |
| 73 |
* Creates a new Float64 quaternion initialized with the values from the |
| 74 |
* supplied array. |
| 75 |
* |
| 76 |
* @param {!goog.vec.AnyType} vec The source 4 element array. |
| 77 |
* @return {!goog.vec.Quaternion.Float64} The new quaternion. |
| 78 |
*/ |
| 79 |
goog.vec.Quaternion.createFloat64FromArray = |
| 80 |
goog.vec.Vec4.createFloat64FromArray; |
| 81 |
|
| 82 |
|
| 83 |
/** |
| 84 |
* Creates a new Float32 quaternion initialized with the supplied values. |
| 85 |
* |
| 86 |
* @param {number} v0 The value for element at index 0. |
| 87 |
* @param {number} v1 The value for element at index 1. |
| 88 |
* @param {number} v2 The value for element at index 2. |
| 89 |
* @param {number} v3 The value for element at index 3. |
| 90 |
* @return {!goog.vec.Quaternion.Float32} The new quaternion. |
| 91 |
*/ |
| 92 |
goog.vec.Quaternion.createFloat32FromValues = |
| 93 |
goog.vec.Vec4.createFloat32FromValues; |
| 94 |
|
| 95 |
|
| 96 |
/** |
| 97 |
* Creates a new Float64 quaternion initialized with the supplied values. |
| 98 |
* |
| 99 |
* @param {number} v0 The value for element at index 0. |
| 100 |
* @param {number} v1 The value for element at index 1. |
| 101 |
* @param {number} v2 The value for element at index 2. |
| 102 |
* @param {number} v3 The value for element at index 3. |
| 103 |
* @return {!goog.vec.Quaternion.Float64} The new quaternion. |
| 104 |
*/ |
| 105 |
goog.vec.Quaternion.createFloat64FromValues = |
| 106 |
goog.vec.Vec4.createFloat64FromValues; |
| 107 |
|
| 108 |
|
| 109 |
/** |
| 110 |
* Creates a clone of the given Float32 quaternion. |
| 111 |
* |
| 112 |
* @param {!goog.vec.Quaternion.Float32} q The source quaternion. |
| 113 |
* @return {!goog.vec.Quaternion.Float32} The new quaternion. |
| 114 |
*/ |
| 115 |
goog.vec.Quaternion.cloneFloat32 = goog.vec.Vec4.cloneFloat32; |
| 116 |
|
| 117 |
|
| 118 |
/** |
| 119 |
* Creates a clone of the given Float64 quaternion. |
| 120 |
* |
| 121 |
* @param {!goog.vec.Quaternion.Float64} q The source quaternion. |
| 122 |
* @return {!goog.vec.Quaternion.Float64} The new quaternion. |
| 123 |
*/ |
| 124 |
goog.vec.Quaternion.cloneFloat64 = goog.vec.Vec4.cloneFloat64; |
| 125 |
|
| 126 |
|
| 127 |
/** |
| 128 |
* Creates a Float32 quaternion, initialized to the identity. |
| 129 |
* |
| 130 |
* @return {!goog.vec.Quaternion.Float32} The new quaternion. |
| 131 |
*/ |
| 132 |
goog.vec.Quaternion.createIdentityFloat32 = function() { |
| 133 |
var quat = goog.vec.Quaternion.createFloat32(); |
| 134 |
goog.vec.Quaternion.makeIdentity(quat); |
| 135 |
return quat; |
| 136 |
}; |
| 137 |
|
| 138 |
|
| 139 |
/** |
| 140 |
* Creates a Float64 quaternion, initialized to the identity. |
| 141 |
* |
| 142 |
* @return {!goog.vec.Quaternion.Float64} The new quaternion. |
| 143 |
*/ |
| 144 |
goog.vec.Quaternion.createIdentityFloat64 = function() { |
| 145 |
var quat = goog.vec.Quaternion.createFloat64(); |
| 146 |
goog.vec.Quaternion.makeIdentity(quat); |
| 147 |
return quat; |
| 148 |
}; |
| 149 |
|
| 150 |
|
| 151 |
/** |
| 152 |
* Initializes the quaternion with the given values. |
| 153 |
* |
| 154 |
* @param {!goog.vec.Quaternion.AnyType} q The quaternion to receive |
| 155 |
* the values. |
| 156 |
* @param {number} v0 The value for element at index 0. |
| 157 |
* @param {number} v1 The value for element at index 1. |
| 158 |
* @param {number} v2 The value for element at index 2. |
| 159 |
* @param {number} v3 The value for element at index 3. |
| 160 |
* @return {!goog.vec.Vec4.AnyType} return q so that operations can be |
| 161 |
* chained together. |
| 162 |
*/ |
| 163 |
goog.vec.Quaternion.setFromValues = goog.vec.Vec4.setFromValues; |
| 164 |
|
| 165 |
|
| 166 |
/** |
| 167 |
* Initializes the quaternion with the given array of values. |
| 168 |
* |
| 169 |
* @param {!goog.vec.Quaternion.AnyType} q The quaternion to receive |
| 170 |
* the values. |
| 171 |
* @param {!goog.vec.AnyType} values The array of values. |
| 172 |
* @return {!goog.vec.Quaternion.AnyType} return q so that operations can be |
| 173 |
* chained together. |
| 174 |
*/ |
| 175 |
goog.vec.Quaternion.setFromArray = goog.vec.Vec4.setFromArray; |
| 176 |
|
| 177 |
|
| 178 |
/** |
| 179 |
* Adds the two quaternions. |
| 180 |
* |
| 181 |
* @param {!goog.vec.Quaternion.AnyType} quat0 The first addend. |
| 182 |
* @param {!goog.vec.Quaternion.AnyType} quat1 The second addend. |
| 183 |
* @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to |
| 184 |
* receive the result. May be quat0 or quat1. |
| 185 |
*/ |
| 186 |
goog.vec.Quaternion.add = goog.vec.Vec4.add; |
| 187 |
|
| 188 |
|
| 189 |
/** |
| 190 |
* Negates a quaternion, storing the result into resultQuat. |
| 191 |
* |
| 192 |
* @param {!goog.vec.Quaternion.AnyType} quat0 The quaternion to negate. |
| 193 |
* @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to |
| 194 |
* receive the result. May be quat0. |
| 195 |
*/ |
| 196 |
goog.vec.Quaternion.negate = goog.vec.Vec4.negate; |
| 197 |
|
| 198 |
|
| 199 |
/** |
| 200 |
* Multiplies each component of quat0 with scalar storing the product into |
| 201 |
* resultVec. |
| 202 |
* |
| 203 |
* @param {!goog.vec.Quaternion.AnyType} quat0 The source quaternion. |
| 204 |
* @param {number} scalar The value to multiply with each component of quat0. |
| 205 |
* @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to |
| 206 |
* receive the result. May be quat0. |
| 207 |
*/ |
| 208 |
goog.vec.Quaternion.scale = goog.vec.Vec4.scale; |
| 209 |
|
| 210 |
|
| 211 |
/** |
| 212 |
* Returns the square magnitude of the given quaternion. |
| 213 |
* |
| 214 |
* @param {!goog.vec.Quaternion.AnyType} quat0 The quaternion. |
| 215 |
* @return {number} The magnitude of the quaternion. |
| 216 |
*/ |
| 217 |
goog.vec.Quaternion.magnitudeSquared = goog.vec.Vec4.magnitudeSquared; |
| 218 |
|
| 219 |
|
| 220 |
/** |
| 221 |
* Returns the magnitude of the given quaternion. |
| 222 |
* |
| 223 |
* @param {!goog.vec.Quaternion.AnyType} quat0 The quaternion. |
| 224 |
* @return {number} The magnitude of the quaternion. |
| 225 |
*/ |
| 226 |
goog.vec.Quaternion.magnitude = goog.vec.Vec4.magnitude; |
| 227 |
|
| 228 |
|
| 229 |
/** |
| 230 |
* Normalizes the given quaternion storing the result into resultVec. |
| 231 |
* |
| 232 |
* @param {!goog.vec.Quaternion.AnyType} quat0 The quaternion to |
| 233 |
* normalize. |
| 234 |
* @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to |
| 235 |
* receive the result. May be quat0. |
| 236 |
*/ |
| 237 |
goog.vec.Quaternion.normalize = goog.vec.Vec4.normalize; |
| 238 |
|
| 239 |
|
| 240 |
/** |
| 241 |
* Computes the dot (scalar) product of two quaternions. |
| 242 |
* |
| 243 |
* @param {!goog.vec.Quaternion.AnyType} q0 The first quaternion. |
| 244 |
* @param {!goog.vec.Quaternion.AnyType} q1 The second quaternion. |
| 245 |
* @return {number} The scalar product. |
| 246 |
*/ |
| 247 |
goog.vec.Quaternion.dot = goog.vec.Vec4.dot; |
| 248 |
|
| 249 |
|
| 250 |
/** |
| 251 |
* Computes the inverse of the quaternion in quat, storing the result into |
| 252 |
* resultQuat. |
| 253 |
* |
| 254 |
* If the quaternion is already normalized, goog.vec.Quaternion.conjugate |
| 255 |
* is faster than this function and produces the same result. |
| 256 |
* |
| 257 |
* @param {!goog.vec.Quaternion.AnyType} quat The quaternion to invert. |
| 258 |
* @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to receive |
| 259 |
* the result. |
| 260 |
* @return {!goog.vec.Quaternion.AnyType} Return resultQuat so that |
| 261 |
* operations can be chained together. |
| 262 |
*/ |
| 263 |
goog.vec.Quaternion.invert = function(quat, resultQuat) { |
| 264 |
var a0 = quat[0], a1 = quat[1], a2 = quat[2], a3 = quat[3]; |
| 265 |
var dot = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3; |
| 266 |
var invDot = dot ? 1.0 / dot : 0; |
| 267 |
|
| 268 |
resultQuat[0] = -a0 * invDot; |
| 269 |
resultQuat[1] = -a1 * invDot; |
| 270 |
resultQuat[2] = -a2 * invDot; |
| 271 |
resultQuat[3] = a3 * invDot; |
| 272 |
return resultQuat; |
| 273 |
}; |
| 274 |
|
| 275 |
|
| 276 |
/** |
| 277 |
* Computes the conjugate of the quaternion in quat, storing the result into |
| 278 |
* resultQuat. |
| 279 |
* |
| 280 |
* If the quaternion is normalized already, this function is faster than |
| 281 |
* goog.Quaternion.inverse and produces the same result. |
| 282 |
* |
| 283 |
* @param {!goog.vec.Quaternion.AnyType} quat The source quaternion. |
| 284 |
* @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to |
| 285 |
* receive the result. |
| 286 |
* @return {!goog.vec.Quaternion.AnyType} Return resultQuat so that |
| 287 |
* operations can be chained together. |
| 288 |
*/ |
| 289 |
goog.vec.Quaternion.conjugate = function(quat, resultQuat) { |
| 290 |
resultQuat[0] = -quat[0]; |
| 291 |
resultQuat[1] = -quat[1]; |
| 292 |
resultQuat[2] = -quat[2]; |
| 293 |
resultQuat[3] = quat[3]; |
| 294 |
return resultQuat; |
| 295 |
}; |
| 296 |
|
| 297 |
|
| 298 |
/** |
| 299 |
* Concatenates the two quaternions storing the result into resultQuat. |
| 300 |
* |
| 301 |
* @param {!goog.vec.Quaternion.AnyType} quat0 The first quaternion. |
| 302 |
* @param {!goog.vec.Quaternion.AnyType} quat1 The second quaternion. |
| 303 |
* @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to |
| 304 |
* receive the result. |
| 305 |
* @return {!goog.vec.Quaternion.AnyType} Return resultQuat so that |
| 306 |
* operations can be chained together. |
| 307 |
*/ |
| 308 |
goog.vec.Quaternion.concat = function(quat0, quat1, resultQuat) { |
| 309 |
var x0 = quat0[0], y0 = quat0[1], z0 = quat0[2], w0 = quat0[3]; |
| 310 |
var x1 = quat1[0], y1 = quat1[1], z1 = quat1[2], w1 = quat1[3]; |
| 311 |
resultQuat[0] = w0 * x1 + x0 * w1 + y0 * z1 - z0 * y1; |
| 312 |
resultQuat[1] = w0 * y1 - x0 * z1 + y0 * w1 + z0 * x1; |
| 313 |
resultQuat[2] = w0 * z1 + x0 * y1 - y0 * x1 + z0 * w1; |
| 314 |
resultQuat[3] = w0 * w1 - x0 * x1 - y0 * y1 - z0 * z1; |
| 315 |
return resultQuat; |
| 316 |
}; |
| 317 |
|
| 318 |
|
| 319 |
/** |
| 320 |
* Makes the given quaternion the identity quaternion (0, 0, 0, 1). |
| 321 |
* |
| 322 |
* @param {!goog.vec.Quaternion.AnyType} quat The quaternion. |
| 323 |
* @return {!goog.vec.Quaternion.AnyType} Return quat so that |
| 324 |
* operations can be chained together. |
| 325 |
*/ |
| 326 |
goog.vec.Quaternion.makeIdentity = function(quat) { |
| 327 |
quat[0] = 0; |
| 328 |
quat[1] = 0; |
| 329 |
quat[2] = 0; |
| 330 |
quat[3] = 1; |
| 331 |
return quat; |
| 332 |
}; |
| 333 |
|
| 334 |
|
| 335 |
/** |
| 336 |
* Generates a unit quaternion from the given angle-axis rotation pair. |
| 337 |
* The rotation axis is not required to be a unit vector, but should |
| 338 |
* have non-zero length. The angle should be specified in radians. |
| 339 |
* |
| 340 |
* @param {number} angle The angle (in radians) to rotate about the axis. |
| 341 |
* @param {!goog.vec.Quaternion.AnyType} axis Unit vector specifying the |
| 342 |
* axis of rotation. |
| 343 |
* @param {!goog.vec.Quaternion.AnyType} quat Unit quaternion to store the |
| 344 |
* result. |
| 345 |
* @return {!goog.vec.Quaternion.AnyType} Return quat so that |
| 346 |
* operations can be chained together. |
| 347 |
*/ |
| 348 |
goog.vec.Quaternion.fromAngleAxis = function(angle, axis, quat) { |
| 349 |
// Normalize the axis of rotation. |
| 350 |
goog.vec.Vec3.normalize(axis, axis); |
| 351 |
|
| 352 |
var halfAngle = 0.5 * angle; |
| 353 |
var sin = Math.sin(halfAngle); |
| 354 |
goog.vec.Quaternion.setFromValues( |
| 355 |
quat, sin * axis[0], sin * axis[1], sin * axis[2], Math.cos(halfAngle)); |
| 356 |
|
| 357 |
// Normalize the resulting quaternion. |
| 358 |
goog.vec.Quaternion.normalize(quat, quat); |
| 359 |
return quat; |
| 360 |
}; |
| 361 |
|
| 362 |
|
| 363 |
/** |
| 364 |
* Generates an angle-axis rotation pair from a unit quaternion. |
| 365 |
* The quaternion is assumed to be of unit length. The calculated |
| 366 |
* values are returned via the passed 'axis' object and the 'angle' |
| 367 |
* number returned by the function itself. The returned rotation axis |
| 368 |
* is a non-zero length unit vector, and the returned angle is in |
| 369 |
* radians in the range of [-PI, +PI]. |
| 370 |
* |
| 371 |
* @param {!goog.vec.Quaternion.AnyType} quat Unit quaternion to convert. |
| 372 |
* @param {!goog.vec.Quaternion.AnyType} axis Vector to store the returned |
| 373 |
* rotation axis. |
| 374 |
* @return {number} angle Angle (in radians) to rotate about 'axis'. |
| 375 |
* The range of the returned angle is [-PI, +PI]. |
| 376 |
*/ |
| 377 |
goog.vec.Quaternion.toAngleAxis = function(quat, axis) { |
| 378 |
var angle = 2 * Math.acos(quat[3]); |
| 379 |
var magnitude = Math.min(Math.max(1 - quat[3] * quat[3], 0), 1); |
| 380 |
if (magnitude < goog.vec.EPSILON) { |
| 381 |
// This is nearly an identity rotation, so just use a fixed +X axis. |
| 382 |
goog.vec.Vec3.setFromValues(axis, 1, 0, 0); |
| 383 |
} else { |
| 384 |
// Compute the proper rotation axis. |
| 385 |
goog.vec.Vec3.setFromValues(axis, quat[0], quat[1], quat[2]); |
| 386 |
// Make sure the rotation axis is of unit length. |
| 387 |
goog.vec.Vec3.normalize(axis, axis); |
| 388 |
} |
| 389 |
// Adjust the range of the returned angle to [-PI, +PI]. |
| 390 |
if (angle > Math.PI) { |
| 391 |
angle -= 2 * Math.PI; |
| 392 |
} |
| 393 |
return angle; |
| 394 |
}; |
| 395 |
|
| 396 |
|
| 397 |
/** |
| 398 |
* Generates the quaternion from the given 3x3 rotation matrix. |
| 399 |
* |
| 400 |
* Perf: http://jsperf.com/conversion-of-3x3-matrix-to-quaternion |
| 401 |
* http://jsperf.com/goog-vec-fromrotationmatrix3-a |
| 402 |
* |
| 403 |
* @param {!goog.vec.AnyType} matrix The source matrix. |
| 404 |
* @param {!goog.vec.Quaternion.AnyType} quat The resulting quaternion. |
| 405 |
* @return {!goog.vec.Quaternion.AnyType} Return quat so that |
| 406 |
* operations can be chained together. |
| 407 |
*/ |
| 408 |
goog.vec.Quaternion.fromRotationMatrix3 = function(matrix, quat) { |
| 409 |
// Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes |
| 410 |
// article "Quaternion Calculus and Fast Animation". |
| 411 |
var fTrace = matrix[0] + matrix[4] + matrix[8]; |
| 412 |
var fRoot; |
| 413 |
|
| 414 |
if (fTrace > 0.0) { |
| 415 |
// |w| > 1/2, may as well choose w > 1/2 |
| 416 |
fRoot = Math.sqrt(fTrace + 1.0); // 2w |
| 417 |
quat[3] = 0.5 * fRoot; |
| 418 |
fRoot = 0.5 / fRoot; // 1 / (4w) |
| 419 |
quat[0] = (matrix[5] - matrix[7]) * fRoot; |
| 420 |
quat[1] = (matrix[6] - matrix[2]) * fRoot; |
| 421 |
quat[2] = (matrix[1] - matrix[3]) * fRoot; |
| 422 |
} else { |
| 423 |
// |w| <= 1/2 |
| 424 |
var i = 0; |
| 425 |
if (matrix[4] > matrix[0]) i = 1; |
| 426 |
if (matrix[8] > matrix[i * 3 + i]) i = 2; |
| 427 |
var j = (i + 1) % 3; |
| 428 |
var k = (i + 2) % 3; |
| 429 |
|
| 430 |
fRoot = Math.sqrt( |
| 431 |
matrix[i * 3 + i] - matrix[j * 3 + j] - matrix[k * 3 + k] + 1.0); |
| 432 |
quat[i] = 0.5 * fRoot; |
| 433 |
fRoot = 0.5 / fRoot; |
| 434 |
quat[3] = (matrix[j * 3 + k] - matrix[k * 3 + j]) * fRoot; |
| 435 |
quat[j] = (matrix[j * 3 + i] + matrix[i * 3 + j]) * fRoot; |
| 436 |
quat[k] = (matrix[k * 3 + i] + matrix[i * 3 + k]) * fRoot; |
| 437 |
|
| 438 |
// Flip all signs if w is negative. |
| 439 |
if (quat[3] < 0) { |
| 440 |
quat[0] = -quat[0]; |
| 441 |
quat[1] = -quat[1]; |
| 442 |
quat[2] = -quat[2]; |
| 443 |
quat[3] = -quat[3]; |
| 444 |
} |
| 445 |
} |
| 446 |
return quat; |
| 447 |
}; |
| 448 |
|
| 449 |
|
| 450 |
/** |
| 451 |
* Generates the quaternion from the given 4x4 rotation matrix. |
| 452 |
* |
| 453 |
* Perf: http://jsperf.com/goog-vec-fromrotationmatrix4 |
| 454 |
* |
| 455 |
* Implementation is the same as fromRotationMatrix3 but using indices from |
| 456 |
* the top left 3x3 in a 4x4 matrix. |
| 457 |
* |
| 458 |
* @param {!goog.vec.AnyType} matrix The source matrix. |
| 459 |
* @param {!goog.vec.Quaternion.AnyType} quat The resulting quaternion. |
| 460 |
* @return {!goog.vec.Quaternion.AnyType} Return quat so that |
| 461 |
* operations can be chained together. |
| 462 |
*/ |
| 463 |
goog.vec.Quaternion.fromRotationMatrix4 = function(matrix, quat) { |
| 464 |
var fTrace = matrix[0] + matrix[5] + matrix[10]; |
| 465 |
var fRoot; |
| 466 |
|
| 467 |
if (fTrace > 0.0) { |
| 468 |
// |w| > 1/2, may as well choose w > 1/2 |
| 469 |
fRoot = Math.sqrt(fTrace + 1.0); // 2w |
| 470 |
quat[3] = 0.5 * fRoot; |
| 471 |
fRoot = 0.5 / fRoot; // 1 / (4w) |
| 472 |
quat[0] = (matrix[6] - matrix[9]) * fRoot; |
| 473 |
quat[1] = (matrix[8] - matrix[2]) * fRoot; |
| 474 |
quat[2] = (matrix[1] - matrix[4]) * fRoot; |
| 475 |
} else { |
| 476 |
// |w| <= 1/2 |
| 477 |
var i = 0; |
| 478 |
if (matrix[5] > matrix[0]) i = 1; |
| 479 |
if (matrix[10] > matrix[i * 4 + i]) i = 2; |
| 480 |
var j = (i + 1) % 3; |
| 481 |
var k = (i + 2) % 3; |
| 482 |
|
| 483 |
fRoot = Math.sqrt( |
| 484 |
matrix[i * 4 + i] - matrix[j * 4 + j] - matrix[k * 4 + k] + 1.0); |
| 485 |
quat[i] = 0.5 * fRoot; |
| 486 |
fRoot = 0.5 / fRoot; |
| 487 |
quat[3] = (matrix[j * 4 + k] - matrix[k * 4 + j]) * fRoot; |
| 488 |
quat[j] = (matrix[j * 4 + i] + matrix[i * 4 + j]) * fRoot; |
| 489 |
quat[k] = (matrix[k * 4 + i] + matrix[i * 4 + k]) * fRoot; |
| 490 |
|
| 491 |
// Flip all signs if w is negative. |
| 492 |
if (quat[3] < 0) { |
| 493 |
quat[0] = -quat[0]; |
| 494 |
quat[1] = -quat[1]; |
| 495 |
quat[2] = -quat[2]; |
| 496 |
quat[3] = -quat[3]; |
| 497 |
} |
| 498 |
} |
| 499 |
return quat; |
| 500 |
}; |
| 501 |
|
| 502 |
|
| 503 |
/** |
| 504 |
* Generates the 3x3 rotation matrix from the given quaternion. |
| 505 |
* |
| 506 |
* @param {!goog.vec.Quaternion.AnyType} quat The source quaternion. |
| 507 |
* @param {!goog.vec.AnyType} matrix The resulting matrix. |
| 508 |
* @return {!goog.vec.AnyType} Return resulting matrix so that |
| 509 |
* operations can be chained together. |
| 510 |
*/ |
| 511 |
goog.vec.Quaternion.toRotationMatrix3 = function(quat, matrix) { |
| 512 |
var x = quat[0], y = quat[1], z = quat[2], w = quat[3]; |
| 513 |
var x2 = 2 * x, y2 = 2 * y, z2 = 2 * z; |
| 514 |
var wx = x2 * w; |
| 515 |
var wy = y2 * w; |
| 516 |
var wz = z2 * w; |
| 517 |
var xx = x2 * x; |
| 518 |
var xy = y2 * x; |
| 519 |
var xz = z2 * x; |
| 520 |
var yy = y2 * y; |
| 521 |
var yz = z2 * y; |
| 522 |
var zz = z2 * z; |
| 523 |
|
| 524 |
matrix[0] = 1 - (yy + zz); |
| 525 |
matrix[1] = xy + wz; |
| 526 |
matrix[2] = xz - wy; |
| 527 |
matrix[3] = xy - wz; |
| 528 |
matrix[4] = 1 - (xx + zz); |
| 529 |
matrix[5] = yz + wx; |
| 530 |
matrix[6] = xz + wy; |
| 531 |
matrix[7] = yz - wx; |
| 532 |
matrix[8] = 1 - (xx + yy); |
| 533 |
return matrix; |
| 534 |
}; |
| 535 |
|
| 536 |
|
| 537 |
/** |
| 538 |
* Generates the 4x4 rotation matrix from the given quaternion. |
| 539 |
* |
| 540 |
* @param {!goog.vec.Quaternion.AnyType} quat The source quaternion. |
| 541 |
* @param {!goog.vec.AnyType} matrix The resulting matrix. |
| 542 |
* @return {!goog.vec.AnyType} Return resulting matrix so that |
| 543 |
* operations can be chained together. |
| 544 |
*/ |
| 545 |
goog.vec.Quaternion.toRotationMatrix4 = function(quat, matrix) { |
| 546 |
var x = quat[0], y = quat[1], z = quat[2], w = quat[3]; |
| 547 |
var x2 = 2 * x, y2 = 2 * y, z2 = 2 * z; |
| 548 |
var wx = x2 * w; |
| 549 |
var wy = y2 * w; |
| 550 |
var wz = z2 * w; |
| 551 |
var xx = x2 * x; |
| 552 |
var xy = y2 * x; |
| 553 |
var xz = z2 * x; |
| 554 |
var yy = y2 * y; |
| 555 |
var yz = z2 * y; |
| 556 |
var zz = z2 * z; |
| 557 |
|
| 558 |
matrix[0] = 1 - (yy + zz); |
| 559 |
matrix[1] = xy + wz; |
| 560 |
matrix[2] = xz - wy; |
| 561 |
matrix[3] = 0; |
| 562 |
matrix[4] = xy - wz; |
| 563 |
matrix[5] = 1 - (xx + zz); |
| 564 |
matrix[6] = yz + wx; |
| 565 |
matrix[7] = 0; |
| 566 |
matrix[8] = xz + wy; |
| 567 |
matrix[9] = yz - wx; |
| 568 |
matrix[10] = 1 - (xx + yy); |
| 569 |
matrix[11] = 0; |
| 570 |
matrix[12] = 0; |
| 571 |
matrix[13] = 0; |
| 572 |
matrix[14] = 0; |
| 573 |
matrix[15] = 1; |
| 574 |
return matrix; |
| 575 |
}; |
| 576 |
|
| 577 |
|
| 578 |
/** |
| 579 |
* Rotates a quaternion by the given angle about the X axis. |
| 580 |
* |
| 581 |
* @param {!goog.vec.Quaternion.AnyType} quat The quaternion. |
| 582 |
* @param {number} angle The angle in radians. |
| 583 |
* @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to |
| 584 |
* receive the result. |
| 585 |
* @return {!goog.vec.Quaternion.AnyType} Return resultQuat so that |
| 586 |
* operations can be chained together. |
| 587 |
*/ |
| 588 |
goog.vec.Quaternion.rotateX = function(quat, angle, resultQuat) { |
| 589 |
angle *= 0.5; |
| 590 |
var ax = quat[0], ay = quat[1], az = quat[2], aw = quat[3]; |
| 591 |
var bx = Math.sin(angle), bw = Math.cos(angle); |
| 592 |
|
| 593 |
resultQuat[0] = ax * bw + aw * bx; |
| 594 |
resultQuat[1] = ay * bw + az * bx; |
| 595 |
resultQuat[2] = az * bw - ay * bx; |
| 596 |
resultQuat[3] = aw * bw - ax * bx; |
| 597 |
return resultQuat; |
| 598 |
}; |
| 599 |
|
| 600 |
|
| 601 |
/** |
| 602 |
* Rotates a quaternion by the given angle about the Y axis. |
| 603 |
* |
| 604 |
* @param {!goog.vec.Quaternion.AnyType} quat The quaternion. |
| 605 |
* @param {number} angle The angle in radians. |
| 606 |
* @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to |
| 607 |
* receive the result. |
| 608 |
* @return {!goog.vec.Quaternion.AnyType} Return resultQuat so that |
| 609 |
* operations can be chained together. |
| 610 |
*/ |
| 611 |
goog.vec.Quaternion.rotateY = function(quat, angle, resultQuat) { |
| 612 |
angle *= 0.5; |
| 613 |
var ax = quat[0], ay = quat[1], az = quat[2], aw = quat[3]; |
| 614 |
var by = Math.sin(angle), bw = Math.cos(angle); |
| 615 |
|
| 616 |
resultQuat[0] = ax * bw - az * by; |
| 617 |
resultQuat[1] = ay * bw + aw * by; |
| 618 |
resultQuat[2] = az * bw + ax * by; |
| 619 |
resultQuat[3] = aw * bw - ay * by; |
| 620 |
return resultQuat; |
| 621 |
}; |
| 622 |
|
| 623 |
|
| 624 |
/** |
| 625 |
* Rotates a quaternion by the given angle about the Z axis. |
| 626 |
* |
| 627 |
* @param {!goog.vec.Quaternion.AnyType} quat The quaternion. |
| 628 |
* @param {number} angle The angle in radians. |
| 629 |
* @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to |
| 630 |
* receive the result. |
| 631 |
* @return {!goog.vec.Quaternion.AnyType} Return resultQuat so that |
| 632 |
* operations can be chained together. |
| 633 |
*/ |
| 634 |
goog.vec.Quaternion.rotateZ = function(quat, angle, resultQuat) { |
| 635 |
angle *= 0.5; |
| 636 |
var ax = quat[0], ay = quat[1], az = quat[2], aw = quat[3]; |
| 637 |
var bz = Math.sin(angle), bw = Math.cos(angle); |
| 638 |
|
| 639 |
resultQuat[0] = ax * bw + ay * bz; |
| 640 |
resultQuat[1] = ay * bw - ax * bz; |
| 641 |
resultQuat[2] = az * bw + aw * bz; |
| 642 |
resultQuat[3] = aw * bw - az * bz; |
| 643 |
return resultQuat; |
| 644 |
}; |
| 645 |
|
| 646 |
|
| 647 |
/** |
| 648 |
* Transforms a vec with a quaternion. Works on both vec3s and vec4s. |
| 649 |
* |
| 650 |
* @param {!goog.vec.AnyType} vec The vec to transform. |
| 651 |
* @param {!goog.vec.Quaternion.AnyType} quat The quaternion. |
| 652 |
* @param {!goog.vec.AnyType} resultVec The vec to receive the result. |
| 653 |
* @return {!goog.vec.AnyType} Return resultVec so that operations can be |
| 654 |
* chained together. Note that the caller is responsible for type-casting. |
| 655 |
*/ |
| 656 |
goog.vec.Quaternion.transformVec = function(vec, quat, resultVec) { |
| 657 |
var x = vec[0], y = vec[1], z = vec[2]; |
| 658 |
var qx = quat[0], qy = quat[1], qz = quat[2], qw = quat[3]; |
| 659 |
// Calculate quat * vec. |
| 660 |
var ix = qw * x + qy * z - qz * y; |
| 661 |
var iy = qw * y + qz * x - qx * z; |
| 662 |
var iz = qw * z + qx * y - qy * x; |
| 663 |
var iw = -qx * x - qy * y - qz * z; |
| 664 |
// Calculate result * inverse quat. |
| 665 |
resultVec[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy; |
| 666 |
resultVec[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz; |
| 667 |
resultVec[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx; |
| 668 |
return resultVec; |
| 669 |
}; |
| 670 |
|
| 671 |
|
| 672 |
/** |
| 673 |
* Computes the spherical linear interpolated value from the given quaternions |
| 674 |
* q0 and q1 according to the coefficient t. The resulting quaternion is stored |
| 675 |
* in resultQuat. |
| 676 |
* |
| 677 |
* @param {!goog.vec.Quaternion.AnyType} q0 The first quaternion. |
| 678 |
* @param {!goog.vec.Quaternion.AnyType} q1 The second quaternion. |
| 679 |
* @param {number} t The interpolating coefficient. |
| 680 |
* @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to |
| 681 |
* receive the result. |
| 682 |
* @return {!goog.vec.Quaternion.AnyType} Return resultQuat so that |
| 683 |
* operations can be chained together. |
| 684 |
*/ |
| 685 |
goog.vec.Quaternion.slerp = function(q0, q1, t, resultQuat) { |
| 686 |
// Compute the dot product between q0 and q1 (cos of the angle between q0 and |
| 687 |
// q1). If it's outside the interval [-1,1], then the arccos is not defined. |
| 688 |
// The usual reason for this is that q0 and q1 are colinear. In this case |
| 689 |
// the angle between the two is zero, so just return q1. |
| 690 |
var cosVal = goog.vec.Quaternion.dot(q0, q1); |
| 691 |
if (cosVal > 1 || cosVal < -1) { |
| 692 |
goog.vec.Vec4.setFromArray(resultQuat, q1); |
| 693 |
return resultQuat; |
| 694 |
} |
| 695 |
|
| 696 |
// Quaternions are a double cover on the space of rotations. That is, q and -q |
| 697 |
// represent the same rotation. Thus we have two possibilities when |
| 698 |
// interpolating between q0 and q1: going the short way or the long way. We |
| 699 |
// prefer the short way since that is the likely expectation from users. |
| 700 |
var factor = 1; |
| 701 |
if (cosVal < 0) { |
| 702 |
factor = -1; |
| 703 |
cosVal = -cosVal; |
| 704 |
} |
| 705 |
|
| 706 |
// Compute the angle between q0 and q1. If it's very small, then just return |
| 707 |
// q1 to avoid a very large denominator below. |
| 708 |
var angle = Math.acos(cosVal); |
| 709 |
if (angle <= goog.vec.EPSILON) { |
| 710 |
goog.vec.Vec4.setFromArray(resultQuat, q1); |
| 711 |
return resultQuat; |
| 712 |
} |
| 713 |
|
| 714 |
// Compute the coefficients and interpolate. |
| 715 |
var invSinVal = 1 / Math.sin(angle); |
| 716 |
var c0 = Math.sin((1 - t) * angle) * invSinVal; |
| 717 |
var c1 = factor * Math.sin(t * angle) * invSinVal; |
| 718 |
|
| 719 |
resultQuat[0] = q0[0] * c0 + q1[0] * c1; |
| 720 |
resultQuat[1] = q0[1] * c0 + q1[1] * c1; |
| 721 |
resultQuat[2] = q0[2] * c0 + q1[2] * c1; |
| 722 |
resultQuat[3] = q0[3] * c0 + q1[3] * c1; |
| 723 |
return resultQuat; |
| 724 |
}; |
| 725 |
|
| 726 |
|
| 727 |
/** |
| 728 |
* Compute the simple linear interpolation of the two quaternions q0 and q1 |
| 729 |
* according to the coefficient t. The resulting quaternion is stored in |
| 730 |
* resultVec. |
| 731 |
* |
| 732 |
* @param {!goog.vec.Quaternion.AnyType} q0 The first quaternion. |
| 733 |
* @param {!goog.vec.Quaternion.AnyType} q1 The second quaternion. |
| 734 |
* @param {number} t The interpolation factor. |
| 735 |
* @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to |
| 736 |
* receive the results (may be q0 or q1). |
| 737 |
*/ |
| 738 |
goog.vec.Quaternion.nlerp = goog.vec.Vec4.lerp; |