Skip to content

Commit

Permalink
final tweaks
Browse files Browse the repository at this point in the history
change templated point classes to use the same preprocessor as the matrix class
unify tests
  • Loading branch information
marauder2k7 committed Nov 22, 2024
1 parent 52184f9 commit 6b4f052
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 144 deletions.
2 changes: 2 additions & 0 deletions Engine/source/math/mPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "math/mPoint3.h"
#include "math/mPoint4.h"

#ifndef USE_TEMPLATE_MATRIX

const Point2I Point2I::One(1, 1);
const Point2I Point2I::Zero(0, 0);
Expand Down Expand Up @@ -59,6 +60,7 @@ const Point4I Point4I::Zero(0, 0, 0, 0);
const Point4F Point4F::One(1.0f, 1.0f, 1.0f, 1.0f);
const Point4F Point4F::Zero(0.0f, 0.0f, 0.0f, 0.0f);

#endif // USE_TEMPLATE_MATRIX

Point3F mPerp( const Point3F &inVec )
{
Expand Down
5 changes: 5 additions & 0 deletions Engine/source/math/mPoint2.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include "math/mMathFn.h"
#endif

#ifndef USE_TEMPLATE_MATRIX

//------------------------------------------------------------------------------
/// 2D integer point
///
Expand Down Expand Up @@ -895,6 +897,8 @@ inline Point2D operator*(F64 mul, const Point2D& multiplicand)
return multiplicand * mul;
}

#endif // USE_TEMPLATE_MATRIX

inline F32 mDot(const Point2F &p1, const Point2F &p2)
{
return (p1.x*p2.x + p1.y*p2.y);
Expand Down Expand Up @@ -929,4 +933,5 @@ namespace DictHash
}
}


#endif // _MPOINT2_H_
68 changes: 19 additions & 49 deletions Engine/source/math/mPoint3.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "math/mPoint2.h"
#endif

#ifndef USE_TEMPLATE_MATRIX

//------------------------------------------------------------------------------
/// 3D integer point
///
Expand Down Expand Up @@ -1055,13 +1057,12 @@ inline Point3F mReflect( const Point3F &v, const Point3F &n )
return v - 2 * n * mDot( v, n );
}

/// Returns a perpendicular vector to the unit length input vector.
extern Point3F mPerp( const Point3F &normal );


//------------------------------------------------------------------------------
// Templated Point class.
//------------------------------------------------------------------------------
#else // USE_TEMPLATE_MATRIX

#pragma warning(push) // nameless struct warning.
#pragma warning(disable: 4201)
template<typename DATA_TYPE, U32 size>
Expand Down Expand Up @@ -1596,52 +1597,6 @@ class PointT {
operator DATA_TYPE* () { return (data); }
operator const DATA_TYPE* () const { return (DATA_TYPE*)(data); }

// Uncomment and replace union when template replaces other classes.
//// Accessors for x, y, z, w components, putting these in operators.
//DATA_TYPE& x() {
// // not sure if needed since we have a hard set minimum of 2.
// AssertFatal(size >= 2, "No X component in point.");
// return data[0];
//}

//const DATA_TYPE& x() const {
// // not sure if needed since we have a hard set minimum of 2.
// AssertFatal(size >= 2, "No X component in point.");
// return data[0];
//}

//DATA_TYPE& y() {
// // not sure if needed since we have a hard set minimum of 2.
// AssertFatal(size >= 2, "No Y component in point.");
// return data[1];
//}

//const DATA_TYPE& y() const {
// // not sure if needed since we have a hard set minimum of 2.
// AssertFatal(size >= 2, "No Y component in point.");
// return data[1];
//}

//DATA_TYPE& z() {
// AssertFatal(size >= 3, "No Z component in point.");
// return data[2];
//}

//const DATA_TYPE& z() const {
// AssertFatal(size >= 3, "No Z component in point.");
// return data[2];
//}

//DATA_TYPE& w() {
// AssertFatal(size >= 4, "No W component in point.");
// return data[3];
//}

//const DATA_TYPE& w() const {
// AssertFatal(size >= 4, "No W component in point.");
// return data[3];
//}

// ------ Static constants ------
const static PointT One;
const static PointT Zero;
Expand Down Expand Up @@ -1715,7 +1670,22 @@ const PointT<DATA_TYPE, size> PointT<DATA_TYPE, size>::UnitZ = [] {
return p;
}();

typedef PointT<F32, 4> Point4F;
typedef PointT<F32, 3> Point3F;
typedef PointT<F32, 2> Point2F;

typedef PointT<F64, 4> Point4D;
typedef PointT<F64, 3> Point3D;
typedef PointT<F64, 2> Point2D;

typedef PointT<S32, 4> Point4I;
typedef PointT<S32, 3> Point3I;
typedef PointT<S32, 2> Point2I;

#pragma warning(pop)
#endif // USE_TEMPLATE_MATRIX

/// Returns a perpendicular vector to the unit length input vector.
extern Point3F mPerp(const Point3F& normal);

#endif // _MPOINT3_H_
3 changes: 3 additions & 0 deletions Engine/source/math/mPoint4.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "math/mPoint3.h"
#endif

#ifndef USE_TEMPLATE_MATRIX

//------------------------------------------------------------------------------
/// 4D integer point
Expand Down Expand Up @@ -248,4 +249,6 @@ inline bool mIsNaN( const Point4F &p )
return mIsNaN_F( p.x ) || mIsNaN_F( p.y ) || mIsNaN_F( p.z ) || mIsNaN_F( p.w );
}

