Compare commits

...

36 Commits

Author SHA1 Message Date
b06fd00673 Merge pull request #41 from orange-cpp/u/improved-cmake
improved cmake, removed useless cmake files
2025-04-30 21:31:34 +03:00
998c8f3a43 improved cmake, removed useless cmake files 2025-04-30 21:26:25 +03:00
faeef594b9 moved installation stuff to INSTALL.md 2025-04-30 18:15:46 +03:00
40a301186e Merge pull request #40 from orange-cpp/u/orange/inverted-matrix
U/orange/inverted matrix
2025-04-29 20:53:02 +03:00
a41526c494 style fix 2025-04-29 20:52:41 +03:00
a0d1dc4313 added test case 2025-04-29 20:49:59 +03:00
1c5c9360c8 added inverse method 2025-04-29 20:33:39 +03:00
4615769682 added additional methods 2025-04-29 20:10:17 +03:00
4ef674f7b4 fixed infinite recursion in compile time 2025-04-29 20:08:27 +03:00
69b9049fb0 fixed gimba lock for unity 2025-04-26 00:52:46 +03:00
2734b58bdd fixed gimba lock for opengl camera 2025-04-26 00:32:53 +03:00
d7f1f49165 resetting state 2025-04-25 23:52:10 +03:00
94b1453cae removed .idea folder 2025-04-23 02:48:45 +03:00
3e67d8a99c added credits 2025-04-23 02:46:08 +03:00
8c00ab3d9d Merge pull request #38 from orange-cpp/u/orange-cpp/3d-primitives
U/orange cpp/3d primitives
2025-04-18 17:00:38 +03:00
d14cb1e93e reset to default 2025-04-18 16:55:07 +03:00
c692cf39e1 updated readme 2025-04-18 16:53:53 +03:00
9c934c5d9c improved tests 2025-04-18 16:36:22 +03:00
Vladislav Alpatov
f8202b116d improved line tracer 2025-04-18 16:00:23 +03:00
8bf0bb8e0d improved line trace and box primitive 2025-04-18 13:56:08 +03:00
a340766348 switched to polygons 2025-04-18 12:34:24 +03:00
254674a62e fixed code style 2025-04-18 12:11:43 +03:00
97c2da893b added ratio param 2025-04-18 01:33:47 +03:00
0ce30a7038 added new build option 2025-04-18 00:51:07 +03:00
492ddfd566 added box 2025-04-18 00:43:46 +03:00
baf7ee8f88 fixed include 2025-04-16 20:35:17 +03:00
9fde11733f Merge pull request #37 from orange-cpp/u/engine_rotation_mats
U/engine rotation mats
2025-04-16 23:21:58 +03:00
0069b8bd96 improved openg gl rotation matrix, added tests 2025-04-16 19:11:02 +03:00
127bae0b78 added tests for source 2025-04-16 18:53:31 +03:00
bed204a663 added unit tests 2025-04-16 18:35:50 +03:00
3f6ea010dc fixed formating 2025-04-16 17:52:57 +03:00
592a98f38c removed method from Mat added method for unity 2025-04-16 17:52:19 +03:00
7873047550 added func
added rotation matrix for opengl

updated unit tests
2025-04-16 17:40:00 +03:00
1601f3cbc8 added func
added rotation matrix for opengl
2025-04-16 17:40:00 +03:00
2180f8ab97 removed whitespaces 2025-04-16 12:21:10 +03:00
b613ff9ef1 added missing header 2025-04-13 23:15:27 +03:00
43 changed files with 554 additions and 232 deletions

8
.idea/modules.xml generated
View File

@@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/uml.iml" filepath="$PROJECT_DIR$/.idea/uml.iml" />
</modules>
</component>
</project>

2
.idea/uml.iml generated
View File

@@ -1,2 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

7
.idea/vcs.xml generated
View File

@@ -1,7 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
<mapping directory="$PROJECT_DIR$/extlibs/googletest" vcs="Git" />
</component>
</project>

View File

