From 6d0d267743f97104d85f788e8099d7717eae2986 Mon Sep 17 00:00:00 2001 From: Vladislav Alpatov Date: Sat, 1 Mar 2025 21:11:46 +0300 Subject: [PATCH 1/3] now template --- CMakeLists.txt | 5 +- include/omath/Color.hpp | 4 +- include/omath/Mat.hpp | 10 +-- include/omath/Matrix.hpp | 6 +- include/omath/Triangle.hpp | 14 ++-- include/omath/Vector2.hpp | 36 ++++---- include/omath/Vector3.hpp | 83 +++++++++++-------- include/omath/Vector4.hpp | 60 +++++++------- include/omath/collision/LineTracer.hpp | 12 +-- include/omath/engines/OpenGL/Camera.hpp | 4 +- include/omath/engines/OpenGL/Constants.hpp | 6 +- include/omath/engines/OpenGL/Formulas.hpp | 8 +- include/omath/engines/Source/Camera.hpp | 4 +- include/omath/engines/Source/Constants.hpp | 6 +- include/omath/engines/Source/Formulas.hpp | 8 +- include/omath/pathfinding/Astar.hpp | 13 ++- include/omath/pathfinding/NavigationMesh.hpp | 6 +- .../projectile_prediction/ProjPredEngine.hpp | 2 +- .../ProjPredEngineAVX2.hpp | 4 +- .../ProjPredEngineLegacy.hpp | 6 +- .../projectile_prediction/Projectile.hpp | 4 +- .../omath/projectile_prediction/Target.hpp | 6 +- include/omath/projection/Camera.hpp | 12 +-- source/CMakeLists.txt | 3 - source/Matrix.cpp | 4 +- source/Vector2.cpp | 11 --- source/Vector3.cpp | 23 ----- source/Vector4.cpp | 16 ---- source/collision/LineTracer.cpp | 8 +- source/engines/OpenGL/Camera.cpp | 4 +- source/engines/Source/Camera.cpp | 4 +- source/pathfinding/Astar.cpp | 16 ++-- source/pathfinding/NavigationMesh.cpp | 10 +-- .../ProjPredEngineAVX2.cpp | 4 +- .../ProjPredEngineLegacy.cpp | 6 +- source/projectile_prediction/Projectile.cpp | 2 +- tests/engines/UnitTestOpenGL.cpp | 2 +- tests/engines/UnitTestSourceEngine.cpp | 2 +- tests/general/UnitTestLineTrace.cpp | 10 +-- tests/general/UnitTestTriangle.cpp | 16 ++-- tests/general/UnitTestVector2.cpp | 6 +- tests/general/UnitTestVector3.cpp | 8 +- tests/general/UnitTestVector4.cpp | 6 +- 43 files changed, 224 insertions(+), 256 deletions(-) delete mode 100644 source/Vector2.cpp delete mode 100644 source/Vector3.cpp delete mode 100644 source/Vector4.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index aba995aa..b1631f66 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,10 +13,7 @@ option(OMATH_BUILD_AS_SHARED_LIBRARY "Build Omath as .so or .dll" OFF) if (OMATH_BUILD_AS_SHARED_LIBRARY) add_library(omath SHARED source/Vector3.cpp) else() - add_library(omath STATIC source/Vector3.cpp - include/omath/engines/OpenGL/Constants.hpp - include/omath/engines/OpenGL/Formulas.hpp - include/omath/engines/OpenGL/Camera.hpp) + add_library(omath STATIC source/Matrix.cpp) endif() set_target_properties(omath PROPERTIES diff --git a/include/omath/Color.hpp b/include/omath/Color.hpp index 642adcc3..54848fcf 100644 --- a/include/omath/Color.hpp +++ b/include/omath/Color.hpp @@ -18,10 +18,10 @@ namespace omath }; - class Color final : public Vector4 + class Color final : public Vector4 { public: - constexpr Color(float r, float g, float b, float a) : Vector4(r,g,b,a) + constexpr Color(const float r, const float g, const float b, const float a) : Vector4(r,g,b,a) { Clamp(0.f, 1.f); } diff --git a/include/omath/Mat.hpp b/include/omath/Mat.hpp index 3a23fbce..9d91ab53 100644 --- a/include/omath/Mat.hpp +++ b/include/omath/Mat.hpp @@ -333,21 +333,21 @@ namespace omath template [[nodiscard]] - constexpr static Mat<1, 4, Type, St> MatRowFromVector(const Vector3& vector) noexcept + constexpr static Mat<1, 4, Type, St> MatRowFromVector(const Vector3& vector) noexcept { return {{vector.x, vector.y, vector.z, 1}}; } template [[nodiscard]] - constexpr static Mat<4, 1, Type, St> MatColumnFromVector(const Vector3& vector) noexcept + constexpr static Mat<4, 1, Type, St> MatColumnFromVector(const Vector3& vector) noexcept { return {{vector.x}, {vector.y}, {vector.z}, {1}}; } template [[nodiscard]] - constexpr Mat<4, 4, Type, St> MatTranslation(const Vector3& diff) noexcept + constexpr Mat<4, 4, Type, St> MatTranslation(const Vector3& diff) noexcept { return { @@ -399,8 +399,8 @@ namespace omath template [[nodiscard]] - static Mat<4, 4, Type, St> MatCameraView(const Vector3& forward, const Vector3& right, const Vector3& up, - const Vector3& cameraOrigin) noexcept + static Mat<4, 4, Type, St> MatCameraView(const Vector3& forward, const Vector3& right, + const Vector3& up, const Vector3& cameraOrigin) noexcept { return Mat<4, 4, Type, St> { diff --git a/include/omath/Matrix.hpp b/include/omath/Matrix.hpp index 2f23e406..3043bc5a 100644 --- a/include/omath/Matrix.hpp +++ b/include/omath/Matrix.hpp @@ -2,10 +2,10 @@ #include #include #include +#include "Vector3.hpp" namespace omath { - class Vector3; class Matrix final { @@ -19,10 +19,10 @@ namespace omath static Matrix ToScreenMatrix(float screenWidth, float screenHeight); [[nodiscard]] - static Matrix TranslationMatrix(const Vector3& diff); + static Matrix TranslationMatrix(const Vector3& diff); [[nodiscard]] - static Matrix OrientationMatrix(const Vector3& forward, const Vector3& right, const Vector3& up); + static Matrix OrientationMatrix(const Vector3& forward, const Vector3& right, const Vector3& up); [[nodiscard]] static Matrix ProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far); diff --git a/include/omath/Triangle.hpp b/include/omath/Triangle.hpp index e9e36655..9779982a 100644 --- a/include/omath/Triangle.hpp +++ b/include/omath/Triangle.hpp @@ -16,12 +16,12 @@ namespace omath { } - Vector3 m_vertex1; - Vector3 m_vertex2; - Vector3 m_vertex3; + Vector3 m_vertex1; + Vector3 m_vertex2; + Vector3 m_vertex3; [[nodiscard]] - constexpr Vector3 CalculateNormal() const + constexpr Vector3 CalculateNormal() const { const auto b = SideBVector(); const auto a = SideAVector(); @@ -41,7 +41,7 @@ namespace omath } [[nodiscard]] - constexpr Vector3 SideAVector() const + constexpr Vector3 SideAVector() const { return m_vertex1 - m_vertex2; } @@ -61,12 +61,12 @@ namespace omath return std::abs(sideA*sideA + sideB*sideB - hypot*hypot) <= 0.0001f; } [[nodiscard]] - constexpr Vector3 SideBVector() const + constexpr Vector3 SideBVector() const { return m_vertex3 - m_vertex2; } [[nodiscard]] - constexpr Vector3 MidPoint() const + constexpr Vector3 MidPoint() const { return (m_vertex1 + m_vertex2 + m_vertex3) / 3; } diff --git a/include/omath/Vector2.hpp b/include/omath/Vector2.hpp index db028cd2..60d2ed56 100644 --- a/include/omath/Vector2.hpp +++ b/include/omath/Vector2.hpp @@ -8,16 +8,18 @@ namespace omath { + + template requires std::is_arithmetic_v class Vector2 { public: - float x = 0.f; - float y = 0.f; + Type x = static_cast(0); + Type y = static_cast(0); // Constructors constexpr Vector2() = default; - constexpr Vector2(const float x, const float y) : x(x), y(y) {} + constexpr Vector2(const Type& x, const Type& y) : x(x), y(y) {} // Equality operators [[nodiscard]] @@ -65,7 +67,7 @@ namespace omath return *this; } - constexpr Vector2& operator*=(const float fl) + constexpr Vector2& operator*=(const Type& fl) { x *= fl; y *= fl; @@ -73,7 +75,7 @@ namespace omath return *this; } - constexpr Vector2& operator/=(const float fl) + constexpr Vector2& operator/=(const Type& fl) { x /= fl; y /= fl; @@ -81,7 +83,7 @@ namespace omath return *this; } - constexpr Vector2& operator+=(const float fl) + constexpr Vector2& operator+=(const Type& fl) { x += fl; y += fl; @@ -89,7 +91,7 @@ namespace omath return *this; } - constexpr Vector2& operator-=(const float fl) + constexpr Vector2& operator-=(const Type& fl) { x -= fl; y -= fl; @@ -98,45 +100,45 @@ namespace omath } // Basic vector operations - [[nodiscard]] float DistTo(const Vector2& vOther) const + [[nodiscard]] Type DistTo(const Vector2& vOther) const { return std::sqrt(DistToSqr(vOther)); } - [[nodiscard]] constexpr float DistToSqr(const Vector2& vOther) const + [[nodiscard]] constexpr Type DistToSqr(const Vector2& vOther) const { return (x - vOther.x) * (x - vOther.x) + (y - vOther.y) * (y - vOther.y); } - [[nodiscard]] constexpr float Dot(const Vector2& vOther) const + [[nodiscard]] constexpr Type Dot(const Vector2& vOther) const { return x * vOther.x + y * vOther.y; } #ifndef _MSC_VER - [[nodiscard]] constexpr float Length() const + [[nodiscard]] constexpr Type& Length() const { return std::hypot(x, y); } [[nodiscard]] constexpr Vector2 Normalized() const { - const float len = Length(); + const Type len = Length(); return len > 0.f ? *this / len : *this; } #else - [[nodiscard]] float Length() const + [[nodiscard]] Type Length() const { return std::hypot(x, y); } [[nodiscard]] Vector2 Normalized() const { - const float len = Length(); + const Type len = Length(); return len > 0.f ? *this / len : *this; } #endif - [[nodiscard]] constexpr float LengthSqr() const + [[nodiscard]] constexpr Type LengthSqr() const { return x * x + y * y; } @@ -187,13 +189,13 @@ namespace omath } // Sum of elements - [[nodiscard]] constexpr float Sum() const + [[nodiscard]] constexpr Type Sum() const { return x + y; } [[nodiscard]] - constexpr std::tuple AsTuple() const + constexpr std::tuple AsTuple() const { return std::make_tuple(x, y); } diff --git a/include/omath/Vector3.hpp b/include/omath/Vector3.hpp index 88dce1c7..9c2e9c3f 100644 --- a/include/omath/Vector3.hpp +++ b/include/omath/Vector3.hpp @@ -20,16 +20,17 @@ namespace omath IMPOSSIBLE_BETWEEN_ANGLE, }; - class Vector3 : public Vector2 + template requires std::is_arithmetic_v + class Vector3 : public Vector2 { public: - float z = 0.f; - constexpr Vector3(const float x, const float y, const float z) : Vector2(x, y), z(z) { } - constexpr Vector3() : Vector2() {}; + Type z = 0.f; + constexpr Vector3(const Type& x, const Type& y, const Type& z) : Vector2(x, y), z(z) { } + constexpr Vector3() : Vector2() {}; [[nodiscard]] constexpr bool operator==(const Vector3& src) const { - return Vector2::operator==(src) && (src.z == z); + return Vector2::operator==(src) && (src.z == z); } [[nodiscard]] constexpr bool operator!=(const Vector3& src) const @@ -39,7 +40,7 @@ namespace omath constexpr Vector3& operator+=(const Vector3& v) { - Vector2::operator+=(v); + Vector2::operator+=(v); z += v.z; return *this; @@ -47,7 +48,7 @@ namespace omath constexpr Vector3& operator-=(const Vector3& v) { - Vector2::operator-=(v); + Vector2::operator-=(v); z -= v.z; return *this; @@ -55,7 +56,7 @@ namespace omath constexpr Vector3& operator*=(const float fl) { - Vector2::operator*=(fl); + Vector2::operator*=(fl); z *= fl; return *this; @@ -63,7 +64,7 @@ namespace omath constexpr Vector3& operator*=(const Vector3& v) { - Vector2::operator*=(v); + Vector2::operator*=(v); z *= v.z; return *this; @@ -71,7 +72,7 @@ namespace omath constexpr Vector3& operator/=(const Vector3& v) { - Vector2::operator/=(v); + Vector2::operator/=(v); z /= v.z; return *this; @@ -79,7 +80,7 @@ namespace omath constexpr Vector3& operator+=(const float fl) { - Vector2::operator+=(fl); + Vector2::operator+=(fl); z += fl; return *this; @@ -87,7 +88,7 @@ namespace omath constexpr Vector3& operator/=(const float fl) { - Vector2::operator/=(fl); + Vector2::operator/=(fl); z /= fl; return *this; @@ -95,7 +96,7 @@ namespace omath constexpr Vector3& operator-=(const float fl) { - Vector2::operator-=(fl); + Vector2::operator-=(fl); z -= fl; return *this; @@ -103,7 +104,7 @@ namespace omath constexpr Vector3& Abs() { - Vector2::Abs(); + Vector2::Abs(); z = z < 0.f ? -z : z; return *this; @@ -116,7 +117,7 @@ namespace omath [[nodiscard]] constexpr float Dot(const Vector3& vOther) const { - return Vector2::Dot(vOther) + z * vOther.z; + return Vector2::Dot(vOther) + z * vOther.z; } #ifndef _MSC_VER @@ -142,7 +143,7 @@ namespace omath #else [[nodiscard]] float Length() const { - return std::hypot(x, y, z); + return std::hypot(this->x, this->y, z); } [[nodiscard]] Vector3 Normalized() const @@ -152,9 +153,9 @@ namespace omath return length != 0 ? *this / length : *this; } - [[nodiscard]] float Length2D() const + [[nodiscard]] Type Length2D() const { - return Vector2::Length(); + return Vector2::Length(); } [[nodiscard]] float DistTo(const Vector3& vOther) const @@ -166,51 +167,52 @@ namespace omath [[nodiscard]] constexpr float LengthSqr() const { - return Vector2::LengthSqr() + z * z; + return Vector2::LengthSqr() + z * z; } [[nodiscard]] constexpr Vector3 operator-() const { - return {-x, -y, -z}; + + return Vector3{-this->x, -this->y, -z}; } [[nodiscard]] constexpr Vector3 operator+(const Vector3& v) const { - return {x + v.x, y + v.y, z + v.z}; + return {this->x + v.x, this->y + v.y, z + v.z}; } [[nodiscard]] constexpr Vector3 operator-(const Vector3& v) const { - return {x - v.x, y - v.y, z - v.z}; + return {this->x - v.x, this->y - v.y, z - v.z}; } [[nodiscard]] constexpr Vector3 operator*(const float fl) const { - return {x * fl, y * fl, z * fl}; + return {this->x * fl, this->y * fl, z * fl}; } [[nodiscard]] constexpr Vector3 operator*(const Vector3& v) const { - return {x * v.x, y * v.y, z * v.z}; + return {this->x * v.x, this->y * v.y, z * v.z}; } [[nodiscard]] constexpr Vector3 operator/(const float fl) const { - return {x / fl, y / fl, z / fl}; + return {this->x / fl, this->y / fl, z / fl}; } [[nodiscard]] constexpr Vector3 operator/(const Vector3& v) const { - return {x / v.x, y / v.y, z / v.z}; + return {this->x / v.x, this->y / v.y, z / v.z}; } [[nodiscard]] constexpr Vector3 Cross(const Vector3 &v) const { return { - y * v.z - z * v.y, - z * v.x - x * v.z, - x * v.y - y * v.x + this->y * v.z - z * v.y, + z * v.x - this->x * v.z, + this->x * v.y - this->y * v.x }; } [[nodiscard]] constexpr float Sum() const @@ -239,14 +241,25 @@ namespace omath [[nodiscard]] constexpr float Sum2D() const { - return Vector2::Sum(); + return Vector2::Sum(); } - [[nodiscard]] Vector3 ViewAngleTo(const Vector3& other) const; - [[nodiscard]] constexpr std::tuple AsTuple() const { - return std::make_tuple(x, y, z); + return std::make_tuple(this->x, this->y, z); + } + + [[nodiscard]] Vector3 ViewAngleTo(const Vector3 &other) const + { + const float distance = DistTo(other); + const auto delta = other - *this; + + return + { + angles::RadiansToDegrees(std::asin(delta.z / distance)), + angles::RadiansToDegrees(std::atan2(delta.y, delta.x)), + 0 + }; } }; } @@ -254,9 +267,9 @@ namespace omath namespace std { template<> - struct hash + struct hash> { - std::size_t operator()(const omath::Vector3& vec) const noexcept + std::size_t operator()(const omath::Vector3& vec) const noexcept { std::size_t hash = 0; constexpr std::hash hasher; diff --git a/include/omath/Vector4.hpp b/include/omath/Vector4.hpp index c70e2d3b..05530ee1 100644 --- a/include/omath/Vector4.hpp +++ b/include/omath/Vector4.hpp @@ -8,18 +8,19 @@ namespace omath { - class Vector4 : public Vector3 + template + class Vector4 : public Vector3 { public: - float w; + Type w; - constexpr Vector4(const float x, const float y, const float z, const float w) : Vector3(x, y, z), w(w) {} - constexpr Vector4() : Vector3(), w(0.f) {}; + constexpr Vector4(const Type& x, const Type& y, const Type& z, const Type& w) : Vector3(x, y, z), w(w) {} + constexpr Vector4() : Vector3(), w(0) {}; [[nodiscard]] constexpr bool operator==(const Vector4& src) const { - return Vector3::operator==(src) && w == src.w; + return Vector3::operator==(src) && w == src.w; } [[nodiscard]] @@ -30,7 +31,7 @@ namespace omath constexpr Vector4& operator+=(const Vector4& v) { - Vector3::operator+=(v); + Vector3::operator+=(v); w += v.w; return *this; @@ -38,7 +39,7 @@ namespace omath constexpr Vector4& operator-=(const Vector4& v) { - Vector3::operator-=(v); + Vector3::operator-=(v); w -= v.w; return *this; @@ -46,7 +47,7 @@ namespace omath constexpr Vector4& operator*=(const float scalar) { - Vector3::operator*=(scalar); + Vector3::operator*=(scalar); w *= scalar; return *this; @@ -54,7 +55,7 @@ namespace omath constexpr Vector4& operator*=(const Vector4& v) { - Vector3::operator*=(v); + Vector3::operator*=(v); w *= v.w; return *this; @@ -62,7 +63,7 @@ namespace omath constexpr Vector4& operator/=(const float scalar) { - Vector3::operator/=(scalar); + Vector3::operator/=(scalar); w /= scalar; return *this; @@ -70,35 +71,38 @@ namespace omath constexpr Vector4& operator/=(const Vector4& v) { - Vector3::operator/=(v); + Vector3::operator/=(v); w /= v.w; return *this; } - [[nodiscard]] constexpr float LengthSqr() const + [[nodiscard]] constexpr Type LengthSqr() const { - return Vector3::LengthSqr() + w * w; + return Vector3::LengthSqr() + w * w; } [[nodiscard]] constexpr float Dot(const Vector4& vOther) const { - return Vector3::Dot(vOther) + w * vOther.w; + return Vector3::Dot(vOther) + w * vOther.w; } - [[nodiscard]] float Length() const; + [[nodiscard]] Vector3 Length() const + { + return std::sqrt(LengthSqr()); + } constexpr Vector4& Abs() { - Vector3::Abs(); + Vector3::Abs(); w = w < 0.f ? -w : w; return *this; } constexpr Vector4& Clamp(const float min, const float max) { - x = std::clamp(x, min, max); - y = std::clamp(y, min, max); - z = std::clamp(z, min, max); + this->x = std::clamp(this->x, min, max); + this->y = std::clamp(this->y, min, max); + this->z = std::clamp(this->z, min, max); return *this; } @@ -106,49 +110,49 @@ namespace omath [[nodiscard]] constexpr Vector4 operator-() const { - return {-x, -y, -z, -w}; + return {-this->x, -this->y, -this->z, -w}; } [[nodiscard]] constexpr Vector4 operator+(const Vector4& v) const { - return {x + v.x, y + v.y, z + v.z, w + v.w}; + return {this->x + v.x, this->y + v.y, this->z + v.z, w + v.w}; } [[nodiscard]] constexpr Vector4 operator-(const Vector4& v) const { - return {x - v.x, y - v.y, z - v.z, w - v.w}; + return {this->x - v.x, this->y - v.y, this->z - v.z, w - v.w}; } [[nodiscard]] constexpr Vector4 operator*(const float scalar) const { - return {x * scalar, y * scalar, z * scalar, w * scalar}; + return {this->x * scalar, this->y * scalar, this->z * scalar, w * scalar}; } [[nodiscard]] constexpr Vector4 operator*(const Vector4& v) const { - return {x * v.x, y * v.y, z * v.z, w * v.w}; + return {this->x * v.x, this->y * v.y, this->z * v.z, w * v.w}; } [[nodiscard]] constexpr Vector4 operator/(const float scalar) const { - return {x / scalar, y / scalar, z / scalar, w / scalar}; + return {this->x / scalar, this->y / scalar, this->z / scalar, w / scalar}; } [[nodiscard]] constexpr Vector4 operator/(const Vector4& v) const { - return {x / v.x, y / v.y, z / v.z, w / v.w}; + return {this->x / v.x, this->y / v.y, this->z / v.z, w / v.w}; } [[nodiscard]] - constexpr float Sum() const + constexpr Type Sum() const { - return Vector3::Sum() + w; + return Vector3::Sum() + w; } }; } diff --git a/include/omath/collision/LineTracer.hpp b/include/omath/collision/LineTracer.hpp index 33b81a24..e8c0b15b 100644 --- a/include/omath/collision/LineTracer.hpp +++ b/include/omath/collision/LineTracer.hpp @@ -11,14 +11,14 @@ namespace omath::collision class Ray { public: - Vector3 start; - Vector3 end; + Vector3 start; + Vector3 end; [[nodiscard]] - Vector3 DirectionVector() const; + Vector3 DirectionVector() const; [[nodiscard]] - Vector3 DirectionVectorNormalized() const; + Vector3 DirectionVectorNormalized() const; }; class LineTracer { @@ -27,12 +27,12 @@ namespace omath::collision [[nodiscard]] - static bool CanTraceLine(const Ray& ray, const Triangle& triangle); + static bool CanTraceLine(const Ray& ray, const Triangle>& triangle); // Realization of Möller–Trumbore intersection algorithm // https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm [[nodiscard]] - static Vector3 GetRayHitPoint(const Ray& ray, const Triangle& triangle); + static Vector3 GetRayHitPoint(const Ray& ray, const Triangle>& triangle); }; } diff --git a/include/omath/engines/OpenGL/Camera.hpp b/include/omath/engines/OpenGL/Camera.hpp index 0ad97291..0f3e7fbb 100644 --- a/include/omath/engines/OpenGL/Camera.hpp +++ b/include/omath/engines/OpenGL/Camera.hpp @@ -10,9 +10,9 @@ namespace omath::opengl class Camera final : public projection::Camera { public: - Camera(const Vector3& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort, + Camera(const Vector3& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort, const Angle& fov, float near, float far); - void LookAt(const Vector3& target) override; + void LookAt(const Vector3& target) override; [[nodiscard]] Mat4x4 CalcViewMatrix() const override; [[nodiscard]] Mat4x4 CalcProjectionMatrix() const override; }; diff --git a/include/omath/engines/OpenGL/Constants.hpp b/include/omath/engines/OpenGL/Constants.hpp index a8912a21..6cedf43f 100644 --- a/include/omath/engines/OpenGL/Constants.hpp +++ b/include/omath/engines/OpenGL/Constants.hpp @@ -10,9 +10,9 @@ namespace omath::opengl { - constexpr Vector3 kAbsUp = {0, 1, 0}; - constexpr Vector3 kAbsRight = {1, 0, 0}; - constexpr Vector3 kAbsForward = {0, 0, -1}; + constexpr Vector3 kAbsUp = {0, 1, 0}; + constexpr Vector3 kAbsRight = {1, 0, 0}; + constexpr Vector3 kAbsForward = {0, 0, -1}; using Mat4x4 = Mat<4, 4, float, MatStoreType::COLUMN_MAJOR>; using Mat3x3 = Mat<4, 4, float, MatStoreType::COLUMN_MAJOR>; diff --git a/include/omath/engines/OpenGL/Formulas.hpp b/include/omath/engines/OpenGL/Formulas.hpp index 8e6b2edb..c551619c 100644 --- a/include/omath/engines/OpenGL/Formulas.hpp +++ b/include/omath/engines/OpenGL/Formulas.hpp @@ -8,7 +8,7 @@ namespace omath::opengl { [[nodiscard]] - inline Vector3 ForwardVector(const ViewAngles& angles) + inline Vector3 ForwardVector(const ViewAngles& angles) { const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsForward); @@ -16,7 +16,7 @@ namespace omath::opengl } [[nodiscard]] - inline Vector3 RightVector(const ViewAngles& angles) + inline Vector3 RightVector(const ViewAngles& angles) { const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsRight); @@ -24,14 +24,14 @@ namespace omath::opengl } [[nodiscard]] - inline Vector3 UpVector(const ViewAngles& angles) + inline Vector3 UpVector(const ViewAngles& angles) { const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsUp); return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)}; } - [[nodiscard]] inline Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3& cam_origin) + [[nodiscard]] inline Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3& cam_origin) { return MatCameraView(-ForwardVector(angles), RightVector(angles), UpVector(angles), cam_origin); } diff --git a/include/omath/engines/Source/Camera.hpp b/include/omath/engines/Source/Camera.hpp index 739aa861..ef895baf 100644 --- a/include/omath/engines/Source/Camera.hpp +++ b/include/omath/engines/Source/Camera.hpp @@ -10,9 +10,9 @@ namespace omath::source class Camera final : public projection::Camera { public: - Camera(const Vector3& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort, + Camera(const Vector3& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort, const Angle& fov, float near, float far); - void LookAt(const Vector3& target) override; + void LookAt(const Vector3& target) override; [[nodiscard]] Mat4x4 CalcViewMatrix() const override; [[nodiscard]] Mat4x4 CalcProjectionMatrix() const override; }; diff --git a/include/omath/engines/Source/Constants.hpp b/include/omath/engines/Source/Constants.hpp index ecf1b400..c0a7b017 100644 --- a/include/omath/engines/Source/Constants.hpp +++ b/include/omath/engines/Source/Constants.hpp @@ -9,9 +9,9 @@ #include namespace omath::source { - constexpr Vector3 kAbsUp = {0, 0, 1}; - constexpr Vector3 kAbsRight = {0, -1, 0}; - constexpr Vector3 kAbsForward = {1, 0, 0}; + constexpr Vector3 kAbsUp = {0, 0, 1}; + constexpr Vector3 kAbsRight = {0, -1, 0}; + constexpr Vector3 kAbsForward = {1, 0, 0}; using Mat4x4 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>; using Mat3x3 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>; diff --git a/include/omath/engines/Source/Formulas.hpp b/include/omath/engines/Source/Formulas.hpp index 0b8ff039..49b9166b 100644 --- a/include/omath/engines/Source/Formulas.hpp +++ b/include/omath/engines/Source/Formulas.hpp @@ -7,7 +7,7 @@ namespace omath::source { [[nodiscard]] - inline Vector3 ForwardVector(const ViewAngles& angles) + inline Vector3 ForwardVector(const ViewAngles& angles) { const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsForward); @@ -15,7 +15,7 @@ namespace omath::source } [[nodiscard]] - inline Vector3 RightVector(const ViewAngles& angles) + inline Vector3 RightVector(const ViewAngles& angles) { const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsRight); @@ -23,14 +23,14 @@ namespace omath::source } [[nodiscard]] - inline Vector3 UpVector(const ViewAngles& angles) + inline Vector3 UpVector(const ViewAngles& angles) { const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsUp); return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)}; } - [[nodiscard]] inline Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3& cam_origin) + [[nodiscard]] inline Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3& cam_origin) { return MatCameraView(ForwardVector(angles), RightVector(angles), UpVector(angles), cam_origin); } diff --git a/include/omath/pathfinding/Astar.hpp b/include/omath/pathfinding/Astar.hpp index 31aa127b..9cbcedbb 100644 --- a/include/omath/pathfinding/Astar.hpp +++ b/include/omath/pathfinding/Astar.hpp @@ -14,12 +14,17 @@ namespace omath::pathfinding { public: [[nodiscard]] - static std::vector FindPath(const Vector3& start, const Vector3& end, const NavigationMesh& navMesh); + static std::vector> FindPath(const Vector3& start, const Vector3& end, + const NavigationMesh& navMesh); + private: [[nodiscard]] - static std::vector ReconstructFinalPath(const std::unordered_map& closedList, const Vector3& current); + static std::vector> + ReconstructFinalPath(const std::unordered_map, PathNode>& closedList, + const Vector3& current); [[nodiscard]] - static auto GetPerfectNode(const std::unordered_map& openList, const Vector3& endVertex); + static auto GetPerfectNode(const std::unordered_map, PathNode>& openList, + const Vector3& endVertex); }; -} \ No newline at end of file +} // namespace omath::pathfinding diff --git a/include/omath/pathfinding/NavigationMesh.hpp b/include/omath/pathfinding/NavigationMesh.hpp index 5fc34d2c..785f619e 100644 --- a/include/omath/pathfinding/NavigationMesh.hpp +++ b/include/omath/pathfinding/NavigationMesh.hpp @@ -22,17 +22,17 @@ namespace omath::pathfinding public: [[nodiscard]] - std::expected GetClosestVertex(const Vector3& point) const; + std::expected, std::string> GetClosestVertex(const Vector3& point) const; [[nodiscard]] - const std::vector& GetNeighbors(const Vector3& vertex) const; + const std::vector>& GetNeighbors(const Vector3& vertex) const; [[nodiscard]] bool Empty() const; [[nodiscard]] std::vector Serialize() const; void Deserialize(const std::vector& raw); - std::unordered_map> m_verTextMap; + std::unordered_map, std::vector>> m_verTextMap; }; } \ No newline at end of file diff --git a/include/omath/projectile_prediction/ProjPredEngine.hpp b/include/omath/projectile_prediction/ProjPredEngine.hpp index 9938db72..83adde05 100644 --- a/include/omath/projectile_prediction/ProjPredEngine.hpp +++ b/include/omath/projectile_prediction/ProjPredEngine.hpp @@ -13,7 +13,7 @@ namespace omath::projectile_prediction { public: [[nodiscard]] - virtual std::optional MaybeCalculateAimPoint(const Projectile& projectile, + virtual std::optional> MaybeCalculateAimPoint(const Projectile& projectile, const Target& target) const = 0; virtual ~ProjPredEngine() = default; }; diff --git a/include/omath/projectile_prediction/ProjPredEngineAVX2.hpp b/include/omath/projectile_prediction/ProjPredEngineAVX2.hpp index 0c0f5f35..ffc0c5ed 100644 --- a/include/omath/projectile_prediction/ProjPredEngineAVX2.hpp +++ b/include/omath/projectile_prediction/ProjPredEngineAVX2.hpp @@ -9,7 +9,7 @@ namespace omath::projectile_prediction class ProjPredEngineAVX2 final : public ProjPredEngine { public: - [[nodiscard]] std::optional MaybeCalculateAimPoint(const Projectile& projectile, + [[nodiscard]] std::optional> MaybeCalculateAimPoint(const Projectile& projectile, const Target& target) const override; @@ -17,7 +17,7 @@ namespace omath::projectile_prediction ~ProjPredEngineAVX2() override = default; private: - [[nodiscard]] static std::optional CalculatePitch(const Vector3& projOrigin, const Vector3& targetPos, + [[nodiscard]] static std::optional CalculatePitch(const Vector3& projOrigin, const Vector3& targetPos, float bulletGravity, float v0, float time); const float m_gravityConstant; const float m_simulationTimeStep; diff --git a/include/omath/projectile_prediction/ProjPredEngineLegacy.hpp b/include/omath/projectile_prediction/ProjPredEngineLegacy.hpp index 6c9a9e85..f22308d6 100644 --- a/include/omath/projectile_prediction/ProjPredEngineLegacy.hpp +++ b/include/omath/projectile_prediction/ProjPredEngineLegacy.hpp @@ -20,7 +20,7 @@ namespace omath::projectile_prediction float distanceTolerance); [[nodiscard]] - std::optional MaybeCalculateAimPoint(const Projectile& projectile, + std::optional> MaybeCalculateAimPoint(const Projectile& projectile, const Target& target) const override; private: @@ -31,11 +31,11 @@ namespace omath::projectile_prediction [[nodiscard]] std::optional MaybeCalculateProjectileLaunchPitchAngle(const Projectile& projectile, - const Vector3& targetPosition) const; + const Vector3& targetPosition) const; [[nodiscard]] - bool IsProjectileReachedTarget(const Vector3& targetPosition, const Projectile& projectile, float pitch, + bool IsProjectileReachedTarget(const Vector3& targetPosition, const Projectile& projectile, float pitch, float time) const; }; } // namespace omath::projectile_prediction diff --git a/include/omath/projectile_prediction/Projectile.hpp b/include/omath/projectile_prediction/Projectile.hpp index 4292100d..c2e0f7bc 100644 --- a/include/omath/projectile_prediction/Projectile.hpp +++ b/include/omath/projectile_prediction/Projectile.hpp @@ -12,9 +12,9 @@ namespace omath::projectile_prediction public: [[nodiscard]] - Vector3 PredictPosition(float pitch, float yaw, float time, float gravity) const; + Vector3 PredictPosition(float pitch, float yaw, float time, float gravity) const; - Vector3 m_origin; + Vector3 m_origin; float m_launchSpeed{}; float m_gravityScale{}; }; diff --git a/include/omath/projectile_prediction/Target.hpp b/include/omath/projectile_prediction/Target.hpp index 7dfed42e..a5e4a12b 100644 --- a/include/omath/projectile_prediction/Target.hpp +++ b/include/omath/projectile_prediction/Target.hpp @@ -12,7 +12,7 @@ namespace omath::projectile_prediction public: [[nodiscard]] - constexpr Vector3 PredictPosition(const float time, const float gravity) const + constexpr Vector3 PredictPosition(const float time, const float gravity) const { auto predicted = m_origin + m_velocity * time; @@ -22,8 +22,8 @@ namespace omath::projectile_prediction return predicted; } - Vector3 m_origin; - Vector3 m_velocity; + Vector3 m_origin; + Vector3 m_velocity; bool m_isAirborne{}; }; } \ No newline at end of file diff --git a/include/omath/projection/Camera.hpp b/include/omath/projection/Camera.hpp index e722d768..4d967374 100644 --- a/include/omath/projection/Camera.hpp +++ b/include/omath/projection/Camera.hpp @@ -31,7 +31,7 @@ namespace omath::projection { public: virtual ~Camera() = default; - Camera(const Vector3& position, const ViewAnglesType& viewAngles, const ViewPort& viewPort, + Camera(const Vector3& position, const ViewAnglesType& viewAngles, const ViewPort& viewPort, const FieldOfView& fov, const float near, const float far) : m_viewPort(viewPort), m_fieldOfView(fov), m_farPlaneDistance(far), m_nearPlaneDistance(near), m_viewAngles(viewAngles), m_origin(position) @@ -39,7 +39,7 @@ namespace omath::projection } - virtual void LookAt(const Vector3& target) = 0; + virtual void LookAt(const Vector3& target) = 0; [[nodiscard]] virtual Mat4x4Type CalcViewMatrix() const = 0; @@ -74,7 +74,7 @@ namespace omath::projection m_viewProjectionMatrix = CalcViewProjectionMatrix(); } - void SetOrigin(const Vector3& origin) + void SetOrigin(const Vector3& origin) { m_origin = origin; m_viewProjectionMatrix = CalcViewProjectionMatrix(); @@ -106,12 +106,12 @@ namespace omath::projection return m_viewAngles; } - [[nodiscard]] const Vector3& GetOrigin() const + [[nodiscard]] const Vector3& GetOrigin() const { return m_origin; } - [[nodiscard]] std::expected WorldToScreen(const Vector3& worldPosition) const + [[nodiscard]] std::expected, Error> WorldToScreen(const Vector3& worldPosition) const { if (!m_viewProjectionMatrix.has_value()) m_viewProjectionMatrix = CalcViewProjectionMatrix(); @@ -143,7 +143,7 @@ namespace omath::projection ViewAnglesType m_viewAngles; - Vector3 m_origin; + Vector3 m_origin; private: template diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index b7a84afe..10ca8ed2 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -1,9 +1,6 @@ target_sources(omath PRIVATE - Vector3.cpp Matrix.cpp color.cpp - Vector4.cpp - Vector2.cpp ) add_subdirectory(projectile_prediction) diff --git a/source/Matrix.cpp b/source/Matrix.cpp index f976f415..98bea2c3 100644 --- a/source/Matrix.cpp +++ b/source/Matrix.cpp @@ -315,7 +315,7 @@ namespace omath }; } - Matrix Matrix::TranslationMatrix(const Vector3& diff) + Matrix Matrix::TranslationMatrix(const Vector3& diff) { return { {1.f, 0.f, 0.f, 0.f}, @@ -325,7 +325,7 @@ namespace omath }; } - Matrix Matrix::OrientationMatrix(const Vector3& forward, const Vector3& right, const Vector3& up) + Matrix Matrix::OrientationMatrix(const Vector3& forward, const Vector3& right, const Vector3& up) { return { {right.x, up.x, forward.x, 0.f}, diff --git a/source/Vector2.cpp b/source/Vector2.cpp deleted file mode 100644 index 58188ace..00000000 --- a/source/Vector2.cpp +++ /dev/null @@ -1,11 +0,0 @@ -// -// Created by Vlad on 02.09.2024. -// -#include "omath/Vector2.hpp" -#include - - -namespace omath -{ - -} \ No newline at end of file diff --git a/source/Vector3.cpp b/source/Vector3.cpp deleted file mode 100644 index 45b9f1e9..00000000 --- a/source/Vector3.cpp +++ /dev/null @@ -1,23 +0,0 @@ -// -// Created by vlad on 10/28/23. -// - -#include -#include -#include - -namespace omath -{ - Vector3 Vector3::ViewAngleTo(const Vector3 &other) const - { - const float distance = DistTo(other); - const auto delta = other - *this; - - return - { - angles::RadiansToDegrees(std::asin(delta.z / distance)), - angles::RadiansToDegrees(std::atan2(delta.y, delta.x)), - 0.f - }; - } -} \ No newline at end of file diff --git a/source/Vector4.cpp b/source/Vector4.cpp deleted file mode 100644 index a995337c..00000000 --- a/source/Vector4.cpp +++ /dev/null @@ -1,16 +0,0 @@ -// -// Vector4.cpp -// - -#include "omath/Vector4.hpp" -#include - - -namespace omath -{ - - float Vector4::Length() const - { - return std::sqrt(LengthSqr()); - } -} diff --git a/source/collision/LineTracer.cpp b/source/collision/LineTracer.cpp index 5f8c5b60..05f5bef7 100644 --- a/source/collision/LineTracer.cpp +++ b/source/collision/LineTracer.cpp @@ -5,21 +5,21 @@ namespace omath::collision { - bool LineTracer::CanTraceLine(const Ray& ray, const Triangle& triangle) + bool LineTracer::CanTraceLine(const Ray& ray, const Triangle>& triangle) { return GetRayHitPoint(ray, triangle) == ray.end; } - Vector3 Ray::DirectionVector() const + Vector3 Ray::DirectionVector() const { return end - start; } - Vector3 Ray::DirectionVectorNormalized() const + Vector3 Ray::DirectionVectorNormalized() const { return DirectionVector().Normalized(); } - Vector3 LineTracer::GetRayHitPoint(const Ray& ray, const Triangle& triangle) + Vector3 LineTracer::GetRayHitPoint(const Ray& ray, const Triangle>& triangle) { constexpr float kEpsilon = std::numeric_limits::epsilon(); diff --git a/source/engines/OpenGL/Camera.cpp b/source/engines/OpenGL/Camera.cpp index 11dd30c3..17e12aa0 100644 --- a/source/engines/OpenGL/Camera.cpp +++ b/source/engines/OpenGL/Camera.cpp @@ -8,12 +8,12 @@ namespace omath::opengl { - Camera::Camera(const Vector3& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort, + Camera::Camera(const Vector3& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort, const Angle& fov, const float near, const float far) : projection::Camera(position, viewAngles, viewPort, fov, near, far) { } - void Camera::LookAt([[maybe_unused]] const Vector3& target) + void Camera::LookAt([[maybe_unused]] const Vector3& target) { const float distance = m_origin.DistTo(target); const auto delta = target - m_origin; diff --git a/source/engines/Source/Camera.cpp b/source/engines/Source/Camera.cpp index 444e208a..1f00af55 100644 --- a/source/engines/Source/Camera.cpp +++ b/source/engines/Source/Camera.cpp @@ -8,12 +8,12 @@ namespace omath::source { - Camera::Camera(const Vector3& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort, + Camera::Camera(const Vector3& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort, const projection::FieldOfView& fov, const float near, const float far) : projection::Camera(position, viewAngles, viewPort, fov, near, far) { } - void Camera::LookAt(const Vector3& target) + void Camera::LookAt(const Vector3& target) { const float distance = m_origin.DistTo(target); const auto delta = target - m_origin; diff --git a/source/pathfinding/Astar.cpp b/source/pathfinding/Astar.cpp index 1ea69a6f..4d553e72 100644 --- a/source/pathfinding/Astar.cpp +++ b/source/pathfinding/Astar.cpp @@ -13,15 +13,15 @@ namespace omath::pathfinding { struct PathNode final { - std::optional cameFrom; + std::optional> cameFrom; float gCost = 0.f; }; - std::vector Astar::ReconstructFinalPath(const std::unordered_map& closedList, - const Vector3& current) + std::vector> Astar::ReconstructFinalPath(const std::unordered_map, PathNode>& closedList, + const Vector3& current) { - std::vector path; + std::vector> path; std::optional currentOpt = current; while (currentOpt) @@ -39,7 +39,7 @@ namespace omath::pathfinding std::ranges::reverse(path); return path; } - auto Astar::GetPerfectNode(const std::unordered_map& openList, const Vector3& endVertex) + auto Astar::GetPerfectNode(const std::unordered_map, PathNode>& openList, const Vector3& endVertex) { return std::ranges::min_element(openList, [&endVertex](const auto& a, const auto& b) @@ -50,10 +50,10 @@ namespace omath::pathfinding }); } - std::vector Astar::FindPath(const Vector3& start, const Vector3& end, const NavigationMesh& navMesh) + std::vector> Astar::FindPath(const Vector3& start, const Vector3& end, const NavigationMesh& navMesh) { - std::unordered_map closedList; - std::unordered_map openList; + std::unordered_map, PathNode> closedList; + std::unordered_map, PathNode> openList; auto maybeStartVertex = navMesh.GetClosestVertex(start); auto maybeEndVertex = navMesh.GetClosestVertex(end); diff --git a/source/pathfinding/NavigationMesh.cpp b/source/pathfinding/NavigationMesh.cpp index 87b9db26..e5491a3a 100644 --- a/source/pathfinding/NavigationMesh.cpp +++ b/source/pathfinding/NavigationMesh.cpp @@ -7,7 +7,7 @@ #include namespace omath::pathfinding { - std::expected NavigationMesh::GetClosestVertex(const Vector3 &point) const + std::expected, std::string> NavigationMesh::GetClosestVertex(const Vector3 &point) const { const auto res = std::ranges::min_element(m_verTextMap, [&point](const auto& a, const auto& b) @@ -21,7 +21,7 @@ namespace omath::pathfinding return res->first; } - const std::vector& NavigationMesh::GetNeighbors(const Vector3 &vertex) const + const std::vector>& NavigationMesh::GetNeighbors(const Vector3 &vertex) const { return m_verTextMap.at(vertex); } @@ -73,18 +73,18 @@ namespace omath::pathfinding while (offset < raw.size()) { - Vector3 vertex; + Vector3 vertex; loadFromVector(raw, offset, vertex); uint16_t neighborsCount; loadFromVector(raw, offset, neighborsCount); - std::vector neighbors; + std::vector> neighbors; neighbors.reserve(neighborsCount); for (size_t i = 0; i < neighborsCount; ++i) { - Vector3 neighbor; + Vector3 neighbor; loadFromVector(raw, offset, neighbor); neighbors.push_back(neighbor); } diff --git a/source/projectile_prediction/ProjPredEngineAVX2.cpp b/source/projectile_prediction/ProjPredEngineAVX2.cpp index 9787e1a1..208d5738 100644 --- a/source/projectile_prediction/ProjPredEngineAVX2.cpp +++ b/source/projectile_prediction/ProjPredEngineAVX2.cpp @@ -6,7 +6,7 @@ namespace omath::projectile_prediction { - std::optional ProjPredEngineAVX2::MaybeCalculateAimPoint(const Projectile& projectile, + std::optional> ProjPredEngineAVX2::MaybeCalculateAimPoint(const Projectile& projectile, const Target& target) const { const float bulletGravity = m_gravityConstant * projectile.m_gravityScale; @@ -110,7 +110,7 @@ namespace omath::projectile_prediction m_maximumSimulationTime(simulationTimeStep) { } - std::optional ProjPredEngineAVX2::CalculatePitch(const Vector3& projOrigin, const Vector3& targetPos, + std::optional ProjPredEngineAVX2::CalculatePitch(const Vector3& projOrigin, const Vector3& targetPos, const float bulletGravity, const float v0, const float time) { if (time <= 0.0f) diff --git a/source/projectile_prediction/ProjPredEngineLegacy.cpp b/source/projectile_prediction/ProjPredEngineLegacy.cpp index 84c5e222..3a8c7652 100644 --- a/source/projectile_prediction/ProjPredEngineLegacy.cpp +++ b/source/projectile_prediction/ProjPredEngineLegacy.cpp @@ -11,7 +11,7 @@ namespace omath::projectile_prediction { } - std::optional ProjPredEngineLegacy::MaybeCalculateAimPoint(const Projectile& projectile, + std::optional> ProjPredEngineLegacy::MaybeCalculateAimPoint(const Projectile& projectile, const Target& target) const { for (float time = 0.f; time < m_maximumSimulationTime; time += m_simulationTimeStep) @@ -36,7 +36,7 @@ namespace omath::projectile_prediction std::optional ProjPredEngineLegacy::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; @@ -57,7 +57,7 @@ namespace omath::projectile_prediction return angles::RadiansToDegrees(angle); } - bool ProjPredEngineLegacy::IsProjectileReachedTarget(const Vector3& targetPosition, const Projectile& projectile, + bool ProjPredEngineLegacy::IsProjectileReachedTarget(const Vector3& targetPosition, const Projectile& projectile, const float pitch, const float time) const { const auto yaw = projectile.m_origin.ViewAngleTo(targetPosition).y; diff --git a/source/projectile_prediction/Projectile.cpp b/source/projectile_prediction/Projectile.cpp index c1ce1567..dcf4c26b 100644 --- a/source/projectile_prediction/Projectile.cpp +++ b/source/projectile_prediction/Projectile.cpp @@ -8,7 +8,7 @@ namespace omath::projectile_prediction { - Vector3 Projectile::PredictPosition(const float pitch, const float yaw, const float time, const float gravity) const + Vector3 Projectile::PredictPosition(const float pitch, const float yaw, const float time, const float gravity) const { auto currentPos = m_origin + source::ForwardVector({source::PitchAngle::FromDegrees(-pitch), source::YawAngle::FromDegrees(yaw), diff --git a/tests/engines/UnitTestOpenGL.cpp b/tests/engines/UnitTestOpenGL.cpp index 461d9d24..db748e87 100644 --- a/tests/engines/UnitTestOpenGL.cpp +++ b/tests/engines/UnitTestOpenGL.cpp @@ -62,7 +62,7 @@ TEST(UnitTestOpenGL, CameraSetAndGetOrigin) { auto cam = omath::opengl::Camera({0, 0, 0}, {}, {1920.f, 1080.f}, {}, 0.01f, 1000.f); - EXPECT_EQ(cam.GetOrigin(), omath::Vector3{}); + EXPECT_EQ(cam.GetOrigin(), omath::Vector3{}); cam.SetFieldOfView(omath::projection::FieldOfView::FromDegrees(50.f)); EXPECT_EQ(cam.GetFieldOfView().AsDegrees(), 50.f); diff --git a/tests/engines/UnitTestSourceEngine.cpp b/tests/engines/UnitTestSourceEngine.cpp index cb73a44d..876557ea 100644 --- a/tests/engines/UnitTestSourceEngine.cpp +++ b/tests/engines/UnitTestSourceEngine.cpp @@ -62,7 +62,7 @@ TEST(UnitTestSourceEngine, CameraSetAndGetOrigin) { auto cam = omath::source::Camera({0, 0, 0}, {}, {1920.f, 1080.f}, {}, 0.01f, 1000.f); - EXPECT_EQ(cam.GetOrigin(), omath::Vector3{}); + EXPECT_EQ(cam.GetOrigin(), omath::Vector3{}); cam.SetFieldOfView(omath::projection::FieldOfView::FromDegrees(50.f)); EXPECT_EQ(cam.GetFieldOfView().AsDegrees(), 50.f); diff --git a/tests/general/UnitTestLineTrace.cpp b/tests/general/UnitTestLineTrace.cpp index 4e37916d..3c0bf044 100644 --- a/tests/general/UnitTestLineTrace.cpp +++ b/tests/general/UnitTestLineTrace.cpp @@ -10,10 +10,10 @@ class LineTracerTest : public ::testing::Test { protected: // Set up common variables for use in each test - Vector3 vertex1{0.0f, 0.0f, 0.0f}; - Vector3 vertex2{1.0f, 0.0f, 0.0f}; - Vector3 vertex3{0.0f, 1.0f, 0.0f}; - Triangle triangle{vertex1, vertex2, vertex3}; + Vector3 vertex1{0.0f, 0.0f, 0.0f}; + Vector3 vertex2{1.0f, 0.0f, 0.0f}; + Vector3 vertex3{0.0f, 1.0f, 0.0f}; + Triangle> triangle{vertex1, vertex2, vertex3}; }; // Test that a ray intersecting the triangle returns false for CanTraceLine @@ -71,7 +71,7 @@ TEST_F(LineTracerTest, TriangleFarBeyondRayEndPoint) 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 distantTriangle{ + constexpr Triangle> distantTriangle{ {1000.0f, 1000.0f, 1000.0f}, {1001.0f, 1000.0f, 1000.0f}, {1000.0f, 1001.0f, 1000.0f} }; diff --git a/tests/general/UnitTestTriangle.cpp b/tests/general/UnitTestTriangle.cpp index 258cb972..8e352289 100644 --- a/tests/general/UnitTestTriangle.cpp +++ b/tests/general/UnitTestTriangle.cpp @@ -13,28 +13,28 @@ class UnitTestTriangle : public ::testing::Test { protected: // Define some Triangles to use in tests - Triangle t1; - Triangle t2; - Triangle t3; + Triangle> t1; + Triangle> t2; + Triangle> t3; constexpr void SetUp() override { // Triangle with vertices (0, 0, 0), (1, 0, 0), (0, 1, 0) - t1 = Triangle( + t1 = Triangle>( Vector3(0.0f, 0.0f, 0.0f), Vector3(1.0f, 0.0f, 0.0f), Vector3(0.0f, 1.0f, 0.0f) ); // Triangle with vertices (1, 2, 3), (4, 5, 6), (7, 8, 9) - t2 = Triangle( + t2 = Triangle>( Vector3(1.0f, 2.0f, 3.0f), Vector3(4.0f, 5.0f, 6.0f), Vector3(7.0f, 8.0f, 9.0f) ); // An isosceles right triangle - t3 = Triangle( + t3 = Triangle>( Vector3(0.0f, 0.0f, 0.0f), Vector3(2.0f, 0.0f, 0.0f), Vector3(0.0f, 2.0f, 0.0f) @@ -45,7 +45,7 @@ class UnitTestTriangle : public ::testing::Test // Test constructor and vertices TEST_F(UnitTestTriangle, Constructor) { - constexpr Triangle t( + constexpr Triangle> t( Vector3(1.0f, 2.0f, 3.0f), Vector3(4.0f, 5.0f, 6.0f), Vector3(7.0f, 8.0f, 9.0f) @@ -113,7 +113,7 @@ TEST_F(UnitTestTriangle, SideVectors) TEST_F(UnitTestTriangle, IsRectangular) { - EXPECT_TRUE(Triangle({2,0,0}, {}, {0,2,0}).IsRectangular()); + EXPECT_TRUE(Triangle>({2,0,0}, {}, {0,2,0}).IsRectangular()); } // Test midpoint TEST_F(UnitTestTriangle, MidPoint) diff --git a/tests/general/UnitTestVector2.cpp b/tests/general/UnitTestVector2.cpp index 0255363a..918115de 100644 --- a/tests/general/UnitTestVector2.cpp +++ b/tests/general/UnitTestVector2.cpp @@ -12,8 +12,8 @@ using namespace omath; class UnitTestVector2 : public ::testing::Test { protected: - Vector2 v1; - Vector2 v2; + Vector2 v1; + Vector2 v2; constexpr void SetUp() override { @@ -25,7 +25,7 @@ class UnitTestVector2 : public ::testing::Test // Test constructor and default values TEST_F(UnitTestVector2, Constructor_Default) { - constexpr Vector2 v; + constexpr Vector2 v; EXPECT_FLOAT_EQ(v.x, 0.0f); EXPECT_FLOAT_EQ(v.y, 0.0f); } diff --git a/tests/general/UnitTestVector3.cpp b/tests/general/UnitTestVector3.cpp index d7560a65..678cdb19 100644 --- a/tests/general/UnitTestVector3.cpp +++ b/tests/general/UnitTestVector3.cpp @@ -13,8 +13,8 @@ using namespace omath; class UnitTestVector3 : public ::testing::Test { protected: - Vector3 v1; - Vector3 v2; + Vector3 v1; + Vector3 v2; void SetUp() override { @@ -26,7 +26,7 @@ class UnitTestVector3 : public ::testing::Test // Test constructor and default values TEST_F(UnitTestVector3, Constructor_Default) { - constexpr Vector3 v; + constexpr Vector3 v; EXPECT_FLOAT_EQ(v.x, 0.0f); EXPECT_FLOAT_EQ(v.y, 0.0f); EXPECT_FLOAT_EQ(v.z, 0.0f); @@ -34,7 +34,7 @@ TEST_F(UnitTestVector3, Constructor_Default) TEST_F(UnitTestVector3, Constructor_Values) { - constexpr Vector3 v(1.0f, 2.0f, 3.0f); + constexpr Vector3 v(1.0f, 2.0f, 3.0f); EXPECT_FLOAT_EQ(v.x, 1.0f); EXPECT_FLOAT_EQ(v.y, 2.0f); EXPECT_FLOAT_EQ(v.z, 3.0f); diff --git a/tests/general/UnitTestVector4.cpp b/tests/general/UnitTestVector4.cpp index 07ca8989..3f6351a6 100644 --- a/tests/general/UnitTestVector4.cpp +++ b/tests/general/UnitTestVector4.cpp @@ -14,8 +14,8 @@ using namespace omath; class UnitTestVector4 : public ::testing::Test { protected: - Vector4 v1; - Vector4 v2; + Vector4 v1; + Vector4 v2; void SetUp() override { @@ -27,7 +27,7 @@ class UnitTestVector4 : public ::testing::Test // Test constructor and default values TEST_F(UnitTestVector4, Constructor_Default) { - constexpr Vector4 v; + constexpr Vector4 v; EXPECT_FLOAT_EQ(v.x, 0.0f); EXPECT_FLOAT_EQ(v.y, 0.0f); EXPECT_FLOAT_EQ(v.z, 0.0f); From a0f746b84c26a4bcfe6b96982eca15c0a6b23b5e Mon Sep 17 00:00:00 2001 From: Vladislav Alpatov Date: Sat, 1 Mar 2025 21:15:26 +0300 Subject: [PATCH 2/3] fixed style --- include/omath/Vector3.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/include/omath/Vector3.hpp b/include/omath/Vector3.hpp index 9c2e9c3f..6fe1229d 100644 --- a/include/omath/Vector3.hpp +++ b/include/omath/Vector3.hpp @@ -172,7 +172,6 @@ namespace omath [[nodiscard]] constexpr Vector3 operator-() const { - return Vector3{-this->x, -this->y, -z}; } From 978656b573507b5e82652aed5f8e4e7a7dada01e Mon Sep 17 00:00:00 2001 From: Vladislav Alpatov Date: Sat, 1 Mar 2025 21:17:03 +0300 Subject: [PATCH 3/3] refactored --- include/omath/Vector3.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/omath/Vector3.hpp b/include/omath/Vector3.hpp index 6fe1229d..af90ed6e 100644 --- a/include/omath/Vector3.hpp +++ b/include/omath/Vector3.hpp @@ -24,7 +24,7 @@ namespace omath class Vector3 : public Vector2 { public: - Type z = 0.f; + Type z = static_cast(0); constexpr Vector3(const Type& x, const Type& y, const Type& z) : Vector2(x, y), z(z) { } constexpr Vector3() : Vector2() {}; @@ -172,7 +172,7 @@ namespace omath [[nodiscard]] constexpr Vector3 operator-() const { - return Vector3{-this->x, -this->y, -z}; + return {-this->x, -this->y, -z}; } [[nodiscard]] constexpr Vector3 operator+(const Vector3& v) const