diff --git a/CMakeLists.txt b/CMakeLists.txt index a693502..ade1bb1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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_BUILD_EXAMPLES "Build example projects with you can learn & play" OFF) option(OMATH_STATIC_MSVC_RUNTIME_LIBRARY "Force Omath to link static runtime" OFF) +option(OMATH_SUPRESS_SAFETY_CHECKS "Supress some safety checks in release build to improve general performance" ON) + if (OMATH_BUILD_AS_SHARED_LIBRARY) add_library(omath SHARED source/matrix.cpp) @@ -19,6 +21,7 @@ else() add_library(omath STATIC source/matrix.cpp source/matrix.cpp) endif() + message(STATUS "Building on ${CMAKE_HOST_SYSTEM_NAME}") add_library(omath::omath ALIAS omath) @@ -45,6 +48,10 @@ if (OMATH_USE_AVX2) target_compile_definitions(omath PUBLIC OMATH_USE_AVX2) endif() +if (OMATH_SUPRESS_SAFETY_CHECKS) + target_compile_definitions(omath PUBLIC OMATH_SUPRESS_SAFETY_CHECKS) +endif() + set_target_properties(omath PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}" LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}" diff --git a/README.md b/README.md index dd70a7d..02cfeb8 100644 --- a/README.md +++ b/README.md @@ -82,9 +82,9 @@ TEST(UnitTestProjection, IsPointOnScreen) EXPECT_TRUE(proj.has_value()); } ``` - +## Showcase
- OMATH for making cheats + OMATH for making cheats (click to open) 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/). diff --git a/include/omath/mat.hpp b/include/omath/mat.hpp index 6b53821..24c57ce 100644 --- a/include/omath/mat.hpp +++ b/include/omath/mat.hpp @@ -9,6 +9,7 @@ #include #include #include "omath/vector3.hpp" +#include #ifdef near @@ -91,6 +92,12 @@ namespace omath 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 { m_data = std::move(other.m_data); @@ -117,9 +124,10 @@ namespace omath [[nodiscard]] 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) throw std::out_of_range("Index out of range"); - +#endif if constexpr (StoreType == MatStoreType::ROW_MAJOR) return m_data[rowIndex * Columns + columnIndex]; @@ -141,12 +149,7 @@ namespace omath [[nodiscard]] constexpr Type Sum() const noexcept { - Type sum = 0; - for (size_t i = 0; i < Rows; ++i) - for (size_t j = 0; j < Columns; ++j) - sum += At(i, j); - - return sum; + return std::accumulate(m_data.begin(), m_data.end(), Type(0)); } constexpr void Clear() noexcept @@ -161,6 +164,7 @@ namespace omath // Operator overloading for multiplication with another Mat template + [[nodiscard]] constexpr Mat operator*(const Mat& other) const { @@ -179,9 +183,7 @@ namespace omath constexpr Mat& operator*=(const Type& f) noexcept { - for (size_t i = 0; i < Rows; ++i) - for (size_t j = 0; j < Columns; ++j) - At(i, j) *= f; + std::ranges::for_each(m_data, [&f](auto& val) {val *= f;}); return *this; } @@ -193,47 +195,39 @@ namespace omath } [[nodiscard]] - constexpr Mat operator*(const Type& f) const noexcept + constexpr Mat operator*(const Type& value) const noexcept { Mat result(*this); - result *= f; + result *= value; return result; } - constexpr Mat& operator/=(const Type& f) noexcept + constexpr Mat& operator/=(const Type& value) noexcept { - for (size_t i = 0; i < Rows; ++i) - for (size_t j = 0; j < Columns; ++j) - At(i, j) /= f; + std::ranges::for_each(m_data, [&value](auto& val) {val /= value;}); return *this; } [[nodiscard]] - constexpr Mat operator/(const Type& f) const noexcept + constexpr Mat operator/(const Type& value) const noexcept { Mat result(*this); - result /= f; + result /= value; return result; } constexpr Mat& operator=(const Mat& other) noexcept { - if (this == &other) - return *this; - for (size_t i = 0; i < Rows; ++i) - for (size_t j = 0; j < Columns; ++j) - At(i, j) = other.At(i, j); + if (this != &other) + m_data = other.m_data; + return *this; } constexpr Mat& operator=(Mat&& other) noexcept { - if (this == &other) - return *this; - - for (size_t i = 0; i < Rows; ++i) - for (size_t j = 0; j < Columns; ++j) - At(i, j) = other.At(i, j); + if (this != &other) + m_data = std::move(other.m_data); return *this; } @@ -257,7 +251,7 @@ namespace omath if constexpr (Rows == 1) 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); else { diff --git a/tests/general/unit_test_mat.cpp b/tests/general/unit_test_mat.cpp index 97b94d1..43b25c1 100644 --- a/tests/general/unit_test_mat.cpp +++ b/tests/general/unit_test_mat.cpp @@ -166,8 +166,10 @@ TEST_F(UnitTestMat, StaticMethod_ToScreenMat) // Test exception handling in At() method 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(0, 2), std::out_of_range); +#endif } // Test Determinant for 3x3 matrix