@@ -11,16 +11,19 @@ option(OMATH_BUILD_AS_SHARED_LIBRARY "Build Omath as .so or .dll" OFF)
option(OMATH_USE_AVX2 "Omath will use AVX2 to boost performance" ON) option(OMATH_USE_AVX2 "Omath will use AVX2 to boost performance" ON)
option(OMATH_IMGUI_INTEGRATION "Omath will define method to convert omath types to imgui types" OFF) option(OMATH_IMGUI_INTEGRATION "Omath will define method to convert omath types to imgui types" OFF)
option(OMATH_BUILD_EXAMPLES "Build example projects with you can learn & play" OFF) option(OMATH_BUILD_EXAMPLES "Build example projects with you can learn & play" OFF)
option(OMATH_STATIC_MSVC_RUNTIME_LIBRARY "Force Omath to link static runtime" OFF) option(OMATH_STATIC_MSVC_RUNTIME_LIBRARY "Force Omath to link static runtime" OFF)
option(OMATH_SUPRESS_SAFETY_CHECKS "Supress some safety checks in release build to improve general performance" ON) option(OMATH_SUPRESS_SAFETY_CHECKS "Supress some safety checks in release build to improve general performance" ON)
option(OMATH_USE_UNITY_BUILD "Will enable unity build to speed up compilation" ON)
file(GLOB_RECURSE OMATH_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp")
file(GLOB_RECURSE OMATH_HEADERS CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp")
if (OMATH_BUILD_AS_SHARED_LIBRARY) if (OMATH_BUILD_AS_SHARED_LIBRARY)
add_library(omath SHARED source/matrix.cpp) add_library(omath SHARED ${OMATH_SOURCES} ${OMATH_HEADERS})
else() else ()
add_library(omath STATIC source/matrix.cpp add_library(omath STATIC ${OMATH_SOURCES} ${OMATH_HEADERS})
source/matrix.cpp) endif ()
endif()
message(STATUS "Building on ${CMAKE_HOST_SYSTEM_NAME}") message(STATUS "Building on ${CMAKE_HOST_SYSTEM_NAME}")
add_library(omath::omath ALIAS omath) add_library(omath::omath ALIAS omath)
@@ -29,66 +32,68 @@ if (OMATH_IMGUI_INTEGRATION)
target_compile_definitions(omath PUBLIC OMATH_IMGUI_INTEGRATION) target_compile_definitions(omath PUBLIC OMATH_IMGUI_INTEGRATION)
# IMGUI is being linked as submodule # IMGUI is being linked as submodule
if(TARGET imgui) if (TARGET imgui)
target_link_libraries(omath PUBLIC imgui) target_link_libraries(omath PUBLIC imgui)
install(TARGETS imgui install(TARGETS imgui
EXPORT omathTargets EXPORT omathTargets
ARCHIVE DESTINATION lib ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib LIBRARY DESTINATION lib
RUNTIME DESTINATION bin) RUNTIME DESTINATION bin)
else() else ()
# Assume that IMGUI linked via VCPKG. # Assume that IMGUI linked via VCPKG.
find_package(imgui CONFIG REQUIRED) find_package(imgui CONFIG REQUIRED)
target_link_libraries(omath PUBLIC imgui::imgui) target_link_libraries(omath PUBLIC imgui::imgui)
endif() endif ()
endif() endif ()
if (OMATH_USE_AVX2) if (OMATH_USE_AVX2)
target_compile_definitions(omath PUBLIC OMATH_USE_AVX2) target_compile_definitions(omath PUBLIC OMATH_USE_AVX2)
endif() endif ()
if (OMATH_SUPRESS_SAFETY_CHECKS) if (OMATH_SUPRESS_SAFETY_CHECKS)
target_compile_definitions(omath PUBLIC OMATH_SUPRESS_SAFETY_CHECKS) target_compile_definitions(omath PUBLIC OMATH_SUPRESS_SAFETY_CHECKS)
endif() endif ()
set_target_properties(omath PROPERTIES set_target_properties(omath PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}" ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}" LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
UNITY_BUILD ON
UNITY_BUILD_BATCH_SIZE 20
CXX_STANDARD 23 CXX_STANDARD 23
CXX_STANDARD_REQUIRED ON) CXX_STANDARD_REQUIRED ON)
if (OMATH_USE_UNITY_BUILD)
set_target_properties(omath PROPERTIES
UNITY_BUILD ON
UNITY_BUILD_BATCH_SIZE 20)
endif ()
if (OMATH_STATIC_MSVC_RUNTIME_LIBRARY) if (OMATH_STATIC_MSVC_RUNTIME_LIBRARY)
set_target_properties(omath PROPERTIES set_target_properties(omath PROPERTIES
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>" MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>"
) )
endif() endif ()
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
target_compile_options(omath PRIVATE -mavx2 -mfma) target_compile_options(omath PRIVATE -mavx2 -mfma)
endif() endif ()
target_compile_features(omath PUBLIC cxx_std_23) target_compile_features(omath PUBLIC cxx_std_23)
add_subdirectory(source) if (OMATH_BUILD_TESTS)
if(OMATH_BUILD_TESTS)
add_subdirectory(extlibs) add_subdirectory(extlibs)
add_subdirectory(tests) add_subdirectory(tests)
endif() endif ()
if (OMATH_BUILD_EXAMPLES) if (OMATH_BUILD_EXAMPLES)
add_subdirectory(examples) add_subdirectory(examples)
endif() endif ()
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND OMATH_THREAT_WARNING_AS_ERROR) if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND OMATH_THREAT_WARNING_AS_ERROR)
target_compile_options(omath PRIVATE /W4 /WX) target_compile_options(omath PRIVATE /W4 /WX)
elseif(OMATH_THREAT_WARNING_AS_ERROR) elseif (OMATH_THREAT_WARNING_AS_ERROR)
target_compile_options(omath PRIVATE -Wall -Wextra -Wpedantic -Werror) target_compile_options(omath PRIVATE -Wall -Wextra -Wpedantic -Werror)
endif() endif ()
target_include_directories(omath target_include_directories(omath
PUBLIC PUBLIC

11
CREDITS.md Normal file
View File

@@ -0,0 +1,11 @@
# OMATH CREDITS
Thanks to everyone who made this possible, including:
- Saikari aka luadebug for VCPKG port.
And a big hand to everyone else who has contributed over the past!
THANKS! <3
-- Orange++ <orange-cpp@yandex.ru>

54
INSTALL.md Normal file
View File

@@ -0,0 +1,54 @@
# 📥Installation Guide
## <img width="28px" src="https://vcpkg.io/assets/mark/mark.svg" /> Using vcpkg
**Note**: Support vcpkg for package management
1. Install [vcpkg](https://github.com/microsoft/vcpkg)
2. Run the following command to install the orange-math package:
```
vcpkg install orange-math
```
CMakeLists.txt
```cmake
find_package(omath CONFIG REQUIRED)
target_link_libraries(main PRIVATE omath::omath)
```
For detailed commands on installing different versions and more information, please refer to Microsoft's [official instructions](https://learn.microsoft.com/en-us/vcpkg/get_started/overview).
## <img width="28px" src="https://upload.wikimedia.org/wikipedia/commons/e/ef/CMake_logo.svg?" /> Build from source using CMake
1. **Preparation**
Install needed tools: cmake, clang, git, msvc (windows only).
1. **Linux:**
```bash
sudo pacman -Sy cmake ninja clang git
```
2. **MacOS:**
```bash
brew install llvm git cmake ninja
```
3. **Windows:**
Install Visual Studio from [here](https://visualstudio.microsoft.com/downloads/) and Git from [here](https://git-scm.com/downloads).
Use x64 Native Tools shell to execute needed commands down below.
2. **Clone the repository:**
```bash
git clone https://github.com/orange-cpp/omath.git
```
3. **Navigate to the project directory:**
```bash
cd omath
```
4. **Build the project using CMake:**
```bash
cmake --preset windows-release -S .
cmake --build cmake-build/build/windows-release --target omath -j 6
```
Use **\<platform\>-\<build configuration\>** preset to build siutable version for yourself. Like **windows-release** or **linux-release**.
| Platform Name | Build Config |
|---------------|---------------|
| windows | release/debug |
| linux | release/debug |
| darwin | release/debug |

View File

@@ -37,40 +37,9 @@ Oranges's Math Library (omath) is a comprehensive, open-source library aimed at
| Linux | ✅YES | | Linux | ✅YES |
| Darwin (MacOS) | ✅YES | | Darwin (MacOS) | ✅YES |
## ⏬ Getting Started ## ⏬ Installation
### Prerequisites Please read our [installation guide](https://github.com/orange-cpp/omath/blob/main/INSTALL.md). If this link doesn't work check out INSTALL.md file.
- C++ Compiler
- CMake (for building the project)
### Installation
### vcpkg
**Note**: Support vcpkg for package management
1. Install vcpkg (https://github.com/microsoft/vcpkg)
2. Run the following command to install the orange-math package:
```
vcpkg install orange-math
```
CMakeLists.txt
```cmake
find_package(omath CONFIG REQUIRED)
target_link_libraries(main PRIVATE omath::omath)
```
For detailed commands on installing different versions and more information, please refer to Microsoft's official instructions (https://learn.microsoft.com/en-us/vcpkg/get_started/overview)
### Build from source
1. Clone the repository:
```
git clone https://github.com/orange-cpp/omath.git
```
2. Navigate to the project directory:
```
cd omath
```
3. Build the project using CMake:
```
cmake --preset windows-release -S .
cmake --build cmake-build/build/windows-release --target omath -j 6
```
Use **\<platform\>-\<build configuration\>** preset to build siutable version for yourself. Like **windows-release** or **linux-release**.
## ❔ Usage ## ❔ Usage
Simple world to screen function Simple world to screen function
```c++ ```c++
@@ -91,7 +60,8 @@ With `omath/projection` module you can achieve simple ESP hack for powered by So
![banner](https://i.imgur.com/lcJrfcZ.png) ![banner](https://i.imgur.com/lcJrfcZ.png)
Or for InfinityWard Engine based games. Like Call of Duty Black Ops 2! Or for InfinityWard Engine based games. Like Call of Duty Black Ops 2!
![banner](https://i.imgur.com/F8dmdoo.png) ![banner](https://i.imgur.com/F8dmdoo.png)
Or create simple trigger bot with embeded traceline from omath::collision::LineTrace
![banner](https://i.imgur.com/fxMjRKo.jpeg)
Or even advanced projectile aimbot Or even advanced projectile aimbot
[Watch Video](https://youtu.be/lM_NJ1yCunw?si=5E87OrQMeypxSJ3E) [Watch Video](https://youtu.be/lM_NJ1yCunw?si=5E87OrQMeypxSJ3E)
</details> </details>

View File

@@ -0,0 +1,17 @@
//
// Created by Vlad on 4/18/2025.
//
#pragma once
#include <array>
#include "omath/triangle.hpp"
#include "omath/vector3.hpp"
namespace omath::primitives
{
[[nodiscard]]
std::array<Triangle<Vector3<float>>, 12> CreateBox(const Vector3<float>& top, const Vector3<float>& bottom,
const Vector3<float>& dirForward, const Vector3<float>& dirRight,
float ratio = 4.f);
}

View File

@@ -3,9 +3,9 @@
// //
#pragma once #pragma once
#include "omath/angles.hpp"
#include <algorithm> #include <algorithm>
#include <utility> #include <utility>
#include "omath/angles.hpp"
namespace omath namespace omath
@@ -17,7 +17,7 @@ namespace omath
}; };
template<class Type = float, Type min = Type(0), Type max = Type(360), AngleFlags flags = AngleFlags::Normalized> template<class Type = float, Type min = Type(0), Type max = Type(360), AngleFlags flags = AngleFlags::Normalized>
requires std::is_arithmetic_v<Type> requires std::is_arithmetic_v<Type>
class Angle class Angle
{ {
Type m_angle; Type m_angle;
@@ -34,6 +34,7 @@ namespace omath
std::unreachable(); std::unreachable();
} }
} }
public: public:
[[nodiscard]] [[nodiscard]]
constexpr static Angle FromDegrees(const Type& degrees) constexpr static Angle FromDegrees(const Type& degrees)
@@ -42,7 +43,6 @@ namespace omath
} }
constexpr Angle() : m_angle(0) constexpr Angle() : m_angle(0)
{ {
} }
[[nodiscard]] [[nodiscard]]
constexpr static Angle FromRadians(const Type& degrees) constexpr static Angle FromRadians(const Type& degrees)
@@ -149,4 +149,4 @@ namespace omath
return Angle{-m_angle}; return Angle{-m_angle};
} }
}; };
} } // namespace omath

