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).
This commit is contained in:
parent
ce04edb8cb
commit
bf29fa91e7
5 changed files with 47 additions and 21 deletions
|
@ -10,26 +10,27 @@
|
|||
#ifndef __MATHDEFS_H__
|
||||
#define __MATHDEFS_H__
|
||||
|
||||
# include <limits>
|
||||
# include <math.h>
|
||||
|
||||
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
|
||||
};
|
||||
};
|
||||
return abs( lhs - rhs ) < std::numeric_limits< T >::epsilon( ) ? 0 : (lhs < rhs ? -1 : 1 );
|
||||
}
|
||||
|
||||
// set this to whichever you like
|
||||
static const Types::Enum Type = Types::RightHandedZUp;
|
||||
template< typename T >
|
||||
inline T min( T lhs, T rhs )
|
||||
{
|
||||
return lhs < rhs ? lhs : rhs;
|
||||
}
|
||||
|
||||
// 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 max( T lhs, T rhs )
|
||||
{
|
||||
return lhs > rhs ? lhs : rhs;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -8,6 +8,7 @@ using namespace igloo;
|
|||
#include "math/Vector3.h"
|
||||
|
||||
// Testing headers:
|
||||
#include "tests/TestsMathDefs.h"
|
||||
#include "tests/TestsVector3.h"
|
||||
|
||||
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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 )
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue