Opentk/Source/OpenTK/Math/Quaternion.cs

522 lines
15 KiB
C#
Raw Normal View History

2007-11-04 15:34:04 +00:00
#region --- License ---
/*
Copyright (c) 2006 - 2008 The Open Toolkit library.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2007-11-04 15:34:04 +00:00
*/
#endregion
using System;
using System.Runtime.InteropServices;
namespace OpenTK.Math
{
/// <summary>
/// Represents a Quaternion
/// </summary>
[Serializable]
2007-11-04 15:34:04 +00:00
[StructLayout(LayoutKind.Sequential)]
public struct Quaternion
{
#region Fields
/// <summary>
/// The vector part of the quaternion
/// </summary>
public Vector3 XYZ;
2007-11-04 15:34:04 +00:00
/// <summary>
/// The w component of the quaternion
/// </summary>
public float W;
#endregion
#region Constructors
/// <summary>
/// Construct a new Quaternion from vector and w components
/// </summary>
/// <param name="v">The vector part</param>
/// <param name="w">The w part</param>
public Quaternion(Vector3 v, float w)
{
XYZ = v;
W = w;
}
/// <summary>
/// Construct a new Quaternion
/// </summary>
/// <param name="x">The x component</param>
/// <param name="y">The y component</param>
/// <param name="z">The z component</param>
/// <param name="w">The w component</param>
public Quaternion(float x, float y, float z, float w)
{
XYZ = new Vector3(x, y, z);
W = w;
}
#endregion
#region Public Members
2007-11-04 15:34:04 +00:00
#region Properties
2007-11-04 15:34:04 +00:00
/// <summary>
/// Gets or sets the X component of this instance.
/// </summary>
public float X
{
get { return XYZ.X; }
set { XYZ.X = value; }
}
/// <summary>
/// Gets or sets the Y component of this instance.
/// </summary>
public float Y
{
get { return XYZ.Y; }
set { XYZ.Y = value; }
}
/// <summary>
/// Gets or sets the Z component of this instance.
/// </summary>
public float Z
{
get { return XYZ.Z; }
set { XYZ.Z = value; }
}
#endregion
#region Instance
#region pubilc void ToAxisAngle(out Vector3 axis, out float angle)
/// <summary>
2007-11-04 15:34:04 +00:00
/// Convert the current quaternion to axis angle representation
/// </summary>
/// <param name="axis">The resultant axis</param>
/// <param name="angle">The resultant angle</param>
public void ToAxisAngle(out Vector3 axis, out float angle)
{
Quaternion q = this;
if (q.W > 1.0f)
q.Normalize();
angle = 2.0f * (float)System.Math.Acos(q.W);
float den = (float)System.Math.Sqrt(1.0 - q.W * q.W);
axis = q.XYZ;
if (den > 0.0001f)
{
axis = q.XYZ / den;
}
}
#endregion
#region public float Length
/// <summary>
/// Gets the length (magnitude) of the quaternion.
/// </summary>
/// <seealso cref="LengthSquared"/>
public float Length
{
get
{
return (float)System.Math.Sqrt(W * W + XYZ.LengthSquared);
}
}
#endregion
#region public float LengthSquared
/// <summary>
/// Gets the square of the quaternion length (magnitude).
/// </summary>
public float LengthSquared
{
get
{
return W * W + XYZ.LengthSquared;
}
}
#endregion
#region public void Normalize()
/// <summary>
/// Scales the Quaternion to unit length.
/// </summary>
public void Normalize()
{
float scale = 1.0f / this.Length;
XYZ *= scale;
W *= scale;
}
#endregion
#region public void Conjugate()
/// <summary>
/// Convert this quaternion to its conjugate
/// </summary>
public void Conjugate()
{
XYZ = -XYZ;
}
#endregion
#endregion
#region Static
2007-11-04 15:34:04 +00:00
#region Fields
/// <summary>
/// Defines the identity quaternion.
/// </summary>
public static Quaternion Identity = new Quaternion(0, 0, 0, 1);
#endregion
#region Add
/// <summary>
/// Add two quaternions
/// </summary>
/// <param name="left">The first operand</param>
/// <param name="right">The second operand</param>
/// <returns>The result of the addition</returns>
public static Quaternion Add(Quaternion left, Quaternion right)
2007-11-04 15:34:04 +00:00
{
left.XYZ += right.XYZ;
left.W += right.W;
return left;
}
/// <summary>
/// Add two quaternions
/// </summary>
/// <param name="left">The first operand</param>
/// <param name="right">The second operand</param>
/// <param name="result">The result of the addition</param>
public static void Add(ref Quaternion left, ref Quaternion right, out Quaternion result)
{
result.XYZ = left.XYZ + right.XYZ;
result.W = left.W + right.W;
}
#endregion
#region Sub
public static Quaternion Sub(Quaternion left, Quaternion right)
2007-11-04 15:34:04 +00:00
{
left.XYZ -= right.XYZ;
left.W -= right.W;
return left;
}
public static void Sub(ref Quaternion left, ref Quaternion right, out Quaternion result)
{
result.XYZ = left.XYZ - right.XYZ;
result.W = left.W - right.W;
}
2007-11-04 15:34:04 +00:00
#endregion
#region Mult
public static Quaternion Mult(Quaternion left, Quaternion right)
2007-11-04 15:34:04 +00:00
{
float w = left.W * right.W - Vector3.Dot(left.XYZ, right.XYZ);
left.XYZ = right.W * left.XYZ + left.W * right.XYZ + Vector3.Cross(left.XYZ, right.XYZ);
left.W = w;
return left;
2007-11-04 15:34:04 +00:00
}
public static void Mult(ref Quaternion left, ref Quaternion right, out Quaternion result)
2007-11-04 15:34:04 +00:00
{
result.W = left.W * right.W - Vector3.Dot(left.XYZ, right.XYZ);
result.XYZ = right.W * left.XYZ + left.W * right.XYZ + Vector3.Cross(left.XYZ, right.XYZ);
2007-11-04 15:34:04 +00:00
}
#endregion
#region Conjugate
2007-11-04 15:34:04 +00:00
/// <summary>
/// Get the conjugate of the given quaternion
/// </summary>
/// <param name="q">The quaternion</param>
/// <returns>The conjugate of the given quaternion</returns>
public static Quaternion Conjugate(Quaternion q)
{
q.XYZ = -q.XYZ;
return q;
}
2007-11-04 15:34:04 +00:00
/// <summary>
/// Get the conjugate of the given quaternion
/// </summary>
/// <param name="q">The quaternion</param>
/// <param name="result">The conjugate of the given quaternion</param>
public static void Conjugate(ref Quaternion q, out Quaternion result)
2007-11-04 15:34:04 +00:00
{
result.XYZ = -q.XYZ;
result.W = q.W;
2007-11-04 15:34:04 +00:00
}
#endregion
2007-11-04 15:34:04 +00:00
#region Invert
2007-11-04 15:34:04 +00:00
/// <summary>
/// Get the inverse of the given quaternion
/// </summary>
/// <param name="q">The quaternion to invert</param>
/// <returns>The inverse of the given quaternion</returns>
public static Quaternion Invert(Quaternion q)
{
float lengthSq = q.LengthSquared;
if (lengthSq != 0.0)
{
float i = 1.0f / lengthSq;
q.XYZ *= -i;
q.W *= i;
}
return q;
}
2007-11-04 15:34:04 +00:00
/// <summary>
/// Get the inverse of the given quaternion
/// </summary>
/// <param name="q">The quaternion to invert</param>
/// <param name="result">The inverse of the given quaternion</param>
public static void Invert(ref Quaternion q, out Quaternion result)
2007-11-04 15:34:04 +00:00
{
float lengthSq = q.LengthSquared;
if (lengthSq != 0.0)
{
float i = 1.0f / lengthSq;
result.XYZ = q.XYZ * -i;
result.W = q.W * i;
}
else
{
result = q;
}
}
2007-11-04 15:34:04 +00:00
#endregion
2007-11-04 15:34:04 +00:00
#region Normalize
2007-11-04 15:34:04 +00:00
/// <summary>
/// Scale the given quaternion to unit length
/// </summary>
/// <param name="q">The quaternion to normalize</param>
/// <returns>The normalized quaternion</returns>
public static Quaternion Normalize(Quaternion q)
{
float scale = 1.0f / q.Length;
q.XYZ *= scale;
q.W *= scale;
return q;
}
2007-11-04 15:34:04 +00:00
/// <summary>
/// Scale the given quaternion to unit length
/// </summary>
/// <param name="q">The quaternion to normalize</param>
/// <param name="result">The normalized quaternion</param>
public static void Normalize(ref Quaternion q, out Quaternion result)
{
float scale = 1.0f / q.Length;
result.XYZ = q.XYZ * scale;
result.W = q.W * scale;
}
2007-11-04 15:34:04 +00:00
#endregion
2007-11-04 15:34:04 +00:00
#region FromAxisAngle
2007-11-04 15:34:04 +00:00
/// <summary>
/// Build a quaternion from the given axis and angle
/// </summary>
/// <param name="axis">The axis to rotate about</param>
/// <param name="angle">The rotation angle in radians</param>
/// <returns></returns>
public static Quaternion FromAxisAngle(Vector3 axis, float angle)
{
if (axis.LengthSquared == 0.0f)
return Identity;
2007-11-04 15:34:04 +00:00
Quaternion result = Identity;
2007-11-04 15:34:04 +00:00
angle *= 0.5f;
axis.Normalize();
result.XYZ = axis * (float)System.Math.Sin(angle);
result.W = (float)System.Math.Cos(angle);
2007-11-04 15:34:04 +00:00
return Normalize(result);
}
2007-11-04 15:34:04 +00:00
#endregion
2007-11-04 15:34:04 +00:00
#region Slerp
2007-11-04 15:34:04 +00:00
/// <summary>
/// Do Spherical linear interpolation between two quaternions
/// </summary>
/// <param name="q1">The first quaternion</param>
/// <param name="q2">The second quaternion</param>
/// <param name="blend">The blend factor</param>
/// <returns>A smooth blend between the given quaternions</returns>
public static Quaternion Slerp(Quaternion q1, Quaternion q2, float blend)
{
// if either input is zero, return the other.
if (q1.LengthSquared == 0.0f)
{
if (q2.LengthSquared == 0.0f)
{
return Identity;
}
return q2;
}
else if (q2.LengthSquared == 0.0f)
{
return q1;
}
2007-11-04 15:34:04 +00:00
float cosHalfAngle = q1.W * q2.W + Vector3.Dot(q1.XYZ, q2.XYZ);
2007-11-04 15:34:04 +00:00
if (cosHalfAngle >= 1.0f || cosHalfAngle <= -1.0f)
{
// angle = 0.0f, so just return one input.
return q1;
}
else if (cosHalfAngle < 0.0f)
{
q2.XYZ = -q2.XYZ;
q2.W = -q2.W;
cosHalfAngle = -cosHalfAngle;
}
2007-11-04 15:34:04 +00:00
float blendA;
float blendB;
if (cosHalfAngle < 0.99f)
{
// do proper slerp for big angles
float halfAngle = (float)System.Math.Acos(cosHalfAngle);
float sinHalfAngle = (float)System.Math.Sin(halfAngle);
float oneOverSinHalfAngle = 1.0f / sinHalfAngle;
blendA = (float)System.Math.Sin(halfAngle * (1.0f - blend)) * oneOverSinHalfAngle;
blendB = (float)System.Math.Sin(halfAngle * blend) * oneOverSinHalfAngle;
}
else
{
// do lerp if angle is really small.
blendA = 1.0f - blend;
blendB = blend;
}
2007-11-04 15:34:04 +00:00
Quaternion result = new Quaternion(blendA * q1.XYZ + blendB * q2.XYZ, blendA * q1.W + blendB * q2.W);
if (result.LengthSquared > 0.0f)
return Normalize(result);
else
return Identity;
}
2007-11-04 15:34:04 +00:00
#endregion
2007-11-04 15:34:04 +00:00
#endregion
2007-11-04 15:34:04 +00:00
#region Operators
2007-11-04 15:34:04 +00:00
public static Quaternion operator +(Quaternion left, Quaternion right)
{
left.XYZ += right.XYZ;
left.W += right.W;
return left;
}
2007-11-04 15:34:04 +00:00
public static Quaternion operator -(Quaternion left, Quaternion right)
{
left.XYZ -= right.XYZ;
left.W -= right.W;
return left;
}
2007-11-04 15:34:04 +00:00
public static Quaternion operator *(Quaternion left, Quaternion right)
2007-11-04 15:34:04 +00:00
{
float w = left.W * right.W - Vector3.Dot(left.XYZ, right.XYZ);
left.XYZ = right.W * left.XYZ + left.W * right.XYZ + Vector3.Cross(left.XYZ, right.XYZ);
left.W = w;
return left;
}
2007-11-04 15:34:04 +00:00
[CLSCompliant(false)]
unsafe public static explicit operator float*(Quaternion q)
{
return &q.XYZ.X;
}
2007-11-04 15:34:04 +00:00
public static explicit operator IntPtr(Quaternion q)
{
unsafe
2007-11-04 15:34:04 +00:00
{
return (IntPtr)(&q.XYZ.X);
2007-11-04 15:34:04 +00:00
}
}
2007-11-04 15:34:04 +00:00
#endregion
2007-11-04 15:34:04 +00:00
#region Overrides
2007-11-04 15:34:04 +00:00
#region public override string ToString()
2007-11-04 15:34:04 +00:00
/// <summary>
2007-11-04 15:34:04 +00:00
/// Returns a System.String that represents the current Quaternion.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return String.Format("V: {0}, W: {1}", XYZ, W);
}
#endregion
#endregion
#endregion
2007-11-04 15:34:04 +00:00
}
}