View File

@@ -13,7 +13,7 @@ namespace omath::collision
public: public:
Vector3<float> start; Vector3<float> start;
Vector3<float> end; Vector3<float> end;
bool infinite_length = false;
[[nodiscard]] [[nodiscard]]
Vector3<float> DirectionVector() const; Vector3<float> DirectionVector() const;

View File

@@ -16,8 +16,10 @@ namespace omath::iw_engine
[[nodiscard]] [[nodiscard]]
Vector3<float> UpVector(const ViewAngles& angles); Vector3<float> UpVector(const ViewAngles& angles);
[[nodiscard]] Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin); [[nodiscard]]
Mat4x4 RotationMatrix(const ViewAngles& angles);
[[nodiscard]] Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin);
[[nodiscard]] [[nodiscard]]
Mat4x4 CalcPerspectiveProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far); Mat4x4 CalcPerspectiveProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far);

View File

@@ -17,9 +17,10 @@ namespace omath::opengl_engine
using Mat4x4 = Mat<4, 4, float, MatStoreType::COLUMN_MAJOR>; using Mat4x4 = Mat<4, 4, float, MatStoreType::COLUMN_MAJOR>;
using Mat3x3 = Mat<4, 4, float, MatStoreType::COLUMN_MAJOR>; using Mat3x3 = Mat<4, 4, float, MatStoreType::COLUMN_MAJOR>;
using Mat1x3 = Mat<1, 3, float, MatStoreType::COLUMN_MAJOR>; using Mat1x3 = Mat<1, 3, float, MatStoreType::COLUMN_MAJOR>;
using PitchAngle = Angle<float, 0.f, 180.f, AngleFlags::Clamped>; using PitchAngle = Angle<float, -90.f, 90.f, AngleFlags::Clamped>;
using YawAngle = Angle<float, 0.f, 360.f, AngleFlags::Normalized>; using YawAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
using RollAngle = Angle<float, 0.f, 360.f, AngleFlags::Normalized>; using RollAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
using ViewAngles = omath::ViewAngles<PitchAngle, YawAngle, RollAngle>; using ViewAngles = omath::ViewAngles<PitchAngle, YawAngle, RollAngle>;
} }

View File

@@ -18,6 +18,8 @@ namespace omath::opengl_engine
[[nodiscard]] Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin); [[nodiscard]] Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin);
[[nodiscard]]
Mat4x4 RotationMatrix(const ViewAngles& angles);
[[nodiscard]] [[nodiscard]]
Mat4x4 CalcPerspectiveProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far); Mat4x4 CalcPerspectiveProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far);

View File

@@ -9,6 +9,9 @@ namespace omath::source_engine
[[nodiscard]] [[nodiscard]]
Vector3<float> ForwardVector(const ViewAngles& angles); Vector3<float> ForwardVector(const ViewAngles& angles);
[[nodiscard]]
Mat4x4 RotationMatrix(const ViewAngles& angles);
[[nodiscard]] [[nodiscard]]
Vector3<float> RightVector(const ViewAngles& angles); Vector3<float> RightVector(const ViewAngles& angles);
@@ -17,7 +20,6 @@ namespace omath::source_engine
[[nodiscard]] Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin); [[nodiscard]] Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin);
[[nodiscard]] [[nodiscard]]
Mat4x4 CalcPerspectiveProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far); Mat4x4 CalcPerspectiveProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far);
} // namespace omath::source } // namespace omath::source

View File

@@ -18,7 +18,7 @@ namespace omath::unity_engine
using Mat4x4 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>; using Mat4x4 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
using Mat3x3 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>; using Mat3x3 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
using Mat1x3 = Mat<1, 3, float, MatStoreType::ROW_MAJOR>; using Mat1x3 = Mat<1, 3, float, MatStoreType::ROW_MAJOR>;
using PitchAngle = Angle<float, -89.f, 89.f, AngleFlags::Clamped>; using PitchAngle = Angle<float, -90.f, 90.f, AngleFlags::Clamped>;
using YawAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>; using YawAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
using RollAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>; using RollAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;

View File

@@ -18,6 +18,8 @@ namespace omath::unity_engine
[[nodiscard]] Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin); [[nodiscard]] Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin);
[[nodiscard]]
Mat4x4 RotationMatrix(const ViewAngles& angles);
[[nodiscard]] [[nodiscard]]
Mat4x4 CalcPerspectiveProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far); Mat4x4 CalcPerspectiveProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far);

View File