#endif // USE_TEMPLATE_MATRIX

#endif // _MPOINT4_H_
95 changes: 0 additions & 95 deletions Engine/source/testing/mathPointTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
#include "math/util/frustum.h"
#include "math/mathUtils.h"

typedef PointT<F32, 3> PointT3F;
typedef PointT<F32, 2> PointT2F;

TEST(PointTest, TestInitFunctions)
{
Point3F test;
Expand All @@ -28,25 +25,6 @@ TEST(PointTest, TestInitFunctions)
EXPECT_NEAR(test4.x, 1.0f, POINT_EPSILON); EXPECT_NEAR(test4.y, 0.5f, POINT_EPSILON); EXPECT_NEAR(test4.z, 0.25f, POINT_EPSILON);
}

TEST(TemplatePointTest, TestInitFunctions)
{
PointT3F test;
EXPECT_NEAR(test.x, 0.0f, POINT_EPSILON); EXPECT_NEAR(test.y, 0.0f, POINT_EPSILON); EXPECT_NEAR(test.z, 0.0f, POINT_EPSILON);

PointT3F test2(1.0f);
EXPECT_NEAR(test2.x, 1.0f, POINT_EPSILON); EXPECT_NEAR(test2.y, 1.0f, POINT_EPSILON); EXPECT_NEAR(test2.z, 1.0f, POINT_EPSILON);

PointT3F test3(test2);
EXPECT_NEAR(test3.x, 1.0f, POINT_EPSILON); EXPECT_NEAR(test3.y, 1.0f, POINT_EPSILON); EXPECT_NEAR(test3.z, 1.0f, POINT_EPSILON);

PointT3F test4(1.0f, 0.5f, 0.25f);
EXPECT_NEAR(test4.x, 1.0f, POINT_EPSILON); EXPECT_NEAR(test4.y, 0.5f, POINT_EPSILON); EXPECT_NEAR(test4.z, 0.25f, POINT_EPSILON);

PointT2F test5(1.0f, 1.0f);
PointT3F test53(test5);
EXPECT_NEAR(test53.x, 1.0f, POINT_EPSILON); EXPECT_NEAR(test53.y, 1.0f, POINT_EPSILON); EXPECT_NEAR(test53.z, 0.0f, POINT_EPSILON);
}

TEST(PointTest, TestSetFunctions)
{
Point3F test;
Expand All @@ -67,29 +45,6 @@ TEST(PointTest, TestSetFunctions)
EXPECT_TRUE(test.isZero());
}

