From d7498d62b440b2adca952aa6d39a1c4bac8b3484 Mon Sep 17 00:00:00 2001 From: chrisbrandtner Date: Sun, 25 Jan 2009 15:06:42 +0000 Subject: [PATCH] Added ref/out overloads to all static Vector*.Transform* methods. --- Source/OpenTK/Math/Vector3.cs | 124 ++++++++++++++++++++++++++++----- Source/OpenTK/Math/Vector3d.cs | Bin 70198 -> 79624 bytes Source/OpenTK/Math/Vector4.cs | 33 +++++++-- Source/OpenTK/Math/Vector4d.cs | 31 ++++++++- 4 files changed, 164 insertions(+), 24 deletions(-) diff --git a/Source/OpenTK/Math/Vector3.cs b/Source/OpenTK/Math/Vector3.cs index fede0363..04ad3fc3 100644 --- a/Source/OpenTK/Math/Vector3.cs +++ b/Source/OpenTK/Math/Vector3.cs @@ -785,8 +785,7 @@ namespace OpenTK.Math #region Transform - /// - /// Transform a direction vector by the given Matrix + /// Transform a direction vector by the given Matrix /// Assumes the matrix has a bottom row of (0,0,0,1), that is the translation part is ignored. /// /// The vector to transform @@ -801,9 +800,28 @@ namespace OpenTK.Math return v; } - /// - /// Transform a Normal by the given Matrix + /// Transform a direction vector by the given Matrix + /// Assumes the matrix has a bottom row of (0,0,0,1), that is the translation part is ignored. /// + /// The vector to transform + /// The desired transformation + /// The transformed vector + public static void TransformVector( ref Vector3 vec, ref Matrix4 mat, out Vector3 result) + { + result.X = vec.X * mat.Row0.X + + vec.Y * mat.Row1.X + + vec.Z * mat.Row2.X; + + result.Y = vec.X * mat.Row0.Y + + vec.Y * mat.Row1.Y + + vec.Z * mat.Row2.Y; + + result.Z = vec.X * mat.Row0.Z + + vec.Y * mat.Row1.Z + + vec.Z * mat.Row2.Z; + } + + /// Transform a Normal by the given Matrix /// /// This calculates the inverse of the given matrix, use TransformNormalInverse if you /// already have the inverse to avoid this extra calculation @@ -817,9 +835,21 @@ namespace OpenTK.Math return TransformNormalInverse(norm, mat); } - /// - /// Transform a Normal by the (transpose of the) given Matrix - /// + /// Transform a Normal by the given Matrix + /// + /// This calculates the inverse of the given matrix, use TransformNormalInverse if you + /// already have the inverse to avoid this extra calculation + /// + /// The normal to transform + /// The desired transformation + /// The transformed normal + public static void TransformNormal( ref Vector3 norm, ref Matrix4 mat, out Vector3 result) + { + Matrix4 Inverse = Matrix4.Invert( mat ); + Vector3.TransformNormalInverse( ref norm, ref Inverse, out result ); + } + + /// Transform a Normal by the (transpose of the) given Matrix /// /// This version doesn't calculate the inverse matrix. /// Use this version if you already have the inverse of the desired transform to hand @@ -836,9 +866,30 @@ namespace OpenTK.Math return n; } - /// - /// Transform a Position by the given Matrix - /// + /// Transform a Normal by the (transpose of the) given Matrix + /// + /// This version doesn't calculate the inverse matrix. + /// Use this version if you already have the inverse of the desired transform to hand + /// + /// The normal to transform + /// The inverse of the desired transformation + /// The transformed normal + public static void TransformNormalInverse( ref Vector3 norm, ref Matrix4 invMat, out Vector3 result ) + { + result.X = norm.X * invMat.Row0.X + + norm.Y * invMat.Row0.Y + + norm.Z * invMat.Row0.Z; + + result.Y = norm.X * invMat.Row1.X + + norm.Y * invMat.Row1.Y + + norm.Z * invMat.Row1.Z; + + result.Z = norm.X * invMat.Row2.X + + norm.Y * invMat.Row2.Y + + norm.Z * invMat.Row2.Z; + } + + /// Transform a Position by the given Matrix /// The position to transform /// The desired transformation /// The transformed position @@ -851,9 +902,29 @@ namespace OpenTK.Math return p; } - /// - /// Transform a Vector by the given Matrix - /// + /// Transform a Position by the given Matrix + /// The position to transform + /// The desired transformation + /// The transformed position + public static void TransformPosition( ref Vector3 pos, ref Matrix4 mat, out Vector3 result ) + { + result.X = pos.X * mat.Row0.X + + pos.Y * mat.Row1.X + + pos.Z * mat.Row2.X + + mat.Row3.X; + + result.Y = pos.X * mat.Row0.Y + + pos.Y * mat.Row1.Y + + pos.Z * mat.Row2.Y + + mat.Row3.Y; + + result.Z = pos.X * mat.Row0.Z + + pos.Y * mat.Row1.Z + + pos.Z * mat.Row2.Z + + mat.Row3.Z; + } + + /// Transform a Vector by the given Matrix /// The vector to transform /// The desired transformation /// The transformed vector @@ -868,9 +939,17 @@ namespace OpenTK.Math return result; } - /// - /// Transform a Vector3 by the given Matrix, and project the resulting Vector4 back to a Vector3 - /// + /// 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 Matrix4 mat, out Vector4 result ) + { + Vector4 v4 = new Vector4( vec.X, vec.Y, vec.Z, 1.0f ); + Vector4.Transform( ref v4, ref mat, out result ); + } + + /// Transform a Vector3 by the given Matrix, and project the resulting Vector4 back to a Vector3 /// The vector to transform /// The desired transformation /// The transformed vector @@ -880,6 +959,19 @@ namespace OpenTK.Math return new Vector3(h.X / h.W, h.Y / h.W, h.Z / h.W); } + /// Transform a Vector3 by the given Matrix, and project the resulting Vector4 back to a Vector3 + /// The vector to transform + /// The desired transformation + /// The transformed vector + public static void TransformPerspective( ref Vector3 vec, ref Matrix4 mat, out Vector3 result ) + { + Vector4 h; + Vector3.Transform( ref vec, ref mat, out h ); + result.X = h.X / h.W; + result.Y = h.Y / h.W; + result.Z = h.Z / h.W; + } + #endregion #region CalculateAngle diff --git a/Source/OpenTK/Math/Vector3d.cs b/Source/OpenTK/Math/Vector3d.cs index 691c23837337bc5d8f97531b67785c42b675eafb..7789db41a22fa928304bf9edc997810d9fbcafbc 100644 GIT binary patch delta 2199 zcmbVNT}TvB6uv9#w%JlTYOZF(Xpy3&n|}!Xx>Qgkm{97HQj5k0rrT-;;X}Roq{f$l zk+4PtMVK)Vy%^S04{xr-E zS#e^+4#$NObUt(xHU%1mZW1Ek?~@-NHM*AJRXi(;5&ZP0s?>Qh5&#bul( zU8Ij%e58l|3*-nzWagHU$bK&=ru%Yr1k{ZcVDbxxM+;>q1?ol5q}DE4T_45lr)y;| zasoS^4@c%FbEFE_#7e|b>O`7@+T9wzL>!r)j$=`B_gJCp20mCT`hxf5Zje7}P3r7g z{FH^kcGeLb8b5QaD|UFPB6}j5)%1oFR6RwqJ)#}`gfh%B6|dx~#~UiB;uWpv@rK(( z+k$nQJVz@vGmlr#QEH<{Jk>dw1W(-*RPph7#>U6zslzj# zTf+CLQPNrXf*0}rhfVaaH=8h+V-Xuxex}eFrCQ)$pHY<+>s8#G;-g+fJ39!M=&Lxq z!C`r2uStMuwUYj`iRxcv6O}u}Cap3*`$$zpn{;2UYN9!MN4&LWGfU5dd+k1kbIka& zR%mL+^Eq3Ys_`+ep|rEG$?@Ug&uvwy;_Xm&rjeWn$9|WyHp=iYvC;&} h>5r|OFxPPa4Q*D*!iuZ4RKpqK;B4v?--UYj`~~u(XRQDL delta 203 zcmeBp$Fgk+%Lbcs(*?K~O*Y@UoWV4?pjBe}1SZCk$pR{DoBb}9FoESJ>pd`;Jm;Lu zWP{~KKqV}j8*Wcyfyu~BmV0K0EIWCFzR_lnw;gcPBsSN)`~)@W*-s0oTCeGSjEq8) ztA5>q*(x!4-fxk~b0UK#`&|^;EcJa0SdYVW8Ae78n5OMOO+_r*fi8AnnXJ%mw|$y0 HW05idsnSn} diff --git a/Source/OpenTK/Math/Vector4.cs b/Source/OpenTK/Math/Vector4.cs index 346dcc91..c90f3882 100644 --- a/Source/OpenTK/Math/Vector4.cs +++ b/Source/OpenTK/Math/Vector4.cs @@ -688,8 +688,6 @@ namespace OpenTK.Math result = left.X * right.X + left.Y * right.Y + left.Z * right.Z + left.W * right.W; } - - #endregion #region Lerp @@ -769,9 +767,7 @@ namespace OpenTK.Math #region Transform - /// - /// Transform a Vector by the given Matrix - /// + /// Transform a Vector by the given Matrix /// The vector to transform /// The desired transformation /// The transformed vector @@ -785,6 +781,33 @@ namespace OpenTK.Math return result; } + /// Transform a Vector by the given Matrix + /// The vector to transform + /// The desired transformation + /// The transformed vector + public static void Transform( ref Vector4 vec, ref Matrix4 mat, out Vector4 result ) + { + result.X = vec.X * mat.Row0.X + + vec.Y * mat.Row1.X + + vec.Z * mat.Row2.X + + vec.W * mat.Row3.X; + + result.Y = vec.X * mat.Row0.Y + + vec.Y * mat.Row1.Y + + vec.Z * mat.Row2.Y + + vec.W * mat.Row3.Y; + + result.Z = vec.X * mat.Row0.Z + + vec.Y * mat.Row1.Z + + vec.Z * mat.Row2.Z + + vec.W * mat.Row3.Z; + + result.W = vec.X * mat.Row0.W + + vec.Y * mat.Row1.W + + vec.Z * mat.Row2.W + + vec.W * mat.Row3.W; + } + #endregion #endregion diff --git a/Source/OpenTK/Math/Vector4d.cs b/Source/OpenTK/Math/Vector4d.cs index bf6a9e78..1db2bdde 100644 --- a/Source/OpenTK/Math/Vector4d.cs +++ b/Source/OpenTK/Math/Vector4d.cs @@ -764,9 +764,7 @@ namespace OpenTK.Math #region Transform - /// - /// Transform a Vector by the given Matrix - /// + /// Transform a Vector by the given Matrix /// The vector to transform /// The desired transformation /// The transformed vector @@ -780,6 +778,33 @@ namespace OpenTK.Math return result; } + /// Transform a Vector by the given Matrix + /// The vector to transform + /// The desired transformation + /// The transformed vector + public static void Transform( ref Vector4d vec, ref Matrix4d mat, out Vector4d result ) + { + result.X = vec.X * mat.Row0.X + + vec.Y * mat.Row1.X + + vec.Z * mat.Row2.X + + vec.W * mat.Row3.X; + + result.Y = vec.X * mat.Row0.Y + + vec.Y * mat.Row1.Y + + vec.Z * mat.Row2.Y + + vec.W * mat.Row3.Y; + + result.Z = vec.X * mat.Row0.Z + + vec.Y * mat.Row1.Z + + vec.Z * mat.Row2.Z + + vec.W * mat.Row3.Z; + + result.W = vec.X * mat.Row0.W + + vec.Y * mat.Row1.W + + vec.Z * mat.Row2.W + + vec.W * mat.Row3.W; + } + #endregion #endregion