mirror of
https://github.com/orange-cpp/omath.git
synced 2026-04-19 10:43:27 +00:00
Compare commits
1 Commits
feature/ph
...
dd59579290
| Author | SHA1 | Date | |
|---|---|---|---|
| dd59579290 |
@@ -34,8 +34,6 @@ option(OMATH_ENABLE_FORCE_INLINE
|
|||||||
|
|
||||||
option(OMATH_ENABLE_LUA
|
option(OMATH_ENABLE_LUA
|
||||||
"omath bindings for lua" OFF)
|
"omath bindings for lua" OFF)
|
||||||
option(OMATH_ENABLE_PHYSX
|
|
||||||
"PhysX-backed collider implementations" OFF)
|
|
||||||
if(VCPKG_MANIFEST_FEATURES)
|
if(VCPKG_MANIFEST_FEATURES)
|
||||||
foreach(omath_feature IN LISTS VCPKG_MANIFEST_FEATURES)
|
foreach(omath_feature IN LISTS VCPKG_MANIFEST_FEATURES)
|
||||||
if(omath_feature STREQUAL "imgui")
|
if(omath_feature STREQUAL "imgui")
|
||||||
@@ -50,8 +48,6 @@ if(VCPKG_MANIFEST_FEATURES)
|
|||||||
set(OMATH_BUILD_EXAMPLES ON)
|
set(OMATH_BUILD_EXAMPLES ON)
|
||||||
elseif(omath_feature STREQUAL "lua")
|
elseif(omath_feature STREQUAL "lua")
|
||||||
set(OMATH_ENABLE_LUA ON)
|
set(OMATH_ENABLE_LUA ON)
|
||||||
elseif(omath_feature STREQUAL "physx")
|
|
||||||
set(OMATH_ENABLE_PHYSX ON)
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
endforeach()
|
endforeach()
|
||||||
@@ -81,8 +77,6 @@ if(${PROJECT_IS_TOP_LEVEL})
|
|||||||
message(STATUS "[${PROJECT_NAME}]: Building using vcpkg ${OMATH_BUILD_VIA_VCPKG}")
|
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}]: Coverage feature status ${OMATH_ENABLE_COVERAGE}")
|
||||||
message(STATUS "[${PROJECT_NAME}]: Valgrind feature status ${OMATH_ENABLE_VALGRIND}")
|
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()
|
endif()
|
||||||
|
|
||||||
file(GLOB_RECURSE OMATH_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp")
|
file(GLOB_RECURSE OMATH_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp")
|
||||||
@@ -105,13 +99,6 @@ if (OMATH_ENABLE_LUA)
|
|||||||
target_include_directories(${PROJECT_NAME} PRIVATE ${SOL2_INCLUDE_DIRS})
|
target_include_directories(${PROJECT_NAME} PRIVATE ${SOL2_INCLUDE_DIRS})
|
||||||
endif ()
|
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})
|
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
|
||||||
|
|
||||||
target_compile_definitions(${PROJECT_NAME} PUBLIC OMATH_VERSION="${PROJECT_VERSION}")
|
target_compile_definitions(${PROJECT_NAME} PUBLIC OMATH_VERSION="${PROJECT_VERSION}")
|
||||||
@@ -203,12 +190,6 @@ elseif(OMATH_THREAT_WARNING_AS_ERROR)
|
|||||||
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -Wpedantic -Werror)
|
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -Wpedantic -Werror)
|
||||||
endif()
|
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
|
# Windows SDK redefine min/max via preprocessor and break std::min and std::max
|
||||||
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
|
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
|
||||||
target_compile_definitions(${PROJECT_NAME} INTERFACE NOMINMAX)
|
target_compile_definitions(${PROJECT_NAME} INTERFACE NOMINMAX)
|
||||||
|
|||||||
@@ -145,7 +145,7 @@
|
|||||||
"hidden": true,
|
"hidden": true,
|
||||||
"inherits": ["linux-base", "vcpkg-base"],
|
"inherits": ["linux-base", "vcpkg-base"],
|
||||||
"cacheVariables": {
|
"cacheVariables": {
|
||||||
"VCPKG_MANIFEST_FEATURES": "tests;imgui;avx2;lua;physx"
|
"VCPKG_MANIFEST_FEATURES": "tests;imgui;avx2;lua"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -9,7 +9,6 @@
|
|||||||
[](https://www.codefactor.io/repository/github/orange-cpp/omath)
|
[](https://www.codefactor.io/repository/github/orange-cpp/omath)
|
||||||

|

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

|
|
||||||

|

|
||||||
[](https://discord.gg/eDgdaWbqwZ)
|
[](https://discord.gg/eDgdaWbqwZ)
|
||||||
[](https://t.me/orangennotes)
|
[](https://t.me/orangennotes)
|
||||||
@@ -84,7 +83,6 @@ if (auto screen = camera.world_to_screen(world_position)) {
|
|||||||
- **Engine support**: Supports coordinate systems of **Source, Unity, Unreal, Frostbite, IWEngine, CryEngine and canonical OpenGL**.
|
- **Engine support**: Supports coordinate systems of **Source, Unity, Unreal, Frostbite, IWEngine, CryEngine and canonical OpenGL**.
|
||||||
- **Cross platform**: Supports Windows, MacOS and Linux.
|
- **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.
|
- **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
|
- **Battle tested**: It's already used by some big players on the market like wraith.su and bluedream.ltd
|
||||||
<div align = center>
|
<div align = center>
|
||||||
|
|
||||||
|
|||||||
@@ -1,59 +0,0 @@
|
|||||||
//
|
|
||||||
// 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
|
|
||||||
@@ -1,137 +0,0 @@
|
|||||||
//
|
|
||||||
// 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
|
|
||||||
@@ -1,64 +0,0 @@
|
|||||||
//
|
|
||||||
// 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
|
|
||||||
@@ -1,82 +0,0 @@
|
|||||||
//
|
|
||||||
// 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
|
|
||||||
@@ -3,23 +3,11 @@
|
|||||||
//
|
//
|
||||||
#pragma once
|
#pragma once
|
||||||
#ifdef OMATH_ENABLE_LUA
|
#ifdef OMATH_ENABLE_LUA
|
||||||
#include <sol/forward.hpp>
|
// ReSharper disable once CppInconsistentNaming
|
||||||
|
struct lua_State;
|
||||||
|
|
||||||
namespace omath::lua
|
namespace omath::lua
|
||||||
{
|
{
|
||||||
class LuaInterpreter final
|
void register_lib(lua_State* lua_state);
|
||||||
{
|
|
||||||
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
|
#endif
|
||||||
@@ -6,9 +6,7 @@
|
|||||||
|
|
||||||
#include "omath/linear_algebra/vector3.hpp"
|
#include "omath/linear_algebra/vector3.hpp"
|
||||||
#include <expected>
|
#include <expected>
|
||||||
#include <optional>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace omath::pathfinding
|
namespace omath::pathfinding
|
||||||
@@ -30,20 +28,10 @@ namespace omath::pathfinding
|
|||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
bool empty() const;
|
bool empty() const;
|
||||||
|
|
||||||
// Events -- per-vertex optional tag (e.g. "jump", "teleport")
|
[[nodiscard]] std::vector<uint8_t> serialize() const noexcept;
|
||||||
void set_event(const Vector3<float>& vertex, const std::string_view& event_id);
|
|
||||||
void clear_event(const Vector3<float>& vertex);
|
|
||||||
|
|
||||||
[[nodiscard]]
|
void deserialize(const std::vector<uint8_t>& raw) noexcept;
|
||||||
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;
|
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
|
} // namespace omath::pathfinding
|
||||||
|
|||||||
50
source/lua/engine_types.hpp
Normal file
50
source/lua/engine_types.hpp
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
//
|
||||||
|
// Created by orange on 07.03.2026.
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
#ifdef OMATH_ENABLE_LUA
|
||||||
|
#include "internal.hpp"
|
||||||
|
#include <omath/projection/camera.hpp>
|
||||||
|
#include <omath/trigonometry/angle.hpp>
|
||||||
|
#include <omath/trigonometry/view_angles.hpp>
|
||||||
|
|
||||||
|
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>;
|
||||||
|
|
||||||
|
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()); });
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
#endif
|
||||||
97
source/lua/engine_utils.hpp
Normal file
97
source/lua/engine_utils.hpp
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
//
|
||||||
|
// Created by orange on 07.03.2026.
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
#ifdef OMATH_ENABLE_LUA
|
||||||
|
#include "engine_types.hpp"
|
||||||
|
#include <omath/linear_algebra/vector3.hpp>
|
||||||
|
#include <omath/projection/error_codes.hpp>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
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";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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())};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
#endif
|
||||||
23
source/lua/internal.hpp
Normal file
23
source/lua/internal.hpp
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
//
|
||||||
|
// Created by orange on 07.03.2026.
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
#ifdef OMATH_ENABLE_LUA
|
||||||
|
#include <sol/sol.hpp>
|
||||||
|
|
||||||
|
namespace omath::lua::detail
|
||||||
|
{
|
||||||
|
void register_vec2(sol::table& omath_table);
|
||||||
|
void register_vec3(sol::table& omath_table);
|
||||||
|
void register_vec4(sol::table& omath_table);
|
||||||
|
void register_color(sol::table& omath_table);
|
||||||
|
void register_shared_types(sol::table& omath_table);
|
||||||
|
void register_opengl_engine(sol::table& omath_table);
|
||||||
|
void register_frostbite_engine(sol::table& omath_table);
|
||||||
|
void register_iw_engine(sol::table& omath_table);
|
||||||
|
void register_source_engine(sol::table& omath_table);
|
||||||
|
void register_unity_engine(sol::table& omath_table);
|
||||||
|
void register_unreal_engine(sol::table& omath_table);
|
||||||
|
void register_cry_engine(sol::table& omath_table);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@@ -3,25 +3,29 @@
|
|||||||
//
|
//
|
||||||
#ifdef OMATH_ENABLE_LUA
|
#ifdef OMATH_ENABLE_LUA
|
||||||
#include "lua.hpp"
|
#include "lua.hpp"
|
||||||
#include <sol/sol.hpp>
|
#include "internal.hpp"
|
||||||
#include "omath/lua/lua.hpp"
|
#include "omath/lua/lua.hpp"
|
||||||
|
|
||||||
namespace omath::lua
|
namespace omath::lua
|
||||||
{
|
{
|
||||||
void LuaInterpreter::register_lib(lua_State* lua_state)
|
void register_lib(lua_State* lua_state)
|
||||||
{
|
{
|
||||||
sol::state_view lua(lua_state);
|
sol::state_view lua(lua_state);
|
||||||
|
|
||||||
auto omath_table = lua["omath"].get_or_create<sol::table>();
|
auto omath_table = lua["omath"].get_or_create<sol::table>();
|
||||||
|
|
||||||
register_vec2(omath_table);
|
detail::register_vec2(omath_table);
|
||||||
register_vec3(omath_table);
|
detail::register_vec3(omath_table);
|
||||||
register_vec4(omath_table);
|
detail::register_vec4(omath_table);
|
||||||
register_color(omath_table);
|
detail::register_color(omath_table);
|
||||||
register_triangle(omath_table);
|
detail::register_shared_types(omath_table);
|
||||||
register_shared_types(omath_table);
|
detail::register_opengl_engine(omath_table);
|
||||||
register_engines(omath_table);
|
detail::register_frostbite_engine(omath_table);
|
||||||
register_pattern_scan(omath_table);
|
detail::register_iw_engine(omath_table);
|
||||||
|
detail::register_source_engine(omath_table);
|
||||||
|
detail::register_unity_engine(omath_table);
|
||||||
|
detail::register_unreal_engine(omath_table);
|
||||||
|
detail::register_cry_engine(omath_table);
|
||||||
}
|
}
|
||||||
} // namespace omath::lua
|
} // namespace omath::lua
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -2,45 +2,47 @@
|
|||||||
// Created by orange on 07.03.2026.
|
// Created by orange on 07.03.2026.
|
||||||
//
|
//
|
||||||
#ifdef OMATH_ENABLE_LUA
|
#ifdef OMATH_ENABLE_LUA
|
||||||
#include "omath/lua/lua.hpp"
|
#include "internal.hpp"
|
||||||
#include <sol/sol.hpp>
|
|
||||||
#include <omath/utility/color.hpp>
|
#include <omath/utility/color.hpp>
|
||||||
|
|
||||||
namespace omath::lua
|
namespace omath::lua::detail
|
||||||
{
|
{
|
||||||
void LuaInterpreter::register_color(sol::table& omath_table)
|
void register_color(sol::table& omath_table)
|
||||||
{
|
{
|
||||||
omath_table.new_usertype<omath::Color>(
|
omath_table.new_usertype<omath::Color>(
|
||||||
"Color",
|
"Color",
|
||||||
sol::factories([](float r, float g, float b, float a) { return omath::Color(r, g, b, a); },
|
sol::factories(
|
||||||
|
[](float r, float g, float b, float a) { return omath::Color(r, g, b, a); },
|
||||||
[]() { return omath::Color(); }),
|
[]() { return omath::Color(); }),
|
||||||
|
|
||||||
"from_rgba", [](uint8_t r, uint8_t g, uint8_t b, uint8_t a)
|
"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",
|
return omath::Color::from_rgba(r, g, b, a);
|
||||||
sol::overload([](float h, float s, float v) { return omath::Color::from_hsv(h, s, v); },
|
},
|
||||||
|
"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); }),
|
[](const omath::Hsv& hsv) { return omath::Color::from_hsv(hsv); }),
|
||||||
"red", []() { return omath::Color::red(); }, "green", []() { return omath::Color::green(); }, "blue",
|
"red", []() { return omath::Color::red(); },
|
||||||
[]() { return omath::Color::blue(); },
|
"green", []() { return omath::Color::green(); },
|
||||||
|
"blue", []() { return omath::Color::blue(); },
|
||||||
|
|
||||||
"r", sol::property([](const omath::Color& c) { return c.value().x; }), "g",
|
"r", sol::property([](const omath::Color& c) { return c.value().x; }),
|
||||||
sol::property([](const omath::Color& c) { return c.value().y; }), "b",
|
"g", sol::property([](const omath::Color& c) { return c.value().y; }),
|
||||||
sol::property([](const omath::Color& c) { return c.value().z; }), "a",
|
"b", sol::property([](const omath::Color& c) { return c.value().z; }),
|
||||||
sol::property([](const omath::Color& c) { return c.value().w; }),
|
"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",
|
"to_hsv", &omath::Color::to_hsv,
|
||||||
&omath::Color::set_saturation, "set_value", &omath::Color::set_value, "blend", &omath::Color::blend,
|
"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);
|
sol::meta_function::to_string, &omath::Color::to_string);
|
||||||
|
|
||||||
omath_table.new_usertype<omath::Hsv>(
|
omath_table.new_usertype<omath::Hsv>(
|
||||||
"Hsv", sol::constructors<omath::Hsv()>(), "hue",
|
"Hsv", sol::constructors<omath::Hsv()>(),
|
||||||
sol::property([](const omath::Hsv& h) { return h.hue; }, [](omath::Hsv& h, float val) { h.hue = val; }),
|
"hue", sol::property([](const omath::Hsv& h) { return h.hue; }, [](omath::Hsv& h, float val) { h.hue = val; }),
|
||||||
"saturation",
|
"saturation", sol::property([](const omath::Hsv& h) { return h.saturation; }, [](omath::Hsv& h, float val) { h.saturation = val; }),
|
||||||
sol::property([](const omath::Hsv& h) { return h.saturation; },
|
"value", sol::property([](const omath::Hsv& h) { return h.value; }, [](omath::Hsv& h, float val) { h.value = val; }));
|
||||||
[](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
|
} // namespace omath::lua::detail
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
26
source/lua/lua_engine_cry.cpp
Normal file
26
source/lua/lua_engine_cry.cpp
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
//
|
||||||
|
// Created by orange on 07.03.2026.
|
||||||
|
//
|
||||||
|
#ifdef OMATH_ENABLE_LUA
|
||||||
|
#include "engine_utils.hpp"
|
||||||
|
#include <omath/engines/cry_engine/camera.hpp>
|
||||||
|
#include <omath/engines/cry_engine/constants.hpp>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
struct CryEngineTraits
|
||||||
|
{
|
||||||
|
using PitchAngle = omath::cry_engine::PitchAngle;
|
||||||
|
using ViewAngles = omath::cry_engine::ViewAngles;
|
||||||
|
using Camera = omath::cry_engine::Camera;
|
||||||
|
};
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
namespace omath::lua::detail
|
||||||
|
{
|
||||||
|
void register_cry_engine(sol::table& omath_table)
|
||||||
|
{
|
||||||
|
register_engine<CryEngineTraits>(omath_table, "cry");
|
||||||
|
}
|
||||||
|
} // namespace omath::lua::detail
|
||||||
|
#endif
|
||||||
26
source/lua/lua_engine_frostbite.cpp
Normal file
26
source/lua/lua_engine_frostbite.cpp
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
//
|
||||||
|
// Created by orange on 07.03.2026.
|
||||||
|
//
|
||||||
|
#ifdef OMATH_ENABLE_LUA
|
||||||
|
#include "engine_utils.hpp"
|
||||||
|
#include <omath/engines/frostbite_engine/camera.hpp>
|
||||||
|
#include <omath/engines/frostbite_engine/constants.hpp>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
struct FrostbiteEngineTraits
|
||||||
|
{
|
||||||
|
using PitchAngle = omath::frostbite_engine::PitchAngle;
|
||||||
|
using ViewAngles = omath::frostbite_engine::ViewAngles;
|
||||||
|
using Camera = omath::frostbite_engine::Camera;
|
||||||
|
};
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
namespace omath::lua::detail
|
||||||
|
{
|
||||||
|
void register_frostbite_engine(sol::table& omath_table)
|
||||||
|
{
|
||||||
|
register_engine<FrostbiteEngineTraits>(omath_table, "frostbite");
|
||||||
|
}
|
||||||
|
} // namespace omath::lua::detail
|
||||||
|
#endif
|
||||||
26
source/lua/lua_engine_iw.cpp
Normal file
26
source/lua/lua_engine_iw.cpp
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
//
|
||||||
|
// Created by orange on 07.03.2026.
|
||||||
|
//
|
||||||
|
#ifdef OMATH_ENABLE_LUA
|
||||||
|
#include "engine_utils.hpp"
|
||||||
|
#include <omath/engines/iw_engine/camera.hpp>
|
||||||
|
#include <omath/engines/iw_engine/constants.hpp>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
struct IWEngineTraits
|
||||||
|
{
|
||||||
|
using PitchAngle = omath::iw_engine::PitchAngle;
|
||||||
|
using ViewAngles = omath::iw_engine::ViewAngles;
|
||||||
|
using Camera = omath::iw_engine::Camera;
|
||||||
|
};
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
namespace omath::lua::detail
|
||||||
|
{
|
||||||
|
void register_iw_engine(sol::table& omath_table)
|
||||||
|
{
|
||||||
|
register_engine<IWEngineTraits>(omath_table, "iw");
|
||||||
|
}
|
||||||
|
} // namespace omath::lua::detail
|
||||||
|
#endif
|
||||||
27
source/lua/lua_engine_opengl.cpp
Normal file
27
source/lua/lua_engine_opengl.cpp
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
//
|
||||||
|
// Created by orange on 07.03.2026.
|
||||||
|
//
|
||||||
|
#ifdef OMATH_ENABLE_LUA
|
||||||
|
#include "engine_utils.hpp"
|
||||||
|
#include <omath/engines/opengl_engine/camera.hpp>
|
||||||
|
#include <omath/engines/opengl_engine/constants.hpp>
|
||||||
|
#include <omath/engines/opengl_engine/formulas.hpp>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
struct OpenGLEngineTraits
|
||||||
|
{
|
||||||
|
using PitchAngle = omath::opengl_engine::PitchAngle;
|
||||||
|
using ViewAngles = omath::opengl_engine::ViewAngles;
|
||||||
|
using Camera = omath::opengl_engine::Camera;
|
||||||
|
};
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
namespace omath::lua::detail
|
||||||
|
{
|
||||||
|
void register_opengl_engine(sol::table& omath_table)
|
||||||
|
{
|
||||||
|
register_engine<OpenGLEngineTraits>(omath_table, "opengl");
|
||||||
|
}
|
||||||
|
} // namespace omath::lua::detail
|
||||||
|
#endif
|
||||||
26
source/lua/lua_engine_source.cpp
Normal file
26
source/lua/lua_engine_source.cpp
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
//
|
||||||
|
// Created by orange on 07.03.2026.
|
||||||
|
//
|
||||||
|
#ifdef OMATH_ENABLE_LUA
|
||||||
|
#include "engine_utils.hpp"
|
||||||
|
#include <omath/engines/source_engine/camera.hpp>
|
||||||
|
#include <omath/engines/source_engine/constants.hpp>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
struct SourceEngineTraits
|
||||||
|
{
|
||||||
|
using PitchAngle = omath::source_engine::PitchAngle;
|
||||||
|
using ViewAngles = omath::source_engine::ViewAngles;
|
||||||
|
using Camera = omath::source_engine::Camera;
|
||||||
|
};
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
namespace omath::lua::detail
|
||||||
|
{
|
||||||
|
void register_source_engine(sol::table& omath_table)
|
||||||
|
{
|
||||||
|
register_engine<SourceEngineTraits>(omath_table, "source");
|
||||||
|
}
|
||||||
|
} // namespace omath::lua::detail
|
||||||
|
#endif
|
||||||
26
source/lua/lua_engine_unity.cpp
Normal file
26
source/lua/lua_engine_unity.cpp
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
//
|
||||||
|
// Created by orange on 07.03.2026.
|
||||||
|
//
|
||||||
|
#ifdef OMATH_ENABLE_LUA
|
||||||
|
#include "engine_utils.hpp"
|
||||||
|
#include <omath/engines/unity_engine/camera.hpp>
|
||||||
|
#include <omath/engines/unity_engine/constants.hpp>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
struct UnityEngineTraits
|
||||||
|
{
|
||||||
|
using PitchAngle = omath::unity_engine::PitchAngle;
|
||||||
|
using ViewAngles = omath::unity_engine::ViewAngles;
|
||||||
|
using Camera = omath::unity_engine::Camera;
|
||||||
|
};
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
namespace omath::lua::detail
|
||||||
|
{
|
||||||
|
void register_unity_engine(sol::table& omath_table)
|
||||||
|
{
|
||||||
|
register_engine<UnityEngineTraits>(omath_table, "unity");
|
||||||
|
}
|
||||||
|
} // namespace omath::lua::detail
|
||||||
|
#endif
|
||||||
26
source/lua/lua_engine_unreal.cpp
Normal file
26
source/lua/lua_engine_unreal.cpp
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
//
|
||||||
|
// Created by orange on 07.03.2026.
|
||||||
|
//
|
||||||
|
#ifdef OMATH_ENABLE_LUA
|
||||||
|
#include "engine_utils.hpp"
|
||||||
|
#include <omath/engines/unreal_engine/camera.hpp>
|
||||||
|
#include <omath/engines/unreal_engine/constants.hpp>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
struct UnrealEngineTraits
|
||||||
|
{
|
||||||
|
using PitchAngle = omath::unreal_engine::PitchAngle;
|
||||||
|
using ViewAngles = omath::unreal_engine::ViewAngles;
|
||||||
|
using Camera = omath::unreal_engine::Camera;
|
||||||
|
};
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
namespace omath::lua::detail
|
||||||
|
{
|
||||||
|
void register_unreal_engine(sol::table& omath_table)
|
||||||
|
{
|
||||||
|
register_engine<UnrealEngineTraits>(omath_table, "unreal");
|
||||||
|
}
|
||||||
|
} // namespace omath::lua::detail
|
||||||
|
#endif
|
||||||
@@ -1,227 +0,0 @@
|
|||||||
//
|
|
||||||
// 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
|
|
||||||
@@ -1,104 +0,0 @@
|
|||||||
//
|
|
||||||
// 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
|
|
||||||
42
source/lua/lua_shared_types.cpp
Normal file
42
source/lua/lua_shared_types.cpp
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
//
|
||||||
|
// Created by orange on 07.03.2026.
|
||||||
|
//
|
||||||
|
#ifdef OMATH_ENABLE_LUA
|
||||||
|
#include "engine_types.hpp"
|
||||||
|
|
||||||
|
namespace omath::lua::detail
|
||||||
|
{
|
||||||
|
void 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; }));
|
||||||
|
}
|
||||||
|
} // namespace omath::lua::detail
|
||||||
|
#endif
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
//
|
|
||||||
// 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
|
|
||||||
@@ -2,13 +2,12 @@
|
|||||||
// Created by orange on 07.03.2026.
|
// Created by orange on 07.03.2026.
|
||||||
//
|
//
|
||||||
#ifdef OMATH_ENABLE_LUA
|
#ifdef OMATH_ENABLE_LUA
|
||||||
#include "omath/lua/lua.hpp"
|
#include "internal.hpp"
|
||||||
#include <omath/linear_algebra/vector2.hpp>
|
#include <omath/linear_algebra/vector2.hpp>
|
||||||
#include <sol/sol.hpp>
|
|
||||||
|
|
||||||
namespace omath::lua
|
namespace omath::lua::detail
|
||||||
{
|
{
|
||||||
void LuaInterpreter::register_vec2(sol::table& omath_table)
|
void register_vec2(sol::table& omath_table)
|
||||||
{
|
{
|
||||||
using Vec2f = omath::Vector2<float>;
|
using Vec2f = omath::Vector2<float>;
|
||||||
|
|
||||||
|
|||||||
@@ -2,13 +2,12 @@
|
|||||||
// Created by orange on 07.03.2026.
|
// Created by orange on 07.03.2026.
|
||||||
//
|
//
|
||||||
#ifdef OMATH_ENABLE_LUA
|
#ifdef OMATH_ENABLE_LUA
|
||||||
#include "omath/lua/lua.hpp"
|
#include "internal.hpp"
|
||||||
#include <sol/sol.hpp>
|
|
||||||
#include <omath/linear_algebra/vector3.hpp>
|
#include <omath/linear_algebra/vector3.hpp>
|
||||||
|
|
||||||
namespace omath::lua
|
namespace omath::lua::detail
|
||||||
{
|
{
|
||||||
void LuaInterpreter::register_vec3(sol::table& omath_table)
|
void register_vec3(sol::table& omath_table)
|
||||||
{
|
{
|
||||||
using Vec3f = omath::Vector3<float>;
|
using Vec3f = omath::Vector3<float>;
|
||||||
|
|
||||||
|
|||||||
@@ -2,13 +2,12 @@
|
|||||||
// Created by orange on 07.03.2026.
|
// Created by orange on 07.03.2026.
|
||||||
//
|
//
|
||||||
#ifdef OMATH_ENABLE_LUA
|
#ifdef OMATH_ENABLE_LUA
|
||||||
#include "omath/lua/lua.hpp"
|
#include "internal.hpp"
|
||||||
#include <sol/sol.hpp>
|
|
||||||
#include <omath/linear_algebra/vector4.hpp>
|
#include <omath/linear_algebra/vector4.hpp>
|
||||||
|
|
||||||
namespace omath::lua
|
namespace omath::lua::detail
|
||||||
{
|
{
|
||||||
void LuaInterpreter::register_vec4(sol::table& omath_table)
|
void register_vec4(sol::table& omath_table)
|
||||||
{
|
{
|
||||||
using Vec4f = omath::Vector4<float>;
|
using Vec4f = omath::Vector4<float>;
|
||||||
|
|
||||||
|
|||||||
@@ -3,9 +3,9 @@
|
|||||||
//
|
//
|
||||||
#include "omath/pathfinding/navigation_mesh.hpp"
|
#include "omath/pathfinding/navigation_mesh.hpp"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <sstream>
|
#include <cstring>
|
||||||
|
#include <limits>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
namespace omath::pathfinding
|
namespace omath::pathfinding
|
||||||
{
|
{
|
||||||
std::expected<Vector3<float>, std::string>
|
std::expected<Vector3<float>, std::string>
|
||||||
@@ -30,72 +30,77 @@ namespace omath::pathfinding
|
|||||||
return m_vertex_map.empty();
|
return m_vertex_map.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
void NavigationMesh::set_event(const Vector3<float>& vertex, const std::string_view& event_id)
|
std::vector<uint8_t> NavigationMesh::serialize() const noexcept
|
||||||
{
|
{
|
||||||
if (!m_vertex_map.contains(vertex))
|
std::vector<std::uint8_t> raw;
|
||||||
throw std::invalid_argument(std::format("Vertex '{}' not found", vertex));
|
|
||||||
|
|
||||||
m_vertex_events[vertex] = event_id;
|
// Pre-calculate total size for better performance
|
||||||
}
|
std::size_t total_size = 0;
|
||||||
|
|
||||||
void NavigationMesh::clear_event(const Vector3<float>& vertex)
|
|
||||||
{
|
|
||||||
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)
|
for (const auto& [vertex, neighbors] : m_vertex_map)
|
||||||
{
|
{
|
||||||
const auto event_it = m_vertex_events.find(vertex);
|
total_size += sizeof(vertex) + sizeof(std::uint16_t) + sizeof(Vector3<float>) * neighbors.size();
|
||||||
const std::string& event = (event_it != m_vertex_events.end()) ? event_it->second : "-";
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
raw.reserve(total_size);
|
||||||
|
|
||||||
void NavigationMesh::deserialize(const std::string& raw)
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
void NavigationMesh::deserialize(const std::vector<uint8_t>& raw) noexcept
|
||||||
|
{
|
||||||
|
auto load_from_vector = [](const std::vector<uint8_t>& vec, std::size_t& offset, auto& value)
|
||||||
|
{
|
||||||
|
if (offset + sizeof(value) > vec.size())
|
||||||
|
throw std::runtime_error("Deserialize: Invalid input data size.");
|
||||||
|
|
||||||
|
std::copy_n(vec.data() + offset, sizeof(value), reinterpret_cast<uint8_t*>(&value));
|
||||||
|
offset += sizeof(value);
|
||||||
|
};
|
||||||
|
|
||||||
m_vertex_map.clear();
|
m_vertex_map.clear();
|
||||||
m_vertex_events.clear();
|
|
||||||
std::istringstream iss(raw);
|
|
||||||
|
|
||||||
Vector3<float> vertex;
|
std::size_t offset = 0;
|
||||||
std::size_t neighbors_count;
|
|
||||||
std::string event;
|
while (offset < raw.size())
|
||||||
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;
|
std::vector<Vector3<float>> neighbors;
|
||||||
neighbors.reserve(neighbors_count);
|
neighbors.reserve(neighbors_count);
|
||||||
|
|
||||||
for (std::size_t i = 0; i < neighbors_count; ++i)
|
for (std::size_t i = 0; i < neighbors_count; ++i)
|
||||||
{
|
{
|
||||||
Vector3<float> n;
|
Vector3<float> neighbor;
|
||||||
if (!(iss >> n.x >> n.y >> n.z))
|
load_from_vector(raw, offset, neighbor);
|
||||||
throw std::runtime_error("Deserialize: Unexpected end of data.");
|
neighbors.push_back(neighbor);
|
||||||
neighbors.push_back(n);
|
|
||||||
}
|
}
|
||||||
m_vertex_map.emplace(vertex, std::move(neighbors));
|
|
||||||
|
|
||||||
if (event != "-")
|
m_vertex_map.emplace(vertex, std::move(neighbors));
|
||||||
m_vertex_events.emplace(vertex, std::move(event));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // namespace omath::pathfinding
|
} // namespace omath::pathfinding
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ project(unit_tests)
|
|||||||
include(GoogleTest)
|
include(GoogleTest)
|
||||||
|
|
||||||
file(GLOB_RECURSE UNIT_TESTS_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/general/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/engines/*.cpp")
|
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)
|
add_executable(${PROJECT_NAME} ${UNIT_TESTS_SOURCES})
|
||||||
|
|
||||||
set_target_properties(
|
set_target_properties(
|
||||||
${PROJECT_NAME}
|
${PROJECT_NAME}
|
||||||
|
|||||||
@@ -8,29 +8,6 @@
|
|||||||
using namespace omath;
|
using namespace omath;
|
||||||
using namespace omath::pathfinding;
|
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)
|
TEST(AStarExtra, TrivialNeighbor)
|
||||||
{
|
{
|
||||||
NavigationMesh nav;
|
NavigationMesh nav;
|
||||||
@@ -101,7 +78,7 @@ TEST(AStarExtra, LongerPathAvoidsBlock)
|
|||||||
constexpr Vector3<float> goal = idx(2, 1);
|
constexpr Vector3<float> goal = idx(2, 1);
|
||||||
const auto path = Astar::find_path(start, goal, nav);
|
const auto path = Astar::find_path(start, goal, nav);
|
||||||
ASSERT_FALSE(path.empty());
|
ASSERT_FALSE(path.empty());
|
||||||
EXPECT_EQ(path.front(), goal);
|
EXPECT_EQ(path.front(), goal); // Astar convention: single-element or endpoint present
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(AstarTests, TrivialDirectNeighborPath)
|
TEST(AstarTests, TrivialDirectNeighborPath)
|
||||||
@@ -114,6 +91,9 @@ TEST(AstarTests, TrivialDirectNeighborPath)
|
|||||||
nav.m_vertex_map.emplace(v2, std::vector<Vector3<float>>{v1});
|
nav.m_vertex_map.emplace(v2, std::vector<Vector3<float>>{v1});
|
||||||
|
|
||||||
const auto path = Astar::find_path(v1, v2, nav);
|
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);
|
ASSERT_EQ(path.size(), 1u);
|
||||||
EXPECT_EQ(path.front(), v2);
|
EXPECT_EQ(path.front(), v2);
|
||||||
}
|
}
|
||||||
@@ -154,154 +134,3 @@ TEST(unit_test_a_star, finding_right_path)
|
|||||||
mesh.m_vertex_map[{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);
|
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);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -7,18 +7,19 @@ using namespace omath::pathfinding;
|
|||||||
TEST(NavigationMeshTests, SerializeDeserializeRoundTrip)
|
TEST(NavigationMeshTests, SerializeDeserializeRoundTrip)
|
||||||
{
|
{
|
||||||
NavigationMesh nav;
|
NavigationMesh nav;
|
||||||
Vector3<float> a{0.f, 0.f, 0.f};
|
Vector3<float> a{0.f,0.f,0.f};
|
||||||
Vector3<float> b{1.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> 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(b, std::vector<Vector3<float>>{a});
|
||||||
nav.m_vertex_map.emplace(c, std::vector<Vector3<float>>{a});
|
nav.m_vertex_map.emplace(c, std::vector<Vector3<float>>{a});
|
||||||
|
|
||||||
std::string data = nav.serialize();
|
auto data = nav.serialize();
|
||||||
NavigationMesh nav2;
|
NavigationMesh nav2;
|
||||||
EXPECT_NO_THROW(nav2.deserialize(data));
|
EXPECT_NO_THROW(nav2.deserialize(data));
|
||||||
|
|
||||||
|
// verify neighbors preserved
|
||||||
EXPECT_EQ(nav2.m_vertex_map.size(), nav.m_vertex_map.size());
|
EXPECT_EQ(nav2.m_vertex_map.size(), nav.m_vertex_map.size());
|
||||||
EXPECT_EQ(nav2.get_neighbors(a).size(), 2u);
|
EXPECT_EQ(nav2.get_neighbors(a).size(), 2u);
|
||||||
}
|
}
|
||||||
@@ -26,223 +27,7 @@ TEST(NavigationMeshTests, SerializeDeserializeRoundTrip)
|
|||||||
TEST(NavigationMeshTests, GetClosestVertexWhenEmpty)
|
TEST(NavigationMeshTests, GetClosestVertexWhenEmpty)
|
||||||
{
|
{
|
||||||
const NavigationMesh nav;
|
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);
|
const auto res = nav.get_closest_vertex(p);
|
||||||
EXPECT_FALSE(res.has_value());
|
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());
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,341 +0,0 @@
|
|||||||
//
|
|
||||||
// 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
|
|
||||||
@@ -1,66 +0,0 @@
|
|||||||
-- 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
|
|
||||||
@@ -1,82 +0,0 @@
|
|||||||
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
|
|
||||||
@@ -14,7 +14,7 @@ protected:
|
|||||||
{
|
{
|
||||||
L = luaL_newstate();
|
L = luaL_newstate();
|
||||||
luaL_openlibs(L);
|
luaL_openlibs(L);
|
||||||
omath::lua::LuaInterpreter::register_lib(L);
|
omath::lua::register_lib(L);
|
||||||
if (luaL_dofile(L, LUA_SCRIPTS_DIR "/color_tests.lua") != LUA_OK)
|
if (luaL_dofile(L, LUA_SCRIPTS_DIR "/color_tests.lua") != LUA_OK)
|
||||||
FAIL() << lua_tostring(L, -1);
|
FAIL() << lua_tostring(L, -1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,113 +0,0 @@
|
|||||||
//
|
|
||||||
// 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"); }
|
|
||||||
@@ -14,7 +14,7 @@ protected:
|
|||||||
{
|
{
|
||||||
L = luaL_newstate();
|
L = luaL_newstate();
|
||||||
luaL_openlibs(L);
|
luaL_openlibs(L);
|
||||||
omath::lua::LuaInterpreter::register_lib(L);
|
omath::lua::register_lib(L);
|
||||||
if (luaL_dofile(L, LUA_SCRIPTS_DIR "/source_engine_tests.lua") != LUA_OK)
|
if (luaL_dofile(L, LUA_SCRIPTS_DIR "/source_engine_tests.lua") != LUA_OK)
|
||||||
FAIL() << lua_tostring(L, -1);
|
FAIL() << lua_tostring(L, -1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,47 +0,0 @@
|
|||||||
//
|
|
||||||
// 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"); }
|
|
||||||
@@ -14,7 +14,7 @@ protected:
|
|||||||
{
|
{
|
||||||
L = luaL_newstate();
|
L = luaL_newstate();
|
||||||
luaL_openlibs(L);
|
luaL_openlibs(L);
|
||||||
omath::lua::LuaInterpreter::register_lib(L);
|
omath::lua::register_lib(L);
|
||||||
if (luaL_dofile(L, LUA_SCRIPTS_DIR "/vec2_tests.lua") != LUA_OK)
|
if (luaL_dofile(L, LUA_SCRIPTS_DIR "/vec2_tests.lua") != LUA_OK)
|
||||||
FAIL() << lua_tostring(L, -1);
|
FAIL() << lua_tostring(L, -1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ protected:
|
|||||||
{
|
{
|
||||||
L = luaL_newstate();
|
L = luaL_newstate();
|
||||||
luaL_openlibs(L);
|
luaL_openlibs(L);
|
||||||
omath::lua::LuaInterpreter::register_lib(L);
|
omath::lua::register_lib(L);
|
||||||
if (luaL_dofile(L, LUA_SCRIPTS_DIR "/vec3_tests.lua") != LUA_OK)
|
if (luaL_dofile(L, LUA_SCRIPTS_DIR "/vec3_tests.lua") != LUA_OK)
|
||||||
FAIL() << lua_tostring(L, -1);
|
FAIL() << lua_tostring(L, -1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ protected:
|
|||||||
{
|
{
|
||||||
L = luaL_newstate();
|
L = luaL_newstate();
|
||||||
luaL_openlibs(L);
|
luaL_openlibs(L);
|
||||||
omath::lua::LuaInterpreter::register_lib(L);
|
omath::lua::register_lib(L);
|
||||||
if (luaL_dofile(L, LUA_SCRIPTS_DIR "/vec4_tests.lua") != LUA_OK)
|
if (luaL_dofile(L, LUA_SCRIPTS_DIR "/vec4_tests.lua") != LUA_OK)
|
||||||
FAIL() << lua_tostring(L, -1);
|
FAIL() << lua_tostring(L, -1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"default-registry": {
|
"default-registry": {
|
||||||
"kind": "git",
|
"kind": "git",
|
||||||
"baseline": "efa4634bd526b87559684607d2cbbdeeec0f07d8",
|
"baseline": "b1b19307e2d2ec1eefbdb7ea069de7d4bcd31f01",
|
||||||
"repository": "https://github.com/microsoft/vcpkg"
|
"repository": "https://github.com/microsoft/vcpkg"
|
||||||
},
|
},
|
||||||
"registries": [
|
"registries": [
|
||||||
|
|||||||
@@ -52,13 +52,6 @@
|
|||||||
"lua",
|
"lua",
|
||||||
"sol2"
|
"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