From 5d245d71515ac07923f7c4f58b09376dc12d6d01 Mon Sep 17 00:00:00 2001 From: Robin Engman Date: Tue, 11 Feb 2014 14:00:38 +0100 Subject: [PATCH] Added quaternion to angular axis in Math --- Code/OysterMath/OysterMath.cpp | 46 ++++++++++++++++++++++++++++++++++ Code/OysterMath/OysterMath.h | 6 +++++ 2 files changed, 52 insertions(+) diff --git a/Code/OysterMath/OysterMath.cpp b/Code/OysterMath/OysterMath.cpp index bbaccf11..7f7b1393 100644 --- a/Code/OysterMath/OysterMath.cpp +++ b/Code/OysterMath/OysterMath.cpp @@ -96,6 +96,52 @@ namespace Oyster { namespace Math3D // return ::LinearAlgebra3D::ExtractAngularAxis( orientationMatrix ); //} + Float4 QuaternionToAngularAxis(Float4 quaternion) + { + Float4 axis = Float4::null; + Float s = sqrtf(1 - quaternion.w*quaternion.w); + + axis.w = 2*acos(quaternion.w*quaternion.w); + + if(1 - quaternion.w > 0.001f) + { + axis.x = quaternion.x/s; + axis.y = quaternion.y/s; + axis.z = quaternion.z/s; + } + else + { + axis.x = quaternion.x; + axis.y = quaternion.y; + axis.z = quaternion.z; + } + + return axis; + } + + Float4 QuaternionToAngularAxis(Quaternion quaternion) + { + Float4 axis = Float4::null; + Float s = sqrtf(1 - quaternion.real*quaternion.real); + + axis.w = 2*acos(quaternion.real*quaternion.real); + + if(1 - quaternion.real > 0.001f) + { + axis.x = quaternion.imaginary.x/s; + axis.y = quaternion.imaginary.y/s; + axis.z = quaternion.imaginary.z/s; + } + else + { + axis.x = quaternion.imaginary.x; + axis.y = quaternion.imaginary.y; + axis.z = quaternion.imaginary.z; + } + + return axis; + } + Float4x4 & TranslationMatrix( const Float3 &position, Float4x4 &targetMem ) { return ::LinearAlgebra3D::TranslationMatrix( position, targetMem ); diff --git a/Code/OysterMath/OysterMath.h b/Code/OysterMath/OysterMath.h index 23ee06db..6e8a66de 100644 --- a/Code/OysterMath/OysterMath.h +++ b/Code/OysterMath/OysterMath.h @@ -149,6 +149,12 @@ namespace Oyster { namespace Math3D //! Oyster's native math library specialized ////! Extracts the angularAxis from orientationMatrix //Float4 ExtractAngularAxis( const Float4x4 &orientationMatrix ); + //! Converts a quaternion as Float4 to angular axis as Float4 + Float4 QuaternionToAngularAxis(Float4 quaternion); + + //! Converts a quaternion to angular axis as Float4 + Float4 QuaternionToAngularAxis(Quaternion quaternion); + //! Sets and returns targetMem to a translationMatrix with position as translation. Float4x4 & TranslationMatrix( const Float3 &position, Float4x4 &targetMem = Float4x4() );