Merge pull request #36 from orange-cpp/u/orange-cpp/small-refactoring

U/orange cpp/small refactoring
This commit is contained in:
2025-04-12 00:27:34 +03:00
committed by Orange
4 changed files with 35 additions and 32 deletions

View File

@@ -12,6 +12,8 @@ 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)
if (OMATH_BUILD_AS_SHARED_LIBRARY) if (OMATH_BUILD_AS_SHARED_LIBRARY)
add_library(omath SHARED source/matrix.cpp) add_library(omath SHARED source/matrix.cpp)
@@ -19,6 +21,7 @@ else()
add_library(omath STATIC source/matrix.cpp add_library(omath STATIC source/matrix.cpp
source/matrix.cpp) 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)
@@ -45,6 +48,10 @@ 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)
target_compile_definitions(omath PUBLIC OMATH_SUPRESS_SAFETY_CHECKS)
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}"

View File

@@ -82,9 +82,9 @@ TEST(UnitTestProjection, IsPointOnScreen)
EXPECT_TRUE(proj.has_value()); EXPECT_TRUE(proj.has_value());
} }
``` ```
## Showcase
<details> <details>
<summary>OMATH for making cheats</summary> <summary>OMATH for making cheats (click to open)</summary>
With `omath/projection` module you can achieve simple ESP hack for powered by Source/Unreal/Unity engine games, like [Apex Legends](https://store.steampowered.com/app/1172470/Apex_Legends/). With `omath/projection` module you can achieve simple ESP hack for powered by Source/Unreal/Unity engine games, like [Apex Legends](https://store.steampowered.com/app/1172470/Apex_Legends/).

View File

@@ -9,6 +9,7 @@
#include <stdexcept> #include <stdexcept>
#include <utility> #include <utility>
#include "omath/vector3.hpp" #include "omath/vector3.hpp"
#include <numeric>
#ifdef near #ifdef near
@@ -91,6 +92,12 @@ namespace omath
return At(row, col); return At(row, col);
} }
[[nodiscard]]
constexpr Type& operator[](const size_t row, const size_t col) const
{
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);
@@ -117,9 +124,10 @@ namespace omath
[[nodiscard]] [[nodiscard]]
constexpr const Type& At(const size_t rowIndex, const size_t columnIndex) const constexpr const Type& At(const size_t rowIndex, const size_t columnIndex) const
{ {
#if !defined(NDEBUG) && defined(OMATH_SUPRESS_SAFETY_CHECKS)
if (rowIndex >= Rows || columnIndex >= Columns) if (rowIndex >= Rows || columnIndex >= Columns)
throw std::out_of_range("Index out of range"); throw std::out_of_range("Index out of range");
#endif
if constexpr (StoreType == MatStoreType::ROW_MAJOR) if constexpr (StoreType == MatStoreType::ROW_MAJOR)
return m_data[rowIndex * Columns + columnIndex]; return m_data[rowIndex * Columns + columnIndex];
@@ -141,12 +149,7 @@ namespace omath
[[nodiscard]] [[nodiscard]]
constexpr Type Sum() const noexcept constexpr Type Sum() const noexcept
{ {
Type sum = 0; return std::accumulate(m_data.begin(), m_data.end(), Type(0));
for (size_t i = 0; i < Rows; ++i)
for (size_t j = 0; j < Columns; ++j)
sum += At(i, j);
return sum;
} }
constexpr void Clear() noexcept constexpr void Clear() noexcept
@@ -161,6 +164,7 @@ namespace omath
// Operator overloading for multiplication with another Mat // Operator overloading for multiplication with another Mat
template<size_t OtherColumns> template<size_t OtherColumns>
[[nodiscard]]
constexpr Mat<Rows, OtherColumns, Type, StoreType> constexpr Mat<Rows, OtherColumns, Type, StoreType>
operator*(const Mat<Columns, OtherColumns, Type, StoreType>& other) const operator*(const Mat<Columns, OtherColumns, Type, StoreType>& other) const
{ {
@@ -179,9 +183,7 @@ namespace omath
constexpr Mat& operator*=(const Type& f) noexcept constexpr Mat& operator*=(const Type& f) noexcept
{ {
for (size_t i = 0; i < Rows; ++i) std::ranges::for_each(m_data, [&f](auto& val) {val *= f;});
for (size_t j = 0; j < Columns; ++j)
At(i, j) *= f;
return *this; return *this;
} }
@@ -193,47 +195,39 @@ namespace omath
} }
[[nodiscard]] [[nodiscard]]
constexpr Mat operator*(const Type& f) const noexcept constexpr Mat operator*(const Type& value) const noexcept
{ {
Mat result(*this); Mat result(*this);
result *= f; result *= value;
return result; return result;
} }
constexpr Mat& operator/=(const Type& f) noexcept constexpr Mat& operator/=(const Type& value) noexcept
{ {
for (size_t i = 0; i < Rows; ++i) std::ranges::for_each(m_data, [&value](auto& val) {val /= value;});
for (size_t j = 0; j < Columns; ++j)
At(i, j) /= f;
return *this; return *this;
} }
[[nodiscard]] [[nodiscard]]
constexpr Mat operator/(const Type& f) const noexcept constexpr Mat operator/(const Type& value) const noexcept
{ {
Mat result(*this); Mat result(*this);
result /= f; result /= value;
return result; return result;
} }
constexpr Mat& operator=(const Mat& other) noexcept constexpr Mat& operator=(const Mat& other) noexcept
{ {
if (this == &other) if (this != &other)
return *this; m_data = other.m_data;
for (size_t i = 0; i < Rows; ++i)
for (size_t j = 0; j < Columns; ++j)
At(i, j) = other.At(i, j);
return *this; return *this;
} }
constexpr Mat& operator=(Mat&& other) noexcept constexpr Mat& operator=(Mat&& other) noexcept
{ {
if (this == &other) if (this != &other)
return *this; m_data = std::move(other.m_data);
for (size_t i = 0; i < Rows; ++i)
for (size_t j = 0; j < Columns; ++j)
At(i, j) = other.At(i, j);
return *this; return *this;
} }
@@ -257,7 +251,7 @@ namespace omath
if constexpr (Rows == 1) if constexpr (Rows == 1)
return At(0, 0); return At(0, 0);
else 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 else
{ {

View File

@@ -166,8 +166,10 @@ TEST_F(UnitTestMat, StaticMethod_ToScreenMat)
// Test exception handling in At() method // Test exception handling in At() method
TEST_F(UnitTestMat, Method_At_OutOfRange) TEST_F(UnitTestMat, Method_At_OutOfRange)
{ {
#if !defined(NDEBUG) && defined(OMATH_SUPRESS_SAFETY_CHECKS)
EXPECT_THROW(std::ignore = m2.At(2, 0), std::out_of_range); EXPECT_THROW(std::ignore = m2.At(2, 0), std::out_of_range);
EXPECT_THROW(std::ignore = m2.At(0, 2), std::out_of_range); EXPECT_THROW(std::ignore = m2.At(0, 2), std::out_of_range);
#endif
} }
// Test Determinant for 3x3 matrix // Test Determinant for 3x3 matrix