From 103c3d38e48814c5680d53e5d547b18ee5a04d09 Mon Sep 17 00:00:00 2001 From: Orange Date: Sun, 20 Oct 2024 00:32:34 +0300 Subject: [PATCH 1/3] moving from .h to .hpp to make thing more clear --- include/omath/{Angles.h => Angles.hpp} | 0 include/omath/{Color.h => Color.hpp} | 4 ++-- include/omath/{Mat.h => Mat.hpp} | 4 ++-- include/omath/{Matrix.h => Matrix.hpp} | 0 include/omath/{Vector2.h => Vector2.hpp} | 0 include/omath/{Vector3.h => Vector3.hpp} | 2 +- include/omath/{Vector4.h => Vector4.hpp} | 2 +- include/omath/pathfinding/{Astar.h => Astar.hpp} | 4 ++-- .../pathfinding/{NavigationMesh.h => NavigationMesh.hpp} | 2 +- include/omath/prediction/{Engine.h => Engine.hpp} | 6 +++--- include/omath/prediction/{Projectile.h => Projectile.hpp} | 2 +- include/omath/prediction/{Target.h => Target.hpp} | 2 +- include/omath/projection/{Camera.h => Camera.hpp} | 6 +++--- include/omath/projection/{ErrorCodes.h => ErrorCodes.hpp} | 0 source/Matrix.cpp | 6 +++--- source/Vector2.cpp | 2 +- source/Vector3.cpp | 4 ++-- source/Vector4.cpp | 2 +- source/color.cpp | 2 +- source/pathfinding/Astar.cpp | 2 +- source/pathfinding/NavigationMesh.cpp | 2 +- source/prediction/Engine.cpp | 4 ++-- source/prediction/Projectile.cpp | 2 +- source/prediction/Target.cpp | 2 +- source/projection/Camera.cpp | 4 ++-- tests/UnitTestAstar.cpp | 2 +- tests/UnitTestColor.cpp | 2 +- tests/UnitTestMat.cpp | 4 ++-- tests/UnitTestMatrix.cpp | 4 ++-- tests/UnitTestPrediction.cpp | 2 +- tests/UnitTestProjection.cpp | 4 ++-- tests/UnitTestVector2.cpp | 2 +- tests/UnitTestVector3.cpp | 2 +- tests/UnitTestVector4.cpp | 2 +- 34 files changed, 45 insertions(+), 45 deletions(-) rename include/omath/{Angles.h => Angles.hpp} (100%) rename include/omath/{Color.h => Color.hpp} (98%) rename include/omath/{Mat.h => Mat.hpp} (99%) rename include/omath/{Matrix.h => Matrix.hpp} (100%) rename include/omath/{Vector2.h => Vector2.hpp} (100%) rename include/omath/{Vector3.h => Vector3.hpp} (99%) rename include/omath/{Vector4.h => Vector4.hpp} (99%) rename include/omath/pathfinding/{Astar.h => Astar.hpp} (83%) rename include/omath/pathfinding/{NavigationMesh.h => NavigationMesh.hpp} (95%) rename include/omath/prediction/{Engine.h => Engine.hpp} (90%) rename include/omath/prediction/{Projectile.h => Projectile.hpp} (92%) rename include/omath/prediction/{Target.h => Target.hpp} (94%) rename include/omath/projection/{Camera.h => Camera.hpp} (92%) rename include/omath/projection/{ErrorCodes.h => ErrorCodes.hpp} (100%) diff --git a/include/omath/Angles.h b/include/omath/Angles.hpp similarity index 100% rename from include/omath/Angles.h rename to include/omath/Angles.hpp diff --git a/include/omath/Color.h b/include/omath/Color.hpp similarity index 98% rename from include/omath/Color.h rename to include/omath/Color.hpp index e8da221..4625545 100644 --- a/include/omath/Color.h +++ b/include/omath/Color.hpp @@ -4,9 +4,9 @@ #pragma once -#include "omath/Vector3.h" +#include "omath/Vector3.hpp" #include -#include "omath/Vector4.h" +#include "omath/Vector4.hpp" namespace omath diff --git a/include/omath/Mat.h b/include/omath/Mat.hpp similarity index 99% rename from include/omath/Mat.h rename to include/omath/Mat.hpp index 188aea0..d19dbca 100644 --- a/include/omath/Mat.h +++ b/include/omath/Mat.hpp @@ -6,9 +6,9 @@ #include #include #include -#include "Vector3.h" +#include "Vector3.hpp" #include -#include "Angles.h" +#include "Angles.hpp" namespace omath diff --git a/include/omath/Matrix.h b/include/omath/Matrix.hpp similarity index 100% rename from include/omath/Matrix.h rename to include/omath/Matrix.hpp diff --git a/include/omath/Vector2.h b/include/omath/Vector2.hpp similarity index 100% rename from include/omath/Vector2.h rename to include/omath/Vector2.hpp diff --git a/include/omath/Vector3.h b/include/omath/Vector3.hpp similarity index 99% rename from include/omath/Vector3.h rename to include/omath/Vector3.hpp index 53ea6ac..ec10848 100644 --- a/include/omath/Vector3.h +++ b/include/omath/Vector3.hpp @@ -6,7 +6,7 @@ #include #include -#include "omath/Vector2.h" +#include "omath/Vector2.hpp" namespace omath diff --git a/include/omath/Vector4.h b/include/omath/Vector4.hpp similarity index 99% rename from include/omath/Vector4.h rename to include/omath/Vector4.hpp index 46d61ae..55c364c 100644 --- a/include/omath/Vector4.h +++ b/include/omath/Vector4.hpp @@ -3,7 +3,7 @@ // #pragma once -#include +#include #include diff --git a/include/omath/pathfinding/Astar.h b/include/omath/pathfinding/Astar.hpp similarity index 83% rename from include/omath/pathfinding/Astar.h rename to include/omath/pathfinding/Astar.hpp index 64bfc88..0609850 100644 --- a/include/omath/pathfinding/Astar.h +++ b/include/omath/pathfinding/Astar.hpp @@ -4,8 +4,8 @@ #pragma once #include -#include "NavigationMesh.h" -#include "omath/Vector3.h" +#include "NavigationMesh.hpp" +#include "omath/Vector3.hpp" namespace omath::pathfinding diff --git a/include/omath/pathfinding/NavigationMesh.h b/include/omath/pathfinding/NavigationMesh.hpp similarity index 95% rename from include/omath/pathfinding/NavigationMesh.h rename to include/omath/pathfinding/NavigationMesh.hpp index 11e08bb..e7aceb4 100644 --- a/include/omath/pathfinding/NavigationMesh.h +++ b/include/omath/pathfinding/NavigationMesh.hpp @@ -4,7 +4,7 @@ #pragma once -#include "omath/Vector3.h" +#include "omath/Vector3.hpp" #include #include #include diff --git a/include/omath/prediction/Engine.h b/include/omath/prediction/Engine.hpp similarity index 90% rename from include/omath/prediction/Engine.h rename to include/omath/prediction/Engine.hpp index af7876f..6d8e617 100644 --- a/include/omath/prediction/Engine.h +++ b/include/omath/prediction/Engine.hpp @@ -5,9 +5,9 @@ #pragma once #include -#include "omath/Vector3.h" -#include "omath/prediction/Projectile.h" -#include "omath/prediction/Target.h" +#include "omath/Vector3.hpp" +#include "omath/prediction/Projectile.hpp" +#include "omath/prediction/Target.hpp" namespace omath::prediction diff --git a/include/omath/prediction/Projectile.h b/include/omath/prediction/Projectile.hpp similarity index 92% rename from include/omath/prediction/Projectile.h rename to include/omath/prediction/Projectile.hpp index 1249e97..93ded29 100644 --- a/include/omath/prediction/Projectile.h +++ b/include/omath/prediction/Projectile.hpp @@ -3,7 +3,7 @@ // #pragma once -#include "omath/Vector3.h" +#include "omath/Vector3.hpp" namespace omath::prediction diff --git a/include/omath/prediction/Target.h b/include/omath/prediction/Target.hpp similarity index 94% rename from include/omath/prediction/Target.h rename to include/omath/prediction/Target.hpp index 0332c53..e44b868 100644 --- a/include/omath/prediction/Target.h +++ b/include/omath/prediction/Target.hpp @@ -3,7 +3,7 @@ // #pragma once -#include "omath/Vector3.h" +#include "omath/Vector3.hpp" namespace omath::prediction diff --git a/include/omath/projection/Camera.h b/include/omath/projection/Camera.hpp similarity index 92% rename from include/omath/projection/Camera.h rename to include/omath/projection/Camera.hpp index f7020c8..d2d450e 100644 --- a/include/omath/projection/Camera.h +++ b/include/omath/projection/Camera.hpp @@ -5,10 +5,10 @@ #pragma once #include -#include -#include +#include +#include #include -#include "ErrorCodes.h" +#include "ErrorCodes.hpp" namespace omath::projection diff --git a/include/omath/projection/ErrorCodes.h b/include/omath/projection/ErrorCodes.hpp similarity index 100% rename from include/omath/projection/ErrorCodes.h rename to include/omath/projection/ErrorCodes.hpp diff --git a/source/Matrix.cpp b/source/Matrix.cpp index 8872774..03096e8 100644 --- a/source/Matrix.cpp +++ b/source/Matrix.cpp @@ -1,6 +1,6 @@ -#include "omath/Matrix.h" -#include "omath/Vector3.h" -#include "omath/Angles.h" +#include "omath/Matrix.hpp" +#include "omath/Vector3.hpp" +#include "omath/Angles.hpp" #include diff --git a/source/Vector2.cpp b/source/Vector2.cpp index 56d8044..58188ac 100644 --- a/source/Vector2.cpp +++ b/source/Vector2.cpp @@ -1,7 +1,7 @@ // // Created by Vlad on 02.09.2024. // -#include "omath/Vector2.h" +#include "omath/Vector2.hpp" #include diff --git a/source/Vector3.cpp b/source/Vector3.cpp index 8da7c68..acd2b02 100644 --- a/source/Vector3.cpp +++ b/source/Vector3.cpp @@ -2,9 +2,9 @@ // Created by vlad on 10/28/23. // -#include +#include #include -#include +#include namespace omath { diff --git a/source/Vector4.cpp b/source/Vector4.cpp index 1d54d80..a995337 100644 --- a/source/Vector4.cpp +++ b/source/Vector4.cpp @@ -2,7 +2,7 @@ // Vector4.cpp // -#include "omath/Vector4.h" +#include "omath/Vector4.hpp" #include diff --git a/source/color.cpp b/source/color.cpp index 1de3bfd..3e8b584 100644 --- a/source/color.cpp +++ b/source/color.cpp @@ -2,7 +2,7 @@ // Created by vlad on 2/4/24. // -#include "omath/Color.h" +#include "omath/Color.hpp" #include #include diff --git a/source/pathfinding/Astar.cpp b/source/pathfinding/Astar.cpp index 10a03e9..37ec8e1 100644 --- a/source/pathfinding/Astar.cpp +++ b/source/pathfinding/Astar.cpp @@ -1,7 +1,7 @@ // // Created by Vlad on 28.07.2024. // -#include "omath/pathfinding/Astar.h" +#include "omath/pathfinding/Astar.hpp" #include #include diff --git a/source/pathfinding/NavigationMesh.cpp b/source/pathfinding/NavigationMesh.cpp index 1a106df..87b9db2 100644 --- a/source/pathfinding/NavigationMesh.cpp +++ b/source/pathfinding/NavigationMesh.cpp @@ -1,7 +1,7 @@ // // Created by Vlad on 28.07.2024. // -#include "omath/pathfinding/NavigationMesh.h" +#include "omath/pathfinding/NavigationMesh.hpp" #include #include diff --git a/source/prediction/Engine.cpp b/source/prediction/Engine.cpp index 47d5ea5..08cb81d 100644 --- a/source/prediction/Engine.cpp +++ b/source/prediction/Engine.cpp @@ -3,9 +3,9 @@ // -#include "omath/prediction/Engine.h" +#include "omath/prediction/Engine.hpp" #include -#include +#include namespace omath::prediction diff --git a/source/prediction/Projectile.cpp b/source/prediction/Projectile.cpp index 32cbe68..26cfd8e 100644 --- a/source/prediction/Projectile.cpp +++ b/source/prediction/Projectile.cpp @@ -2,7 +2,7 @@ // Created by Vlad on 6/9/2024. // -#include "omath/prediction/Projectile.h" +#include "omath/prediction/Projectile.hpp" #include diff --git a/source/prediction/Target.cpp b/source/prediction/Target.cpp index 3b4c2da..9b12395 100644 --- a/source/prediction/Target.cpp +++ b/source/prediction/Target.cpp @@ -2,7 +2,7 @@ // Created by Vlad on 6/9/2024. // -#include "omath/prediction/Target.h" +#include "omath/prediction/Target.hpp" namespace omath::prediction diff --git a/source/projection/Camera.cpp b/source/projection/Camera.cpp index 41275b4..be552fa 100644 --- a/source/projection/Camera.cpp +++ b/source/projection/Camera.cpp @@ -1,11 +1,11 @@ // // Created by Vlad on 27.08.2024. // -#include "omath/projection/Camera.h" +#include "omath/projection/Camera.hpp" #include -#include "omath/Angles.h" +#include "omath/Angles.hpp" namespace omath::projection diff --git a/tests/UnitTestAstar.cpp b/tests/UnitTestAstar.cpp index f16cd8e..da65f9f 100644 --- a/tests/UnitTestAstar.cpp +++ b/tests/UnitTestAstar.cpp @@ -2,7 +2,7 @@ // Created by Vlad on 18.08.2024. // #include -#include +#include TEST(UnitTestAstar, FindingRightPath) diff --git a/tests/UnitTestColor.cpp b/tests/UnitTestColor.cpp index a78276d..aade7f8 100644 --- a/tests/UnitTestColor.cpp +++ b/tests/UnitTestColor.cpp @@ -2,7 +2,7 @@ // Created by Vlad on 01.09.2024. // #include -#include +#include using namespace omath; diff --git a/tests/UnitTestMat.cpp b/tests/UnitTestMat.cpp index 931c72f..613325c 100644 --- a/tests/UnitTestMat.cpp +++ b/tests/UnitTestMat.cpp @@ -1,7 +1,7 @@ // UnitTestMat.cpp #include -#include "omath/Mat.h" -#include "omath/Vector3.h" +#include "omath/Mat.hpp" +#include "omath/Vector3.hpp" using namespace omath; diff --git a/tests/UnitTestMatrix.cpp b/tests/UnitTestMatrix.cpp index 7450bc6..c9a8b93 100644 --- a/tests/UnitTestMatrix.cpp +++ b/tests/UnitTestMatrix.cpp @@ -2,8 +2,8 @@ // Created by vlad on 5/18/2024. // #include -#include -#include "omath/Vector3.h" +#include +#include "omath/Vector3.hpp" using namespace omath; diff --git a/tests/UnitTestPrediction.cpp b/tests/UnitTestPrediction.cpp index c57d79b..64fe578 100644 --- a/tests/UnitTestPrediction.cpp +++ b/tests/UnitTestPrediction.cpp @@ -1,5 +1,5 @@ #include -#include +#include TEST(UnitTestPrediction, PredictionTest) { diff --git a/tests/UnitTestProjection.cpp b/tests/UnitTestProjection.cpp index 2bb50f9..3424633 100644 --- a/tests/UnitTestProjection.cpp +++ b/tests/UnitTestProjection.cpp @@ -3,9 +3,9 @@ // #include #include -#include +#include #include -#include +#include TEST(UnitTestProjection, Projection) { diff --git a/tests/UnitTestVector2.cpp b/tests/UnitTestVector2.cpp index dc74b29..0255363 100644 --- a/tests/UnitTestVector2.cpp +++ b/tests/UnitTestVector2.cpp @@ -3,7 +3,7 @@ // #include -#include +#include #include // For std::isinf and std::isnan #include // For FLT_MAX and FLT_MIN diff --git a/tests/UnitTestVector3.cpp b/tests/UnitTestVector3.cpp index 67625bb..f08a35f 100644 --- a/tests/UnitTestVector3.cpp +++ b/tests/UnitTestVector3.cpp @@ -3,7 +3,7 @@ // #include -#include +#include #include #include // For FLT_MAX, FLT_MIN #include // For std::numeric_limits diff --git a/tests/UnitTestVector4.cpp b/tests/UnitTestVector4.cpp index f9c947f..07ca898 100644 --- a/tests/UnitTestVector4.cpp +++ b/tests/UnitTestVector4.cpp @@ -6,7 +6,7 @@ // #include -#include +#include #include // For std::numeric_limits using namespace omath; From 6c85515dd0c5fe2ea91118804580553e288d09e9 Mon Sep 17 00:00:00 2001 From: Orange Date: Sun, 20 Oct 2024 08:52:11 -0700 Subject: [PATCH 2/3] small fixes --- include/omath/Vector2.hpp | 2 +- source/prediction/Engine.cpp | 25 +++++++++++++------------ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/include/omath/Vector2.hpp b/include/omath/Vector2.hpp index 1ad0341..ba5017e 100644 --- a/include/omath/Vector2.hpp +++ b/include/omath/Vector2.hpp @@ -18,7 +18,7 @@ namespace omath // Constructors constexpr Vector2() : x(0.f), y(0.f) {} - constexpr Vector2(float x, float y) : x(x), y(y) {} + constexpr Vector2(const float x, const float y) : x(x), y(y) {} // Equality operators [[nodiscard]] diff --git a/source/prediction/Engine.cpp b/source/prediction/Engine.cpp index 08cb81d..2bc1b1c 100644 --- a/source/prediction/Engine.cpp +++ b/source/prediction/Engine.cpp @@ -11,11 +11,11 @@ namespace omath::prediction { Engine::Engine(const float gravityConstant, const float simulationTimeStep, - const float maximumSimulationTime, const float distanceTolerance) - : m_gravityConstant(gravityConstant), - m_simulationTimeStep(simulationTimeStep), - m_maximumSimulationTime(maximumSimulationTime), - m_distanceTolerance(distanceTolerance) + const float maximumSimulationTime, const float distanceTolerance) + : m_gravityConstant(gravityConstant), + m_simulationTimeStep(simulationTimeStep), + m_maximumSimulationTime(maximumSimulationTime), + m_distanceTolerance(distanceTolerance) { } @@ -28,7 +28,7 @@ namespace omath::prediction const auto projectilePitch = MaybeCalculateProjectileLaunchPitchAngle(projectile, predictedTargetPosition); if (!projectilePitch.has_value()) [[unlikely]] - continue; + continue; if (!IsProjectileReachedTarget(predictedTargetPosition, projectile, projectilePitch.value(), time)) continue; @@ -42,19 +42,20 @@ namespace omath::prediction } std::optional Engine::MaybeCalculateProjectileLaunchPitchAngle(const Projectile &projectile, - const Vector3 &targetPosition) const + const Vector3 &targetPosition) const { const auto bulletGravity = m_gravityConstant * projectile.m_gravityScale; const auto delta = targetPosition - projectile.m_origin; const auto distance2d = delta.Length2D(); + const auto distance2dSqr = distance2d * distance2d; + const auto launchSpeedSqr = projectile.m_launchSpeed * projectile.m_launchSpeed; - - float root = std::pow(projectile.m_launchSpeed, 4.f) - bulletGravity * (bulletGravity * - std::pow(distance2d, 2.f) + 2.0f * delta.z * std::pow(projectile.m_launchSpeed, 2.f)); + float root = launchSpeedSqr * launchSpeedSqr - bulletGravity * (bulletGravity * + distance2dSqr + 2.0f * delta.z * launchSpeedSqr); if (root < 0.0f) [[unlikely]] - return std::nullopt; + return std::nullopt; root = std::sqrt(root); const float angle = std::atan((std::pow(projectile.m_launchSpeed, 2.f) - root) / (bulletGravity * distance2d)); @@ -63,7 +64,7 @@ namespace omath::prediction } bool Engine::IsProjectileReachedTarget(const Vector3 &targetPosition, const Projectile &projectile, - const float pitch, const float time) const + const float pitch, const float time) const { const auto yaw = projectile.m_origin.ViewAngleTo(targetPosition).y; const auto projectilePosition = projectile.PredictPosition(pitch, yaw, time, m_gravityConstant); From 50c322dbfcced270d45074635d96ae2f9a550fe5 Mon Sep 17 00:00:00 2001 From: Orange Date: Sun, 20 Oct 2024 09:01:18 -0700 Subject: [PATCH 3/3] Mat support multiple integral types --- include/omath/Mat.hpp | 130 ++++++++++++++++++++++-------------------- 1 file changed, 67 insertions(+), 63 deletions(-) diff --git a/include/omath/Mat.hpp b/include/omath/Mat.hpp index d19dbca..c41bfb9 100644 --- a/include/omath/Mat.hpp +++ b/include/omath/Mat.hpp @@ -13,18 +13,17 @@ namespace omath { - template + template class Mat final { public: - constexpr Mat() { Clear(); } - constexpr Mat(const std::initializer_list>& rows) + constexpr Mat(const std::initializer_list > &rows) { if (rows.size() != Rows) throw std::invalid_argument("Initializer list rows size does not match template parameter Rows"); @@ -33,7 +32,8 @@ namespace omath for (size_t i = 0; i < Rows; ++i, ++rowIt) { if (rowIt->size() != Columns) - throw std::invalid_argument("All rows must have the same number of columns as template parameter Columns"); + throw std::invalid_argument( + "All rows must have the same number of columns as template parameter Columns"); auto colIt = rowIt->begin(); for (size_t j = 0; j < Columns; ++j, ++colIt) @@ -44,13 +44,13 @@ namespace omath } - constexpr Mat(const Mat& other) + constexpr Mat(const Mat &other) noexcept { m_data = other.m_data; } - constexpr Mat(Mat&& other) noexcept + constexpr Mat(Mat &&other) noexcept { m_data = std::move(other.m_data); } @@ -62,24 +62,26 @@ namespace omath static consteval size_t ColumnsCount() noexcept { return Columns; } [[nodiscard]] - static consteval std::pair Size() noexcept { return { Rows, Columns }; } + static consteval std::pair Size() noexcept { return {Rows, Columns}; } - [[nodiscard]] constexpr const float& At(const size_t rowIndex, const size_t columnIndex) const + [[nodiscard]] constexpr const Type &At(const size_t rowIndex, const size_t columnIndex) const { if (rowIndex >= Rows || columnIndex >= Columns) throw std::out_of_range("Index out of range"); return m_data[rowIndex * Columns + columnIndex]; } - [[nodiscard]] constexpr float& At(const size_t rowIndex, const size_t columnIndex) + + [[nodiscard]] constexpr Type &At(const size_t rowIndex, const size_t columnIndex) { - return const_cast(std::as_const(*this).At(rowIndex, columnIndex)); + return const_cast(std::as_const(*this).At(rowIndex, columnIndex)); } + [[nodiscard]] - constexpr float Sum() const + constexpr Type Sum() const noexcept { - float sum = 0.f; + Type sum = 0; for (size_t i = 0; i < Rows; ++i) for (size_t j = 0; j < Columns; ++j) sum += At(i, j); @@ -87,25 +89,26 @@ namespace omath return sum; } - constexpr void Clear() + constexpr void Clear() noexcept { - Set(0.f); + Set(0); } - constexpr void Set(const float value) + constexpr void Set(const Type &value) noexcept { std::ranges::fill(m_data, value); } + // Operator overloading for multiplication with another Mat - template - constexpr Mat operator*(const Mat& other) const + template + constexpr Mat operator*(const Mat &other) const { Mat result; for (size_t i = 0; i < Rows; ++i) for (size_t j = 0; j < OtherColumns; ++j) { - float sum = 0.f; + Type sum = 0; for (size_t k = 0; k < Columns; ++k) sum += At(i, k) * other.At(k, j); result.At(i, j) = sum; @@ -113,7 +116,7 @@ namespace omath return result; } - constexpr Mat& operator*=(float f) + constexpr Mat &operator*=(Type f) noexcept { for (size_t i = 0; i < Rows; ++i) for (size_t j = 0; j < Columns; ++j) @@ -121,20 +124,20 @@ namespace omath return *this; } - template - constexpr Mat operator*=(const Mat& other) + template + constexpr Mat operator*=(const Mat &other) { return *this = *this * other; } - constexpr Mat operator*(float f) const + constexpr Mat operator*(const Type &f) const noexcept { Mat result(*this); result *= f; return result; } - constexpr Mat& operator/=(float f) + constexpr Mat &operator/=(const Type &f) noexcept { for (size_t i = 0; i < Rows; ++i) for (size_t j = 0; j < Columns; ++j) @@ -142,14 +145,14 @@ namespace omath return *this; } - constexpr Mat operator/(float f) const + constexpr Mat operator/(const Type &f) const noexcept { Mat result(*this); result /= f; return result; } - constexpr Mat& operator=(const Mat& other) + constexpr Mat &operator=(const Mat &other) noexcept { if (this == &other) return *this; @@ -159,7 +162,7 @@ namespace omath return *this; } - constexpr Mat& operator=(Mat&& other) noexcept + constexpr Mat &operator=(Mat &&other) noexcept { if (this == &other) return *this; @@ -172,7 +175,7 @@ namespace omath } [[nodiscard]] - constexpr Mat Transpose() const + constexpr Mat Transpose() const noexcept { Mat transposed; for (size_t i = 0; i < Rows; ++i) @@ -183,7 +186,7 @@ namespace omath } [[nodiscard]] - constexpr float Determinant() const + constexpr Type Determinant() const { static_assert(Rows == Columns, "Determinant is only defined for square matrices."); @@ -194,10 +197,10 @@ namespace omath return At(0, 0) * At(1, 1) - At(0, 1) * At(1, 0); else { - float det = 0.f; + Type det = 0; for (size_t i = 0; i < Columns; ++i) { - const float cofactor = (i % 2 == 0 ? 1.f : -1.f) * At(0, i) * Minor(0, i).Determinant(); + const Type cofactor = (i % 2 == 0 ? 1 : -1) * At(0, i) * Minor(0, i).Determinant(); det += cofactor; } return det; @@ -225,7 +228,7 @@ namespace omath } [[nodiscard]] - std::string ToString() const + std::string ToString() const noexcept { std::ostringstream oss; for (size_t i = 0; i < Rows; ++i) @@ -243,58 +246,59 @@ namespace omath // Static methods that return fixed-size matrices [[nodiscard]] - constexpr static Mat<4, 4> ToScreenMat(const float screenWidth, const float screenHeight) + constexpr static Mat<4, 4> ToScreenMat(const Type &screenWidth, const Type &screenHeight) noexcept { return { - {screenWidth / 2.f, 0.f, 0.f, 0.f}, - {0.f, -screenHeight / 2.f, 0.f, 0.f}, - {0.f, 0.f, 1.f, 0.f}, - {screenWidth / 2.f, screenHeight / 2.f, 0.f, 1.f}, - }; - } - - [[nodiscard]] - constexpr static Mat<4, 4> TranslationMat(const Vector3& diff) - { - return - { - {1.f, 0.f, 0.f, 0.f}, - {0.f, 1.f, 0.f, 0.f}, - {0.f, 0.f, 1.f, 0.f}, - {diff.x, diff.y, diff.z, 1.f}, + {screenWidth / 2, 0, 0, 0}, + {0, -screenHeight / 2, 0, 0}, + {0, 0, 1, 0}, + {screenWidth / 2, screenHeight / 2, 0, 1}, }; } [[nodiscard]] - constexpr static Mat<4, 4> OrientationMat(const Vector3& forward, const Vector3& right, const Vector3& up) + constexpr static Mat<4, 4> TranslationMat(const Vector3 &diff) noexcept { return { - {right.x, up.x, forward.x, 0.f}, - {right.y, up.y, forward.y, 0.f}, - {right.z, up.z, forward.z, 0.f}, - {0.f, 0.f, 0.f, 1.f}, + {1, 0, 0, 0}, + {0, 1, 0, 0}, + {0, 0, 1, 0}, + {diff.x, diff.y, diff.z, 1}, }; } [[nodiscard]] - constexpr static Mat<4, 4> ProjectionMat(const float fieldOfView, const float aspectRatio, - const float near, const float far, const float lensZoom) + constexpr static Mat<4, 4> OrientationMat(const Vector3 &forward, const Vector3 &right, + const Vector3 &up) noexcept { - const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f); - const float frustumHeight = far - near; + return + { + {right.x, up.x, forward.x, 0}, + {right.y, up.y, forward.y, 0}, + {right.z, up.z, forward.z, 0}, + {0, 0, 0, 1}, + }; + } + + [[nodiscard]] + constexpr static Mat<4, 4> ProjectionMat(const Type &fieldOfView, const Type &aspectRatio, + const Type &near, const Type &far, const Type &lensZoom) noexcept + { + const Type &fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2); + const Type &frustumHeight = far - near; return { - {-1.f / (aspectRatio * fovHalfTan) * lensZoom, 0.f, 0.f, 0.f}, - {0.f, -1.f / fovHalfTan * lensZoom, 0.f, 0.f}, - {0.f, 0.f, -far / frustumHeight, -1}, - {0.f, 0.f, near * far / frustumHeight, 0.f} + {-1 / (aspectRatio * fovHalfTan) * lensZoom, 0, 0, 0}, + {0, -1 / fovHalfTan * lensZoom, 0, 0}, + {0, 0, -far / frustumHeight, -1}, + {0, 0, near * far / frustumHeight, 0} }; } private: - std::array m_data; + std::array m_data; }; -} \ No newline at end of file +}