Skip to content

Commit 31907ce

Browse files
authored
Merge pull request #24 from orange-cpp/u/orange-cpp/improved-triangle-class
U/orange cpp/improved triangle class
2 parents 42c84f2 + afcfed4 commit 31907ce

File tree

9 files changed

+217
-87
lines changed

9 files changed

+217
-87
lines changed

include/omath/Triangle.hpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//
2+
// Created by Orange on 11/13/2024.
3+
//
4+
#pragma once
5+
#include "omath/Vector3.hpp"
6+
7+
namespace omath
8+
{
9+
template<class Vector>
10+
class Triangle final
11+
{
12+
public:
13+
constexpr Triangle() = default;
14+
constexpr Triangle(const Vector& vertex1, const Vector& vertex2, const Vector& vertex3)
15+
: m_vertex1(vertex1), m_vertex2(vertex2), m_vertex3(vertex3)
16+
{
17+
}
18+
19+
Vector3 m_vertex1;
20+
Vector3 m_vertex2;
21+
Vector3 m_vertex3;
22+
23+
[[nodiscard]]
24+
constexpr Vector3 CalculateNormal() const
25+
{
26+
const auto b = SideBVector();
27+
const auto a = SideAVector();
28+
return b.Cross(a).Normalized();
29+
}
30+
31+
[[nodiscard]]
32+
float SideALength() const
33+
{
34+
return m_vertex1.DistTo(m_vertex2);
35+
}
36+
37+
[[nodiscard]]
38+
float SideBLength() const
39+
{
40+
return m_vertex3.DistTo(m_vertex2);
41+
}
42+
43+
[[nodiscard]]
44+
constexpr Vector3 SideAVector() const
45+
{
46+
return m_vertex1 - m_vertex2;
47+
}
48+
49+
[[nodiscard]]
50+
constexpr float Hypot() const
51+
{
52+
return m_vertex1.DistTo(m_vertex3);
53+
}
54+
[[nodiscard]]
55+
constexpr bool IsRectangular() const
56+
{
57+
const auto sideA = SideALength();
58+
const auto sideB = SideBLength();
59+
const auto hypot = Hypot();
60+
61+
return std::abs(sideA*sideA + sideB*sideB - hypot*hypot) <= 0.0001f;
62+
}
63+
[[nodiscard]]
64+
constexpr Vector3 SideBVector() const
65+
{
66+
return m_vertex3 - m_vertex2;
67+
}
68+
[[nodiscard]]
69+
constexpr Vector3 MidPoint() const
70+
{
71+
return (m_vertex1 + m_vertex2 + m_vertex3) / 3;
72+
}
73+
};
74+
} // namespace omath

include/omath/Triangle3d.hpp

Lines changed: 0 additions & 36 deletions
This file was deleted.

include/omath/collision/LineTracer.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#pragma once
55

66
#include "omath/Vector3.hpp"
7-
#include "omath/Triangle3d.hpp"
7+
#include "omath/Triangle.hpp"
88

99
namespace omath::collision
1010
{
@@ -27,12 +27,12 @@ namespace omath::collision
2727

2828

2929
[[nodiscard]]
30-
static bool CanTraceLine(const Ray& ray, const Triangle3d& triangle);
30+
static bool CanTraceLine(const Ray& ray, const Triangle<Vector3>& triangle);
3131

3232

3333
// Realization of Möller–Trumbore intersection algorithm
3434
// https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm
3535
[[nodiscard]]
36-
static Vector3 GetRayHitPoint(const Ray& ray, const Triangle3d& triangle);
36+
static Vector3 GetRayHitPoint(const Ray& ray, const Triangle<Vector3>& triangle);
3737
};
3838
}

source/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ target_sources(omath PRIVATE
44
color.cpp
55
Vector4.cpp
66
Vector2.cpp
7-
Triangle3d.cpp
87
)
98

109
add_subdirectory(prediction)

source/Triangle3d.cpp

Lines changed: 0 additions & 40 deletions
This file was deleted.

source/collision/LineTracer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace omath::collision
77
{
8-
bool LineTracer::CanTraceLine(const Ray &ray, const Triangle3d &triangle)
8+
bool LineTracer::CanTraceLine(const Ray& ray, const Triangle<Vector3>& triangle)
99
{
1010
return GetRayHitPoint(ray, triangle) == ray.end;
1111
}
@@ -19,7 +19,7 @@ namespace omath::collision
1919
return DirectionVector().Normalized();
2020
}
2121

