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 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
|
// 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.provide('goog.vec.Quaternion.AnyType');
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;
/**
* Creates a Float32 quaternion, initialized to the identity.
*
* @return {!goog.vec.Quaternion.Float32} The new quaternion.
*/
goog.vec.Quaternion.createIdentityFloat32 = function() {
var quat = goog.vec.Quaternion.createFloat32();
goog.vec.Quaternion.makeIdentity(quat);
return quat;
};
/**
* Creates a Float64 quaternion, initialized to the identity.
*
* @return {!goog.vec.Quaternion.Float64} The new quaternion.
*/
goog.vec.Quaternion.createIdentityFloat64 = function() {
var quat = goog.vec.Quaternion.createFloat64();
goog.vec.Quaternion.makeIdentity(quat);
return quat;
};
/**
* 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 inverse of the quaternion in quat, storing the result into
* resultQuat.
*
* If the quaternion is already normalized, goog.vec.Quaternion.conjugate
* is faster than this function and produces the same result.
*
* @param {!goog.vec.Quaternion.AnyType} quat The quaternion to invert.
* @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to receive
* the result.
* @return {!goog.vec.Quaternion.AnyType} Return resultQuat so that
* operations can be chained together.
*/
goog.vec.Quaternion.invert = function(quat, resultQuat) {
var a0 = quat[0], a1 = quat[1], a2 = quat[2], a3 = quat[3];
var dot = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3;
var invDot = dot ? 1.0 / dot : 0;
resultQuat[0] = -a0 * invDot;
resultQuat[1] = -a1 * invDot;
resultQuat[2] = -a2 * invDot;
resultQuat[3] = a3 * invDot;
return resultQuat;
};
/**
* Computes the conjugate of the quaternion in quat, storing the result into
* resultQuat.
*
* If the quaternion is normalized already, this function is faster than
* goog.Quaternion.inverse and produces the same result.
*
* @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 resultQuat 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 resultQuat 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;
};
/**
* Makes the given quaternion the identity quaternion (0, 0, 0, 1).
*
* @param {!goog.vec.Quaternion.AnyType} quat The quaternion.
* @return {!goog.vec.Quaternion.AnyType} Return quat so that
* operations can be chained together.
*/
goog.vec.Quaternion.makeIdentity = function(quat) {
quat[0] = 0;
quat[1] = 0;
quat[2] = 0;
quat[3] = 1;
return quat;
};
/**
* 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 quat 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 3x3 rotation matrix.
*
* Perf: http://jsperf.com/conversion-of-3x3-matrix-to-quaternion
* http://jsperf.com/goog-vec-fromrotationmatrix3-a
*
* @param {!goog.vec.AnyType} matrix The source matrix.
* @param {!goog.vec.Quaternion.AnyType} quat The resulting quaternion.
* @return {!goog.vec.Quaternion.AnyType} Return quat so that
* operations can be chained together.
*/
goog.vec.Quaternion.fromRotationMatrix3 = function(matrix, quat) {
// Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
// article "Quaternion Calculus and Fast Animation".
var fTrace = matrix[0] + matrix[4] + matrix[8];
var fRoot;
if (fTrace > 0.0) {
// |w| > 1/2, may as well choose w > 1/2
fRoot = Math.sqrt(fTrace + 1.0); // 2w
quat[3] = 0.5 * fRoot;
fRoot = 0.5 / fRoot; // 1 / (4w)
quat[0] = (matrix[5] - matrix[7]) * fRoot;
quat[1] = (matrix[6] - matrix[2]) * fRoot;
quat[2] = (matrix[1] - matrix[3]) * fRoot;
} else {
// |w| <= 1/2
var i = 0;
if (matrix[4] > matrix[0]) i = 1;
if (matrix[8] > matrix[i * 3 + i]) i = 2;
var j = (i + 1) % 3;
var k = (i + 2) % 3;
fRoot = Math.sqrt(
matrix[i * 3 + i] - matrix[j * 3 + j] - matrix[k * 3 + k] + 1.0);
quat[i] = 0.5 * fRoot;
fRoot = 0.5 / fRoot;
quat[3] = (matrix[j * 3 + k] - matrix[k * 3 + j]) * fRoot;
quat[j] = (matrix[j * 3 + i] + matrix[i * 3 + j]) * fRoot;
quat[k] = (matrix[k * 3 + i] + matrix[i * 3 + k]) * fRoot;
// Flip all signs if w is negative.
if (quat[3] < 0) {
quat[0] = -quat[0];
quat[1] = -quat[1];
quat[2] = -quat[2];
quat[3] = -quat[3];
}
}
return quat;
};
/**
* Generates the quaternion from the given 4x4 rotation matrix.
*
* Perf: http://jsperf.com/goog-vec-fromrotationmatrix4
*
* Implementation is the same as fromRotationMatrix3 but using indices from
* the top left 3x3 in a 4x4 matrix.
*
* @param {!goog.vec.AnyType} matrix The source matrix.
* @param {!goog.vec.Quaternion.AnyType} quat The resulting quaternion.
* @return {!goog.vec.Quaternion.AnyType} Return quat so that
* operations can be chained together.
*/
goog.vec.Quaternion.fromRotationMatrix4 = function(matrix, quat) {
var fTrace = matrix[0] + matrix[5] + matrix[10];
var fRoot;
if (fTrace > 0.0) {
// |w| > 1/2, may as well choose w > 1/2
fRoot = Math.sqrt(fTrace + 1.0); // 2w
quat[3] = 0.5 * fRoot;
fRoot = 0.5 / fRoot; // 1 / (4w)
quat[0] = (matrix[6] - matrix[9]) * fRoot;
quat[1] = (matrix[8] - matrix[2]) * fRoot;
quat[2] = (matrix[1] - matrix[4]) * fRoot;
} else {
// |w| <= 1/2
var i = 0;
if (matrix[5] > matrix[0]) i = 1;
if (matrix[10] > matrix[i * 4 + i]) i = 2;
var j = (i + 1) % 3;
var k = (i + 2) % 3;
fRoot = Math.sqrt(
matrix[i * 4 + i] - matrix[j * 4 + j] - matrix[k * 4 + k] + 1.0);
quat[i] = 0.5 * fRoot;
fRoot = 0.5 / fRoot;
quat[3] = (matrix[j * 4 + k] - matrix[k * 4 + j]) * fRoot;
quat[j] = (matrix[j * 4 + i] + matrix[i * 4 + j]) * fRoot;
quat[k] = (matrix[k * 4 + i] + matrix[i * 4 + k]) * fRoot;
// Flip all signs if w is negative.
if (quat[3] < 0) {
quat[0] = -quat[0];
quat[1] = -quat[1];
quat[2] = -quat[2];
quat[3] = -quat[3];
}
}
return quat;
};
/**
* Generates the 3x3 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.toRotationMatrix3 = 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] = xy - wz;
matrix[4] = 1 - (xx + zz);
matrix[5] = yz + wx;
matrix[6] = xz + wy;
matrix[7] = yz - wx;
matrix[8] = 1 - (xx + yy);
return matrix;
};
/**
* Generates the 4x4 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;
};
/**
* Rotates a quaternion by the given angle about the X axis.
*
* @param {!goog.vec.Quaternion.AnyType} quat The quaternion.
* @param {number} angle The angle in radians.
* @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to
* receive the result.
* @return {!goog.vec.Quaternion.AnyType} Return resultQuat so that
* operations can be chained together.
*/
goog.vec.Quaternion.rotateX = function(quat, angle, resultQuat) {
angle *= 0.5;
var ax = quat[0], ay = quat[1], az = quat[2], aw = quat[3];
var bx = Math.sin(angle), bw = Math.cos(angle);
resultQuat[0] = ax * bw + aw * bx;
resultQuat[1] = ay * bw + az * bx;
resultQuat[2] = az * bw - ay * bx;
resultQuat[3] = aw * bw - ax * bx;
return resultQuat;
};
/**
* Rotates a quaternion by the given angle about the Y axis.
*
* @param {!goog.vec.Quaternion.AnyType} quat The quaternion.
* @param {number} angle The angle in radians.
* @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to
* receive the result.
* @return {!goog.vec.Quaternion.AnyType} Return resultQuat so that
* operations can be chained together.
*/
goog.vec.Quaternion.rotateY = function(quat, angle, resultQuat) {
angle *= 0.5;
var ax = quat[0], ay = quat[1], az = quat[2], aw = quat[3];
var by = Math.sin(angle), bw = Math.cos(angle);
resultQuat[0] = ax * bw - az * by;
resultQuat[1] = ay * bw + aw * by;
resultQuat[2] = az * bw + ax * by;
resultQuat[3] = aw * bw - ay * by;
return resultQuat;
};
/**
* Rotates a quaternion by the given angle about the Z axis.
*
* @param {!goog.vec.Quaternion.AnyType} quat The quaternion.
* @param {number} angle The angle in radians.
* @param {!goog.vec.Quaternion.AnyType} resultQuat The quaternion to
* receive the result.
* @return {!goog.vec.Quaternion.AnyType} Return resultQuat so that
* operations can be chained together.
*/
goog.vec.Quaternion.rotateZ = function(quat, angle, resultQuat) {
angle *= 0.5;
var ax = quat[0], ay = quat[1], az = quat[2], aw = quat[3];
var bz = Math.sin(angle), bw = Math.cos(angle);
resultQuat[0] = ax * bw + ay * bz;
resultQuat[1] = ay * bw - ax * bz;
resultQuat[2] = az * bw + aw * bz;
resultQuat[3] = aw * bw - az * bz;
return resultQuat;
};
/**
* Transforms a vec with a quaternion. Works on both vec3s and vec4s.
*
* @param {!goog.vec.AnyType} vec The vec to transform.
* @param {!goog.vec.Quaternion.AnyType} quat The quaternion.
* @param {!goog.vec.AnyType} resultVec The vec to receive the result.
* @return {!goog.vec.AnyType} Return resultVec so that operations can be
* chained together. Note that the caller is responsible for type-casting.
*/
goog.vec.Quaternion.transformVec = function(vec, quat, resultVec) {
var x = vec[0], y = vec[1], z = vec[2];
var qx = quat[0], qy = quat[1], qz = quat[2], qw = quat[3];
// Calculate quat * vec.
var ix = qw * x + qy * z - qz * y;
var iy = qw * y + qz * x - qx * z;
var iz = qw * z + qx * y - qy * x;
var iw = -qx * x - qy * y - qz * z;
// Calculate result * inverse quat.
resultVec[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy;
resultVec[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz;
resultVec[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx;
return resultVec;
};
/**
* 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 resultQuat 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;
|