From 66cd883fb431c84a0977d1246b7f066d93996895 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 4 Jan 2016 12:34:02 +0200 Subject: [PATCH] Multiplication operator for Matrix-Vector transformation and Quaternion-Vector transformations --- Source/OpenTK/Math/Vector3.cs | 51 ++++++++++++++++++++++++++++++++++- Source/OpenTK/Math/Vector4.cs | 28 ++++++++++++++++++- 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/Source/OpenTK/Math/Vector3.cs b/Source/OpenTK/Math/Vector3.cs index bd0ee0cb..cfa6f32a 100644 --- a/Source/OpenTK/Math/Vector3.cs +++ b/Source/OpenTK/Math/Vector3.cs @@ -1114,6 +1114,29 @@ namespace OpenTK mat.Row3.Z; } + /// Transform a Vector by the given Matrix + /// The vector to transform + /// The desired transformation + /// The transformed vector + public static Vector3 Transform(Vector3 vec, Matrix3 mat) + { + Vector3 result; + Transform(ref vec, ref mat, out result); + return result; + } + + /// Transform a Vector by the given Matrix + /// The vector to transform + /// The desired transformation + /// The transformed vector + public static void Transform(ref Vector3 vec, ref Matrix3 mat, out Vector3 result) + { + result = new Vector3( + vec.X * mat.Row0.X + vec.Y * mat.Row1.X + vec.Z * mat.Row2.X, + vec.X * mat.Row0.Y + vec.Y * mat.Row1.Y + vec.Z * mat.Row2.Y, + vec.X * mat.Row0.Z + vec.Y * mat.Row1.Z + vec.Z * mat.Row2.Z); + } + /// Transform a Vector by the given Matrix /// The vector to transform /// The desired transformation @@ -1502,7 +1525,33 @@ namespace OpenTK vec.Z *= scale.Z; return vec; } - + + /// + /// Transform a Vector by the given Matrix. + /// + /// The vector to transform + /// The desired transformation + /// + public static Vector3 operator *(Matrix3 mat, Vector3 vec) + { + Vector3 result; + Vector3.Transform(ref vec, ref mat, out result); + return result; + } + + /// + /// Transforms a vector by a quaternion rotation. + /// + /// The vector to transform. + /// The quaternion to rotate the vector by. + /// + public static Vector3 operator *(Quaternion quat, Vector3 vec) + { + Vector3 result; + Vector3.Transform(ref vec, ref quat, out result); + return result; + } + /// /// Divides an instance by a scalar. /// diff --git a/Source/OpenTK/Math/Vector4.cs b/Source/OpenTK/Math/Vector4.cs index 98f374fc..58e729a9 100644 --- a/Source/OpenTK/Math/Vector4.cs +++ b/Source/OpenTK/Math/Vector4.cs @@ -1505,7 +1505,33 @@ namespace OpenTK vec.W *= scale.W; return vec; } - + + /// + /// Transform a Vector by the given Matrix. + /// + /// The vector to transform + /// The desired transformation + /// + public static Vector4 operator *(Matrix4 mat, Vector4 vec) + { + Vector4 result; + Vector4.Transform(ref vec, ref mat, out result); + return result; + } + + /// + /// Transforms a vector by a quaternion rotation. + /// + /// The vector to transform. + /// The quaternion to rotate the vector by. + /// + public static Vector4 operator *(Quaternion quat, Vector4 vec) + { + Vector4 result; + Vector4.Transform(ref vec, ref quat, out result); + return result; + } + /// /// Divides an instance by a scalar. ///