From ee5392c60d99ff29bcfa4a2d4a8d5064152aec04 Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Thu, 23 Jan 2014 18:34:48 +0100 Subject: [PATCH] Vector::PiecewiseMultiplicationAdd and a few other minor changes in the math lib --- Code/OysterMath/LinearMath.h | 18 +++++----- Code/OysterMath/Matrix.h | 24 +++++++++++++ Code/OysterMath/OysterMath.h | 44 +++++------------------- Code/OysterMath/Vector.h | 65 ++++++++++++++++++++++++++++++++++-- 4 files changed, 105 insertions(+), 46 deletions(-) diff --git a/Code/OysterMath/LinearMath.h b/Code/OysterMath/LinearMath.h index d32ea04f..35adef43 100644 --- a/Code/OysterMath/LinearMath.h +++ b/Code/OysterMath/LinearMath.h @@ -35,7 +35,7 @@ namespace std // x2 template -::LinearAlgebra::Matrix2x2 operator * ( const ::LinearAlgebra::Matrix2x2 &left, const ::LinearAlgebra::Matrix2x2 &right ) +inline ::LinearAlgebra::Matrix2x2 operator * ( const ::LinearAlgebra::Matrix2x2 &left, const ::LinearAlgebra::Matrix2x2 &right ) { return ::LinearAlgebra::Matrix2x2( (left.m11 * right.m11) + (left.m12 * right.m21), (left.m11 * right.m12) + (left.m12 * right.m22), @@ -44,14 +44,14 @@ template } template -::LinearAlgebra::Vector2 operator * ( const ::LinearAlgebra::Matrix2x2 &matrix, const ::LinearAlgebra::Vector2 &vector ) +inline ::LinearAlgebra::Vector2 operator * ( const ::LinearAlgebra::Matrix2x2 &matrix, const ::LinearAlgebra::Vector2 &vector ) { return ::LinearAlgebra::Vector2( (matrix.m11 * vector.x) + (matrix.m12 * vector.y), (matrix.m21 * vector.x) + (matrix.m22 * vector.y) ); } template -::LinearAlgebra::Vector2 operator * ( const ::LinearAlgebra::Vector2 &vector, const ::LinearAlgebra::Matrix2x2 &left ) +inline ::LinearAlgebra::Vector2 operator * ( const ::LinearAlgebra::Vector2 &vector, const ::LinearAlgebra::Matrix2x2 &left ) { return ::LinearAlgebra::Vector2( (vector.x * matrix.m11) + (vector.y * matrix.m21), (vector.x * matrix.m12) + (vector.y * matrix.m22) ); @@ -60,7 +60,7 @@ template // x3 template -::LinearAlgebra::Matrix3x3 operator * ( const ::LinearAlgebra::Matrix3x3 &left, const ::LinearAlgebra::Matrix3x3 &right ) +inline ::LinearAlgebra::Matrix3x3 operator * ( const ::LinearAlgebra::Matrix3x3 &left, const ::LinearAlgebra::Matrix3x3 &right ) { return ::LinearAlgebra::Matrix3x3( (left.m11 * right.m11) + (left.m12 * right.m21) + (left.m13 * right.m31), (left.m11 * right.m12) + (left.m12 * right.m22) + (left.m13 * right.m32), (left.m11 * right.m13) + (left.m12 * right.m23) + (left.m13 * right.m33), (left.m21 * right.m11) + (left.m22 * right.m21) + (left.m23 * right.m31), (left.m21 * right.m12) + (left.m22 * right.m22) + (left.m23 * right.m32), (left.m21 * right.m13) + (left.m22 * right.m23) + (left.m23 * right.m33), @@ -68,7 +68,7 @@ template } template -::LinearAlgebra::Vector3 operator * ( const ::LinearAlgebra::Matrix3x3 &matrix, const ::LinearAlgebra::Vector3 &vector ) +inline ::LinearAlgebra::Vector3 operator * ( const ::LinearAlgebra::Matrix3x3 &matrix, const ::LinearAlgebra::Vector3 &vector ) { return ::LinearAlgebra::Vector3( (matrix.m11 * vector.x) + (matrix.m12 * vector.y) + (matrix.m13 * vector.z), (matrix.m21 * vector.x) + (matrix.m22 * vector.y) + (matrix.m23 * vector.z), @@ -76,7 +76,7 @@ template } template -::LinearAlgebra::Vector3 operator * ( const ::LinearAlgebra::Vector3 &vector, const ::LinearAlgebra::Matrix3x3 &left ) +inline ::LinearAlgebra::Vector3 operator * ( const ::LinearAlgebra::Vector3 &vector, const ::LinearAlgebra::Matrix3x3 &left ) { return ::LinearAlgebra::Vector3( (vector.x * matrix.m11) + (vector.y * matrix.m21) + (vector.z * matrix.m31), (vector.x * matrix.m12) + (vector.y * matrix.m22) + (vector.z * matrix.m32), @@ -86,7 +86,7 @@ template // x4 template -::LinearAlgebra::Matrix4x4 operator * ( const ::LinearAlgebra::Matrix4x4 &left, const ::LinearAlgebra::Matrix4x4 &right ) +inline ::LinearAlgebra::Matrix4x4 operator * ( const ::LinearAlgebra::Matrix4x4 &left, const ::LinearAlgebra::Matrix4x4 &right ) { return ::LinearAlgebra::Matrix4x4( (left.m11 * right.m11) + (left.m12 * right.m21) + (left.m13 * right.m31) + (left.m14 * right.m41), (left.m11 * right.m12) + (left.m12 * right.m22) + (left.m13 * right.m32) + (left.m14 * right.m42), (left.m11 * right.m13) + (left.m12 * right.m23) + (left.m13 * right.m33) + (left.m14 * right.m43), (left.m11 * right.m14) + (left.m12 * right.m24) + (left.m13 * right.m34) + (left.m14 * right.m44), (left.m21 * right.m11) + (left.m22 * right.m21) + (left.m23 * right.m31) + (left.m24 * right.m41), (left.m21 * right.m12) + (left.m22 * right.m22) + (left.m23 * right.m32) + (left.m24 * right.m42), (left.m21 * right.m13) + (left.m22 * right.m23) + (left.m23 * right.m33) + (left.m24 * right.m43), (left.m21 * right.m14) + (left.m22 * right.m24) + (left.m23 * right.m34) + (left.m24 * right.m44), @@ -95,7 +95,7 @@ template } template -::LinearAlgebra::Vector4 operator * ( const ::LinearAlgebra::Matrix4x4 &matrix, const ::LinearAlgebra::Vector4 &vector ) +inline ::LinearAlgebra::Vector4 operator * ( const ::LinearAlgebra::Matrix4x4 &matrix, const ::LinearAlgebra::Vector4 &vector ) { return ::LinearAlgebra::Vector4( (matrix.m11 * vector.x) + (matrix.m12 * vector.y) + (matrix.m13 * vector.z) + (matrix.m14 * vector.w), (matrix.m21 * vector.x) + (matrix.m22 * vector.y) + (matrix.m23 * vector.z) + (matrix.m24 * vector.w), @@ -104,7 +104,7 @@ template } template -::LinearAlgebra::Vector4 operator * ( const ::LinearAlgebra::Vector4 &vector, const ::LinearAlgebra::Matrix4x4 &matrix ) +inline ::LinearAlgebra::Vector4 operator * ( const ::LinearAlgebra::Vector4 &vector, const ::LinearAlgebra::Matrix4x4 &matrix ) { return ::LinearAlgebra::Vector4( (vector.x * matrix.m11) + (vector.y * matrix.m21) + (vector.z * matrix.m31) + (vector.w * matrix.m41), (vector.x * matrix.m12) + (vector.y * matrix.m22) + (vector.z * matrix.m32) + (vector.w * matrix.m42), diff --git a/Code/OysterMath/Matrix.h b/Code/OysterMath/Matrix.h index c7b6b9e0..5d129431 100644 --- a/Code/OysterMath/Matrix.h +++ b/Code/OysterMath/Matrix.h @@ -163,12 +163,18 @@ namespace LinearAlgebra Vector4 GetRowVector( unsigned int rowID ) const; const Vector4 & GetColumnVector( unsigned int colID ) const; }; +} +template LinearAlgebra::Matrix2x2 operator * ( const ScalarType &left, const LinearAlgebra::Matrix2x2 &right ); +template LinearAlgebra::Matrix3x3 operator * ( const ScalarType &left, const LinearAlgebra::Matrix3x3 &right ); +template LinearAlgebra::Matrix4x4 operator * ( const ScalarType &left, const LinearAlgebra::Matrix4x4 &right ); /////////////////////////////////////////////////////////////////////////////////// // Body /////////////////////////////////////////////////////////////////////////////////// +namespace LinearAlgebra +{ // Matrix2x2 /////////////////////////////////////// template @@ -753,4 +759,22 @@ namespace LinearAlgebra { return this->v[colID]; } } +template +inline LinearAlgebra::Matrix2x2 operator * ( const ScalarType &left, const LinearAlgebra::Matrix2x2 &right ) +{ + return right * left; +} + +template +inline LinearAlgebra::Matrix3x3 operator * ( const ScalarType &left, const LinearAlgebra::Matrix3x3 &right ) +{ + return right * left; +} + +template +inline LinearAlgebra::Matrix4x4 operator * ( const ScalarType &left, const LinearAlgebra::Matrix4x4 &right ) +{ + return right * left; +} + #endif \ No newline at end of file diff --git a/Code/OysterMath/OysterMath.h b/Code/OysterMath/OysterMath.h index dd4655c4..ef6dc65e 100644 --- a/Code/OysterMath/OysterMath.h +++ b/Code/OysterMath/OysterMath.h @@ -61,55 +61,29 @@ namespace Oyster { namespace Math //! Oyster's native math library inline ::Oyster::Math::Float2 & operator *= ( ::Oyster::Math::Float2 &left, const ::Oyster::Math::Float2 &right ) { - left.x *= right.x; - left.y *= right.y; - return left; + return left.PiecewiseMultiplicationAdd( right ); } inline ::Oyster::Math::Float2 operator * ( const ::Oyster::Math::Float2 &left, const ::Oyster::Math::Float2 &right ) -{ return left.PiecewiseMultiplication( right; } - -inline ::Oyster::Math::Float2 operator * ( const ::Oyster::Math::Float &left, const ::Oyster::Math::Float2 &right ) -{ return ::Oyster::Math::Float2(right) *= left; } +{ + return left.PiecewiseMultiplication( right ); +} inline ::Oyster::Math::Float3 & operator *= ( ::Oyster::Math::Float3 &left, const ::Oyster::Math::Float3 &right ) { - left.x *= right.x; - left.y *= right.y; - left.z *= right.z; - return left; + return left.PiecewiseMultiplicationAdd( right ); } inline ::Oyster::Math::Float3 operator * ( const ::Oyster::Math::Float3 &left, const ::Oyster::Math::Float3 &right ) -{ return left.PiecewiseMultiplication( right ); } - -inline ::Oyster::Math::Float3 operator * ( const ::Oyster::Math::Float &left, const ::Oyster::Math::Float3 &right ) -{ return ::Oyster::Math::Float3(right) *= left; } +{ + return left.PiecewiseMultiplication( right ); +} inline ::Oyster::Math::Float4 & operator *= ( ::Oyster::Math::Float4 &left, const ::Oyster::Math::Float4 &right ) { - left.x *= right.x; - left.y *= right.y; - left.z *= right.z; - left.w *= right.w; - return left; + return left.PiecewiseMultiplicationAdd( right ); } -inline ::Oyster::Math::Float4 operator * ( const ::Oyster::Math::Float4 &left, const ::Oyster::Math::Float4 &right ) -{ return left.PiecewiseMultiplication( right ); } - -inline ::Oyster::Math::Float4 operator * ( const ::Oyster::Math::Float &left, const ::Oyster::Math::Float4 &right ) -{ return ::Oyster::Math::Float4(right) *= left; } - -inline ::Oyster::Math::Float2x2 operator * ( const ::Oyster::Math::Float &left, const ::Oyster::Math::Float2x2 &right ) -{ return ::Oyster::Math::Float2x2(right) *= left; } - -inline ::Oyster::Math::Float3x3 operator * ( const ::Oyster::Math::Float &left, const ::Oyster::Math::Float3x3 &right ) -{ return ::Oyster::Math::Float3x3(right) *= left; } - -inline ::Oyster::Math::Float4x4 operator * ( const ::Oyster::Math::Float &left, const ::Oyster::Math::Float4x4 &right ) -{ return ::Oyster::Math::Float4x4(right) *= left; } - namespace Oyster { namespace Math2D //! Oyster's native math library specialized for 2D { using namespace ::Oyster::Math; // deliberate inheritance from ::Oyster::Math namespace diff --git a/Code/OysterMath/Vector.h b/Code/OysterMath/Vector.h index d6ab69d9..ea36cfc9 100644 --- a/Code/OysterMath/Vector.h +++ b/Code/OysterMath/Vector.h @@ -58,7 +58,10 @@ namespace LinearAlgebra ScalarType Dot( const Vector2 &vector ) const; //! @return (a.x * b.x, a.y * b.y) - Vector2 PiecewiseMultiplication( const Vector2 &vector ) const; + Vector2 PiecewiseMultiplication( const Vector2 &vector ) const; + + //! @return a = (a.x * b.x, a.y * b.y) + Vector2 & PiecewiseMultiplicationAdd( const Vector2 &vector ); Vector2 & Normalize( ); Vector2 GetNormalized( ) const; @@ -118,6 +121,9 @@ namespace LinearAlgebra //! @return (a.x * b.x, a.y * b.y, a.z * b.z) Vector3 PiecewiseMultiplication( const Vector3 &vector ) const; + //! @return a = (a.x * b.x, a.y * b.y, a.z * b.z) + Vector3 & PiecewiseMultiplicationAdd( const Vector3 &vector ); + Vector3 & Normalize( ); Vector3 GetNormalized( ) const; }; @@ -175,17 +181,27 @@ namespace LinearAlgebra ScalarType GetMagnitude( ) const; ScalarType Dot( const Vector4 &vector ) const; - //! @return (a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w ) + //! @return (a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w) Vector4 PiecewiseMultiplication( const Vector4 &vector ) const; + //! @return a = (a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w) + Vector4 & PiecewiseMultiplicationAdd( const Vector4 &vector ); + Vector4 & Normalize( ); Vector4 GetNormalized( ) const; }; +} + +template ::LinearAlgebra::Vector2 operator * ( const ScalarType &left, const ::LinearAlgebra::Vector2 &right ); +template ::LinearAlgebra::Vector3 operator * ( const ScalarType &left, const ::LinearAlgebra::Vector3 &right ); +template ::LinearAlgebra::Vector4 operator * ( const ScalarType &left, const ::LinearAlgebra::Vector4 &right ); /////////////////////////////////////////////////////////////////////////////////// // Body /////////////////////////////////////////////////////////////////////////////////// +namespace LinearAlgebra +{ // Vector2 /////////////////////////////////////// template const Vector2 Vector2::null = Vector2( 0, 0 ); @@ -342,6 +358,14 @@ namespace LinearAlgebra return Vector2( this->x * vector.x, this->y * vector.y ); } + template + inline Vector2 & Vector2::PiecewiseMultiplicationAdd( const Vector2 &vector ) + { + this->x *= vector.x; + this->y *= vector.y; + return *this; + } + template inline Vector2 & Vector2::Normalize( ) { return (*this) /= this->GetLength(); } @@ -520,6 +544,15 @@ namespace LinearAlgebra return Vector3( this->x * vector.x, this->y * vector.y, this->z * vector.z ); } + template + inline Vector3 & Vector3::PiecewiseMultiplicationAdd( const Vector3 &vector ) + { + this->x *= vector.x; + this->y *= vector.y; + this->z *= vector.z; + return *this; + } + template inline Vector3 & Vector3::Normalize( ) { return (*this) /= this->GetLength(); } @@ -704,6 +737,16 @@ namespace LinearAlgebra return Vector4( this->x * vector.x, this->y * vector.y, this->z * vector.z, this->w * vector.w ); } + template + inline Vector4 & Vector4::PiecewiseMultiplicationAdd( const Vector4 &vector ) + { + this->x *= vector.x; + this->y *= vector.y; + this->z *= vector.z; + this->w *= vector.w; + return *this; + } + template inline Vector4 & Vector4::Normalize( ) { return (*this) /= this->GetLength(); } @@ -713,4 +756,22 @@ namespace LinearAlgebra { return Vector4(*this).Normalize(); } } +template +inline ::LinearAlgebra::Vector2 operator * ( const ScalarType &left, const ::LinearAlgebra::Vector2 &right ) +{ + return right * left; +} + +template +inline ::LinearAlgebra::Vector3 operator * ( const ScalarType &left, const ::LinearAlgebra::Vector3 &right ) +{ + return right * left; +} + +template +inline ::LinearAlgebra::Vector4 operator * ( const ScalarType &left, const ::LinearAlgebra::Vector4 &right ) +{ + return right * left; +} + #endif \ No newline at end of file