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 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
|
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Globalization;
namespace System.Numerics
{
/// <summary>
/// A structure encapsulating a four-dimensional vector (x,y,z,w),
/// which is used to efficiently rotate an object about the (x,y,z) vector by the angle theta, where w = cos(theta/2).
/// </summary>
public struct Quaternion : IEquatable<Quaternion>
{
/// <summary>
/// Specifies the X-value of the vector component of the Quaternion.
/// </summary>
public float X;
/// <summary>
/// Specifies the Y-value of the vector component of the Quaternion.
/// </summary>
public float Y;
/// <summary>
/// Specifies the Z-value of the vector component of the Quaternion.
/// </summary>
public float Z;
/// <summary>
/// Specifies the rotation component of the Quaternion.
/// </summary>
public float W;
/// <summary>
/// Returns a Quaternion representing no rotation.
/// </summary>
public static Quaternion Identity
{
get { return new Quaternion(0, 0, 0, 1); }
}
/// <summary>
/// Returns whether the Quaternion is the identity Quaternion.
/// </summary>
public bool IsIdentity
{
get { return X == 0f && Y == 0f && Z == 0f && W == 1f; }
}
/// <summary>
/// Constructs a Quaternion from the given components.
/// </summary>
/// <param name="x">The X component of the Quaternion.</param>
/// <param name="y">The Y component of the Quaternion.</param>
/// <param name="z">The Z component of the Quaternion.</param>
/// <param name="w">The W component of the Quaternion.</param>
public Quaternion(float x, float y, float z, float w)
{
this.X = x;
this.Y = y;
this.Z = z;
this.W = w;
}
/// <summary>
/// Constructs a Quaternion from the given vector and rotation parts.
/// </summary>
/// <param name="vectorPart">The vector part of the Quaternion.</param>
/// <param name="scalarPart">The rotation part of the Quaternion.</param>
public Quaternion(Vector3 vectorPart, float scalarPart)
{
X = vectorPart.X;
Y = vectorPart.Y;
Z = vectorPart.Z;
W = scalarPart;
}
/// <summary>
/// Calculates the length of the Quaternion.
/// </summary>
/// <returns>The computed length of the Quaternion.</returns>
public float Length()
{
float ls = X * X + Y * Y + Z * Z + W * W;
return (float)Math.Sqrt((double)ls);
}
/// <summary>
/// Calculates the length squared of the Quaternion. This operation is cheaper than Length().
/// </summary>
/// <returns>The length squared of the Quaternion.</returns>
public float LengthSquared()
{
return X * X + Y * Y + Z * Z + W * W;
}
/// <summary>
/// Divides each component of the Quaternion by the length of the Quaternion.
/// </summary>
/// <param name="value">The source Quaternion.</param>
/// <returns>The normalized Quaternion.</returns>
public static Quaternion Normalize(Quaternion value)
{
Quaternion ans;
float ls = value.X * value.X + value.Y * value.Y + value.Z * value.Z + value.W * value.W;
float invNorm = 1.0f / (float)Math.Sqrt((double)ls);
ans.X = value.X * invNorm;
ans.Y = value.Y * invNorm;
ans.Z = value.Z * invNorm;
ans.W = value.W * invNorm;
return ans;
}
/// <summary>
/// Creates the conjugate of a specified Quaternion.
/// </summary>
/// <param name="value">The Quaternion of which to return the conjugate.</param>
/// <returns>A new Quaternion that is the conjugate of the specified one.</returns>
public static Quaternion Conjugate(Quaternion value)
{
Quaternion ans;
ans.X = -value.X;
ans.Y = -value.Y;
ans.Z = -value.Z;
ans.W = value.W;
return ans;
}
/// <summary>
/// Returns the inverse of a Quaternion.
/// </summary>
/// <param name="value">The source Quaternion.</param>
/// <returns>The inverted Quaternion.</returns>
public static Quaternion Inverse(Quaternion value)
{
// -1 ( a -v )
// q = ( ------------- ------------- )
// ( a^2 + |v|^2 , a^2 + |v|^2 )
Quaternion ans;
float ls = value.X * value.X + value.Y * value.Y + value.Z * value.Z + value.W * value.W;
float invNorm = 1.0f / ls;
ans.X = -value.X * invNorm;
ans.Y = -value.Y * invNorm;
ans.Z = -value.Z * invNorm;
ans.W = value.W * invNorm;
return ans;
}
/// <summary>
/// Creates a Quaternion from a vector and an angle to rotate about the vector.
/// </summary>
/// <param name="axis">The vector to rotate around.</param>
/// <param name="angle">The angle, in radians, to rotate around the vector.</param>
/// <returns>The created Quaternion.</returns>
public static Quaternion CreateFromAxisAngle(Vector3 axis, float angle)
{
Quaternion ans;
float halfAngle = angle * 0.5f;
float s = (float)Math.Sin(halfAngle);
float c = (float)Math.Cos(halfAngle);
ans.X = axis.X * s;
ans.Y = axis.Y * s;
ans.Z = axis.Z * s;
ans.W = c;
return ans;
}
/// <summary>
/// Creates a new Quaternion from the given yaw, pitch, and roll, in radians.
/// </summary>
/// <param name="yaw">The yaw angle, in radians, around the Y-axis.</param>
/// <param name="pitch">The pitch angle, in radians, around the X-axis.</param>
/// <param name="roll">The roll angle, in radians, around the Z-axis.</param>
/// <returns></returns>
public static Quaternion CreateFromYawPitchRoll(float yaw, float pitch, float roll)
{
// Roll first, about axis the object is facing, then
// pitch upward, then yaw to face into the new heading
float sr, cr, sp, cp, sy, cy;
float halfRoll = roll * 0.5f;
sr = (float)Math.Sin(halfRoll);
cr = (float)Math.Cos(halfRoll);
float halfPitch = pitch * 0.5f;
sp = (float)Math.Sin(halfPitch);
cp = (float)Math.Cos(halfPitch);
float halfYaw = yaw * 0.5f;
sy = (float)Math.Sin(halfYaw);
cy = (float)Math.Cos(halfYaw);
Quaternion result;
result.X = cy * sp * cr + sy * cp * sr;
result.Y = sy * cp * cr - cy * sp * sr;
result.Z = cy * cp * sr - sy * sp * cr;
result.W = cy * cp * cr + sy * sp * sr;
return result;
}
/// <summary>
/// Creates a Quaternion from the given rotation matrix.
/// </summary>
/// <param name="matrix">The rotation matrix.</param>
/// <returns>The created Quaternion.</returns>
public static Quaternion CreateFromRotationMatrix(Matrix4x4 matrix)
{
float trace = matrix.M11 + matrix.M22 + matrix.M33;
Quaternion q = new Quaternion();
if (trace > 0.0f)
{
float s = (float)Math.Sqrt(trace + 1.0f);
q.W = s * 0.5f;
s = 0.5f / s;
q.X = (matrix.M23 - matrix.M32) * s;
q.Y = (matrix.M31 - matrix.M13) * s;
q.Z = (matrix.M12 - matrix.M21) * s;
}
else
{
if (matrix.M11 >= matrix.M22 && matrix.M11 >= matrix.M33)
{
float s = (float)Math.Sqrt(1.0f + matrix.M11 - matrix.M22 - matrix.M33);
float invS = 0.5f / s;
q.X = 0.5f * s;
q.Y = (matrix.M12 + matrix.M21) * invS;
q.Z = (matrix.M13 + matrix.M31) * invS;
q.W = (matrix.M23 - matrix.M32) * invS;
}
else if (matrix.M22 > matrix.M33)
{
float s = (float)Math.Sqrt(1.0f + matrix.M22 - matrix.M11 - matrix.M33);
float invS = 0.5f / s;
q.X = (matrix.M21 + matrix.M12) * invS;
q.Y = 0.5f * s;
q.Z = (matrix.M32 + matrix.M23) * invS;
q.W = (matrix.M31 - matrix.M13) * invS;
}
else
{
float s = (float)Math.Sqrt(1.0f + matrix.M33 - matrix.M11 - matrix.M22);
float invS = 0.5f / s;
q.X = (matrix.M31 + matrix.M13) * invS;
q.Y = (matrix.M32 + matrix.M23) * invS;
q.Z = 0.5f * s;
q.W = (matrix.M12 - matrix.M21) * invS;
}
}
return q;
}
/// <summary>
/// Calculates the dot product of two Quaternions.
/// </summary>
/// <param name="quaternion1">The first source Quaternion.</param>
/// <param name="quaternion2">The second source Quaternion.</param>
/// <returns>The dot product of the Quaternions.</returns>
public static float Dot(Quaternion quaternion1, Quaternion quaternion2)
{
return quaternion1.X * quaternion2.X +
quaternion1.Y * quaternion2.Y +
quaternion1.Z * quaternion2.Z +
quaternion1.W * quaternion2.W;
}
/// <summary>
/// Interpolates between two quaternions, using spherical linear interpolation.
/// </summary>
/// <param name="quaternion1">The first source Quaternion.</param>
/// <param name="quaternion2">The second source Quaternion.</param>
/// <param name="amount">The relative weight of the second source Quaternion in the interpolation.</param>
/// <returns>The interpolated Quaternion.</returns>
public static Quaternion Slerp(Quaternion quaternion1, Quaternion quaternion2, float amount)
{
const float epsilon = 1e-6f;
float t = amount;
float cosOmega = quaternion1.X * quaternion2.X + quaternion1.Y * quaternion2.Y +
quaternion1.Z * quaternion2.Z + quaternion1.W * quaternion2.W;
bool flip = false;
if (cosOmega < 0.0f)
{
flip = true;
cosOmega = -cosOmega;
}
float s1, s2;
if (cosOmega > (1.0f - epsilon))
{
// Too close, do straight linear interpolation.
s1 = 1.0f - t;
s2 = (flip) ? -t : t;
}
else
{
float omega = (float)Math.Acos(cosOmega);
float invSinOmega = (float)(1 / Math.Sin(omega));
s1 = (float)Math.Sin((1.0f - t) * omega) * invSinOmega;
s2 = (flip)
? (float)-Math.Sin(t * omega) * invSinOmega
: (float)Math.Sin(t * omega) * invSinOmega;
}
Quaternion ans;
ans.X = s1 * quaternion1.X + s2 * quaternion2.X;
ans.Y = s1 * quaternion1.Y + s2 * quaternion2.Y;
ans.Z = s1 * quaternion1.Z + s2 * quaternion2.Z;
ans.W = s1 * quaternion1.W + s2 * quaternion2.W;
return ans;
}
/// <summary>
/// Linearly interpolates between two quaternions.
/// </summary>
/// <param name="quaternion1">The first source Quaternion.</param>
/// <param name="quaternion2">The second source Quaternion.</param>
/// <param name="amount">The relative weight of the second source Quaternion in the interpolation.</param>
/// <returns>The interpolated Quaternion.</returns>
public static Quaternion Lerp(Quaternion quaternion1, Quaternion quaternion2, float amount)
{
float t = amount;
float t1 = 1.0f - t;
Quaternion r = new Quaternion();
float dot = quaternion1.X * quaternion2.X + quaternion1.Y * quaternion2.Y +
quaternion1.Z * quaternion2.Z + quaternion1.W * quaternion2.W;
if (dot >= 0.0f)
{
r.X = t1 * quaternion1.X + t * quaternion2.X;
r.Y = t1 * quaternion1.Y + t * quaternion2.Y;
r.Z = t1 * quaternion1.Z + t * quaternion2.Z;
r.W = t1 * quaternion1.W + t * quaternion2.W;
}
else
{
r.X = t1 * quaternion1.X - t * quaternion2.X;
r.Y = t1 * quaternion1.Y - t * quaternion2.Y;
r.Z = t1 * quaternion1.Z - t * quaternion2.Z;
r.W = t1 * quaternion1.W - t * quaternion2.W;
}
// Normalize it.
float ls = r.X * r.X + r.Y * r.Y + r.Z * r.Z + r.W * r.W;
float invNorm = 1.0f / (float)Math.Sqrt((double)ls);
r.X *= invNorm;
r.Y *= invNorm;
r.Z *= invNorm;
r.W *= invNorm;
return r;
}
/// <summary>
/// Concatenates two Quaternions; the result represents the value1 rotation followed by the value2 rotation.
/// </summary>
/// <param name="value1">The first Quaternion rotation in the series.</param>
/// <param name="value2">The second Quaternion rotation in the series.</param>
/// <returns>A new Quaternion representing the concatenation of the value1 rotation followed by the value2 rotation.</returns>
public static Quaternion Concatenate(Quaternion value1, Quaternion value2)
{
Quaternion ans;
// Concatenate rotation is actually q2 * q1 instead of q1 * q2.
// So that's why value2 goes q1 and value1 goes q2.
float q1x = value2.X;
float q1y = value2.Y;
float q1z = value2.Z;
float q1w = value2.W;
float q2x = value1.X;
float q2y = value1.Y;
float q2z = value1.Z;
float q2w = value1.W;
// cross(av, bv)
float cx = q1y * q2z - q1z * q2y;
float cy = q1z * q2x - q1x * q2z;
float cz = q1x * q2y - q1y * q2x;
float dot = q1x * q2x + q1y * q2y + q1z * q2z;
ans.X = q1x * q2w + q2x * q1w + cx;
ans.Y = q1y * q2w + q2y * q1w + cy;
ans.Z = q1z * q2w + q2z * q1w + cz;
ans.W = q1w * q2w - dot;
return ans;
}
/// <summary>
/// Flips the sign of each component of the quaternion.
/// </summary>
/// <param name="value">The source Quaternion.</param>
/// <returns>The negated Quaternion.</returns>
public static Quaternion Negate(Quaternion value)
{
Quaternion ans;
ans.X = -value.X;
ans.Y = -value.Y;
ans.Z = -value.Z;
ans.W = -value.W;
return ans;
}
/// <summary>
/// Adds two Quaternions element-by-element.
/// </summary>
/// <param name="value1">The first source Quaternion.</param>
/// <param name="value2">The second source Quaternion.</param>
/// <returns>The result of adding the Quaternions.</returns>
public static Quaternion Add(Quaternion value1, Quaternion value2)
{
Quaternion ans;
ans.X = value1.X + value2.X;
ans.Y = value1.Y + value2.Y;
ans.Z = value1.Z + value2.Z;
ans.W = value1.W + value2.W;
return ans;
}
/// <summary>
/// Subtracts one Quaternion from another.
/// </summary>
/// <param name="value1">The first source Quaternion.</param>
/// <param name="value2">The second Quaternion, to be subtracted from the first.</param>
/// <returns>The result of the subtraction.</returns>
public static Quaternion Subtract(Quaternion value1, Quaternion value2)
{
Quaternion ans;
ans.X = value1.X - value2.X;
ans.Y = value1.Y - value2.Y;
ans.Z = value1.Z - value2.Z;
ans.W = value1.W - value2.W;
return ans;
}
/// <summary>
/// Multiplies two Quaternions together.
/// </summary>
/// <param name="value1">The Quaternion on the left side of the multiplication.</param>
/// <param name="value2">The Quaternion on the right side of the multiplication.</param>
/// <returns>The result of the multiplication.</returns>
public static Quaternion Multiply(Quaternion value1, Quaternion value2)
{
Quaternion ans;
float q1x = value1.X;
float q1y = value1.Y;
float q1z = value1.Z;
float q1w = value1.W;
float q2x = value2.X;
float q2y = value2.Y;
float q2z = value2.Z;
float q2w = value2.W;
// cross(av, bv)
float cx = q1y * q2z - q1z * q2y;
float cy = q1z * q2x - q1x * q2z;
float cz = q1x * q2y - q1y * q2x;
float dot = q1x * q2x + q1y * q2y + q1z * q2z;
ans.X = q1x * q2w + q2x * q1w + cx;
ans.Y = q1y * q2w + q2y * q1w + cy;
ans.Z = q1z * q2w + q2z * q1w + cz;
ans.W = q1w * q2w - dot;
return ans;
}
/// <summary>
/// Multiplies a Quaternion by a scalar value.
/// </summary>
/// <param name="value1">The source Quaternion.</param>
/// <param name="value2">The scalar value.</param>
/// <returns>The result of the multiplication.</returns>
public static Quaternion Multiply(Quaternion value1, float value2)
{
Quaternion ans;
ans.X = value1.X * value2;
ans.Y = value1.Y * value2;
ans.Z = value1.Z * value2;
ans.W = value1.W * value2;
return ans;
}
/// <summary>
/// Divides a Quaternion by another Quaternion.
/// </summary>
/// <param name="value1">The source Quaternion.</param>
/// <param name="value2">The divisor.</param>
/// <returns>The result of the division.</returns>
public static Quaternion Divide(Quaternion value1, Quaternion value2)
{
Quaternion ans;
float q1x = value1.X;
float q1y = value1.Y;
float q1z = value1.Z;
float q1w = value1.W;
//-------------------------------------
// Inverse part.
float ls = value2.X * value2.X + value2.Y * value2.Y +
value2.Z * value2.Z + value2.W * value2.W;
float invNorm = 1.0f / ls;
float q2x = -value2.X * invNorm;
float q2y = -value2.Y * invNorm;
float q2z = -value2.Z * invNorm;
float q2w = value2.W * invNorm;
//-------------------------------------
// Multiply part.
// cross(av, bv)
float cx = q1y * q2z - q1z * q2y;
float cy = q1z * q2x - q1x * q2z;
float cz = q1x * q2y - q1y * q2x;
float dot = q1x * q2x + q1y * q2y + q1z * q2z;
ans.X = q1x * q2w + q2x * q1w + cx;
ans.Y = q1y * q2w + q2y * q1w + cy;
ans.Z = q1z * q2w + q2z * q1w + cz;
ans.W = q1w * q2w - dot;
return ans;
}
/// <summary>
/// Flips the sign of each component of the quaternion.
/// </summary>
/// <param name="value">The source Quaternion.</param>
/// <returns>The negated Quaternion.</returns>
public static Quaternion operator -(Quaternion value)
{
Quaternion ans;
ans.X = -value.X;
ans.Y = -value.Y;
ans.Z = -value.Z;
ans.W = -value.W;
return ans;
}
/// <summary>
/// Adds two Quaternions element-by-element.
/// </summary>
/// <param name="value1">The first source Quaternion.</param>
/// <param name="value2">The second source Quaternion.</param>
/// <returns>The result of adding the Quaternions.</returns>
public static Quaternion operator +(Quaternion value1, Quaternion value2)
{
Quaternion ans;
ans.X = value1.X + value2.X;
ans.Y = value1.Y + value2.Y;
ans.Z = value1.Z + value2.Z;
ans.W = value1.W + value2.W;
return ans;
}
/// <summary>
/// Subtracts one Quaternion from another.
/// </summary>
/// <param name="value1">The first source Quaternion.</param>
/// <param name="value2">The second Quaternion, to be subtracted from the first.</param>
/// <returns>The result of the subtraction.</returns>
public static Quaternion operator -(Quaternion value1, Quaternion value2)
{
Quaternion ans;
ans.X = value1.X - value2.X;
ans.Y = value1.Y - value2.Y;
ans.Z = value1.Z - value2.Z;
ans.W = value1.W - value2.W;
return ans;
}
/// <summary>
/// Multiplies two Quaternions together.
/// </summary>
/// <param name="value1">The Quaternion on the left side of the multiplication.</param>
/// <param name="value2">The Quaternion on the right side of the multiplication.</param>
/// <returns>The result of the multiplication.</returns>
public static Quaternion operator *(Quaternion value1, Quaternion value2)
{
Quaternion ans;
float q1x = value1.X;
float q1y = value1.Y;
float q1z = value1.Z;
float q1w = value1.W;
float q2x = value2.X;
float q2y = value2.Y;
float q2z = value2.Z;
float q2w = value2.W;
// cross(av, bv)
float cx = q1y * q2z - q1z * q2y;
float cy = q1z * q2x - q1x * q2z;
float cz = q1x * q2y - q1y * q2x;
float dot = q1x * q2x + q1y * q2y + q1z * q2z;
ans.X = q1x * q2w + q2x * q1w + cx;
ans.Y = q1y * q2w + q2y * q1w + cy;
ans.Z = q1z * q2w + q2z * q1w + cz;
ans.W = q1w * q2w - dot;
return ans;
}
/// <summary>
/// Multiplies a Quaternion by a scalar value.
/// </summary>
/// <param name="value1">The source Quaternion.</param>
/// <param name="value2">The scalar value.</param>
/// <returns>The result of the multiplication.</returns>
public static Quaternion operator *(Quaternion value1, float value2)
{
Quaternion ans;
ans.X = value1.X * value2;
ans.Y = value1.Y * value2;
ans.Z = value1.Z * value2;
ans.W = value1.W * value2;
return ans;
}
/// <summary>
/// Divides a Quaternion by another Quaternion.
/// </summary>
/// <param name="value1">The source Quaternion.</param>
/// <param name="value2">The divisor.</param>
/// <returns>The result of the division.</returns>
public static Quaternion operator /(Quaternion value1, Quaternion value2)
{
Quaternion ans;
float q1x = value1.X;
float q1y = value1.Y;
float q1z = value1.Z;
float q1w = value1.W;
//-------------------------------------
// Inverse part.
float ls = value2.X * value2.X + value2.Y * value2.Y +
value2.Z * value2.Z + value2.W * value2.W;
float invNorm = 1.0f / ls;
float q2x = -value2.X * invNorm;
float q2y = -value2.Y * invNorm;
float q2z = -value2.Z * invNorm;
float q2w = value2.W * invNorm;
//-------------------------------------
// Multiply part.
// cross(av, bv)
float cx = q1y * q2z - q1z * q2y;
float cy = q1z * q2x - q1x * q2z;
float cz = q1x * q2y - q1y * q2x;
float dot = q1x * q2x + q1y * q2y + q1z * q2z;
ans.X = q1x * q2w + q2x * q1w + cx;
ans.Y = q1y * q2w + q2y * q1w + cy;
ans.Z = q1z * q2w + q2z * q1w + cz;
ans.W = q1w * q2w - dot;
return ans;
}
/// <summary>
/// Returns a boolean indicating whether the two given Quaternions are equal.
/// </summary>
/// <param name="value1">The first Quaternion to compare.</param>
/// <param name="value2">The second Quaternion to compare.</param>
/// <returns>True if the Quaternions are equal; False otherwise.</returns>
public static bool operator ==(Quaternion value1, Quaternion value2)
{
return (value1.X == value2.X &&
value1.Y == value2.Y &&
value1.Z == value2.Z &&
value1.W == value2.W);
}
/// <summary>
/// Returns a boolean indicating whether the two given Quaternions are not equal.
/// </summary>
/// <param name="value1">The first Quaternion to compare.</param>
/// <param name="value2">The second Quaternion to compare.</param>
/// <returns>True if the Quaternions are not equal; False if they are equal.</returns>
public static bool operator !=(Quaternion value1, Quaternion value2)
{
return (value1.X != value2.X ||
value1.Y != value2.Y ||
value1.Z != value2.Z ||
value1.W != value2.W);
}
/// <summary>
/// Returns a boolean indicating whether the given Quaternion is equal to this Quaternion instance.
/// </summary>
/// <param name="other">The Quaternion to compare this instance to.</param>
/// <returns>True if the other Quaternion is equal to this instance; False otherwise.</returns>
public bool Equals(Quaternion other)
{
return (X == other.X &&
Y == other.Y &&
Z == other.Z &&
W == other.W);
}
/// <summary>
/// Returns a boolean indicating whether the given Object is equal to this Quaternion instance.
/// </summary>
/// <param name="obj">The Object to compare against.</param>
/// <returns>True if the Object is equal to this Quaternion; False otherwise.</returns>
public override bool Equals(object obj)
{
if (obj is Quaternion)
{
return Equals((Quaternion)obj);
}
return false;
}
/// <summary>
/// Returns a String representing this Quaternion instance.
/// </summary>
/// <returns>The string representation.</returns>
public override string ToString()
{
CultureInfo ci = CultureInfo.CurrentCulture;
return String.Format(ci, "{{X:{0} Y:{1} Z:{2} W:{3}}}", X.ToString(ci), Y.ToString(ci), Z.ToString(ci), W.ToString(ci));
}
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <returns>The hash code.</returns>
public override int GetHashCode()
{
return X.GetHashCode() + Y.GetHashCode() + Z.GetHashCode() + W.GetHashCode();
}
}
}
|