@@ -97,7 +97,7 @@ namespace omath
{ {
return At(row, col); return At(row, col);
} }
constexpr Mat(Mat&& other) noexcept constexpr Mat(Mat&& other) noexcept
{ {
m_data = std::move(other.m_data); m_data = std::move(other.m_data);
@@ -253,21 +253,24 @@ namespace omath
if constexpr (Rows == 2) if constexpr (Rows == 2)
return At(0, 0) * At(1, 1) - At(0, 1) * At(1, 0); return At(0, 0) * At(1, 1) - At(0, 1) * At(1, 0);
else
if constexpr (Rows > 2)
{ {
Type det = 0; Type det = 0;
for (size_t i = 0; i < Columns; ++i) for (size_t column = 0; column < Columns; ++column)
{ {
const Type cofactor = (i % 2 == 0 ? 1 : -1) * At(0, i) * Minor(0, i).Determinant(); const Type cofactor = At(0, column) * AlgComplement(0, column);
det += cofactor; det += cofactor;
} }
return det; return det;
} }
std::unreachable();
} }
[[nodiscard]] [[nodiscard]]
constexpr Mat<Rows - 1, Columns - 1, Type, StoreType> Minor(const size_t row, const size_t column) const constexpr Mat<Rows - 1, Columns - 1, Type, StoreType> Strip(const size_t row, const size_t column) const
{ {
static_assert(Rows-1 > 0 && Columns-1 > 0);
Mat<Rows - 1, Columns - 1, Type, StoreType> result; Mat<Rows - 1, Columns - 1, Type, StoreType> result;
for (size_t i = 0, m = 0; i < Rows; ++i) for (size_t i = 0, m = 0; i < Rows; ++i)
{ {
@@ -285,6 +288,19 @@ namespace omath
return result; return result;
} }
[[nodiscard]]
constexpr Type Minor(const size_t row, const size_t column) const
{
return Strip(row, column).Determinant();
}
[[nodiscard]]
constexpr Type AlgComplement(const size_t row, const size_t column) const
{
const auto minor = Minor(row, column);
return (row + column + 2) % 2 == 0 ? minor: -minor;
}
[[nodiscard]] [[nodiscard]]
constexpr const std::array<Type, Rows * Columns>& RawArray() const constexpr const std::array<Type, Rows * Columns>& RawArray() const
{ {
@@ -294,7 +310,7 @@ namespace omath
[[nodiscard]] [[nodiscard]]
constexpr std::array<Type, Rows * Columns>& RawArray() constexpr std::array<Type, Rows * Columns>& RawArray()
{ {
return const_cast<std::array<Type, Rows * Columns>>(std::as_const(*this).RawArray()); return m_data;
} }
[[nodiscard]] [[nodiscard]]
@@ -343,6 +359,25 @@ namespace omath
}; };
} }
[[nodiscard]]
constexpr std::optional<Mat> Inverted() const
{
const auto det = Determinant();
if (det == 0)
return std::nullopt;
const auto transposed = Transposed();
Mat result;
for (std::size_t row = 0; row < Rows; row++)
for (std::size_t column = 0; column < Rows; column++)
result.At(row, column) = transposed.AlgComplement(row, column);
result /= det;
return {result};
}
private: private:
std::array<Type, Rows * Columns> m_data; std::array<Type, Rows * Columns> m_data;
}; };
@@ -428,13 +463,6 @@ namespace omath
} * MatTranslation<Type, St>(-cameraOrigin); } * MatTranslation<Type, St>(-cameraOrigin);
} }
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR, class ViewAngles>
[[nodiscard]]
Mat<4, 4, Type, St> MatRotation(const ViewAngles& angles) noexcept
{
return MatRotationAxisZ(angles.yaw) * MatRotationAxisY(angles.pitch) * MatRotationAxisX(angles.roll);
}
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR> template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
[[nodiscard]] [[nodiscard]]
Mat<4, 4, Type, St> MatPerspectiveLeftHanded(const float fieldOfView, const float aspectRatio, const float near, Mat<4, 4, Type, St> MatPerspectiveLeftHanded(const float fieldOfView, const float aspectRatio, const float near,

View File

@@ -62,31 +62,37 @@ namespace omath::projection
void SetFieldOfView(const FieldOfView& fov) void SetFieldOfView(const FieldOfView& fov)
{ {
m_fieldOfView = fov; m_fieldOfView = fov;
m_viewProjectionMatrix = std::nullopt;
} }
void SetNearPlane(const float near) void SetNearPlane(const float near)
{ {
m_nearPlaneDistance = near; m_nearPlaneDistance = near;
m_viewProjectionMatrix = std::nullopt;
} }
void SetFarPlane(const float far) void SetFarPlane(const float far)
{ {
m_farPlaneDistance = far; m_farPlaneDistance = far;
m_viewProjectionMatrix = std::nullopt;
} }
void SetViewAngles(const ViewAnglesType& viewAngles) void SetViewAngles(const ViewAnglesType& viewAngles)
{ {
m_viewAngles = viewAngles; m_viewAngles = viewAngles;
m_viewProjectionMatrix = std::nullopt;
} }
void SetOrigin(const Vector3<float>& origin) void SetOrigin(const Vector3<float>& origin)
{ {
m_origin = origin; m_origin = origin;
m_viewProjectionMatrix = std::nullopt;
} }
void SetViewPort(const ViewPort& viewPort) void SetViewPort(const ViewPort& viewPort)
{ {
m_viewPort = viewPort; m_viewPort = viewPort;
m_viewProjectionMatrix = std::nullopt;
} }
[[nodiscard]] const FieldOfView& GetFieldOfView() const [[nodiscard]] const FieldOfView& GetFieldOfView() const

View File

@@ -0,0 +1,56 @@
//
// Created by Vlad on 4/18/2025.
//
#include "omath/3d_primitives/box.hpp"
namespace omath::primitives
{
std::array<Triangle<Vector3<float>>, 12> CreateBox(const Vector3<float>& top, const Vector3<float>& bottom,
const Vector3<float>& dirForward,
const Vector3<float>& dirRight,
const float ratio)
{
const auto height = top.DistTo(bottom);
const auto sideSize = height / ratio;
// corner layout (03 bottom, 47 top)
std::array<Vector3<float>, 8> p;
p[0] = bottom + (dirForward + dirRight) * sideSize; // frontrightbottom
p[1] = bottom + (dirForward - dirRight) * sideSize; // frontleftbottom
p[2] = bottom + (-dirForward + dirRight) * sideSize; // backrightbottom
p[3] = bottom + (-dirForward - dirRight) * sideSize; // backleftbottom
p[4] = top + (dirForward + dirRight) * sideSize; // frontrighttop
p[5] = top + (dirForward - dirRight) * sideSize; // frontlefttop
p[6] = top + (-dirForward + dirRight) * sideSize; // backrighttop
p[7] = top + (-dirForward - dirRight) * sideSize; // backlefttop
std::array<Triangle<Vector3<float>>, 12> poly;
// bottom face (+Y up ⇒ wind CW when viewed from above)
poly[0] = {p[0], p[2], p[3]};
poly[1] = {p[0], p[3], p[1]};
// top face
poly[2] = {p[4], p[7], p[6]};
poly[3] = {p[4], p[5], p[7]};
// front face
poly[4] = {p[0], p[5], p[1]};
poly[5] = {p[0], p[4], p[5]};
// right face
poly[6] = {p[0], p[6], p[2]};
poly[7] = {p[0], p[4], p[6]};
// back face
poly[8] = {p[2], p[7], p[3]};
poly[9] = {p[2], p[6], p[7]};
// left face
poly[10] = {p[1], p[7], p[5]};
poly[11] = {p[1], p[3], p[7]};
return poly;
}
}

View File

@@ -1,10 +0,0 @@
target_sources(omath PRIVATE
matrix.cpp
color.cpp
)
add_subdirectory(projectile_prediction)
add_subdirectory(pathfinding)
add_subdirectory(projection)
add_subdirectory(collision)
add_subdirectory(engines)

View File

@@ -1,3 +0,0 @@
target_sources(omath PRIVATE
line_tracer.cpp
)

View File

@@ -54,7 +54,12 @@ namespace omath::collision
const auto tHit = sideB.Dot(q) * invDet; const auto tHit = sideB.Dot(q) * invDet;
if (tHit <= kEpsilon) if (ray.infinite_length)
{
if (tHit <= kEpsilon)
return ray.end;
}
else if (tHit <= kEpsilon || tHit > 1.0f - kEpsilon)
return ray.end; return ray.end;
return ray.start + rayDir * tHit; return ray.start + rayDir * tHit;

View File

@@ -1,4 +0,0 @@
add_subdirectory(source_engine)
add_subdirectory(opengl_engine)
add_subdirectory(iw_engine)
add_subdirectory(unity_engine)

View File

@@ -1 +0,0 @@
target_sources(omath PRIVATE camera.cpp formulas.cpp)

View File

@@ -9,23 +9,27 @@ namespace omath::iw_engine
Vector3<float> ForwardVector(const ViewAngles& angles) Vector3<float> ForwardVector(const ViewAngles& angles)
{ {
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsForward); const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsForward);
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)}; return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
} }
Vector3<float> RightVector(const ViewAngles& angles) Vector3<float> RightVector(const ViewAngles& angles)
{ {
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsRight); const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsRight);
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)}; return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
} }
Vector3<float> UpVector(const ViewAngles& angles) Vector3<float> UpVector(const ViewAngles& angles)
{ {
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsUp); const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsUp);
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)}; return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
} }
Mat4x4 RotationMatrix(const ViewAngles& angles)
{
return MatRotationAxisZ(angles.yaw) * MatRotationAxisY(angles.pitch) * MatRotationAxisX(angles.roll);
}
Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin) Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin)
{ {

View File

@@ -1 +0,0 @@
target_sources(omath PRIVATE camera.cpp formulas.cpp)

View File

@@ -9,19 +9,19 @@ namespace omath::opengl_engine
Vector3<float> ForwardVector(const ViewAngles& angles) Vector3<float> ForwardVector(const ViewAngles& angles)
{ {
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsForward); const auto vec = RotationMatrix(angles) * MatColumnFromVector<float, MatStoreType::COLUMN_MAJOR>(kAbsForward);
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)}; return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
} }
Vector3<float> RightVector(const ViewAngles& angles) Vector3<float> RightVector(const ViewAngles& angles)
{ {
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsRight); const auto vec = RotationMatrix(angles) * MatColumnFromVector<float, MatStoreType::COLUMN_MAJOR>(kAbsRight);
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)}; return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
} }
Vector3<float> UpVector(const ViewAngles& angles) Vector3<float> UpVector(const ViewAngles& angles)
{ {
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsUp); const auto vec = RotationMatrix(angles) * MatColumnFromVector<float, MatStoreType::COLUMN_MAJOR>(kAbsUp);
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)}; return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
} }
@@ -30,6 +30,12 @@ namespace omath::opengl_engine
return MatCameraView<float, MatStoreType::COLUMN_MAJOR>(-ForwardVector(angles), RightVector(angles), return MatCameraView<float, MatStoreType::COLUMN_MAJOR>(-ForwardVector(angles), RightVector(angles),
UpVector(angles), cam_origin); UpVector(angles), cam_origin);
} }
Mat4x4 RotationMatrix(const ViewAngles& angles)
{
return MatRotationAxisX<float, MatStoreType::COLUMN_MAJOR>(-angles.pitch) *
MatRotationAxisY<float, MatStoreType::COLUMN_MAJOR>(-angles.yaw) *
MatRotationAxisZ<float, MatStoreType::COLUMN_MAJOR>(angles.roll);
}
Mat4x4 CalcPerspectiveProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near, Mat4x4 CalcPerspectiveProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near,
const float far) const float far)
{ {

View File

@@ -1 +0,0 @@
target_sources(omath PRIVATE camera.cpp formulas.cpp)

View File

@@ -8,20 +8,25 @@ namespace omath::source_engine
{ {
Vector3<float> ForwardVector(const ViewAngles& angles) Vector3<float> ForwardVector(const ViewAngles& angles)
{ {
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsForward); const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsForward);
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)}; return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
} }
Mat4x4 RotationMatrix(const ViewAngles& angles)
{
return MatRotationAxisZ(angles.yaw) * MatRotationAxisY(angles.pitch) * MatRotationAxisX(angles.roll);
}
Vector3<float> RightVector(const ViewAngles& angles) Vector3<float> RightVector(const ViewAngles& angles)
{ {
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsRight); const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsRight);
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)}; return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
} }
Vector3<float> UpVector(const ViewAngles& angles) Vector3<float> UpVector(const ViewAngles& angles)
{ {
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsUp); const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsUp);
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)}; return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
} }