TEST(TemplatePointTest, TestSetFunctions)
{
PointT3F test;
EXPECT_NEAR(test.x, 0.0f, POINT_EPSILON); EXPECT_NEAR(test.y, 0.0f, POINT_EPSILON); EXPECT_NEAR(test.z, 0.0f, POINT_EPSILON);

test.set(1.0f);
EXPECT_NEAR(test.x, 1.0f, POINT_EPSILON); EXPECT_NEAR(test.y, 1.0f, POINT_EPSILON); EXPECT_NEAR(test.z, 1.0f, POINT_EPSILON);

PointT3F test2(1.0f, 0.5f, 0.25f);
test.set(test2);
EXPECT_NEAR(test.x, 1.0f, POINT_EPSILON); EXPECT_NEAR(test.y, 0.5f, POINT_EPSILON); EXPECT_NEAR(test.z, 0.25f, POINT_EPSILON);

PointT2F test22(1.0f, 1.0f);
test.set(test22);
EXPECT_NEAR(test.x, 1.0f, POINT_EPSILON); EXPECT_NEAR(test.y, 1.0f, POINT_EPSILON); EXPECT_NEAR(test.z, 0.0f, POINT_EPSILON);

test.set(0.25f, 1.0f, 0.5f);
EXPECT_NEAR(test.x, 0.25f, POINT_EPSILON); EXPECT_NEAR(test.y, 1.0f, POINT_EPSILON); EXPECT_NEAR(test.z, 0.5f, POINT_EPSILON);

test.zero();
EXPECT_NEAR(test.x, 0.0f, POINT_EPSILON); EXPECT_NEAR(test.y, 0.0f, POINT_EPSILON); EXPECT_NEAR(test.z, 0.0f, POINT_EPSILON);
EXPECT_TRUE(test.isZero());
}

TEST(PointTest, TestQueryFunctions)
{
Expand Down Expand Up @@ -120,36 +75,6 @@ TEST(PointTest, TestQueryFunctions)
EXPECT_TRUE(test.isUnitLength());
}

TEST(TemplatePointTest, TestQueryFunctions)
{
PointT3F test(1.0f, 0.5f, 0.25f);
PointT3F test2(1.0f, 0.5f, 0.25f);
PointT3F test3(5.0f, 0.5f, 0.25f);

EXPECT_FALSE(test.equal(test3));
EXPECT_TRUE(test.equal(test2));

F32 testLen = test.len();
EXPECT_NEAR(testLen, 1.14564395f, POINT_EPSILON);

F32 testLenSq = test.lenSquared();
EXPECT_NEAR(testLenSq, 1.3125f, POINT_EPSILON);

U32 idx = test.getLeastComponentIndex();

EXPECT_EQ(idx, 2);

idx = test.getGreatestComponentIndex();

EXPECT_EQ(idx, 0);

EXPECT_FALSE(test.isUnitLength());

test.normalizeSafe();
EXPECT_TRUE(test.isUnitLength());

}

TEST(PointTest, TestMutatorFunctions)
{
Point3F test(1.0f, 0.5f, 0.25f);
Expand All @@ -171,23 +96,3 @@ TEST(PointTest, TestMutatorFunctions)
EXPECT_NEAR(test.x, -2.18217897f, POINT_EPSILON); EXPECT_NEAR(test.y, -1.09108949f, POINT_EPSILON); EXPECT_NEAR(test.z, -0.545544744f, POINT_EPSILON);
}

TEST(TemplatePointTest, TestMutatorFunctions)
{
PointT3F test(1.0f, 0.5f, 0.25f);
PointT3F test2(5.0f, 0.5f, 0.25f);

test.convolve(test2);
EXPECT_NEAR(test.x, 5.0f, POINT_EPSILON); EXPECT_NEAR(test.y, 0.25f, POINT_EPSILON); EXPECT_NEAR(test.z, 0.0625f, POINT_EPSILON);

test.convolveInverse(test2);
EXPECT_NEAR(test.x, 1.0f, POINT_EPSILON); EXPECT_NEAR(test.y, 0.5f, POINT_EPSILON); EXPECT_NEAR(test.z, 0.25f, POINT_EPSILON);

test.neg();
EXPECT_NEAR(test.x, -1.0f, POINT_EPSILON); EXPECT_NEAR(test.y, -0.5f, POINT_EPSILON); EXPECT_NEAR(test.z, -0.25f, POINT_EPSILON);

test.normalizeSafe();
EXPECT_NEAR(test.x, -0.872871518f, POINT_EPSILON); EXPECT_NEAR(test.y, -0.436435759f, POINT_EPSILON); EXPECT_NEAR(test.z, -0.218217880f, POINT_EPSILON);

test.normalize(2.5f);
EXPECT_NEAR(test.x, -2.18217897f, POINT_EPSILON); EXPECT_NEAR(test.y, -1.09108949f, POINT_EPSILON); EXPECT_NEAR(test.z, -0.545544744f, POINT_EPSILON);
}

0 comments on commit 6b4f052

Please sign in to comment.