22-
Vector3 LineTracer::GetRayHitPoint(const Ray &ray, const Triangle3d &triangle)
22+
Vector3 LineTracer::GetRayHitPoint(const Ray& ray, const Triangle<Vector3>& triangle)
2323
{
2424
constexpr float kEpsilon = std::numeric_limits<float>::epsilon();
2525

@@ -41,7 +41,7 @@ namespace omath::collision
4141
const auto u = t.Dot(p) * invDet;
4242

4343

44-
if ((u < 0 && std::abs(u) > kEpsilon) || (u > 1 && std::abs(u-1) > kEpsilon))
44+
if ((u < 0 && std::abs(u) > kEpsilon) || (u > 1 && std::abs(u - 1) > kEpsilon))
4545
return ray.end;
4646

4747
const auto q = t.Cross(sideA);
@@ -59,4 +59,4 @@ namespace omath::collision
5959

6060
return ray.start + rayDir * tHit;
6161
}
62-
}
62+
} // namespace omath::collision

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ add_executable(unit-tests
1818
general/UnitTestAngles.cpp
1919
general/UnitTestViewAngles.cpp
2020
general/UnitTestAngle.cpp
21+
general/UnitTestTriangle.cpp
2122

2223
engines/UnitTestOpenGL.cpp
2324
engines/UnitTestUnityEngine.cpp

tests/general/UnitTestLineTrace.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "gtest/gtest.h"
22
#include "omath/collision/LineTracer.hpp"
3-
#include "omath/Triangle3d.hpp"
3+
#include "omath/Triangle.hpp"
44
#include "omath/Vector3.hpp"
55

66
using namespace omath;
@@ -13,7 +13,7 @@ class LineTracerTest : public ::testing::Test
1313
Vector3 vertex1{0.0f, 0.0f, 0.0f};
1414
Vector3 vertex2{1.0f, 0.0f, 0.0f};
1515
Vector3 vertex3{0.0f, 1.0f, 0.0f};
16-
Triangle3d triangle{vertex1, vertex2, vertex3};
16+
Triangle<Vector3> triangle{vertex1, vertex2, vertex3};
1717
};
1818

1919
// Test that a ray intersecting the triangle returns false for CanTraceLine
@@ -71,7 +71,7 @@ TEST_F(LineTracerTest, TriangleFarBeyondRayEndPoint)
7171
constexpr Ray ray{{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f}};
7272