View File

@@ -1 +0,0 @@
target_sources(omath PRIVATE formulas.cpp camera.cpp)

View File

@@ -9,19 +9,19 @@ namespace omath::unity_engine
{ {
Vector3<float> ForwardVector(const ViewAngles& angles) Vector3<float> ForwardVector(const ViewAngles& angles)
{ {
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsForward); const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsForward);
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)}; return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
} }
Vector3<float> RightVector(const ViewAngles& angles) Vector3<float> RightVector(const ViewAngles& angles)
{ {
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsRight); const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsRight);
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)}; return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
} }
Vector3<float> UpVector(const ViewAngles& angles) Vector3<float> UpVector(const ViewAngles& angles)
{ {
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsUp); const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsUp);
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)}; return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
} }
@@ -30,6 +30,12 @@ namespace omath::unity_engine
return MatCameraView<float, MatStoreType::ROW_MAJOR>(ForwardVector(angles), -RightVector(angles), return MatCameraView<float, MatStoreType::ROW_MAJOR>(ForwardVector(angles), -RightVector(angles),
UpVector(angles), cam_origin); UpVector(angles), cam_origin);
} }
Mat4x4 RotationMatrix(const ViewAngles& angles)
{
return MatRotationAxisX<float, MatStoreType::ROW_MAJOR>(angles.pitch) *
MatRotationAxisY<float, MatStoreType::ROW_MAJOR>(angles.yaw) *
MatRotationAxisZ<float, MatStoreType::ROW_MAJOR>(angles.roll);
}
Mat4x4 CalcPerspectiveProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near, Mat4x4 CalcPerspectiveProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near,
const float far) const float far)
{ {

View File

@@ -1 +0,0 @@
target_sources(omath PRIVATE navigation_mesh.cpp a_star.cpp)

View File

@@ -1 +0,0 @@
target_sources(omath PRIVATE proj_pred_engine_legacy.cpp projectile.cpp target.cpp proj_pred_engine_avx2.cpp proj_pred_engine.cpp)

View File

@@ -2,11 +2,12 @@
// Created by Vlad on 2/23/2025. // Created by Vlad on 2/23/2025.
// //
#include "omath/projectile_prediction/proj_pred_engine_avx2.hpp" #include "omath/projectile_prediction/proj_pred_engine_avx2.hpp"
#include "source_location" #include <source_location>
#include <stdexcept>
#if defined(OMATH_USE_AVX2) && defined(__i386__) && defined(__x86_64__) #if defined(OMATH_USE_AVX2) && defined(__i386__) && defined(__x86_64__)
#include <immintrin.h> #include <immintrin.h>
#else
#include <format> #include <format>
#endif #endif

View File

@@ -1 +0,0 @@
target_sources(omath PRIVATE camera.cpp)

View File

@@ -1,32 +1,13 @@
enable_testing() enable_testing()
project(unit-tests) project(unit_tests)
include(GoogleTest) include(GoogleTest)
add_executable(unit-tests
general/unit_test_prediction.cpp
general/unit_test_matrix.cpp
general/unit_test_mat.cpp
general/unit_test_a_star.cpp
general/unit_test_projection.cpp
general/unit_test_vector3.cpp
general/unit_test_vector2.cpp
general/unit_test_color.cpp
general/unit_test_vector4.cpp
general/unit_test_line_trace.cpp
general/unit_test_angles.cpp
general/unit_test_view_angles.cpp
general/unit_test_angle.cpp
general/unit_test_triangle.cpp
engines/unit_test_open_gl.cpp file(GLOB_RECURSE UNIT_TESTS_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
engines/unit_test_unity_engine.cpp add_executable(unit_tests ${UNIT_TESTS_SOURCES})
engines/unit_test_source_engine.cpp
engines/unit_test_iw_engine.cpp
) set_target_properties(unit_tests PROPERTIES
set_target_properties(unit-tests PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}" ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}" LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
UNITY_BUILD ON UNITY_BUILD ON
@@ -35,6 +16,6 @@ set_target_properties(unit-tests PROPERTIES
CXX_STANDARD_REQUIRED ON) CXX_STANDARD_REQUIRED ON)
target_link_libraries(unit-tests PRIVATE gtest gtest_main omath::omath) target_link_libraries(unit_tests PRIVATE gtest gtest_main omath::omath)
gtest_discover_tests(unit-tests) gtest_discover_tests(unit_tests)

View File

@@ -27,6 +27,42 @@ TEST(UnitTestIwEngine, UpVector)
EXPECT_EQ(up, omath::iw_engine::kAbsUp); EXPECT_EQ(up, omath::iw_engine::kAbsUp);
} }
TEST(UnitTestIwEngine, ForwardVectorRotationYaw)
{
omath::iw_engine::ViewAngles angles;
angles.yaw = omath::iw_engine::YawAngle::FromDegrees(-90.f);
const auto forward = omath::iw_engine::ForwardVector(angles);
EXPECT_NEAR(forward.x, omath::iw_engine::kAbsRight.x, 0.00001f);
EXPECT_NEAR(forward.y, omath::iw_engine::kAbsRight.y, 0.00001f);
EXPECT_NEAR(forward.z, omath::iw_engine::kAbsRight.z, 0.00001f);
}
TEST(UnitTestIwEngine, ForwardVectorRotationPitch)
{
omath::iw_engine::ViewAngles angles;
angles.pitch = omath::iw_engine::PitchAngle::FromDegrees(-89.f);
const auto forward = omath::iw_engine::ForwardVector(angles);
EXPECT_NEAR(forward.x, omath::iw_engine::kAbsUp.x, 0.02f);
EXPECT_NEAR(forward.y, omath::iw_engine::kAbsUp.y, 0.01f);
EXPECT_NEAR(forward.z, omath::iw_engine::kAbsUp.z, 0.01f);
}
TEST(UnitTestIwEngine, ForwardVectorRotationRoll)
{
omath::iw_engine::ViewAngles angles;
angles.roll = omath::iw_engine::RollAngle::FromDegrees(90.f);
const auto forward = omath::iw_engine::UpVector(angles);
EXPECT_NEAR(forward.x, omath::iw_engine::kAbsRight.x, 0.00001f);
EXPECT_NEAR(forward.y, omath::iw_engine::kAbsRight.y, 0.00001f);
EXPECT_NEAR(forward.z, omath::iw_engine::kAbsRight.z, 0.00001f);
}
TEST(UnitTestIwEngine, ProjectTargetMovedFromCamera) TEST(UnitTestIwEngine, ProjectTargetMovedFromCamera)
{ {
constexpr auto fov = omath::projection::FieldOfView::FromDegrees(90.f); constexpr auto fov = omath::projection::FieldOfView::FromDegrees(90.f);

View File

@@ -10,14 +10,12 @@
TEST(UnitTestOpenGL, ForwardVector) TEST(UnitTestOpenGL, ForwardVector)
{ {
const auto forward = omath::opengl_engine::ForwardVector({}); const auto forward = omath::opengl_engine::ForwardVector({});
EXPECT_EQ(forward, omath::opengl_engine::kAbsForward); EXPECT_EQ(forward, omath::opengl_engine::kAbsForward);
} }
TEST(UnitTestOpenGL, RightVector) TEST(UnitTestOpenGL, RightVector)
{ {
const auto right = omath::opengl_engine::RightVector({}); const auto right = omath::opengl_engine::RightVector({});
EXPECT_EQ(right, omath::opengl_engine::kAbsRight); EXPECT_EQ(right, omath::opengl_engine::kAbsRight);
} }
@@ -27,6 +25,44 @@ TEST(UnitTestOpenGL, UpVector)
EXPECT_EQ(up, omath::opengl_engine::kAbsUp); EXPECT_EQ(up, omath::opengl_engine::kAbsUp);
} }
TEST(UnitTestOpenGL, ForwardVectorRotationYaw)
{
omath::opengl_engine::ViewAngles angles;
angles.yaw = omath::opengl_engine::YawAngle::FromDegrees(90.f);
const auto forward = omath::opengl_engine::ForwardVector(angles);
EXPECT_NEAR(forward.x, omath::opengl_engine::kAbsRight.x, 0.00001f);
EXPECT_NEAR(forward.y, omath::opengl_engine::kAbsRight.y, 0.00001f);
EXPECT_NEAR(forward.z, omath::opengl_engine::kAbsRight.z, 0.00001f);
}
TEST(UnitTestOpenGL, ForwardVectorRotationPitch)
{
omath::opengl_engine::ViewAngles angles;
angles.pitch = omath::opengl_engine::PitchAngle::FromDegrees(-90.f);
const auto forward = omath::opengl_engine::ForwardVector(angles);
EXPECT_NEAR(forward.x, omath::opengl_engine::kAbsUp.x, 0.00001f);
EXPECT_NEAR(forward.y, omath::opengl_engine::kAbsUp.y, 0.00001f);
EXPECT_NEAR(forward.z, omath::opengl_engine::kAbsUp.z, 0.00001f);
}
TEST(UnitTestOpenGL, ForwardVectorRotationRoll)
{
omath::opengl_engine::ViewAngles angles;
angles.roll = omath::opengl_engine::RollAngle::FromDegrees(-90.f);
const auto forward = omath::opengl_engine::UpVector(angles);
EXPECT_NEAR(forward.x, omath::opengl_engine::kAbsRight.x, 0.00001f);
EXPECT_NEAR(forward.y, omath::opengl_engine::kAbsRight.y, 0.00001f);
EXPECT_NEAR(forward.z, omath::opengl_engine::kAbsRight.z, 0.00001f);
}
TEST(UnitTestOpenGL, ProjectTargetMovedFromCamera) TEST(UnitTestOpenGL, ProjectTargetMovedFromCamera)
{ {
constexpr auto fov = omath::projection::FieldOfView::FromDegrees(90.f); constexpr auto fov = omath::projection::FieldOfView::FromDegrees(90.f);

View File

@@ -27,6 +27,42 @@ TEST(UnitTestSourceEngine, UpVector)
EXPECT_EQ(up, omath::source_engine::kAbsUp); EXPECT_EQ(up, omath::source_engine::kAbsUp);
} }
TEST(UnitTestSourceEngine, ForwardVectorRotationYaw)
{
omath::source_engine::ViewAngles angles;
angles.yaw = omath::source_engine::YawAngle::FromDegrees(-90.f);
const auto forward = omath::source_engine::ForwardVector(angles);
EXPECT_NEAR(forward.x, omath::source_engine::kAbsRight.x, 0.00001f);
EXPECT_NEAR(forward.y, omath::source_engine::kAbsRight.y, 0.00001f);
EXPECT_NEAR(forward.z, omath::source_engine::kAbsRight.z, 0.00001f);
}
TEST(UnitTestSourceEngine, ForwardVectorRotationPitch)
{
omath::source_engine::ViewAngles angles;
angles.pitch = omath::source_engine::PitchAngle::FromDegrees(-89.f);
const auto forward = omath::source_engine::ForwardVector(angles);
EXPECT_NEAR(forward.x, omath::source_engine::kAbsUp.x, 0.02f);
EXPECT_NEAR(forward.y, omath::source_engine::kAbsUp.y, 0.01f);
EXPECT_NEAR(forward.z, omath::source_engine::kAbsUp.z, 0.01f);
}
TEST(UnitTestSourceEngine, ForwardVectorRotationRoll)
{
omath::source_engine::ViewAngles angles;
angles.roll = omath::source_engine::RollAngle::FromDegrees(90.f);
const auto forward = omath::source_engine::UpVector(angles);
EXPECT_NEAR(forward.x, omath::source_engine::kAbsRight.x, 0.00001f);
EXPECT_NEAR(forward.y, omath::source_engine::kAbsRight.y, 0.00001f);
EXPECT_NEAR(forward.z, omath::source_engine::kAbsRight.z, 0.00001f);
}
TEST(UnitTestSourceEngine, ProjectTargetMovedFromCamera) TEST(UnitTestSourceEngine, ProjectTargetMovedFromCamera)
{ {
constexpr auto fov = omath::projection::FieldOfView::FromDegrees(90.f); constexpr auto fov = omath::projection::FieldOfView::FromDegrees(90.f);

View File

@@ -5,7 +5,7 @@
#include <omath/engines/unity_engine/camera.hpp> #include <omath/engines/unity_engine/camera.hpp>
#include <omath/engines/unity_engine/constants.hpp> #include <omath/engines/unity_engine/constants.hpp>
#include <omath/engines/unity_engine/formulas.hpp> #include <omath/engines/unity_engine/formulas.hpp>
#include <print>
TEST(UnitTestUnityEngine, ForwardVector) TEST(UnitTestUnityEngine, ForwardVector)
{ {
@@ -14,6 +14,42 @@ TEST(UnitTestUnityEngine, ForwardVector)
EXPECT_EQ(forward, omath::unity_engine::kAbsForward); EXPECT_EQ(forward, omath::unity_engine::kAbsForward);
} }
TEST(UnitTestUnityEngine, ForwardVectorRotationYaw)
{
omath::unity_engine::ViewAngles angles;
angles.yaw = omath::unity_engine::YawAngle::FromDegrees(90.f);
const auto forward = omath::unity_engine::ForwardVector(angles);
EXPECT_NEAR(forward.x, omath::unity_engine::kAbsRight.x, 0.00001f);
EXPECT_NEAR(forward.y, omath::unity_engine::kAbsRight.y, 0.00001f);
EXPECT_NEAR(forward.z, omath::unity_engine::kAbsRight.z, 0.00001f);
}
TEST(UnitTestUnityEngine, ForwardVectorRotationPitch)
{
omath::unity_engine::ViewAngles angles;
angles.pitch = omath::unity_engine::PitchAngle::FromDegrees(-90.f);
const auto forward = omath::unity_engine::ForwardVector(angles);
EXPECT_NEAR(forward.x, omath::unity_engine::kAbsUp.x, 0.00001f);
EXPECT_NEAR(forward.y, omath::unity_engine::kAbsUp.y, 0.00001f);
EXPECT_NEAR(forward.z, omath::unity_engine::kAbsUp.z, 0.00001f);
}
TEST(UnitTestUnityEngine, ForwardVectorRotationRoll)
{
omath::unity_engine::ViewAngles angles;
angles.roll = omath::unity_engine::RollAngle::FromDegrees(-90.f);
const auto forward = omath::unity_engine::UpVector(angles);
EXPECT_NEAR(forward.x, omath::unity_engine::kAbsRight.x, 0.00001f);
EXPECT_NEAR(forward.y, omath::unity_engine::kAbsRight.y, 0.00001f);
EXPECT_NEAR(forward.z, omath::unity_engine::kAbsRight.z, 0.00001f);
}
TEST(UnitTestUnityEngine, RightVector) TEST(UnitTestUnityEngine, RightVector)
{ {
const auto right = omath::unity_engine::RightVector({}); const auto right = omath::unity_engine::RightVector({});

View File

@@ -0,0 +1,6 @@
//
// Created by Vlad on 4/18/2025.
//
#include <gtest/gtest.h>
#include <omath/3d_primitives/box.hpp>

View File

@@ -1,80 +1,121 @@
//
// Revised unittest suite for LineTracer (segmentbased MöllerTrumbore)
// Pure ASCII: avoids nonstandard characters that MSVC rejects.
//
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "omath/collision/line_tracer.hpp" #include "omath/collision/line_tracer.hpp"
#include "omath/triangle.hpp" #include "omath/triangle.hpp"
#include "omath/vector3.hpp" #include "omath/vector3.hpp"
#include <cmath>
using namespace omath; using namespace omath;
using namespace omath::collision; using namespace omath::collision;
class LineTracerTest : public ::testing::Test using Vec3 = Vector3<float>;
namespace
{ {
protected:
// Set up common variables for use in each test
Vector3<float> vertex1{0.0f, 0.0f, 0.0f};
Vector3<float> vertex2{1.0f, 0.0f, 0.0f};
Vector3<float> vertex3{0.0f, 1.0f, 0.0f};
Triangle<Vector3<float>> triangle{vertex1, vertex2, vertex3};
};
// Test that a ray intersecting the triangle returns false for CanTraceLine // -----------------------------------------------------------------------------
TEST_F(LineTracerTest, RayIntersectsTriangle) // Constants & helpers
{ // -----------------------------------------------------------------------------
constexpr Ray ray{{0.3f, 0.3f, -1.0f}, {0.3f, 0.3f, 1.0f}}; constexpr float kTol = 1e-5f;
EXPECT_FALSE(LineTracer::CanTraceLine(ray, triangle));
}
// Test that a ray parallel to the triangle plane returns true for CanTraceLine bool VecEqual(const Vec3& a, const Vec3& b, float tol = kTol)
TEST_F(LineTracerTest, RayParallelToTriangle) {
{ return std::fabs(a.x - b.x) < tol &&
constexpr Ray ray{{0.3f, 0.3f, 1.0f}, {0.3f, 0.3f, 2.0f}}; std::fabs(a.y - b.y) < tol &&
EXPECT_TRUE(LineTracer::CanTraceLine(ray, triangle)); std::fabs(a.z - b.z) < tol;
} }
// Test that a ray starting inside the triangle but pointing away returns true // -----------------------------------------------------------------------------
TEST_F(LineTracerTest, RayStartsInTriangleButDoesNotIntersect) // Fixture with one canonical rightangled triangle in the XY plane.
{ // -----------------------------------------------------------------------------
constexpr Ray ray{{0.3f, 0.3f, 0.0f}, {0.3f, 0.3f, -1.0f}}; class LineTracerFixture : public ::testing::Test
EXPECT_TRUE(LineTracer::CanTraceLine(ray, triangle)); {
} protected:
LineTracerFixture() :
triangle({0.f, 0.f, 0.f}, {1.f, 0.f, 0.f}, {0.f, 1.f, 0.f})
{
}
// Test that a ray not intersecting the triangle plane returns true Triangle<Vec3> triangle;
TEST_F(LineTracerTest, RayMissesTriangle)
{
constexpr Ray ray{{2.0f, 2.0f, -1.0f}, {2.0f, 2.0f, 1.0f}};
EXPECT_TRUE(LineTracer::CanTraceLine(ray, triangle));
}
// Test that a ray lying exactly in the plane of the triangle without intersecting returns true
TEST_F(LineTracerTest, RayInPlaneNotIntersecting)
{
constexpr Ray ray{{-1.0f, -1.0f, 0.0f}, {1.5f, 1.5f, 0.0f}};
EXPECT_TRUE(LineTracer::CanTraceLine(ray, triangle));
}
TEST_F(LineTracerTest, RayIntersectsVertex)
{
const Ray ray{{-1.0f, -1.0f, -1.0f}, vertex1}; // Intersecting at vertex1
EXPECT_TRUE(LineTracer::CanTraceLine(ray, triangle));
}
TEST_F(LineTracerTest, RayIntersectsEdge)
{
constexpr Ray ray{{-1.0f, 0.0f, -1.0f}, {0.5f, 0.0f, 0.0f}};
// Intersecting on the edge between vertex1 and vertex2
EXPECT_TRUE(LineTracer::CanTraceLine(ray, triangle));
}
TEST_F(LineTracerTest, TriangleFarBeyondRayEndPoint)
{
// Define a ray with a short length
constexpr Ray ray{{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f}};
// Define a triangle far beyond the ray's endpoint
constexpr Triangle<Vector3<float>> distantTriangle{
{1000.0f, 1000.0f, 1000.0f}, {1001.0f, 1000.0f, 1000.0f}, {1000.0f, 1001.0f, 1000.0f}
}; };
// Expect true because the ray ends long before it could reach the distant triangle // -----------------------------------------------------------------------------
EXPECT_TRUE(LineTracer::CanTraceLine(ray, distantTriangle)); // Datadriven tests for CanTraceLine
} // -----------------------------------------------------------------------------
struct TraceCase
{
Ray ray;
bool expected_clear; // true => segment does NOT hit the triangle
};
class CanTraceLineParam : public LineTracerFixture,
public ::testing::WithParamInterface<TraceCase>
{
};
TEST_P(CanTraceLineParam, VariousRays)
{
const auto& p = GetParam();
EXPECT_EQ(LineTracer::CanTraceLine(p.ray, triangle), p.expected_clear);
}
INSTANTIATE_TEST_SUITE_P(
BasicScenarios,
CanTraceLineParam,
::testing::Values(
TraceCase{Ray{{ 0.3f, 0.3f, -1.f},{ 0.3f, 0.3f, 1.f}}, false}, // hit through centre
TraceCase{Ray{{ 0.3f, 0.3f, 1.f},{ 0.3f, 0.3f, 2.f}}, true}, // parallel above
TraceCase{Ray{{ 0.3f, 0.3f, 0.f},{ 0.3f, 0.3f,-1.f}}, true}, // starts inside, goes away
TraceCase{Ray{{ 2.0f, 2.0f, -1.f},{ 2.0f, 2.0f, 1.f}}, true}, // misses entirely
TraceCase{Ray{{-1.0f,-1.0f, 0.f},{ 1.5f, 1.5f, 0.f}},true}, // lies in plane, outside tri
TraceCase{Ray{{-1.0f,-1.0f, -1.f},{ 0.0f, 0.0f, 0.f}}, true}, // endpoint on vertex
TraceCase{Ray{{-1.0f, 0.0f, -1.f},{ 0.5f, 0.0f, 0.f}}, true} // endpoint on edge
)
);
// -----------------------------------------------------------------------------
// Validate that the reported hit point is correct for a genuine intersection.
// -----------------------------------------------------------------------------
TEST_F(LineTracerFixture, HitPointCorrect)
{
constexpr Ray ray{{0.3f, 0.3f, -1.f}, {0.3f, 0.3f, 1.f}};
constexpr Vec3 expected{0.3f, 0.3f, 0.f};
const Vec3 hit = LineTracer::GetRayHitPoint(ray, triangle);
ASSERT_FALSE(VecEqual(hit, ray.end));
EXPECT_TRUE(VecEqual(hit, expected));
}
// -----------------------------------------------------------------------------
// Triangle far beyond the ray should not block.
// -----------------------------------------------------------------------------
TEST_F(LineTracerFixture, DistantTriangleClear)
{
constexpr Ray short_ray{{0.f, 0.f, 0.f}, {0.f, 0.f, 1.f}};
constexpr Triangle<Vec3> distant{{1000.f, 1000.f, 1000.f},
{1001.f, 1000.f, 1000.f},
{1000.f, 1001.f, 1000.f}};
EXPECT_TRUE(LineTracer::CanTraceLine(short_ray, distant));
}
TEST(LineTracerTraceRayEdge, CantHit)
{
constexpr omath::Triangle<Vector3<float>> triangle{{2, 0, 0}, {2, 2, 0}, {2, 2, 2}};
constexpr Ray ray{{}, {1.0, 0, 0}, false};
EXPECT_TRUE(omath::collision::LineTracer::CanTraceLine(ray, triangle));
}
TEST(LineTracerTraceRayEdge, CanHit)
{
constexpr omath::Triangle<Vector3<float>> triangle{{2, 0, 0}, {2, 2, 0}, {2, 2, 2}};
constexpr Ray ray{{}, {2.1, 0, 0}, false};
auto endPoint = omath::collision::LineTracer::GetRayHitPoint(ray, triangle);
EXPECT_FALSE(omath::collision::LineTracer::CanTraceLine(ray, triangle));
}
} // namespace

View File

@@ -180,10 +180,10 @@ TEST(UnitTestMatStandalone, Determinant_3x3)
} }
// Test Minor for 3x3 matrix // Test Minor for 3x3 matrix
TEST(UnitTestMatStandalone, Minor_3x3) TEST(UnitTestMatStandalone, Strip_3x3)
{ {
constexpr Mat<3, 3> m{{3, 0, 2}, {2, 0, -2}, {0, 1, 1}}; constexpr Mat<3, 3> m{{3, 0, 2}, {2, 0, -2}, {0, 1, 1}};
auto minor = m.Minor(0, 0); auto minor = m.Strip(0, 0);
EXPECT_EQ(minor.RowCount(), 2); EXPECT_EQ(minor.RowCount(), 2);
EXPECT_EQ(minor.ColumnsCount(), 2); EXPECT_EQ(minor.ColumnsCount(), 2);
EXPECT_FLOAT_EQ(minor.At(0, 0), 0.0f); EXPECT_FLOAT_EQ(minor.At(0, 0), 0.0f);
@@ -206,3 +206,11 @@ TEST(UnitTestMatStandalone, Transpose_NonSquare)
EXPECT_FLOAT_EQ(transposed.At(1, 1), 5.0f); EXPECT_FLOAT_EQ(transposed.At(1, 1), 5.0f);
EXPECT_FLOAT_EQ(transposed.At(2, 1), 6.0f); EXPECT_FLOAT_EQ(transposed.At(2, 1), 6.0f);
} }
TEST(UnitTestMatStandalone, Enverse)
{
constexpr Mat<2, 2> m{{1.0f, 3.0f}, {2.0f, 5.0f}};
constexpr Mat<2,2> mv{{-5.0f, 3.0f}, {2.0f, -1.0f}};
EXPECT_EQ(mv, m.Inverted());
}