mirror of
https://github.com/orange-cpp/omath.git
synced 2026-04-18 21:43:27 +00:00
Compare commits
62 Commits
v5.1.0.rc1
...
feature/ph
| Author | SHA1 | Date | |
|---|---|---|---|
| 69e9735063 | |||
| 35b8510cf4 | |||
| ba662e44e2 | |||
| 2002bcca83 | |||
| ffacba71e2 | |||
| 6081a9c426 | |||
| 8bbd504356 | |||
| 1d54039f57 | |||
| 93fc93d4f6 | |||
| b8a578774c | |||
| bfa6c77776 | |||
| 1341ef9925 | |||
| 3ccbb0b25b | |||
| 68993db48a | |||
| 337204b3bf | |||
| 1e601d2d8f | |||
| 2b4a75d011 | |||
| 99a30e8fdf | |||
| 0cdd1d021f | |||
| a79ac9743a | |||
| f26afb703b | |||
| f237ee5f6a | |||
| 9058ea9b39 | |||
| f707ac1adb | |||
| cbdabd3fc2 | |||
| 30e3feb4f8 | |||
| 0726fdef32 | |||
| 0ffe0c2bdc | |||
| e9600ad42b | |||
| 673835618c | |||
| afb2a13dd6 | |||
| 943472cf64 | |||
| 9752accb14 | |||
| 7373e6d3df | |||
| 68f4c8cc72 | |||
| 2dafc8a49d | |||
| 11fe49e801 | |||
| dee705a391 | |||
| bfe147ef80 | |||
| 2c70288a8f | |||
| 529322fe34 | |||
| 414b2af289 | |||
| a79ad6948c | |||
| ea2c7c3d7f | |||
| 91c2e0d74b | |||
| 52e9b906ff | |||
| cc6d625c2d | |||
| 5eaec70846 | |||
| 2063c4d33a | |||
| 60bf8ca30f | |||
| 6fca106edc | |||
| 78cb644920 | |||
| 646a920e4c | |||
| 52687a70c7 | |||
| a9eff7d320 | |||
| 211e4c3d9b | |||
| 74dc2234f7 | |||
| 7ebbed6763 | |||
| e271bccaf5 | |||
| 50765f69c5 | |||
| 1169534133 | |||
| 783501aab9 |
4
.github/FUNDING.yml
vendored
Normal file
4
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
# These are supported funding model platforms
|
||||
|
||||
open_collective: libomathorg
|
||||
github: orange-cpp
|
||||
20
.github/workflows/cmake-multi-platform.yml
vendored
20
.github/workflows/cmake-multi-platform.yml
vendored
@@ -107,7 +107,7 @@ jobs:
|
||||
-DOMATH_BUILD_TESTS=ON \
|
||||
-DOMATH_BUILD_BENCHMARK=OFF \
|
||||
-DOMATH_ENABLE_COVERAGE=${{ matrix.coverage == true && 'ON' || 'OFF' }} \
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui;avx2;tests"
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui;avx2;tests;lua"
|
||||
|
||||
- name: Build
|
||||
shell: bash
|
||||
@@ -193,7 +193,7 @@ jobs:
|
||||
-DOMATH_BUILD_TESTS=ON \
|
||||
-DOMATH_BUILD_BENCHMARK=OFF \
|
||||
-DOMATH_ENABLE_COVERAGE=OFF \
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui;avx2;tests"
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui;avx2;tests;lua"
|
||||
|
||||
- name: Build
|
||||
shell: bash
|
||||
@@ -234,7 +234,7 @@ jobs:
|
||||
-DOMATH_ENABLE_COVERAGE=ON \
|
||||
-DOMATH_THREAT_WARNING_AS_ERROR=OFF \
|
||||
-DCMAKE_BUILD_TYPE=Debug \
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui;avx2;tests"
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui;avx2;tests;lua"
|
||||
cmake --build cmake-build/build/${{ matrix.preset }} --config Debug --target unit_tests omath
|
||||
|
||||
- name: Run Tests (Generates .profraw)
|
||||
@@ -373,7 +373,7 @@ jobs:
|
||||
-DOMATH_BUILD_TESTS=ON \
|
||||
-DOMATH_BUILD_BENCHMARK=OFF \
|
||||
-DOMATH_ENABLE_COVERAGE=${{ matrix.coverage == true && 'ON' || 'OFF' }} \
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui;avx2;tests"
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui;avx2;tests;lua"
|
||||
|
||||
- name: Build
|
||||
shell: bash
|
||||
@@ -450,7 +450,7 @@ jobs:
|
||||
-DVCPKG_INSTALL_OPTIONS="--allow-unsupported" \
|
||||
-DOMATH_BUILD_TESTS=ON \
|
||||
-DOMATH_BUILD_BENCHMARK=OFF \
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui;tests"
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui;tests;lua"
|
||||
|
||||
- name: Build
|
||||
shell: bash
|
||||
@@ -509,7 +509,7 @@ jobs:
|
||||
cmake --preset ${{ matrix.preset }} \
|
||||
-DOMATH_BUILD_TESTS=ON \
|
||||
-DOMATH_BUILD_BENCHMARK=OFF \
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui;avx2;tests" \
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui;avx2;tests;lua" \
|
||||
-DVCPKG_INSTALL_OPTIONS="--allow-unsupported"
|
||||
cmake --build cmake-build/build/${{ matrix.preset }} --target unit_tests omath
|
||||
./out/Release/unit_tests
|
||||
@@ -581,7 +581,7 @@ jobs:
|
||||
-DVCPKG_INSTALL_OPTIONS="--allow-unsupported" \
|
||||
-DOMATH_BUILD_TESTS=ON \
|
||||
-DOMATH_BUILD_BENCHMARK=OFF \
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui;tests"
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui;tests;lua"
|
||||
|
||||
- name: Build
|
||||
shell: bash
|
||||
@@ -650,7 +650,7 @@ jobs:
|
||||
-DVCPKG_INSTALL_OPTIONS="--allow-unsupported" \
|
||||
-DOMATH_BUILD_TESTS=ON \
|
||||
-DOMATH_BUILD_BENCHMARK=OFF \
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui;tests"
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui;tests;lua"
|
||||
|
||||
- name: Build
|
||||
shell: bash
|
||||
@@ -735,7 +735,7 @@ jobs:
|
||||
-DVCPKG_INSTALL_OPTIONS="--allow-unsupported" \
|
||||
-DOMATH_BUILD_TESTS=ON \
|
||||
-DOMATH_BUILD_BENCHMARK=OFF \
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui;tests"
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui;tests;lua"
|
||||
|
||||
- name: Build
|
||||
run: |
|
||||
@@ -800,7 +800,7 @@ jobs:
|
||||
-DOMATH_BUILD_TESTS=ON \
|
||||
-DOMATH_BUILD_BENCHMARK=ON \
|
||||
-DOMATH_ENABLE_VALGRIND=ON \
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui;avx2;tests;benchmark"
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui;avx2;lua;tests;benchmark"
|
||||
|
||||
- name: Build All Targets
|
||||
shell: bash
|
||||
|
||||
5
.luarc.json
Normal file
5
.luarc.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"diagnostics.globals": [
|
||||
"omath"
|
||||
]
|
||||
}
|
||||
@@ -31,6 +31,11 @@ option(OMATH_SUPRESS_SAFETY_CHECKS
|
||||
option(OMATH_ENABLE_COVERAGE "Enable coverage" OFF)
|
||||
option(OMATH_ENABLE_FORCE_INLINE
|
||||
"Will for compiler to make some functions to be force inlined no matter what" ON)
|
||||
|
||||
option(OMATH_ENABLE_LUA
|
||||
"omath bindings for lua" OFF)
|
||||
option(OMATH_ENABLE_PHYSX
|
||||
"PhysX-backed collider implementations" OFF)
|
||||
if(VCPKG_MANIFEST_FEATURES)
|
||||
foreach(omath_feature IN LISTS VCPKG_MANIFEST_FEATURES)
|
||||
if(omath_feature STREQUAL "imgui")
|
||||
@@ -43,6 +48,10 @@ if(VCPKG_MANIFEST_FEATURES)
|
||||
set(OMATH_BUILD_BENCHMARK ON)
|
||||
elseif(omath_feature STREQUAL "examples")
|
||||
set(OMATH_BUILD_EXAMPLES ON)
|
||||
elseif(omath_feature STREQUAL "lua")
|
||||
set(OMATH_ENABLE_LUA ON)
|
||||
elseif(omath_feature STREQUAL "physx")
|
||||
set(OMATH_ENABLE_PHYSX ON)
|
||||
endif()
|
||||
|
||||
endforeach()
|
||||
@@ -72,6 +81,8 @@ if(${PROJECT_IS_TOP_LEVEL})
|
||||
message(STATUS "[${PROJECT_NAME}]: Building using vcpkg ${OMATH_BUILD_VIA_VCPKG}")
|
||||
message(STATUS "[${PROJECT_NAME}]: Coverage feature status ${OMATH_ENABLE_COVERAGE}")
|
||||
message(STATUS "[${PROJECT_NAME}]: Valgrind feature status ${OMATH_ENABLE_VALGRIND}")
|
||||
message(STATUS "[${PROJECT_NAME}]: Lua feature status ${OMATH_ENABLE_LUA}")
|
||||
message(STATUS "[${PROJECT_NAME}]: PhysX feature status ${OMATH_ENABLE_PHYSX}")
|
||||
endif()
|
||||
|
||||
file(GLOB_RECURSE OMATH_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp")
|
||||
@@ -83,6 +94,24 @@ else()
|
||||
add_library(${PROJECT_NAME} STATIC ${OMATH_SOURCES} ${OMATH_HEADERS})
|
||||
endif()
|
||||
|
||||
if (OMATH_ENABLE_LUA)
|
||||
target_compile_definitions(${PROJECT_NAME} PUBLIC OMATH_ENABLE_LUA)
|
||||
|
||||
find_package(Lua REQUIRED)
|
||||
target_include_directories(${PROJECT_NAME} PRIVATE ${LUA_INCLUDE_DIR})
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE ${LUA_LIBRARIES})
|
||||
|
||||
find_path(SOL2_INCLUDE_DIRS "sol/abort.hpp")
|
||||
target_include_directories(${PROJECT_NAME} PRIVATE ${SOL2_INCLUDE_DIRS})
|
||||
endif ()
|
||||
|
||||
if (OMATH_ENABLE_PHYSX)
|
||||
target_compile_definitions(${PROJECT_NAME} PUBLIC OMATH_ENABLE_PHYSX)
|
||||
|
||||
find_package(unofficial-omniverse-physx-sdk CONFIG REQUIRED)
|
||||
target_link_libraries(${PROJECT_NAME} PUBLIC unofficial::omniverse-physx-sdk::sdk)
|
||||
endif ()
|
||||
|
||||
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
|
||||
|
||||
target_compile_definitions(${PROJECT_NAME} PUBLIC OMATH_VERSION="${PROJECT_VERSION}")
|
||||
@@ -174,6 +203,12 @@ elseif(OMATH_THREAT_WARNING_AS_ERROR)
|
||||
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -Wpedantic -Werror)
|
||||
endif()
|
||||
|
||||
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
|
||||
target_compile_options(${PROJECT_NAME} PRIVATE /bigobj)
|
||||
endif()
|
||||
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_HOST_SYSTEM_NAME EQUAL "Windows")
|
||||
target_compile_options(${PROJECT_NAME} PRIVATE -mbig-obj)
|
||||
endif()
|
||||
# Windows SDK redefine min/max via preprocessor and break std::min and std::max
|
||||
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
|
||||
target_compile_definitions(${PROJECT_NAME} INTERFACE NOMINMAX)
|
||||
|
||||
@@ -145,7 +145,7 @@
|
||||
"hidden": true,
|
||||
"inherits": ["linux-base", "vcpkg-base"],
|
||||
"cacheVariables": {
|
||||
"VCPKG_MANIFEST_FEATURES": "tests;imgui;avx2"
|
||||
"VCPKG_MANIFEST_FEATURES": "tests;imgui;avx2;lua;physx"
|
||||
}
|
||||
},
|
||||
{
|
||||
@@ -235,7 +235,7 @@
|
||||
"hidden": true,
|
||||
"inherits": ["darwin-base", "vcpkg-base"],
|
||||
"cacheVariables": {
|
||||
"VCPKG_MANIFEST_FEATURES": "tests;imgui;avx2;examples"
|
||||
"VCPKG_MANIFEST_FEATURES": "tests;imgui;avx2;examples;lua"
|
||||
}
|
||||
},
|
||||
{
|
||||
|
||||
37
CODEOWNERS
Normal file
37
CODEOWNERS
Normal file
@@ -0,0 +1,37 @@
|
||||
## List of maintainers for the omath library
|
||||
|
||||
## This file purpose is to give newcomers to the project the responsible
|
||||
## developers when submitting a pull request on GitHub, or opening a bug
|
||||
## report in issues.
|
||||
|
||||
## This file will notably establish who is responsible for a specific
|
||||
## area of omath. Being a maintainer means the following:
|
||||
## - that person has good knownledge in the area
|
||||
## - that person is able to enforce consistency in the area
|
||||
## - that person may be available for giving help in the area
|
||||
## - that person has push access on the repository
|
||||
## Being a maintainer does not mean the following:
|
||||
## - that person is dedicated to the area
|
||||
## - that person is working full-time on the area/on omath
|
||||
## - that person is paid
|
||||
## - that person is always available
|
||||
|
||||
|
||||
# omath core source code
|
||||
/source @orange-cpp
|
||||
/include @orange-cpp
|
||||
|
||||
# Tests and becnchmarks
|
||||
/benchmark @orange-cpp
|
||||
/tests @orange-cpp @luadebug
|
||||
|
||||
# Examples and documentation
|
||||
/examples @luadebug @orange-cpp
|
||||
/docs @orange-cpp
|
||||
|
||||
# Misc like formating
|
||||
/scripts @luadebug
|
||||
/pixi @luadebug
|
||||
|
||||
# CI/CD
|
||||
/.github/workflows @luadbg @orange-cpp
|
||||
42
INSTALL.md
42
INSTALL.md
@@ -1,6 +1,6 @@
|
||||
# 📥Installation Guide
|
||||
|
||||
## <img width="28px" src="https://vcpkg.io/assets/mark/mark.svg" /> Using vcpkg
|
||||
## <img width="28px" src="https://vcpkg.io/assets/mark/mark.svg" /> Using vcpkg (recomended)
|
||||
**Note**: Support vcpkg for package management
|
||||
1. Install [vcpkg](https://github.com/microsoft/vcpkg)
|
||||
2. Run the following command to install the orange-math package:
|
||||
@@ -28,6 +28,46 @@ target("...")
|
||||
add_packages("omath")
|
||||
```
|
||||
|
||||
## <img width="28px" src="https://github.githubassets.com/favicons/favicon.svg" /> Using prebuilt binaries (GitHub Releases)
|
||||
|
||||
**Note**: This is the fastest option if you don’t want to build from source.
|
||||
|
||||
1. **Go to the Releases page**
|
||||
- Open the project’s GitHub **Releases** page and choose the latest version.
|
||||
|
||||
2. **Download the correct asset for your platform**
|
||||
- Pick the archive that matches your OS and architecture (for example: Windows x64 / Linux x64 / macOS arm64).
|
||||
|
||||
3. **Extract the archive**
|
||||
- You should end up with something like:
|
||||
- `include/` (headers)
|
||||
- `lib/` or `bin/` (library files / DLLs)
|
||||
- sometimes `cmake/` (CMake package config)
|
||||
|
||||
4. **Use it in your project**
|
||||
|
||||
### Option A: CMake package (recommended if the release includes CMake config files)
|
||||
If the extracted folder contains something like `lib/cmake/omath` or `cmake/omath`, you can point CMake to it:
|
||||
|
||||
```cmake
|
||||
# Example: set this to the extracted prebuilt folder
|
||||
list(APPEND CMAKE_PREFIX_PATH "path/to/omath-prebuilt")
|
||||
|
||||
find_package(omath CONFIG REQUIRED)
|
||||
target_link_libraries(main PRIVATE omath::omath)
|
||||
```
|
||||
### Option B: Manual include + link (works with any layout)
|
||||
If there’s no CMake package config, link it manually:
|
||||
```cmake
|
||||
target_include_directories(main PRIVATE "path/to/omath-prebuilt/include")
|
||||
|
||||
# Choose ONE depending on what you downloaded:
|
||||
# - Static library: .lib / .a
|
||||
# - Shared library: .dll + .lib import (Windows), .so (Linux), .dylib (macOS)
|
||||
|
||||
target_link_directories(main PRIVATE "path/to/omath-prebuilt/lib")
|
||||
target_link_libraries(main PRIVATE omath) # or the actual library filename
|
||||
```
|
||||
## <img width="28px" src="https://upload.wikimedia.org/wikipedia/commons/e/ef/CMake_logo.svg?" /> Build from source using CMake
|
||||
1. **Preparation**
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
[](https://www.codefactor.io/repository/github/orange-cpp/omath)
|
||||

|
||||
[](https://repology.org/project/orange-math/versions)
|
||||

|
||||

|
||||
[](https://discord.gg/eDgdaWbqwZ)
|
||||
[](https://t.me/orangennotes)
|
||||
@@ -83,6 +84,7 @@ if (auto screen = camera.world_to_screen(world_position)) {
|
||||
- **Engine support**: Supports coordinate systems of **Source, Unity, Unreal, Frostbite, IWEngine, CryEngine and canonical OpenGL**.
|
||||
- **Cross platform**: Supports Windows, MacOS and Linux.
|
||||
- **Algorithms**: Has ability to scan for byte pattern with wildcards in ELF/Mach-O/PE files/modules, binary slices, works even with Wine apps.
|
||||
- **Scripting**: Supports to make scripts in Lua out of box
|
||||
- **Battle tested**: It's already used by some big players on the market like wraith.su and bluedream.ltd
|
||||
<div align = center>
|
||||
|
||||
|
||||
161
benchmark/benchmark_collision.cpp
Normal file
161
benchmark/benchmark_collision.cpp
Normal file
@@ -0,0 +1,161 @@
|
||||
//
|
||||
// Created by Vlad on 3/2/2026.
|
||||
//
|
||||
#include <benchmark/benchmark.h>
|
||||
#include <memory_resource>
|
||||
#include <omath/collision/epa_algorithm.hpp>
|
||||
#include <omath/collision/gjk_algorithm.hpp>
|
||||
#include <omath/engines/source_engine/collider.hpp>
|
||||
#include <omath/engines/source_engine/mesh.hpp>
|
||||
|
||||
using Mesh = omath::source_engine::Mesh;
|
||||
using Collider = omath::source_engine::MeshCollider;
|
||||
using Gjk = omath::collision::GjkAlgorithm<Collider>;
|
||||
using Epa = omath::collision::Epa<Collider>;
|
||||
|
||||
namespace
|
||||
{
|
||||
// Unit cube with half-extent 1 — 8 vertices in [-1,1]^3.
|
||||
const std::vector<omath::primitives::Vertex<>> k_cube_vbo = {
|
||||
{ { -1.f, -1.f, -1.f }, {}, {} },
|
||||
{ { -1.f, -1.f, 1.f }, {}, {} },
|
||||
{ { -1.f, 1.f, -1.f }, {}, {} },
|
||||
{ { -1.f, 1.f, 1.f }, {}, {} },
|
||||
{ { 1.f, 1.f, 1.f }, {}, {} },
|
||||
{ { 1.f, 1.f, -1.f }, {}, {} },
|
||||
{ { 1.f, -1.f, 1.f }, {}, {} },
|
||||
{ { 1.f, -1.f, -1.f }, {}, {} },
|
||||
};
|
||||
const std::vector<omath::Vector3<std::uint32_t>> k_empty_vao{};
|
||||
} // namespace
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// GJK benchmarks
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// Separated cubes — origin distance 2.1, no overlap.
|
||||
// Exercises the early-exit path and the centroid-based initial direction.
|
||||
static void BM_Gjk_Separated(benchmark::State& state)
|
||||
{
|
||||
const Collider a{Mesh{k_cube_vbo, k_empty_vao}};
|
||||
|
||||
Mesh mesh_b{k_cube_vbo, k_empty_vao};
|
||||
mesh_b.set_origin({0.f, 2.1f, 0.f});
|
||||
const Collider b{mesh_b};
|
||||
|
||||
for ([[maybe_unused]] auto _ : state)
|
||||
benchmark::DoNotOptimize(Gjk::is_collide(a, b));
|
||||
}
|
||||
|
||||
// Overlapping cubes — B offset by 0.5 along X, ~1.5 units penetration depth.
|
||||
static void BM_Gjk_Overlapping(benchmark::State& state)
|
||||
{
|
||||
const Collider a{Mesh{k_cube_vbo, k_empty_vao}};
|
||||
|
||||
Mesh mesh_b{k_cube_vbo, k_empty_vao};
|
||||
mesh_b.set_origin({0.5f, 0.f, 0.f});
|
||||
const Collider b{mesh_b};
|
||||
|
||||
for ([[maybe_unused]] auto _ : state)
|
||||
benchmark::DoNotOptimize(Gjk::is_collide(a, b));
|
||||
}
|
||||
|
||||
// Identical cubes at the same origin — deep overlap / worst case for GJK.
|
||||
static void BM_Gjk_SameOrigin(benchmark::State& state)
|
||||
{
|
||||
const Collider a{Mesh{k_cube_vbo, k_empty_vao}};
|
||||
const Collider b{Mesh{k_cube_vbo, k_empty_vao}};
|
||||
|
||||
for ([[maybe_unused]] auto _ : state)
|
||||
benchmark::DoNotOptimize(Gjk::is_collide(a, b));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// EPA benchmarks
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// EPA with a pre-allocated monotonic buffer (reset each iteration).
|
||||
// Isolates algorithmic cost from allocator overhead.
|
||||
static void BM_Epa_MonotonicBuffer(benchmark::State& state)
|
||||
{
|
||||
const Collider a{Mesh{k_cube_vbo, k_empty_vao}};
|
||||
|
||||
Mesh mesh_b{k_cube_vbo, k_empty_vao};
|
||||
mesh_b.set_origin({0.5f, 0.f, 0.f});
|
||||
const Collider b{mesh_b};
|
||||
|
||||
const auto [hit, simplex] = Gjk::is_collide_with_simplex_info(a, b);
|
||||
if (!hit)
|
||||
return; // shouldn't happen, but guard for safety
|
||||
|
||||
constexpr Epa::Params params{.max_iterations = 64, .tolerance = 1e-4f};
|
||||
|
||||
// Pre-allocate a 32 KiB stack buffer — enough for typical polytope growth.
|
||||
constexpr std::size_t k_buf_size = 32768;
|
||||
alignas(std::max_align_t) char buf[k_buf_size];
|
||||
std::pmr::monotonic_buffer_resource mr{buf, k_buf_size, std::pmr::null_memory_resource()};
|
||||
|
||||
for ([[maybe_unused]] auto _ : state)
|
||||
{
|
||||
mr.release(); // reset the buffer without touching the upstream resource
|
||||
benchmark::DoNotOptimize(Epa::solve(a, b, simplex, params, mr));
|
||||
}
|
||||
}
|
||||
|
||||
// EPA with the default (malloc-backed) memory resource.
|
||||
// Shows total cost including allocator pressure.
|
||||
static void BM_Epa_DefaultResource(benchmark::State& state)
|
||||
{
|
||||
const Collider a{Mesh{k_cube_vbo, k_empty_vao}};
|
||||
|
||||
Mesh mesh_b{k_cube_vbo, k_empty_vao};
|
||||
mesh_b.set_origin({0.5f, 0.f, 0.f});
|
||||
const Collider b{mesh_b};
|
||||
|
||||
const auto [hit, simplex] = Gjk::is_collide_with_simplex_info(a, b);
|
||||
if (!hit)
|
||||
return;
|
||||
|
||||
constexpr Epa::Params params{.max_iterations = 64, .tolerance = 1e-4f};
|
||||
|
||||
for ([[maybe_unused]] auto _ : state)
|
||||
benchmark::DoNotOptimize(Epa::solve(a, b, simplex, params));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Combined GJK + EPA pipeline
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// Full collision pipeline: GJK detects contact, EPA resolves penetration.
|
||||
// This is the hot path in a physics engine tick.
|
||||
static void BM_GjkEpa_Pipeline(benchmark::State& state)
|
||||
{
|
||||
const Collider a{Mesh{k_cube_vbo, k_empty_vao}};
|
||||
|
||||
Mesh mesh_b{k_cube_vbo, k_empty_vao};
|
||||
mesh_b.set_origin({0.5f, 0.f, 0.f});
|
||||
const Collider b{mesh_b};
|
||||
|
||||
constexpr Epa::Params params{.max_iterations = 64, .tolerance = 1e-4f};
|
||||
|
||||
constexpr std::size_t k_buf_size = 32768;
|
||||
alignas(std::max_align_t) char buf[k_buf_size];
|
||||
std::pmr::monotonic_buffer_resource mr{buf, k_buf_size, std::pmr::null_memory_resource()};
|
||||
|
||||
for ([[maybe_unused]] auto _ : state)
|
||||
{
|
||||
mr.release();
|
||||
const auto [hit, simplex] = Gjk::is_collide_with_simplex_info(a, b);
|
||||
if (hit)
|
||||
benchmark::DoNotOptimize(Epa::solve(a, b, simplex, params, mr));
|
||||
}
|
||||
}
|
||||
|
||||
BENCHMARK(BM_Gjk_Separated)->Iterations(100'000);
|
||||
BENCHMARK(BM_Gjk_Overlapping)->Iterations(100'000);
|
||||
BENCHMARK(BM_Gjk_SameOrigin)->Iterations(100'000);
|
||||
|
||||
BENCHMARK(BM_Epa_MonotonicBuffer)->Iterations(100'000);
|
||||
BENCHMARK(BM_Epa_DefaultResource)->Iterations(100'000);
|
||||
|
||||
BENCHMARK(BM_GjkEpa_Pipeline)->Iterations(100'000);
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <memory>
|
||||
#include <memory_resource>
|
||||
#include <queue>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
@@ -56,83 +57,76 @@ namespace omath::collision
|
||||
const Simplex<VectorType>& simplex, const Params params = {},
|
||||
std::pmr::memory_resource& mem_resource = *std::pmr::get_default_resource())
|
||||
{
|
||||
// --- Build initial polytope from simplex (4 points) ---
|
||||
std::pmr::vector<VectorType> vertexes = build_initial_polytope_from_simplex(simplex, mem_resource);
|
||||
|
||||
// Initial tetra faces (windings corrected in make_face)
|
||||
std::pmr::vector<Face> faces = create_initial_tetra_faces(mem_resource, vertexes);
|
||||
|
||||
auto heap = rebuild_heap(faces, mem_resource);
|
||||
// Build initial min-heap by distance.
|
||||
Heap heap = rebuild_heap(faces, mem_resource);
|
||||
|
||||
Result out{};
|
||||
|
||||
// Hoisted outside the loop to reuse bucket allocation across iterations.
|
||||
// Initial bucket count 16 covers a typical horizon without rehashing.
|
||||
BoundaryMap boundary{16, &mem_resource};
|
||||
|
||||
for (int it = 0; it < params.max_iterations; ++it)
|
||||
{
|
||||
// If heap might be stale after face edits, rebuild lazily.
|
||||
if (heap.empty())
|
||||
break;
|
||||
// Rebuild when the "closest" face changed (simple cheap guard)
|
||||
// (We could keep face handles; this is fine for small Ns.)
|
||||
|
||||
if (const auto top = heap.top(); faces[top.idx].d != top.d)
|
||||
heap = rebuild_heap(faces, mem_resource);
|
||||
// Lazily discard stale (deleted or index-mismatched) heap entries.
|
||||
discard_stale_heap_entries(faces, heap);
|
||||
|
||||
if (heap.empty())
|
||||
break;
|
||||
|
||||
// FIXME: STORE REF VALUE, DO NOT USE
|
||||
// AFTER IF STATEMENT BLOCK
|
||||
const Face& face = faces[heap.top().idx];
|
||||
|
||||
// Get the furthest point in face normal direction
|
||||
const VectorType p = support_point(a, b, face.n);
|
||||
const auto p_dist = face.n.dot(p);
|
||||
|
||||
// Converged if we can’t push the face closer than tolerance
|
||||
// Converged: new support can't push the face closer than tolerance.
|
||||
if (p_dist - face.d <= params.tolerance)
|
||||
{
|
||||
out.normal = face.n;
|
||||
out.depth = face.d; // along unit normal
|
||||
out.depth = face.d;
|
||||
out.iterations = it + 1;
|
||||
out.num_vertices = static_cast<int>(vertexes.size());
|
||||
out.num_faces = static_cast<int>(faces.size());
|
||||
|
||||
out.penetration_vector = out.normal * out.depth;
|
||||
return out;
|
||||
}
|
||||
|
||||
// Add new vertex
|
||||
const int new_idx = static_cast<int>(vertexes.size());
|
||||
vertexes.emplace_back(p);
|
||||
|
||||
const auto [to_delete, boundary] = mark_visible_and_collect_horizon(faces, p);
|
||||
// Tombstone visible faces and collect the horizon boundary.
|
||||
// This avoids copying the faces array (O(n)) each iteration.
|
||||
tombstone_visible_faces(faces, boundary, p);
|
||||
|
||||
erase_marked(faces, to_delete);
|
||||
|
||||
// Stitch new faces around the horizon
|
||||
for (const auto& e : boundary)
|
||||
// Stitch new faces around the horizon and push them directly onto the
|
||||
// heap — no full O(n log n) rebuild needed.
|
||||
for (const auto& [key, e] : boundary)
|
||||
{
|
||||
const int fi = static_cast<int>(faces.size());
|
||||
faces.emplace_back(make_face(vertexes, e.a, e.b, new_idx));
|
||||
|
||||
// Rebuild heap after topology change
|
||||
heap = rebuild_heap(faces, mem_resource);
|
||||
heap.emplace(faces.back().d, fi);
|
||||
}
|
||||
|
||||
if (!std::isfinite(vertexes.back().dot(vertexes.back())))
|
||||
break; // safety
|
||||
|
||||
out.iterations = it + 1;
|
||||
}
|
||||
|
||||
if (faces.empty())
|
||||
// Find the best surviving (non-deleted) face.
|
||||
const Face* best = find_best_surviving_face(faces);
|
||||
|
||||
if (!best)
|
||||
return std::nullopt;
|
||||
|
||||
const auto best = *std::ranges::min_element(faces, [](const auto& first, const auto& second)
|
||||
{ return first.d < second.d; });
|
||||
out.normal = best.n;
|
||||
out.depth = best.d;
|
||||
out.normal = best->n;
|
||||
out.depth = best->d;
|
||||
out.num_vertices = static_cast<int>(vertexes.size());
|
||||
out.num_faces = static_cast<int>(faces.size());
|
||||
|
||||
out.penetration_vector = out.normal * out.depth;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -141,7 +135,8 @@ namespace omath::collision
|
||||
{
|
||||
int i0, i1, i2;
|
||||
VectorType n; // unit outward normal
|
||||
FloatingType d; // n · v0 (>=0 ideally because origin is inside)
|
||||
FloatingType d; // n · v0 (>= 0 ideally because origin is inside)
|
||||
bool deleted{false}; // tombstone flag — avoids O(n) compaction per iteration
|
||||
};
|
||||
|
||||
struct Edge final
|
||||
@@ -154,6 +149,7 @@ namespace omath::collision
|
||||
FloatingType d;
|
||||
int idx;
|
||||
};
|
||||
|
||||
struct HeapCmp final
|
||||
{
|
||||
[[nodiscard]]
|
||||
@@ -165,35 +161,44 @@ namespace omath::collision
|
||||
|
||||
using Heap = std::priority_queue<HeapItem, std::pmr::vector<HeapItem>, HeapCmp>;
|
||||
|
||||
// Horizon boundary: maps packed(a,b) → Edge.
|
||||
// Opposite edges cancel in O(1) via hash lookup instead of O(h) linear scan.
|
||||
using BoundaryMap = std::pmr::unordered_map<std::int64_t, Edge>;
|
||||
|
||||
[[nodiscard]]
|
||||
static constexpr std::int64_t pack_edge(const int a, const int b) noexcept
|
||||
{
|
||||
return (static_cast<std::int64_t>(a) << 32) | static_cast<std::uint32_t>(b);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
static Heap rebuild_heap(const std::pmr::vector<Face>& faces, auto& memory_resource)
|
||||
{
|
||||
std::pmr::vector<HeapItem> storage{&memory_resource};
|
||||
storage.reserve(faces.size()); // optional but recommended
|
||||
|
||||
storage.reserve(faces.size());
|
||||
Heap h{HeapCmp{}, std::move(storage)};
|
||||
|
||||
for (int i = 0; i < static_cast<int>(faces.size()); ++i)
|
||||
h.emplace(faces[i].d, i);
|
||||
|
||||
return h; // allocator is preserved
|
||||
if (!faces[i].deleted)
|
||||
h.emplace(faces[i].d, i);
|
||||
return h;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
static bool visible_from(const Face& f, const VectorType& p)
|
||||
{
|
||||
// positive if p is in front of the face
|
||||
return f.n.dot(p) - f.d > static_cast<FloatingType>(1e-7);
|
||||
}
|
||||
|
||||
static void add_edge_boundary(std::pmr::vector<Edge>& boundary, int a, int b)
|
||||
static void add_edge_boundary(BoundaryMap& boundary, int a, int b)
|
||||
{
|
||||
// Keep edges that appear only once; erase if opposite already present
|
||||
auto itb = std::ranges::find_if(boundary, [&](const Edge& e) { return e.a == b && e.b == a; });
|
||||
if (itb != boundary.end())
|
||||
boundary.erase(itb); // internal edge cancels out
|
||||
// O(1) cancel: if the opposite edge (b→a) is already in the map it is an
|
||||
// internal edge shared by two visible faces and must be removed.
|
||||
// Otherwise this is a horizon edge and we insert it.
|
||||
const std::int64_t rev = pack_edge(b, a);
|
||||
if (const auto it = boundary.find(rev); it != boundary.end())
|
||||
boundary.erase(it);
|
||||
else
|
||||
boundary.emplace_back(a, b); // horizon edge (directed)
|
||||
boundary.emplace(pack_edge(a, b), Edge{a, b});
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
@@ -204,9 +209,7 @@ namespace omath::collision
|
||||
const VectorType& a2 = vertexes[i2];
|
||||
VectorType n = (a1 - a0).cross(a2 - a0);
|
||||
if (n.dot(n) <= static_cast<FloatingType>(1e-30))
|
||||
{
|
||||
n = any_perp_vec(a1 - a0); // degenerate guard
|
||||
}
|
||||
// Ensure normal points outward (away from origin): require n·a0 >= 0
|
||||
if (n.dot(a0) < static_cast<FloatingType>(0.0))
|
||||
{
|
||||
@@ -243,6 +246,7 @@ namespace omath::collision
|
||||
return d;
|
||||
return V{1, 0, 0};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
static std::pmr::vector<Face> create_initial_tetra_faces(std::pmr::memory_resource& mem_resource,
|
||||
const std::pmr::vector<VectorType>& vertexes)
|
||||
@@ -262,48 +266,45 @@ namespace omath::collision
|
||||
{
|
||||
std::pmr::vector<VectorType> vertexes{&mem_resource};
|
||||
vertexes.reserve(simplex.size());
|
||||
|
||||
for (std::size_t i = 0; i < simplex.size(); ++i)
|
||||
vertexes.emplace_back(simplex[i]);
|
||||
|
||||
return vertexes;
|
||||
}
|
||||
static void erase_marked(std::pmr::vector<Face>& faces, const std::pmr::vector<bool>& to_delete)
|
||||
|
||||
static const Face* find_best_surviving_face(const std::pmr::vector<Face>& faces)
|
||||
{
|
||||
auto* mr = faces.get_allocator().resource(); // keep same resource
|
||||
std::pmr::vector<Face> kept{mr};
|
||||
kept.reserve(faces.size());
|
||||
|
||||
for (std::size_t i = 0; i < faces.size(); ++i)
|
||||
if (!to_delete[i])
|
||||
kept.emplace_back(faces[i]);
|
||||
|
||||
faces.swap(kept);
|
||||
const Face* best = nullptr;
|
||||
for (const auto& f : faces)
|
||||
if (!f.deleted && (best == nullptr || f.d < best->d))
|
||||
best = &f;
|
||||
return best;
|
||||
}
|
||||
struct Horizon
|
||||
static void tombstone_visible_faces(std::pmr::vector<Face>& faces, BoundaryMap& boundary,
|
||||
const VectorType& p)
|
||||
{
|
||||
std::pmr::vector<bool> to_delete;
|
||||
std::pmr::vector<Edge> boundary;
|
||||
};
|
||||
|
||||
static Horizon mark_visible_and_collect_horizon(const std::pmr::vector<Face>& faces, const VectorType& p)
|
||||
{
|
||||
auto* mr = faces.get_allocator().resource();
|
||||
|
||||
Horizon horizon{std::pmr::vector<bool>(faces.size(), false, mr), std::pmr::vector<Edge>(mr)};
|
||||
horizon.boundary.reserve(faces.size());
|
||||
|
||||
for (std::size_t i = 0; i < faces.size(); ++i)
|
||||
if (visible_from(faces[i], p))
|
||||
boundary.clear();
|
||||
for (auto& f : faces)
|
||||
{
|
||||
if (!f.deleted && visible_from(f, p))
|
||||
{
|
||||
const auto& rf = faces[i];
|
||||
horizon.to_delete[i] = true;
|
||||
add_edge_boundary(horizon.boundary, rf.i0, rf.i1);
|
||||
add_edge_boundary(horizon.boundary, rf.i1, rf.i2);
|
||||
add_edge_boundary(horizon.boundary, rf.i2, rf.i0);
|
||||
f.deleted = true;
|
||||
add_edge_boundary(boundary, f.i0, f.i1);
|
||||
add_edge_boundary(boundary, f.i1, f.i2);
|
||||
add_edge_boundary(boundary, f.i2, f.i0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return horizon;
|
||||
static void discard_stale_heap_entries(const std::pmr::vector<Face>& faces,
|
||||
std::priority_queue<HeapItem, std::pmr::vector<HeapItem>, HeapCmp>& heap)
|
||||
{
|
||||
while (!heap.empty())
|
||||
{
|
||||
const auto& top = heap.top();
|
||||
if (!faces[top.idx].deleted && faces[top.idx].d == top.d)
|
||||
break;
|
||||
heap.pop();
|
||||
}
|
||||
}
|
||||
};
|
||||
} // namespace omath::collision
|
||||
|
||||
@@ -14,11 +14,15 @@ namespace omath::collision
|
||||
Simplex<VertexType> simplex; // valid only if hit == true and size==4
|
||||
};
|
||||
|
||||
struct GjkSettings final
|
||||
{
|
||||
float epsilon = 1e-6f;
|
||||
std::size_t max_iterations = 64;
|
||||
};
|
||||
template<class ColliderInterfaceType>
|
||||
class GjkAlgorithm final
|
||||
{
|
||||
using VectorType = ColliderInterfaceType::VectorType;
|
||||
|
||||
public:
|
||||
[[nodiscard]]
|
||||
static VectorType find_support_vertex(const ColliderInterfaceType& collider_a,
|
||||
@@ -36,20 +40,34 @@ namespace omath::collision
|
||||
|
||||
[[nodiscard]]
|
||||
static GjkHitInfo<VectorType> is_collide_with_simplex_info(const ColliderInterfaceType& collider_a,
|
||||
const ColliderInterfaceType& collider_b)
|
||||
const ColliderInterfaceType& collider_b,
|
||||
const GjkSettings& settings = {})
|
||||
{
|
||||
auto support = find_support_vertex(collider_a, collider_b, VectorType{1, 0, 0});
|
||||
// Use centroid difference as initial direction — greatly reduces iterations for separated shapes.
|
||||
VectorType initial_dir;
|
||||
if constexpr (requires { collider_b.get_origin() - collider_a.get_origin(); })
|
||||
{
|
||||
initial_dir = collider_b.get_origin() - collider_a.get_origin();
|
||||
if (initial_dir.dot(initial_dir) < settings.epsilon * settings.epsilon)
|
||||
initial_dir = VectorType{1, 0, 0};
|
||||
}
|
||||
else
|
||||
{
|
||||
initial_dir = VectorType{1, 0, 0};
|
||||
}
|
||||
|
||||
auto support = find_support_vertex(collider_a, collider_b, initial_dir);
|
||||
|
||||
Simplex<VectorType> simplex;
|
||||
simplex.push_front(support);
|
||||
|
||||
auto direction = -support;
|
||||
|
||||
while (true)
|
||||
for (std::size_t iteration = 0; iteration < settings.max_iterations; ++iteration)
|
||||
{
|
||||
support = find_support_vertex(collider_a, collider_b, direction);
|
||||
|
||||
if (support.dot(direction) <= 0.f)
|
||||
if (support.dot(direction) <= settings.epsilon)
|
||||
return {false, simplex};
|
||||
|
||||
simplex.push_front(support);
|
||||
@@ -57,6 +75,7 @@ namespace omath::collision
|
||||
if (simplex.handle(direction))
|
||||
return {true, simplex};
|
||||
}
|
||||
return {false, simplex};
|
||||
}
|
||||
};
|
||||
} // namespace omath::collision
|
||||
@@ -42,13 +42,40 @@ namespace omath::collision
|
||||
m_mesh.set_origin(new_origin);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
const MeshType& get_mesh() const
|
||||
{
|
||||
return m_mesh;
|
||||
}
|
||||
[[nodiscard]]
|
||||
MeshType& get_mesh()
|
||||
{
|
||||
return m_mesh;
|
||||
}
|
||||
private:
|
||||
[[nodiscard]]
|
||||
const VertexType& find_furthest_vertex(const VectorType& direction) const
|
||||
{
|
||||
return *std::ranges::max_element(
|
||||
m_mesh.m_vertex_buffer, [&direction](const auto& first, const auto& second)
|
||||
{ return first.position.dot(direction) < second.position.dot(direction); });
|
||||
// The support query arrives in world space, but vertex positions are stored
|
||||
// in local space. We need argmax_v { world(v) · d }.
|
||||
//
|
||||
// world(v) = M·v (ignoring translation, which is constant across vertices)
|
||||
// world(v) · d = v · Mᵀ·d
|
||||
//
|
||||
// So we transform the direction to local space once — O(1) — then compare
|
||||
// raw local positions, which is far cheaper than calling
|
||||
// vertex_position_to_world_space (full 4×4 multiply) for every vertex.
|
||||
//
|
||||
// d_local = upper-left 3×3 of M, transposed, times d_world:
|
||||
// d_local[j] = sum_i M.at(i,j) * d[i] (i.e. column j of M dotted with d)
|
||||
const auto& m = m_mesh.get_to_world_matrix();
|
||||
const VectorType d_local = {
|
||||
m[0, 0] * direction.x + m[1, 0] * direction.y + m[2, 0] * direction.z,
|
||||
m[0, 1] * direction.x + m[1, 1] * direction.y + m[2, 1] * direction.z,
|
||||
m[0, 2] * direction.x + m[1, 2] * direction.y + m[2, 2] * direction.z,
|
||||
};
|
||||
return *std::ranges::max_element(m_mesh.m_vertex_buffer, [&d_local](const auto& first, const auto& second)
|
||||
{ return first.position.dot(d_local) < second.position.dot(d_local); });
|
||||
}
|
||||
MeshType m_mesh;
|
||||
};
|
||||
|
||||
59
include/omath/collision/physx_box_collider.hpp
Normal file
59
include/omath/collision/physx_box_collider.hpp
Normal file
@@ -0,0 +1,59 @@
|
||||
//
|
||||
// Created by orange-cpp
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#ifdef OMATH_ENABLE_PHYSX
|
||||
|
||||
#include "collider_interface.hpp"
|
||||
#include <PxPhysicsAPI.h>
|
||||
|
||||
namespace omath::collision
|
||||
{
|
||||
/// Axis-aligned box collider backed by PhysX PxBoxGeometry.
|
||||
/// Half-extents are stored in PhysX convention (positive values along each axis).
|
||||
class PhysXBoxCollider final : public ColliderInterface<Vector3<float>>
|
||||
{
|
||||
public:
|
||||
/// @param half_extents Half-widths along X, Y and Z axes (all must be > 0).
|
||||
/// @param origin World-space centre of the box.
|
||||
explicit PhysXBoxCollider(const VectorType& half_extents, const VectorType& origin = {})
|
||||
: m_geometry(physx::PxVec3(half_extents.x, half_extents.y, half_extents.z))
|
||||
, m_origin(origin)
|
||||
{
|
||||
}
|
||||
|
||||
/// Support function: returns the world-space point on the box furthest in @p direction.
|
||||
/// For a box, the furthest point along d is origin + (sign(d.x)*hx, sign(d.y)*hy, sign(d.z)*hz).
|
||||
[[nodiscard]]
|
||||
VectorType find_abs_furthest_vertex_position(const VectorType& direction) const override
|
||||
{
|
||||
const auto& he = m_geometry.halfExtents;
|
||||
return {
|
||||
m_origin.x + (direction.x >= 0.f ? he.x : -he.x),
|
||||
m_origin.y + (direction.y >= 0.f ? he.y : -he.y),
|
||||
m_origin.z + (direction.z >= 0.f ? he.z : -he.z),
|
||||
};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
const VectorType& get_origin() const override { return m_origin; }
|
||||
|
||||
void set_origin(const VectorType& new_origin) override { m_origin = new_origin; }
|
||||
|
||||
[[nodiscard]]
|
||||
const physx::PxBoxGeometry& get_geometry() const { return m_geometry; }
|
||||
|
||||
/// Update half-extents at runtime.
|
||||
void set_half_extents(const VectorType& half_extents)
|
||||
{
|
||||
m_geometry = physx::PxBoxGeometry(physx::PxVec3(half_extents.x, half_extents.y, half_extents.z));
|
||||
}
|
||||
|
||||
private:
|
||||
physx::PxBoxGeometry m_geometry;
|
||||
VectorType m_origin;
|
||||
};
|
||||
} // namespace omath::collision
|
||||
|
||||
#endif // OMATH_ENABLE_PHYSX
|
||||
137
include/omath/collision/physx_rigid_body.hpp
Normal file
137
include/omath/collision/physx_rigid_body.hpp
Normal file
@@ -0,0 +1,137 @@
|
||||
//
|
||||
// Created by orange-cpp
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#ifdef OMATH_ENABLE_PHYSX
|
||||
|
||||
#include "collider_interface.hpp"
|
||||
#include "physx_world.hpp"
|
||||
#include <PxPhysicsAPI.h>
|
||||
#include <extensions/PxRigidBodyExt.h>
|
||||
#include <cmath>
|
||||
|
||||
namespace omath::collision
|
||||
{
|
||||
/// Dynamic rigid body backed by a PhysX PxRigidDynamic actor.
|
||||
/// Implements ColliderInterface so it can participate in both omath GJK
|
||||
/// and PhysX simulation-based collision resolution.
|
||||
///
|
||||
/// Ownership: the actor is added to the world's scene on construction
|
||||
/// and removed + released on destruction.
|
||||
class PhysXRigidBody final : public ColliderInterface<Vector3<float>>
|
||||
{
|
||||
public:
|
||||
/// @param world PhysXWorld that owns the scene.
|
||||
/// @param geometry Shape geometry (PxBoxGeometry, PxSphereGeometry, …).
|
||||
/// @param origin Initial world-space position.
|
||||
/// @param density Mass density used to compute mass and inertia.
|
||||
PhysXRigidBody(PhysXWorld& world, const physx::PxGeometry& geometry,
|
||||
const VectorType& origin = {}, float density = 1.f)
|
||||
: m_world(world)
|
||||
, m_geometry(geometry)
|
||||
{
|
||||
const physx::PxTransform pose(physx::PxVec3(origin.x, origin.y, origin.z));
|
||||
m_actor = world.get_physics().createRigidDynamic(pose);
|
||||
|
||||
physx::PxShape* shape = world.get_physics().createShape(
|
||||
geometry, world.get_default_material(), true);
|
||||
m_actor->attachShape(*shape);
|
||||
shape->release();
|
||||
|
||||
physx::PxRigidBodyExt::updateMassAndInertia(*m_actor, density);
|
||||
world.get_scene().addActor(*m_actor);
|
||||
}
|
||||
|
||||
~PhysXRigidBody() override
|
||||
{
|
||||
m_world.get_scene().removeActor(*m_actor);
|
||||
m_actor->release();
|
||||
}
|
||||
|
||||
PhysXRigidBody(const PhysXRigidBody&) = delete;
|
||||
PhysXRigidBody& operator=(const PhysXRigidBody&) = delete;
|
||||
|
||||
// ── ColliderInterface ────────────────────────────────────────────────
|
||||
|
||||
/// Support function — delegates to the stored geometry type so the body
|
||||
/// can be used with omath GJK alongside the non-simulated colliders.
|
||||
[[nodiscard]]
|
||||
VectorType find_abs_furthest_vertex_position(const VectorType& direction) const override
|
||||
{
|
||||
const VectorType o = get_origin();
|
||||
switch (m_geometry.getType())
|
||||
{
|
||||
case physx::PxGeometryType::eBOX:
|
||||
{
|
||||
const auto& he = m_geometry.box().halfExtents;
|
||||
return {
|
||||
o.x + (direction.x >= 0.f ? he.x : -he.x),
|
||||
o.y + (direction.y >= 0.f ? he.y : -he.y),
|
||||
o.z + (direction.z >= 0.f ? he.z : -he.z),
|
||||
};
|
||||
}
|
||||
case physx::PxGeometryType::eSPHERE:
|
||||
{
|
||||
const float r = m_geometry.sphere().radius;
|
||||
const float len = std::sqrt(direction.x * direction.x +
|
||||
direction.y * direction.y +
|
||||
direction.z * direction.z);
|
||||
if (len == 0.f)
|
||||
return o;
|
||||
const float inv = r / len;
|
||||
return { o.x + direction.x * inv,
|
||||
o.y + direction.y * inv,
|
||||
o.z + direction.z * inv };
|
||||
}
|
||||
default:
|
||||
return o; // unsupported geometry — return centre
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
const VectorType& get_origin() const override
|
||||
{
|
||||
const auto& p = m_actor->getGlobalPose().p;
|
||||
m_cached_origin = { p.x, p.y, p.z };
|
||||
return m_cached_origin;
|
||||
}
|
||||
|
||||
void set_origin(const VectorType& new_origin) override
|
||||
{
|
||||
physx::PxTransform pose = m_actor->getGlobalPose();
|
||||
pose.p = physx::PxVec3(new_origin.x, new_origin.y, new_origin.z);
|
||||
m_actor->setGlobalPose(pose);
|
||||
}
|
||||
|
||||
// ── PhysX-specific API ───────────────────────────────────────────────
|
||||
|
||||
[[nodiscard]] physx::PxRigidDynamic& get_actor() { return *m_actor; }
|
||||
[[nodiscard]] const physx::PxRigidDynamic& get_actor() const { return *m_actor; }
|
||||
|
||||
void set_linear_velocity(const VectorType& v)
|
||||
{
|
||||
m_actor->setLinearVelocity(physx::PxVec3(v.x, v.y, v.z));
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
VectorType get_linear_velocity() const
|
||||
{
|
||||
const auto& v = m_actor->getLinearVelocity();
|
||||
return { v.x, v.y, v.z };
|
||||
}
|
||||
|
||||
void set_kinematic(bool enabled)
|
||||
{
|
||||
m_actor->setRigidBodyFlag(physx::PxRigidBodyFlag::eKINEMATIC, enabled);
|
||||
}
|
||||
|
||||
private:
|
||||
PhysXWorld& m_world;
|
||||
physx::PxGeometryHolder m_geometry;
|
||||
physx::PxRigidDynamic* m_actor{nullptr};
|
||||
mutable VectorType m_cached_origin{};
|
||||
};
|
||||
} // namespace omath::collision
|
||||
|
||||
#endif // OMATH_ENABLE_PHYSX
|
||||
64
include/omath/collision/physx_sphere_collider.hpp
Normal file
64
include/omath/collision/physx_sphere_collider.hpp
Normal file
@@ -0,0 +1,64 @@
|
||||
//
|
||||
// Created by orange-cpp
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#ifdef OMATH_ENABLE_PHYSX
|
||||
|
||||
#include "collider_interface.hpp"
|
||||
#include <PxPhysicsAPI.h>
|
||||
#include <cmath>
|
||||
|
||||
namespace omath::collision
|
||||
{
|
||||
/// Sphere collider backed by PhysX PxSphereGeometry.
|
||||
class PhysXSphereCollider final : public ColliderInterface<Vector3<float>>
|
||||
{
|
||||
public:
|
||||
/// @param radius Sphere radius (must be > 0).
|
||||
/// @param origin World-space centre of the sphere.
|
||||
explicit PhysXSphereCollider(float radius, const VectorType& origin = {})
|
||||
: m_geometry(radius)
|
||||
, m_origin(origin)
|
||||
{
|
||||
}
|
||||
|
||||
/// Support function: returns the world-space point on the sphere furthest in @p direction.
|
||||
/// For a sphere that is simply origin + normalize(direction) * radius.
|
||||
[[nodiscard]]
|
||||
VectorType find_abs_furthest_vertex_position(const VectorType& direction) const override
|
||||
{
|
||||
const float len = std::sqrt(direction.x * direction.x +
|
||||
direction.y * direction.y +
|
||||
direction.z * direction.z);
|
||||
if (len == 0.f)
|
||||
return m_origin;
|
||||
|
||||
const float inv = m_geometry.radius / len;
|
||||
return {
|
||||
m_origin.x + direction.x * inv,
|
||||
m_origin.y + direction.y * inv,
|
||||
m_origin.z + direction.z * inv,
|
||||
};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
const VectorType& get_origin() const override { return m_origin; }
|
||||
|
||||
void set_origin(const VectorType& new_origin) override { m_origin = new_origin; }
|
||||
|
||||
[[nodiscard]]
|
||||
const physx::PxSphereGeometry& get_geometry() const { return m_geometry; }
|
||||
|
||||
[[nodiscard]]
|
||||
float get_radius() const { return m_geometry.radius; }
|
||||
|
||||
void set_radius(float radius) { m_geometry = physx::PxSphereGeometry(radius); }
|
||||
|
||||
private:
|
||||
physx::PxSphereGeometry m_geometry;
|
||||
VectorType m_origin;
|
||||
};
|
||||
} // namespace omath::collision
|
||||
|
||||
#endif // OMATH_ENABLE_PHYSX
|
||||
82
include/omath/collision/physx_world.hpp
Normal file
82
include/omath/collision/physx_world.hpp
Normal file
@@ -0,0 +1,82 @@
|
||||
//
|
||||
// Created by orange-cpp
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#ifdef OMATH_ENABLE_PHYSX
|
||||
|
||||
#include <PxPhysicsAPI.h>
|
||||
|
||||
namespace omath::collision
|
||||
{
|
||||
/// RAII owner of a PhysX Foundation + Physics + Scene.
|
||||
/// One world per simulation context; not copyable or movable.
|
||||
class PhysXWorld final
|
||||
{
|
||||
public:
|
||||
explicit PhysXWorld(physx::PxVec3 gravity = {0.f, -9.81f, 0.f},
|
||||
physx::PxU32 cpu_threads = 2)
|
||||
{
|
||||
m_foundation = PxCreateFoundation(PX_PHYSICS_VERSION, m_allocator, m_error_callback);
|
||||
|
||||
m_physics = PxCreatePhysics(PX_PHYSICS_VERSION, *m_foundation,
|
||||
physx::PxTolerancesScale{});
|
||||
|
||||
physx::PxSceneDesc desc(m_physics->getTolerancesScale());
|
||||
desc.gravity = gravity;
|
||||
desc.cpuDispatcher = physx::PxDefaultCpuDispatcherCreate(cpu_threads);
|
||||
m_dispatcher = static_cast<physx::PxDefaultCpuDispatcher*>(desc.cpuDispatcher);
|
||||
desc.filterShader = physx::PxDefaultSimulationFilterShader;
|
||||
|
||||
m_scene = m_physics->createScene(desc);
|
||||
|
||||
// Default material: static friction 0.5, dynamic friction 0.5, restitution 0.
|
||||
m_default_material = m_physics->createMaterial(0.5f, 0.5f, 0.f);
|
||||
}
|
||||
|
||||
~PhysXWorld()
|
||||
{
|
||||
m_scene->release();
|
||||
m_dispatcher->release();
|
||||
m_default_material->release();
|
||||
m_physics->release();
|
||||
m_foundation->release();
|
||||
}
|
||||
|
||||
PhysXWorld(const PhysXWorld&) = delete;
|
||||
PhysXWorld& operator=(const PhysXWorld&) = delete;
|
||||
|
||||
/// Advance the simulation by @p dt seconds and block until results are ready.
|
||||
void step(float dt)
|
||||
{
|
||||
m_scene->simulate(dt);
|
||||
m_scene->fetchResults(true);
|
||||
}
|
||||
|
||||
[[nodiscard]] physx::PxPhysics& get_physics() { return *m_physics; }
|
||||
[[nodiscard]] physx::PxScene& get_scene() { return *m_scene; }
|
||||
[[nodiscard]] physx::PxMaterial& get_default_material() { return *m_default_material; }
|
||||
|
||||
/// Add an infinite static ground plane at y = @p y_level facing +Y.
|
||||
physx::PxRigidStatic* add_ground_plane(float y_level = 0.f)
|
||||
{
|
||||
physx::PxRigidStatic* plane = PxCreatePlane(
|
||||
*m_physics,
|
||||
physx::PxPlane(0.f, 1.f, 0.f, -y_level),
|
||||
*m_default_material);
|
||||
m_scene->addActor(*plane);
|
||||
return plane;
|
||||
}
|
||||
|
||||
private:
|
||||
physx::PxDefaultAllocator m_allocator{};
|
||||
physx::PxDefaultErrorCallback m_error_callback{};
|
||||
physx::PxFoundation* m_foundation{nullptr};
|
||||
physx::PxPhysics* m_physics{nullptr};
|
||||
physx::PxDefaultCpuDispatcher* m_dispatcher{nullptr};
|
||||
physx::PxScene* m_scene{nullptr};
|
||||
physx::PxMaterial* m_default_material{nullptr};
|
||||
};
|
||||
} // namespace omath::collision
|
||||
|
||||
#endif // OMATH_ENABLE_PHYSX
|
||||
@@ -62,20 +62,13 @@ namespace omath::detail
|
||||
return splitmix64(base_seed() + 0xD1B54A32D192ED03ull * (Stream + 1));
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
consteval std::uint64_t bounded_u64(const std::uint64_t x, const std::uint64_t bound)
|
||||
{
|
||||
return (x * bound) >> 64;
|
||||
}
|
||||
|
||||
template<std::int64_t Lo, std::int64_t Hi, std::uint64_t Stream>
|
||||
[[nodiscard]]
|
||||
consteval std::int64_t rand_uint8_t()
|
||||
{
|
||||
static_assert(Lo <= Hi);
|
||||
const std::uint64_t span = static_cast<std::uint64_t>(Hi - Lo) + 1ull;
|
||||
const std::uint64_t r = rand_u64<Stream>();
|
||||
return static_cast<std::int64_t>(bounded_u64(r, span)) + Lo;
|
||||
return static_cast<std::int64_t>(r) + Lo;
|
||||
}
|
||||
[[nodiscard]]
|
||||
consteval std::uint64_t rand_u64(const std::uint64_t seed, const std::uint64_t i)
|
||||
|
||||
219
include/omath/linear_algebra/quaternion.hpp
Normal file
219
include/omath/linear_algebra/quaternion.hpp
Normal file
@@ -0,0 +1,219 @@
|
||||
//
|
||||
// Created by vlad on 3/1/2026.
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#include "omath/linear_algebra/mat.hpp"
|
||||
#include "omath/linear_algebra/vector3.hpp"
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <format>
|
||||
|
||||
namespace omath
|
||||
{
|
||||
template<class Type>
|
||||
requires std::is_arithmetic_v<Type>
|
||||
class Quaternion
|
||||
{
|
||||
public:
|
||||
using ContainedType = Type;
|
||||
|
||||
Type x = static_cast<Type>(0);
|
||||
Type y = static_cast<Type>(0);
|
||||
Type z = static_cast<Type>(0);
|
||||
Type w = static_cast<Type>(1); // identity quaternion
|
||||
|
||||
constexpr Quaternion() noexcept = default;
|
||||
|
||||
constexpr Quaternion(const Type& x, const Type& y, const Type& z, const Type& w) noexcept
|
||||
: x(x), y(y), z(z), w(w)
|
||||
{
|
||||
}
|
||||
|
||||
// Factory: build from a normalized axis and an angle in radians
|
||||
[[nodiscard]]
|
||||
static Quaternion from_axis_angle(const Vector3<Type>& axis, const Type& angle_rad) noexcept
|
||||
{
|
||||
const Type half = angle_rad / static_cast<Type>(2);
|
||||
const Type s = std::sin(half);
|
||||
return {axis.x * s, axis.y * s, axis.z * s, std::cos(half)};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr bool operator==(const Quaternion& other) const noexcept
|
||||
{
|
||||
return x == other.x && y == other.y && z == other.z && w == other.w;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr bool operator!=(const Quaternion& other) const noexcept
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
// Hamilton product: this * other
|
||||
[[nodiscard]] constexpr Quaternion operator*(const Quaternion& other) const noexcept
|
||||
{
|
||||
return {
|
||||
w * other.x + x * other.w + y * other.z - z * other.y,
|
||||
w * other.y - x * other.z + y * other.w + z * other.x,
|
||||
w * other.z + x * other.y - y * other.x + z * other.w,
|
||||
w * other.w - x * other.x - y * other.y - z * other.z,
|
||||
};
|
||||
}
|
||||
|
||||
constexpr Quaternion& operator*=(const Quaternion& other) noexcept
|
||||
{
|
||||
return *this = *this * other;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Quaternion operator*(const Type& scalar) const noexcept
|
||||
{
|
||||
return {x * scalar, y * scalar, z * scalar, w * scalar};
|
||||
}
|
||||
|
||||
constexpr Quaternion& operator*=(const Type& scalar) noexcept
|
||||
{
|
||||
x *= scalar;
|
||||
y *= scalar;
|
||||
z *= scalar;
|
||||
w *= scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Quaternion operator+(const Quaternion& other) const noexcept
|
||||
{
|
||||
return {x + other.x, y + other.y, z + other.z, w + other.w};
|
||||
}
|
||||
|
||||
constexpr Quaternion& operator+=(const Quaternion& other) noexcept
|
||||
{
|
||||
x += other.x;
|
||||
y += other.y;
|
||||
z += other.z;
|
||||
w += other.w;
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Quaternion operator-() const noexcept
|
||||
{
|
||||
return {-x, -y, -z, -w};
|
||||
}
|
||||
|
||||
// Conjugate: negates the vector part (x, y, z)
|
||||
[[nodiscard]] constexpr Quaternion conjugate() const noexcept
|
||||
{
|
||||
return {-x, -y, -z, w};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Type dot(const Quaternion& other) const noexcept
|
||||
{
|
||||
return x * other.x + y * other.y + z * other.z + w * other.w;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Type length_sqr() const noexcept
|
||||
{
|
||||
return x * x + y * y + z * z + w * w;
|
||||
}
|
||||
|
||||
#ifndef _MSC_VER
|
||||
[[nodiscard]] constexpr Type length() const noexcept
|
||||
{
|
||||
return std::sqrt(length_sqr());
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Quaternion normalized() const noexcept
|
||||
{
|
||||
const Type len = length();
|
||||
return len != static_cast<Type>(0) ? *this * (static_cast<Type>(1) / len) : *this;
|
||||
}
|
||||
#else
|
||||
[[nodiscard]] Type length() const noexcept
|
||||
{
|
||||
return std::sqrt(length_sqr());
|
||||
}
|
||||
|
||||
[[nodiscard]] Quaternion normalized() const noexcept
|
||||
{
|
||||
const Type len = length();
|
||||
return len != static_cast<Type>(0) ? *this * (static_cast<Type>(1) / len) : *this;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Inverse: q* / |q|^2 (for unit quaternions inverse == conjugate)
|
||||
[[nodiscard]] constexpr Quaternion inverse() const noexcept
|
||||
{
|
||||
return conjugate() * (static_cast<Type>(1) / length_sqr());
|
||||
}
|
||||
|
||||
// Rotate a 3D vector: v' = q * pure(v) * q^-1
|
||||
// Computed via Rodrigues' formula to avoid full quaternion product overhead
|
||||
[[nodiscard]] constexpr Vector3<Type> rotate(const Vector3<Type>& v) const noexcept
|
||||
{
|
||||
const Vector3<Type> q_vec{x, y, z};
|
||||
const Vector3<Type> cross = q_vec.cross(v);
|
||||
return v + cross * (static_cast<Type>(2) * w) + q_vec.cross(cross) * static_cast<Type>(2);
|
||||
}
|
||||
|
||||
// 3x3 rotation matrix from this (unit) quaternion
|
||||
[[nodiscard]] constexpr Mat<3, 3, Type> to_rotation_matrix3() const noexcept
|
||||
{
|
||||
const Type xx = x * x, yy = y * y, zz = z * z;
|
||||
const Type xy = x * y, xz = x * z, yz = y * z;
|
||||
const Type wx = w * x, wy = w * y, wz = w * z;
|
||||
const Type one = static_cast<Type>(1);
|
||||
const Type two = static_cast<Type>(2);
|
||||
|
||||
return {
|
||||
{one - two * (yy + zz), two * (xy - wz), two * (xz + wy) },
|
||||
{two * (xy + wz), one - two * (xx + zz), two * (yz - wx) },
|
||||
{two * (xz - wy), two * (yz + wx), one - two * (xx + yy)},
|
||||
};
|
||||
}
|
||||
|
||||
// 4x4 rotation matrix (with homogeneous row/column)
|
||||
[[nodiscard]] constexpr Mat<4, 4, Type> to_rotation_matrix4() const noexcept
|
||||
{
|
||||
const Type xx = x * x, yy = y * y, zz = z * z;
|
||||
const Type xy = x * y, xz = x * z, yz = y * z;
|
||||
const Type wx = w * x, wy = w * y, wz = w * z;
|
||||
const Type one = static_cast<Type>(1);
|
||||
const Type two = static_cast<Type>(2);
|
||||
const Type zero = static_cast<Type>(0);
|
||||
|
||||
return {
|
||||
{one - two * (yy + zz), two * (xy - wz), two * (xz + wy), zero},
|
||||
{two * (xy + wz), one - two * (xx + zz), two * (yz - wx), zero},
|
||||
{two * (xz - wy), two * (yz + wx), one - two * (xx + yy), zero},
|
||||
{zero, zero, zero, one },
|
||||
};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr std::array<Type, 4> as_array() const noexcept
|
||||
{
|
||||
return {x, y, z, w};
|
||||
}
|
||||
};
|
||||
} // namespace omath
|
||||
|
||||
template<class Type>
|
||||
struct std::formatter<omath::Quaternion<Type>> // NOLINT(*-dcl58-cpp)
|
||||
{
|
||||
[[nodiscard]]
|
||||
static constexpr auto parse(std::format_parse_context& ctx)
|
||||
{
|
||||
return ctx.begin();
|
||||
}
|
||||
|
||||
template<class FormatContext>
|
||||
[[nodiscard]]
|
||||
static auto format(const omath::Quaternion<Type>& q, FormatContext& ctx)
|
||||
{
|
||||
if constexpr (std::is_same_v<typename FormatContext::char_type, char>)
|
||||
return std::format_to(ctx.out(), "[{}, {}, {}, {}]", q.x, q.y, q.z, q.w);
|
||||
|
||||
if constexpr (std::is_same_v<typename FormatContext::char_type, wchar_t>)
|
||||
return std::format_to(ctx.out(), L"[{}, {}, {}, {}]", q.x, q.y, q.z, q.w);
|
||||
|
||||
if constexpr (std::is_same_v<typename FormatContext::char_type, char8_t>)
|
||||
return std::format_to(ctx.out(), u8"[{}, {}, {}, {}]", q.x, q.y, q.z, q.w);
|
||||
}
|
||||
};
|
||||
25
include/omath/lua/lua.hpp
Normal file
25
include/omath/lua/lua.hpp
Normal file
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// Created by orange on 07.03.2026.
|
||||
//
|
||||
#pragma once
|
||||
#ifdef OMATH_ENABLE_LUA
|
||||
#include <sol/forward.hpp>
|
||||
namespace omath::lua
|
||||
{
|
||||
class LuaInterpreter final
|
||||
{
|
||||
public:
|
||||
static void register_lib(lua_State* lua_state);
|
||||
|
||||
private:
|
||||
static void register_vec2(sol::table& omath_table);
|
||||
static void register_vec3(sol::table& omath_table);
|
||||
static void register_vec4(sol::table& omath_table);
|
||||
static void register_color(sol::table& omath_table);
|
||||
static void register_triangle(sol::table& omath_table);
|
||||
static void register_shared_types(sol::table& omath_table);
|
||||
static void register_engines(sol::table& omath_table);
|
||||
static void register_pattern_scan(sol::table& omath_table);
|
||||
};
|
||||
}
|
||||
#endif
|
||||
@@ -17,6 +17,9 @@
|
||||
// Matrix classes
|
||||
#include "omath/linear_algebra/mat.hpp"
|
||||
|
||||
// Quaternion
|
||||
#include "omath/linear_algebra/quaternion.hpp"
|
||||
|
||||
// Color functionality
|
||||
#include "omath/utility/color.hpp"
|
||||
|
||||
|
||||
@@ -6,7 +6,9 @@
|
||||
|
||||
#include "omath/linear_algebra/vector3.hpp"
|
||||
#include <expected>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace omath::pathfinding
|
||||
@@ -28,10 +30,20 @@ namespace omath::pathfinding
|
||||
[[nodiscard]]
|
||||
bool empty() const;
|
||||
|
||||
[[nodiscard]] std::vector<uint8_t> serialize() const noexcept;
|
||||
// Events -- per-vertex optional tag (e.g. "jump", "teleport")
|
||||
void set_event(const Vector3<float>& vertex, const std::string_view& event_id);
|
||||
void clear_event(const Vector3<float>& vertex);
|
||||
|
||||
void deserialize(const std::vector<uint8_t>& raw) noexcept;
|
||||
[[nodiscard]]
|
||||
std::optional<std::string> get_event(const Vector3<float>& vertex) const noexcept;
|
||||
|
||||
[[nodiscard]] std::string serialize() const noexcept;
|
||||
|
||||
void deserialize(const std::string& raw);
|
||||
|
||||
std::unordered_map<Vector3<float>, std::vector<Vector3<float>>> m_vertex_map;
|
||||
|
||||
private:
|
||||
std::unordered_map<Vector3<float>, std::string> m_vertex_events;
|
||||
};
|
||||
} // namespace omath::pathfinding
|
||||
|
||||
@@ -16,19 +16,28 @@ namespace omath
|
||||
float value{};
|
||||
};
|
||||
|
||||
class Color final : public Vector4<float>
|
||||
class Color final
|
||||
{
|
||||
Vector4<float> m_value;
|
||||
public:
|
||||
constexpr Color(const float r, const float g, const float b, const float a) noexcept: Vector4(r, g, b, a)
|
||||
constexpr const Vector4<float>& value() const
|
||||
{
|
||||
clamp(0.f, 1.f);
|
||||
return m_value;
|
||||
}
|
||||
constexpr Color(const float r, const float g, const float b, const float a) noexcept: m_value(r, g, b, a)
|
||||
{
|
||||
m_value.clamp(0.f, 1.f);
|
||||
}
|
||||
|
||||
constexpr explicit Color(const Vector4<float>& value) : m_value(value)
|
||||
{
|
||||
m_value.clamp(0.f, 1.f);
|
||||
}
|
||||
constexpr explicit Color() noexcept = default;
|
||||
[[nodiscard]]
|
||||
constexpr static Color from_rgba(const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a) noexcept
|
||||
{
|
||||
return Color{Vector4(r, g, b, a) / 255.f};
|
||||
return Color(Vector4<float>(r, g, b, a) / 255.f);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
@@ -82,9 +91,9 @@ namespace omath
|
||||
{
|
||||
Hsv hsv_data;
|
||||
|
||||
const float& red = x;
|
||||
const float& green = y;
|
||||
const float& blue = z;
|
||||
const float& red = m_value.x;
|
||||
const float& green = m_value.y;
|
||||
const float& blue = m_value.z;
|
||||
|
||||
const float max = std::max({red, green, blue});
|
||||
const float min = std::min({red, green, blue});
|
||||
@@ -109,11 +118,6 @@ namespace omath
|
||||
|
||||
return hsv_data;
|
||||
}
|
||||
|
||||
constexpr explicit Color(const Vector4& vec) noexcept: Vector4(vec)
|
||||
{
|
||||
clamp(0.f, 1.f);
|
||||
}
|
||||
constexpr void set_hue(const float hue) noexcept
|
||||
{
|
||||
auto hsv = to_hsv();
|
||||
@@ -141,7 +145,7 @@ namespace omath
|
||||
constexpr Color blend(const Color& other, float ratio) const noexcept
|
||||
{
|
||||
ratio = std::clamp(ratio, 0.f, 1.f);
|
||||
return Color(*this * (1.f - ratio) + other * ratio);
|
||||
return Color(this->m_value * (1.f - ratio) + other.m_value * ratio);
|
||||
}
|
||||
|
||||
[[nodiscard]] static constexpr Color red()
|
||||
@@ -160,16 +164,26 @@ namespace omath
|
||||
[[nodiscard]]
|
||||
ImColor to_im_color() const noexcept
|
||||
{
|
||||
return {to_im_vec4()};
|
||||
return {m_value.to_im_vec4()};
|
||||
}
|
||||
#endif
|
||||
[[nodiscard]] std::string to_string() const noexcept
|
||||
{
|
||||
return std::format("[r:{}, g:{}, b:{}, a:{}]",
|
||||
static_cast<int>(x * 255.f),
|
||||
static_cast<int>(y * 255.f),
|
||||
static_cast<int>(z * 255.f),
|
||||
static_cast<int>(w * 255.f));
|
||||
static_cast<int>(m_value.x * 255.f),
|
||||
static_cast<int>(m_value.y * 255.f),
|
||||
static_cast<int>(m_value.z * 255.f),
|
||||
static_cast<int>(m_value.w * 255.f));
|
||||
}
|
||||
[[nodiscard]] std::string to_rgbf_string() const noexcept
|
||||
{
|
||||
return std::format("[r:{}, g:{}, b:{}, a:{}]",
|
||||
m_value.x, m_value.y, m_value.z, m_value.w);
|
||||
}
|
||||
[[nodiscard]] std::string to_hsv_string() const noexcept
|
||||
{
|
||||
const auto [hue, saturation, value] = to_hsv();
|
||||
return std::format("[h:{}, s:{}, v:{}]", hue, saturation, value);
|
||||
}
|
||||
[[nodiscard]] std::wstring to_wstring() const noexcept
|
||||
{
|
||||
@@ -188,23 +202,55 @@ namespace omath
|
||||
template<>
|
||||
struct std::formatter<omath::Color> // NOLINT(*-dcl58-cpp)
|
||||
{
|
||||
[[nodiscard]]
|
||||
static constexpr auto parse(const std::format_parse_context& ctx)
|
||||
enum class ColorFormat { rgb, rgbf, hsv };
|
||||
ColorFormat color_format = ColorFormat::rgb;
|
||||
|
||||
constexpr auto parse(std::format_parse_context& ctx)
|
||||
{
|
||||
return ctx.begin();
|
||||
const auto it = ctx.begin();
|
||||
const auto end = ctx.end();
|
||||
|
||||
if (it == end || *it == '}')
|
||||
return it;
|
||||
|
||||
const std::string_view spec(it, end);
|
||||
|
||||
if (spec.starts_with("rgbf"))
|
||||
{
|
||||
color_format = ColorFormat::rgbf;
|
||||
return it + 4;
|
||||
}
|
||||
if (spec.starts_with("rgb"))
|
||||
{
|
||||
color_format = ColorFormat::rgb;
|
||||
return it + 3;
|
||||
}
|
||||
if (spec.starts_with("hsv"))
|
||||
{
|
||||
color_format = ColorFormat::hsv;
|
||||
return it + 3;
|
||||
}
|
||||
|
||||
throw std::format_error("Invalid format specifier for omath::Color. Use rgb, rgbf, or hsv.");
|
||||
}
|
||||
|
||||
template<class FormatContext>
|
||||
[[nodiscard]]
|
||||
static auto format(const omath::Color& col, FormatContext& ctx)
|
||||
auto format(const omath::Color& col, FormatContext& ctx) const
|
||||
{
|
||||
if constexpr (std::is_same_v<typename FormatContext::char_type, char>)
|
||||
return std::format_to(ctx.out(), "{}", col.to_string());
|
||||
if constexpr (std::is_same_v<typename FormatContext::char_type, wchar_t>)
|
||||
return std::format_to(ctx.out(), L"{}", col.to_wstring());
|
||||
std::string str;
|
||||
switch (color_format)
|
||||
{
|
||||
case ColorFormat::rgb: str = col.to_string(); break;
|
||||
case ColorFormat::rgbf: str = col.to_rgbf_string(); break;
|
||||
case ColorFormat::hsv: str = col.to_hsv_string(); break;
|
||||
}
|
||||
|
||||
if constexpr (std::is_same_v<typename FormatContext::char_type, char>)
|
||||
return std::format_to(ctx.out(), "{}", str);
|
||||
if constexpr (std::is_same_v<typename FormatContext::char_type, wchar_t>)
|
||||
return std::format_to(ctx.out(), L"{}", std::wstring(str.cbegin(), str.cend()));
|
||||
if constexpr (std::is_same_v<typename FormatContext::char_type, char8_t>)
|
||||
return std::format_to(ctx.out(), u8"{}", col.to_u8string());
|
||||
return std::format_to(ctx.out(), u8"{}", std::u8string(str.cbegin(), str.cend()));
|
||||
|
||||
std::unreachable();
|
||||
}
|
||||
|
||||
27
source/lua/lua.cpp
Normal file
27
source/lua/lua.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// Created by orange on 07.03.2026.
|
||||
//
|
||||
#ifdef OMATH_ENABLE_LUA
|
||||
#include "lua.hpp"
|
||||
#include <sol/sol.hpp>
|
||||
#include "omath/lua/lua.hpp"
|
||||
|
||||
namespace omath::lua
|
||||
{
|
||||
void LuaInterpreter::register_lib(lua_State* lua_state)
|
||||
{
|
||||
sol::state_view lua(lua_state);
|
||||
|
||||
auto omath_table = lua["omath"].get_or_create<sol::table>();
|
||||
|
||||
register_vec2(omath_table);
|
||||
register_vec3(omath_table);
|
||||
register_vec4(omath_table);
|
||||
register_color(omath_table);
|
||||
register_triangle(omath_table);
|
||||
register_shared_types(omath_table);
|
||||
register_engines(omath_table);
|
||||
register_pattern_scan(omath_table);
|
||||
}
|
||||
} // namespace omath::lua
|
||||
#endif
|
||||
46
source/lua/lua_color.cpp
Normal file
46
source/lua/lua_color.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
//
|
||||
// Created by orange on 07.03.2026.
|
||||
//
|
||||
#ifdef OMATH_ENABLE_LUA
|
||||
#include "omath/lua/lua.hpp"
|
||||
#include <sol/sol.hpp>
|
||||
#include <omath/utility/color.hpp>
|
||||
|
||||
namespace omath::lua
|
||||
{
|
||||
void LuaInterpreter::register_color(sol::table& omath_table)
|
||||
{
|
||||
omath_table.new_usertype<omath::Color>(
|
||||
"Color",
|
||||
sol::factories([](float r, float g, float b, float a) { return omath::Color(r, g, b, a); },
|
||||
[]() { return omath::Color(); }),
|
||||
|
||||
"from_rgba", [](uint8_t r, uint8_t g, uint8_t b, uint8_t a)
|
||||
{ return omath::Color::from_rgba(r, g, b, a); }, "from_hsv",
|
||||
sol::overload([](float h, float s, float v) { return omath::Color::from_hsv(h, s, v); },
|
||||
[](const omath::Hsv& hsv) { return omath::Color::from_hsv(hsv); }),
|
||||
"red", []() { return omath::Color::red(); }, "green", []() { return omath::Color::green(); }, "blue",
|
||||
[]() { return omath::Color::blue(); },
|
||||
|
||||
"r", sol::property([](const omath::Color& c) { return c.value().x; }), "g",
|
||||
sol::property([](const omath::Color& c) { return c.value().y; }), "b",
|
||||
sol::property([](const omath::Color& c) { return c.value().z; }), "a",
|
||||
sol::property([](const omath::Color& c) { return c.value().w; }),
|
||||
|
||||
"to_hsv", &omath::Color::to_hsv, "set_hue", &omath::Color::set_hue, "set_saturation",
|
||||
&omath::Color::set_saturation, "set_value", &omath::Color::set_value, "blend", &omath::Color::blend,
|
||||
|
||||
sol::meta_function::to_string, &omath::Color::to_string);
|
||||
|
||||
omath_table.new_usertype<omath::Hsv>(
|
||||
"Hsv", sol::constructors<omath::Hsv()>(), "hue",
|
||||
sol::property([](const omath::Hsv& h) { return h.hue; }, [](omath::Hsv& h, float val) { h.hue = val; }),
|
||||
"saturation",
|
||||
sol::property([](const omath::Hsv& h) { return h.saturation; },
|
||||
[](omath::Hsv& h, float val) { h.saturation = val; }),
|
||||
"value",
|
||||
sol::property([](const omath::Hsv& h) { return h.value; },
|
||||
[](omath::Hsv& h, float val) { h.value = val; }));
|
||||
}
|
||||
} // namespace omath::lua::detail
|
||||
#endif
|
||||
227
source/lua/lua_engines.cpp
Normal file
227
source/lua/lua_engines.cpp
Normal file
@@ -0,0 +1,227 @@
|
||||
//
|
||||
// Created by orange on 07.03.2026.
|
||||
//
|
||||
#ifdef OMATH_ENABLE_LUA
|
||||
#include "omath/lua/lua.hpp"
|
||||
#include <omath/engines/cry_engine/camera.hpp>
|
||||
#include <omath/engines/frostbite_engine/camera.hpp>
|
||||
#include <omath/engines/iw_engine/camera.hpp>
|
||||
#include <omath/engines/opengl_engine/camera.hpp>
|
||||
#include <omath/engines/source_engine/camera.hpp>
|
||||
#include <omath/engines/unity_engine/camera.hpp>
|
||||
#include <omath/engines/unreal_engine/camera.hpp>
|
||||
#include <sol/sol.hpp>
|
||||
#include <string_view>
|
||||
|
||||
namespace
|
||||
{
|
||||
// ---- Canonical shared C++ type aliases ----------------------------------
|
||||
// Each unique template instantiation must be registered exactly once.
|
||||
|
||||
using PitchAngle90 = omath::Angle<float, -90.f, 90.f, omath::AngleFlags::Clamped>;
|
||||
using PitchAngle89 = omath::Angle<float, -89.f, 89.f, omath::AngleFlags::Clamped>;
|
||||
using SharedYawRoll = omath::Angle<float, -180.f, 180.f, omath::AngleFlags::Normalized>;
|
||||
using SharedFoV = omath::Angle<float, 0.f, 180.f, omath::AngleFlags::Clamped>;
|
||||
using ViewAngles90 = omath::ViewAngles<PitchAngle90, SharedYawRoll, SharedYawRoll>;
|
||||
using ViewAngles89 = omath::ViewAngles<PitchAngle89, SharedYawRoll, SharedYawRoll>;
|
||||
|
||||
std::string projection_error_to_string(omath::projection::Error e)
|
||||
{
|
||||
switch (e)
|
||||
{
|
||||
case omath::projection::Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS:
|
||||
return "world position is out of screen bounds";
|
||||
case omath::projection::Error::INV_VIEW_PROJ_MAT_DET_EQ_ZERO:
|
||||
return "inverse view-projection matrix determinant is zero";
|
||||
}
|
||||
return "unknown error";
|
||||
}
|
||||
|
||||
template<class AngleType>
|
||||
void register_angle(sol::table& table, const char* name)
|
||||
{
|
||||
table.new_usertype<AngleType>(
|
||||
name, sol::no_constructor, "from_degrees", &AngleType::from_degrees, "from_radians",
|
||||
&AngleType::from_radians, "as_degrees", &AngleType::as_degrees, "as_radians", &AngleType::as_radians,
|
||||
"sin", &AngleType::sin, "cos", &AngleType::cos, "tan", &AngleType::tan, "cot", &AngleType::cot,
|
||||
sol::meta_function::addition, [](const AngleType& a, const AngleType& b)
|
||||
{ return AngleType::from_degrees(a.as_degrees() + b.as_degrees()); }, sol::meta_function::subtraction,
|
||||
[](const AngleType& a, const AngleType& b)
|
||||
{ return AngleType::from_degrees(a.as_degrees() - b.as_degrees()); }, sol::meta_function::unary_minus,
|
||||
[](const AngleType& a) { return AngleType::from_degrees(-a.as_degrees()); },
|
||||
sol::meta_function::equal_to, [](const AngleType& a, const AngleType& b) { return a == b; },
|
||||
sol::meta_function::to_string, [](const AngleType& a) { return std::format("{}deg", a.as_degrees()); });
|
||||
}
|
||||
|
||||
// Set aliases in an engine subtable pointing to the already-registered shared types
|
||||
template<class PitchAngleType, class ViewAnglesType>
|
||||
void set_engine_aliases(sol::table& engine_table, sol::table& types)
|
||||
{
|
||||
if constexpr (std::is_same_v<PitchAngleType, PitchAngle90>)
|
||||
engine_table["PitchAngle"] = types["PitchAngle90"];
|
||||
else
|
||||
engine_table["PitchAngle"] = types["PitchAngle89"];
|
||||
|
||||
engine_table["YawAngle"] = types["YawRoll"];
|
||||
engine_table["RollAngle"] = types["YawRoll"];
|
||||
engine_table["FieldOfView"] = types["FieldOfView"];
|
||||
engine_table["ViewPort"] = types["ViewPort"];
|
||||
|
||||
if constexpr (std::is_same_v<ViewAnglesType, ViewAngles90>)
|
||||
engine_table["ViewAngles"] = types["ViewAngles90"];
|
||||
else
|
||||
engine_table["ViewAngles"] = types["ViewAngles89"];
|
||||
}
|
||||
|
||||
// Register an engine: alias shared types, register unique Camera
|
||||
template<class EngineTraits>
|
||||
void register_engine(sol::table& omath_table, const char* subtable_name)
|
||||
{
|
||||
using PitchAngle = typename EngineTraits::PitchAngle;
|
||||
using ViewAngles = typename EngineTraits::ViewAngles;
|
||||
using Camera = typename EngineTraits::Camera;
|
||||
|
||||
auto engine_table = omath_table[subtable_name].get_or_create<sol::table>();
|
||||
auto types = omath_table["_types"].get<sol::table>();
|
||||
|
||||
set_engine_aliases<PitchAngle, ViewAngles>(engine_table, types);
|
||||
|
||||
engine_table.new_usertype<Camera>(
|
||||
"Camera",
|
||||
sol::constructors<Camera(const omath::Vector3<float>&, const ViewAngles&,
|
||||
const omath::projection::ViewPort&, const omath::projection::FieldOfView&,
|
||||
float, float)>(),
|
||||
"look_at", &Camera::look_at, "get_forward", &Camera::get_forward, "get_right", &Camera::get_right,
|
||||
"get_up", &Camera::get_up, "get_origin", &Camera::get_origin, "get_view_angles",
|
||||
&Camera::get_view_angles, "get_near_plane", &Camera::get_near_plane, "get_far_plane",
|
||||
&Camera::get_far_plane, "get_field_of_view", &Camera::get_field_of_view, "set_origin",
|
||||
&Camera::set_origin, "set_view_angles", &Camera::set_view_angles, "set_view_port",
|
||||
&Camera::set_view_port, "set_field_of_view", &Camera::set_field_of_view, "set_near_plane",
|
||||
&Camera::set_near_plane, "set_far_plane", &Camera::set_far_plane,
|
||||
|
||||
"world_to_screen",
|
||||
[](const Camera& cam, const omath::Vector3<float>& pos)
|
||||
-> std::tuple<sol::optional<omath::Vector3<float>>, sol::optional<std::string>>
|
||||
{
|
||||
auto result = cam.world_to_screen(pos);
|
||||
if (result)
|
||||
return {*result, sol::nullopt};
|
||||
return {sol::nullopt, projection_error_to_string(result.error())};
|
||||
},
|
||||
|
||||
"screen_to_world",
|
||||
[](const Camera& cam, const omath::Vector3<float>& pos)
|
||||
-> std::tuple<sol::optional<omath::Vector3<float>>, sol::optional<std::string>>
|
||||
{
|
||||
auto result = cam.screen_to_world(pos);
|
||||
if (result)
|
||||
return {*result, sol::nullopt};
|
||||
return {sol::nullopt, projection_error_to_string(result.error())};
|
||||
});
|
||||
}
|
||||
|
||||
// ---- Engine trait structs -----------------------------------------------
|
||||
|
||||
struct OpenGLEngineTraits
|
||||
{
|
||||
using PitchAngle = omath::opengl_engine::PitchAngle;
|
||||
using ViewAngles = omath::opengl_engine::ViewAngles;
|
||||
using Camera = omath::opengl_engine::Camera;
|
||||
};
|
||||
struct FrostbiteEngineTraits
|
||||
{
|
||||
using PitchAngle = omath::frostbite_engine::PitchAngle;
|
||||
using ViewAngles = omath::frostbite_engine::ViewAngles;
|
||||
using Camera = omath::frostbite_engine::Camera;
|
||||
};
|
||||
struct IWEngineTraits
|
||||
{
|
||||
using PitchAngle = omath::iw_engine::PitchAngle;
|
||||
using ViewAngles = omath::iw_engine::ViewAngles;
|
||||
using Camera = omath::iw_engine::Camera;
|
||||
};
|
||||
struct SourceEngineTraits
|
||||
{
|
||||
using PitchAngle = omath::source_engine::PitchAngle;
|
||||
using ViewAngles = omath::source_engine::ViewAngles;
|
||||
using Camera = omath::source_engine::Camera;
|
||||
};
|
||||
struct UnityEngineTraits
|
||||
{
|
||||
using PitchAngle = omath::unity_engine::PitchAngle;
|
||||
using ViewAngles = omath::unity_engine::ViewAngles;
|
||||
using Camera = omath::unity_engine::Camera;
|
||||
};
|
||||
struct UnrealEngineTraits
|
||||
{
|
||||
using PitchAngle = omath::unreal_engine::PitchAngle;
|
||||
using ViewAngles = omath::unreal_engine::ViewAngles;
|
||||
using Camera = omath::unreal_engine::Camera;
|
||||
};
|
||||
struct CryEngineTraits
|
||||
{
|
||||
using PitchAngle = omath::cry_engine::PitchAngle;
|
||||
using ViewAngles = omath::cry_engine::ViewAngles;
|
||||
using Camera = omath::cry_engine::Camera;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
namespace omath::lua
|
||||
{
|
||||
void LuaInterpreter::register_shared_types(sol::table& omath_table)
|
||||
{
|
||||
auto t = omath_table["_types"].get_or_create<sol::table>();
|
||||
|
||||
register_angle<PitchAngle90>(t, "PitchAngle90");
|
||||
register_angle<PitchAngle89>(t, "PitchAngle89");
|
||||
register_angle<SharedYawRoll>(t, "YawRoll");
|
||||
register_angle<SharedFoV>(t, "FieldOfView");
|
||||
|
||||
t.new_usertype<omath::projection::ViewPort>(
|
||||
"ViewPort", sol::factories([](float w, float h) { return omath::projection::ViewPort{w, h}; }), "width",
|
||||
sol::property([](const omath::projection::ViewPort& vp) { return vp.m_width; },
|
||||
[](omath::projection::ViewPort& vp, float val) { vp.m_width = val; }),
|
||||
"height",
|
||||
sol::property([](const omath::projection::ViewPort& vp) { return vp.m_height; },
|
||||
[](omath::projection::ViewPort& vp, float val) { vp.m_height = val; }),
|
||||
"aspect_ratio", &omath::projection::ViewPort::aspect_ratio);
|
||||
|
||||
t.new_usertype<ViewAngles90>(
|
||||
"ViewAngles90",
|
||||
sol::factories([](PitchAngle90 p, SharedYawRoll y, SharedYawRoll r) { return ViewAngles90{p, y, r}; }),
|
||||
"pitch",
|
||||
sol::property([](const ViewAngles90& va) { return va.pitch; },
|
||||
[](ViewAngles90& va, const PitchAngle90& val) { va.pitch = val; }),
|
||||
"yaw",
|
||||
sol::property([](const ViewAngles90& va) { return va.yaw; },
|
||||
[](ViewAngles90& va, const SharedYawRoll& val) { va.yaw = val; }),
|
||||
"roll",
|
||||
sol::property([](const ViewAngles90& va) { return va.roll; },
|
||||
[](ViewAngles90& va, const SharedYawRoll& val) { va.roll = val; }));
|
||||
|
||||
t.new_usertype<ViewAngles89>(
|
||||
"ViewAngles89",
|
||||
sol::factories([](PitchAngle89 p, SharedYawRoll y, SharedYawRoll r) { return ViewAngles89{p, y, r}; }),
|
||||
"pitch",
|
||||
sol::property([](const ViewAngles89& va) { return va.pitch; },
|
||||
[](ViewAngles89& va, const PitchAngle89& val) { va.pitch = val; }),
|
||||
"yaw",
|
||||
sol::property([](const ViewAngles89& va) { return va.yaw; },
|
||||
[](ViewAngles89& va, const SharedYawRoll& val) { va.yaw = val; }),
|
||||
"roll",
|
||||
sol::property([](const ViewAngles89& va) { return va.roll; },
|
||||
[](ViewAngles89& va, const SharedYawRoll& val) { va.roll = val; }));
|
||||
}
|
||||
|
||||
void LuaInterpreter::register_engines(sol::table& omath_table)
|
||||
{
|
||||
register_engine<OpenGLEngineTraits>(omath_table, "opengl");
|
||||
register_engine<FrostbiteEngineTraits>(omath_table, "frostbite");
|
||||
register_engine<IWEngineTraits>(omath_table, "iw");
|
||||
register_engine<SourceEngineTraits>(omath_table, "source");
|
||||
register_engine<UnityEngineTraits>(omath_table, "unity");
|
||||
register_engine<UnrealEngineTraits>(omath_table, "unreal");
|
||||
register_engine<CryEngineTraits>(omath_table, "cry");
|
||||
}
|
||||
} // namespace omath::lua::detail
|
||||
#endif
|
||||
104
source/lua/lua_pattern_scan.cpp
Normal file
104
source/lua/lua_pattern_scan.cpp
Normal file
@@ -0,0 +1,104 @@
|
||||
//
|
||||
// Created by orange on 10.03.2026.
|
||||
//
|
||||
#ifdef OMATH_ENABLE_LUA
|
||||
#include "omath/lua/lua.hpp"
|
||||
#include <format>
|
||||
#include <omath/utility/elf_pattern_scan.hpp>
|
||||
#include <omath/utility/macho_pattern_scan.hpp>
|
||||
#include <omath/utility/pattern_scan.hpp>
|
||||
#include <omath/utility/pe_pattern_scan.hpp>
|
||||
#include <omath/utility/section_scan_result.hpp>
|
||||
#include <sol/sol.hpp>
|
||||
|
||||
namespace omath::lua
|
||||
{
|
||||
void LuaInterpreter::register_pattern_scan(sol::table& omath_table)
|
||||
{
|
||||
omath_table.new_usertype<SectionScanResult>(
|
||||
"SectionScanResult", sol::no_constructor,
|
||||
"virtual_base_addr",
|
||||
sol::property([](const SectionScanResult& r) { return r.virtual_base_addr; }),
|
||||
"raw_base_addr",
|
||||
sol::property([](const SectionScanResult& r) { return r.raw_base_addr; }),
|
||||
"target_offset",
|
||||
sol::property([](const SectionScanResult& r) { return r.target_offset; }),
|
||||
sol::meta_function::to_string,
|
||||
[](const SectionScanResult& r)
|
||||
{
|
||||
return std::format("SectionScanResult(vbase=0x{:X}, raw_base=0x{:X}, offset={})",
|
||||
r.virtual_base_addr, r.raw_base_addr, r.target_offset);
|
||||
});
|
||||
|
||||
// Generic scanner: accepts a Lua string as a byte buffer
|
||||
auto ps_table = omath_table["PatternScanner"].get_or_create<sol::table>();
|
||||
ps_table["scan"] = [](const std::string& data, const std::string& pattern) -> sol::optional<std::ptrdiff_t>
|
||||
{
|
||||
const auto* begin = reinterpret_cast<const std::byte*>(data.data());
|
||||
const auto* end = begin + data.size();
|
||||
const auto* result = PatternScanner::scan_for_pattern(begin, end, pattern);
|
||||
if (result == end)
|
||||
return sol::nullopt;
|
||||
return std::distance(begin, result);
|
||||
};
|
||||
|
||||
auto pe_table = omath_table["PePatternScanner"].get_or_create<sol::table>();
|
||||
pe_table["scan_in_module"] = [](std::uintptr_t base_addr, const std::string& pattern,
|
||||
sol::optional<std::string> section) -> sol::optional<std::uintptr_t>
|
||||
{
|
||||
auto result = PePatternScanner::scan_for_pattern_in_loaded_module(reinterpret_cast<const void*>(base_addr),
|
||||
pattern, section.value_or(".text"));
|
||||
if (!result)
|
||||
return sol::nullopt;
|
||||
return *result;
|
||||
};
|
||||
pe_table["scan_in_file"] = [](const std::string& path, const std::string& pattern,
|
||||
sol::optional<std::string> section) -> sol::optional<SectionScanResult>
|
||||
{
|
||||
auto result = PePatternScanner::scan_for_pattern_in_file(std::filesystem::path(path), pattern,
|
||||
section.value_or(".text"));
|
||||
if (!result)
|
||||
return sol::nullopt;
|
||||
return *result;
|
||||
};
|
||||
auto elf_table = omath_table["ElfPatternScanner"].get_or_create<sol::table>();
|
||||
elf_table["scan_in_module"] = [](std::uintptr_t base_addr, const std::string& pattern,
|
||||
sol::optional<std::string> section) -> sol::optional<std::uintptr_t>
|
||||
{
|
||||
auto result = ElfPatternScanner::scan_for_pattern_in_loaded_module(reinterpret_cast<const void*>(base_addr),
|
||||
pattern, section.value_or(".text"));
|
||||
if (!result)
|
||||
return sol::nullopt;
|
||||
return *result;
|
||||
};
|
||||
elf_table["scan_in_file"] = [](const std::string& path, const std::string& pattern,
|
||||
sol::optional<std::string> section) -> sol::optional<SectionScanResult>
|
||||
{
|
||||
auto result = ElfPatternScanner::scan_for_pattern_in_file(std::filesystem::path(path), pattern,
|
||||
section.value_or(".text"));
|
||||
if (!result)
|
||||
return sol::nullopt;
|
||||
return *result;
|
||||
};
|
||||
auto macho_table = omath_table["MachOPatternScanner"].get_or_create<sol::table>();
|
||||
macho_table["scan_in_module"] = [](std::uintptr_t base_addr, const std::string& pattern,
|
||||
sol::optional<std::string> section) -> sol::optional<std::uintptr_t>
|
||||
{
|
||||
auto result = MachOPatternScanner::scan_for_pattern_in_loaded_module(
|
||||
reinterpret_cast<const void*>(base_addr), pattern, section.value_or("__text"));
|
||||
if (!result)
|
||||
return sol::nullopt;
|
||||
return *result;
|
||||
};
|
||||
macho_table["scan_in_file"] = [](const std::string& path, const std::string& pattern,
|
||||
sol::optional<std::string> section) -> sol::optional<SectionScanResult>
|
||||
{
|
||||
auto result = MachOPatternScanner::scan_for_pattern_in_file(std::filesystem::path(path), pattern,
|
||||
section.value_or("__text"));
|
||||
if (!result)
|
||||
return sol::nullopt;
|
||||
return *result;
|
||||
};
|
||||
}
|
||||
} // namespace omath::lua
|
||||
#endif
|
||||
48
source/lua/lua_triangle.cpp
Normal file
48
source/lua/lua_triangle.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
//
|
||||
// Created by orange on 10.03.2026.
|
||||
//
|
||||
#ifdef OMATH_ENABLE_LUA
|
||||
#include "omath/lua/lua.hpp"
|
||||
#include <sol/sol.hpp>
|
||||
#include <omath/linear_algebra/triangle.hpp>
|
||||
|
||||
namespace omath::lua
|
||||
{
|
||||
void LuaInterpreter::register_triangle(sol::table& omath_table)
|
||||
{
|
||||
using Vec3f = omath::Vector3<float>;
|
||||
using Tri3f = omath::Triangle<Vec3f>;
|
||||
|
||||
omath_table.new_usertype<Tri3f>(
|
||||
"Triangle", sol::constructors<Tri3f(), Tri3f(const Vec3f&, const Vec3f&, const Vec3f&)>(),
|
||||
|
||||
"vertex1",
|
||||
sol::property([](const Tri3f& t) { return t.m_vertex1; },
|
||||
[](Tri3f& t, const Vec3f& v) { t.m_vertex1 = v; }),
|
||||
"vertex2",
|
||||
sol::property([](const Tri3f& t) { return t.m_vertex2; },
|
||||
[](Tri3f& t, const Vec3f& v) { t.m_vertex2 = v; }),
|
||||
"vertex3",
|
||||
sol::property([](const Tri3f& t) { return t.m_vertex3; },
|
||||
[](Tri3f& t, const Vec3f& v) { t.m_vertex3 = v; }),
|
||||
|
||||
"calculate_normal", &Tri3f::calculate_normal,
|
||||
"side_a_length", &Tri3f::side_a_length,
|
||||
"side_b_length", &Tri3f::side_b_length,
|
||||
"side_a_vector", &Tri3f::side_a_vector,
|
||||
"side_b_vector", &Tri3f::side_b_vector,
|
||||
"hypot", &Tri3f::hypot,
|
||||
"is_rectangular", &Tri3f::is_rectangular,
|
||||
"mid_point", &Tri3f::mid_point,
|
||||
|
||||
sol::meta_function::to_string,
|
||||
[](const Tri3f& t)
|
||||
{
|
||||
return std::format("Triangle(({}, {}, {}), ({}, {}, {}), ({}, {}, {}))",
|
||||
t.m_vertex1.x, t.m_vertex1.y, t.m_vertex1.z,
|
||||
t.m_vertex2.x, t.m_vertex2.y, t.m_vertex2.z,
|
||||
t.m_vertex3.x, t.m_vertex3.y, t.m_vertex3.z);
|
||||
});
|
||||
}
|
||||
} // namespace omath::lua
|
||||
#endif
|
||||
54
source/lua/lua_vec2.cpp
Normal file
54
source/lua/lua_vec2.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// Created by orange on 07.03.2026.
|
||||
//
|
||||
#ifdef OMATH_ENABLE_LUA
|
||||
#include "omath/lua/lua.hpp"
|
||||
#include <omath/linear_algebra/vector2.hpp>
|
||||
#include <sol/sol.hpp>
|
||||
|
||||
namespace omath::lua
|
||||
{
|
||||
void LuaInterpreter::register_vec2(sol::table& omath_table)
|
||||
{
|
||||
using Vec2f = omath::Vector2<float>;
|
||||
|
||||
omath_table.new_usertype<Vec2f>(
|
||||
"Vec2", sol::constructors<Vec2f(), Vec2f(float, float)>(),
|
||||
|
||||
"x", sol::property([](const Vec2f& v) { return v.x; }, [](Vec2f& v, const float val) { v.x = val; }),
|
||||
"y", sol::property([](const Vec2f& v) { return v.y; }, [](Vec2f& v, const float val) { v.y = val; }),
|
||||
|
||||
sol::meta_function::addition, sol::resolve<Vec2f(const Vec2f&) const>(&Vec2f::operator+),
|
||||
sol::meta_function::subtraction, sol::resolve<Vec2f(const Vec2f&) const>(&Vec2f::operator-),
|
||||
sol::meta_function::unary_minus, sol::resolve<Vec2f() const>(&Vec2f::operator-),
|
||||
sol::meta_function::equal_to, &Vec2f::operator==,
|
||||
sol::meta_function::less_than, sol::resolve<bool(const Vec2f&) const>(&Vec2f::operator<),
|
||||
sol::meta_function::less_than_or_equal_to, sol::resolve<bool(const Vec2f&) const>(&Vec2f::operator<=),
|
||||
sol::meta_function::to_string,
|
||||
[](const Vec2f& v) { return std::format("Vec2({}, {})", v.x, v.y); },
|
||||
|
||||
sol::meta_function::multiplication,
|
||||
sol::overload(sol::resolve<Vec2f(const float&) const>(&Vec2f::operator*),
|
||||
[](const float s, const Vec2f& v) { return v * s; }),
|
||||
|
||||
sol::meta_function::division,
|
||||
sol::resolve<Vec2f(const float&) const>(&Vec2f::operator/),
|
||||
|
||||
"length", &Vec2f::length,
|
||||
"length_sqr", &Vec2f::length_sqr,
|
||||
"normalized", &Vec2f::normalized,
|
||||
"dot", &Vec2f::dot,
|
||||
"distance_to", &Vec2f::distance_to,
|
||||
"distance_to_sqr", &Vec2f::distance_to_sqr,
|
||||
"sum", &Vec2f::sum,
|
||||
|
||||
"abs",
|
||||
[](const Vec2f& v)
|
||||
{
|
||||
Vec2f copy = v;
|
||||
copy.abs();
|
||||
return copy;
|
||||
});
|
||||
}
|
||||
} // namespace omath::lua::detail
|
||||
#endif
|
||||
81
source/lua/lua_vec3.cpp
Normal file
81
source/lua/lua_vec3.cpp
Normal file
@@ -0,0 +1,81 @@
|
||||
//
|
||||
// Created by orange on 07.03.2026.
|
||||
//
|
||||
#ifdef OMATH_ENABLE_LUA
|
||||
#include "omath/lua/lua.hpp"
|
||||
#include <sol/sol.hpp>
|
||||
#include <omath/linear_algebra/vector3.hpp>
|
||||
|
||||
namespace omath::lua
|
||||
{
|
||||
void LuaInterpreter::register_vec3(sol::table& omath_table)
|
||||
{
|
||||
using Vec3f = omath::Vector3<float>;
|
||||
|
||||
omath_table.new_usertype<Vec3f>(
|
||||
"Vec3", sol::constructors<Vec3f(), Vec3f(float, float, float)>(),
|
||||
|
||||
"x", sol::property([](const Vec3f& v) { return v.x; }, [](Vec3f& v, float val) { v.x = val; }),
|
||||
"y", sol::property([](const Vec3f& v) { return v.y; }, [](Vec3f& v, float val) { v.y = val; }),
|
||||
"z", sol::property([](const Vec3f& v) { return v.z; }, [](Vec3f& v, float val) { v.z = val; }),
|
||||
|
||||
sol::meta_function::addition, sol::resolve<Vec3f(const Vec3f&) const>(&Vec3f::operator+),
|
||||
sol::meta_function::subtraction, sol::resolve<Vec3f(const Vec3f&) const>(&Vec3f::operator-),
|
||||
sol::meta_function::unary_minus, sol::resolve<Vec3f() const>(&Vec3f::operator-),
|
||||
sol::meta_function::equal_to, &Vec3f::operator==, sol::meta_function::less_than,
|
||||
sol::resolve<bool(const Vec3f&) const>(&Vec3f::operator<), sol::meta_function::less_than_or_equal_to,
|
||||
sol::resolve<bool(const Vec3f&) const>(&Vec3f::operator<=), sol::meta_function::to_string,
|
||||
[](const Vec3f& v) { return std::format("Vec3({}, {}, {})", v.x, v.y, v.z); },
|
||||
|
||||
sol::meta_function::multiplication,
|
||||
sol::overload(sol::resolve<Vec3f(const float&) const>(&Vec3f::operator*),
|
||||
sol::resolve<Vec3f(const Vec3f&) const>(&Vec3f::operator*),
|
||||
[](const float s, const Vec3f& v) { return v * s; }),
|
||||
|
||||
sol::meta_function::division,
|
||||
sol::overload(sol::resolve<Vec3f(const float&) const>(&Vec3f::operator/),
|
||||
sol::resolve<Vec3f(const Vec3f&) const>(&Vec3f::operator/)),
|
||||
|
||||
"length", &Vec3f::length, "length_2d", &Vec3f::length_2d, "length_sqr", &Vec3f::length_sqr,
|
||||
"normalized", &Vec3f::normalized, "dot", &Vec3f::dot, "cross", &Vec3f::cross, "distance_to",
|
||||
&Vec3f::distance_to, "distance_to_sqr", &Vec3f::distance_to_sqr, "sum",
|
||||
sol::resolve<float() const>(&Vec3f::sum), "sum_2d", &Vec3f::sum_2d, "point_to_same_direction",
|
||||
&Vec3f::point_to_same_direction, "as_array", &Vec3f::as_array,
|
||||
|
||||
"abs",
|
||||
[](const Vec3f& v)
|
||||
{
|
||||
Vec3f copy = v;
|
||||
copy.abs();
|
||||
return copy;
|
||||
},
|
||||
|
||||
"angle_between",
|
||||
[](const Vec3f& self,
|
||||
const Vec3f& other) -> std::tuple<sol::optional<float>, sol::optional<std::string>>
|
||||
{
|
||||
auto result = self.angle_between(other);
|
||||
if (result)
|
||||
return std::make_tuple(sol::optional<float>(result->as_degrees()),
|
||||
sol::optional<std::string>(sol::nullopt));
|
||||
return std::make_tuple(sol::optional<float>(sol::nullopt),
|
||||
sol::optional<std::string>("impossible angle (zero-length vector)"));
|
||||
},
|
||||
|
||||
"is_perpendicular",
|
||||
[](const Vec3f& self, const Vec3f& other, sol::optional<float> eps)
|
||||
{ return self.is_perpendicular(other, eps.value_or(0.0001f)); },
|
||||
|
||||
"as_table",
|
||||
[](const Vec3f& v, sol::this_state s) -> sol::table
|
||||
{
|
||||
sol::state_view lua(s);
|
||||
sol::table t = lua.create_table();
|
||||
t["x"] = v.x;
|
||||
t["y"] = v.y;
|
||||
t["z"] = v.z;
|
||||
return t;
|
||||
});
|
||||
}
|
||||
} // namespace omath::lua::detail
|
||||
#endif
|
||||
62
source/lua/lua_vec4.cpp
Normal file
62
source/lua/lua_vec4.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
//
|
||||
// Created by orange on 07.03.2026.
|
||||
//
|
||||
#ifdef OMATH_ENABLE_LUA
|
||||
#include "omath/lua/lua.hpp"
|
||||
#include <sol/sol.hpp>
|
||||
#include <omath/linear_algebra/vector4.hpp>
|
||||
|
||||
namespace omath::lua
|
||||
{
|
||||
void LuaInterpreter::register_vec4(sol::table& omath_table)
|
||||
{
|
||||
using Vec4f = omath::Vector4<float>;
|
||||
|
||||
omath_table.new_usertype<Vec4f>(
|
||||
"Vec4", sol::constructors<Vec4f(), Vec4f(float, float, float, float)>(),
|
||||
|
||||
"x", sol::property([](const Vec4f& v) { return v.x; }, [](Vec4f& v, float val) { v.x = val; }),
|
||||
"y", sol::property([](const Vec4f& v) { return v.y; }, [](Vec4f& v, float val) { v.y = val; }),
|
||||
"z", sol::property([](const Vec4f& v) { return v.z; }, [](Vec4f& v, float val) { v.z = val; }),
|
||||
"w", sol::property([](const Vec4f& v) { return v.w; }, [](Vec4f& v, float val) { v.w = val; }),
|
||||
|
||||
sol::meta_function::addition, sol::resolve<Vec4f(const Vec4f&) const>(&Vec4f::operator+),
|
||||
sol::meta_function::subtraction, sol::resolve<Vec4f(const Vec4f&) const>(&Vec4f::operator-),
|
||||
sol::meta_function::unary_minus, sol::resolve<Vec4f() const>(&Vec4f::operator-),
|
||||
sol::meta_function::equal_to, &Vec4f::operator==,
|
||||
sol::meta_function::less_than, sol::resolve<bool(const Vec4f&) const>(&Vec4f::operator<),
|
||||
sol::meta_function::less_than_or_equal_to, sol::resolve<bool(const Vec4f&) const>(&Vec4f::operator<=),
|
||||
sol::meta_function::to_string,
|
||||
[](const Vec4f& v) { return std::format("Vec4({}, {}, {}, {})", v.x, v.y, v.z, v.w); },
|
||||
|
||||
sol::meta_function::multiplication,
|
||||
sol::overload(sol::resolve<Vec4f(const float&) const>(&Vec4f::operator*),
|
||||
sol::resolve<Vec4f(const Vec4f&) const>(&Vec4f::operator*),
|
||||
[](const float s, const Vec4f& v) { return v * s; }),
|
||||
|
||||
sol::meta_function::division,
|
||||
sol::overload(sol::resolve<Vec4f(const float&) const>(&Vec4f::operator/),
|
||||
sol::resolve<Vec4f(const Vec4f&) const>(&Vec4f::operator/)),
|
||||
|
||||
"length", &Vec4f::length,
|
||||
"length_sqr", &Vec4f::length_sqr,
|
||||
"dot", &Vec4f::dot,
|
||||
"sum", &Vec4f::sum,
|
||||
|
||||
"abs",
|
||||
[](const Vec4f& v)
|
||||
{
|
||||
Vec4f copy = v;
|
||||
copy.abs();
|
||||
return copy;
|
||||
},
|
||||
|
||||
"clamp",
|
||||
[](Vec4f& v, float mn, float mx)
|
||||
{
|
||||
v.clamp(mn, mx);
|
||||
return v;
|
||||
});
|
||||
}
|
||||
} // namespace omath::lua::detail
|
||||
#endif
|
||||
@@ -3,9 +3,9 @@
|
||||
//
|
||||
#include "omath/pathfinding/navigation_mesh.hpp"
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace omath::pathfinding
|
||||
{
|
||||
std::expected<Vector3<float>, std::string>
|
||||
@@ -30,77 +30,72 @@ namespace omath::pathfinding
|
||||
return m_vertex_map.empty();
|
||||
}
|
||||
|
||||
std::vector<uint8_t> NavigationMesh::serialize() const noexcept
|
||||
void NavigationMesh::set_event(const Vector3<float>& vertex, const std::string_view& event_id)
|
||||
{
|
||||
std::vector<std::uint8_t> raw;
|
||||
if (!m_vertex_map.contains(vertex))
|
||||
throw std::invalid_argument(std::format("Vertex '{}' not found", vertex));
|
||||
|
||||
// Pre-calculate total size for better performance
|
||||
std::size_t total_size = 0;
|
||||
for (const auto& [vertex, neighbors] : m_vertex_map)
|
||||
{
|
||||
total_size += sizeof(vertex) + sizeof(std::uint16_t) + sizeof(Vector3<float>) * neighbors.size();
|
||||
}
|
||||
raw.reserve(total_size);
|
||||
|
||||
auto dump_to_vector = [&raw]<typename T>(const T& t)
|
||||
{
|
||||
const auto* byte_ptr = reinterpret_cast<const std::uint8_t*>(&t);
|
||||
raw.insert(raw.end(), byte_ptr, byte_ptr + sizeof(T));
|
||||
};
|
||||
|
||||
for (const auto& [vertex, neighbors] : m_vertex_map)
|
||||
{
|
||||
// Clamp neighbors count to fit in uint16_t (prevents silent data corruption)
|
||||
// NOTE: If neighbors.size() > 65535, only the first 65535 neighbors will be serialized.
|
||||
// This is a limitation of the current serialization format using uint16_t for count.
|
||||
const auto clamped_count =
|
||||
std::min<std::size_t>(neighbors.size(), std::numeric_limits<std::uint16_t>::max());
|
||||
const auto neighbors_count = static_cast<std::uint16_t>(clamped_count);
|
||||
|
||||
dump_to_vector(vertex);
|
||||
dump_to_vector(neighbors_count);
|
||||
|
||||
// Only serialize up to the clamped count
|
||||
for (std::size_t i = 0; i < clamped_count; ++i)
|
||||
dump_to_vector(neighbors[i]);
|
||||
}
|
||||
return raw;
|
||||
m_vertex_events[vertex] = event_id;
|
||||
}
|
||||
|
||||
void NavigationMesh::deserialize(const std::vector<uint8_t>& raw) noexcept
|
||||
void NavigationMesh::clear_event(const Vector3<float>& vertex)
|
||||
{
|
||||
auto load_from_vector = [](const std::vector<uint8_t>& vec, std::size_t& offset, auto& value)
|
||||
m_vertex_events.erase(vertex);
|
||||
}
|
||||
|
||||
std::optional<std::string> NavigationMesh::get_event(const Vector3<float>& vertex) const noexcept
|
||||
{
|
||||
const auto it = m_vertex_events.find(vertex);
|
||||
if (it == m_vertex_events.end())
|
||||
return std::nullopt;
|
||||
return it->second;
|
||||
}
|
||||
|
||||
// Serialization format per vertex line:
|
||||
// x y z neighbor_count event_id
|
||||
// where event_id is "-" when no event is set.
|
||||
// Neighbor lines follow: nx ny nz
|
||||
|
||||
std::string NavigationMesh::serialize() const noexcept
|
||||
{
|
||||
std::ostringstream oss;
|
||||
for (const auto& [vertex, neighbors] : m_vertex_map)
|
||||
{
|
||||
if (offset + sizeof(value) > vec.size())
|
||||
throw std::runtime_error("Deserialize: Invalid input data size.");
|
||||
const auto event_it = m_vertex_events.find(vertex);
|
||||
const std::string& event = (event_it != m_vertex_events.end()) ? event_it->second : "-";
|
||||
|
||||
std::copy_n(vec.data() + offset, sizeof(value), reinterpret_cast<uint8_t*>(&value));
|
||||
offset += sizeof(value);
|
||||
};
|
||||
oss << vertex.x << ' ' << vertex.y << ' ' << vertex.z << ' ' << neighbors.size() << ' ' << event << '\n';
|
||||
|
||||
for (const auto& n : neighbors)
|
||||
oss << n.x << ' ' << n.y << ' ' << n.z << '\n';
|
||||
}
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
void NavigationMesh::deserialize(const std::string& raw)
|
||||
{
|
||||
m_vertex_map.clear();
|
||||
m_vertex_events.clear();
|
||||
std::istringstream iss(raw);
|
||||
|
||||
std::size_t offset = 0;
|
||||
|
||||
while (offset < raw.size())
|
||||
Vector3<float> vertex;
|
||||
std::size_t neighbors_count;
|
||||
std::string event;
|
||||
while (iss >> vertex.x >> vertex.y >> vertex.z >> neighbors_count >> event)
|
||||
{
|
||||
Vector3<float> vertex;
|
||||
load_from_vector(raw, offset, vertex);
|
||||
|
||||
std::uint16_t neighbors_count;
|
||||
load_from_vector(raw, offset, neighbors_count);
|
||||
|
||||
std::vector<Vector3<float>> neighbors;
|
||||
neighbors.reserve(neighbors_count);
|
||||
|
||||
for (std::size_t i = 0; i < neighbors_count; ++i)
|
||||
{
|
||||
Vector3<float> neighbor;
|
||||
load_from_vector(raw, offset, neighbor);
|
||||
neighbors.push_back(neighbor);
|
||||
Vector3<float> n;
|
||||
if (!(iss >> n.x >> n.y >> n.z))
|
||||
throw std::runtime_error("Deserialize: Unexpected end of data.");
|
||||
neighbors.push_back(n);
|
||||
}
|
||||
|
||||
m_vertex_map.emplace(vertex, std::move(neighbors));
|
||||
|
||||
if (event != "-")
|
||||
m_vertex_events.emplace(vertex, std::move(event));
|
||||
}
|
||||
}
|
||||
} // namespace omath::pathfinding
|
||||
|
||||
@@ -4,8 +4,8 @@ project(unit_tests)
|
||||
|
||||
include(GoogleTest)
|
||||
|
||||
file(GLOB_RECURSE UNIT_TESTS_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
|
||||
add_executable(${PROJECT_NAME} ${UNIT_TESTS_SOURCES})
|
||||
file(GLOB_RECURSE UNIT_TESTS_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/general/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/engines/*.cpp")
|
||||
add_executable(${PROJECT_NAME} ${UNIT_TESTS_SOURCES} main.cpp)
|
||||
|
||||
set_target_properties(
|
||||
${PROJECT_NAME}
|
||||
@@ -22,6 +22,16 @@ else() # GTest is being linked as vcpkg package
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE GTest::gtest GTest::gtest_main omath::omath)
|
||||
endif()
|
||||
|
||||
if (OMATH_ENABLE_LUA)
|
||||
file(GLOB_RECURSE UNIT_TESTS_SOURCES_LUA CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/lua/*.cpp")
|
||||
target_compile_definitions(${PROJECT_NAME} PRIVATE LUA_SCRIPTS_DIR="${CMAKE_CURRENT_SOURCE_DIR}/lua")
|
||||
target_sources(${PROJECT_NAME} PRIVATE ${UNIT_TESTS_SOURCES_LUA})
|
||||
if (EMSCRIPTEN)
|
||||
target_link_options(${PROJECT_NAME} PRIVATE
|
||||
"SHELL:--embed-file ${CMAKE_CURRENT_SOURCE_DIR}/lua@${CMAKE_CURRENT_SOURCE_DIR}/lua")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(OMATH_ENABLE_COVERAGE)
|
||||
include(${CMAKE_SOURCE_DIR}/cmake/Coverage.cmake)
|
||||
omath_setup_coverage(${PROJECT_NAME})
|
||||
@@ -36,3 +46,4 @@ endif()
|
||||
if(NOT (ANDROID OR IOS OR EMSCRIPTEN))
|
||||
gtest_discover_tests(${PROJECT_NAME})
|
||||
endif()
|
||||
|
||||
|
||||
@@ -8,6 +8,29 @@
|
||||
using namespace omath;
|
||||
using namespace omath::pathfinding;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static NavigationMesh make_linear_chain(int length)
|
||||
{
|
||||
// 0 -> 1 -> 2 -> ... -> length-1 (directed)
|
||||
NavigationMesh nav;
|
||||
for (int i = 0; i < length; ++i)
|
||||
{
|
||||
const Vector3<float> v{static_cast<float>(i), 0.f, 0.f};
|
||||
if (i + 1 < length)
|
||||
nav.m_vertex_map[v] = {Vector3<float>{static_cast<float>(i + 1), 0.f, 0.f}};
|
||||
else
|
||||
nav.m_vertex_map[v] = {};
|
||||
}
|
||||
return nav;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Basic reachability
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST(AStarExtra, TrivialNeighbor)
|
||||
{
|
||||
NavigationMesh nav;
|
||||
@@ -78,7 +101,7 @@ TEST(AStarExtra, LongerPathAvoidsBlock)
|
||||
constexpr Vector3<float> goal = idx(2, 1);
|
||||
const auto path = Astar::find_path(start, goal, nav);
|
||||
ASSERT_FALSE(path.empty());
|
||||
EXPECT_EQ(path.front(), goal); // Astar convention: single-element or endpoint present
|
||||
EXPECT_EQ(path.front(), goal);
|
||||
}
|
||||
|
||||
TEST(AstarTests, TrivialDirectNeighborPath)
|
||||
@@ -91,9 +114,6 @@ TEST(AstarTests, TrivialDirectNeighborPath)
|
||||
nav.m_vertex_map.emplace(v2, std::vector<Vector3<float>>{v1});
|
||||
|
||||
const auto path = Astar::find_path(v1, v2, nav);
|
||||
// Current A* implementation returns the end vertex as the reconstructed
|
||||
// path (single-element) in the simple neighbor scenario. Assert that the
|
||||
// endpoint is present and reachable.
|
||||
ASSERT_EQ(path.size(), 1u);
|
||||
EXPECT_EQ(path.front(), v2);
|
||||
}
|
||||
@@ -133,4 +153,155 @@ TEST(unit_test_a_star, finding_right_path)
|
||||
mesh.m_vertex_map[{0.f, 2.f, 0.f}] = {{0.f, 3.f, 0.f}};
|
||||
mesh.m_vertex_map[{0.f, 3.f, 0.f}] = {};
|
||||
std::ignore = omath::pathfinding::Astar::find_path({}, {0.f, 3.f, 0.f}, mesh);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Directed edges
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST(AstarTests, DirectedEdge_ForwardPathExists)
|
||||
{
|
||||
// A -> B only; path from A to B should succeed
|
||||
NavigationMesh nav;
|
||||
constexpr Vector3<float> a{0.f, 0.f, 0.f};
|
||||
constexpr Vector3<float> b{1.f, 0.f, 0.f};
|
||||
nav.m_vertex_map[a] = {b};
|
||||
nav.m_vertex_map[b] = {}; // no edge back
|
||||
|
||||
const auto path = Astar::find_path(a, b, nav);
|
||||
ASSERT_FALSE(path.empty());
|
||||
EXPECT_EQ(path.back(), b);
|
||||
}
|
||||
|
||||
TEST(AstarTests, DirectedEdge_ReversePathMissing)
|
||||
{
|
||||
// A -> B only; path from B to A should fail
|
||||
NavigationMesh nav;
|
||||
constexpr Vector3<float> a{0.f, 0.f, 0.f};
|
||||
constexpr Vector3<float> b{1.f, 0.f, 0.f};
|
||||
nav.m_vertex_map[a] = {b};
|
||||
nav.m_vertex_map[b] = {};
|
||||
|
||||
const auto path = Astar::find_path(b, a, nav);
|
||||
EXPECT_TRUE(path.empty());
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Vertex snapping
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST(AstarTests, OffMeshStart_SnapsToNearestVertex)
|
||||
{
|
||||
NavigationMesh nav;
|
||||
constexpr Vector3<float> v1{0.f, 0.f, 0.f};
|
||||
constexpr Vector3<float> v2{10.f, 0.f, 0.f};
|
||||
nav.m_vertex_map[v1] = {v2};
|
||||
nav.m_vertex_map[v2] = {v1};
|
||||
|
||||
// Start is slightly off v1 but closer to it than to v2
|
||||
constexpr Vector3<float> off_start{0.1f, 0.f, 0.f};
|
||||
const auto path = Astar::find_path(off_start, v2, nav);
|
||||
ASSERT_FALSE(path.empty());
|
||||
EXPECT_EQ(path.back(), v2);
|
||||
}
|
||||
|
||||
TEST(AstarTests, OffMeshEnd_SnapsToNearestVertex)
|
||||
{
|
||||
NavigationMesh nav;
|
||||
constexpr Vector3<float> v1{0.f, 0.f, 0.f};
|
||||
constexpr Vector3<float> v2{10.f, 0.f, 0.f};
|
||||
nav.m_vertex_map[v1] = {v2};
|
||||
nav.m_vertex_map[v2] = {v1};
|
||||
|
||||
// Goal is slightly off v2 but closer to it than to v1
|
||||
constexpr Vector3<float> off_goal{9.9f, 0.f, 0.f};
|
||||
const auto path = Astar::find_path(v1, off_goal, nav);
|
||||
ASSERT_FALSE(path.empty());
|
||||
EXPECT_EQ(path.back(), v2);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Cycle handling
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST(AstarTests, CyclicGraph_FindsPathWithoutLooping)
|
||||
{
|
||||
// Triangle: A <-> B <-> C <-> A
|
||||
NavigationMesh nav;
|
||||
constexpr Vector3<float> a{0.f, 0.f, 0.f};
|
||||
constexpr Vector3<float> b{1.f, 0.f, 0.f};
|
||||
constexpr Vector3<float> c{0.5f, 1.f, 0.f};
|
||||
nav.m_vertex_map[a] = {b, c};
|
||||
nav.m_vertex_map[b] = {a, c};
|
||||
nav.m_vertex_map[c] = {a, b};
|
||||
|
||||
const auto path = Astar::find_path(a, c, nav);
|
||||
ASSERT_FALSE(path.empty());
|
||||
EXPECT_EQ(path.back(), c);
|
||||
}
|
||||
|
||||
TEST(AstarTests, SelfLoopVertex_DoesNotBreakSearch)
|
||||
{
|
||||
// Vertex with itself as a neighbor
|
||||
NavigationMesh nav;
|
||||
constexpr Vector3<float> a{0.f, 0.f, 0.f};
|
||||
constexpr Vector3<float> b{1.f, 0.f, 0.f};
|
||||
nav.m_vertex_map[a] = {a, b}; // self-loop on a
|
||||
nav.m_vertex_map[b] = {a};
|
||||
|
||||
const auto path = Astar::find_path(a, b, nav);
|
||||
ASSERT_FALSE(path.empty());
|
||||
EXPECT_EQ(path.back(), b);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Longer chains
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST(AstarTests, LinearChain_ReachesEnd)
|
||||
{
|
||||
constexpr int kLength = 10;
|
||||
const NavigationMesh nav = make_linear_chain(kLength);
|
||||
|
||||
const Vector3<float> start{0.f, 0.f, 0.f};
|
||||
const Vector3<float> goal{static_cast<float>(kLength - 1), 0.f, 0.f};
|
||||
|
||||
const auto path = Astar::find_path(start, goal, nav);
|
||||
ASSERT_FALSE(path.empty());
|
||||
EXPECT_EQ(path.back(), goal);
|
||||
}
|
||||
|
||||
TEST(AstarTests, LinearChain_MidpointReachable)
|
||||
{
|
||||
constexpr int kLength = 6;
|
||||
const NavigationMesh nav = make_linear_chain(kLength);
|
||||
|
||||
const Vector3<float> start{0.f, 0.f, 0.f};
|
||||
const Vector3<float> mid{3.f, 0.f, 0.f};
|
||||
|
||||
const auto path = Astar::find_path(start, mid, nav);
|
||||
ASSERT_FALSE(path.empty());
|
||||
EXPECT_EQ(path.back(), mid);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Serialize -> pathfind integration
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST(AstarTests, PathfindAfterSerializeDeserialize)
|
||||
{
|
||||
NavigationMesh nav;
|
||||
constexpr Vector3<float> a{0.f, 0.f, 0.f};
|
||||
constexpr Vector3<float> b{1.f, 0.f, 0.f};
|
||||
constexpr Vector3<float> c{2.f, 0.f, 0.f};
|
||||
nav.m_vertex_map[a] = {b};
|
||||
nav.m_vertex_map[b] = {a, c};
|
||||
nav.m_vertex_map[c] = {b};
|
||||
|
||||
NavigationMesh nav2;
|
||||
nav2.deserialize(nav.serialize());
|
||||
|
||||
const auto path = Astar::find_path(a, c, nav2);
|
||||
ASSERT_FALSE(path.empty());
|
||||
EXPECT_EQ(path.back(), c);
|
||||
}
|
||||
|
||||
@@ -26,38 +26,38 @@ protected:
|
||||
TEST_F(UnitTestColorGrouped, Constructor_Float)
|
||||
{
|
||||
constexpr Color color(0.5f, 0.5f, 0.5f, 1.0f);
|
||||
EXPECT_FLOAT_EQ(color.x, 0.5f);
|
||||
EXPECT_FLOAT_EQ(color.y, 0.5f);
|
||||
EXPECT_FLOAT_EQ(color.z, 0.5f);
|
||||
EXPECT_FLOAT_EQ(color.w, 1.0f);
|
||||
EXPECT_FLOAT_EQ(color.value().x, 0.5f);
|
||||
EXPECT_FLOAT_EQ(color.value().y, 0.5f);
|
||||
EXPECT_FLOAT_EQ(color.value().z, 0.5f);
|
||||
EXPECT_FLOAT_EQ(color.value().w, 1.0f);
|
||||
}
|
||||
|
||||
TEST_F(UnitTestColorGrouped, Constructor_Vector4)
|
||||
{
|
||||
constexpr omath::Vector4 vec(0.2f, 0.4f, 0.6f, 0.8f);
|
||||
constexpr Color color(vec);
|
||||
EXPECT_FLOAT_EQ(color.x, 0.2f);
|
||||
EXPECT_FLOAT_EQ(color.y, 0.4f);
|
||||
EXPECT_FLOAT_EQ(color.z, 0.6f);
|
||||
EXPECT_FLOAT_EQ(color.w, 0.8f);
|
||||
EXPECT_FLOAT_EQ(color.value().x, 0.2f);
|
||||
EXPECT_FLOAT_EQ(color.value().y, 0.4f);
|
||||
EXPECT_FLOAT_EQ(color.value().z, 0.6f);
|
||||
EXPECT_FLOAT_EQ(color.value().w, 0.8f);
|
||||
}
|
||||
|
||||
TEST_F(UnitTestColorGrouped, FromRGBA)
|
||||
{
|
||||
constexpr Color color = Color::from_rgba(128, 64, 32, 255);
|
||||
EXPECT_FLOAT_EQ(color.x, 128.0f / 255.0f);
|
||||
EXPECT_FLOAT_EQ(color.y, 64.0f / 255.0f);
|
||||
EXPECT_FLOAT_EQ(color.z, 32.0f / 255.0f);
|
||||
EXPECT_FLOAT_EQ(color.w, 1.0f);
|
||||
EXPECT_FLOAT_EQ(color.value().x, 128.0f / 255.0f);
|
||||
EXPECT_FLOAT_EQ(color.value().y, 64.0f / 255.0f);
|
||||
EXPECT_FLOAT_EQ(color.value().z, 32.0f / 255.0f);
|
||||
EXPECT_FLOAT_EQ(color.value().w, 1.0f);
|
||||
}
|
||||
|
||||
TEST_F(UnitTestColorGrouped, FromHSV)
|
||||
{
|
||||
constexpr Color color = Color::from_hsv(0.0f, 1.0f, 1.0f); // Red in HSV
|
||||
EXPECT_FLOAT_EQ(color.x, 1.0f);
|
||||
EXPECT_FLOAT_EQ(color.y, 0.0f);
|
||||
EXPECT_FLOAT_EQ(color.z, 0.0f);
|
||||
EXPECT_FLOAT_EQ(color.w, 1.0f);
|
||||
EXPECT_FLOAT_EQ(color.value().x, 1.0f);
|
||||
EXPECT_FLOAT_EQ(color.value().y, 0.0f);
|
||||
EXPECT_FLOAT_EQ(color.value().z, 0.0f);
|
||||
EXPECT_FLOAT_EQ(color.value().w, 1.0f);
|
||||
}
|
||||
|
||||
TEST_F(UnitTestColorGrouped, ToHSV)
|
||||
@@ -71,10 +71,10 @@ TEST_F(UnitTestColorGrouped, ToHSV)
|
||||
TEST_F(UnitTestColorGrouped, Blend)
|
||||
{
|
||||
const Color blended = color1.blend(color2, 0.5f);
|
||||
EXPECT_FLOAT_EQ(blended.x, 0.5f);
|
||||
EXPECT_FLOAT_EQ(blended.y, 0.5f);
|
||||
EXPECT_FLOAT_EQ(blended.z, 0.0f);
|
||||
EXPECT_FLOAT_EQ(blended.w, 1.0f);
|
||||
EXPECT_FLOAT_EQ(blended.value().x, 0.5f);
|
||||
EXPECT_FLOAT_EQ(blended.value().y, 0.5f);
|
||||
EXPECT_FLOAT_EQ(blended.value().z, 0.0f);
|
||||
EXPECT_FLOAT_EQ(blended.value().w, 1.0f);
|
||||
}
|
||||
|
||||
TEST_F(UnitTestColorGrouped, PredefinedColors)
|
||||
@@ -83,20 +83,20 @@ TEST_F(UnitTestColorGrouped, PredefinedColors)
|
||||
constexpr Color green = Color::green();
|
||||
constexpr Color blue = Color::blue();
|
||||
|
||||
EXPECT_FLOAT_EQ(red.x, 1.0f);
|
||||
EXPECT_FLOAT_EQ(red.y, 0.0f);
|
||||
EXPECT_FLOAT_EQ(red.z, 0.0f);
|
||||
EXPECT_FLOAT_EQ(red.w, 1.0f);
|
||||
EXPECT_FLOAT_EQ(red.value().x, 1.0f);
|
||||
EXPECT_FLOAT_EQ(red.value().y, 0.0f);
|
||||
EXPECT_FLOAT_EQ(red.value().z, 0.0f);
|
||||
EXPECT_FLOAT_EQ(red.value().w, 1.0f);
|
||||
|
||||
EXPECT_FLOAT_EQ(green.x, 0.0f);
|
||||
EXPECT_FLOAT_EQ(green.y, 1.0f);
|
||||
EXPECT_FLOAT_EQ(green.z, 0.0f);
|
||||
EXPECT_FLOAT_EQ(green.w, 1.0f);
|
||||
EXPECT_FLOAT_EQ(green.value().x, 0.0f);
|
||||
EXPECT_FLOAT_EQ(green.value().y, 1.0f);
|
||||
EXPECT_FLOAT_EQ(green.value().z, 0.0f);
|
||||
EXPECT_FLOAT_EQ(green.value().w, 1.0f);
|
||||
|
||||
EXPECT_FLOAT_EQ(blue.x, 0.0f);
|
||||
EXPECT_FLOAT_EQ(blue.y, 0.0f);
|
||||
EXPECT_FLOAT_EQ(blue.z, 1.0f);
|
||||
EXPECT_FLOAT_EQ(blue.w, 1.0f);
|
||||
EXPECT_FLOAT_EQ(blue.value().x, 0.0f);
|
||||
EXPECT_FLOAT_EQ(blue.value().y, 0.0f);
|
||||
EXPECT_FLOAT_EQ(blue.value().z, 1.0f);
|
||||
EXPECT_FLOAT_EQ(blue.value().w, 1.0f);
|
||||
}
|
||||
|
||||
TEST_F(UnitTestColorGrouped, BlendVector3)
|
||||
@@ -104,9 +104,9 @@ TEST_F(UnitTestColorGrouped, BlendVector3)
|
||||
constexpr Color v1(1.0f, 0.0f, 0.0f, 1.f); // Red
|
||||
constexpr Color v2(0.0f, 1.0f, 0.0f, 1.f); // Green
|
||||
constexpr Color blended = v1.blend(v2, 0.5f);
|
||||
EXPECT_FLOAT_EQ(blended.x, 0.5f);
|
||||
EXPECT_FLOAT_EQ(blended.y, 0.5f);
|
||||
EXPECT_FLOAT_EQ(blended.z, 0.0f);
|
||||
EXPECT_FLOAT_EQ(blended.value().x, 0.5f);
|
||||
EXPECT_FLOAT_EQ(blended.value().y, 0.5f);
|
||||
EXPECT_FLOAT_EQ(blended.value().z, 0.0f);
|
||||
}
|
||||
|
||||
// From unit_test_color_extra.cpp
|
||||
@@ -148,37 +148,37 @@ TEST(UnitTestColorGrouped_Extra, BlendEdgeCases)
|
||||
constexpr Color a = Color::red();
|
||||
constexpr Color b = Color::blue();
|
||||
constexpr auto r0 = a.blend(b, 0.f);
|
||||
EXPECT_FLOAT_EQ(r0.x, a.x);
|
||||
EXPECT_FLOAT_EQ(r0.value().x, a.value().x);
|
||||
constexpr auto r1 = a.blend(b, 1.f);
|
||||
EXPECT_FLOAT_EQ(r1.x, b.x);
|
||||
EXPECT_FLOAT_EQ(r1.value().x, b.value().x);
|
||||
}
|
||||
|
||||
// From unit_test_color_more.cpp
|
||||
TEST(UnitTestColorGrouped_More, DefaultCtorIsZero)
|
||||
{
|
||||
constexpr Color c;
|
||||
EXPECT_FLOAT_EQ(c.x, 0.0f);
|
||||
EXPECT_FLOAT_EQ(c.y, 0.0f);
|
||||
EXPECT_FLOAT_EQ(c.z, 0.0f);
|
||||
EXPECT_FLOAT_EQ(c.w, 0.0f);
|
||||
EXPECT_FLOAT_EQ(c.value().x, 0.0f);
|
||||
EXPECT_FLOAT_EQ(c.value().y, 0.0f);
|
||||
EXPECT_FLOAT_EQ(c.value().z, 0.0f);
|
||||
EXPECT_FLOAT_EQ(c.value().w, 0.0f);
|
||||
}
|
||||
|
||||
TEST(UnitTestColorGrouped_More, FloatCtorAndClampForRGB)
|
||||
{
|
||||
constexpr Color c(1.2f, -0.5f, 0.5f, 2.0f);
|
||||
EXPECT_FLOAT_EQ(c.x, 1.0f);
|
||||
EXPECT_FLOAT_EQ(c.y, 0.0f);
|
||||
EXPECT_FLOAT_EQ(c.z, 0.5f);
|
||||
EXPECT_FLOAT_EQ(c.w, 2.0f);
|
||||
EXPECT_FLOAT_EQ(c.value().x, 1.0f);
|
||||
EXPECT_FLOAT_EQ(c.value().y, 0.0f);
|
||||
EXPECT_FLOAT_EQ(c.value().z, 0.5f);
|
||||
EXPECT_FLOAT_EQ(c.value().w, 2.0f);
|
||||
}
|
||||
|
||||
TEST(UnitTestColorGrouped_More, FromRgbaProducesScaledComponents)
|
||||
{
|
||||
constexpr Color c = Color::from_rgba(25u, 128u, 230u, 64u);
|
||||
EXPECT_NEAR(c.x, 25.0f/255.0f, 1e-6f);
|
||||
EXPECT_NEAR(c.y, 128.0f/255.0f, 1e-6f);
|
||||
EXPECT_NEAR(c.z, 230.0f/255.0f, 1e-6f);
|
||||
EXPECT_NEAR(c.w, 64.0f/255.0f, 1e-6f);
|
||||
EXPECT_NEAR(c.value().x, 25.0f/255.0f, 1e-6f);
|
||||
EXPECT_NEAR(c.value().y, 128.0f/255.0f, 1e-6f);
|
||||
EXPECT_NEAR(c.value().z, 230.0f/255.0f, 1e-6f);
|
||||
EXPECT_NEAR(c.value().w, 64.0f/255.0f, 1e-6f);
|
||||
}
|
||||
|
||||
TEST(UnitTestColorGrouped_More, BlendProducesIntermediate)
|
||||
@@ -186,10 +186,10 @@ TEST(UnitTestColorGrouped_More, BlendProducesIntermediate)
|
||||
constexpr Color c0(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
constexpr Color c1(1.0f, 1.0f, 1.0f, 0.0f);
|
||||
constexpr Color mid = c0.blend(c1, 0.5f);
|
||||
EXPECT_FLOAT_EQ(mid.x, 0.5f);
|
||||
EXPECT_FLOAT_EQ(mid.y, 0.5f);
|
||||
EXPECT_FLOAT_EQ(mid.z, 0.5f);
|
||||
EXPECT_FLOAT_EQ(mid.w, 0.5f);
|
||||
EXPECT_FLOAT_EQ(mid.value().x, 0.5f);
|
||||
EXPECT_FLOAT_EQ(mid.value().y, 0.5f);
|
||||
EXPECT_FLOAT_EQ(mid.value().z, 0.5f);
|
||||
EXPECT_FLOAT_EQ(mid.value().w, 0.5f);
|
||||
}
|
||||
|
||||
TEST(UnitTestColorGrouped_More, HsvRoundTrip)
|
||||
@@ -197,9 +197,9 @@ TEST(UnitTestColorGrouped_More, HsvRoundTrip)
|
||||
constexpr Color red = Color::red();
|
||||
const auto hsv = red.to_hsv();
|
||||
const Color back = Color::from_hsv(hsv);
|
||||
EXPECT_NEAR(back.x, 1.0f, 1e-6f);
|
||||
EXPECT_NEAR(back.y, 0.0f, 1e-6f);
|
||||
EXPECT_NEAR(back.z, 0.0f, 1e-6f);
|
||||
EXPECT_NEAR(back.value().x, 1.0f, 1e-6f);
|
||||
EXPECT_NEAR(back.value().y, 0.0f, 1e-6f);
|
||||
EXPECT_NEAR(back.value().z, 0.0f, 1e-6f);
|
||||
}
|
||||
|
||||
TEST(UnitTestColorGrouped_More, ToStringContainsComponents)
|
||||
@@ -230,18 +230,18 @@ TEST(UnitTestColorGrouped_More2, FromHsvCases)
|
||||
auto check_hue = [&](float h) {
|
||||
SCOPED_TRACE(::testing::Message() << "h=" << h);
|
||||
Color c = Color::from_hsv(h, 1.f, 1.f);
|
||||
EXPECT_TRUE(std::isfinite(c.x));
|
||||
EXPECT_TRUE(std::isfinite(c.y));
|
||||
EXPECT_TRUE(std::isfinite(c.z));
|
||||
EXPECT_GE(c.x, -eps);
|
||||
EXPECT_LE(c.x, 1.f + eps);
|
||||
EXPECT_GE(c.y, -eps);
|
||||
EXPECT_LE(c.y, 1.f + eps);
|
||||
EXPECT_GE(c.z, -eps);
|
||||
EXPECT_LE(c.z, 1.f + eps);
|
||||
EXPECT_TRUE(std::isfinite(c.value().x));
|
||||
EXPECT_TRUE(std::isfinite(c.value().y));
|
||||
EXPECT_TRUE(std::isfinite(c.value().z));
|
||||
EXPECT_GE(c.value().x, -eps);
|
||||
EXPECT_LE(c.value().x, 1.f + eps);
|
||||
EXPECT_GE(c.value().y, -eps);
|
||||
EXPECT_LE(c.value().y, 1.f + eps);
|
||||
EXPECT_GE(c.value().z, -eps);
|
||||
EXPECT_LE(c.value().z, 1.f + eps);
|
||||
|
||||
float mx = std::max({c.x, c.y, c.z});
|
||||
float mn = std::min({c.x, c.y, c.z});
|
||||
float mx = std::max({c.value().x, c.value().y, c.value().z});
|
||||
float mn = std::min({c.value().x, c.value().y, c.value().z});
|
||||
EXPECT_GE(mx, 0.999f);
|
||||
EXPECT_LE(mn, 1e-3f + 1e-4f);
|
||||
};
|
||||
@@ -261,13 +261,13 @@ TEST(UnitTestColorGrouped_More2, ToHsvAndSetters)
|
||||
EXPECT_NEAR(hsv.value, 0.6f, 1e-6f);
|
||||
|
||||
c.set_hue(0.0f);
|
||||
EXPECT_TRUE(std::isfinite(c.x));
|
||||
EXPECT_TRUE(std::isfinite(c.value().x));
|
||||
|
||||
c.set_saturation(0.0f);
|
||||
EXPECT_TRUE(std::isfinite(c.y));
|
||||
EXPECT_TRUE(std::isfinite(c.value().y));
|
||||
|
||||
c.set_value(0.5f);
|
||||
EXPECT_TRUE(std::isfinite(c.z));
|
||||
EXPECT_TRUE(std::isfinite(c.value().z));
|
||||
}
|
||||
|
||||
TEST(UnitTestColorGrouped_More2, BlendAndStaticColors)
|
||||
@@ -275,14 +275,14 @@ TEST(UnitTestColorGrouped_More2, BlendAndStaticColors)
|
||||
constexpr Color a = Color::red();
|
||||
constexpr Color b = Color::blue();
|
||||
constexpr auto mid = a.blend(b, 0.5f);
|
||||
EXPECT_GT(mid.x, 0.f);
|
||||
EXPECT_GT(mid.z, 0.f);
|
||||
EXPECT_GT(mid.value().x, 0.f);
|
||||
EXPECT_GT(mid.value().z, 0.f);
|
||||
|
||||
constexpr auto all_a = a.blend(b, -1.f);
|
||||
EXPECT_NEAR(all_a.x, a.x, 1e-6f);
|
||||
EXPECT_NEAR(all_a.value().x, a.value().x, 1e-6f);
|
||||
|
||||
constexpr auto all_b = a.blend(b, 2.f);
|
||||
EXPECT_NEAR(all_b.z, b.z, 1e-6f);
|
||||
EXPECT_NEAR(all_b.value().z, b.value().z, 1e-6f);
|
||||
}
|
||||
|
||||
TEST(UnitTestColorGrouped_More2, FormatterUsesToString)
|
||||
@@ -291,3 +291,35 @@ TEST(UnitTestColorGrouped_More2, FormatterUsesToString)
|
||||
const auto formatted = std::format("{}", c);
|
||||
EXPECT_NE(formatted.find("r:10"), std::string::npos);
|
||||
}
|
||||
|
||||
TEST(UnitTestColorGrouped_More2, FormatterRgb)
|
||||
{
|
||||
constexpr Color c = Color::from_rgba(255, 128, 0, 64);
|
||||
const auto s = std::format("{:rgb}", c);
|
||||
EXPECT_NE(s.find("r:255"), std::string::npos);
|
||||
EXPECT_NE(s.find("g:128"), std::string::npos);
|
||||
EXPECT_NE(s.find("b:0"), std::string::npos);
|
||||
EXPECT_NE(s.find("a:64"), std::string::npos);
|
||||
}
|
||||
|
||||
TEST(UnitTestColorGrouped_More2, FormatterRgbf)
|
||||
{
|
||||
constexpr Color c(0.5f, 0.25f, 1.0f, 0.75f);
|
||||
const auto s = std::format("{:rgbf}", c);
|
||||
EXPECT_NE(s.find("r:"), std::string::npos);
|
||||
EXPECT_NE(s.find("g:"), std::string::npos);
|
||||
EXPECT_NE(s.find("b:"), std::string::npos);
|
||||
EXPECT_NE(s.find("a:"), std::string::npos);
|
||||
// Values should be in [0,1] float range, not 0-255
|
||||
EXPECT_EQ(s.find("r:127"), std::string::npos);
|
||||
EXPECT_EQ(s.find("r:255"), std::string::npos);
|
||||
}
|
||||
|
||||
TEST(UnitTestColorGrouped_More2, FormatterHsv)
|
||||
{
|
||||
const Color c = Color::red();
|
||||
const auto s = std::format("{:hsv}", c);
|
||||
EXPECT_NE(s.find("h:"), std::string::npos);
|
||||
EXPECT_NE(s.find("s:"), std::string::npos);
|
||||
EXPECT_NE(s.find("v:"), std::string::npos);
|
||||
}
|
||||
|
||||
471
tests/general/unit_test_epa_comprehensive.cpp
Normal file
471
tests/general/unit_test_epa_comprehensive.cpp
Normal file
@@ -0,0 +1,471 @@
|
||||
//
|
||||
// Comprehensive EPA tests.
|
||||
// Covers: all 3 axis directions, multiple depth levels, penetration-vector
|
||||
// round-trips, depth monotonicity, symmetry, asymmetric sizes, memory
|
||||
// resource variants, tolerance sensitivity, and iteration bookkeeping.
|
||||
//
|
||||
#include <cmath>
|
||||
#include <gtest/gtest.h>
|
||||
#include <memory_resource>
|
||||
#include <omath/collision/epa_algorithm.hpp>
|
||||
#include <omath/collision/gjk_algorithm.hpp>
|
||||
#include <omath/engines/source_engine/collider.hpp>
|
||||
#include <omath/engines/source_engine/mesh.hpp>
|
||||
|
||||
using Mesh = omath::source_engine::Mesh;
|
||||
using Collider = omath::source_engine::MeshCollider;
|
||||
using Gjk = omath::collision::GjkAlgorithm<Collider>;
|
||||
using Epa = omath::collision::Epa<Collider>;
|
||||
using Vec3 = omath::Vector3<float>;
|
||||
|
||||
namespace
|
||||
{
|
||||
const std::vector<omath::primitives::Vertex<>> k_cube_vbo = {
|
||||
{ { -1.f, -1.f, -1.f }, {}, {} },
|
||||
{ { -1.f, -1.f, 1.f }, {}, {} },
|
||||
{ { -1.f, 1.f, -1.f }, {}, {} },
|
||||
{ { -1.f, 1.f, 1.f }, {}, {} },
|
||||
{ { 1.f, 1.f, 1.f }, {}, {} },
|
||||
{ { 1.f, 1.f, -1.f }, {}, {} },
|
||||
{ { 1.f, -1.f, 1.f }, {}, {} },
|
||||
{ { 1.f, -1.f, -1.f }, {}, {} },
|
||||
};
|
||||
const std::vector<omath::Vector3<std::uint32_t>> k_empty_ebo{};
|
||||
|
||||
constexpr Epa::Params k_default_params{ .max_iterations = 64, .tolerance = 1e-4f };
|
||||
|
||||
Collider make_cube(const Vec3& origin = {}, const Vec3& scale = { 1, 1, 1 })
|
||||
{
|
||||
Mesh m{ k_cube_vbo, k_empty_ebo, scale };
|
||||
m.set_origin(origin);
|
||||
return Collider{ m };
|
||||
}
|
||||
|
||||
// Run GJK then EPA; asserts GJK hit and EPA converged.
|
||||
Epa::Result solve(const Collider& a, const Collider& b,
|
||||
const Epa::Params& params = k_default_params)
|
||||
{
|
||||
const auto [hit, simplex] = Gjk::is_collide_with_simplex_info(a, b);
|
||||
EXPECT_TRUE(hit) << "GJK must detect collision before EPA can run";
|
||||
auto result = Epa::solve(a, b, simplex, params);
|
||||
EXPECT_TRUE(result.has_value()) << "EPA must converge";
|
||||
return *result;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Normal direction per axis
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// For two unit cubes (half-extent 1) with B offset by d along an axis:
|
||||
// depth = 2 - d (distance from origin to nearest face of Minkowski diff)
|
||||
// normal component along that axis ≈ ±1
|
||||
|
||||
TEST(EpaComprehensive, NormalAlongX_Positive)
|
||||
{
|
||||
const auto r = solve(make_cube({ 0, 0, 0 }), make_cube({ 0.5f, 0, 0 }));
|
||||
EXPECT_NEAR(std::abs(r.normal.x), 1.f, 1e-3f);
|
||||
EXPECT_NEAR(r.normal.y, 0.f, 1e-3f);
|
||||
EXPECT_NEAR(r.normal.z, 0.f, 1e-3f);
|
||||
}
|
||||
|
||||
TEST(EpaComprehensive, NormalAlongX_Negative)
|
||||
{
|
||||
const auto r = solve(make_cube({ 0, 0, 0 }), make_cube({ -0.5f, 0, 0 }));
|
||||
EXPECT_NEAR(std::abs(r.normal.x), 1.f, 1e-3f);
|
||||
EXPECT_NEAR(r.normal.y, 0.f, 1e-3f);
|
||||
EXPECT_NEAR(r.normal.z, 0.f, 1e-3f);
|
||||
}
|
||||
|
||||
TEST(EpaComprehensive, NormalAlongY_Positive)
|
||||
{
|
||||
const auto r = solve(make_cube({ 0, 0, 0 }), make_cube({ 0, 0.5f, 0 }));
|
||||
EXPECT_NEAR(r.normal.x, 0.f, 1e-3f);
|
||||
EXPECT_NEAR(std::abs(r.normal.y), 1.f, 1e-3f);
|
||||
EXPECT_NEAR(r.normal.z, 0.f, 1e-3f);
|
||||
}
|
||||
|
||||
TEST(EpaComprehensive, NormalAlongY_Negative)
|
||||
{
|
||||
const auto r = solve(make_cube({ 0, 0, 0 }), make_cube({ 0, -0.5f, 0 }));
|
||||
EXPECT_NEAR(r.normal.x, 0.f, 1e-3f);
|
||||
EXPECT_NEAR(std::abs(r.normal.y), 1.f, 1e-3f);
|
||||
EXPECT_NEAR(r.normal.z, 0.f, 1e-3f);
|
||||
}
|
||||
|
||||
TEST(EpaComprehensive, NormalAlongZ_Positive)
|
||||
{
|
||||
const auto r = solve(make_cube({ 0, 0, 0 }), make_cube({ 0, 0, 0.5f }));
|
||||
EXPECT_NEAR(r.normal.x, 0.f, 1e-3f);
|
||||
EXPECT_NEAR(r.normal.y, 0.f, 1e-3f);
|
||||
EXPECT_NEAR(std::abs(r.normal.z), 1.f, 1e-3f);
|
||||
}
|
||||
|
||||
TEST(EpaComprehensive, NormalAlongZ_Negative)
|
||||
{
|
||||
const auto r = solve(make_cube({ 0, 0, 0 }), make_cube({ 0, 0, -0.5f }));
|
||||
EXPECT_NEAR(r.normal.x, 0.f, 1e-3f);
|
||||
EXPECT_NEAR(r.normal.y, 0.f, 1e-3f);
|
||||
EXPECT_NEAR(std::abs(r.normal.z), 1.f, 1e-3f);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Depth correctness (depth = 2 - offset for unit cubes)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST(EpaComprehensive, Depth_ShallowOverlap)
|
||||
{
|
||||
// offset 1.9 → depth 0.1
|
||||
const auto r = solve(make_cube({ 0, 0, 0 }), make_cube({ 1.9f, 0, 0 }));
|
||||
EXPECT_NEAR(r.depth, 0.1f, 1e-2f);
|
||||
}
|
||||
|
||||
TEST(EpaComprehensive, Depth_QuarterOverlap)
|
||||
{
|
||||
// offset 1.5 → depth 0.5
|
||||
const auto r = solve(make_cube({ 0, 0, 0 }), make_cube({ 1.5f, 0, 0 }));
|
||||
EXPECT_NEAR(r.depth, 0.5f, 1e-2f);
|
||||
}
|
||||
|
||||
TEST(EpaComprehensive, Depth_HalfOverlap)
|
||||
{
|
||||
// offset 1.0 → depth 1.0
|
||||
const auto r = solve(make_cube({ 0, 0, 0 }), make_cube({ 1.0f, 0, 0 }));
|
||||
EXPECT_NEAR(r.depth, 1.0f, 1e-2f);
|
||||
}
|
||||
|
||||
TEST(EpaComprehensive, Depth_ThreeQuarterOverlap)
|
||||
{
|
||||
// offset 0.5 → depth 1.5
|
||||
const auto r = solve(make_cube({ 0, 0, 0 }), make_cube({ 0.5f, 0, 0 }));
|
||||
EXPECT_NEAR(r.depth, 1.5f, 1e-2f);
|
||||
}
|
||||
|
||||
TEST(EpaComprehensive, Depth_AlongY_HalfOverlap)
|
||||
{
|
||||
const auto r = solve(make_cube({ 0, 0, 0 }), make_cube({ 0, 1.0f, 0 }));
|
||||
EXPECT_NEAR(r.depth, 1.0f, 1e-2f);
|
||||
}
|
||||
|
||||
TEST(EpaComprehensive, Depth_AlongZ_HalfOverlap)
|
||||
{
|
||||
const auto r = solve(make_cube({ 0, 0, 0 }), make_cube({ 0, 0, 1.0f }));
|
||||
EXPECT_NEAR(r.depth, 1.0f, 1e-2f);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Depth monotonicity — deeper overlap → larger depth
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST(EpaComprehensive, DepthMonotonic_AlongX)
|
||||
{
|
||||
const float d1 = solve(make_cube({ 0, 0, 0 }), make_cube({ 1.9f, 0, 0 })).depth; // ~0.1
|
||||
const float d2 = solve(make_cube({ 0, 0, 0 }), make_cube({ 1.5f, 0, 0 })).depth; // ~0.5
|
||||
const float d3 = solve(make_cube({ 0, 0, 0 }), make_cube({ 1.0f, 0, 0 })).depth; // ~1.0
|
||||
const float d4 = solve(make_cube({ 0, 0, 0 }), make_cube({ 0.5f, 0, 0 })).depth; // ~1.5
|
||||
|
||||
EXPECT_LT(d1, d2);
|
||||
EXPECT_LT(d2, d3);
|
||||
EXPECT_LT(d3, d4);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Normal is a unit vector
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST(EpaComprehensive, NormalIsUnit_AlongX)
|
||||
{
|
||||
const auto r = solve(make_cube({ 0, 0, 0 }), make_cube({ 0.5f, 0, 0 }));
|
||||
EXPECT_NEAR(r.normal.dot(r.normal), 1.f, 1e-5f);
|
||||
}
|
||||
|
||||
TEST(EpaComprehensive, NormalIsUnit_AlongY)
|
||||
{
|
||||
const auto r = solve(make_cube({ 0, 0, 0 }), make_cube({ 0, 1.2f, 0 }));
|
||||
EXPECT_NEAR(r.normal.dot(r.normal), 1.f, 1e-5f);
|
||||
}
|
||||
|
||||
TEST(EpaComprehensive, NormalIsUnit_AlongZ)
|
||||
{
|
||||
const auto r = solve(make_cube({ 0, 0, 0 }), make_cube({ 0, 0, 0.8f }));
|
||||
EXPECT_NEAR(r.normal.dot(r.normal), 1.f, 1e-5f);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Penetration vector = normal * depth
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST(EpaComprehensive, PenetrationVectorLength_EqualsDepth)
|
||||
{
|
||||
const auto r = solve(make_cube({ 0, 0, 0 }), make_cube({ 0.5f, 0, 0 }));
|
||||
const float pen_len = std::sqrt(r.penetration_vector.dot(r.penetration_vector));
|
||||
EXPECT_NEAR(pen_len, r.depth, 1e-5f);
|
||||
}
|
||||
|
||||
TEST(EpaComprehensive, PenetrationVectorDirection_ParallelToNormal)
|
||||
{
|
||||
const auto r = solve(make_cube({ 0, 0, 0 }), make_cube({ 0, 1.0f, 0 }));
|
||||
// penetration_vector = normal * depth → cross product must be ~zero
|
||||
const auto cross = r.penetration_vector.cross(r.normal);
|
||||
EXPECT_NEAR(cross.dot(cross), 0.f, 1e-8f);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Round-trip: applying penetration_vector separates the shapes
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST(EpaComprehensive, RoundTrip_AlongX)
|
||||
{
|
||||
const auto a = make_cube({ 0, 0, 0 });
|
||||
Mesh mesh_b{ k_cube_vbo, k_empty_ebo };
|
||||
mesh_b.set_origin({ 0.5f, 0, 0 });
|
||||
const auto b = Collider{ mesh_b };
|
||||
|
||||
const auto r = solve(a, b);
|
||||
constexpr float margin = 1.f + 1e-3f;
|
||||
|
||||
// Move B along the penetration vector; it should separate from A
|
||||
Mesh mesh_sep{ k_cube_vbo, k_empty_ebo };
|
||||
mesh_sep.set_origin(mesh_b.get_origin() + r.penetration_vector * margin);
|
||||
EXPECT_FALSE(Gjk::is_collide(a, Collider{ mesh_sep })) << "Applying pen vector must separate";
|
||||
|
||||
// Moving the wrong way must still collide
|
||||
Mesh mesh_wrong{ k_cube_vbo, k_empty_ebo };
|
||||
mesh_wrong.set_origin(mesh_b.get_origin() - r.penetration_vector * margin);
|
||||
EXPECT_TRUE(Gjk::is_collide(a, Collider{ mesh_wrong })) << "Opposite direction must still collide";
|
||||
}
|
||||
|
||||
TEST(EpaComprehensive, RoundTrip_AlongY)
|
||||
{
|
||||
const auto a = make_cube({ 0, 0, 0 });
|
||||
Mesh mesh_b{ k_cube_vbo, k_empty_ebo };
|
||||
mesh_b.set_origin({ 0, 0.8f, 0 });
|
||||
const auto b = Collider{ mesh_b };
|
||||
|
||||
const auto r = solve(a, b);
|
||||
constexpr float margin = 1.f + 1e-3f;
|
||||
|
||||
Mesh mesh_sep{ k_cube_vbo, k_empty_ebo };
|
||||
mesh_sep.set_origin(mesh_b.get_origin() + r.penetration_vector * margin);
|
||||
EXPECT_FALSE(Gjk::is_collide(a, Collider{ mesh_sep }));
|
||||
|
||||
Mesh mesh_wrong{ k_cube_vbo, k_empty_ebo };
|
||||
mesh_wrong.set_origin(mesh_b.get_origin() - r.penetration_vector * margin);
|
||||
EXPECT_TRUE(Gjk::is_collide(a, Collider{ mesh_wrong }));
|
||||
}
|
||||
|
||||
TEST(EpaComprehensive, RoundTrip_AlongZ)
|
||||
{
|
||||
const auto a = make_cube({ 0, 0, 0 });
|
||||
Mesh mesh_b{ k_cube_vbo, k_empty_ebo };
|
||||
mesh_b.set_origin({ 0, 0, 1.2f });
|
||||
const auto b = Collider{ mesh_b };
|
||||
|
||||
const auto r = solve(a, b);
|
||||
constexpr float margin = 1.f + 1e-3f;
|
||||
|
||||
Mesh mesh_sep{ k_cube_vbo, k_empty_ebo };
|
||||
mesh_sep.set_origin(mesh_b.get_origin() + r.penetration_vector * margin);
|
||||
EXPECT_FALSE(Gjk::is_collide(a, Collider{ mesh_sep }));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Symmetry — swapping A and B preserves depth
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST(EpaComprehensive, Symmetry_DepthIsIndependentOfOrder)
|
||||
{
|
||||
const auto a = make_cube({ 0, 0, 0 });
|
||||
const auto b = make_cube({ 0.5f, 0, 0 });
|
||||
|
||||
const float depth_ab = solve(a, b).depth;
|
||||
const float depth_ba = solve(b, a).depth;
|
||||
|
||||
EXPECT_NEAR(depth_ab, depth_ba, 1e-2f);
|
||||
}
|
||||
|
||||
TEST(EpaComprehensive, Symmetry_NormalsAreOpposite)
|
||||
{
|
||||
const auto a = make_cube({ 0, 0, 0 });
|
||||
const auto b = make_cube({ 0.5f, 0, 0 });
|
||||
|
||||
const Vec3 n_ab = solve(a, b).normal;
|
||||
const Vec3 n_ba = solve(b, a).normal;
|
||||
|
||||
// The normals should be anti-parallel: n_ab · n_ba ≈ -1
|
||||
EXPECT_NEAR(n_ab.dot(n_ba), -1.f, 1e-3f);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Asymmetric sizes
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST(EpaComprehensive, LargeVsSmall_DepthCorrect)
|
||||
{
|
||||
// Big (half-ext 2) at origin, small (half-ext 0.5) at (2.0, 0, 0)
|
||||
// Minkowski diff closest face in X at distance 0.5
|
||||
const auto r = solve(make_cube({ 0, 0, 0 }, { 2, 2, 2 }), make_cube({ 2.0f, 0, 0 }, { 0.5f, 0.5f, 0.5f }));
|
||||
EXPECT_NEAR(r.depth, 0.5f, 1e-2f);
|
||||
EXPECT_NEAR(std::abs(r.normal.x), 1.f, 1e-3f);
|
||||
}
|
||||
|
||||
TEST(EpaComprehensive, LargeVsSmall_RoundTrip)
|
||||
{
|
||||
const auto a = make_cube({ 0, 0, 0 }, { 2, 2, 2 });
|
||||
|
||||
Mesh mesh_b{ k_cube_vbo, k_empty_ebo, { 0.5f, 0.5f, 0.5f } };
|
||||
mesh_b.set_origin({ 2.0f, 0, 0 });
|
||||
const auto b = Collider{ mesh_b };
|
||||
|
||||
const auto r = solve(a, b);
|
||||
constexpr float margin = 1.f + 1e-3f;
|
||||
|
||||
Mesh mesh_sep{ k_cube_vbo, k_empty_ebo, { 0.5f, 0.5f, 0.5f } };
|
||||
mesh_sep.set_origin(mesh_b.get_origin() + r.penetration_vector * margin);
|
||||
EXPECT_FALSE(Gjk::is_collide(a, Collider{ mesh_sep }));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Memory resource variants
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST(EpaComprehensive, MonotonicBuffer_ConvergesCorrectly)
|
||||
{
|
||||
const auto a = make_cube({ 0, 0, 0 });
|
||||
const auto b = make_cube({ 0.5f, 0, 0 });
|
||||
const auto [hit, simplex] = Gjk::is_collide_with_simplex_info(a, b);
|
||||
ASSERT_TRUE(hit);
|
||||
|
||||
constexpr std::size_t k_buf = 32768;
|
||||
alignas(std::max_align_t) char buf[k_buf];
|
||||
std::pmr::monotonic_buffer_resource mr{ buf, k_buf, std::pmr::null_memory_resource() };
|
||||
|
||||
const auto r = Epa::solve(a, b, simplex, k_default_params, mr);
|
||||
ASSERT_TRUE(r.has_value());
|
||||
EXPECT_NEAR(r->depth, 1.5f, 1e-2f);
|
||||
}
|
||||
|
||||
TEST(EpaComprehensive, MonotonicBuffer_MultipleReleaseCycles)
|
||||
{
|
||||
// Verify mr.release() correctly resets the buffer across multiple calls
|
||||
const auto a = make_cube({ 0, 0, 0 });
|
||||
const auto b = make_cube({ 0.5f, 0, 0 });
|
||||
const auto [hit, simplex] = Gjk::is_collide_with_simplex_info(a, b);
|
||||
ASSERT_TRUE(hit);
|
||||
|
||||
constexpr std::size_t k_buf = 32768;
|
||||
alignas(std::max_align_t) char buf[k_buf];
|
||||
std::pmr::monotonic_buffer_resource mr{ buf, k_buf, std::pmr::null_memory_resource() };
|
||||
|
||||
float first_depth = 0.f;
|
||||
for (int i = 0; i < 5; ++i)
|
||||
{
|
||||
mr.release();
|
||||
const auto r = Epa::solve(a, b, simplex, k_default_params, mr);
|
||||
ASSERT_TRUE(r.has_value()) << "solve must converge on iteration " << i;
|
||||
if (i == 0)
|
||||
first_depth = r->depth;
|
||||
else
|
||||
EXPECT_NEAR(r->depth, first_depth, 1e-6f) << "depth must be deterministic";
|
||||
}
|
||||
}
|
||||
|
||||
TEST(EpaComprehensive, DefaultResource_ConvergesCorrectly)
|
||||
{
|
||||
const auto a = make_cube({ 0, 0, 0 });
|
||||
const auto b = make_cube({ 1.0f, 0, 0 });
|
||||
const auto [hit, simplex] = Gjk::is_collide_with_simplex_info(a, b);
|
||||
ASSERT_TRUE(hit);
|
||||
|
||||
const auto r = Epa::solve(a, b, simplex);
|
||||
ASSERT_TRUE(r.has_value());
|
||||
EXPECT_NEAR(r->depth, 1.0f, 1e-2f);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tolerance sensitivity
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST(EpaComprehensive, TighterTolerance_MoreAccurateDepth)
|
||||
{
|
||||
const auto a = make_cube({ 0, 0, 0 });
|
||||
const auto b = make_cube({ 1.0f, 0, 0 });
|
||||
const auto [hit, simplex] = Gjk::is_collide_with_simplex_info(a, b);
|
||||
ASSERT_TRUE(hit);
|
||||
|
||||
const Epa::Params loose{ .max_iterations = 64, .tolerance = 1e-2f };
|
||||
const Epa::Params tight{ .max_iterations = 64, .tolerance = 1e-5f };
|
||||
|
||||
const auto r_loose = Epa::solve(a, b, simplex, loose);
|
||||
const auto r_tight = Epa::solve(a, b, simplex, tight);
|
||||
ASSERT_TRUE(r_loose.has_value());
|
||||
ASSERT_TRUE(r_tight.has_value());
|
||||
|
||||
// Tighter tolerance must yield a result at least as accurate
|
||||
EXPECT_LE(std::abs(r_tight->depth - 1.0f), std::abs(r_loose->depth - 1.0f) + 1e-4f);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Bookkeeping fields
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST(EpaComprehensive, Bookkeeping_IterationsInBounds)
|
||||
{
|
||||
const auto a = make_cube({ 0, 0, 0 });
|
||||
const auto b = make_cube({ 0.5f, 0, 0 });
|
||||
const auto r = solve(a, b);
|
||||
|
||||
EXPECT_GT(r.iterations, 0);
|
||||
EXPECT_LE(r.iterations, k_default_params.max_iterations);
|
||||
}
|
||||
|
||||
TEST(EpaComprehensive, Bookkeeping_FacesAndVerticesGrow)
|
||||
{
|
||||
const auto a = make_cube({ 0, 0, 0 });
|
||||
const auto b = make_cube({ 0.5f, 0, 0 });
|
||||
const auto r = solve(a, b);
|
||||
|
||||
// Started with a tetrahedron (4 faces, 4 vertices); EPA must have expanded it
|
||||
EXPECT_GE(r.num_faces, 4);
|
||||
EXPECT_GE(r.num_vertices, 4);
|
||||
}
|
||||
|
||||
TEST(EpaComprehensive, Bookkeeping_MaxIterationsRespected)
|
||||
{
|
||||
const auto a = make_cube({ 0, 0, 0 });
|
||||
const auto b = make_cube({ 0.5f, 0, 0 });
|
||||
const auto [hit, simplex] = Gjk::is_collide_with_simplex_info(a, b);
|
||||
ASSERT_TRUE(hit);
|
||||
|
||||
constexpr Epa::Params tight{ .max_iterations = 3, .tolerance = 1e-10f };
|
||||
const auto r = Epa::solve(a, b, simplex, tight);
|
||||
|
||||
// Must return something (fallback best-face path) and respect the cap
|
||||
if (r.has_value())
|
||||
EXPECT_LE(r->iterations, tight.max_iterations);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Determinism
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST(EpaComprehensive, Deterministic_SameResultOnRepeatedCalls)
|
||||
{
|
||||
const auto a = make_cube({ 0, 0, 0 });
|
||||
const auto b = make_cube({ 0.7f, 0, 0 });
|
||||
const auto [hit, simplex] = Gjk::is_collide_with_simplex_info(a, b);
|
||||
ASSERT_TRUE(hit);
|
||||
|
||||
const auto first = Epa::solve(a, b, simplex);
|
||||
ASSERT_TRUE(first.has_value());
|
||||
|
||||
for (int i = 0; i < 5; ++i)
|
||||
{
|
||||
const auto r = Epa::solve(a, b, simplex);
|
||||
ASSERT_TRUE(r.has_value());
|
||||
EXPECT_NEAR(r->depth, first->depth, 1e-6f);
|
||||
EXPECT_NEAR(r->normal.x, first->normal.x, 1e-6f);
|
||||
EXPECT_NEAR(r->normal.y, first->normal.y, 1e-6f);
|
||||
EXPECT_NEAR(r->normal.z, first->normal.z, 1e-6f);
|
||||
}
|
||||
}
|
||||
277
tests/general/unit_test_gjk_comprehensive.cpp
Normal file
277
tests/general/unit_test_gjk_comprehensive.cpp
Normal file
@@ -0,0 +1,277 @@
|
||||
//
|
||||
// Comprehensive GJK tests.
|
||||
// Covers: all 6 axis directions, diagonal cases, boundary touching,
|
||||
// asymmetric sizes, nesting, symmetry, simplex info, far separation.
|
||||
//
|
||||
#include <gtest/gtest.h>
|
||||
#include <omath/collision/gjk_algorithm.hpp>
|
||||
#include <omath/engines/source_engine/collider.hpp>
|
||||
#include <omath/engines/source_engine/mesh.hpp>
|
||||
|
||||
using Mesh = omath::source_engine::Mesh;
|
||||
using Collider = omath::source_engine::MeshCollider;
|
||||
using Gjk = omath::collision::GjkAlgorithm<Collider>;
|
||||
using Vec3 = omath::Vector3<float>;
|
||||
|
||||
namespace
|
||||
{
|
||||
// Unit cube [-1, 1]^3 in local space.
|
||||
const std::vector<omath::primitives::Vertex<>> k_cube_vbo = {
|
||||
{ { -1.f, -1.f, -1.f }, {}, {} },
|
||||
{ { -1.f, -1.f, 1.f }, {}, {} },
|
||||
{ { -1.f, 1.f, -1.f }, {}, {} },
|
||||
{ { -1.f, 1.f, 1.f }, {}, {} },
|
||||
{ { 1.f, 1.f, 1.f }, {}, {} },
|
||||
{ { 1.f, 1.f, -1.f }, {}, {} },
|
||||
{ { 1.f, -1.f, 1.f }, {}, {} },
|
||||
{ { 1.f, -1.f, -1.f }, {}, {} },
|
||||
};
|
||||
const std::vector<omath::Vector3<std::uint32_t>> k_empty_ebo{};
|
||||
|
||||
Collider make_cube(const Vec3& origin = {}, const Vec3& scale = { 1, 1, 1 })
|
||||
{
|
||||
Mesh m{ k_cube_vbo, k_empty_ebo, scale };
|
||||
m.set_origin(origin);
|
||||
return Collider{ m };
|
||||
}
|
||||
} // namespace
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Separation — expect false
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST(GjkComprehensive, Separated_AlongPosX)
|
||||
{
|
||||
// A extends to x=1, B starts at x=1.1 → clear gap
|
||||
EXPECT_FALSE(Gjk::is_collide(make_cube({ 0, 0, 0 }), make_cube({ 2.1f, 0, 0 })));
|
||||
}
|
||||
|
||||
TEST(GjkComprehensive, Separated_AlongNegX)
|
||||
{
|
||||
// B to the left of A
|
||||
EXPECT_FALSE(Gjk::is_collide(make_cube({ 0, 0, 0 }), make_cube({ -2.1f, 0, 0 })));
|
||||
}
|
||||
|
||||
TEST(GjkComprehensive, Separated_AlongPosY)
|
||||
{
|
||||
EXPECT_FALSE(Gjk::is_collide(make_cube({ 0, 0, 0 }), make_cube({ 0, 2.1f, 0 })));
|
||||
}
|
||||
|
||||
TEST(GjkComprehensive, Separated_AlongNegY)
|
||||
{
|
||||
EXPECT_FALSE(Gjk::is_collide(make_cube({ 0, 0, 0 }), make_cube({ 0, -2.1f, 0 })));
|
||||
}
|
||||
|
||||
TEST(GjkComprehensive, Separated_AlongPosZ)
|
||||
{
|
||||
EXPECT_FALSE(Gjk::is_collide(make_cube({ 0, 0, 0 }), make_cube({ 0, 0, 2.1f })));
|
||||
}
|
||||
|
||||
TEST(GjkComprehensive, Separated_AlongNegZ)
|
||||
{
|
||||
EXPECT_FALSE(Gjk::is_collide(make_cube({ 0, 0, 0 }), make_cube({ 0, 0, -2.1f })));
|
||||
}
|
||||
|
||||
TEST(GjkComprehensive, Separated_AlongDiagonal)
|
||||
{
|
||||
// All components exceed 2.0 — no overlap on any axis
|
||||
EXPECT_FALSE(Gjk::is_collide(make_cube({ 0, 0, 0 }), make_cube({ 2.1f, 2.1f, 2.1f })));
|
||||
}
|
||||
|
||||
TEST(GjkComprehensive, Separated_LargeDistance)
|
||||
{
|
||||
EXPECT_FALSE(Gjk::is_collide(make_cube({ 0, 0, 0 }), make_cube({ 100.f, 0, 0 })));
|
||||
}
|
||||
|
||||
TEST(GjkComprehensive, Separated_AsymmetricSizes)
|
||||
{
|
||||
// Big (scale 2, half-ext 2), small (scale 0.5, half-ext 0.5) at 2.6 → gap of 0.1
|
||||
EXPECT_FALSE(Gjk::is_collide(make_cube({ 0, 0, 0 }, { 2, 2, 2 }), make_cube({ 2.6f, 0, 0 }, { 0.5f, 0.5f, 0.5f })));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Overlap — expect true
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST(GjkComprehensive, Overlapping_AlongPosX)
|
||||
{
|
||||
// B offset 1.5 → overlap depth 0.5 in X
|
||||
EXPECT_TRUE(Gjk::is_collide(make_cube({ 0, 0, 0 }), make_cube({ 1.5f, 0, 0 })));
|
||||
}
|
||||
|
||||
TEST(GjkComprehensive, Overlapping_AlongNegX)
|
||||
{
|
||||
EXPECT_TRUE(Gjk::is_collide(make_cube({ 0, 0, 0 }), make_cube({ -1.5f, 0, 0 })));
|
||||
}
|
||||
|
||||
TEST(GjkComprehensive, Overlapping_AlongPosZ)
|
||||
{
|
||||
EXPECT_TRUE(Gjk::is_collide(make_cube({ 0, 0, 0 }), make_cube({ 0, 0, 1.5f })));
|
||||
}
|
||||
|
||||
TEST(GjkComprehensive, Overlapping_AlongNegZ)
|
||||
{
|
||||
EXPECT_TRUE(Gjk::is_collide(make_cube({ 0, 0, 0 }), make_cube({ 0, 0, -1.5f })));
|
||||
}
|
||||
|
||||
TEST(GjkComprehensive, Overlapping_AlongDiagonalXY)
|
||||
{
|
||||
// Minkowski sum extends ±2 on each axis; offset (1,1,0) is inside
|
||||
EXPECT_TRUE(Gjk::is_collide(make_cube({ 0, 0, 0 }), make_cube({ 1.f, 1.f, 0.f })));
|
||||
}
|
||||
|
||||
TEST(GjkComprehensive, Overlapping_AlongDiagonalXYZ)
|
||||
{
|
||||
// All three axes overlap: (1,1,1) is inside the Minkowski sum
|
||||
EXPECT_TRUE(Gjk::is_collide(make_cube({ 0, 0, 0 }), make_cube({ 1.f, 1.f, 1.f })));
|
||||
}
|
||||
|
||||
TEST(GjkComprehensive, FullyNested_SmallInsideBig)
|
||||
{
|
||||
// Small cube (half-ext 0.5) fully inside big cube (half-ext 2)
|
||||
EXPECT_TRUE(Gjk::is_collide(make_cube({ 0, 0, 0 }, { 2, 2, 2 }), make_cube({ 0, 0, 0 }, { 0.5f, 0.5f, 0.5f })));
|
||||
}
|
||||
|
||||
TEST(GjkComprehensive, FullyNested_OffCenter)
|
||||
{
|
||||
// Small at (0.5, 0, 0) still fully inside big (half-ext 2)
|
||||
EXPECT_TRUE(Gjk::is_collide(make_cube({ 0, 0, 0 }, { 2, 2, 2 }), make_cube({ 0.5f, 0, 0 }, { 0.5f, 0.5f, 0.5f })));
|
||||
}
|
||||
|
||||
TEST(GjkComprehensive, Overlapping_AsymmetricSizes)
|
||||
{
|
||||
// Big (scale 2, half-ext 2) and small (scale 0.5, half-ext 0.5) at 2.0 → overlap 0.5 in X
|
||||
EXPECT_TRUE(Gjk::is_collide(make_cube({ 0, 0, 0 }, { 2, 2, 2 }), make_cube({ 2.0f, 0, 0 }, { 0.5f, 0.5f, 0.5f })));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Boundary cases
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST(GjkComprehensive, BoundaryCase_JustColliding)
|
||||
{
|
||||
// B at 1.999 — 0.001 overlap in X
|
||||
EXPECT_TRUE(Gjk::is_collide(make_cube({ 0, 0, 0 }), make_cube({ 1.999f, 0, 0 })));
|
||||
}
|
||||
|
||||
TEST(GjkComprehensive, BoundaryCase_JustSeparated)
|
||||
{
|
||||
// B at 2.001 — 0.001 gap in X
|
||||
EXPECT_FALSE(Gjk::is_collide(make_cube({ 0, 0, 0 }), make_cube({ 2.001f, 0, 0 })));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Symmetry
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST(GjkComprehensive, Symmetry_WhenColliding)
|
||||
{
|
||||
const auto a = make_cube({ 0, 0, 0 });
|
||||
const auto b = make_cube({ 1.5f, 0, 0 });
|
||||
EXPECT_EQ(Gjk::is_collide(a, b), Gjk::is_collide(b, a));
|
||||
}
|
||||
|
||||
TEST(GjkComprehensive, Symmetry_WhenSeparated)
|
||||
{
|
||||
const auto a = make_cube({ 0, 0, 0 });
|
||||
const auto b = make_cube({ 2.1f, 0.5f, 0 });
|
||||
EXPECT_EQ(Gjk::is_collide(a, b), Gjk::is_collide(b, a));
|
||||
}
|
||||
|
||||
TEST(GjkComprehensive, Symmetry_DiagonalSeparation)
|
||||
{
|
||||
const auto a = make_cube({ 0, 0, 0 });
|
||||
const auto b = make_cube({ 1.5f, 1.5f, 1.5f });
|
||||
EXPECT_EQ(Gjk::is_collide(a, b), Gjk::is_collide(b, a));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Simplex info
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST(GjkComprehensive, SimplexInfo_HitProducesSimplex4)
|
||||
{
|
||||
// On collision the simplex must be a full tetrahedron (4 points)
|
||||
const auto [hit, simplex] = Gjk::is_collide_with_simplex_info(make_cube({ 0, 0, 0 }), make_cube({ 0.5f, 0, 0 }));
|
||||
EXPECT_TRUE(hit);
|
||||
EXPECT_EQ(simplex.size(), 4u);
|
||||
}
|
||||
|
||||
TEST(GjkComprehensive, SimplexInfo_MissProducesLessThan4)
|
||||
{
|
||||
// On non-collision the simplex can never be a full tetrahedron
|
||||
const auto [hit, simplex] = Gjk::is_collide_with_simplex_info(make_cube({ 0, 0, 0 }), make_cube({ 2.1f, 0, 0 }));
|
||||
EXPECT_FALSE(hit);
|
||||
EXPECT_LT(simplex.size(), 4u);
|
||||
}
|
||||
|
||||
TEST(GjkComprehensive, SimplexInfo_HitAlongY)
|
||||
{
|
||||
const auto [hit, simplex] = Gjk::is_collide_with_simplex_info(make_cube({ 0, 0, 0 }), make_cube({ 0, 1.5f, 0 }));
|
||||
EXPECT_TRUE(hit);
|
||||
EXPECT_EQ(simplex.size(), 4u);
|
||||
}
|
||||
|
||||
TEST(GjkComprehensive, SimplexInfo_HitAlongZ)
|
||||
{
|
||||
const auto [hit, simplex] = Gjk::is_collide_with_simplex_info(make_cube({ 0, 0, 0 }), make_cube({ 0, 0, 1.5f }));
|
||||
EXPECT_TRUE(hit);
|
||||
EXPECT_EQ(simplex.size(), 4u);
|
||||
}
|
||||
|
||||
TEST(GjkComprehensive, SimplexInfo_MissAlongDiagonal)
|
||||
{
|
||||
const auto [hit, simplex] = Gjk::is_collide_with_simplex_info(make_cube({ 0, 0, 0 }), make_cube({ 2.1f, 2.1f, 2.1f }));
|
||||
EXPECT_FALSE(hit);
|
||||
EXPECT_LT(simplex.size(), 4u);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Non-trivial geometry — tetrahedron shaped colliders
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST(GjkComprehensive, TetrahedronShapes_Overlapping)
|
||||
{
|
||||
// A rough tetrahedron mesh; two of them close enough to overlap
|
||||
const std::vector<omath::primitives::Vertex<>> tet_vbo = {
|
||||
{ { 0.f, 1.f, 0.f }, {}, {} },
|
||||
{ { -1.f, -1.f, 1.f }, {}, {} },
|
||||
{ { 1.f, -1.f, 1.f }, {}, {} },
|
||||
{ { 0.f, -1.f, -1.f }, {}, {} },
|
||||
};
|
||||
|
||||
Mesh m_a{ tet_vbo, k_empty_ebo };
|
||||
Mesh m_b{ tet_vbo, k_empty_ebo };
|
||||
m_b.set_origin({ 0.5f, 0.f, 0.f });
|
||||
|
||||
EXPECT_TRUE(Gjk::is_collide(Collider{ m_a }, Collider{ m_b }));
|
||||
}
|
||||
|
||||
TEST(GjkComprehensive, TetrahedronShapes_Separated)
|
||||
{
|
||||
const std::vector<omath::primitives::Vertex<>> tet_vbo = {
|
||||
{ { 0.f, 1.f, 0.f }, {}, {} },
|
||||
{ { -1.f, -1.f, 1.f }, {}, {} },
|
||||
{ { 1.f, -1.f, 1.f }, {}, {} },
|
||||
{ { 0.f, -1.f, -1.f }, {}, {} },
|
||||
};
|
||||
|
||||
Mesh m_a{ tet_vbo, k_empty_ebo };
|
||||
Mesh m_b{ tet_vbo, k_empty_ebo };
|
||||
m_b.set_origin({ 3.f, 0.f, 0.f });
|
||||
|
||||
EXPECT_FALSE(Gjk::is_collide(Collider{ m_a }, Collider{ m_b }));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Determinism
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST(GjkComprehensive, Deterministic_SameResultOnRepeatedCalls)
|
||||
{
|
||||
const auto a = make_cube({ 0, 0, 0 });
|
||||
const auto b = make_cube({ 1.2f, 0.3f, 0.1f });
|
||||
const bool first = Gjk::is_collide(a, b);
|
||||
for (int i = 0; i < 10; ++i)
|
||||
EXPECT_EQ(Gjk::is_collide(a, b), first);
|
||||
}
|
||||
@@ -7,19 +7,18 @@ using namespace omath::pathfinding;
|
||||
TEST(NavigationMeshTests, SerializeDeserializeRoundTrip)
|
||||
{
|
||||
NavigationMesh nav;
|
||||
Vector3<float> a{0.f,0.f,0.f};
|
||||
Vector3<float> b{1.f,0.f,0.f};
|
||||
Vector3<float> c{0.f,1.f,0.f};
|
||||
Vector3<float> a{0.f, 0.f, 0.f};
|
||||
Vector3<float> b{1.f, 0.f, 0.f};
|
||||
Vector3<float> c{0.f, 1.f, 0.f};
|
||||
|
||||
nav.m_vertex_map.emplace(a, std::vector<Vector3<float>>{b,c});
|
||||
nav.m_vertex_map.emplace(a, std::vector<Vector3<float>>{b, c});
|
||||
nav.m_vertex_map.emplace(b, std::vector<Vector3<float>>{a});
|
||||
nav.m_vertex_map.emplace(c, std::vector<Vector3<float>>{a});
|
||||
|
||||
auto data = nav.serialize();
|
||||
std::string data = nav.serialize();
|
||||
NavigationMesh nav2;
|
||||
EXPECT_NO_THROW(nav2.deserialize(data));
|
||||
|
||||
// verify neighbors preserved
|
||||
EXPECT_EQ(nav2.m_vertex_map.size(), nav.m_vertex_map.size());
|
||||
EXPECT_EQ(nav2.get_neighbors(a).size(), 2u);
|
||||
}
|
||||
@@ -27,7 +26,223 @@ TEST(NavigationMeshTests, SerializeDeserializeRoundTrip)
|
||||
TEST(NavigationMeshTests, GetClosestVertexWhenEmpty)
|
||||
{
|
||||
const NavigationMesh nav;
|
||||
constexpr Vector3<float> p{5.f,5.f,5.f};
|
||||
constexpr Vector3<float> p{5.f, 5.f, 5.f};
|
||||
const auto res = nav.get_closest_vertex(p);
|
||||
EXPECT_FALSE(res.has_value());
|
||||
}
|
||||
|
||||
TEST(NavigationMeshTests, SerializeEmptyMesh)
|
||||
{
|
||||
const NavigationMesh nav;
|
||||
const std::string data = nav.serialize();
|
||||
EXPECT_TRUE(data.empty());
|
||||
}
|
||||
|
||||
TEST(NavigationMeshTests, DeserializeEmptyString)
|
||||
{
|
||||
NavigationMesh nav;
|
||||
EXPECT_NO_THROW(nav.deserialize(""));
|
||||
EXPECT_TRUE(nav.empty());
|
||||
}
|
||||
|
||||
TEST(NavigationMeshTests, SerializeProducesHumanReadableText)
|
||||
{
|
||||
NavigationMesh nav;
|
||||
nav.m_vertex_map.emplace(Vector3<float>{1.f, 2.f, 3.f}, std::vector<Vector3<float>>{{4.f, 5.f, 6.f}});
|
||||
|
||||
const std::string data = nav.serialize();
|
||||
|
||||
// Must contain the vertex and neighbor coords as plain text
|
||||
EXPECT_NE(data.find("1"), std::string::npos);
|
||||
EXPECT_NE(data.find("2"), std::string::npos);
|
||||
EXPECT_NE(data.find("3"), std::string::npos);
|
||||
EXPECT_NE(data.find("4"), std::string::npos);
|
||||
EXPECT_NE(data.find("5"), std::string::npos);
|
||||
EXPECT_NE(data.find("6"), std::string::npos);
|
||||
}
|
||||
|
||||
TEST(NavigationMeshTests, DeserializeRestoresNeighborValues)
|
||||
{
|
||||
NavigationMesh nav;
|
||||
const Vector3<float> v{1.f, 2.f, 3.f};
|
||||
const Vector3<float> n1{4.f, 5.f, 6.f};
|
||||
const Vector3<float> n2{7.f, 8.f, 9.f};
|
||||
nav.m_vertex_map.emplace(v, std::vector<Vector3<float>>{n1, n2});
|
||||
|
||||
NavigationMesh nav2;
|
||||
nav2.deserialize(nav.serialize());
|
||||
|
||||
ASSERT_EQ(nav2.m_vertex_map.count(v), 1u);
|
||||
const auto& neighbors = nav2.get_neighbors(v);
|
||||
ASSERT_EQ(neighbors.size(), 2u);
|
||||
EXPECT_EQ(neighbors[0], n1);
|
||||
EXPECT_EQ(neighbors[1], n2);
|
||||
}
|
||||
|
||||
TEST(NavigationMeshTests, DeserializeOverwritesPreviousData)
|
||||
{
|
||||
NavigationMesh nav;
|
||||
const Vector3<float> v{1.f, 0.f, 0.f};
|
||||
nav.m_vertex_map.emplace(v, std::vector<Vector3<float>>{});
|
||||
|
||||
// Load a different mesh into the same object
|
||||
NavigationMesh other;
|
||||
const Vector3<float> a{10.f, 20.f, 30.f};
|
||||
other.m_vertex_map.emplace(a, std::vector<Vector3<float>>{});
|
||||
|
||||
nav.deserialize(other.serialize());
|
||||
|
||||
EXPECT_EQ(nav.m_vertex_map.size(), 1u);
|
||||
EXPECT_EQ(nav.m_vertex_map.count(v), 0u);
|
||||
EXPECT_EQ(nav.m_vertex_map.count(a), 1u);
|
||||
}
|
||||
|
||||
TEST(NavigationMeshTests, RoundTripNegativeAndFractionalCoords)
|
||||
{
|
||||
NavigationMesh nav;
|
||||
const Vector3<float> v{-1.5f, 0.25f, -3.75f};
|
||||
const Vector3<float> n{100.f, -200.f, 0.001f};
|
||||
nav.m_vertex_map.emplace(v, std::vector<Vector3<float>>{n});
|
||||
|
||||
NavigationMesh nav2;
|
||||
nav2.deserialize(nav.serialize());
|
||||
|
||||
ASSERT_EQ(nav2.m_vertex_map.count(v), 1u);
|
||||
const auto& neighbors = nav2.get_neighbors(v);
|
||||
ASSERT_EQ(neighbors.size(), 1u);
|
||||
EXPECT_NEAR(neighbors[0].x, n.x, 1e-3f);
|
||||
EXPECT_NEAR(neighbors[0].y, n.y, 1e-3f);
|
||||
EXPECT_NEAR(neighbors[0].z, n.z, 1e-3f);
|
||||
}
|
||||
|
||||
TEST(NavigationMeshTests, GetClosestVertexReturnsNearest)
|
||||
{
|
||||
NavigationMesh nav;
|
||||
const Vector3<float> a{0.f, 0.f, 0.f};
|
||||
const Vector3<float> b{10.f, 0.f, 0.f};
|
||||
nav.m_vertex_map.emplace(a, std::vector<Vector3<float>>{});
|
||||
nav.m_vertex_map.emplace(b, std::vector<Vector3<float>>{});
|
||||
|
||||
const auto res = nav.get_closest_vertex({1.f, 0.f, 0.f});
|
||||
ASSERT_TRUE(res.has_value());
|
||||
EXPECT_EQ(res.value(), a);
|
||||
}
|
||||
|
||||
TEST(NavigationMeshTests, VertexWithNoNeighborsRoundTrip)
|
||||
{
|
||||
NavigationMesh nav;
|
||||
const Vector3<float> v{5.f, 5.f, 5.f};
|
||||
nav.m_vertex_map.emplace(v, std::vector<Vector3<float>>{});
|
||||
|
||||
NavigationMesh nav2;
|
||||
nav2.deserialize(nav.serialize());
|
||||
|
||||
ASSERT_EQ(nav2.m_vertex_map.count(v), 1u);
|
||||
EXPECT_TRUE(nav2.get_neighbors(v).empty());
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Vertex events
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
TEST(NavigationMeshTests, SetEventOnNonExistentVertexThrows)
|
||||
{
|
||||
NavigationMesh nav;
|
||||
const Vector3<float> v{99.f, 99.f, 99.f};
|
||||
EXPECT_THROW(nav.set_event(v, "jump"), std::invalid_argument);
|
||||
}
|
||||
|
||||
TEST(NavigationMeshTests, EventNotSetByDefault)
|
||||
{
|
||||
NavigationMesh nav;
|
||||
const Vector3<float> v{0.f, 0.f, 0.f};
|
||||
nav.m_vertex_map.emplace(v, std::vector<Vector3<float>>{});
|
||||
|
||||
EXPECT_FALSE(nav.get_event(v).has_value());
|
||||
}
|
||||
|
||||
TEST(NavigationMeshTests, SetAndGetEvent)
|
||||
{
|
||||
NavigationMesh nav;
|
||||
const Vector3<float> v{1.f, 0.f, 0.f};
|
||||
nav.m_vertex_map.emplace(v, std::vector<Vector3<float>>{});
|
||||
nav.set_event(v, "jump");
|
||||
|
||||
const auto event = nav.get_event(v);
|
||||
ASSERT_TRUE(event.has_value());
|
||||
EXPECT_EQ(event.value(), "jump");
|
||||
}
|
||||
|
||||
TEST(NavigationMeshTests, OverwriteEvent)
|
||||
{
|
||||
NavigationMesh nav;
|
||||
const Vector3<float> v{1.f, 0.f, 0.f};
|
||||
nav.m_vertex_map.emplace(v, std::vector<Vector3<float>>{});
|
||||
nav.set_event(v, "jump");
|
||||
nav.set_event(v, "teleport");
|
||||
|
||||
EXPECT_EQ(nav.get_event(v).value(), "teleport");
|
||||
}
|
||||
|
||||
TEST(NavigationMeshTests, ClearEvent)
|
||||
{
|
||||
NavigationMesh nav;
|
||||
const Vector3<float> v{1.f, 0.f, 0.f};
|
||||
nav.m_vertex_map.emplace(v, std::vector<Vector3<float>>{});
|
||||
nav.set_event(v, "jump");
|
||||
nav.clear_event(v);
|
||||
|
||||
EXPECT_FALSE(nav.get_event(v).has_value());
|
||||
}
|
||||
|
||||
TEST(NavigationMeshTests, EventRoundTripSerialization)
|
||||
{
|
||||
NavigationMesh nav;
|
||||
const Vector3<float> a{0.f, 0.f, 0.f};
|
||||
const Vector3<float> b{1.f, 0.f, 0.f};
|
||||
nav.m_vertex_map.emplace(a, std::vector<Vector3<float>>{b});
|
||||
nav.m_vertex_map.emplace(b, std::vector<Vector3<float>>{});
|
||||
nav.set_event(b, "jump");
|
||||
|
||||
NavigationMesh nav2;
|
||||
nav2.deserialize(nav.serialize());
|
||||
|
||||
ASSERT_FALSE(nav2.get_event(a).has_value());
|
||||
ASSERT_TRUE(nav2.get_event(b).has_value());
|
||||
EXPECT_EQ(nav2.get_event(b).value(), "jump");
|
||||
}
|
||||
|
||||
TEST(NavigationMeshTests, MultipleEventsRoundTrip)
|
||||
{
|
||||
NavigationMesh nav;
|
||||
const Vector3<float> a{0.f, 0.f, 0.f};
|
||||
const Vector3<float> b{1.f, 0.f, 0.f};
|
||||
const Vector3<float> c{2.f, 0.f, 0.f};
|
||||
nav.m_vertex_map.emplace(a, std::vector<Vector3<float>>{});
|
||||
nav.m_vertex_map.emplace(b, std::vector<Vector3<float>>{});
|
||||
nav.m_vertex_map.emplace(c, std::vector<Vector3<float>>{});
|
||||
nav.set_event(a, "spawn");
|
||||
nav.set_event(c, "teleport");
|
||||
|
||||
NavigationMesh nav2;
|
||||
nav2.deserialize(nav.serialize());
|
||||
|
||||
EXPECT_EQ(nav2.get_event(a).value(), "spawn");
|
||||
EXPECT_FALSE(nav2.get_event(b).has_value());
|
||||
EXPECT_EQ(nav2.get_event(c).value(), "teleport");
|
||||
}
|
||||
|
||||
TEST(NavigationMeshTests, DeserializeClearsOldEvents)
|
||||
{
|
||||
NavigationMesh nav;
|
||||
const Vector3<float> v{0.f, 0.f, 0.f};
|
||||
nav.m_vertex_map.emplace(v, std::vector<Vector3<float>>{});
|
||||
nav.set_event(v, "jump");
|
||||
|
||||
// Deserialize a mesh that has no events
|
||||
NavigationMesh empty_events;
|
||||
empty_events.m_vertex_map.emplace(v, std::vector<Vector3<float>>{});
|
||||
|
||||
nav.deserialize(empty_events.serialize());
|
||||
EXPECT_FALSE(nav.get_event(v).has_value());
|
||||
}
|
||||
|
||||
341
tests/general/unit_test_physx_colliders.cpp
Normal file
341
tests/general/unit_test_physx_colliders.cpp
Normal file
@@ -0,0 +1,341 @@
|
||||
//
|
||||
// Created by orange-cpp
|
||||
//
|
||||
#ifdef OMATH_ENABLE_PHYSX
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <omath/collision/gjk_algorithm.hpp>
|
||||
#include <omath/collision/physx_box_collider.hpp>
|
||||
#include <omath/collision/physx_rigid_body.hpp>
|
||||
#include <omath/collision/physx_sphere_collider.hpp>
|
||||
#include <omath/collision/physx_world.hpp>
|
||||
|
||||
using namespace omath::collision;
|
||||
using omath::Vector3;
|
||||
|
||||
// ─── PhysXBoxCollider ────────────────────────────────────────────────────────
|
||||
|
||||
TEST(PhysXBoxCollider, DefaultOriginIsZero)
|
||||
{
|
||||
PhysXBoxCollider box({1.f, 1.f, 1.f});
|
||||
EXPECT_EQ(box.get_origin(), Vector3<float>(0.f, 0.f, 0.f));
|
||||
}
|
||||
|
||||
TEST(PhysXBoxCollider, SetAndGetOrigin)
|
||||
{
|
||||
PhysXBoxCollider box({1.f, 1.f, 1.f}, {3.f, 4.f, 5.f});
|
||||
EXPECT_EQ(box.get_origin(), Vector3<float>(3.f, 4.f, 5.f));
|
||||
|
||||
box.set_origin({-1.f, 0.f, 2.f});
|
||||
EXPECT_EQ(box.get_origin(), Vector3<float>(-1.f, 0.f, 2.f));
|
||||
}
|
||||
|
||||
TEST(PhysXBoxCollider, FurthestPointPositiveDirection)
|
||||
{
|
||||
// Box centred at origin with half-extents (2, 3, 4).
|
||||
// Direction (+x, +y, +z) → furthest corner is (+2, +3, +4).
|
||||
PhysXBoxCollider box({2.f, 3.f, 4.f});
|
||||
const auto p = box.find_abs_furthest_vertex_position({1.f, 1.f, 1.f});
|
||||
EXPECT_FLOAT_EQ(p.x, 2.f);
|
||||
EXPECT_FLOAT_EQ(p.y, 3.f);
|
||||
EXPECT_FLOAT_EQ(p.z, 4.f);
|
||||
}
|
||||
|
||||
TEST(PhysXBoxCollider, FurthestPointNegativeDirection)
|
||||
{
|
||||
// Direction (-x, -y, -z) → furthest corner is (-2, -3, -4).
|
||||
PhysXBoxCollider box({2.f, 3.f, 4.f});
|
||||
const auto p = box.find_abs_furthest_vertex_position({-1.f, -1.f, -1.f});
|
||||
EXPECT_FLOAT_EQ(p.x, -2.f);
|
||||
EXPECT_FLOAT_EQ(p.y, -3.f);
|
||||
EXPECT_FLOAT_EQ(p.z, -4.f);
|
||||
}
|
||||
|
||||
TEST(PhysXBoxCollider, FurthestPointMixedDirection)
|
||||
{
|
||||
// Direction (+x, -y, +z) → furthest corner is (+2, -3, +4).
|
||||
PhysXBoxCollider box({2.f, 3.f, 4.f});
|
||||
const auto p = box.find_abs_furthest_vertex_position({1.f, -1.f, 1.f});
|
||||
EXPECT_FLOAT_EQ(p.x, 2.f);
|
||||
EXPECT_FLOAT_EQ(p.y, -3.f);
|
||||
EXPECT_FLOAT_EQ(p.z, 4.f);
|
||||
}
|
||||
|
||||
TEST(PhysXBoxCollider, FurthestPointWithNonZeroOrigin)
|
||||
{
|
||||
// Box at (10, 0, 0), half-extents (1, 1, 1). Direction +x → (11, 1, 1).
|
||||
PhysXBoxCollider box({1.f, 1.f, 1.f}, {10.f, 0.f, 0.f});
|
||||
const auto p = box.find_abs_furthest_vertex_position({1.f, 1.f, 1.f});
|
||||
EXPECT_FLOAT_EQ(p.x, 11.f);
|
||||
EXPECT_FLOAT_EQ(p.y, 1.f);
|
||||
EXPECT_FLOAT_EQ(p.z, 1.f);
|
||||
}
|
||||
|
||||
TEST(PhysXBoxCollider, SetHalfExtentsUpdatesGeometry)
|
||||
{
|
||||
PhysXBoxCollider box({1.f, 1.f, 1.f});
|
||||
box.set_half_extents({5.f, 6.f, 7.f});
|
||||
|
||||
const auto& he = box.get_geometry().halfExtents;
|
||||
EXPECT_FLOAT_EQ(he.x, 5.f);
|
||||
EXPECT_FLOAT_EQ(he.y, 6.f);
|
||||
EXPECT_FLOAT_EQ(he.z, 7.f);
|
||||
|
||||
// Furthest vertex must reflect the new extents.
|
||||
const auto p = box.find_abs_furthest_vertex_position({1.f, 1.f, 1.f});
|
||||
EXPECT_FLOAT_EQ(p.x, 5.f);
|
||||
EXPECT_FLOAT_EQ(p.y, 6.f);
|
||||
EXPECT_FLOAT_EQ(p.z, 7.f);
|
||||
}
|
||||
|
||||
// ─── PhysXSphereCollider ─────────────────────────────────────────────────────
|
||||
|
||||
TEST(PhysXSphereCollider, DefaultOriginIsZero)
|
||||
{
|
||||
PhysXSphereCollider sphere(1.f);
|
||||
EXPECT_EQ(sphere.get_origin(), Vector3<float>(0.f, 0.f, 0.f));
|
||||
}
|
||||
|
||||
TEST(PhysXSphereCollider, SetAndGetOrigin)
|
||||
{
|
||||
PhysXSphereCollider sphere(1.f, {1.f, 2.f, 3.f});
|
||||
EXPECT_EQ(sphere.get_origin(), Vector3<float>(1.f, 2.f, 3.f));
|
||||
|
||||
sphere.set_origin({-5.f, 0.f, 0.f});
|
||||
EXPECT_EQ(sphere.get_origin(), Vector3<float>(-5.f, 0.f, 0.f));
|
||||
}
|
||||
|
||||
TEST(PhysXSphereCollider, FurthestPointAlongPureXAxis)
|
||||
{
|
||||
// Direction (1,0,0), radius 3 → furthest point is (3, 0, 0).
|
||||
PhysXSphereCollider sphere(3.f);
|
||||
const auto p = sphere.find_abs_furthest_vertex_position({1.f, 0.f, 0.f});
|
||||
EXPECT_FLOAT_EQ(p.x, 3.f);
|
||||
EXPECT_FLOAT_EQ(p.y, 0.f);
|
||||
EXPECT_FLOAT_EQ(p.z, 0.f);
|
||||
}
|
||||
|
||||
TEST(PhysXSphereCollider, FurthestPointAlongDiagonal)
|
||||
{
|
||||
// Direction (1,1,0), radius 1 → furthest point at distance 1 from origin.
|
||||
PhysXSphereCollider sphere(1.f);
|
||||
const auto p = sphere.find_abs_furthest_vertex_position({1.f, 1.f, 0.f});
|
||||
const float dist = std::sqrt(p.x * p.x + p.y * p.y + p.z * p.z);
|
||||
EXPECT_NEAR(dist, 1.f, 1e-5f);
|
||||
}
|
||||
|
||||
TEST(PhysXSphereCollider, FurthestPointWithNonZeroOrigin)
|
||||
{
|
||||
// Sphere at (5, 0, 0), radius 2. Direction +x → (7, 0, 0).
|
||||
PhysXSphereCollider sphere(2.f, {5.f, 0.f, 0.f});
|
||||
const auto p = sphere.find_abs_furthest_vertex_position({1.f, 0.f, 0.f});
|
||||
EXPECT_FLOAT_EQ(p.x, 7.f);
|
||||
EXPECT_FLOAT_EQ(p.y, 0.f);
|
||||
EXPECT_FLOAT_EQ(p.z, 0.f);
|
||||
}
|
||||
|
||||
TEST(PhysXSphereCollider, ZeroDirectionReturnsOrigin)
|
||||
{
|
||||
PhysXSphereCollider sphere(5.f, {1.f, 2.f, 3.f});
|
||||
const auto p = sphere.find_abs_furthest_vertex_position({0.f, 0.f, 0.f});
|
||||
EXPECT_EQ(p, sphere.get_origin());
|
||||
}
|
||||
|
||||
TEST(PhysXSphereCollider, SetRadiusUpdatesGeometry)
|
||||
{
|
||||
PhysXSphereCollider sphere(1.f);
|
||||
sphere.set_radius(10.f);
|
||||
EXPECT_FLOAT_EQ(sphere.get_radius(), 10.f);
|
||||
|
||||
// Furthest point along +x should now be at x = 10.
|
||||
const auto p = sphere.find_abs_furthest_vertex_position({1.f, 0.f, 0.f});
|
||||
EXPECT_FLOAT_EQ(p.x, 10.f);
|
||||
}
|
||||
|
||||
// ─── GJK: Box vs Box ─────────────────────────────────────────────────────────
|
||||
|
||||
using GjkBox = omath::collision::GjkAlgorithm<PhysXBoxCollider>;
|
||||
using GjkSphere = omath::collision::GjkAlgorithm<PhysXSphereCollider>;
|
||||
|
||||
TEST(PhysXBoxGjk, CollidingOverlap)
|
||||
{
|
||||
// Two unit boxes: A at origin, B shifted by 0.5 — clearly overlapping.
|
||||
const PhysXBoxCollider a({1.f, 1.f, 1.f});
|
||||
const PhysXBoxCollider b({1.f, 1.f, 1.f}, {0.5f, 0.f, 0.f});
|
||||
EXPECT_TRUE(GjkBox::is_collide(a, b));
|
||||
}
|
||||
|
||||
TEST(PhysXBoxGjk, NotCollidingTouching)
|
||||
{
|
||||
// Boxes exactly touching on the +X face: A[-1,1] and B[1,3] along X.
|
||||
// GJK treats boundary contact (Minkowski difference passes through origin) as non-collision.
|
||||
const PhysXBoxCollider a({1.f, 1.f, 1.f});
|
||||
const PhysXBoxCollider b({1.f, 1.f, 1.f}, {2.f, 0.f, 0.f});
|
||||
EXPECT_FALSE(GjkBox::is_collide(a, b));
|
||||
}
|
||||
|
||||
TEST(PhysXBoxGjk, CollidingSlightOverlap)
|
||||
{
|
||||
// Boxes overlapping by 0.1 along X: A[-1,1] and B[0.9,2.9].
|
||||
const PhysXBoxCollider a({1.f, 1.f, 1.f});
|
||||
const PhysXBoxCollider b({1.f, 1.f, 1.f}, {1.9f, 0.f, 0.f});
|
||||
EXPECT_TRUE(GjkBox::is_collide(a, b));
|
||||
}
|
||||
|
||||
TEST(PhysXBoxGjk, NotCollidingSeparated)
|
||||
{
|
||||
// Boxes separated by a gap: A[-1,1] and B[3,5] along X.
|
||||
const PhysXBoxCollider a({1.f, 1.f, 1.f});
|
||||
const PhysXBoxCollider b({1.f, 1.f, 1.f}, {4.f, 0.f, 0.f});
|
||||
EXPECT_FALSE(GjkBox::is_collide(a, b));
|
||||
}
|
||||
|
||||
TEST(PhysXBoxGjk, CollidingSameOrigin)
|
||||
{
|
||||
// Same position — fully overlapping.
|
||||
const PhysXBoxCollider a({1.f, 1.f, 1.f});
|
||||
const PhysXBoxCollider b({1.f, 1.f, 1.f});
|
||||
EXPECT_TRUE(GjkBox::is_collide(a, b));
|
||||
}
|
||||
|
||||
TEST(PhysXBoxGjk, NotCollidingDiagonalSeparation)
|
||||
{
|
||||
// Boxes separated along a diagonal so no axis-aligned faces overlap.
|
||||
const PhysXBoxCollider a({1.f, 1.f, 1.f});
|
||||
const PhysXBoxCollider b({1.f, 1.f, 1.f}, {3.f, 3.f, 3.f});
|
||||
EXPECT_FALSE(GjkBox::is_collide(a, b));
|
||||
}
|
||||
|
||||
TEST(PhysXBoxGjk, DifferentSizesColliding)
|
||||
{
|
||||
// Large box vs small box inside it.
|
||||
const PhysXBoxCollider large({5.f, 5.f, 5.f});
|
||||
const PhysXBoxCollider small_box({1.f, 1.f, 1.f}, {2.f, 0.f, 0.f});
|
||||
EXPECT_TRUE(GjkBox::is_collide(large, small_box));
|
||||
}
|
||||
|
||||
// ─── GJK: Sphere vs Sphere ───────────────────────────────────────────────────
|
||||
|
||||
TEST(PhysXSphereGjk, CollidingOverlap)
|
||||
{
|
||||
// Radii 1 each, centres 1 apart — overlapping.
|
||||
const PhysXSphereCollider a(1.f);
|
||||
const PhysXSphereCollider b(1.f, {1.f, 0.f, 0.f});
|
||||
EXPECT_TRUE(GjkSphere::is_collide(a, b));
|
||||
}
|
||||
|
||||
TEST(PhysXSphereGjk, CollidingSameOrigin)
|
||||
{
|
||||
const PhysXSphereCollider a(1.f);
|
||||
const PhysXSphereCollider b(1.f);
|
||||
EXPECT_TRUE(GjkSphere::is_collide(a, b));
|
||||
}
|
||||
|
||||
TEST(PhysXSphereGjk, NotCollidingSeparated)
|
||||
{
|
||||
// Radii 1 each, centres 3 apart — gap of 1.
|
||||
const PhysXSphereCollider a(1.f);
|
||||
const PhysXSphereCollider b(1.f, {3.f, 0.f, 0.f});
|
||||
EXPECT_FALSE(GjkSphere::is_collide(a, b));
|
||||
}
|
||||
|
||||
TEST(PhysXSphereGjk, DifferentRadiiColliding)
|
||||
{
|
||||
// r=2 and r=1, centres 2.5 apart — still overlapping.
|
||||
const PhysXSphereCollider a(2.f);
|
||||
const PhysXSphereCollider b(1.f, {2.5f, 0.f, 0.f});
|
||||
EXPECT_TRUE(GjkSphere::is_collide(a, b));
|
||||
}
|
||||
|
||||
TEST(PhysXSphereGjk, DifferentRadiiNotColliding)
|
||||
{
|
||||
// r=1 and r=1, centres 5 apart — separated.
|
||||
const PhysXSphereCollider a(1.f);
|
||||
const PhysXSphereCollider b(1.f, {5.f, 0.f, 0.f});
|
||||
EXPECT_FALSE(GjkSphere::is_collide(a, b));
|
||||
}
|
||||
|
||||
// ─── PhysX simulation-based collision resolution ─────────────────────────────
|
||||
|
||||
// Helper: step the world N times with a fixed dt.
|
||||
static void step_n(omath::collision::PhysXWorld& world, int n, float dt = 1.f / 60.f)
|
||||
{
|
||||
for (int i = 0; i < n; ++i)
|
||||
world.step(dt);
|
||||
}
|
||||
|
||||
TEST(PhysXSimulation, BoxFallsAndStopsOnGround)
|
||||
{
|
||||
// A box dropped from y=5 should come to rest at y≈0.5 (half-extent) above the ground plane.
|
||||
omath::collision::PhysXWorld world;
|
||||
world.add_ground_plane(0.f);
|
||||
|
||||
omath::collision::PhysXRigidBody box(world, physx::PxBoxGeometry(0.5f, 0.5f, 0.5f),
|
||||
{0.f, 5.f, 0.f});
|
||||
|
||||
step_n(world, 300); // ~5 simulated seconds
|
||||
|
||||
EXPECT_NEAR(box.get_origin().y, 0.5f, 0.05f);
|
||||
}
|
||||
|
||||
TEST(PhysXSimulation, SphereFallsAndStopsOnGround)
|
||||
{
|
||||
// A sphere of radius 1 dropped from y=5 should rest at y≈1.
|
||||
omath::collision::PhysXWorld world;
|
||||
world.add_ground_plane(0.f);
|
||||
|
||||
omath::collision::PhysXRigidBody sphere(world, physx::PxSphereGeometry(1.f),
|
||||
{0.f, 5.f, 0.f});
|
||||
|
||||
step_n(world, 300);
|
||||
|
||||
EXPECT_NEAR(sphere.get_origin().y, 1.f, 0.05f);
|
||||
}
|
||||
|
||||
TEST(PhysXSimulation, TwoBoxesCollideSeparate)
|
||||
{
|
||||
// Two boxes launched toward each other — after collision they must be
|
||||
// further apart than their combined half-extents (no overlap).
|
||||
omath::collision::PhysXWorld world({0.f, 0.f, 0.f}); // no gravity
|
||||
|
||||
omath::collision::PhysXRigidBody left (world, physx::PxBoxGeometry(0.5f, 0.5f, 0.5f), {-3.f, 0.f, 0.f});
|
||||
omath::collision::PhysXRigidBody right(world, physx::PxBoxGeometry(0.5f, 0.5f, 0.5f), { 3.f, 0.f, 0.f});
|
||||
|
||||
left.set_linear_velocity({ 5.f, 0.f, 0.f});
|
||||
right.set_linear_velocity({-5.f, 0.f, 0.f});
|
||||
|
||||
step_n(world, 120); // 2 simulated seconds
|
||||
|
||||
const float distance = right.get_origin().x - left.get_origin().x;
|
||||
// Boxes must not be overlapping (combined extents = 1.0).
|
||||
EXPECT_GE(distance, 1.0f);
|
||||
}
|
||||
|
||||
TEST(PhysXSimulation, BoxGetOriginMatchesSetOrigin)
|
||||
{
|
||||
// Kinematic teleport — set_origin must immediately reflect in get_origin.
|
||||
omath::collision::PhysXWorld world;
|
||||
omath::collision::PhysXRigidBody box(world, physx::PxBoxGeometry(1.f, 1.f, 1.f));
|
||||
box.set_kinematic(true);
|
||||
|
||||
box.set_origin({7.f, 3.f, -2.f});
|
||||
|
||||
EXPECT_NEAR(box.get_origin().x, 7.f, 1e-4f);
|
||||
EXPECT_NEAR(box.get_origin().y, 3.f, 1e-4f);
|
||||
EXPECT_NEAR(box.get_origin().z, -2.f, 1e-4f);
|
||||
}
|
||||
|
||||
TEST(PhysXSimulation, BoxFallsUnderGravity)
|
||||
{
|
||||
// Without a floor, a box should be lower after simulation than its start.
|
||||
omath::collision::PhysXWorld world; // default gravity -9.81 Y
|
||||
omath::collision::PhysXRigidBody box(world, physx::PxBoxGeometry(0.5f, 0.5f, 0.5f),
|
||||
{0.f, 10.f, 0.f});
|
||||
|
||||
const float y_start = box.get_origin().y;
|
||||
step_n(world, 60); // 1 simulated second
|
||||
|
||||
EXPECT_LT(box.get_origin().y, y_start);
|
||||
}
|
||||
|
||||
#endif // OMATH_ENABLE_PHYSX
|
||||
402
tests/general/unit_test_quaternion.cpp
Normal file
402
tests/general/unit_test_quaternion.cpp
Normal file
@@ -0,0 +1,402 @@
|
||||
//
|
||||
// Created by vlad on 3/1/2026.
|
||||
//
|
||||
#include <omath/linear_algebra/quaternion.hpp>
|
||||
#include <cmath>
|
||||
#include <gtest/gtest.h>
|
||||
#include <numbers>
|
||||
|
||||
using namespace omath;
|
||||
|
||||
static constexpr float kEps = 1e-5f;
|
||||
|
||||
// ── Helpers ──────────────────────────────────────────────────────────────────
|
||||
|
||||
static void expect_quat_near(const Quaternion<float>& a, const Quaternion<float>& b, float eps = kEps)
|
||||
{
|
||||
EXPECT_NEAR(a.x, b.x, eps);
|
||||
EXPECT_NEAR(a.y, b.y, eps);
|
||||
EXPECT_NEAR(a.z, b.z, eps);
|
||||
EXPECT_NEAR(a.w, b.w, eps);
|
||||
}
|
||||
|
||||
static void expect_vec3_near(const Vector3<float>& a, const Vector3<float>& b, float eps = kEps)
|
||||
{
|
||||
EXPECT_NEAR(a.x, b.x, eps);
|
||||
EXPECT_NEAR(a.y, b.y, eps);
|
||||
EXPECT_NEAR(a.z, b.z, eps);
|
||||
}
|
||||
|
||||
// ── Constructors ─────────────────────────────────────────────────────────────
|
||||
|
||||
TEST(Quaternion, DefaultConstructorIsIdentity)
|
||||
{
|
||||
constexpr Quaternion<float> q;
|
||||
EXPECT_FLOAT_EQ(q.x, 0.f);
|
||||
EXPECT_FLOAT_EQ(q.y, 0.f);
|
||||
EXPECT_FLOAT_EQ(q.z, 0.f);
|
||||
EXPECT_FLOAT_EQ(q.w, 1.f);
|
||||
}
|
||||
|
||||
TEST(Quaternion, ValueConstructor)
|
||||
{
|
||||
constexpr Quaternion<float> q{1.f, 2.f, 3.f, 4.f};
|
||||
EXPECT_FLOAT_EQ(q.x, 1.f);
|
||||
EXPECT_FLOAT_EQ(q.y, 2.f);
|
||||
EXPECT_FLOAT_EQ(q.z, 3.f);
|
||||
EXPECT_FLOAT_EQ(q.w, 4.f);
|
||||
}
|
||||
|
||||
TEST(Quaternion, DoubleInstantiation)
|
||||
{
|
||||
constexpr Quaternion<double> q{0.0, 0.0, 0.0, 1.0};
|
||||
EXPECT_DOUBLE_EQ(q.w, 1.0);
|
||||
}
|
||||
|
||||
// ── Equality ─────────────────────────────────────────────────────────────────
|
||||
|
||||
TEST(Quaternion, EqualityOperators)
|
||||
{
|
||||
constexpr Quaternion<float> a{1.f, 2.f, 3.f, 4.f};
|
||||
constexpr Quaternion<float> b{1.f, 2.f, 3.f, 4.f};
|
||||
constexpr Quaternion<float> c{1.f, 2.f, 3.f, 5.f};
|
||||
|
||||
EXPECT_TRUE(a == b);
|
||||
EXPECT_FALSE(a == c);
|
||||
EXPECT_FALSE(a != b);
|
||||
EXPECT_TRUE(a != c);
|
||||
}
|
||||
|
||||
// ── Arithmetic ───────────────────────────────────────────────────────────────
|
||||
|
||||
TEST(Quaternion, ScalarMultiply)
|
||||
{
|
||||
constexpr Quaternion<float> q{1.f, 2.f, 3.f, 4.f};
|
||||
constexpr auto r = q * 2.f;
|
||||
EXPECT_FLOAT_EQ(r.x, 2.f);
|
||||
EXPECT_FLOAT_EQ(r.y, 4.f);
|
||||
EXPECT_FLOAT_EQ(r.z, 6.f);
|
||||
EXPECT_FLOAT_EQ(r.w, 8.f);
|
||||
}
|
||||
|
||||
TEST(Quaternion, ScalarMultiplyAssign)
|
||||
{
|
||||
Quaternion<float> q{1.f, 2.f, 3.f, 4.f};
|
||||
q *= 3.f;
|
||||
EXPECT_FLOAT_EQ(q.x, 3.f);
|
||||
EXPECT_FLOAT_EQ(q.y, 6.f);
|
||||
EXPECT_FLOAT_EQ(q.z, 9.f);
|
||||
EXPECT_FLOAT_EQ(q.w, 12.f);
|
||||
}
|
||||
|
||||
TEST(Quaternion, Addition)
|
||||
{
|
||||
constexpr Quaternion<float> a{1.f, 2.f, 3.f, 4.f};
|
||||
constexpr Quaternion<float> b{4.f, 3.f, 2.f, 1.f};
|
||||
constexpr auto r = a + b;
|
||||
EXPECT_FLOAT_EQ(r.x, 5.f);
|
||||
EXPECT_FLOAT_EQ(r.y, 5.f);
|
||||
EXPECT_FLOAT_EQ(r.z, 5.f);
|
||||
EXPECT_FLOAT_EQ(r.w, 5.f);
|
||||
}
|
||||
|
||||
TEST(Quaternion, AdditionAssign)
|
||||
{
|
||||
Quaternion<float> a{1.f, 0.f, 0.f, 0.f};
|
||||
const Quaternion<float> b{0.f, 1.f, 0.f, 0.f};
|
||||
a += b;
|
||||
EXPECT_FLOAT_EQ(a.x, 1.f);
|
||||
EXPECT_FLOAT_EQ(a.y, 1.f);
|
||||
}
|
||||
|
||||
TEST(Quaternion, UnaryNegation)
|
||||
{
|
||||
constexpr Quaternion<float> q{1.f, -2.f, 3.f, -4.f};
|
||||
constexpr auto r = -q;
|
||||
EXPECT_FLOAT_EQ(r.x, -1.f);
|
||||
EXPECT_FLOAT_EQ(r.y, 2.f);
|
||||
EXPECT_FLOAT_EQ(r.z, -3.f);
|
||||
EXPECT_FLOAT_EQ(r.w, 4.f);
|
||||
}
|
||||
|
||||
// ── Hamilton product ──────────────────────────────────────────────────────────
|
||||
|
||||
TEST(Quaternion, MultiplyByIdentityIsNoop)
|
||||
{
|
||||
constexpr Quaternion<float> identity;
|
||||
constexpr Quaternion<float> q{0.5f, 0.5f, 0.5f, 0.5f};
|
||||
expect_quat_near(q * identity, q);
|
||||
expect_quat_near(identity * q, q);
|
||||
}
|
||||
|
||||
TEST(Quaternion, MultiplyAssign)
|
||||
{
|
||||
constexpr Quaternion<float> identity;
|
||||
Quaternion<float> q{0.5f, 0.5f, 0.5f, 0.5f};
|
||||
q *= identity;
|
||||
expect_quat_near(q, {0.5f, 0.5f, 0.5f, 0.5f});
|
||||
}
|
||||
|
||||
TEST(Quaternion, MultiplyKnownResult)
|
||||
{
|
||||
// i * j = k → (1,0,0,0) * (0,1,0,0) = (0,0,1,0)
|
||||
constexpr Quaternion<float> i{1.f, 0.f, 0.f, 0.f};
|
||||
constexpr Quaternion<float> j{0.f, 1.f, 0.f, 0.f};
|
||||
constexpr auto k = i * j;
|
||||
EXPECT_FLOAT_EQ(k.x, 0.f);
|
||||
EXPECT_FLOAT_EQ(k.y, 0.f);
|
||||
EXPECT_FLOAT_EQ(k.z, 1.f);
|
||||
EXPECT_FLOAT_EQ(k.w, 0.f);
|
||||
}
|
||||
|
||||
TEST(Quaternion, MultiplyByInverseGivesIdentity)
|
||||
{
|
||||
const Quaternion<float> q = Quaternion<float>::from_axis_angle({0.f, 0.f, 1.f},
|
||||
std::numbers::pi_v<float> / 3.f);
|
||||
const auto result = q * q.inverse();
|
||||
expect_quat_near(result, Quaternion<float>{});
|
||||
}
|
||||
|
||||
// ── Conjugate ────────────────────────────────────────────────────────────────
|
||||
|
||||
TEST(Quaternion, Conjugate)
|
||||
{
|
||||
constexpr Quaternion<float> q{1.f, 2.f, 3.f, 4.f};
|
||||
constexpr auto c = q.conjugate();
|
||||
EXPECT_FLOAT_EQ(c.x, -1.f);
|
||||
EXPECT_FLOAT_EQ(c.y, -2.f);
|
||||
EXPECT_FLOAT_EQ(c.z, -3.f);
|
||||
EXPECT_FLOAT_EQ(c.w, 4.f);
|
||||
}
|
||||
|
||||
TEST(Quaternion, ConjugateOfIdentityIsIdentity)
|
||||
{
|
||||
constexpr Quaternion<float> id;
|
||||
constexpr auto c = id.conjugate();
|
||||
EXPECT_FLOAT_EQ(c.x, 0.f);
|
||||
EXPECT_FLOAT_EQ(c.y, 0.f);
|
||||
EXPECT_FLOAT_EQ(c.z, 0.f);
|
||||
EXPECT_FLOAT_EQ(c.w, 1.f);
|
||||
}
|
||||
|
||||
// ── Dot / length ─────────────────────────────────────────────────────────────
|
||||
|
||||
TEST(Quaternion, Dot)
|
||||
{
|
||||
constexpr Quaternion<float> a{1.f, 0.f, 0.f, 0.f};
|
||||
constexpr Quaternion<float> b{0.f, 1.f, 0.f, 0.f};
|
||||
EXPECT_FLOAT_EQ(a.dot(b), 0.f);
|
||||
EXPECT_FLOAT_EQ(a.dot(a), 1.f);
|
||||
}
|
||||
|
||||
TEST(Quaternion, LengthSqrIdentity)
|
||||
{
|
||||
constexpr Quaternion<float> id;
|
||||
EXPECT_FLOAT_EQ(id.length_sqr(), 1.f);
|
||||
}
|
||||
|
||||
TEST(Quaternion, LengthSqrGeneral)
|
||||
{
|
||||
constexpr Quaternion<float> q{1.f, 2.f, 3.f, 4.f};
|
||||
EXPECT_FLOAT_EQ(q.length_sqr(), 30.f);
|
||||
}
|
||||
|
||||
TEST(Quaternion, LengthIdentity)
|
||||
{
|
||||
const Quaternion<float> id;
|
||||
EXPECT_NEAR(id.length(), 1.f, kEps);
|
||||
}
|
||||
|
||||
TEST(Quaternion, Normalized)
|
||||
{
|
||||
const Quaternion<float> q{1.f, 1.f, 1.f, 1.f};
|
||||
const auto n = q.normalized();
|
||||
EXPECT_NEAR(n.length(), 1.f, kEps);
|
||||
EXPECT_NEAR(n.x, 0.5f, kEps);
|
||||
EXPECT_NEAR(n.y, 0.5f, kEps);
|
||||
EXPECT_NEAR(n.z, 0.5f, kEps);
|
||||
EXPECT_NEAR(n.w, 0.5f, kEps);
|
||||
}
|
||||
|
||||
TEST(Quaternion, NormalizedOfZeroLengthReturnsSelf)
|
||||
{
|
||||
// length_sqr = 0 would be UB, but zero-vector part + zero w is degenerate;
|
||||
// we just verify the guard branch (divides by zero) doesn't crash by
|
||||
// keeping length > 0 via the default constructor path.
|
||||
const Quaternion<float> unit;
|
||||
const auto n = unit.normalized();
|
||||
expect_quat_near(n, unit);
|
||||
}
|
||||
|
||||
// ── Inverse ───────────────────────────────────────────────────────────────────
|
||||
|
||||
TEST(Quaternion, InverseOfUnitIsConjugate)
|
||||
{
|
||||
const Quaternion<float> q = Quaternion<float>::from_axis_angle({1.f, 0.f, 0.f},
|
||||
std::numbers::pi_v<float> / 4.f);
|
||||
const auto inv = q.inverse();
|
||||
const auto conj = q.conjugate();
|
||||
expect_quat_near(inv, conj);
|
||||
}
|
||||
|
||||
// ── from_axis_angle ──────────────────────────────────────────────────────────
|
||||
|
||||
TEST(Quaternion, FromAxisAngleZeroAngleIsIdentity)
|
||||
{
|
||||
const auto q = Quaternion<float>::from_axis_angle({1.f, 0.f, 0.f}, 0.f);
|
||||
EXPECT_NEAR(q.x, 0.f, kEps);
|
||||
EXPECT_NEAR(q.y, 0.f, kEps);
|
||||
EXPECT_NEAR(q.z, 0.f, kEps);
|
||||
EXPECT_NEAR(q.w, 1.f, kEps);
|
||||
}
|
||||
|
||||
TEST(Quaternion, FromAxisAngle90DegZ)
|
||||
{
|
||||
const float half_pi = std::numbers::pi_v<float> / 2.f;
|
||||
const auto q = Quaternion<float>::from_axis_angle({0.f, 0.f, 1.f}, half_pi);
|
||||
const float s = std::sin(half_pi / 2.f);
|
||||
const float c = std::cos(half_pi / 2.f);
|
||||
EXPECT_NEAR(q.x, 0.f, kEps);
|
||||
EXPECT_NEAR(q.y, 0.f, kEps);
|
||||
EXPECT_NEAR(q.z, s, kEps);
|
||||
EXPECT_NEAR(q.w, c, kEps);
|
||||
}
|
||||
|
||||
// ── rotate ───────────────────────────────────────────────────────────────────
|
||||
|
||||
TEST(Quaternion, RotateByIdentityIsNoop)
|
||||
{
|
||||
constexpr Quaternion<float> id;
|
||||
constexpr Vector3<float> v{1.f, 2.f, 3.f};
|
||||
const auto r = id.rotate(v);
|
||||
expect_vec3_near(r, v);
|
||||
}
|
||||
|
||||
TEST(Quaternion, Rotate90DegAroundZ)
|
||||
{
|
||||
// Rotating (1,0,0) by 90° around Z should give (0,1,0)
|
||||
const auto q = Quaternion<float>::from_axis_angle({0.f, 0.f, 1.f}, std::numbers::pi_v<float> / 2.f);
|
||||
const auto r = q.rotate({1.f, 0.f, 0.f});
|
||||
expect_vec3_near(r, {0.f, 1.f, 0.f});
|
||||
}
|
||||
|
||||
TEST(Quaternion, Rotate180DegAroundY)
|
||||
{
|
||||
// Rotating (1,0,0) by 180° around Y should give (-1,0,0)
|
||||
const auto q = Quaternion<float>::from_axis_angle({0.f, 1.f, 0.f}, std::numbers::pi_v<float>);
|
||||
const auto r = q.rotate({1.f, 0.f, 0.f});
|
||||
expect_vec3_near(r, {-1.f, 0.f, 0.f});
|
||||
}
|
||||
|
||||
TEST(Quaternion, Rotate90DegAroundX)
|
||||
{
|
||||
// Rotating (0,1,0) by 90° around X should give (0,0,1)
|
||||
const auto q = Quaternion<float>::from_axis_angle({1.f, 0.f, 0.f}, std::numbers::pi_v<float> / 2.f);
|
||||
const auto r = q.rotate({0.f, 1.f, 0.f});
|
||||
expect_vec3_near(r, {0.f, 0.f, 1.f});
|
||||
}
|
||||
|
||||
// ── to_rotation_matrix3 ───────────────────────────────────────────────────────
|
||||
|
||||
TEST(Quaternion, RotationMatrix3FromIdentityIsIdentityMatrix)
|
||||
{
|
||||
constexpr Quaternion<float> id;
|
||||
constexpr auto m = id.to_rotation_matrix3();
|
||||
for (size_t i = 0; i < 3; ++i)
|
||||
for (size_t j = 0; j < 3; ++j)
|
||||
EXPECT_NEAR(m.at(i, j), i == j ? 1.f : 0.f, kEps);
|
||||
}
|
||||
|
||||
TEST(Quaternion, RotationMatrix3From90DegZ)
|
||||
{
|
||||
// Expected: | 0 -1 0 |
|
||||
// | 1 0 0 |
|
||||
// | 0 0 1 |
|
||||
const auto q = Quaternion<float>::from_axis_angle({0.f, 0.f, 1.f}, std::numbers::pi_v<float> / 2.f);
|
||||
const auto m = q.to_rotation_matrix3();
|
||||
EXPECT_NEAR(m.at(0, 0), 0.f, kEps);
|
||||
EXPECT_NEAR(m.at(0, 1), -1.f, kEps);
|
||||
EXPECT_NEAR(m.at(0, 2), 0.f, kEps);
|
||||
EXPECT_NEAR(m.at(1, 0), 1.f, kEps);
|
||||
EXPECT_NEAR(m.at(1, 1), 0.f, kEps);
|
||||
EXPECT_NEAR(m.at(1, 2), 0.f, kEps);
|
||||
EXPECT_NEAR(m.at(2, 0), 0.f, kEps);
|
||||
EXPECT_NEAR(m.at(2, 1), 0.f, kEps);
|
||||
EXPECT_NEAR(m.at(2, 2), 1.f, kEps);
|
||||
}
|
||||
|
||||
TEST(Quaternion, RotationMatrix3ConsistentWithRotate)
|
||||
{
|
||||
// Matrix-vector multiply must agree with the rotate() method
|
||||
const auto q = Quaternion<float>::from_axis_angle({1.f, 1.f, 0.f}, std::numbers::pi_v<float> / 3.f);
|
||||
const Vector3<float> v{2.f, -1.f, 0.5f};
|
||||
|
||||
const auto rotated = q.rotate(v);
|
||||
const auto m = q.to_rotation_matrix3();
|
||||
|
||||
// manual mat-vec multiply (row-major)
|
||||
const float rx = m.at(0, 0) * v.x + m.at(0, 1) * v.y + m.at(0, 2) * v.z;
|
||||
const float ry = m.at(1, 0) * v.x + m.at(1, 1) * v.y + m.at(1, 2) * v.z;
|
||||
const float rz = m.at(2, 0) * v.x + m.at(2, 1) * v.y + m.at(2, 2) * v.z;
|
||||
|
||||
EXPECT_NEAR(rotated.x, rx, kEps);
|
||||
EXPECT_NEAR(rotated.y, ry, kEps);
|
||||
EXPECT_NEAR(rotated.z, rz, kEps);
|
||||
}
|
||||
|
||||
// ── to_rotation_matrix4 ───────────────────────────────────────────────────────
|
||||
|
||||
TEST(Quaternion, RotationMatrix4FromIdentityIsIdentityMatrix)
|
||||
{
|
||||
constexpr Quaternion<float> id;
|
||||
constexpr auto m = id.to_rotation_matrix4();
|
||||
for (size_t i = 0; i < 4; ++i)
|
||||
for (size_t j = 0; j < 4; ++j)
|
||||
EXPECT_NEAR(m.at(i, j), i == j ? 1.f : 0.f, kEps);
|
||||
}
|
||||
|
||||
TEST(Quaternion, RotationMatrix4HomogeneousRowAndColumn)
|
||||
{
|
||||
const auto q = Quaternion<float>::from_axis_angle({1.f, 0.f, 0.f}, std::numbers::pi_v<float> / 5.f);
|
||||
const auto m = q.to_rotation_matrix4();
|
||||
|
||||
// Last row and last column must be (0,0,0,1)
|
||||
for (size_t i = 0; i < 3; ++i)
|
||||
{
|
||||
EXPECT_NEAR(m.at(3, i), 0.f, kEps);
|
||||
EXPECT_NEAR(m.at(i, 3), 0.f, kEps);
|
||||
}
|
||||
EXPECT_NEAR(m.at(3, 3), 1.f, kEps);
|
||||
}
|
||||
|
||||
TEST(Quaternion, RotationMatrix4Upper3x3MatchesMatrix3)
|
||||
{
|
||||
const auto q = Quaternion<float>::from_axis_angle({0.f, 1.f, 0.f}, std::numbers::pi_v<float> / 7.f);
|
||||
const auto m3 = q.to_rotation_matrix3();
|
||||
const auto m4 = q.to_rotation_matrix4();
|
||||
|
||||
for (size_t i = 0; i < 3; ++i)
|
||||
for (size_t j = 0; j < 3; ++j)
|
||||
EXPECT_NEAR(m4.at(i, j), m3.at(i, j), kEps);
|
||||
}
|
||||
|
||||
// ── as_array ──────────────────────────────────────────────────────────────────
|
||||
|
||||
TEST(Quaternion, AsArray)
|
||||
{
|
||||
constexpr Quaternion<float> q{1.f, 2.f, 3.f, 4.f};
|
||||
constexpr auto arr = q.as_array();
|
||||
EXPECT_FLOAT_EQ(arr[0], 1.f);
|
||||
EXPECT_FLOAT_EQ(arr[1], 2.f);
|
||||
EXPECT_FLOAT_EQ(arr[2], 3.f);
|
||||
EXPECT_FLOAT_EQ(arr[3], 4.f);
|
||||
}
|
||||
|
||||
// ── std::formatter ────────────────────────────────────────────────────────────
|
||||
|
||||
TEST(Quaternion, Formatter)
|
||||
{
|
||||
const Quaternion<float> q{1.f, 2.f, 3.f, 4.f};
|
||||
const auto s = std::format("{}", q);
|
||||
EXPECT_EQ(s, "[1, 2, 3, 4]");
|
||||
}
|
||||
96
tests/lua/color_tests.lua
Normal file
96
tests/lua/color_tests.lua
Normal file
@@ -0,0 +1,96 @@
|
||||
local function approx(a, b, eps) return math.abs(a - b) < (eps or 1e-4) end
|
||||
|
||||
function Color_Constructor_float()
|
||||
local c = omath.Color.new(1, 0.5, 0.25, 1)
|
||||
assert(approx(c.r, 1) and approx(c.g, 0.5) and approx(c.b, 0.25) and approx(c.a, 1))
|
||||
end
|
||||
|
||||
function Color_Constructor_default()
|
||||
local c = omath.Color.new()
|
||||
assert(c ~= nil)
|
||||
end
|
||||
|
||||
function Color_Constructor_clamping()
|
||||
local c = omath.Color.new(2, -1, 0.5, 1)
|
||||
assert(approx(c.r, 1) and approx(c.g, 0) and approx(c.b, 0.5))
|
||||
end
|
||||
|
||||
function Color_from_rgba()
|
||||
local c = omath.Color.from_rgba(255, 128, 0, 255)
|
||||
assert(approx(c.r, 1) and approx(c.g, 128/255) and approx(c.b, 0) and approx(c.a, 1))
|
||||
end
|
||||
|
||||
function Color_from_hsv_components()
|
||||
local c = omath.Color.from_hsv(0, 1, 1)
|
||||
assert(approx(c.r, 1) and approx(c.g, 0) and approx(c.b, 0))
|
||||
end
|
||||
|
||||
function Color_from_hsv_struct()
|
||||
local hsv = omath.Hsv.new()
|
||||
hsv.hue = 0
|
||||
hsv.saturation = 1
|
||||
hsv.value = 1
|
||||
local c = omath.Color.from_hsv(hsv)
|
||||
assert(approx(c.r, 1) and approx(c.g, 0) and approx(c.b, 0))
|
||||
end
|
||||
|
||||
function Color_red()
|
||||
local c = omath.Color.red()
|
||||
assert(approx(c.r, 1) and approx(c.g, 0) and approx(c.b, 0) and approx(c.a, 1))
|
||||
end
|
||||
|
||||
function Color_green()
|
||||
local c = omath.Color.green()
|
||||
assert(approx(c.r, 0) and approx(c.g, 1) and approx(c.b, 0) and approx(c.a, 1))
|
||||
end
|
||||
|
||||
function Color_blue()
|
||||
local c = omath.Color.blue()
|
||||
assert(approx(c.r, 0) and approx(c.g, 0) and approx(c.b, 1) and approx(c.a, 1))
|
||||
end
|
||||
|
||||
function Color_to_hsv()
|
||||
local hsv = omath.Color.red():to_hsv()
|
||||
assert(approx(hsv.hue, 0) and approx(hsv.saturation, 1) and approx(hsv.value, 1))
|
||||
end
|
||||
|
||||
function Color_set_hue()
|
||||
local c = omath.Color.red()
|
||||
c:set_hue(1/3)
|
||||
assert(approx(c.g, 1, 1e-3))
|
||||
end
|
||||
|
||||
function Color_set_saturation()
|
||||
local c = omath.Color.red()
|
||||
c:set_saturation(0)
|
||||
assert(approx(c.r, c.g) and approx(c.g, c.b))
|
||||
end
|
||||
|
||||
function Color_set_value()
|
||||
local c = omath.Color.red()
|
||||
c:set_value(0)
|
||||
assert(approx(c.r, 0) and approx(c.g, 0) and approx(c.b, 0))
|
||||
end
|
||||
|
||||
function Color_blend()
|
||||
local c = omath.Color.red():blend(omath.Color.blue(), 0.5)
|
||||
assert(approx(c.r, 0.5) and approx(c.b, 0.5))
|
||||
end
|
||||
|
||||
function Color_blend_clamped_ratio()
|
||||
local c = omath.Color.red():blend(omath.Color.blue(), 2.0)
|
||||
assert(approx(c.r, 0) and approx(c.b, 1))
|
||||
end
|
||||
|
||||
function Color_to_string()
|
||||
local s = tostring(omath.Color.red())
|
||||
assert(s == "[r:255, g:0, b:0, a:255]")
|
||||
end
|
||||
|
||||
function Hsv_fields()
|
||||
local hsv = omath.Hsv.new()
|
||||
hsv.hue = 0.5
|
||||
hsv.saturation = 0.8
|
||||
hsv.value = 0.9
|
||||
assert(approx(hsv.hue, 0.5) and approx(hsv.saturation, 0.8) and approx(hsv.value, 0.9))
|
||||
end
|
||||
66
tests/lua/pe_scanner_tests.lua
Normal file
66
tests/lua/pe_scanner_tests.lua
Normal file
@@ -0,0 +1,66 @@
|
||||
-- PatternScanner tests: generic scan over a Lua string buffer
|
||||
|
||||
function PatternScanner_FindsExactPattern()
|
||||
local buf = "\x90\x01\x02\x03\x04"
|
||||
local offset = omath.PatternScanner.scan(buf, "90 01 02")
|
||||
assert(offset ~= nil, "expected pattern to be found")
|
||||
assert(offset == 0, "expected offset 0, got " .. tostring(offset))
|
||||
end
|
||||
|
||||
function PatternScanner_FindsPatternAtNonZeroOffset()
|
||||
local buf = "\x00\x00\xAB\xCD\xEF"
|
||||
local offset = omath.PatternScanner.scan(buf, "AB CD EF")
|
||||
assert(offset ~= nil, "expected pattern to be found")
|
||||
assert(offset == 2, "expected offset 2, got " .. tostring(offset))
|
||||
end
|
||||
|
||||
function PatternScanner_WildcardMatches()
|
||||
local buf = "\xDE\xAD\xBE\xEF"
|
||||
local offset = omath.PatternScanner.scan(buf, "DE ?? BE")
|
||||
assert(offset ~= nil, "expected wildcard match")
|
||||
assert(offset == 0)
|
||||
end
|
||||
|
||||
function PatternScanner_ReturnsNilWhenNotFound()
|
||||
local buf = "\x01\x02\x03"
|
||||
local offset = omath.PatternScanner.scan(buf, "AA BB CC")
|
||||
assert(offset == nil, "expected nil for not-found pattern")
|
||||
end
|
||||
|
||||
function PatternScanner_ReturnsNilForEmptyBuffer()
|
||||
local offset = omath.PatternScanner.scan("", "90 01")
|
||||
assert(offset == nil)
|
||||
end
|
||||
|
||||
-- PePatternScanner tests: scan_in_module uses FAKE_MODULE_BASE injected from C++
|
||||
-- The fake module contains {0x90, 0x01, 0x02, 0x03, 0x04} placed at raw offset 0x200
|
||||
|
||||
function PeScanner_FindsExactPattern()
|
||||
local addr = omath.PePatternScanner.scan_in_module(FAKE_MODULE_BASE, "90 01 02")
|
||||
assert(addr ~= nil, "expected pattern to be found in module")
|
||||
local offset = addr - FAKE_MODULE_BASE
|
||||
assert(offset == 0x200, string.format("expected offset 0x200, got 0x%X", offset))
|
||||
end
|
||||
|
||||
function PeScanner_WildcardMatches()
|
||||
local addr = omath.PePatternScanner.scan_in_module(FAKE_MODULE_BASE, "90 ?? 02")
|
||||
assert(addr ~= nil, "expected wildcard match in module")
|
||||
local offset = addr - FAKE_MODULE_BASE
|
||||
assert(offset == 0x200, string.format("expected offset 0x200, got 0x%X", offset))
|
||||
end
|
||||
|
||||
function PeScanner_ReturnsNilWhenNotFound()
|
||||
local addr = omath.PePatternScanner.scan_in_module(FAKE_MODULE_BASE, "AA BB CC DD")
|
||||
assert(addr == nil, "expected nil for not-found pattern")
|
||||
end
|
||||
|
||||
function PeScanner_CustomSectionFallsBackToNil()
|
||||
-- Request a section that doesn't exist in our fake module
|
||||
local addr = omath.PePatternScanner.scan_in_module(FAKE_MODULE_BASE, "90 01 02", ".rdata")
|
||||
assert(addr == nil, "expected nil for wrong section name")
|
||||
end
|
||||
|
||||
-- SectionScanResult: verify the type is registered and tostring works on a C++-returned value
|
||||
function SectionScanResult_TypeIsRegistered()
|
||||
assert(omath.SectionScanResult ~= nil, "SectionScanResult type should be registered")
|
||||
end
|
||||
197
tests/lua/source_engine_tests.lua
Normal file
197
tests/lua/source_engine_tests.lua
Normal file
@@ -0,0 +1,197 @@
|
||||
local function approx(a, b, eps) return math.abs(a - b) < (eps or 1e-4) end
|
||||
|
||||
local function make_camera()
|
||||
local pos = omath.Vec3.new(0, 0, 0)
|
||||
local pitch = omath.source.PitchAngle.from_degrees(0)
|
||||
local yaw = omath.source.YawAngle.from_degrees(0)
|
||||
local roll = omath.source.RollAngle.from_degrees(0)
|
||||
local angles = omath.source.ViewAngles.new(pitch, yaw, roll)
|
||||
local vp = omath.opengl.ViewPort.new(1920, 1080)
|
||||
local fov = omath.source.FieldOfView.from_degrees(90)
|
||||
return omath.source.Camera.new(pos, angles, vp, fov, 0.1, 1000)
|
||||
end
|
||||
|
||||
-- PitchAngle
|
||||
function Source_PitchAngle_from_degrees()
|
||||
assert(omath.source.PitchAngle.from_degrees(45):as_degrees() == 45)
|
||||
end
|
||||
|
||||
function Source_PitchAngle_clamping_max()
|
||||
assert(omath.source.PitchAngle.from_degrees(100):as_degrees() == 89)
|
||||
end
|
||||
|
||||
function Source_PitchAngle_clamping_min()
|
||||
assert(omath.source.PitchAngle.from_degrees(-100):as_degrees() == -89)
|
||||
end
|
||||
|
||||
function Source_PitchAngle_from_radians()
|
||||
assert(approx(omath.source.PitchAngle.from_radians(math.pi / 4):as_degrees(), 45))
|
||||
end
|
||||
|
||||
function Source_PitchAngle_as_radians()
|
||||
assert(approx(omath.source.PitchAngle.from_degrees(0):as_radians(), 0))
|
||||
end
|
||||
|
||||
function Source_PitchAngle_sin()
|
||||
assert(approx(omath.source.PitchAngle.from_degrees(30):sin(), 0.5))
|
||||
end
|
||||
|
||||
function Source_PitchAngle_cos()
|
||||
assert(approx(omath.source.PitchAngle.from_degrees(60):cos(), 0.5))
|
||||
end
|
||||
|
||||
function Source_PitchAngle_tan()
|
||||
assert(approx(omath.source.PitchAngle.from_degrees(45):tan(), 1.0))
|
||||
end
|
||||
|
||||
function Source_PitchAngle_addition()
|
||||
local c = omath.source.PitchAngle.from_degrees(20) + omath.source.PitchAngle.from_degrees(15)
|
||||
assert(c:as_degrees() == 35)
|
||||
end
|
||||
|
||||
function Source_PitchAngle_addition_clamped()
|
||||
local c = omath.source.PitchAngle.from_degrees(80) + omath.source.PitchAngle.from_degrees(20)
|
||||
assert(c:as_degrees() == 89)
|
||||
end
|
||||
|
||||
function Source_PitchAngle_subtraction()
|
||||
local c = omath.source.PitchAngle.from_degrees(50) - omath.source.PitchAngle.from_degrees(20)
|
||||
assert(c:as_degrees() == 30)
|
||||
end
|
||||
|
||||
function Source_PitchAngle_unary_minus()
|
||||
assert((-omath.source.PitchAngle.from_degrees(45)):as_degrees() == -45)
|
||||
end
|
||||
|
||||
function Source_PitchAngle_equal_to()
|
||||
local a = omath.source.PitchAngle.from_degrees(45)
|
||||
assert(a == omath.source.PitchAngle.from_degrees(45))
|
||||
assert(not (a == omath.source.PitchAngle.from_degrees(30)))
|
||||
end
|
||||
|
||||
function Source_PitchAngle_to_string()
|
||||
assert(tostring(omath.source.PitchAngle.from_degrees(45)) == "45deg")
|
||||
end
|
||||
|
||||
-- YawAngle
|
||||
function Source_YawAngle_from_degrees()
|
||||
assert(omath.source.YawAngle.from_degrees(90):as_degrees() == 90)
|
||||
end
|
||||
|
||||
function Source_YawAngle_normalization()
|
||||
assert(approx(omath.source.YawAngle.from_degrees(200):as_degrees(), -160))
|
||||
end
|
||||
|
||||
-- RollAngle
|
||||
function Source_RollAngle_from_degrees()
|
||||
assert(omath.source.RollAngle.from_degrees(45):as_degrees() == 45)
|
||||
end
|
||||
|
||||
-- FieldOfView
|
||||
function Source_FieldOfView_from_degrees()
|
||||
assert(omath.source.FieldOfView.from_degrees(90):as_degrees() == 90)
|
||||
end
|
||||
|
||||
function Source_FieldOfView_clamping()
|
||||
assert(omath.source.FieldOfView.from_degrees(200):as_degrees() == 180)
|
||||
end
|
||||
|
||||
-- ViewAngles
|
||||
function Source_ViewAngles_new()
|
||||
local angles = omath.source.ViewAngles.new(
|
||||
omath.source.PitchAngle.from_degrees(30),
|
||||
omath.source.YawAngle.from_degrees(90),
|
||||
omath.source.RollAngle.from_degrees(0))
|
||||
assert(angles.pitch:as_degrees() == 30)
|
||||
assert(angles.yaw:as_degrees() == 90)
|
||||
assert(angles.roll:as_degrees() == 0)
|
||||
end
|
||||
|
||||
function Source_ViewAngles_field_mutation()
|
||||
local angles = omath.source.ViewAngles.new(
|
||||
omath.source.PitchAngle.from_degrees(0),
|
||||
omath.source.YawAngle.from_degrees(0),
|
||||
omath.source.RollAngle.from_degrees(0))
|
||||
angles.pitch = omath.source.PitchAngle.from_degrees(45)
|
||||
assert(angles.pitch:as_degrees() == 45)
|
||||
end
|
||||
|
||||
-- Camera
|
||||
function Source_Camera_constructor()
|
||||
assert(make_camera() ~= nil)
|
||||
end
|
||||
|
||||
function Source_Camera_get_set_origin()
|
||||
local cam = make_camera()
|
||||
cam:set_origin(omath.Vec3.new(1, 2, 3))
|
||||
local o = cam:get_origin()
|
||||
assert(approx(o.x, 1) and approx(o.y, 2) and approx(o.z, 3))
|
||||
end
|
||||
|
||||
function Source_Camera_get_set_near_plane()
|
||||
local cam = make_camera()
|
||||
cam:set_near_plane(0.5)
|
||||
assert(approx(cam:get_near_plane(), 0.5))
|
||||
end
|
||||
|
||||
function Source_Camera_get_set_far_plane()
|
||||
local cam = make_camera()
|
||||
cam:set_far_plane(500)
|
||||
assert(approx(cam:get_far_plane(), 500))
|
||||
end
|
||||
|
||||
function Source_Camera_get_set_fov()
|
||||
local cam = make_camera()
|
||||
cam:set_field_of_view(omath.source.FieldOfView.from_degrees(60))
|
||||
assert(approx(cam:get_field_of_view():as_degrees(), 60))
|
||||
end
|
||||
|
||||
function Source_Camera_get_set_view_angles()
|
||||
local cam = make_camera()
|
||||
cam:set_view_angles(omath.source.ViewAngles.new(
|
||||
omath.source.PitchAngle.from_degrees(30),
|
||||
omath.source.YawAngle.from_degrees(90),
|
||||
omath.source.RollAngle.from_degrees(0)))
|
||||
assert(approx(cam:get_view_angles().pitch:as_degrees(), 30))
|
||||
assert(approx(cam:get_view_angles().yaw:as_degrees(), 90))
|
||||
end
|
||||
|
||||
function Source_Camera_look_at()
|
||||
local cam = make_camera()
|
||||
cam:look_at(omath.Vec3.new(10, 0, 0))
|
||||
assert(cam:get_view_angles() ~= nil)
|
||||
end
|
||||
|
||||
function Source_Camera_get_forward()
|
||||
local fwd = make_camera():get_forward()
|
||||
assert(approx(fwd:length(), 1.0))
|
||||
end
|
||||
|
||||
function Source_Camera_get_right()
|
||||
assert(approx(make_camera():get_right():length(), 1.0))
|
||||
end
|
||||
|
||||
function Source_Camera_get_up()
|
||||
assert(approx(make_camera():get_up():length(), 1.0))
|
||||
end
|
||||
|
||||
function Source_Camera_world_to_screen_success()
|
||||
local cam = make_camera()
|
||||
cam:look_at(omath.Vec3.new(1, 0, 0))
|
||||
local screen, err = cam:world_to_screen(omath.Vec3.new(5, 0, 0))
|
||||
assert(screen ~= nil, "expected screen pos, got: " .. tostring(err))
|
||||
end
|
||||
|
||||
function Source_Camera_world_to_screen_error()
|
||||
local cam = make_camera()
|
||||
cam:look_at(omath.Vec3.new(1, 0, 0))
|
||||
local screen, err = cam:world_to_screen(omath.Vec3.new(-100, 0, 0))
|
||||
assert(screen == nil and err ~= nil)
|
||||
end
|
||||
|
||||
function Source_Camera_screen_to_world()
|
||||
local cam = make_camera()
|
||||
cam:look_at(omath.Vec3.new(1, 0, 0))
|
||||
local world, err = cam:screen_to_world(omath.Vec3.new(960, 540, 1))
|
||||
assert(world ~= nil, "expected world pos, got: " .. tostring(err))
|
||||
end
|
||||
82
tests/lua/triangle_tests.lua
Normal file
82
tests/lua/triangle_tests.lua
Normal file
@@ -0,0 +1,82 @@
|
||||
local function approx(a, b, eps) return math.abs(a - b) < (eps or 1e-5) end
|
||||
|
||||
function Triangle_Constructor_default()
|
||||
local t = omath.Triangle.new()
|
||||
assert(t.vertex1.x == 0 and t.vertex1.y == 0 and t.vertex1.z == 0)
|
||||
assert(t.vertex2.x == 0 and t.vertex2.y == 0 and t.vertex2.z == 0)
|
||||
assert(t.vertex3.x == 0 and t.vertex3.y == 0 and t.vertex3.z == 0)
|
||||
end
|
||||
|
||||
function Triangle_Constructor_vertices()
|
||||
local v1 = omath.Vec3.new(1, 0, 0)
|
||||
local v2 = omath.Vec3.new(0, 1, 0)
|
||||
local v3 = omath.Vec3.new(0, 0, 1)
|
||||
local t = omath.Triangle.new(v1, v2, v3)
|
||||
assert(t.vertex1.x == 1 and t.vertex1.y == 0 and t.vertex1.z == 0)
|
||||
assert(t.vertex2.x == 0 and t.vertex2.y == 1 and t.vertex2.z == 0)
|
||||
assert(t.vertex3.x == 0 and t.vertex3.y == 0 and t.vertex3.z == 1)
|
||||
end
|
||||
|
||||
function Triangle_Vertex_mutation()
|
||||
local t = omath.Triangle.new()
|
||||
t.vertex1 = omath.Vec3.new(5, 6, 7)
|
||||
assert(t.vertex1.x == 5 and t.vertex1.y == 6 and t.vertex1.z == 7)
|
||||
end
|
||||
|
||||
-- Right triangle: v1=(0,3,0), v2=(0,0,0), v3=(4,0,0) — sides 3, 4, hypot 5
|
||||
function Triangle_SideALength()
|
||||
local t = omath.Triangle.new(omath.Vec3.new(0, 3, 0), omath.Vec3.new(0, 0, 0), omath.Vec3.new(4, 0, 0))
|
||||
assert(approx(t:side_a_length(), 3.0))
|
||||
end
|
||||
|
||||
function Triangle_SideBLength()
|
||||
local t = omath.Triangle.new(omath.Vec3.new(0, 3, 0), omath.Vec3.new(0, 0, 0), omath.Vec3.new(4, 0, 0))
|
||||
assert(approx(t:side_b_length(), 4.0))
|
||||
end
|
||||
|
||||
function Triangle_Hypot()
|
||||
local t = omath.Triangle.new(omath.Vec3.new(0, 3, 0), omath.Vec3.new(0, 0, 0), omath.Vec3.new(4, 0, 0))
|
||||
assert(approx(t:hypot(), 5.0))
|
||||
end
|
||||
|
||||
function Triangle_SideAVector()
|
||||
local t = omath.Triangle.new(omath.Vec3.new(0, 3, 0), omath.Vec3.new(0, 0, 0), omath.Vec3.new(4, 0, 0))
|
||||
local a = t:side_a_vector()
|
||||
assert(approx(a.x, 0) and approx(a.y, 3) and approx(a.z, 0))
|
||||
end
|
||||
|
||||
function Triangle_SideBVector()
|
||||
local t = omath.Triangle.new(omath.Vec3.new(0, 3, 0), omath.Vec3.new(0, 0, 0), omath.Vec3.new(4, 0, 0))
|
||||
local b = t:side_b_vector()
|
||||
assert(approx(b.x, 4) and approx(b.y, 0) and approx(b.z, 0))
|
||||
end
|
||||
|
||||
function Triangle_IsRectangular_true()
|
||||
local t = omath.Triangle.new(omath.Vec3.new(0, 3, 0), omath.Vec3.new(0, 0, 0), omath.Vec3.new(4, 0, 0))
|
||||
assert(t:is_rectangular() == true)
|
||||
end
|
||||
|
||||
function Triangle_IsRectangular_false()
|
||||
-- equilateral-ish triangle, not rectangular
|
||||
local t = omath.Triangle.new(omath.Vec3.new(0, 1, 0), omath.Vec3.new(-1, 0, 0), omath.Vec3.new(1, 0, 0))
|
||||
assert(t:is_rectangular() == false)
|
||||
end
|
||||
|
||||
function Triangle_MidPoint()
|
||||
local t = omath.Triangle.new(omath.Vec3.new(3, 0, 0), omath.Vec3.new(0, 3, 0), omath.Vec3.new(0, 0, 3))
|
||||
local m = t:mid_point()
|
||||
assert(approx(m.x, 1.0) and approx(m.y, 1.0) and approx(m.z, 1.0))
|
||||
end
|
||||
|
||||
function Triangle_CalculateNormal()
|
||||
-- flat triangle in XY plane — normal should be (0, 0, 1)
|
||||
local t = omath.Triangle.new(omath.Vec3.new(0, 1, 0), omath.Vec3.new(0, 0, 0), omath.Vec3.new(1, 0, 0))
|
||||
local n = t:calculate_normal()
|
||||
assert(approx(n.x, 0) and approx(n.y, 0) and approx(n.z, 1))
|
||||
end
|
||||
|
||||
function Triangle_ToString()
|
||||
local t = omath.Triangle.new(omath.Vec3.new(1, 0, 0), omath.Vec3.new(0, 1, 0), omath.Vec3.new(0, 0, 1))
|
||||
local s = tostring(t)
|
||||
assert(s == "Triangle((1, 0, 0), (0, 1, 0), (0, 0, 1))")
|
||||
end
|
||||
51
tests/lua/unit_test_lua_color.cpp
Normal file
51
tests/lua/unit_test_lua_color.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
//
|
||||
// Created by orange on 08.03.2026.
|
||||
//
|
||||
#include <gtest/gtest.h>
|
||||
#include <lua.hpp>
|
||||
#include <omath/lua/lua.hpp>
|
||||
|
||||
class LuaColor : public ::testing::Test
|
||||
{
|
||||
protected:
|
||||
lua_State* L = nullptr;
|
||||
|
||||
void SetUp() override
|
||||
{
|
||||
L = luaL_newstate();
|
||||
luaL_openlibs(L);
|
||||
omath::lua::LuaInterpreter::register_lib(L);
|
||||
if (luaL_dofile(L, LUA_SCRIPTS_DIR "/color_tests.lua") != LUA_OK)
|
||||
FAIL() << lua_tostring(L, -1);
|
||||
}
|
||||
|
||||
void TearDown() override { lua_close(L); }
|
||||
|
||||
void check(const char* func_name)
|
||||
{
|
||||
lua_getglobal(L, func_name);
|
||||
if (lua_pcall(L, 0, 0, 0) != LUA_OK)
|
||||
{
|
||||
FAIL() << lua_tostring(L, -1);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(LuaColor, Constructor_float) { check("Color_Constructor_float"); }
|
||||
TEST_F(LuaColor, Constructor_default) { check("Color_Constructor_default"); }
|
||||
TEST_F(LuaColor, Constructor_clamping) { check("Color_Constructor_clamping"); }
|
||||
TEST_F(LuaColor, from_rgba) { check("Color_from_rgba"); }
|
||||
TEST_F(LuaColor, from_hsv_components) { check("Color_from_hsv_components"); }
|
||||
TEST_F(LuaColor, from_hsv_struct) { check("Color_from_hsv_struct"); }
|
||||
TEST_F(LuaColor, red) { check("Color_red"); }
|
||||
TEST_F(LuaColor, green) { check("Color_green"); }
|
||||
TEST_F(LuaColor, blue) { check("Color_blue"); }
|
||||
TEST_F(LuaColor, to_hsv) { check("Color_to_hsv"); }
|
||||
TEST_F(LuaColor, set_hue) { check("Color_set_hue"); }
|
||||
TEST_F(LuaColor, set_saturation) { check("Color_set_saturation"); }
|
||||
TEST_F(LuaColor, set_value) { check("Color_set_value"); }
|
||||
TEST_F(LuaColor, blend) { check("Color_blend"); }
|
||||
TEST_F(LuaColor, blend_clamped_ratio) { check("Color_blend_clamped_ratio"); }
|
||||
TEST_F(LuaColor, to_string) { check("Color_to_string"); }
|
||||
TEST_F(LuaColor, Hsv_fields) { check("Hsv_fields"); }
|
||||
113
tests/lua/unit_test_lua_pe_scanner.cpp
Normal file
113
tests/lua/unit_test_lua_pe_scanner.cpp
Normal file
@@ -0,0 +1,113 @@
|
||||
//
|
||||
// Created by orange on 10.03.2026.
|
||||
//
|
||||
#include <gtest/gtest.h>
|
||||
#include <lua.hpp>
|
||||
#include <omath/lua/lua.hpp>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
std::vector<std::uint8_t> make_fake_pe_module(std::uint32_t base_of_code, std::uint32_t size_code,
|
||||
const std::vector<std::uint8_t>& code_bytes)
|
||||
{
|
||||
constexpr std::uint32_t e_lfanew = 0x80;
|
||||
constexpr std::uint32_t nt_sig = 0x4550;
|
||||
constexpr std::uint16_t opt_magic = 0x020B; // PE32+
|
||||
constexpr std::uint16_t num_sections = 1;
|
||||
constexpr std::uint16_t opt_hdr_size = 0xF0;
|
||||
constexpr std::uint32_t section_table_off = e_lfanew + 4 + 20 + opt_hdr_size;
|
||||
constexpr std::uint32_t section_hdr_size = 40;
|
||||
constexpr std::uint32_t text_chars = 0x60000020;
|
||||
|
||||
const std::uint32_t headers_end = section_table_off + section_hdr_size;
|
||||
const std::uint32_t code_end = base_of_code + size_code;
|
||||
const std::uint32_t total_size = std::max(headers_end, code_end) + 0x100;
|
||||
std::vector<std::uint8_t> buf(total_size, 0);
|
||||
|
||||
auto w16 = [&](std::size_t off, std::uint16_t v) { std::memcpy(buf.data() + off, &v, 2); };
|
||||
auto w32 = [&](std::size_t off, std::uint32_t v) { std::memcpy(buf.data() + off, &v, 4); };
|
||||
auto w64 = [&](std::size_t off, std::uint64_t v) { std::memcpy(buf.data() + off, &v, 8); };
|
||||
|
||||
w16(0x00, 0x5A4D);
|
||||
w32(0x3C, e_lfanew);
|
||||
w32(e_lfanew, nt_sig);
|
||||
|
||||
const std::size_t fh = e_lfanew + 4;
|
||||
w16(fh + 2, num_sections);
|
||||
w16(fh + 16, opt_hdr_size);
|
||||
|
||||
const std::size_t opt = fh + 20;
|
||||
w16(opt + 0, opt_magic);
|
||||
w32(opt + 4, size_code);
|
||||
w32(opt + 20, base_of_code);
|
||||
w64(opt + 24, 0);
|
||||
w32(opt + 32, 0x1000);
|
||||
w32(opt + 36, 0x200);
|
||||
w32(opt + 56, code_end);
|
||||
w32(opt + 60, headers_end);
|
||||
w32(opt + 108, 0);
|
||||
|
||||
const std::size_t sh = section_table_off;
|
||||
std::memcpy(buf.data() + sh, ".text", 5);
|
||||
w32(sh + 8, size_code);
|
||||
w32(sh + 12, base_of_code);
|
||||
w32(sh + 16, size_code);
|
||||
w32(sh + 20, base_of_code);
|
||||
w32(sh + 36, text_chars);
|
||||
|
||||
if (base_of_code + code_bytes.size() <= buf.size())
|
||||
std::memcpy(buf.data() + base_of_code, code_bytes.data(), code_bytes.size());
|
||||
|
||||
return buf;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
class LuaPeScanner : public ::testing::Test
|
||||
{
|
||||
protected:
|
||||
lua_State* L = nullptr;
|
||||
std::vector<std::uint8_t> m_fake_module;
|
||||
|
||||
void SetUp() override
|
||||
{
|
||||
const std::vector<std::uint8_t> code = {0x90, 0x01, 0x02, 0x03, 0x04};
|
||||
m_fake_module = make_fake_pe_module(0x200, static_cast<std::uint32_t>(code.size()), code);
|
||||
|
||||
L = luaL_newstate();
|
||||
luaL_openlibs(L);
|
||||
omath::lua::LuaInterpreter::register_lib(L);
|
||||
|
||||
lua_pushinteger(L, static_cast<lua_Integer>(
|
||||
reinterpret_cast<std::uintptr_t>(m_fake_module.data())));
|
||||
lua_setglobal(L, "FAKE_MODULE_BASE");
|
||||
|
||||
if (luaL_dofile(L, LUA_SCRIPTS_DIR "/pe_scanner_tests.lua") != LUA_OK)
|
||||
FAIL() << lua_tostring(L, -1);
|
||||
}
|
||||
|
||||
void TearDown() override { lua_close(L); }
|
||||
|
||||
void check(const char* func_name)
|
||||
{
|
||||
lua_getglobal(L, func_name);
|
||||
if (lua_pcall(L, 0, 0, 0) != LUA_OK)
|
||||
{
|
||||
FAIL() << lua_tostring(L, -1);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(LuaPeScanner, PatternScanner_FindsExactPattern) { check("PatternScanner_FindsExactPattern"); }
|
||||
TEST_F(LuaPeScanner, PatternScanner_FindsPatternAtOffset) { check("PatternScanner_FindsPatternAtNonZeroOffset"); }
|
||||
TEST_F(LuaPeScanner, PatternScanner_WildcardMatches) { check("PatternScanner_WildcardMatches"); }
|
||||
TEST_F(LuaPeScanner, PatternScanner_ReturnsNilWhenNotFound) { check("PatternScanner_ReturnsNilWhenNotFound"); }
|
||||
TEST_F(LuaPeScanner, PatternScanner_ReturnsNilForEmptyBuffer){ check("PatternScanner_ReturnsNilForEmptyBuffer"); }
|
||||
TEST_F(LuaPeScanner, PeScanner_FindsExactPattern) { check("PeScanner_FindsExactPattern"); }
|
||||
TEST_F(LuaPeScanner, PeScanner_WildcardMatches) { check("PeScanner_WildcardMatches"); }
|
||||
TEST_F(LuaPeScanner, PeScanner_ReturnsNilWhenNotFound) { check("PeScanner_ReturnsNilWhenNotFound"); }
|
||||
TEST_F(LuaPeScanner, PeScanner_CustomSectionFallsBackToNil) { check("PeScanner_CustomSectionFallsBackToNil"); }
|
||||
TEST_F(LuaPeScanner, SectionScanResult_TypeIsRegistered) { check("SectionScanResult_TypeIsRegistered"); }
|
||||
79
tests/lua/unit_test_lua_source_engine.cpp
Normal file
79
tests/lua/unit_test_lua_source_engine.cpp
Normal file
@@ -0,0 +1,79 @@
|
||||
//
|
||||
// Created by orange on 07.03.2026.
|
||||
//
|
||||
#include <gtest/gtest.h>
|
||||
#include <lua.hpp>
|
||||
#include <omath/lua/lua.hpp>
|
||||
|
||||
class LuaSourceEngine : public ::testing::Test
|
||||
{
|
||||
protected:
|
||||
lua_State* L = nullptr;
|
||||
|
||||
void SetUp() override
|
||||
{
|
||||
L = luaL_newstate();
|
||||
luaL_openlibs(L);
|
||||
omath::lua::LuaInterpreter::register_lib(L);
|
||||
if (luaL_dofile(L, LUA_SCRIPTS_DIR "/source_engine_tests.lua") != LUA_OK)
|
||||
FAIL() << lua_tostring(L, -1);
|
||||
}
|
||||
|
||||
void TearDown() override { lua_close(L); }
|
||||
|
||||
void check(const char* func_name)
|
||||
{
|
||||
lua_getglobal(L, func_name);
|
||||
if (lua_pcall(L, 0, 0, 0) != LUA_OK)
|
||||
{
|
||||
FAIL() << lua_tostring(L, -1);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// PitchAngle
|
||||
TEST_F(LuaSourceEngine, PitchAngle_from_degrees) { check("Source_PitchAngle_from_degrees"); }
|
||||
TEST_F(LuaSourceEngine, PitchAngle_clamping_max) { check("Source_PitchAngle_clamping_max"); }
|
||||
TEST_F(LuaSourceEngine, PitchAngle_clamping_min) { check("Source_PitchAngle_clamping_min"); }
|
||||
TEST_F(LuaSourceEngine, PitchAngle_from_radians) { check("Source_PitchAngle_from_radians"); }
|
||||
TEST_F(LuaSourceEngine, PitchAngle_as_radians) { check("Source_PitchAngle_as_radians"); }
|
||||
TEST_F(LuaSourceEngine, PitchAngle_sin) { check("Source_PitchAngle_sin"); }
|
||||
TEST_F(LuaSourceEngine, PitchAngle_cos) { check("Source_PitchAngle_cos"); }
|
||||
TEST_F(LuaSourceEngine, PitchAngle_tan) { check("Source_PitchAngle_tan"); }
|
||||
TEST_F(LuaSourceEngine, PitchAngle_addition) { check("Source_PitchAngle_addition"); }
|
||||
TEST_F(LuaSourceEngine, PitchAngle_addition_clamped) { check("Source_PitchAngle_addition_clamped"); }
|
||||
TEST_F(LuaSourceEngine, PitchAngle_subtraction) { check("Source_PitchAngle_subtraction"); }
|
||||
TEST_F(LuaSourceEngine, PitchAngle_unary_minus) { check("Source_PitchAngle_unary_minus"); }
|
||||
TEST_F(LuaSourceEngine, PitchAngle_equal_to) { check("Source_PitchAngle_equal_to"); }
|
||||
TEST_F(LuaSourceEngine, PitchAngle_to_string) { check("Source_PitchAngle_to_string"); }
|
||||
|
||||
// YawAngle
|
||||
TEST_F(LuaSourceEngine, YawAngle_from_degrees) { check("Source_YawAngle_from_degrees"); }
|
||||
TEST_F(LuaSourceEngine, YawAngle_normalization) { check("Source_YawAngle_normalization"); }
|
||||
|
||||
// RollAngle
|
||||
TEST_F(LuaSourceEngine, RollAngle_from_degrees) { check("Source_RollAngle_from_degrees"); }
|
||||
|
||||
// FieldOfView
|
||||
TEST_F(LuaSourceEngine, FieldOfView_from_degrees) { check("Source_FieldOfView_from_degrees"); }
|
||||
TEST_F(LuaSourceEngine, FieldOfView_clamping) { check("Source_FieldOfView_clamping"); }
|
||||
|
||||
// ViewAngles
|
||||
TEST_F(LuaSourceEngine, ViewAngles_new) { check("Source_ViewAngles_new"); }
|
||||
TEST_F(LuaSourceEngine, ViewAngles_field_mutation) { check("Source_ViewAngles_field_mutation"); }
|
||||
|
||||
// Camera
|
||||
TEST_F(LuaSourceEngine, Camera_constructor) { check("Source_Camera_constructor"); }
|
||||
TEST_F(LuaSourceEngine, Camera_get_set_origin) { check("Source_Camera_get_set_origin"); }
|
||||
TEST_F(LuaSourceEngine, Camera_get_set_near_plane) { check("Source_Camera_get_set_near_plane"); }
|
||||
TEST_F(LuaSourceEngine, Camera_get_set_far_plane) { check("Source_Camera_get_set_far_plane"); }
|
||||
TEST_F(LuaSourceEngine, Camera_get_set_fov) { check("Source_Camera_get_set_fov"); }
|
||||
TEST_F(LuaSourceEngine, Camera_get_set_view_angles) { check("Source_Camera_get_set_view_angles"); }
|
||||
TEST_F(LuaSourceEngine, Camera_look_at) { check("Source_Camera_look_at"); }
|
||||
TEST_F(LuaSourceEngine, Camera_get_forward) { check("Source_Camera_get_forward"); }
|
||||
TEST_F(LuaSourceEngine, Camera_get_right) { check("Source_Camera_get_right"); }
|
||||
TEST_F(LuaSourceEngine, Camera_get_up) { check("Source_Camera_get_up"); }
|
||||
TEST_F(LuaSourceEngine, Camera_world_to_screen_success) { check("Source_Camera_world_to_screen_success"); }
|
||||
TEST_F(LuaSourceEngine, Camera_world_to_screen_error) { check("Source_Camera_world_to_screen_error"); }
|
||||
TEST_F(LuaSourceEngine, Camera_screen_to_world) { check("Source_Camera_screen_to_world"); }
|
||||
47
tests/lua/unit_test_lua_triangle.cpp
Normal file
47
tests/lua/unit_test_lua_triangle.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
//
|
||||
// Created by orange on 10.03.2026.
|
||||
//
|
||||
#include <gtest/gtest.h>
|
||||
#include <lua.hpp>
|
||||
#include <omath/lua/lua.hpp>
|
||||
|
||||
class LuaTriangle : public ::testing::Test
|
||||
{
|
||||
protected:
|
||||
lua_State* L = nullptr;
|
||||
|
||||
void SetUp() override
|
||||
{
|
||||
L = luaL_newstate();
|
||||
luaL_openlibs(L);
|
||||
omath::lua::LuaInterpreter::register_lib(L);
|
||||
if (luaL_dofile(L, LUA_SCRIPTS_DIR "/triangle_tests.lua") != LUA_OK)
|
||||
FAIL() << lua_tostring(L, -1);
|
||||
}
|
||||
|
||||
void TearDown() override { lua_close(L); }
|
||||
|
||||
void check(const char* func_name)
|
||||
{
|
||||
lua_getglobal(L, func_name);
|
||||
if (lua_pcall(L, 0, 0, 0) != LUA_OK)
|
||||
{
|
||||
FAIL() << lua_tostring(L, -1);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(LuaTriangle, Constructor_default) { check("Triangle_Constructor_default"); }
|
||||
TEST_F(LuaTriangle, Constructor_vertices) { check("Triangle_Constructor_vertices"); }
|
||||
TEST_F(LuaTriangle, Vertex_mutation) { check("Triangle_Vertex_mutation"); }
|
||||
TEST_F(LuaTriangle, SideALength) { check("Triangle_SideALength"); }
|
||||
TEST_F(LuaTriangle, SideBLength) { check("Triangle_SideBLength"); }
|
||||
TEST_F(LuaTriangle, Hypot) { check("Triangle_Hypot"); }
|
||||
TEST_F(LuaTriangle, SideAVector) { check("Triangle_SideAVector"); }
|
||||
TEST_F(LuaTriangle, SideBVector) { check("Triangle_SideBVector"); }
|
||||
TEST_F(LuaTriangle, IsRectangular_true) { check("Triangle_IsRectangular_true"); }
|
||||
TEST_F(LuaTriangle, IsRectangular_false) { check("Triangle_IsRectangular_false"); }
|
||||
TEST_F(LuaTriangle, MidPoint) { check("Triangle_MidPoint"); }
|
||||
TEST_F(LuaTriangle, CalculateNormal) { check("Triangle_CalculateNormal"); }
|
||||
TEST_F(LuaTriangle, ToString) { check("Triangle_ToString"); }
|
||||
56
tests/lua/unit_test_lua_vector2.cpp
Normal file
56
tests/lua/unit_test_lua_vector2.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
//
|
||||
// Created by orange on 07.03.2026.
|
||||
//
|
||||
#include <gtest/gtest.h>
|
||||
#include <lua.hpp>
|
||||
#include <omath/lua/lua.hpp>
|
||||
|
||||
class LuaVec2 : public ::testing::Test
|
||||
{
|
||||
protected:
|
||||
lua_State* L = nullptr;
|
||||
|
||||
void SetUp() override
|
||||
{
|
||||
L = luaL_newstate();
|
||||
luaL_openlibs(L);
|
||||
omath::lua::LuaInterpreter::register_lib(L);
|
||||
if (luaL_dofile(L, LUA_SCRIPTS_DIR "/vec2_tests.lua") != LUA_OK)
|
||||
FAIL() << lua_tostring(L, -1);
|
||||
}
|
||||
|
||||
void TearDown() override { lua_close(L); }
|
||||
|
||||
void check(const char* func_name)
|
||||
{
|
||||
lua_getglobal(L, func_name);
|
||||
if (lua_pcall(L, 0, 0, 0) != LUA_OK)
|
||||
{
|
||||
FAIL() << lua_tostring(L, -1);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(LuaVec2, Constructor_default) { check("Vec2_Constructor_default"); }
|
||||
TEST_F(LuaVec2, Constructor_xy) { check("Vec2_Constructor_xy"); }
|
||||
TEST_F(LuaVec2, Field_mutation) { check("Vec2_Field_mutation"); }
|
||||
TEST_F(LuaVec2, Addition) { check("Vec2_Addition"); }
|
||||
TEST_F(LuaVec2, Subtraction) { check("Vec2_Subtraction"); }
|
||||
TEST_F(LuaVec2, UnaryMinus) { check("Vec2_UnaryMinus"); }
|
||||
TEST_F(LuaVec2, Multiplication_scalar) { check("Vec2_Multiplication_scalar"); }
|
||||
TEST_F(LuaVec2, Multiplication_scalar_reversed) { check("Vec2_Multiplication_scalar_reversed"); }
|
||||
TEST_F(LuaVec2, Division_scalar) { check("Vec2_Division_scalar"); }
|
||||
TEST_F(LuaVec2, EqualTo_true) { check("Vec2_EqualTo_true"); }
|
||||
TEST_F(LuaVec2, EqualTo_false) { check("Vec2_EqualTo_false"); }
|
||||
TEST_F(LuaVec2, LessThan) { check("Vec2_LessThan"); }
|
||||
TEST_F(LuaVec2, LessThanOrEqual) { check("Vec2_LessThanOrEqual"); }
|
||||
TEST_F(LuaVec2, ToString) { check("Vec2_ToString"); }
|
||||
TEST_F(LuaVec2, Length) { check("Vec2_Length"); }
|
||||
TEST_F(LuaVec2, LengthSqr) { check("Vec2_LengthSqr"); }
|
||||
TEST_F(LuaVec2, Normalized) { check("Vec2_Normalized"); }
|
||||
TEST_F(LuaVec2, Dot) { check("Vec2_Dot"); }
|
||||
TEST_F(LuaVec2, DistanceTo) { check("Vec2_DistanceTo"); }
|
||||
TEST_F(LuaVec2, DistanceToSqr) { check("Vec2_DistanceToSqr"); }
|
||||
TEST_F(LuaVec2, Sum) { check("Vec2_Sum"); }
|
||||
TEST_F(LuaVec2, Abs) { check("Vec2_Abs"); }
|
||||
69
tests/lua/unit_test_lua_vector3.cpp
Normal file
69
tests/lua/unit_test_lua_vector3.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
//
|
||||
// Created by orange on 07.03.2026.
|
||||
//
|
||||
#include <gtest/gtest.h>
|
||||
#include <lua.hpp>
|
||||
#include <omath/lua/lua.hpp>
|
||||
|
||||
class LuaVec3 : public ::testing::Test
|
||||
{
|
||||
protected:
|
||||
lua_State* L = nullptr;
|
||||
|
||||
void SetUp() override
|
||||
{
|
||||
L = luaL_newstate();
|
||||
luaL_openlibs(L);
|
||||
omath::lua::LuaInterpreter::register_lib(L);
|
||||
if (luaL_dofile(L, LUA_SCRIPTS_DIR "/vec3_tests.lua") != LUA_OK)
|
||||
FAIL() << lua_tostring(L, -1);
|
||||
}
|
||||
|
||||
void TearDown() override { lua_close(L); }
|
||||
|
||||
void check(const char* func_name)
|
||||
{
|
||||
lua_getglobal(L, func_name);
|
||||
if (lua_pcall(L, 0, 0, 0) != LUA_OK)
|
||||
{
|
||||
FAIL() << lua_tostring(L, -1);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(LuaVec3, Constructor_default) { check("Vec3_Constructor_default"); }
|
||||
TEST_F(LuaVec3, Constructor_xyz) { check("Vec3_Constructor_xyz"); }
|
||||
TEST_F(LuaVec3, Field_mutation) { check("Vec3_Field_mutation"); }
|
||||
TEST_F(LuaVec3, Addition) { check("Vec3_Addition"); }
|
||||
TEST_F(LuaVec3, Subtraction) { check("Vec3_Subtraction"); }
|
||||
TEST_F(LuaVec3, UnaryMinus) { check("Vec3_UnaryMinus"); }
|
||||
TEST_F(LuaVec3, Multiplication_scalar) { check("Vec3_Multiplication_scalar"); }
|
||||
TEST_F(LuaVec3, Multiplication_scalar_reversed) { check("Vec3_Multiplication_scalar_reversed"); }
|
||||
TEST_F(LuaVec3, Multiplication_vec) { check("Vec3_Multiplication_vec"); }
|
||||
TEST_F(LuaVec3, Division_scalar) { check("Vec3_Division_scalar"); }
|
||||
TEST_F(LuaVec3, Division_vec) { check("Vec3_Division_vec"); }
|
||||
TEST_F(LuaVec3, EqualTo_true) { check("Vec3_EqualTo_true"); }
|
||||
TEST_F(LuaVec3, EqualTo_false) { check("Vec3_EqualTo_false"); }
|
||||
TEST_F(LuaVec3, LessThan) { check("Vec3_LessThan"); }
|
||||
TEST_F(LuaVec3, LessThanOrEqual) { check("Vec3_LessThanOrEqual"); }
|
||||
TEST_F(LuaVec3, ToString) { check("Vec3_ToString"); }
|
||||
TEST_F(LuaVec3, Length) { check("Vec3_Length"); }
|
||||
TEST_F(LuaVec3, Length2d) { check("Vec3_Length2d"); }
|
||||
TEST_F(LuaVec3, LengthSqr) { check("Vec3_LengthSqr"); }
|
||||
TEST_F(LuaVec3, Normalized) { check("Vec3_Normalized"); }
|
||||
TEST_F(LuaVec3, Dot_perpendicular) { check("Vec3_Dot_perpendicular"); }
|
||||
TEST_F(LuaVec3, Dot_parallel) { check("Vec3_Dot_parallel"); }
|
||||
TEST_F(LuaVec3, Cross) { check("Vec3_Cross"); }
|
||||
TEST_F(LuaVec3, DistanceTo) { check("Vec3_DistanceTo"); }
|
||||
TEST_F(LuaVec3, DistanceToSqr) { check("Vec3_DistanceToSqr"); }
|
||||
TEST_F(LuaVec3, Sum) { check("Vec3_Sum"); }
|
||||
TEST_F(LuaVec3, Sum2d) { check("Vec3_Sum2d"); }
|
||||
TEST_F(LuaVec3, Abs) { check("Vec3_Abs"); }
|
||||
TEST_F(LuaVec3, PointToSameDirection_true) { check("Vec3_PointToSameDirection_true"); }
|
||||
TEST_F(LuaVec3, PointToSameDirection_false) { check("Vec3_PointToSameDirection_false"); }
|
||||
TEST_F(LuaVec3, IsPerpendicular_true) { check("Vec3_IsPerpendicular_true"); }
|
||||
TEST_F(LuaVec3, IsPerpendicular_false) { check("Vec3_IsPerpendicular_false"); }
|
||||
TEST_F(LuaVec3, AngleBetween_90deg) { check("Vec3_AngleBetween_90deg"); }
|
||||
TEST_F(LuaVec3, AngleBetween_zero_vector_error) { check("Vec3_AngleBetween_zero_vector_error"); }
|
||||
TEST_F(LuaVec3, AsTable) { check("Vec3_AsTable"); }
|
||||
57
tests/lua/unit_test_lua_vector4.cpp
Normal file
57
tests/lua/unit_test_lua_vector4.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
//
|
||||
// Created by orange on 07.03.2026.
|
||||
//
|
||||
#include <gtest/gtest.h>
|
||||
#include <lua.hpp>
|
||||
#include <omath/lua/lua.hpp>
|
||||
|
||||
class LuaVec4 : public ::testing::Test
|
||||
{
|
||||
protected:
|
||||
lua_State* L = nullptr;
|
||||
|
||||
void SetUp() override
|
||||
{
|
||||
L = luaL_newstate();
|
||||
luaL_openlibs(L);
|
||||
omath::lua::LuaInterpreter::register_lib(L);
|
||||
if (luaL_dofile(L, LUA_SCRIPTS_DIR "/vec4_tests.lua") != LUA_OK)
|
||||
FAIL() << lua_tostring(L, -1);
|
||||
}
|
||||
|
||||
void TearDown() override { lua_close(L); }
|
||||
|
||||
void check(const char* func_name)
|
||||
{
|
||||
lua_getglobal(L, func_name);
|
||||
if (lua_pcall(L, 0, 0, 0) != LUA_OK)
|
||||
{
|
||||
FAIL() << lua_tostring(L, -1);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(LuaVec4, Constructor_default) { check("Vec4_Constructor_default"); }
|
||||
TEST_F(LuaVec4, Constructor_xyzw) { check("Vec4_Constructor_xyzw"); }
|
||||
TEST_F(LuaVec4, Field_mutation) { check("Vec4_Field_mutation"); }
|
||||
TEST_F(LuaVec4, Addition) { check("Vec4_Addition"); }
|
||||
TEST_F(LuaVec4, Subtraction) { check("Vec4_Subtraction"); }
|
||||
TEST_F(LuaVec4, UnaryMinus) { check("Vec4_UnaryMinus"); }
|
||||
TEST_F(LuaVec4, Multiplication_scalar) { check("Vec4_Multiplication_scalar"); }
|
||||
TEST_F(LuaVec4, Multiplication_scalar_reversed) { check("Vec4_Multiplication_scalar_reversed"); }
|
||||
TEST_F(LuaVec4, Multiplication_vec) { check("Vec4_Multiplication_vec"); }
|
||||
TEST_F(LuaVec4, Division_scalar) { check("Vec4_Division_scalar"); }
|
||||
TEST_F(LuaVec4, Division_vec) { check("Vec4_Division_vec"); }
|
||||
TEST_F(LuaVec4, EqualTo_true) { check("Vec4_EqualTo_true"); }
|
||||
TEST_F(LuaVec4, EqualTo_false) { check("Vec4_EqualTo_false"); }
|
||||
TEST_F(LuaVec4, LessThan) { check("Vec4_LessThan"); }
|
||||
TEST_F(LuaVec4, LessThanOrEqual) { check("Vec4_LessThanOrEqual"); }
|
||||
TEST_F(LuaVec4, ToString) { check("Vec4_ToString"); }
|
||||
TEST_F(LuaVec4, Length) { check("Vec4_Length"); }
|
||||
TEST_F(LuaVec4, LengthSqr) { check("Vec4_LengthSqr"); }
|
||||
TEST_F(LuaVec4, Dot) { check("Vec4_Dot"); }
|
||||
TEST_F(LuaVec4, Dot_perpendicular) { check("Vec4_Dot_perpendicular"); }
|
||||
TEST_F(LuaVec4, Sum) { check("Vec4_Sum"); }
|
||||
TEST_F(LuaVec4, Abs) { check("Vec4_Abs"); }
|
||||
TEST_F(LuaVec4, Clamp) { check("Vec4_Clamp"); }
|
||||
102
tests/lua/vec2_tests.lua
Normal file
102
tests/lua/vec2_tests.lua
Normal file
@@ -0,0 +1,102 @@
|
||||
local function approx(a, b) return math.abs(a - b) < 1e-5 end
|
||||
|
||||
function Vec2_Constructor_default()
|
||||
local v = omath.Vec2.new()
|
||||
assert(v.x == 0 and v.y == 0)
|
||||
end
|
||||
|
||||
function Vec2_Constructor_xy()
|
||||
local v = omath.Vec2.new(3, 4)
|
||||
assert(v.x == 3 and v.y == 4)
|
||||
end
|
||||
|
||||
function Vec2_Field_mutation()
|
||||
local v = omath.Vec2.new(1, 2)
|
||||
v.x = 9; v.y = 8
|
||||
assert(v.x == 9 and v.y == 8)
|
||||
end
|
||||
|
||||
function Vec2_Addition()
|
||||
local c = omath.Vec2.new(1, 2) + omath.Vec2.new(3, 4)
|
||||
assert(c.x == 4 and c.y == 6)
|
||||
end
|
||||
|
||||
function Vec2_Subtraction()
|
||||
local c = omath.Vec2.new(5, 7) - omath.Vec2.new(2, 3)
|
||||
assert(c.x == 3 and c.y == 4)
|
||||
end
|
||||
|
||||
function Vec2_UnaryMinus()
|
||||
local b = -omath.Vec2.new(1, 2)
|
||||
assert(b.x == -1 and b.y == -2)
|
||||
end
|
||||
|
||||
function Vec2_Multiplication_scalar()
|
||||
local b = omath.Vec2.new(2, 3) * 2.0
|
||||
assert(b.x == 4 and b.y == 6)
|
||||
end
|
||||
|
||||
function Vec2_Multiplication_scalar_reversed()
|
||||
local b = 2.0 * omath.Vec2.new(2, 3)
|
||||
assert(b.x == 4 and b.y == 6)
|
||||
end
|
||||
|
||||
function Vec2_Division_scalar()
|
||||
local b = omath.Vec2.new(4, 6) / 2.0
|
||||
assert(b.x == 2 and b.y == 3)
|
||||
end
|
||||
|
||||
function Vec2_EqualTo_true()
|
||||
assert(omath.Vec2.new(1, 2) == omath.Vec2.new(1, 2))
|
||||
end
|
||||
|
||||
function Vec2_EqualTo_false()
|
||||
assert(not (omath.Vec2.new(1, 2) == omath.Vec2.new(9, 9)))
|
||||
end
|
||||
|
||||
function Vec2_LessThan()
|
||||
assert(omath.Vec2.new(1, 0) < omath.Vec2.new(3, 4))
|
||||
end
|
||||
|
||||
function Vec2_LessThanOrEqual()
|
||||
-- (3,4) and (4,3) both have length 5
|
||||
assert(omath.Vec2.new(3, 4) <= omath.Vec2.new(4, 3))
|
||||
end
|
||||
|
||||
function Vec2_ToString()
|
||||
assert(tostring(omath.Vec2.new(1, 2)) == "Vec2(1, 2)")
|
||||
end
|
||||
|
||||
function Vec2_Length()
|
||||
assert(approx(omath.Vec2.new(3, 4):length(), 5.0))
|
||||
end
|
||||
|
||||
function Vec2_LengthSqr()
|
||||
assert(omath.Vec2.new(3, 4):length_sqr() == 25.0)
|
||||
end
|
||||
|
||||
function Vec2_Normalized()
|
||||
local n = omath.Vec2.new(3, 4):normalized()
|
||||
assert(approx(n.x, 0.6) and approx(n.y, 0.8))
|
||||
end
|
||||
|
||||
function Vec2_Dot()
|
||||
assert(omath.Vec2.new(1, 2):dot(omath.Vec2.new(3, 4)) == 11.0)
|
||||
end
|
||||
|
||||
function Vec2_DistanceTo()
|
||||
assert(approx(omath.Vec2.new(0, 0):distance_to(omath.Vec2.new(3, 4)), 5.0))
|
||||
end
|
||||
|
||||
function Vec2_DistanceToSqr()
|
||||
assert(omath.Vec2.new(0, 0):distance_to_sqr(omath.Vec2.new(3, 4)) == 25.0)
|
||||
end
|
||||
|
||||
function Vec2_Sum()
|
||||
assert(omath.Vec2.new(3, 4):sum() == 7.0)
|
||||
end
|
||||
|
||||
function Vec2_Abs()
|
||||
local a = omath.Vec2.new(-3, -4):abs()
|
||||
assert(a.x == 3 and a.y == 4)
|
||||
end
|
||||
163
tests/lua/vec3_tests.lua
Normal file
163
tests/lua/vec3_tests.lua
Normal file
@@ -0,0 +1,163 @@
|
||||
local function approx(a, b, eps) return math.abs(a - b) < (eps or 1e-5) end
|
||||
|
||||
function Vec3_Constructor_default()
|
||||
local v = omath.Vec3.new()
|
||||
assert(v.x == 0 and v.y == 0 and v.z == 0)
|
||||
end
|
||||
|
||||
function Vec3_Constructor_xyz()
|
||||
local v = omath.Vec3.new(1, 2, 3)
|
||||
assert(v.x == 1 and v.y == 2 and v.z == 3)
|
||||
end
|
||||
|
||||
function Vec3_Field_mutation()
|
||||
local v = omath.Vec3.new(1, 2, 3)
|
||||
v.x = 9; v.y = 8; v.z = 7
|
||||
assert(v.x == 9 and v.y == 8 and v.z == 7)
|
||||
end
|
||||
|
||||
function Vec3_Addition()
|
||||
local c = omath.Vec3.new(1, 2, 3) + omath.Vec3.new(4, 5, 6)
|
||||
assert(c.x == 5 and c.y == 7 and c.z == 9)
|
||||
end
|
||||
|
||||
function Vec3_Subtraction()
|
||||
local c = omath.Vec3.new(4, 5, 6) - omath.Vec3.new(1, 2, 3)
|
||||
assert(c.x == 3 and c.y == 3 and c.z == 3)
|
||||
end
|
||||
|
||||
function Vec3_UnaryMinus()
|
||||
local b = -omath.Vec3.new(1, 2, 3)
|
||||
assert(b.x == -1 and b.y == -2 and b.z == -3)
|
||||
end
|
||||
|
||||
function Vec3_Multiplication_scalar()
|
||||
local b = omath.Vec3.new(1, 2, 3) * 2.0
|
||||
assert(b.x == 2 and b.y == 4 and b.z == 6)
|
||||
end
|
||||
|
||||
function Vec3_Multiplication_scalar_reversed()
|
||||
local b = 2.0 * omath.Vec3.new(1, 2, 3)
|
||||
assert(b.x == 2 and b.y == 4 and b.z == 6)
|
||||
end
|
||||
|
||||
function Vec3_Multiplication_vec()
|
||||
local c = omath.Vec3.new(2, 3, 4) * omath.Vec3.new(2, 2, 2)
|
||||
assert(c.x == 4 and c.y == 6 and c.z == 8)
|
||||
end
|
||||
|
||||
function Vec3_Division_scalar()
|
||||
local b = omath.Vec3.new(2, 4, 6) / 2.0
|
||||
assert(b.x == 1 and b.y == 2 and b.z == 3)
|
||||
end
|
||||
|
||||
function Vec3_Division_vec()
|
||||
local c = omath.Vec3.new(4, 6, 8) / omath.Vec3.new(2, 2, 2)
|
||||
assert(c.x == 2 and c.y == 3 and c.z == 4)
|
||||
end
|
||||
|
||||
function Vec3_EqualTo_true()
|
||||
assert(omath.Vec3.new(1, 2, 3) == omath.Vec3.new(1, 2, 3))
|
||||
end
|
||||
|
||||
function Vec3_EqualTo_false()
|
||||
assert(not (omath.Vec3.new(1, 2, 3) == omath.Vec3.new(9, 9, 9)))
|
||||
end
|
||||
|
||||
function Vec3_LessThan()
|
||||
assert(omath.Vec3.new(1, 0, 0) < omath.Vec3.new(3, 4, 0))
|
||||
end
|
||||
|
||||
function Vec3_LessThanOrEqual()
|
||||
-- (0,3,4) and (0,4,3) both have length 5
|
||||
assert(omath.Vec3.new(0, 3, 4) <= omath.Vec3.new(0, 4, 3))
|
||||
end
|
||||
|
||||
function Vec3_ToString()
|
||||
assert(tostring(omath.Vec3.new(1, 2, 3)) == "Vec3(1, 2, 3)")
|
||||
end
|
||||
|
||||
function Vec3_Length()
|
||||
assert(approx(omath.Vec3.new(1, 2, 2):length(), 3.0))
|
||||
end
|
||||
|
||||
function Vec3_Length2d()
|
||||
assert(approx(omath.Vec3.new(3, 4, 99):length_2d(), 5.0))
|
||||
end
|
||||
|
||||
function Vec3_LengthSqr()
|
||||
assert(omath.Vec3.new(1, 2, 2):length_sqr() == 9.0)
|
||||
end
|
||||
|
||||
function Vec3_Normalized()
|
||||
local n = omath.Vec3.new(3, 0, 0):normalized()
|
||||
assert(approx(n.x, 1.0) and approx(n.y, 0.0) and approx(n.z, 0.0))
|
||||
end
|
||||
|
||||
function Vec3_Dot_perpendicular()
|
||||
assert(omath.Vec3.new(1, 0, 0):dot(omath.Vec3.new(0, 1, 0)) == 0.0)
|
||||
end
|
||||
|
||||
function Vec3_Dot_parallel()
|
||||
local a = omath.Vec3.new(1, 2, 3)
|
||||
assert(a:dot(a) == 14.0)
|
||||
end
|
||||
|
||||
function Vec3_Cross()
|
||||
local c = omath.Vec3.new(1, 0, 0):cross(omath.Vec3.new(0, 1, 0))
|
||||
assert(approx(c.x, 0) and approx(c.y, 0) and approx(c.z, 1))
|
||||
end
|
||||
|
||||
function Vec3_DistanceTo()
|
||||
assert(approx(omath.Vec3.new(0, 0, 0):distance_to(omath.Vec3.new(1, 2, 2)), 3.0))
|
||||
end
|
||||
|
||||
function Vec3_DistanceToSqr()
|
||||
assert(omath.Vec3.new(0, 0, 0):distance_to_sqr(omath.Vec3.new(1, 2, 2)) == 9.0)
|
||||
end
|
||||
|
||||
function Vec3_Sum()
|
||||
assert(omath.Vec3.new(1, 2, 3):sum() == 6.0)
|
||||
end
|
||||
|
||||
function Vec3_Sum2d()
|
||||
assert(omath.Vec3.new(1, 2, 3):sum_2d() == 3.0)
|
||||
end
|
||||
|
||||
function Vec3_Abs()
|
||||
local a = omath.Vec3.new(-1, -2, -3):abs()
|
||||
assert(a.x == 1 and a.y == 2 and a.z == 3)
|
||||
end
|
||||
|
||||
function Vec3_PointToSameDirection_true()
|
||||
assert(omath.Vec3.new(1, 1, 0):point_to_same_direction(omath.Vec3.new(2, 2, 0)) == true)
|
||||
end
|
||||
|
||||
function Vec3_PointToSameDirection_false()
|
||||
assert(omath.Vec3.new(1, 0, 0):point_to_same_direction(omath.Vec3.new(-1, 0, 0)) == false)
|
||||
end
|
||||
|
||||
function Vec3_IsPerpendicular_true()
|
||||
assert(omath.Vec3.new(1, 0, 0):is_perpendicular(omath.Vec3.new(0, 1, 0)) == true)
|
||||
end
|
||||
|
||||
function Vec3_IsPerpendicular_false()
|
||||
local a = omath.Vec3.new(1, 0, 0)
|
||||
assert(a:is_perpendicular(a) == false)
|
||||
end
|
||||
|
||||
function Vec3_AngleBetween_90deg()
|
||||
local angle, err = omath.Vec3.new(1, 0, 0):angle_between(omath.Vec3.new(0, 1, 0))
|
||||
assert(angle ~= nil, err)
|
||||
assert(math.abs(angle - 90.0) < 1e-3)
|
||||
end
|
||||
|
||||
function Vec3_AngleBetween_zero_vector_error()
|
||||
local angle, err = omath.Vec3.new(0, 0, 0):angle_between(omath.Vec3.new(1, 0, 0))
|
||||
assert(angle == nil and err ~= nil)
|
||||
end
|
||||
|
||||
function Vec3_AsTable()
|
||||
local t = omath.Vec3.new(1, 2, 3):as_table()
|
||||
assert(t.x == 1 and t.y == 2 and t.z == 3)
|
||||
end
|
||||
110
tests/lua/vec4_tests.lua
Normal file
110
tests/lua/vec4_tests.lua
Normal file
@@ -0,0 +1,110 @@
|
||||
local function approx(a, b) return math.abs(a - b) < 1e-5 end
|
||||
|
||||
function Vec4_Constructor_default()
|
||||
local v = omath.Vec4.new()
|
||||
assert(v.x == 0 and v.y == 0 and v.z == 0 and v.w == 0)
|
||||
end
|
||||
|
||||
function Vec4_Constructor_xyzw()
|
||||
local v = omath.Vec4.new(1, 2, 3, 4)
|
||||
assert(v.x == 1 and v.y == 2 and v.z == 3 and v.w == 4)
|
||||
end
|
||||
|
||||
function Vec4_Field_mutation()
|
||||
local v = omath.Vec4.new(1, 2, 3, 4)
|
||||
v.w = 99
|
||||
assert(v.w == 99)
|
||||
end
|
||||
|
||||
function Vec4_Addition()
|
||||
local c = omath.Vec4.new(1, 2, 3, 4) + omath.Vec4.new(4, 3, 2, 1)
|
||||
assert(c.x == 5 and c.y == 5 and c.z == 5 and c.w == 5)
|
||||
end
|
||||
|
||||
function Vec4_Subtraction()
|
||||
local c = omath.Vec4.new(5, 5, 5, 5) - omath.Vec4.new(1, 2, 3, 4)
|
||||
assert(c.x == 4 and c.y == 3 and c.z == 2 and c.w == 1)
|
||||
end
|
||||
|
||||
function Vec4_UnaryMinus()
|
||||
local b = -omath.Vec4.new(1, 2, 3, 4)
|
||||
assert(b.x == -1 and b.y == -2 and b.z == -3 and b.w == -4)
|
||||
end
|
||||
|
||||
function Vec4_Multiplication_scalar()
|
||||
local b = omath.Vec4.new(1, 2, 3, 4) * 2.0
|
||||
assert(b.x == 2 and b.y == 4 and b.z == 6 and b.w == 8)
|
||||
end
|
||||
|
||||
function Vec4_Multiplication_scalar_reversed()
|
||||
local b = 2.0 * omath.Vec4.new(1, 2, 3, 4)
|
||||
assert(b.x == 2 and b.y == 4 and b.z == 6 and b.w == 8)
|
||||
end
|
||||
|
||||
function Vec4_Multiplication_vec()
|
||||
local c = omath.Vec4.new(2, 3, 4, 5) * omath.Vec4.new(2, 2, 2, 2)
|
||||
assert(c.x == 4 and c.y == 6 and c.z == 8 and c.w == 10)
|
||||
end
|
||||
|
||||
function Vec4_Division_scalar()
|
||||
local b = omath.Vec4.new(2, 4, 6, 8) / 2.0
|
||||
assert(b.x == 1 and b.y == 2 and b.z == 3 and b.w == 4)
|
||||
end
|
||||
|
||||
function Vec4_Division_vec()
|
||||
local c = omath.Vec4.new(4, 6, 8, 10) / omath.Vec4.new(2, 2, 2, 2)
|
||||
assert(c.x == 2 and c.y == 3 and c.z == 4 and c.w == 5)
|
||||
end
|
||||
|
||||
function Vec4_EqualTo_true()
|
||||
assert(omath.Vec4.new(1, 2, 3, 4) == omath.Vec4.new(1, 2, 3, 4))
|
||||
end
|
||||
|
||||
function Vec4_EqualTo_false()
|
||||
assert(not (omath.Vec4.new(1, 2, 3, 4) == omath.Vec4.new(9, 9, 9, 9)))
|
||||
end
|
||||
|
||||
function Vec4_LessThan()
|
||||
assert(omath.Vec4.new(1, 0, 0, 0) < omath.Vec4.new(0, 0, 3, 4))
|
||||
end
|
||||
|
||||
function Vec4_LessThanOrEqual()
|
||||
-- (0,0,3,4) and (0,0,4,3) both have length 5
|
||||
assert(omath.Vec4.new(0, 0, 3, 4) <= omath.Vec4.new(0, 0, 4, 3))
|
||||
end
|
||||
|
||||
function Vec4_ToString()
|
||||
assert(tostring(omath.Vec4.new(1, 2, 3, 4)) == "Vec4(1, 2, 3, 4)")
|
||||
end
|
||||
|
||||
function Vec4_Length()
|
||||
assert(approx(omath.Vec4.new(0, 0, 3, 4):length(), 5.0))
|
||||
end
|
||||
|
||||
function Vec4_LengthSqr()
|
||||
assert(omath.Vec4.new(0, 0, 3, 4):length_sqr() == 25.0)
|
||||
end
|
||||
|
||||
function Vec4_Dot()
|
||||
local a = omath.Vec4.new(1, 2, 3, 4)
|
||||
assert(a:dot(a) == 30.0)
|
||||
end
|
||||
|
||||
function Vec4_Dot_perpendicular()
|
||||
assert(omath.Vec4.new(1, 0, 0, 0):dot(omath.Vec4.new(0, 1, 0, 0)) == 0.0)
|
||||
end
|
||||
|
||||
function Vec4_Sum()
|
||||
assert(omath.Vec4.new(1, 2, 3, 4):sum() == 10.0)
|
||||
end
|
||||
|
||||
function Vec4_Abs()
|
||||
local a = omath.Vec4.new(-1, -2, -3, -4):abs()
|
||||
assert(a.x == 1 and a.y == 2 and a.z == 3 and a.w == 4)
|
||||
end
|
||||
|
||||
function Vec4_Clamp()
|
||||
local v = omath.Vec4.new(5, -3, 10, 99)
|
||||
v:clamp(0, 7)
|
||||
assert(v.x == 5 and v.y == 0 and v.z == 7)
|
||||
end
|
||||
29
tests/lua/vector2_test.lua
Normal file
29
tests/lua/vector2_test.lua
Normal file
@@ -0,0 +1,29 @@
|
||||
local a = omath.Vec2.new(1, 2)
|
||||
local b = omath.Vec2.new(10, 20)
|
||||
|
||||
-- Operators
|
||||
local c = a + b
|
||||
local d = a - b
|
||||
local e = a * 2.0
|
||||
local f = -a
|
||||
print("a + b = " .. tostring(c))
|
||||
print("a - b = " .. tostring(d))
|
||||
print("a * 2 = " .. tostring(e))
|
||||
print("-a = " .. tostring(f))
|
||||
print("a == Vec2(1,2): " .. tostring(a == omath.Vec2.new(1, 2)))
|
||||
print("a < b: " .. tostring(a < b))
|
||||
|
||||
-- Field access + mutation
|
||||
print("c.x = " .. c.x .. ", c.y = " .. c.y)
|
||||
c.x = 99
|
||||
print("c.x after mutation = " .. c.x)
|
||||
|
||||
-- Methods
|
||||
print("a:length() = " .. a:length())
|
||||
print("a:length_sqr() = " .. a:length_sqr())
|
||||
print("a:normalized() = " .. tostring(a:normalized()))
|
||||
print("a:dot(b) = " .. a:dot(b))
|
||||
print("a:distance_to(b) = " .. a:distance_to(b))
|
||||
print("a:distance_to_sqr(b) = " .. a:distance_to_sqr(b))
|
||||
print("a:sum() = " .. a:sum())
|
||||
print("a:abs() = " .. tostring(a:abs()))
|
||||
55
tests/lua/vector3_test.lua
Normal file
55
tests/lua/vector3_test.lua
Normal file
@@ -0,0 +1,55 @@
|
||||
local a = omath.Vec3.new(1, 0, 0)
|
||||
local b = omath.Vec3.new(0, 1, 0)
|
||||
|
||||
-- Operators
|
||||
local c = a + b
|
||||
local d = a - b
|
||||
local e = a * 2.0
|
||||
local f = -a
|
||||
print("a + b = " .. tostring(c))
|
||||
print("a - b = " .. tostring(d))
|
||||
print("a * 2 = " .. tostring(e))
|
||||
print("-a = " .. tostring(f))
|
||||
print("a == Vec3(1,2,3): " .. tostring(a == omath.Vec3.new(1, 2, 3)))
|
||||
print("a < b: " .. tostring(a < b))
|
||||
|
||||
-- Field access + mutation
|
||||
print("c.x = " .. c.x .. ", c.y = " .. c.y .. ", c.z = " .. c.z)
|
||||
c.x = 99
|
||||
print("c.x after mutation = " .. c.x)
|
||||
|
||||
-- Methods
|
||||
print("a:length() = " .. a:length())
|
||||
print("a:length_2d() = " .. a:length_2d())
|
||||
print("a:length_sqr() = " .. a:length_sqr())
|
||||
print("a:normalized() = " .. tostring(a:normalized()))
|
||||
print("a:dot(b) = " .. a:dot(b))
|
||||
print("a:cross(b) = " .. tostring(a:cross(b)))
|
||||
print("a:distance_to(b) = " .. a:distance_to(b))
|
||||
print("a:distance_to_sqr(b) = " .. a:distance_to_sqr(b))
|
||||
print("a:abs() = " .. tostring(a:abs()))
|
||||
print("a:sum() = " .. a:sum())
|
||||
print("a:sum_2d() = " .. a:sum_2d())
|
||||
print("a:point_to_same_direction(b) = " .. tostring(a:point_to_same_direction(b)))
|
||||
print("a:is_perpendicular(b) = " .. tostring(a:is_perpendicular(b)))
|
||||
|
||||
-- angle_between
|
||||
local angle, err = a:angle_between(b)
|
||||
if angle then
|
||||
print("angle_between = " .. angle .. " degrees")
|
||||
else
|
||||
print("angle_between error: " .. err)
|
||||
end
|
||||
|
||||
-- Zero vector edge case
|
||||
local zero = omath.Vec3.new(0, 0, 0)
|
||||
local ang2, err2 = zero:angle_between(a)
|
||||
if ang2 then
|
||||
print("zero angle = " .. ang2)
|
||||
else
|
||||
print("zero angle error: " .. err2)
|
||||
end
|
||||
|
||||
-- as_table
|
||||
local t = a:as_table()
|
||||
print("as_table: x=" .. t.x .. " y=" .. t.y .. " z=" .. t.z)
|
||||
31
tests/lua/vector4_test.lua
Normal file
31
tests/lua/vector4_test.lua
Normal file
@@ -0,0 +1,31 @@
|
||||
local a = omath.Vec4.new(1, 2, 3, 4)
|
||||
local b = omath.Vec4.new(10, 20, 30, 40)
|
||||
|
||||
-- Operators
|
||||
local c = a + b
|
||||
local d = a - b
|
||||
local e = a * 2.0
|
||||
local f = -a
|
||||
print("a + b = " .. tostring(c))
|
||||
print("a - b = " .. tostring(d))
|
||||
print("a * 2 = " .. tostring(e))
|
||||
print("-a = " .. tostring(f))
|
||||
print("a == Vec4(1,2,3,4): " .. tostring(a == omath.Vec4.new(1, 2, 3, 4)))
|
||||
print("a < b: " .. tostring(a < b))
|
||||
|
||||
-- Field access + mutation
|
||||
print("c.x=" .. c.x .. " c.y=" .. c.y .. " c.z=" .. c.z .. " c.w=" .. c.w)
|
||||
c.w = 99
|
||||
print("c.w after mutation = " .. c.w)
|
||||
|
||||
-- Methods
|
||||
print("a:length() = " .. a:length())
|
||||
print("a:length_sqr() = " .. a:length_sqr())
|
||||
print("a:dot(b) = " .. a:dot(b))
|
||||
print("a:sum() = " .. a:sum())
|
||||
print("a:abs() = " .. tostring(a:abs()))
|
||||
|
||||
-- clamp
|
||||
local clamped = omath.Vec4.new(5, -3, 10, 1)
|
||||
clamped:clamp(0, 7)
|
||||
print("clamp([5,-3,10,1], 0, 7).x=" .. clamped.x .. " .y=" .. clamped.y .. " .z=" .. clamped.z)
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"default-registry": {
|
||||
"kind": "git",
|
||||
"baseline": "b1b19307e2d2ec1eefbdb7ea069de7d4bcd31f01",
|
||||
"baseline": "efa4634bd526b87559684607d2cbbdeeec0f07d8",
|
||||
"repository": "https://github.com/microsoft/vcpkg"
|
||||
},
|
||||
"registries": [
|
||||
|
||||
18
vcpkg.json
18
vcpkg.json
@@ -17,7 +17,7 @@
|
||||
],
|
||||
"features": {
|
||||
"avx2": {
|
||||
"description": "Omath will use AVX2 to boost performance",
|
||||
"description": "omath will use AVX2 to boost performance",
|
||||
"supports": "!arm"
|
||||
},
|
||||
"benchmark": {
|
||||
@@ -35,7 +35,7 @@
|
||||
]
|
||||
},
|
||||
"imgui": {
|
||||
"description": "Omath will define method to convert omath types to imgui types",
|
||||
"description": "omath will define method to convert omath types to imgui types",
|
||||
"dependencies": [
|
||||
"imgui"
|
||||
]
|
||||
@@ -45,6 +45,20 @@
|
||||
"dependencies": [
|
||||
"gtest"
|
||||
]
|
||||
},
|
||||
"lua": {
|
||||
"description": "lua support for omath",
|
||||
"dependencies": [
|
||||
"lua",
|
||||
"sol2"
|
||||
]
|
||||
},
|
||||
"physx": {
|
||||
"description": "PhysX-backed collider implementations",
|
||||
"dependencies": [
|
||||
"physx"
|
||||
],
|
||||
"supports": "(windows & x64 & !mingw & !uwp) | (linux & x64) | (linux & arm64)"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user