From 492ddfd566addac5f10fc36e079b23f36a7fadaf Mon Sep 17 00:00:00 2001 From: Orange Date: Fri, 18 Apr 2025 00:43:46 +0300 Subject: [PATCH 01/10] added box --- CMakeLists.txt | 3 +-- include/omath/3d_primitives/box.hpp | 14 ++++++++++ source/3d_primitives/CMakeLists.txt | 1 + source/3d_primitives/box.cpp | 32 +++++++++++++++++++++++ source/CMakeLists.txt | 3 ++- tests/CMakeLists.txt | 1 + tests/general/unit_test_box_primitive.cpp | 4 +++ 7 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 include/omath/3d_primitives/box.hpp create mode 100644 source/3d_primitives/CMakeLists.txt create mode 100644 source/3d_primitives/box.cpp create mode 100644 tests/general/unit_test_box_primitive.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ade1bb1..d87052c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,8 +18,7 @@ option(OMATH_SUPRESS_SAFETY_CHECKS "Supress some safety checks in release build if (OMATH_BUILD_AS_SHARED_LIBRARY) add_library(omath SHARED source/matrix.cpp) else() - add_library(omath STATIC source/matrix.cpp - source/matrix.cpp) + add_library(omath STATIC source/matrix.cpp) endif() message(STATUS "Building on ${CMAKE_HOST_SYSTEM_NAME}") diff --git a/include/omath/3d_primitives/box.hpp b/include/omath/3d_primitives/box.hpp new file mode 100644 index 0000000..a59b235 --- /dev/null +++ b/include/omath/3d_primitives/box.hpp @@ -0,0 +1,14 @@ +// +// Created by Vlad on 4/18/2025. +// + +#pragma once +#include + + +namespace omath::primitives +{ + [[nodiscard]] + std::array, 8> CreateBox(const Vector3& top, const Vector3& bottom, + const Vector3& dirForward, const Vector3& dirRight); +} diff --git a/source/3d_primitives/CMakeLists.txt b/source/3d_primitives/CMakeLists.txt new file mode 100644 index 0000000..ef4e0a4 --- /dev/null +++ b/source/3d_primitives/CMakeLists.txt @@ -0,0 +1 @@ +target_sources(omath PRIVATE box.cpp) \ No newline at end of file diff --git a/source/3d_primitives/box.cpp b/source/3d_primitives/box.cpp new file mode 100644 index 0000000..652cef6 --- /dev/null +++ b/source/3d_primitives/box.cpp @@ -0,0 +1,32 @@ +// +// Created by Vlad on 4/18/2025. +// +#include "omath/3d_primitives/box.hpp" + +namespace omath::primitives +{ + + std::array, 8> CreateBox(const Vector3& top, const Vector3& bottom, + const Vector3& dirForward, + const Vector3& dirRight) + { + const auto height = top.DistTo(bottom); + const auto sideSize = height / 6.f; + + std::array, 8> points; + + points[0] = bottom + (dirForward + dirRight) * sideSize; + points[1] = bottom + (dirForward - dirRight) * sideSize; + + points[2] = bottom + (-dirForward + dirRight) * sideSize; + points[3] = bottom + (-dirForward - dirRight) * sideSize; + + points[4] = top + (dirForward + dirRight) * sideSize; + points[5] = top + (dirForward - dirRight) * sideSize; + + points[6] = top + (-dirForward + dirRight) * sideSize; + points[7] = top + (-dirForward - dirRight) * sideSize; + + return points; + } +} diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index f690fe3..1053134 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -7,4 +7,5 @@ add_subdirectory(projectile_prediction) add_subdirectory(pathfinding) add_subdirectory(projection) add_subdirectory(collision) -add_subdirectory(engines) \ No newline at end of file +add_subdirectory(engines) +add_subdirectory(3d_primitives) \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 244f7bb..5f1c8e0 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -18,6 +18,7 @@ add_executable(unit-tests general/unit_test_view_angles.cpp general/unit_test_angle.cpp general/unit_test_triangle.cpp + general/unit_test_box_primitive.cpp engines/unit_test_open_gl.cpp engines/unit_test_unity_engine.cpp diff --git a/tests/general/unit_test_box_primitive.cpp b/tests/general/unit_test_box_primitive.cpp new file mode 100644 index 0000000..f38e6e1 --- /dev/null +++ b/tests/general/unit_test_box_primitive.cpp @@ -0,0 +1,4 @@ +// +// Created by Vlad on 4/18/2025. +// +#include \ No newline at end of file From 0ce30a70383e63e111133417ef1dbbc98f5d1323 Mon Sep 17 00:00:00 2001 From: Orange Date: Fri, 18 Apr 2025 00:51:07 +0300 Subject: [PATCH 02/10] added new build option --- CMakeLists.txt | 12 ++++++++---- include/omath/3d_primitives/box.hpp | 2 +- tests/general/unit_test_box_primitive.cpp | 4 +++- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d87052c..a51519c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ project(omath VERSION 1.0.1 LANGUAGES CXX) include(CMakePackageConfigHelpers) -option(OMATH_BUILD_TESTS "Build unit tests" OFF) +option(OMATH_BUILD_TESTS "Build unit tests" ON) option(OMATH_THREAT_WARNING_AS_ERROR "Set highest level of warnings and force compiler to treat them as errors" ON) option(OMATH_BUILD_AS_SHARED_LIBRARY "Build Omath as .so or .dll" OFF) option(OMATH_USE_AVX2 "Omath will use AVX2 to boost performance" ON) @@ -13,7 +13,7 @@ option(OMATH_IMGUI_INTEGRATION "Omath will define method to convert omath types option(OMATH_BUILD_EXAMPLES "Build example projects with you can learn & play" OFF) option(OMATH_STATIC_MSVC_RUNTIME_LIBRARY "Force Omath to link static runtime" OFF) option(OMATH_SUPRESS_SAFETY_CHECKS "Supress some safety checks in release build to improve general performance" ON) - +option(OMATH_USE_UNITY_BUILD "Will enable unity build to speed up compilation" OFF) if (OMATH_BUILD_AS_SHARED_LIBRARY) add_library(omath SHARED source/matrix.cpp) @@ -54,11 +54,15 @@ endif() set_target_properties(omath PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}" LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}" - UNITY_BUILD ON - UNITY_BUILD_BATCH_SIZE 20 CXX_STANDARD 23 CXX_STANDARD_REQUIRED ON) +if (OMATH_USE_UNITY_BUILD) + set_target_properties(omath PROPERTIES + UNITY_BUILD ON + UNITY_BUILD_BATCH_SIZE 20) +endif() + if (OMATH_STATIC_MSVC_RUNTIME_LIBRARY) set_target_properties(omath PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>" diff --git a/include/omath/3d_primitives/box.hpp b/include/omath/3d_primitives/box.hpp index a59b235..afc7003 100644 --- a/include/omath/3d_primitives/box.hpp +++ b/include/omath/3d_primitives/box.hpp @@ -4,7 +4,7 @@ #pragma once #include - +#include "omath/vector3.hpp" namespace omath::primitives { diff --git a/tests/general/unit_test_box_primitive.cpp b/tests/general/unit_test_box_primitive.cpp index f38e6e1..215f222 100644 --- a/tests/general/unit_test_box_primitive.cpp +++ b/tests/general/unit_test_box_primitive.cpp @@ -1,4 +1,6 @@ // // Created by Vlad on 4/18/2025. // -#include \ No newline at end of file +#include +#include + From 97c2da893b9498f824189d91f0519ea2aa61ad69 Mon Sep 17 00:00:00 2001 From: Orange Date: Fri, 18 Apr 2025 01:33:47 +0300 Subject: [PATCH 03/10] added ratio param --- include/omath/3d_primitives/box.hpp | 2 +- source/3d_primitives/box.cpp | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/include/omath/3d_primitives/box.hpp b/include/omath/3d_primitives/box.hpp index afc7003..bbe43b2 100644 --- a/include/omath/3d_primitives/box.hpp +++ b/include/omath/3d_primitives/box.hpp @@ -10,5 +10,5 @@ namespace omath::primitives { [[nodiscard]] std::array, 8> CreateBox(const Vector3& top, const Vector3& bottom, - const Vector3& dirForward, const Vector3& dirRight); + const Vector3& dirForward, const Vector3& dirRight, float ratio = 4.f); } diff --git a/source/3d_primitives/box.cpp b/source/3d_primitives/box.cpp index 652cef6..4403b23 100644 --- a/source/3d_primitives/box.cpp +++ b/source/3d_primitives/box.cpp @@ -8,10 +8,11 @@ namespace omath::primitives std::array, 8> CreateBox(const Vector3& top, const Vector3& bottom, const Vector3& dirForward, - const Vector3& dirRight) + const Vector3& dirRight, + const float ratio) { const auto height = top.DistTo(bottom); - const auto sideSize = height / 6.f; + const auto sideSize = height / ratio; std::array, 8> points; From 254674a62ee8f514ac3ae33f3c72a0972d6cb250 Mon Sep 17 00:00:00 2001 From: Orange Date: Fri, 18 Apr 2025 12:11:43 +0300 Subject: [PATCH 04/10] fixed code style --- include/omath/3d_primitives/box.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/omath/3d_primitives/box.hpp b/include/omath/3d_primitives/box.hpp index bbe43b2..3b786e3 100644 --- a/include/omath/3d_primitives/box.hpp +++ b/include/omath/3d_primitives/box.hpp @@ -10,5 +10,6 @@ namespace omath::primitives { [[nodiscard]] std::array, 8> CreateBox(const Vector3& top, const Vector3& bottom, - const Vector3& dirForward, const Vector3& dirRight, float ratio = 4.f); + const Vector3& dirForward, const Vector3& dirRight, + float ratio = 4.f); } From a34076634880d5b2a35b6179dc40c08f15929519 Mon Sep 17 00:00:00 2001 From: Orange Date: Fri, 18 Apr 2025 12:34:24 +0300 Subject: [PATCH 05/10] switched to polygons --- include/omath/3d_primitives/box.hpp | 8 +++++--- source/3d_primitives/box.cpp | 10 ++++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/include/omath/3d_primitives/box.hpp b/include/omath/3d_primitives/box.hpp index 3b786e3..d915ca2 100644 --- a/include/omath/3d_primitives/box.hpp +++ b/include/omath/3d_primitives/box.hpp @@ -4,12 +4,14 @@ #pragma once #include +#include "omath/triangle.hpp" #include "omath/vector3.hpp" + namespace omath::primitives { [[nodiscard]] - std::array, 8> CreateBox(const Vector3& top, const Vector3& bottom, - const Vector3& dirForward, const Vector3& dirRight, - float ratio = 4.f); + std::array>, 8> CreateBox(const Vector3& top, const Vector3& bottom, + const Vector3& dirForward, const Vector3& dirRight, + float ratio = 4.f); } diff --git a/source/3d_primitives/box.cpp b/source/3d_primitives/box.cpp index 4403b23..202e47b 100644 --- a/source/3d_primitives/box.cpp +++ b/source/3d_primitives/box.cpp @@ -3,10 +3,12 @@ // #include "omath/3d_primitives/box.hpp" + + namespace omath::primitives { - std::array, 8> CreateBox(const Vector3& top, const Vector3& bottom, + std::array>, 8> CreateBox(const Vector3& top, const Vector3& bottom, const Vector3& dirForward, const Vector3& dirRight, const float ratio) @@ -28,6 +30,10 @@ namespace omath::primitives points[6] = top + (-dirForward + dirRight) * sideSize; points[7] = top + (-dirForward - dirRight) * sideSize; - return points; + + std::array>, 8> polygons; + + polygons[0] = {points[0], points[2], points[3]}; + return polygons; } } From 8bf0bb8e0d7e2d9bb22f78605333c57eb7b71669 Mon Sep 17 00:00:00 2001 From: Orange Date: Fri, 18 Apr 2025 13:56:08 +0300 Subject: [PATCH 06/10] improved line trace and box primitive --- include/omath/3d_primitives/box.hpp | 2 +- include/omath/collision/line_tracer.hpp | 2 +- source/3d_primitives/box.cpp | 53 ++++++++++++++++--------- source/collision/line_tracer.cpp | 7 +++- 4 files changed, 43 insertions(+), 21 deletions(-) diff --git a/include/omath/3d_primitives/box.hpp b/include/omath/3d_primitives/box.hpp index d915ca2..0f18c40 100644 --- a/include/omath/3d_primitives/box.hpp +++ b/include/omath/3d_primitives/box.hpp @@ -11,7 +11,7 @@ namespace omath::primitives { [[nodiscard]] - std::array>, 8> CreateBox(const Vector3& top, const Vector3& bottom, + std::array>, 12> CreateBox(const Vector3& top, const Vector3& bottom, const Vector3& dirForward, const Vector3& dirRight, float ratio = 4.f); } diff --git a/include/omath/collision/line_tracer.hpp b/include/omath/collision/line_tracer.hpp index 8834136..cd7a87a 100644 --- a/include/omath/collision/line_tracer.hpp +++ b/include/omath/collision/line_tracer.hpp @@ -13,7 +13,7 @@ namespace omath::collision public: Vector3 start; Vector3 end; - + bool infinite_length = false; [[nodiscard]] Vector3 DirectionVector() const; diff --git a/source/3d_primitives/box.cpp b/source/3d_primitives/box.cpp index 202e47b..0e45eaa 100644 --- a/source/3d_primitives/box.cpp +++ b/source/3d_primitives/box.cpp @@ -4,36 +4,53 @@ #include "omath/3d_primitives/box.hpp" - namespace omath::primitives { - - std::array>, 8> CreateBox(const Vector3& top, const Vector3& bottom, - const Vector3& dirForward, - const Vector3& dirRight, - const float ratio) + std::array>, 12> CreateBox(const Vector3& top, const Vector3& bottom, + const Vector3& dirForward, + const Vector3& dirRight, + const float ratio) { const auto height = top.DistTo(bottom); const auto sideSize = height / ratio; - std::array, 8> points; + // corner layout (0‑3 bottom, 4‑7 top) + std::array, 8> p; + p[0] = bottom + (dirForward + dirRight) * sideSize; // front‑right‑bottom + p[1] = bottom + (dirForward - dirRight) * sideSize; // front‑left‑bottom + p[2] = bottom + (-dirForward + dirRight) * sideSize; // back‑right‑bottom + p[3] = bottom + (-dirForward - dirRight) * sideSize; // back‑left‑bottom + p[4] = top + (dirForward + dirRight) * sideSize; // front‑right‑top + p[5] = top + (dirForward - dirRight) * sideSize; // front‑left‑top + p[6] = top + (-dirForward + dirRight) * sideSize; // back‑right‑top + p[7] = top + (-dirForward - dirRight) * sideSize; // back‑left‑top - points[0] = bottom + (dirForward + dirRight) * sideSize; - points[1] = bottom + (dirForward - dirRight) * sideSize; + std::array>, 12> poly; - points[2] = bottom + (-dirForward + dirRight) * sideSize; - points[3] = bottom + (-dirForward - dirRight) * sideSize; + // bottom face (+Y up ⇒ wind CW when viewed from above) + poly[0] = {p[0], p[2], p[3]}; + poly[1] = {p[0], p[3], p[1]}; - points[4] = top + (dirForward + dirRight) * sideSize; - points[5] = top + (dirForward - dirRight) * sideSize; + // top face + poly[2] = {p[4], p[7], p[6]}; + poly[3] = {p[4], p[5], p[7]}; - points[6] = top + (-dirForward + dirRight) * sideSize; - points[7] = top + (-dirForward - dirRight) * sideSize; + // front face + poly[4] = {p[0], p[5], p[1]}; + poly[5] = {p[0], p[4], p[5]}; + // right face + poly[6] = {p[0], p[6], p[2]}; + poly[7] = {p[0], p[4], p[6]}; - std::array>, 8> polygons; + // back face + poly[8] = {p[2], p[7], p[3]}; + poly[9] = {p[2], p[6], p[7]}; - polygons[0] = {points[0], points[2], points[3]}; - return polygons; + // left face + poly[10] = {p[1], p[7], p[5]}; + poly[11] = {p[1], p[3], p[7]}; + + return poly; } } diff --git a/source/collision/line_tracer.cpp b/source/collision/line_tracer.cpp index 0dfc7f5..2d55956 100644 --- a/source/collision/line_tracer.cpp +++ b/source/collision/line_tracer.cpp @@ -54,7 +54,12 @@ namespace omath::collision const auto tHit = sideB.Dot(q) * invDet; - if (tHit <= kEpsilon) + if (ray.infinite_length) + { + if (tHit <= kEpsilon) + return ray.end; + } + else if (tHit < 0.0f || tHit > 1.0f) return ray.end; return ray.start + rayDir * tHit; From f8202b116d8554c819d8f874183d1e0e17db4ce2 Mon Sep 17 00:00:00 2001 From: Vladislav Alpatov Date: Fri, 18 Apr 2025 16:00:23 +0300 Subject: [PATCH 07/10] improved line tracer --- source/collision/line_tracer.cpp | 2 +- tests/general/unit_test_line_trace.cpp | 138 ++++++++++++++----------- 2 files changed, 80 insertions(+), 60 deletions(-) diff --git a/source/collision/line_tracer.cpp b/source/collision/line_tracer.cpp index 2d55956..74aa308 100644 --- a/source/collision/line_tracer.cpp +++ b/source/collision/line_tracer.cpp @@ -59,7 +59,7 @@ namespace omath::collision if (tHit <= kEpsilon) return ray.end; } - else if (tHit < 0.0f || tHit > 1.0f) + else if (tHit <= kEpsilon || tHit >= 1.0f - kEpsilon) return ray.end; return ray.start + rayDir * tHit; diff --git a/tests/general/unit_test_line_trace.cpp b/tests/general/unit_test_line_trace.cpp index c5fc23b..600a18b 100644 --- a/tests/general/unit_test_line_trace.cpp +++ b/tests/general/unit_test_line_trace.cpp @@ -1,80 +1,100 @@ +// +// Revised unit‑test suite for LineTracer (segment‑based Möller–Trumbore) +// Pure ASCII: avoids non‑standard characters that MSVC rejects. +// #include "gtest/gtest.h" #include "omath/collision/line_tracer.hpp" #include "omath/triangle.hpp" #include "omath/vector3.hpp" +#include using namespace omath; using namespace omath::collision; -class LineTracerTest : public ::testing::Test +using Vec3 = Vector3; + +namespace { + +// ----------------------------------------------------------------------------- +// 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 && + std::fabs(a.y - b.y) < tol && + std::fabs(a.z - b.z) < tol; +} + +// ----------------------------------------------------------------------------- +// Fixture with one canonical right‑angled triangle in the XY plane. +// ----------------------------------------------------------------------------- +class LineTracerFixture : 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}; - Triangle> triangle{vertex1, vertex2, vertex3}; + LineTracerFixture() + : triangle({0.f, 0.f, 0.f}, {1.f, 0.f, 0.f}, {0.f, 1.f, 0.f}) {} + + Triangle triangle; }; -// Test that a ray intersecting the triangle returns false for CanTraceLine -TEST_F(LineTracerTest, RayIntersectsTriangle) +// ----------------------------------------------------------------------------- +// Data‑driven tests for CanTraceLine +// ----------------------------------------------------------------------------- +struct TraceCase { - constexpr Ray ray{{0.3f, 0.3f, -1.0f}, {0.3f, 0.3f, 1.0f}}; - EXPECT_FALSE(LineTracer::CanTraceLine(ray, triangle)); + Ray ray; + bool expected_clear; // true => segment does NOT hit the triangle +}; + +class CanTraceLineParam : public LineTracerFixture, + public ::testing::WithParamInterface {}; + +TEST_P(CanTraceLineParam, VariousRays) +{ + const auto& p = GetParam(); + EXPECT_EQ(LineTracer::CanTraceLine(p.ray, triangle), p.expected_clear); } -// Test that a ray parallel to the triangle plane returns true for CanTraceLine -TEST_F(LineTracerTest, RayParallelToTriangle) +INSTANTIATE_TEST_SUITE_P( + BasicScenarios, + CanTraceLineParam, + ::testing::Values( + TraceCase{Ray{{ 0.3f, 0.3f, -1.f},{ 0.3f, 0.3f, 1.f}}, false}, // hit through centre + TraceCase{Ray{{ 0.3f, 0.3f, 1.f},{ 0.3f, 0.3f, 2.f}}, true}, // parallel above + TraceCase{Ray{{ 0.3f, 0.3f, 0.f},{ 0.3f, 0.3f,-1.f}}, true}, // starts inside, goes away + TraceCase{Ray{{ 2.0f, 2.0f, -1.f},{ 2.0f, 2.0f, 1.f}}, true}, // misses entirely + TraceCase{Ray{{-1.0f,-1.0f, 0.f},{ 1.5f, 1.5f, 0.f}},true}, // lies in plane, outside tri + 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 + ) +); + +// ----------------------------------------------------------------------------- +// Validate that the reported hit point is correct for a genuine intersection. +// ----------------------------------------------------------------------------- +TEST_F(LineTracerFixture, HitPointCorrect) { - constexpr Ray ray{{0.3f, 0.3f, 1.0f}, {0.3f, 0.3f, 2.0f}}; - EXPECT_TRUE(LineTracer::CanTraceLine(ray, triangle)); + Ray ray{{0.3f, 0.3f, -1.f}, {0.3f, 0.3f, 1.f}}; + Vec3 expected{0.3f, 0.3f, 0.f}; + + Vec3 hit = LineTracer::GetRayHitPoint(ray, triangle); + ASSERT_FALSE(VecEqual(hit, ray.end)); + EXPECT_TRUE(VecEqual(hit, expected)); } -// Test that a ray starting inside the triangle but pointing away returns true -TEST_F(LineTracerTest, RayStartsInTriangleButDoesNotIntersect) +// ----------------------------------------------------------------------------- +// Triangle far beyond the ray should not block. +// ----------------------------------------------------------------------------- +TEST_F(LineTracerFixture, DistantTriangleClear) { - constexpr Ray ray{{0.3f, 0.3f, 0.0f}, {0.3f, 0.3f, -1.0f}}; - EXPECT_TRUE(LineTracer::CanTraceLine(ray, triangle)); + Ray short_ray{{0.f, 0.f, 0.f}, {0.f, 0.f, 1.f}}; + Triangle distant{{1000.f,1000.f,1000.f}, + {1001.f,1000.f,1000.f}, + {1000.f,1001.f,1000.f}}; + + EXPECT_TRUE(LineTracer::CanTraceLine(short_ray, distant)); } -// 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_F(LineTracerTest, RayIntersectsVertex) -{ - const Ray ray{{-1.0f, -1.0f, -1.0f}, vertex1}; // Intersecting at vertex1 - EXPECT_TRUE(LineTracer::CanTraceLine(ray, triangle)); -} - -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_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 - constexpr Triangle> 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)); -} +} // namespace From 9c934c5d9cc2c0b2b8bac6efc708fbf64fc7959e Mon Sep 17 00:00:00 2001 From: Orange Date: Fri, 18 Apr 2025 16:36:22 +0300 Subject: [PATCH 08/10] improved tests --- source/collision/line_tracer.cpp | 2 +- tests/general/unit_test_line_trace.cpp | 165 ++++++++++++++----------- 2 files changed, 94 insertions(+), 73 deletions(-) diff --git a/source/collision/line_tracer.cpp b/source/collision/line_tracer.cpp index 74aa308..5bf7837 100644 --- a/source/collision/line_tracer.cpp +++ b/source/collision/line_tracer.cpp @@ -59,7 +59,7 @@ namespace omath::collision if (tHit <= kEpsilon) return ray.end; } - else if (tHit <= kEpsilon || tHit >= 1.0f - kEpsilon) + else if (tHit <= kEpsilon || tHit > 1.0f - kEpsilon) return ray.end; return ray.start + rayDir * tHit; diff --git a/tests/general/unit_test_line_trace.cpp b/tests/general/unit_test_line_trace.cpp index 600a18b..9472494 100644 --- a/tests/general/unit_test_line_trace.cpp +++ b/tests/general/unit_test_line_trace.cpp @@ -13,88 +13,109 @@ using namespace omath::collision; using Vec3 = Vector3; -namespace { - -// ----------------------------------------------------------------------------- -// Constants & helpers -// ----------------------------------------------------------------------------- -constexpr float kTol = 1e-5f; - -bool VecEqual(const Vec3& a, const Vec3& b, float tol = kTol) +namespace { - return std::fabs(a.x - b.x) < tol && - std::fabs(a.y - b.y) < tol && - std::fabs(a.z - b.z) < tol; -} -// ----------------------------------------------------------------------------- -// Fixture with one canonical right‑angled triangle in the XY plane. -// ----------------------------------------------------------------------------- -class LineTracerFixture : public ::testing::Test -{ -protected: - LineTracerFixture() - : triangle({0.f, 0.f, 0.f}, {1.f, 0.f, 0.f}, {0.f, 1.f, 0.f}) {} + // ----------------------------------------------------------------------------- + // Constants & helpers + // ----------------------------------------------------------------------------- + constexpr float kTol = 1e-5f; - Triangle triangle; -}; + bool VecEqual(const Vec3& a, const Vec3& b, float tol = kTol) + { + return std::fabs(a.x - b.x) < tol && + std::fabs(a.y - b.y) < tol && + std::fabs(a.z - b.z) < tol; + } -// ----------------------------------------------------------------------------- -// Data‑driven tests for CanTraceLine -// ----------------------------------------------------------------------------- -struct TraceCase -{ - Ray ray; - bool expected_clear; // true => segment does NOT hit the triangle -}; + // ----------------------------------------------------------------------------- + // Fixture with one canonical right‑angled triangle in the XY plane. + // ----------------------------------------------------------------------------- + class LineTracerFixture : public ::testing::Test + { + protected: + LineTracerFixture() : + triangle({0.f, 0.f, 0.f}, {1.f, 0.f, 0.f}, {0.f, 1.f, 0.f}) + { + } -class CanTraceLineParam : public LineTracerFixture, - public ::testing::WithParamInterface {}; + Triangle triangle; + }; -TEST_P(CanTraceLineParam, VariousRays) -{ - const auto& p = GetParam(); - EXPECT_EQ(LineTracer::CanTraceLine(p.ray, triangle), p.expected_clear); -} + // ----------------------------------------------------------------------------- + // Data‑driven tests for CanTraceLine + // ----------------------------------------------------------------------------- + struct TraceCase + { + Ray ray; + bool expected_clear; // true => segment does NOT hit the triangle + }; -INSTANTIATE_TEST_SUITE_P( - BasicScenarios, - CanTraceLineParam, - ::testing::Values( - TraceCase{Ray{{ 0.3f, 0.3f, -1.f},{ 0.3f, 0.3f, 1.f}}, false}, // hit through centre - TraceCase{Ray{{ 0.3f, 0.3f, 1.f},{ 0.3f, 0.3f, 2.f}}, true}, // parallel above - TraceCase{Ray{{ 0.3f, 0.3f, 0.f},{ 0.3f, 0.3f,-1.f}}, true}, // starts inside, goes away - TraceCase{Ray{{ 2.0f, 2.0f, -1.f},{ 2.0f, 2.0f, 1.f}}, true}, // misses entirely - TraceCase{Ray{{-1.0f,-1.0f, 0.f},{ 1.5f, 1.5f, 0.f}},true}, // lies in plane, outside tri - 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 - ) -); + class CanTraceLineParam : public LineTracerFixture, + public ::testing::WithParamInterface + { + }; -// ----------------------------------------------------------------------------- -// Validate that the reported hit point is correct for a genuine intersection. -// ----------------------------------------------------------------------------- -TEST_F(LineTracerFixture, HitPointCorrect) -{ - Ray ray{{0.3f, 0.3f, -1.f}, {0.3f, 0.3f, 1.f}}; - Vec3 expected{0.3f, 0.3f, 0.f}; + TEST_P(CanTraceLineParam, VariousRays) + { + const auto& p = GetParam(); + EXPECT_EQ(LineTracer::CanTraceLine(p.ray, triangle), p.expected_clear); + } - Vec3 hit = LineTracer::GetRayHitPoint(ray, triangle); - ASSERT_FALSE(VecEqual(hit, ray.end)); - EXPECT_TRUE(VecEqual(hit, expected)); -} + INSTANTIATE_TEST_SUITE_P( + BasicScenarios, + CanTraceLineParam, + ::testing::Values( + TraceCase{Ray{{ 0.3f, 0.3f, -1.f},{ 0.3f, 0.3f, 1.f}}, false}, // hit through centre + TraceCase{Ray{{ 0.3f, 0.3f, 1.f},{ 0.3f, 0.3f, 2.f}}, true}, // parallel above + TraceCase{Ray{{ 0.3f, 0.3f, 0.f},{ 0.3f, 0.3f,-1.f}}, true}, // starts inside, goes away + TraceCase{Ray{{ 2.0f, 2.0f, -1.f},{ 2.0f, 2.0f, 1.f}}, true}, // misses entirely + TraceCase{Ray{{-1.0f,-1.0f, 0.f},{ 1.5f, 1.5f, 0.f}},true}, // lies in plane, outside tri + 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 + ) + ); -// ----------------------------------------------------------------------------- -// Triangle far beyond the ray should not block. -// ----------------------------------------------------------------------------- -TEST_F(LineTracerFixture, DistantTriangleClear) -{ - Ray short_ray{{0.f, 0.f, 0.f}, {0.f, 0.f, 1.f}}; - Triangle distant{{1000.f,1000.f,1000.f}, - {1001.f,1000.f,1000.f}, - {1000.f,1001.f,1000.f}}; + // ----------------------------------------------------------------------------- + // Validate that the reported hit point is correct for a genuine intersection. + // ----------------------------------------------------------------------------- + TEST_F(LineTracerFixture, HitPointCorrect) + { + constexpr Ray ray{{0.3f, 0.3f, -1.f}, {0.3f, 0.3f, 1.f}}; + constexpr Vec3 expected{0.3f, 0.3f, 0.f}; - EXPECT_TRUE(LineTracer::CanTraceLine(short_ray, distant)); -} + const Vec3 hit = LineTracer::GetRayHitPoint(ray, triangle); + ASSERT_FALSE(VecEqual(hit, ray.end)); + EXPECT_TRUE(VecEqual(hit, expected)); + } + // ----------------------------------------------------------------------------- + // Triangle far beyond the ray should not block. + // ----------------------------------------------------------------------------- + TEST_F(LineTracerFixture, DistantTriangleClear) + { + constexpr Ray short_ray{{0.f, 0.f, 0.f}, {0.f, 0.f, 1.f}}; + constexpr Triangle distant{{1000.f, 1000.f, 1000.f}, + {1001.f, 1000.f, 1000.f}, + {1000.f, 1001.f, 1000.f}}; + + EXPECT_TRUE(LineTracer::CanTraceLine(short_ray, distant)); + } + + TEST(LineTracerTraceRayEdge, CantHit) + { + constexpr omath::Triangle> 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> 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 From c692cf39e1a31b87da12e4e77f210c2b6c3031a5 Mon Sep 17 00:00:00 2001 From: Orange Date: Fri, 18 Apr 2025 16:53:53 +0300 Subject: [PATCH 09/10] updated readme --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 02cfeb8..21dc8ec 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,8 @@ With `omath/projection` module you can achieve simple ESP hack for powered by So ![banner](https://i.imgur.com/lcJrfcZ.png) Or for InfinityWard Engine based games. Like Call of Duty Black Ops 2! ![banner](https://i.imgur.com/F8dmdoo.png) - +Or create simple trigger bot with embeded traceline from omath::collision::LineTrace +![banner](https://i.imgur.com/fxMjRKo.jpeg) Or even advanced projectile aimbot [Watch Video](https://youtu.be/lM_NJ1yCunw?si=5E87OrQMeypxSJ3E) From d14cb1e93eb70913703ad35c5bee53faa23b616b Mon Sep 17 00:00:00 2001 From: Orange Date: Fri, 18 Apr 2025 16:55:07 +0300 Subject: [PATCH 10/10] reset to default --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a51519c..5dfad3e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ project(omath VERSION 1.0.1 LANGUAGES CXX) include(CMakePackageConfigHelpers) -option(OMATH_BUILD_TESTS "Build unit tests" ON) +option(OMATH_BUILD_TESTS "Build unit tests" OFF) option(OMATH_THREAT_WARNING_AS_ERROR "Set highest level of warnings and force compiler to treat them as errors" ON) option(OMATH_BUILD_AS_SHARED_LIBRARY "Build Omath as .so or .dll" OFF) option(OMATH_USE_AVX2 "Omath will use AVX2 to boost performance" ON) @@ -13,7 +13,7 @@ option(OMATH_IMGUI_INTEGRATION "Omath will define method to convert omath types option(OMATH_BUILD_EXAMPLES "Build example projects with you can learn & play" OFF) option(OMATH_STATIC_MSVC_RUNTIME_LIBRARY "Force Omath to link static runtime" OFF) option(OMATH_SUPRESS_SAFETY_CHECKS "Supress some safety checks in release build to improve general performance" ON) -option(OMATH_USE_UNITY_BUILD "Will enable unity build to speed up compilation" OFF) +option(OMATH_USE_UNITY_BUILD "Will enable unity build to speed up compilation" ON) if (OMATH_BUILD_AS_SHARED_LIBRARY) add_library(omath SHARED source/matrix.cpp)