improved tests

This commit is contained in:
2025-04-18 16:36:22 +03:00
parent f8202b116d
commit 9c934c5d9c
2 changed files with 94 additions and 73 deletions

View File

@@ -59,7 +59,7 @@ namespace omath::collision
if (tHit <= kEpsilon) if (tHit <= kEpsilon)
return ray.end; return ray.end;
} }
else if (tHit <= kEpsilon || tHit >= 1.0f - kEpsilon) else if (tHit <= kEpsilon || tHit > 1.0f - kEpsilon)
return ray.end; return ray.end;
return ray.start + rayDir * tHit; return ray.start + rayDir * tHit;

View File

@@ -13,51 +13,56 @@ using namespace omath::collision;
using Vec3 = Vector3<float>; using Vec3 = Vector3<float>;
namespace { namespace
// -----------------------------------------------------------------------------
// Constants & helpers
// -----------------------------------------------------------------------------
constexpr float kTol = 1e-5f;
bool VecEqual(const Vec3& a, const Vec3& b, float tol = kTol)
{ {
// -----------------------------------------------------------------------------
// Constants & helpers
// -----------------------------------------------------------------------------
constexpr float kTol = 1e-5f;
bool VecEqual(const Vec3& a, const Vec3& b, float tol = kTol)
{
return std::fabs(a.x - b.x) < tol && return std::fabs(a.x - b.x) < tol &&
std::fabs(a.y - b.y) < tol && std::fabs(a.y - b.y) < tol &&
std::fabs(a.z - b.z) < tol; std::fabs(a.z - b.z) < tol;
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Fixture with one canonical rightangled triangle in the XY plane. // Fixture with one canonical rightangled triangle in the XY plane.
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
class LineTracerFixture : public ::testing::Test class LineTracerFixture : public ::testing::Test
{ {
protected: protected:
LineTracerFixture() LineTracerFixture() :
: triangle({0.f, 0.f, 0.f}, {1.f, 0.f, 0.f}, {0.f, 1.f, 0.f}) {} triangle({0.f, 0.f, 0.f}, {1.f, 0.f, 0.f}, {0.f, 1.f, 0.f})
{
}
Triangle<Vec3> triangle; Triangle<Vec3> triangle;
}; };
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Datadriven tests for CanTraceLine // Datadriven tests for CanTraceLine
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
struct TraceCase struct TraceCase
{ {
Ray ray; Ray ray;
bool expected_clear; // true => segment does NOT hit the triangle bool expected_clear; // true => segment does NOT hit the triangle
}; };
class CanTraceLineParam : public LineTracerFixture, class CanTraceLineParam : public LineTracerFixture,
public ::testing::WithParamInterface<TraceCase> {}; public ::testing::WithParamInterface<TraceCase>
{
};
TEST_P(CanTraceLineParam, VariousRays) TEST_P(CanTraceLineParam, VariousRays)
{ {
const auto& p = GetParam(); const auto& p = GetParam();
EXPECT_EQ(LineTracer::CanTraceLine(p.ray, triangle), p.expected_clear); EXPECT_EQ(LineTracer::CanTraceLine(p.ray, triangle), p.expected_clear);
} }
INSTANTIATE_TEST_SUITE_P( INSTANTIATE_TEST_SUITE_P(
BasicScenarios, BasicScenarios,
CanTraceLineParam, CanTraceLineParam,
::testing::Values( ::testing::Values(
@@ -69,32 +74,48 @@ INSTANTIATE_TEST_SUITE_P(
TraceCase{Ray{{-1.0f,-1.0f, -1.f},{ 0.0f, 0.0f, 0.f}}, true}, // endpoint on vertex TraceCase{Ray{{-1.0f,-1.0f, -1.f},{ 0.0f, 0.0f, 0.f}}, true}, // endpoint on vertex
TraceCase{Ray{{-1.0f, 0.0f, -1.f},{ 0.5f, 0.0f, 0.f}}, true} // endpoint on edge TraceCase{Ray{{-1.0f, 0.0f, -1.f},{ 0.5f, 0.0f, 0.f}}, true} // endpoint on edge
) )
); );
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Validate that the reported hit point is correct for a genuine intersection. // Validate that the reported hit point is correct for a genuine intersection.
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
TEST_F(LineTracerFixture, HitPointCorrect) TEST_F(LineTracerFixture, HitPointCorrect)
{ {
Ray ray{{0.3f, 0.3f, -1.f}, {0.3f, 0.3f, 1.f}}; constexpr Ray ray{{0.3f, 0.3f, -1.f}, {0.3f, 0.3f, 1.f}};
Vec3 expected{0.3f, 0.3f, 0.f}; constexpr Vec3 expected{0.3f, 0.3f, 0.f};
Vec3 hit = LineTracer::GetRayHitPoint(ray, triangle); const Vec3 hit = LineTracer::GetRayHitPoint(ray, triangle);
ASSERT_FALSE(VecEqual(hit, ray.end)); ASSERT_FALSE(VecEqual(hit, ray.end));
EXPECT_TRUE(VecEqual(hit, expected)); EXPECT_TRUE(VecEqual(hit, expected));
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Triangle far beyond the ray should not block. // Triangle far beyond the ray should not block.
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
TEST_F(LineTracerFixture, DistantTriangleClear) TEST_F(LineTracerFixture, DistantTriangleClear)
{ {
Ray short_ray{{0.f, 0.f, 0.f}, {0.f, 0.f, 1.f}}; constexpr Ray short_ray{{0.f, 0.f, 0.f}, {0.f, 0.f, 1.f}};
Triangle<Vec3> distant{{1000.f,1000.f,1000.f}, constexpr Triangle<Vec3> distant{{1000.f, 1000.f, 1000.f},
{1001.f,1000.f,1000.f}, {1001.f, 1000.f, 1000.f},
{1000.f,1001.f,1000.f}}; {1000.f, 1001.f, 1000.f}};
EXPECT_TRUE(LineTracer::CanTraceLine(short_ray, distant)); EXPECT_TRUE(LineTracer::CanTraceLine(short_ray, distant));
} }
TEST(LineTracerTraceRayEdge, CantHit)
{
constexpr omath::Triangle<Vector3<float>> triangle{{2, 0, 0}, {2, 2, 0}, {2, 2, 2}};
constexpr Ray ray{{}, {1.0, 0, 0}, false};
EXPECT_TRUE(omath::collision::LineTracer::CanTraceLine(ray, triangle));
}
TEST(LineTracerTraceRayEdge, CanHit)
{
constexpr omath::Triangle<Vector3<float>> triangle{{2, 0, 0}, {2, 2, 0}, {2, 2, 2}};
constexpr Ray ray{{}, {2.1, 0, 0}, false};
auto endPoint = omath::collision::LineTracer::GetRayHitPoint(ray, triangle);
EXPECT_FALSE(omath::collision::LineTracer::CanTraceLine(ray, triangle));
}
} // namespace } // namespace