diff --git a/Source/OpenTK/Math/Vector2.cs b/Source/OpenTK/Math/Vector2.cs index ebfc8101..da08ae5a 100644 --- a/Source/OpenTK/Math/Vector2.cs +++ b/Source/OpenTK/Math/Vector2.cs @@ -885,6 +885,41 @@ namespace OpenTK #endregion + #region Transform + + /// + /// Transforms a vector by a quaternion rotation. + /// + /// The vector to transform. + /// The quaternion to rotate the vector by. + /// The result of the operation. + public static Vector2 Transform(Vector2 vec, Quaternion quat) + { + Vector2 result; + Transform(ref vec, ref quat, out result); + return result; + } + + /// + /// Transforms a vector by a quaternion rotation. + /// + /// The vector to transform. + /// The quaternion to rotate the vector by. + /// The result of the operation. + public static void Transform(ref Vector2 vec, ref Quaternion quat, out Vector2 result) + { + Quaternion v = new Quaternion() { X = vec.X, Y = vec.Y, Z = 0, W = 0 }; + Quaternion i; + Quaternion t; + Quaternion.Invert(ref quat, out i); + t = i * v; + v = t * quat; + + result = new Vector2(v.X, v.Y); + } + + #endregion + #endregion #region Operators diff --git a/Source/OpenTK/Math/Vector2d.cs b/Source/OpenTK/Math/Vector2d.cs index 48794668..98c6dfd4 100644 --- a/Source/OpenTK/Math/Vector2d.cs +++ b/Source/OpenTK/Math/Vector2d.cs @@ -773,6 +773,41 @@ namespace OpenTK #endregion + #region Transform + + /// + /// Transforms a vector by a quaternion rotation. + /// + /// The vector to transform. + /// The quaternion to rotate the vector by. + /// The result of the operation. + public static Vector2d Transform(Vector2d vec, Quaterniond quat) + { + Vector2d result; + Transform(ref vec, ref quat, out result); + return result; + } + + /// + /// Transforms a vector by a quaternion rotation. + /// + /// The vector to transform. + /// The quaternion to rotate the vector by. + /// The result of the operation. + public static void Transform(ref Vector2d vec, ref Quaterniond quat, out Vector2d result) + { + Quaterniond v = new Quaterniond() { X = vec.X, Y = vec.Y, Z = 0, W = 0 }; + Quaterniond i; + Quaterniond t; + Quaterniond.Invert(ref quat, out i); + t = i * v; + v = t * quat; + + result = new Vector2d(v.X, v.Y); + } + + #endregion + #endregion #region Operators diff --git a/Source/OpenTK/Math/Vector3.cs b/Source/OpenTK/Math/Vector3.cs index c3c85c09..a34584de 100644 --- a/Source/OpenTK/Math/Vector3.cs +++ b/Source/OpenTK/Math/Vector3.cs @@ -1094,6 +1094,37 @@ namespace OpenTK result = v4.Xyz; } + /// + /// Transforms a vector by a quaternion rotation. + /// + /// The vector to transform. + /// The quaternion to rotate the vector by. + /// The result of the operation. + public static Vector3 Transform(Vector3 vec, Quaternion quat) + { + Vector3 result; + Transform(ref vec, ref quat, out result); + return result; + } + + /// + /// Transforms a vector by a quaternion rotation. + /// + /// The vector to transform. + /// The quaternion to rotate the vector by. + /// The result of the operation. + public static void Transform(ref Vector3 vec, ref Quaternion quat, out Vector3 result) + { + Quaternion v = new Quaternion() { X = vec.X, Y = vec.Y, Z = vec.Z, W = 0 }; + Quaternion i; + Quaternion t; + Quaternion.Invert(ref quat, out i); + t = i * v; + v = t * quat; + + result = new Vector3(v.X, v.Y, v.Z); + } + /// Transform a Vector3 by the given Matrix, and project the resulting Vector4 back to a Vector3 /// The vector to transform /// The desired transformation diff --git a/Source/OpenTK/Math/Vector3d.cs b/Source/OpenTK/Math/Vector3d.cs index a5557c32..d924395b 100644 --- a/Source/OpenTK/Math/Vector3d.cs +++ b/Source/OpenTK/Math/Vector3d.cs @@ -1090,6 +1090,37 @@ namespace OpenTK result = v4.Xyz; } + /// + /// Transforms a vector by a quaternion rotation. + /// + /// The vector to transform. + /// The quaternion to rotate the vector by. + /// The result of the operation. + public static Vector3d Transform(Vector3d vec, Quaterniond quat) + { + Vector3d result; + Transform(ref vec, ref quat, out result); + return result; + } + + /// + /// Transforms a vector by a quaternion rotation. + /// + /// The vector to transform. + /// The quaternion to rotate the vector by. + /// The result of the operation. + public static void Transform(ref Vector3d vec, ref Quaterniond quat, out Vector3d result) + { + Quaterniond v = new Quaterniond() { X = vec.X, Y = vec.Y, Z = vec.Z, W = 0 }; + Quaterniond i; + Quaterniond t; + Quaterniond.Invert(ref quat, out i); + t = i * v; + v = t * quat; + + result = new Vector3d(v.X, v.Y, v.Z); + } + /// /// Transform a Vector3d by the given Matrix, and project the resulting Vector4 back to a Vector3 /// diff --git a/Source/OpenTK/Math/Vector4.cs b/Source/OpenTK/Math/Vector4.cs index dcf756b1..9b09ee76 100644 --- a/Source/OpenTK/Math/Vector4.cs +++ b/Source/OpenTK/Math/Vector4.cs @@ -942,6 +942,37 @@ namespace OpenTK vec.X * mat.Row0.W + vec.Y * mat.Row1.W + vec.Z * mat.Row2.W + vec.W * mat.Row3.W); } + /// + /// Transforms a vector by a quaternion rotation. + /// + /// The vector to transform. + /// The quaternion to rotate the vector by. + /// The result of the operation. + public static Vector4 Transform(Vector4 vec, Quaternion quat) + { + Vector4 result; + Transform(ref vec, ref quat, out result); + return result; + } + + /// + /// Transforms a vector by a quaternion rotation. + /// + /// The vector to transform. + /// The quaternion to rotate the vector by. + /// The result of the operation. + public static void Transform(ref Vector4 vec, ref Quaternion quat, out Vector4 result) + { + Quaternion v = new Quaternion() { X = vec.X, Y = vec.Y, Z = vec.Z, W = vec.W }; + Quaternion i; + Quaternion t; + Quaternion.Invert(ref quat, out i); + t = i * v; + v = t * quat; + + result = new Vector4(v.X, v.Y, v.Z, v.W); + } + #endregion #endregion diff --git a/Source/OpenTK/Math/Vector4d.cs b/Source/OpenTK/Math/Vector4d.cs index 2072813b..23f0e6c6 100644 --- a/Source/OpenTK/Math/Vector4d.cs +++ b/Source/OpenTK/Math/Vector4d.cs @@ -945,6 +945,37 @@ namespace OpenTK vec.X * mat.Row0.W + vec.Y * mat.Row1.W + vec.Z * mat.Row2.W + vec.W * mat.Row3.W); } + /// + /// Transforms a vector by a quaternion rotation. + /// + /// The vector to transform. + /// The quaternion to rotate the vector by. + /// The result of the operation. + public static Vector4d Transform(Vector4d vec, Quaterniond quat) + { + Vector4d result; + Transform(ref vec, ref quat, out result); + return result; + } + + /// + /// Transforms a vector by a quaternion rotation. + /// + /// The vector to transform. + /// The quaternion to rotate the vector by. + /// The result of the operation. + public static void Transform(ref Vector4d vec, ref Quaterniond quat, out Vector4d result) + { + Quaterniond v = new Quaterniond() { X = vec.X, Y = vec.Y, Z = vec.Z, W = vec.W }; + Quaterniond i; + Quaterniond t; + Quaterniond.Invert(ref quat, out i); + t = i * v; + v = t * quat; + + result = new Vector4d(v.X, v.Y, v.Z, v.W); + } + #endregion #endregion