26#region Axiom LGPL License
66using System.Runtime.InteropServices;
68using System.Xml.Serialization;
75 [StructLayout(LayoutKind.Sequential)]
76 [AdvBrowsableOrder(
"W,X,Y,Z")]
77#if !CompactFramework && !WindowsCE && !PocketPC && !XBOX360 && !SILVERLIGHT && !WINDOWS_PHONE && !NETFX_CORE
92 private static readonly
int[]
next =
new int[3] { 1, 2, 0 };
94 #region Static methods
97 return Slerp(time, quatA, quatB,
false);
119 Scalar inverseSin = 1 / sin;
125 if (cos < 0 && useShortestPath)
129 Quaternion t = coeff0 * quatA + coeff1 * quatB;
135 returnvalue = (coeff0 * quatA + coeff1 * quatB);
150 Scalar halfAngle = 0.5f * angle;
154 quat.
X = sin * aXis.
X;
155 quat.
Y = sin * aXis.
Y;
156 quat.
Z = sin * aXis.
Z;
162 return Squad(t, p, a, b, q,
false);
176 Scalar slerpT = 2 * t * (1 - t);
183 return Slerp(slerpT, slerpP, slerpQ);
212 [InstanceConstructor(
"W,X,Y,Z")]
232 return X *
X +
Y *
Y +
Z *
Z +
W *
W;
254 result.
X = 1 - (fTYY + fTZZ);
255 result.
Y = fTXY + fTWZ;
256 result.
Z = fTXZ - fTWY;
281 result.
X = fTXY - fTWZ;
282 result.
Y = 1 - (fTXX + fTZZ);
283 result.
Z = fTYZ + fTWX;
308 result.
X = fTXZ + fTWY;
309 result.
Y = fTYZ - fTWX;
310 result.
Z = 1 - (fTXX + fTYY);
336 if (Math.Abs(test) > 0.499f)
353 if (Math.Abs(test) > 0.499f)
371 if (Math.Abs(test) > 0.499f)
380 #region Public methods
412 else if (test < -0.499f)
428 if (pitch <=
Scalar.Epsilon)
432 if (roll <=
Scalar.Epsilon)
484 return this.W * quat.
W + this.X * quat.
X + this.Y * quat.
Y + this.Z * quat.
Z;
517 aXis.X =
X * invLength;
518 aXis.Y =
Y * invLength;
519 aXis.Z =
Z * invLength;
551 rotation.
m00 = 1 - (tYY + tZZ);
552 rotation.
m01 = tXY - tWZ;
553 rotation.
m02 = tXZ + tWY;
554 rotation.
m10 = tXY + tWZ;
555 rotation.
m11 = 1 - (tXX + tZZ);
556 rotation.
m12 = tYZ - tWX;
557 rotation.
m20 = tXZ - tWY;
558 rotation.
m21 = tYZ + tWX;
559 rotation.
m22 = 1 - (tXX + tYY);
570 Scalar norm = this.W * this.W + this.X * this.X + this.Y * this.Y + this.Z * this.
Z;
573 Scalar inverseNorm = 1 / norm;
574 return new Quaternion(this.W * inverseNorm, -this.X * inverseNorm, -this.Y * inverseNorm, -this.Z * inverseNorm);
597 XAxis.X = rotation.
m00;
633 FromRotationMatrix(rotation);
640 public void FromRotationMatrix(Matrix3x3 matriX)
645 Scalar trace = matriX.m00 + matriX.m11 + matriX.m22;
652 root = MathHelper.Sqrt(trace + 1);
653 this.W = 0.5f * root;
657 this.X = (matriX.m21 - matriX.m12) * root;
658 this.Y = (matriX.m02 - matriX.m20) * root;
659 this.Z = (matriX.m10 - matriX.m01) * root;
666 if (matriX.m11 > matriX.m00)
670 if (matriX.m22 > matriX[i, i])
677 root = MathHelper.Sqrt(matriX[i, i] - matriX[j, j] - matriX[k, k] + 1);
681 fixed (
Scalar* apkQuat = &this.X)
683 apkQuat[i] = 0.5f * root;
686 this.W = (matriX[k, j] - matriX[j, k]) * root;
688 apkQuat[j] = (matriX[j, i] + matriX[i, j]) * root;
689 apkQuat[k] = (matriX[k, i] + matriX[i, k]) * root;
716 Scalar coeff = angle / sin;
717 returnvalue.
X = coeff *
X;
718 returnvalue.
Y = coeff *
Y;
719 returnvalue.
Z = coeff *
Z;
752 Scalar coeff = sin / angle;
754 returnvalue.
X = coeff *
X;
755 returnvalue.
Y = coeff *
Y;
756 returnvalue.
Z = coeff *
Z;
769 #region Object overloads
778 return string.Format(
"Quaternion({0}, {1}, {2}, {3})", this.X, this.Y, this.Z, this.W);
783 string[] vals = text.Replace(
"Quaternion",
"").Trim(
' ',
'(',
'[',
'<',
')',
']',
'>').Split(
',');
785 if (vals.Length != 4)
787 throw new FormatException(
string.Format(
"Cannot parse the text '{0}' because it does not have 4 parts separated by commas in the form (x,y,z,w) with optional parenthesis.", text));
794 returnvalue.
X =
Scalar.Parse(vals[0].Trim());
795 returnvalue.
Y =
Scalar.Parse(vals[1].Trim());
796 returnvalue.
Z =
Scalar.Parse(vals[2].Trim());
797 returnvalue.
W =
Scalar.Parse(vals[3].Trim());
802 throw new FormatException(
"The parts of the vectors must be decimal numbers", ex);
808 return (
int)
X ^ (int)
Y ^ (
int)
Z ^ (int)
W;
817 #region operator overloads
822 result.
W = left.
W * right.
W - left.
X * right.
X - left.
Y * right.
Y - left.
Z * right.
Z;
823 result.
X = left.
W * right.
X + left.
X * right.
W + left.
Y * right.
Z - left.
Z * right.
Y;
824 result.
Y = left.
W * right.
Y + left.
Y * right.
W + left.
Z * right.
X - left.
X * right.
Z;
825 result.
Z = left.
W * right.
Z + left.
Z * right.
W + left.
X * right.
Y - left.
Y * right.
X;
830 Scalar W = left.W * right.W - left.X * right.X - left.Y * right.Y - left.Z * right.Z;
831 Scalar X = left.W * right.X + left.X * right.W + left.Y * right.Z - left.Z * right.Y;
832 Scalar Y = left.W * right.Y + left.Y * right.W + left.Z * right.X - left.X * right.Z;
833 result.Z = left.W * right.Z + left.Z * right.W + left.X * right.Y - left.Y * right.X;
845 result.
W = left.
W * scalar;
846 result.
X = left.
X * scalar;
847 result.
Y = left.
Y * scalar;
848 result.
Z = left.
Z * scalar;
853 result.W = left.W * scalar;
854 result.X = left.X * scalar;
855 result.Y = left.Y * scalar;
856 result.Z = left.Z * scalar;
862 result.
W = left.
W + right.
W;
863 result.
X = left.
X + right.
X;
864 result.
Y = left.
Y + right.
Y;
865 result.
Z = left.
Z + right.
Z;
870 result.W = left.W + right.W;
871 result.X = left.X + right.X;
872 result.Y = left.Y + right.Y;
873 result.Z = left.Z + right.Z;
879 result.
W = left.
W - right.
W;
880 result.
X = left.
X - right.
X;
881 result.
Y = left.
Y - right.
Y;
882 result.
Z = left.
Z - right.
Z;
887 result.W = left.W - right.W;
888 result.X = left.X - right.X;
889 result.Y = left.Y - right.Y;
890 result.Z = left.Z - right.Z;
904 Negate(ref value, out value);
918 result.
W = left.
W * right.
W - left.
X * right.
X - left.
Y * right.
Y - left.
Z * right.
Z;
919 result.
X = left.
W * right.
X + left.
X * right.
W + left.
Y * right.
Z - left.
Z * right.
Y;
920 result.
Y = left.
W * right.
Y + left.
Y * right.
W + left.
Z * right.
X - left.
X * right.
Z;
921 result.
Z = left.
W * right.
Z + left.
Z * right.
W + left.
X * right.
Y - left.
Y * right.
X;
928 result.
W = scalar * right.
W;
929 result.
X = scalar * right.
X;
930 result.
Y = scalar * right.
Y;
931 result.
Z = scalar * right.
Z;
938 result.
W = left.
W * scalar;
939 result.
X = left.
X * scalar;
940 result.
Y = left.
Y * scalar;
941 result.
Z = left.
Z * scalar;
948 result.
W = left.
W + right.
W;
949 result.
X = left.
X + right.
X;
950 result.
Y = left.
Y + right.
Y;
951 result.
Z = left.
Z + right.
Z;
958 result.
W = left.
W - right.
W;
959 result.
X = left.
X - right.
X;
960 result.
Y = left.
Y - right.
Y;
961 result.
Z = left.
Z - right.
Z;
986 return !(left == right);
static Scalar Acos(Scalar d)
static Scalar Cos(Scalar d)
static Scalar Atan2(Scalar y, Scalar x)
static Scalar Sin(Scalar a)
static Scalar ToRadians(Scalar degrees)
Converts degrees to radians.
static Scalar InvSqrt(Scalar number)
static Scalar Sqrt(Scalar d)
static Scalar ToDegrees(Scalar radians)
Converts radians to degrees.
static Scalar Asin(Scalar d)
A 3x3 matrix which can represent rotations around axes.
Summary description for Quaternion.
Quaternion Inverse()
Computes the inverse of a Quaternion.
static Quaternion operator+(Quaternion left, Quaternion right)
Scalar Norm
Squared 'length' of this quaternion.
static void Multiply(ref Quaternion left, ref Scalar scalar, out Quaternion result)
static Quaternion Squad(Scalar t, Quaternion p, Quaternion a, Quaternion b, Quaternion q, bool useShortestPath)
Performs spherical quadratic interpolation.
void ToAxis(out Vector3D XAxis, out Vector3D YAxis, out Vector3D ZAxis)
Scalar Dot(Quaternion quat)
Performs a Dot Product operation on 2 Quaternions.
static void Add(ref Quaternion left, ref Quaternion right, out Quaternion result)
static Quaternion Squad(Scalar t, Quaternion p, Quaternion a, Quaternion b, Quaternion q)
static Quaternion Multiply(Quaternion left, Scalar scalar)
Vector3D ZAxis
Local Z-aXis portion of this rotation.
static Quaternion Multiply(Quaternion left, Quaternion right)
static Quaternion Slerp(Scalar time, Quaternion quatA, Quaternion quatB)
static Quaternion Parse(string text)
static readonly Quaternion Identity
An Identity Quaternion.
void ToEulerAnglesInDegrees(out Scalar pitch, out Scalar Yaw, out Scalar roll)
static Quaternion Add(Quaternion left, Quaternion right)
Quaternion Exp()
Calculates the Exponent of a Quaternion.
static Quaternion FromAngleAxis(Scalar angle, Vector3D aXis)
Creates a Quaternion from a supplied angle and aXis.
static readonly int[] next
static void Negate(ref Quaternion value)
Vector3D ToEulerAnglesInDegrees()
static Quaternion Slerp(Scalar time, Quaternion quatA, Quaternion quatB, bool useShortestPath)
static bool operator==(Quaternion left, Quaternion right)
void ToAngleAxis(ref Scalar angle, ref Vector3D aXis)
Matrix3x3 ToRotationMatrix()
Gets a 3X3 rotation matriX from this Quaternion.
Vector3D YAxis
Local Y-aXis portion of this rotation.
static Quaternion Subtract(Quaternion left, Quaternion right)
override bool Equals(object obj)
override string ToString()
Overrides the Object.ToString() method to provide a teXt representation of a Quaternion.
static Quaternion FromEulerAnglesInDegrees(Scalar pitch, Scalar Yaw, Scalar roll)
static void Negate(ref Quaternion value, out Quaternion result)
override int GetHashCode()
void ToEulerAngles(out Scalar pitch, out Scalar Yaw, out Scalar roll)
static bool operator!=(Quaternion left, Quaternion right)
static Quaternion operator*(Quaternion left, Quaternion right)
static Quaternion FromEulerAngles(Scalar pitch, Scalar Yaw, Scalar roll)
Combines the euler angles in the order Yaw, pitch, roll to create a rotation quaternion
Quaternion Log()
Calculates the logarithm of a Quaternion.
void Normalize()
Normalizes elements of this quaterion to the range [0,1].
static void Subtract(ref Quaternion left, ref Quaternion right, out Quaternion result)
Vector3D XAxis
Local X-aXis portion of this rotation.
static readonly Quaternion Zero
A Quaternion With all elements set to 0;
Quaternion(Scalar W, Scalar X, Scalar Y, Scalar Z)
Creates a new Quaternion.
static Quaternion operator-(Quaternion left, Quaternion right)
static Quaternion Negate(Quaternion value)
static void Multiply(ref Quaternion left, ref Quaternion right, out Quaternion result)
A Vector with 3 dimensions.
static readonly Vector3D XAxis
Vector3D(1,0,0)
Scalar X
This is the X value.
static readonly Vector3D ZAxis
Vector3D(0,0,1)
static readonly Vector3D YAxis
Vector3D(0,1,0)
Scalar Y
This is the Y value.
Scalar Z
This is the Z value.