From bf29fa91e7c8ca8a8910e4a99344ab5a551daedc Mon Sep 17 00:00:00 2001 From: Brian Date: Thu, 20 Feb 2014 23:03:12 -0700 Subject: [PATCH] Playing around with some additional math functions and testing. I know I'm probably not doing the testing entirely correctly, or at least my naming of the tests sucks. Would love feedback, though of course I'll be doing research on it. With the addition of cmpf, Vector is clearly intended to be used with a floating point type, float, double, or long double. I may or may not change this. In my commercial ready version the class would support virtually any type, even a custom number class (fixed point). --- include/math/MathDefs.h | 35 ++++++++++++++++++----------------- include/math/Vector3.h | 18 +++++++++++++++--- tests/main.cpp | 1 + tests/premake4.lua | 2 +- tests/tests/TestsVector3.h | 12 ++++++++++++ 5 files changed, 47 insertions(+), 21 deletions(-) diff --git a/include/math/MathDefs.h b/include/math/MathDefs.h index 9bb1132..a8fcc47 100644 --- a/include/math/MathDefs.h +++ b/include/math/MathDefs.h @@ -10,26 +10,27 @@ #ifndef __MATHDEFS_H__ #define __MATHDEFS_H__ +# include +# include + typedef float Scalar; - // May not need this, so may be removed. - struct CoordinateSystem + template< typename T > + inline int cmpf( T lhs, T rhs ) { - struct Types - { - enum Enum - { - RightHandedZUp, // x-right, y-forward, z-up - RightHandedYUp, // x-right, y-up, z-backwards - LeftHandedZUp, // x-left y-forward, z-up - LeftHandedYUp // x-right, y-up, z-backwards - }; - }; - - // set this to whichever you like - static const Types::Enum Type = Types::RightHandedZUp; + return abs( lhs - rhs ) < std::numeric_limits< T >::epsilon( ) ? 0 : (lhs < rhs ? -1 : 1 ); + } - // If I want to provide helpers for up/down/left/right/forward/backwards vectors, then you'll need to know the coordinate system type^ - }; + template< typename T > + inline T min( T lhs, T rhs ) + { + return lhs < rhs ? lhs : rhs; + } + + template< typename T > + inline T max( T lhs, T rhs ) + { + return lhs > rhs ? lhs : rhs; + } #endif diff --git a/include/math/Vector3.h b/include/math/Vector3.h index d3f9aa3..fee6a6d 100644 --- a/include/math/Vector3.h +++ b/include/math/Vector3.h @@ -11,6 +11,7 @@ #define __VECTOR3_H__ # include "Debug.h" + # include "math/MathDefs.h" template< typename BaseType > @@ -29,6 +30,7 @@ bool operator==( const TVector3& ) const; bool operator!=( const TVector3& ) const; + TVector3 operator-( void ) const; TVector3 operator*( BaseType ) const; TVector3 operator/( BaseType ) const; TVector3 operator*( const TVector3& ) const; @@ -50,6 +52,7 @@ // Predefined helper vectors static TVector3 Zero; + static BaseType Epsilon; }; typedef TVector3< Scalar > Vec3f; @@ -71,13 +74,19 @@ template< typename BaseType > inline bool TVector3< BaseType >::operator==( const TVector3& vect ) const { - return x == vect.x && y == vect.y && z == vect.z; + return cmpf( x, vect.x ) == 0 && cmpf( y, vect.y ) == 0 && cmpf( z, vect.z ) == 0; } template< typename BaseType > inline bool TVector3< BaseType >::operator!=( const TVector3& vect ) const { - return x != vect.x || y != vect.y || z != vect.z; + return cmpf( x, vect.x ) != 0 && cmpf( y, vect.y ) != 0 && cmpf( z, vect.z ) != 0; + } + + template< typename BaseType > + inline TVector3< BaseType > TVector3< BaseType >::operator-( void ) const + { + return TVector3< BaseType >( -x, -y, -z ); } template< typename BaseType > @@ -178,6 +187,9 @@ } template< typename BaseType > - TVector3< BaseType > TVector3< BaseType >::Zero = TVector3< BaseType >(0.f, 0.f, 0.f); + TVector3< BaseType > TVector3< BaseType >::Zero = TVector3< BaseType >( 0, 0, 0 ); + + template< typename BaseType > + BaseType TVector3< BaseType >::Epsilon = std::numeric_limits< BaseType >::epsilon( ); #endif diff --git a/tests/main.cpp b/tests/main.cpp index a44ab46..0723bd7 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -8,6 +8,7 @@ using namespace igloo; #include "math/Vector3.h" // Testing headers: +#include "tests/TestsMathDefs.h" #include "tests/TestsVector3.h" diff --git a/tests/premake4.lua b/tests/premake4.lua index 91f0365..a890501 100644 --- a/tests/premake4.lua +++ b/tests/premake4.lua @@ -29,7 +29,7 @@ solution "tests" kind "ConsoleApp" language "C++" targetdir "bin" - files { "../src/**.cpp", "main.cpp", "tests/*.h" } + files { "../src/**.h", "../src/**.cpp", "main.cpp", "tests/*.h" } includedirs { "../include", diff --git a/tests/tests/TestsVector3.h b/tests/tests/TestsVector3.h index 69751bb..f129038 100644 --- a/tests/tests/TestsVector3.h +++ b/tests/tests/TestsVector3.h @@ -3,6 +3,18 @@ # include "math/Vector3.h" + When( vector3_negate ) + { + Then( it_should_be_correct ) + { + value1 = -Vec3f( 1.f, 1.f, 1.f ); + + Assert::That( value1, Equals( Vec3f( -1.f, -1.f, -1.f ) ) ); + } + + Vec3f value1; + }; + When( vector3_scalar_multiplied ) { Then( it_should_be_correct )