7373
// Define a triangle far beyond the ray's endpoint
74-
const Triangle3d distantTriangle{
74+
constexpr Triangle<Vector3> distantTriangle{
7575
{1000.0f, 1000.0f, 1000.0f}, {1001.0f, 1000.0f, 1000.0f}, {1000.0f, 1001.0f, 1000.0f}
7676
};
7777

tests/general/UnitTestTriangle.cpp

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
//
2+
// Created by Orange on 1/6/2025.
3+
//
4+
#include "omath/Triangle.hpp"
5+
#include <gtest/gtest.h>
6+
#include <omath/Vector3.hpp>
7+
#include <cmath> // For std::sqrt, std::isinf, std::isnan
8+
9+
10+
using namespace omath;
11+
12+
class UnitTestTriangle : public ::testing::Test
13+
{
14+
protected:
15+
// Define some Triangles to use in tests
16+
Triangle<Vector3> t1;
17+
Triangle<Vector3> t2;
18+
Triangle<Vector3> t3;
19+
20+
constexpr void SetUp() override
21+
{
22+
// Triangle with vertices (0, 0, 0), (1, 0, 0), (0, 1, 0)
23+
t1 = Triangle<Vector3>(
24+
Vector3(0.0f, 0.0f, 0.0f),
25+
Vector3(1.0f, 0.0f, 0.0f),
26+
Vector3(0.0f, 1.0f, 0.0f)
27+
);
28+
29+
// Triangle with vertices (1, 2, 3), (4, 5, 6), (7, 8, 9)
30+
t2 = Triangle<Vector3>(
31+
Vector3(1.0f, 2.0f, 3.0f),
32+
Vector3(4.0f, 5.0f, 6.0f),
33+
Vector3(7.0f, 8.0f, 9.0f)
34+
);
35+
36+
// An isosceles right triangle
37+
t3 = Triangle<Vector3>(
38+
Vector3(0.0f, 0.0f, 0.0f),
39+
Vector3(2.0f, 0.0f, 0.0f),
40+
Vector3(0.0f, 2.0f, 0.0f)
41+
);
42+
}
43+
};
44+
45+
// Test constructor and vertices
46+
TEST_F(UnitTestTriangle, Constructor)
47+
{
48+
constexpr Triangle<Vector3> t(
49+
Vector3(1.0f, 2.0f, 3.0f),
50+
Vector3(4.0f, 5.0f, 6.0f),
51+
Vector3(7.0f, 8.0f, 9.0f)
52+
);
53+
54+
EXPECT_FLOAT_EQ(t.m_vertex1.x, 1.0f);
55+
EXPECT_FLOAT_EQ(t.m_vertex1.y, 2.0f);
56+
EXPECT_FLOAT_EQ(t.m_vertex1.z, 3.0f);
57+
58+
EXPECT_FLOAT_EQ(t.m_vertex2.x, 4.0f);
59+
EXPECT_FLOAT_EQ(t.m_vertex2.y, 5.0f);
60+
EXPECT_FLOAT_EQ(t.m_vertex2.z, 6.0f);
61+
62+
EXPECT_FLOAT_EQ(t.m_vertex3.x, 7.0f);
63+
EXPECT_FLOAT_EQ(t.m_vertex3.y, 8.0f);
64+
EXPECT_FLOAT_EQ(t.m_vertex3.z, 9.0f);
65+
}
66+
67+
// Test CalculateNormal
68+
TEST_F(UnitTestTriangle, CalculateNormal)
69+
{
70+
// For t1, the normal should point in the +Z direction (0, 0, 1) or (0, 0, -1)
71+
const Vector3 normal_t1 = t1.CalculateNormal();
72+
// Check if it's normalized and pointed along Z (sign can differ, so use absolute check)
73+
EXPECT_NEAR(std::fabs(normal_t1.z), 1.0f, 1e-5f);
74+
EXPECT_NEAR(normal_t1.Length(), 1.0f, 1e-5f);
75+
76+
77+
// For t3, we expect the normal to be along +Z as well
78+
const Vector3 normal_t3 = t3.CalculateNormal();
79+
EXPECT_NEAR(std::fabs(normal_t3.z), 1.0f, 1e-5f);
80+
}
81+
82+
// Test side lengths
83+
TEST_F(UnitTestTriangle, SideLengths)
84+
{
85+
// For t1 side lengths
86+
EXPECT_FLOAT_EQ(t1.SideALength(), std::sqrt(1.0f)); // distance between (0,0,0) and (1,0,0)
87+
EXPECT_FLOAT_EQ(t1.SideBLength(), std::sqrt(1.0f + 1.0f)); // distance between (4,5,6) & (7,8,9)... but we are testing t1, so let's be accurate:
88+
// Actually, for t1: vertex2=(1,0,0), vertex3=(0,1,0)
89+
// Dist between (0,1,0) and (1,0,0) = sqrt((1-0)^2 + (0-1)^2) = sqrt(1 + 1) = sqrt(2)
90+
EXPECT_FLOAT_EQ(t1.SideBLength(), std::sqrt(2.0f));
91+
92+
// For t3, side a = distance between vertex1=(0,0,0) and vertex2=(2,0,0), which is 2
93+
// side b = distance between vertex3=(0,2,0) and vertex2=(2,0,0), which is sqrt(2^2 + (-2)^2)= sqrt(8)= 2.828...
94+
// We'll just check side a first:
95+
EXPECT_FLOAT_EQ(t3.SideALength(), 2.0f);
96+
// Then side b:
97+
EXPECT_FLOAT_EQ(t3.SideBLength(), std::sqrt(8.0f));
98+
}
99+
100+
// Test side vectors
101+
TEST_F(UnitTestTriangle, SideVectors)
102+
{
103+
const Vector3 sideA_t1 = t1.SideAVector(); // m_vertex1 - m_vertex2
104+
EXPECT_FLOAT_EQ(sideA_t1.x, 0.0f - 1.0f);
105+
EXPECT_FLOAT_EQ(sideA_t1.y, 0.0f - 0.0f);
106+
EXPECT_FLOAT_EQ(sideA_t1.z, 0.0f - 0.0f);
107+
108+
const Vector3 sideB_t1 = t1.SideBVector(); // m_vertex3 - m_vertex2
109+
EXPECT_FLOAT_EQ(sideB_t1.x, 0.0f - 1.0f);
110+
EXPECT_FLOAT_EQ(sideB_t1.y, 1.0f - 0.0f);
111+
EXPECT_FLOAT_EQ(sideB_t1.z, 0.0f - 0.0f);
112+
}
113+
114+
TEST_F(UnitTestTriangle, IsRectangular)
115+
{
116+
EXPECT_TRUE(Triangle<Vector3>({2,0,0}, {}, {0,2,0}).IsRectangular());
117+
}
118+
// Test midpoint
119+
TEST_F(UnitTestTriangle, MidPoint)
120+
{
121+
// For t1, midpoint of (0,0,0), (1,0,0), (0,1,0)
122+
const Vector3 mid1 = t1.MidPoint();
123+
EXPECT_FLOAT_EQ(mid1.x, (0.0f + 1.0f + 0.0f) / 3.0f);
124+
EXPECT_FLOAT_EQ(mid1.y, (0.0f + 0.0f + 1.0f) / 3.0f);
125+
EXPECT_FLOAT_EQ(mid1.z, 0.0f);
126+
127+
// For t2, midpoint of (1,2,3), (4,5,6), (7,8,9)
128+
const Vector3 mid2 = t2.MidPoint();
129+
EXPECT_FLOAT_EQ(mid2.x, (1.0f + 4.0f + 7.0f) / 3.0f);
130+
EXPECT_FLOAT_EQ(mid2.y, (2.0f + 5.0f + 8.0f) / 3.0f);
131+
EXPECT_FLOAT_EQ(mid2.z, (3.0f + 6.0f + 9.0f) / 3.0f);
132+
}

0 commit comments

Comments
 (0)