Files
omath/tests/UnitTestLineTrace.cpp
2024-11-14 06:37:41 +03:00

68 lines
2.3 KiB
C++

#include "gtest/gtest.h"
#include "omath/collision/LineTracer.hpp"
#include "omath/Triangle3d.hpp"
#include "omath/Vector3.hpp"
using namespace omath;
using namespace omath::collision;
class LineTracerTest : public ::testing::Test
{
protected:
// Set up common variables for use in each test
Vector3 vertex1{0.0f, 0.0f, 0.0f};
Vector3 vertex2{1.0f, 0.0f, 0.0f};
Vector3 vertex3{0.0f, 1.0f, 0.0f};
Triangle3d triangle{vertex1, vertex2, vertex3};
};
// Test that a ray intersecting the triangle returns false for CanTraceLine
TEST_F(LineTracerTest, RayIntersectsTriangle)
{
constexpr Ray ray{{0.3f, 0.3f, -1.0f}, {0.3f, 0.3f, 1.0f}};
EXPECT_FALSE(LineTracer::CanTraceLine(ray, triangle));
}
// Test that a ray parallel to the triangle plane returns true for CanTraceLine
TEST_F(LineTracerTest, RayParallelToTriangle)
{
constexpr Ray ray{{0.3f, 0.3f, 1.0f}, {0.3f, 0.3f, 2.0f}};
EXPECT_TRUE(LineTracer::CanTraceLine(ray, triangle));
}
// Test that a ray starting inside the triangle but pointing away returns true
TEST_F(LineTracerTest, RayStartsInTriangleButDoesNotIntersect)
{
constexpr Ray ray{{0.3f, 0.3f, 0.0f}, {0.3f, 0.3f, -1.0f}};
EXPECT_TRUE(LineTracer::CanTraceLine(ray, triangle));
}
// Test that a ray not intersecting the triangle plane returns true
TEST_F(LineTracerTest, RayMissesTriangle)
{
constexpr Ray ray{{2.0f, 2.0f, -1.0f}, {2.0f, 2.0f, 1.0f}};
EXPECT_TRUE(LineTracer::CanTraceLine(ray, triangle));
}
// Test that a ray lying exactly in the plane of the triangle without intersecting returns true
TEST_F(LineTracerTest, RayInPlaneNotIntersecting)
{
constexpr Ray ray{{-1.0f, -1.0f, 0.0f}, {1.5f, 1.5f, 0.0f}};
EXPECT_TRUE(LineTracer::CanTraceLine(ray, triangle));
}
// Test edge case where the ray exactly intersects one of the triangle's vertices, expecting false
TEST_F(LineTracerTest, RayIntersectsVertex)
{
const Ray ray{{-1.0f, -1.0f, -1.0f}, vertex1}; // Intersecting at vertex1
EXPECT_FALSE(LineTracer::CanTraceLine(ray, triangle));
}
// Test edge case where the ray exactly intersects one of the triangle's edges, expecting false
TEST_F(LineTracerTest, RayIntersectsEdge)
{
constexpr Ray ray{{-1.0f, 0.0f, -1.0f}, {0.5f, 0.0f, 0.0f}};
// Intersecting on the edge between vertex1 and vertex2
EXPECT_FALSE(LineTracer::CanTraceLine(ray, triangle));
}