mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 07:03:25 +00:00
improved traceline
This commit is contained in:
@@ -2,14 +2,25 @@
|
|||||||
// Created by Orange on 11/13/2024.
|
// Created by Orange on 11/13/2024.
|
||||||
//
|
//
|
||||||
#pragma once
|
#pragma once
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
#include "omath/Vector3.hpp"
|
#include "omath/Vector3.hpp"
|
||||||
#include "omath/Triangle3d.hpp"
|
#include "omath/Triangle3d.hpp"
|
||||||
|
|
||||||
|
|
||||||
namespace omath::collision
|
namespace omath::collision
|
||||||
{
|
{
|
||||||
struct Ray
|
class Ray
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
Vector3 start;
|
Vector3 start;
|
||||||
Vector3 end;
|
Vector3 end;
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
Vector3 DirectionVector() const;
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
Vector3 DirectionVectorNormalized() const;
|
||||||
};
|
};
|
||||||
class LineTracer
|
class LineTracer
|
||||||
{
|
{
|
||||||
@@ -18,5 +29,11 @@ namespace omath::collision
|
|||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
static bool CanTraceLine(const Ray& ray, const Triangle3d& triangle);
|
static bool CanTraceLine(const Ray& ray, const Triangle3d& triangle);
|
||||||
|
|
||||||
|
|
||||||
|
// Realization of Möller–Trumbore intersection algorithm
|
||||||
|
// https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm
|
||||||
|
[[nodiscard]]
|
||||||
|
static std::optional<Vector3> GetRayHitPoint(const Ray& ray, const Triangle3d& triangle);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,33 +8,56 @@ namespace omath::collision
|
|||||||
{
|
{
|
||||||
bool LineTracer::CanTraceLine(const Ray &ray, const Triangle3d &triangle)
|
bool LineTracer::CanTraceLine(const Ray &ray, const Triangle3d &triangle)
|
||||||
{
|
{
|
||||||
|
return GetRayHitPoint(ray, triangle) == ray.end;
|
||||||
|
}
|
||||||
|
Vector3 Ray::DirectionVector() const
|
||||||
|
{
|
||||||
|
return end - start;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector3 Ray::DirectionVectorNormalized() const
|
||||||
|
{
|
||||||
|
return DirectionVector().Normalized();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<Vector3> LineTracer::GetRayHitPoint(const Ray &ray, const Triangle3d &triangle)
|
||||||
|
{
|
||||||
|
constexpr float kEpsilon = std::numeric_limits<float>::epsilon();
|
||||||
|
|
||||||
const auto sideA = triangle.SideAVector();
|
const auto sideA = triangle.SideAVector();
|
||||||
const auto sideB = triangle.SideBVector();
|
const auto sideB = triangle.SideBVector();
|
||||||
|
|
||||||
const auto rayDir = ray.end - ray.start;
|
|
||||||
|
const auto rayDir = ray.DirectionVector();
|
||||||
|
|
||||||
const auto p = rayDir.Cross(sideB);
|
const auto p = rayDir.Cross(sideB);
|
||||||
|
|
||||||
const auto det = sideA.Dot(p);
|
const auto det = sideA.Dot(p);
|
||||||
|
|
||||||
if (std::abs(det) < 1e-6)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
const auto invDet = 1 / det;
|
if (std::abs(det) < kEpsilon)
|
||||||
|
return ray.end;
|
||||||
|
|
||||||
|
const auto invDet = 1.0f / det;
|
||||||
const auto t = ray.start - triangle.m_vertex2;
|
const auto t = ray.start - triangle.m_vertex2;
|
||||||
|
|
||||||
const auto u = t.Dot(p) * invDet;
|
const auto u = t.Dot(p) * invDet;
|
||||||
|
|
||||||
if (u < 0.f || u > 1.f)
|
|
||||||
return true;
|
if ((u < 0 && std::abs(u) > kEpsilon) || (u > 1 && std::abs(u-1) > kEpsilon))
|
||||||
|
return ray.end;
|
||||||
|
|
||||||
const auto q = t.Cross(sideA);
|
const auto q = t.Cross(sideA);
|
||||||
const auto v = rayDir.Dot(q) * invDet;
|
const auto v = rayDir.Dot(q) * invDet;
|
||||||
|
|
||||||
if (v < 0.f || u + v > 1.f)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
return sideB.Dot(q) * invDet <= 0.f;
|
if ((v < 0 && std::abs(v) > kEpsilon) || (u + v > 1 && std::abs(u + v - 1) > kEpsilon))
|
||||||
|
return ray.end;
|
||||||
|
|
||||||
|
const auto tHit = sideB.Dot(q) * invDet;
|
||||||
|
|
||||||
|
|
||||||
|
if (tHit <= kEpsilon)
|
||||||
|
return ray.end;
|
||||||
|
|
||||||
|
return ray.start + rayDir * tHit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,17 +51,30 @@ TEST_F(LineTracerTest, RayInPlaneNotIntersecting)
|
|||||||
EXPECT_TRUE(LineTracer::CanTraceLine(ray, triangle));
|
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)
|
TEST_F(LineTracerTest, RayIntersectsVertex)
|
||||||
{
|
{
|
||||||
const Ray ray{{-1.0f, -1.0f, -1.0f}, vertex1}; // Intersecting at vertex1
|
const Ray ray{{-1.0f, -1.0f, -1.0f}, vertex1}; // Intersecting at vertex1
|
||||||
EXPECT_FALSE(LineTracer::CanTraceLine(ray, triangle));
|
EXPECT_TRUE(LineTracer::CanTraceLine(ray, triangle));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test edge case where the ray exactly intersects one of the triangle's edges, expecting false
|
|
||||||
TEST_F(LineTracerTest, RayIntersectsEdge)
|
TEST_F(LineTracerTest, RayIntersectsEdge)
|
||||||
{
|
{
|
||||||
constexpr Ray ray{{-1.0f, 0.0f, -1.0f}, {0.5f, 0.0f, 0.0f}};
|
constexpr Ray ray{{-1.0f, 0.0f, -1.0f}, {0.5f, 0.0f, 0.0f}};
|
||||||
// Intersecting on the edge between vertex1 and vertex2
|
// Intersecting on the edge between vertex1 and vertex2
|
||||||
EXPECT_FALSE(LineTracer::CanTraceLine(ray, triangle));
|
EXPECT_TRUE(LineTracer::CanTraceLine(ray, triangle));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(LineTracerTest, TriangleFarBeyondRayEndPoint)
|
||||||
|
{
|
||||||
|
// Define a ray with a short length
|
||||||
|
constexpr Ray ray{{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f}};
|
||||||
|
|
||||||
|
// Define a triangle far beyond the ray's endpoint
|
||||||
|
const Triangle3d distantTriangle{
|
||||||
|
{1000.0f, 1000.0f, 1000.0f}, {1001.0f, 1000.0f, 1000.0f}, {1000.0f, 1001.0f, 1000.0f}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Expect true because the ray ends long before it could reach the distant triangle
|
||||||
|
EXPECT_TRUE(LineTracer::CanTraceLine(ray, distantTriangle));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user