From cf67b283f5779d13f836df1bb3f15153b07fb627 Mon Sep 17 00:00:00 2001 From: THoehne <77296181+THoehne@users.noreply.github.com> Date: Thu, 22 Aug 2024 14:25:37 +0200 Subject: [PATCH] Bug fixes --- .../public/Math/Detail/IntVector2Decl.inl | 6 +- .../public/Math/Detail/IntVector3Decl.inl | 10 +-- .../Runtime/Core/public/Math/IntVector2.hpp | 41 +++++++--- .../Runtime/Core/public/Math/IntVector2.inl | 26 +++--- .../Runtime/Core/public/Math/IntVector3.hpp | 82 +++++++------------ .../Runtime/Core/public/Math/IntVector3.inl | 22 ++--- .../Source/Runtime/Core/public/Math/MathFwd.h | 14 ++++ 7 files changed, 103 insertions(+), 98 deletions(-) diff --git a/Engine/Source/Runtime/Core/public/Math/Detail/IntVector2Decl.inl b/Engine/Source/Runtime/Core/public/Math/Detail/IntVector2Decl.inl index 0831627..f098116 100644 --- a/Engine/Source/Runtime/Core/public/Math/Detail/IntVector2Decl.inl +++ b/Engine/Source/Runtime/Core/public/Math/Detail/IntVector2Decl.inl @@ -146,10 +146,8 @@ namespace Phanes::Core::Math::Detail static constexpr void map(Phanes::Core::Math::TIntVector2& r, const Phanes::Core::Math::TIntVector2& v1, T s) { - s = (T)1.0 / s; - - r.x = v1.x * s; - r.y = v1.y * s; + r.x = v1.x / s; + r.y = v1.y / s; } }; diff --git a/Engine/Source/Runtime/Core/public/Math/Detail/IntVector3Decl.inl b/Engine/Source/Runtime/Core/public/Math/Detail/IntVector3Decl.inl index ae81615..612e3d1 100644 --- a/Engine/Source/Runtime/Core/public/Math/Detail/IntVector3Decl.inl +++ b/Engine/Source/Runtime/Core/public/Math/Detail/IntVector3Decl.inl @@ -77,7 +77,7 @@ namespace Phanes::Core::Math::Detail { v1.x = x; v1.y = y; - v1.y = z; + v1.z = z; v1.w = (T)0; } @@ -169,11 +169,9 @@ namespace Phanes::Core::Math::Detail static constexpr void map(Phanes::Core::Math::TIntVector3& r, const Phanes::Core::Math::TIntVector3& v1, T s) { - s = (T)1.0 / s; - - r.x = v1.x * s; - r.y = v1.y * s; - r.z = v1.z * s; + r.x = v1.x / s; + r.y = v1.y / s; + r.z = v1.z / s; } }; diff --git a/Engine/Source/Runtime/Core/public/Math/IntVector2.hpp b/Engine/Source/Runtime/Core/public/Math/IntVector2.hpp index 07d8472..84ebf9f 100644 --- a/Engine/Source/Runtime/Core/public/Math/IntVector2.hpp +++ b/Engine/Source/Runtime/Core/public/Math/IntVector2.hpp @@ -127,10 +127,10 @@ namespace Phanes::Core::Math { // ======================== // /** - * Addition operation on same TIntVector2 (this) by a floating point value. + * Addition operation on same TIntVector2 (this) by a scalar value. * * @param(v1) Vector to add to - * @param(s) Floating point to add + * @param(s) Scalar to add */ template @@ -147,10 +147,10 @@ namespace Phanes::Core::Math { TIntVector2 operator+= (TIntVector2& v1, const TIntVector2& v2); /** - * Substraction operation on same TIntVector2 (this) by a floating point. + * Substraction operation on same TIntVector2 (this) by a scalar. * * @param(v1) Vector to substract from - * @param(v2) Floating point to substract + * @param(v2) Scalar to substract */ template @@ -168,27 +168,33 @@ namespace Phanes::Core::Math { /** - * Multiplication of TIntVector2 (this) with a floating point. + * Multiplication of TIntVector2 (this) with a scalar. * * @param(v1) Vector to multiply with - * @param(s Floating point to multiply with + * @param(s) scalar to multiply with */ template TIntVector2 operator*= (TIntVector2& v1, T s); + template + TIntVector2 operator*= (TIntVector2& v1, const TIntVector2& v2); + /** * Devision of Vector * * @param(v1) Vector to divide with * @param(s) Scalar to divide with * - * @note Result is rounded (obviously) + * @note Result is rounded */ template TIntVector2 operator/= (TIntVector2& v1, T s); + template + TIntVector2 operator/= (TIntVector2& v1, const TIntVector2& v2); + /** * Stores the remainder of division by a scalar. * @@ -434,6 +440,11 @@ namespace Phanes::Core::Math { // TIntVector2 static function implementation // // ============================================== // + template + T DotP(const TIntVector2& v, const TIntVector2& v1) + { + return v.x * v1.x + v.y * v1.y; + } template TIntVector2 MaxV(TIntVector2& v1, const TIntVector2& v2) @@ -462,6 +473,13 @@ namespace Phanes::Core::Math { return v1; } + /// + /// Returns signs of numbers (1 / -1).
+ /// Returns 1 for zero. + ///
+ /// + /// + /// template TIntVector2 SignVectorV(TIntVector2& v1) { @@ -514,6 +532,8 @@ namespace Phanes::Core::Math { { v1.x = -v1.x; v1.y = -v1.y; + + return v1; } /** @@ -530,7 +550,7 @@ namespace Phanes::Core::Math { template inline bool IsPerpendicular(const TIntVector2& v1, const TIntVector2& v2) { - return (abs(DotP(v1, v2)) = 0); + return (Abs(DotP(v1, v2)) == 0); } /** @@ -547,7 +567,7 @@ namespace Phanes::Core::Math { template inline bool IsParallel(const TIntVector2& v1, const TIntVector2& v2) { - return (abs(DotP(v1, v2)) = 1); + return ((v1.x / v2.x) == (v1.y / v2.y)); } /** @@ -564,7 +584,8 @@ namespace Phanes::Core::Math { template inline bool IsCoincident(const TIntVector2& v1, const TIntVector2& v2) { - return (DotP(v1, v2) = 1); + T tmp = v1.x / v2.x; + return (tmp == (v1.y / v2.y) && tmp > -1); } /** diff --git a/Engine/Source/Runtime/Core/public/Math/IntVector2.inl b/Engine/Source/Runtime/Core/public/Math/IntVector2.inl index 64a9f59..5f862e2 100644 --- a/Engine/Source/Runtime/Core/public/Math/IntVector2.inl +++ b/Engine/Source/Runtime/Core/public/Math/IntVector2.inl @@ -267,7 +267,7 @@ namespace Phanes::Core::Math template TIntVector2 operator&(TIntVector2& v1, const TIntVector2& v2) { - TVector2 r; + TIntVector2 r; Detail::compute_ivec2_and::map(r, v1, v2); return r; } @@ -275,7 +275,7 @@ namespace Phanes::Core::Math template TIntVector2 operator&(TIntVector2& v1, T s) { - TVector2 r; + TIntVector2 r; Detail::compute_ivec2_and::map(r, v1, s); return r; } @@ -283,7 +283,7 @@ namespace Phanes::Core::Math template TIntVector2 operator|(TIntVector2& v1, const TIntVector2& v2) { - TVector2 r; + TIntVector2 r; Detail::compute_ivec2_or::map(r, v1, v2); return r; } @@ -291,7 +291,7 @@ namespace Phanes::Core::Math template TIntVector2 operator|(TIntVector2& v1, T s) { - TVector2 r; + TIntVector2 r; Detail::compute_ivec2_or::map(r, v1, s); return r; } @@ -299,7 +299,7 @@ namespace Phanes::Core::Math template TIntVector2 operator^(TIntVector2& v1, const TIntVector2& v2) { - TVector2 r; + TIntVector2 r; Detail::compute_ivec2_xor::map(r, v1, v2); return r; } @@ -307,7 +307,7 @@ namespace Phanes::Core::Math template TIntVector2 operator^(TIntVector2& v1, T s) { - TVector2 r; + TIntVector2 r; Detail::compute_ivec2_xor::map(r, v1, s); return r; } @@ -315,7 +315,7 @@ namespace Phanes::Core::Math template TIntVector2 operator<<(TIntVector2& v1, const TIntVector2& v2) { - TVector2 r; + TIntVector2 r; Detail::compute_ivec2_left_shift::map(r, v1, v2); return r; } @@ -323,7 +323,7 @@ namespace Phanes::Core::Math template TIntVector2 operator<<(TIntVector2& v1, T s) { - TVector2 r; + TIntVector2 r; Detail::compute_ivec2_left_shift::map(r, v1, s); return r; } @@ -331,7 +331,7 @@ namespace Phanes::Core::Math template TIntVector2 operator>>(TIntVector2& v1, const TIntVector2& v2) { - TVector2 r; + TIntVector2 r; Detail::compute_ivec2_right_shift::map(r, v1, v2); return r; } @@ -339,7 +339,7 @@ namespace Phanes::Core::Math template TIntVector2 operator>>(TIntVector2& v1, T s) { - TVector2 r; + TIntVector2 r; Detail::compute_ivec2_right_shift::map(r, v1, s); return r; } @@ -347,7 +347,7 @@ namespace Phanes::Core::Math template TIntVector2 operator~(TIntVector2& v1) { - TVector2 r; + TIntVector2 r; Detail::compute_ivec2_bnot::map(r, v1); return r; } @@ -376,14 +376,14 @@ namespace Phanes::Core::Math template TIntVector2& operator++(TIntVector2& v1) { - Detail::compute_ivec2_inc::map(v1); + Detail::compute_ivec2_inc::map(v1, v1); return v1; } template TIntVector2& operator--(TIntVector2& v1) { - Detail::compute_ivec2_inc::map(v1); + Detail::compute_ivec2_dec::map(v1, v1); return v1; } diff --git a/Engine/Source/Runtime/Core/public/Math/IntVector3.hpp b/Engine/Source/Runtime/Core/public/Math/IntVector3.hpp index 248b718..4d37be9 100644 --- a/Engine/Source/Runtime/Core/public/Math/IntVector3.hpp +++ b/Engine/Source/Runtime/Core/public/Math/IntVector3.hpp @@ -393,11 +393,11 @@ namespace Phanes::Core::Math { */ template - TIntVector3 CrossPV(TIntVector3& v1, const TIntVector3& v2) + TIntVector3& CrossPV(TIntVector3& v1, const TIntVector3& v2) { - float x = v1.x; - float y = v1.y; - float z = v1.z; + T x = v1.x; + T y = v1.y; + T z = v1.z; v1.x = (y * v2.z) - (z * v2.y); v1.y = (z * v2.x) - (x * v2.z); @@ -416,7 +416,7 @@ namespace Phanes::Core::Math { */ template - TIntVector3 MaxV(TIntVector3& v1, const TIntVector3& v2) + TIntVector3& MaxV(TIntVector3& v1, const TIntVector3& v2) { v1.x = Phanes::Core::Math::Max(v1.x, v2.x); v1.y = Phanes::Core::Math::Max(v1.y, v2.y); @@ -435,7 +435,7 @@ namespace Phanes::Core::Math { */ template - TIntVector3 MinV(TIntVector3& v1, const TIntVector3& v2) + TIntVector3& MinV(TIntVector3& v1, const TIntVector3& v2) { v1.x = Phanes::Core::Math::Min(v1.x, v2.x); v1.y = Phanes::Core::Math::Min(v1.y, v2.y); @@ -453,7 +453,7 @@ namespace Phanes::Core::Math { */ template - TIntVector3 NegateV(TIntVector3& v1) + TIntVector3& NegateV(TIntVector3& v1) { v1.x = -v1.x; v1.y = -v1.y; @@ -462,25 +462,6 @@ namespace Phanes::Core::Math { return v1; } - /** - * Performes componentwise multiplication of two vectors. - * - * @param(v1) Vector one - * @param(v2) Vector two - * - * @note result is stored in v1. - */ - - template - TIntVector3 ScaleV(TIntVector3& v1, const TIntVector3& v2) - { - v1.x *= v2.x; - v1.y *= v2.y; - v1.z *= v2.z; - - return v1; - } - /** * Copies v1 vector * @@ -489,7 +470,7 @@ namespace Phanes::Core::Math { */ template - TIntVector3 Set(TIntVector3& v1, const TIntVector3& v2) + TIntVector3& Set(TIntVector3& v1, const TIntVector3& v2) { v1 = v2; @@ -506,7 +487,7 @@ namespace Phanes::Core::Math { */ template - TIntVector3 Set(TIntVector3& v1, T x, T y, T z) + TIntVector3& Set(TIntVector3& v1, T x, T y, T z) { v1.x = x; v1.y = y; @@ -522,7 +503,7 @@ namespace Phanes::Core::Math { */ template - TIntVector3 SignVectorV(TIntVector3& v1) + TIntVector3& SignVectorV(TIntVector3& v1) { v1.x = (v1.x >= 0) ? 1 : -1; v1.y = (v1.y >= 0) ? 1 : -1; @@ -545,7 +526,7 @@ namespace Phanes::Core::Math { template T ScalarTriple(const TIntVector3& v1, const TIntVector3& v2, const TIntVector3& v3) { - return CrossP(v1, v2) * v3; + return DotP(CrossP(v1, v2), v3); } /** @@ -559,7 +540,7 @@ namespace Phanes::Core::Math { */ template - TIntVector3 VectorTripleV(TIntVector3& v1, const TIntVector3& v2, const TIntVector3& v3) + TIntVector3& VectorTripleV(TIntVector3& v1, const TIntVector3& v2, const TIntVector3& v3) { CrossPV(CrossPV(v1, v2), v3); @@ -593,7 +574,8 @@ namespace Phanes::Core::Math { template inline bool IsParallel(const TIntVector3& v1, const TIntVector3& v2) { - return (abs(DotP(v1, v2)) == 1); + T tmp = v1.x / v2.x; + return (tmp == (v1.y / v2.y) && tmp == (v1.z / v2.z)); } /** @@ -608,7 +590,8 @@ namespace Phanes::Core::Math { template inline bool IsCoincident(const TIntVector3& v1, const TIntVector3& v2) { - return (DotP(v1, v2) == 1); + T tmp = v1.x / v2.x; + return (tmp == (v1.y / v2.y) && tmp == (v1.z / v2.z) && tmp > -1); } /** @@ -645,8 +628,8 @@ namespace Phanes::Core::Math { TIntVector3 CrossP(const TIntVector3& v1, const TIntVector3& v2) { return TIntVector3((v1.y * v2.z) - (v1.z * v2.y), - (v1.z * v2.x) - (v1.x * v2.z), - (v1.x * v2.y) - (v1.y * v2.x)); + (v1.z * v2.x) - (v1.x * v2.z), + (v1.x * v2.y) - (v1.y * v2.x)); } /** @@ -662,8 +645,8 @@ namespace Phanes::Core::Math { TIntVector3 Max(const TIntVector3& v1, const TIntVector3& v2) { return TIntVector3((v1.x > v2.x) ? v1.x : v2.x, - (v1.y > v2.y) ? v1.y : v2.y, - (v1.z > v2.z) ? v1.z : v2.z); + (v1.y > v2.y) ? v1.y : v2.y, + (v1.z > v2.z) ? v1.z : v2.z); } /** @@ -679,8 +662,8 @@ namespace Phanes::Core::Math { TIntVector3 Min(const TIntVector3& v1, const TIntVector3& v2) { return TIntVector3((v1.x < v2.x) ? v1.x : v2.x, - (v1.y < v2.y) ? v1.y : v2.y, - (v1.z < v2.z) ? v1.z : v2.z); + (v1.y < v2.y) ? v1.y : v2.y, + (v1.z < v2.z) ? v1.z : v2.z); } /** @@ -697,21 +680,6 @@ namespace Phanes::Core::Math { return TIntVector3(-v1.x, -v1.y, -v1.z); } - /** - * Multiplies vector componentwise. - * - * @param(v1) Vector one - * @param(v2) Vector two - * - * @return Vector with componentwise products - */ - - template - TIntVector3 Scale(const TIntVector3& v1, const TIntVector3& v2) - { - return TIntVector3(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z); - } - /** * Gets vector triple product ((v1 x v2) x v3). * @@ -728,6 +696,12 @@ namespace Phanes::Core::Math { return CrossP(CrossP(v1, v2), v3); } + template + TIntVector3 SignVector(const TIntVector3& v1) + { + return TIntVector3((v1.x >= 0) ? 1 : -1, (v1.y >= 0) ? 1 : -1, (v1.z >= 0) ? 1 : -1); + } + } // phanes::core::math::coretypes #endif // !INTVECTOR3_H diff --git a/Engine/Source/Runtime/Core/public/Math/IntVector3.inl b/Engine/Source/Runtime/Core/public/Math/IntVector3.inl index 149f296..8c3c0d3 100644 --- a/Engine/Source/Runtime/Core/public/Math/IntVector3.inl +++ b/Engine/Source/Runtime/Core/public/Math/IntVector3.inl @@ -274,7 +274,7 @@ namespace Phanes::Core::Math template TIntVector3 operator&(TIntVector3& v1, const TIntVector3& v2) { - TVector2 r; + TIntVector3 r; Detail::compute_ivec3_and::map(r, v1, v2); return r; } @@ -282,7 +282,7 @@ namespace Phanes::Core::Math template TIntVector3 operator&(TIntVector3& v1, T s) { - TVector2 r; + TIntVector3 r; Detail::compute_ivec3_and::map(r, v1, s); return r; } @@ -290,7 +290,7 @@ namespace Phanes::Core::Math template TIntVector3 operator|(TIntVector3& v1, const TIntVector3& v2) { - TVector2 r; + TIntVector3 r; Detail::compute_ivec3_or::map(r, v1, v2); return r; } @@ -298,7 +298,7 @@ namespace Phanes::Core::Math template TIntVector3 operator|(TIntVector3& v1, T s) { - TVector2 r; + TIntVector3 r; Detail::compute_ivec3_or::map(r, v1, s); return r; } @@ -306,7 +306,7 @@ namespace Phanes::Core::Math template TIntVector3 operator^(TIntVector3& v1, const TIntVector3& v2) { - TVector2 r; + TIntVector3 r; Detail::compute_ivec3_xor::map(r, v1, v2); return r; } @@ -314,7 +314,7 @@ namespace Phanes::Core::Math template TIntVector3 operator^(TIntVector3& v1, T s) { - TVector2 r; + TIntVector3 r; Detail::compute_ivec3_xor::map(r, v1, s); return r; } @@ -322,7 +322,7 @@ namespace Phanes::Core::Math template TIntVector3 operator<<(TIntVector3& v1, const TIntVector3& v2) { - TVector2 r; + TIntVector3 r; Detail::compute_ivec3_left_shift::map(r, v1, v2); return r; } @@ -330,7 +330,7 @@ namespace Phanes::Core::Math template TIntVector3 operator<<(TIntVector3& v1, T s) { - TVector2 r; + TIntVector3 r; Detail::compute_ivec3_left_shift::map(r, v1, s); return r; } @@ -338,7 +338,7 @@ namespace Phanes::Core::Math template TIntVector3 operator>>(TIntVector3& v1, const TIntVector3& v2) { - TVector2 r; + TIntVector3 r; Detail::compute_ivec3_right_shift::map(r, v1, v2); return r; } @@ -346,7 +346,7 @@ namespace Phanes::Core::Math template TIntVector3 operator>>(TIntVector3& v1, T s) { - TVector2 r; + TIntVector3 r; Detail::compute_ivec3_right_shift::map(r, v1, s); return r; } @@ -354,7 +354,7 @@ namespace Phanes::Core::Math template TIntVector3 operator~(TIntVector3& v1) { - TVector2 r; + TIntVector3 r; Detail::compute_ivec3_bnot::map(r, v1); return r; } diff --git a/Engine/Source/Runtime/Core/public/Math/MathFwd.h b/Engine/Source/Runtime/Core/public/Math/MathFwd.h index 912b22c..d880a64 100644 --- a/Engine/Source/Runtime/Core/public/Math/MathFwd.h +++ b/Engine/Source/Runtime/Core/public/Math/MathFwd.h @@ -52,6 +52,20 @@ namespace Phanes::Core::Math { * Specific instantiation of forward declarations. */ + // IntVetor2 + + typedef TIntVector2 IntVector2; + typedef TIntVector2 Vector2i; + typedef TIntVector2 LongVector2; + typedef TIntVector2 Vector2l; + + // IntVetor3 + + typedef TIntVector3 IntVector3; + typedef TIntVector3 Vector3i; + typedef TIntVector3 LongVector3; + typedef TIntVector3 Vector3l; + // Vector2 typedef TVector2 Vector2;