From eb928091b692dda8571f96a42e0e0e348e0ecd3e Mon Sep 17 00:00:00 2001 From: intolerantape Date: Mon, 23 Aug 2021 01:47:17 -0700 Subject: [PATCH 1/8] Added GetForward() and GetBackward() methods to Transform class. --- Source/Engine/Core/Math/Transform.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Source/Engine/Core/Math/Transform.h b/Source/Engine/Core/Math/Transform.h index 069a49992..9dedeb321 100644 --- a/Source/Engine/Core/Math/Transform.h +++ b/Source/Engine/Core/Math/Transform.h @@ -312,6 +312,16 @@ public: { return Vector3::Transform(Vector3::Down, Orientation); } + + FORCE_INLINE Vector3 GetForward() const + { + return Vector3::Transform(Vector3::Forward, Orientation); + } + + FORCE_INLINE Vector3 GetBackward() const + { + return Vector3::Transform(Vector3::Backward, Orientation); + } public: From 81390cf86099791b9e96ed5285f29f43ef1915e6 Mon Sep 17 00:00:00 2001 From: intolerantape Date: Wed, 29 Sep 2021 20:03:46 -0700 Subject: [PATCH 2/8] Added InverseLerp to Math.h Ported from C# --- Source/Engine/Core/Math/Math.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Source/Engine/Core/Math/Math.h b/Source/Engine/Core/Math/Math.h index f138fadb3..df280cce9 100644 --- a/Source/Engine/Core/Math/Math.h +++ b/Source/Engine/Core/Math/Math.h @@ -411,6 +411,15 @@ namespace Math return (T)(a * (1.0f - alpha) + b * alpha); } + // Calculates the linear parameter t that produces the interpolation value within the range [a, b]. + template + static T InverseLerp(const T& a, const T& b, const U& value) + { + if (a == b) + return (T)0; + return Saturate((value - a) / (b - a)); + } + // Performs smooth (cubic Hermite) interpolation between 0 and 1 // @param amount Value between 0 and 1 indicating interpolation amount static float SmoothStep(float amount) From 6657bc924b7646ea0925ae39918f3eda76c54607 Mon Sep 17 00:00:00 2001 From: intolerantape Date: Wed, 29 Sep 2021 20:34:05 -0700 Subject: [PATCH 3/8] Updated Double4.h and Double4.cpp Added a constructor with Vector4 input and cleaned up some documentation. --- Source/Engine/Core/Math/Double4.cpp | 8 ++++++++ Source/Engine/Core/Math/Double4.h | 8 +++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Source/Engine/Core/Math/Double4.cpp b/Source/Engine/Core/Math/Double4.cpp index ecaf611c1..f562e333d 100644 --- a/Source/Engine/Core/Math/Double4.cpp +++ b/Source/Engine/Core/Math/Double4.cpp @@ -54,6 +54,14 @@ Double4::Double4(const Vector3& xyz, double w) { } +Double4::Double4(const Vector4& xyzw) + : X(xyzw.X) + , Y(xyzw.Y) + , Z(xyzw.Z) + , W(xyzw.W) +{ +} + Double4::Double4(const Int2& xy, double z, double w) : X(static_cast(xy.X)) , Y(static_cast(xy.Y)) diff --git a/Source/Engine/Core/Math/Double4.h b/Source/Engine/Core/Math/Double4.h index 65f32e279..7dbaa3630 100644 --- a/Source/Engine/Core/Math/Double4.h +++ b/Source/Engine/Core/Math/Double4.h @@ -131,14 +131,16 @@ public: // Init // @param xy X and Y values in the vector // @param zw Z and W values in the vector - // @param z Z component value - // @param w W component value explicit Double4(const Vector2& xy, const Vector2& zw); // Init // @param xyz X, Y and Z values in the vector // @param w W component value explicit Double4(const Vector3& xyz, double w); + + // Init + // @param xyzw Vector4 value + explicit Double4(const Vector4& xyzw); // Init // @param xy X and Y values in the vector @@ -152,7 +154,7 @@ public: explicit Double4(const Int3& xyz, double w); // Init - // @param color Int4 value + // @param xyzw Int4 value explicit Double4(const Int4& xyzw); // Init From 6150aaaa773586adb4bde31ae2a92265db8270c2 Mon Sep 17 00:00:00 2001 From: intolerantape Date: Wed, 29 Sep 2021 20:34:59 -0700 Subject: [PATCH 4/8] Update Vector4.h Added some missing explicit tags to Vector4 constructors to match the pattern established by other Vector types. --- Source/Engine/Core/Math/Vector4.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Source/Engine/Core/Math/Vector4.h b/Source/Engine/Core/Math/Vector4.h index 1c0926bc3..3e24ae971 100644 --- a/Source/Engine/Core/Math/Vector4.h +++ b/Source/Engine/Core/Math/Vector4.h @@ -125,19 +125,19 @@ public: // @param xy X and Y values in the vector // @param z Z component value // @param w W component value - Vector4(const Vector2& xy, float z, float w); + explicit Vector4(const Vector2& xy, float z, float w); // Init // @param xy X and Y values in the vector // @param zw Z and W values in the vector // @param z Z component value // @param w W component value - Vector4(const Vector2& xy, const Vector2& zw); + explicit Vector4(const Vector2& xy, const Vector2& zw); // Init // @param xyz X, Y and Z values in the vector // @param w W component value - Vector4(const Vector3& xyz, float w); + explicit Vector4(const Vector3& xyz, float w); // Init // @param xy X and Y values in the vector From 06a4c30cc03486badf48e134381c56fb8b559f5b Mon Sep 17 00:00:00 2001 From: intolerantape Date: Wed, 29 Sep 2021 20:40:37 -0700 Subject: [PATCH 5/8] Updated Double3.h and Double3.cpp Added a constructor with Vector3 input and cleaned up some documentation. Also fixed a parameter name and some weird extra whitespace. --- Source/Engine/Core/Math/Double3.cpp | 23 +++++++++++++++-------- Source/Engine/Core/Math/Double3.h | 21 ++++++++++++--------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/Source/Engine/Core/Math/Double3.cpp b/Source/Engine/Core/Math/Double3.cpp index 4c0a31df7..c31f076dd 100644 --- a/Source/Engine/Core/Math/Double3.cpp +++ b/Source/Engine/Core/Math/Double3.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. +// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. #include "Double2.h" #include "Double3.h" @@ -45,6 +45,20 @@ Double3::Double3(const Vector2& xy) { } +Double3::Double3(const Vector3& xyz) + : X(xyz.X) + , Y(xyz.Y) + , Z(xyz.Z) +{ +} + +Double3::Double3(const Vector4& xyzw) + : X(xyzw.X) + , Y(xyzw.Y) + , Z(xyzw.Z) +{ +} + Double3::Double3(const Int2& xy, double z) : X(static_cast(xy.X)) , Y(static_cast(xy.Y)) @@ -66,13 +80,6 @@ Double3::Double3(const Int4& xyzw) { } -Double3::Double3(const Vector4& xyzw) - : X(xyzw.X) - , Y(xyzw.Y) - , Z(xyzw.Z) -{ -} - Double3::Double3(const Double2& xy) : X(xy.X) , Y(xy.Y) diff --git a/Source/Engine/Core/Math/Double3.h b/Source/Engine/Core/Math/Double3.h index f57608190..177bbcc5e 100644 --- a/Source/Engine/Core/Math/Double3.h +++ b/Source/Engine/Core/Math/Double3.h @@ -143,11 +143,19 @@ public: // Init // @param xy Vector2 value explicit Double3(const Vector2& xy); + + // Init + // @param xyz Vector3 value + explicit Double3(const Vector3& xyz); // Init - // @param xy Int22 with X and Y components values + // @param xyz Vector4 value + explicit Double3(const Vector4& xyzw); + + // Init + // @param xy Int2 with X and Y components values // @param z Z component value - explicit Double3(const Int2& xy, double z); + explicit Double3(const Int2& xy, double z); // Init // @param xyz Int3 value @@ -155,22 +163,17 @@ public: // Init // @param xyzw Int4 value - explicit Double3(const Int4& xyzw); - - // Init - // @param xyz Vector4 value - explicit Double3(const Vector4& xyzw); + explicit Double3(const Int4& xyzw); // Init // @param xy Double2 value - Double3(const Double2& xy); + explicit Double3(const Double2& xy); // Init // @param xy Double2 value // @param z Z component value explicit Double3(const Double2& xy, double z); - // Init // @param xyzw Double4 value explicit Double3(const Double4& xyzw); From 31c92cd94c2555207f25e400b15dc7032e8341ea Mon Sep 17 00:00:00 2001 From: intolerantape Date: Thu, 30 Sep 2021 12:22:12 -0700 Subject: [PATCH 6/8] Added Double2::Angle() --- Source/Engine/Core/Math/Double2.h | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Source/Engine/Core/Math/Double2.h b/Source/Engine/Core/Math/Double2.h index e1ee3ef46..905a87a59 100644 --- a/Source/Engine/Core/Math/Double2.h +++ b/Source/Engine/Core/Math/Double2.h @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. +// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. #pragma once @@ -625,6 +625,20 @@ public: /// The third triangle vertex. /// The triangle area. static double TriangleArea(const Double2& v0, const Double2& v1, const Double2& v2); + + /// + /// Calculates the angle (in radians) between from and to. This is always the smallest value. + /// + /// The first vector. + /// The second vector. + /// The angle (in radians). + static double Angle(const Double2& from, const Double2& to) + { + const double dot = Math::Clamp(Dot(Normalize(from), Normalize(to)), -1.0, 1.0); + if (Math::Abs(dot) > (1.0 - ZeroTolerance)) + return dot > 0.0 ? 0.0 : PI; + return Math::Acos(dot); + } }; inline Double2 operator+(double a, const Double2& b) From f25dae2da2d0afac31c98387d70c06974cfad33e Mon Sep 17 00:00:00 2001 From: intolerantape Date: Thu, 30 Sep 2021 12:24:30 -0700 Subject: [PATCH 7/8] Added Double3::Angle --- Source/Engine/Core/Math/Double3.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Source/Engine/Core/Math/Double3.h b/Source/Engine/Core/Math/Double3.h index 177bbcc5e..356c2ee26 100644 --- a/Source/Engine/Core/Math/Double3.h +++ b/Source/Engine/Core/Math/Double3.h @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. +// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. #pragma once @@ -934,6 +934,19 @@ public: /// The triangle area. static double TriangleArea(const Double3& v0, const Double3& v1, const Double3& v2); + /// + /// Calculates the angle (in radians) between from and to. This is always the smallest value. + /// + /// The first vector. + /// The second vector. + /// The angle (in radians). + static double Angle(const Double3& from, const Double3& to) + { + const double dot = Math::Clamp(Dot(Normalize(from), Normalize(to)), -1.0, 1.0); + if (Math::Abs(dot) > (1.0 - ZeroTolerance)) + return dot > 0.0 ? 0.0 : PI; + return Math::Acos(dot); + } }; inline Double3 operator+(double a, const Double3& b) From 9ee0773ab19d8c37696116f7510b7e3b7dd0b543 Mon Sep 17 00:00:00 2001 From: intolerantape Date: Thu, 30 Sep 2021 13:05:09 -0700 Subject: [PATCH 8/8] Moved the various Vector::Angle functions into their respective CPP files. They didn't seem like prime candidates for inlining. --- Source/Engine/Core/Math/Double2.cpp | 10 +++++++++- Source/Engine/Core/Math/Double2.h | 9 ++------- Source/Engine/Core/Math/Double3.cpp | 8 ++++++++ Source/Engine/Core/Math/Double3.h | 9 ++------- Source/Engine/Core/Math/Vector2.cpp | 8 ++++++++ Source/Engine/Core/Math/Vector2.h | 9 ++------- Source/Engine/Core/Math/Vector3.cpp | 8 ++++++++ Source/Engine/Core/Math/Vector3.h | 9 ++------- 8 files changed, 41 insertions(+), 29 deletions(-) diff --git a/Source/Engine/Core/Math/Double2.cpp b/Source/Engine/Core/Math/Double2.cpp index 48534cc7d..2019f9edb 100644 --- a/Source/Engine/Core/Math/Double2.cpp +++ b/Source/Engine/Core/Math/Double2.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. +// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. #include "Double2.h" #include "Double3.h" @@ -107,3 +107,11 @@ double Double2::TriangleArea(const Double2& v0, const Double2& v1, const Double2 { return Math::Abs((v0.X * (v1.Y - v2.Y) + v1.X * (v2.Y - v0.Y) + v2.X * (v0.Y - v1.Y)) / 2.); } + +double Double2::Angle(const Double2& from, const Double2& to) +{ + const double dot = Math::Clamp(Dot(Normalize(from), Normalize(to)), -1.0, 1.0); + if (Math::Abs(dot) > (1.0 - ZeroTolerance)) + return dot > 0.0 ? 0.0 : PI; + return Math::Acos(dot); +} diff --git a/Source/Engine/Core/Math/Double2.h b/Source/Engine/Core/Math/Double2.h index 905a87a59..bf8b51045 100644 --- a/Source/Engine/Core/Math/Double2.h +++ b/Source/Engine/Core/Math/Double2.h @@ -632,13 +632,8 @@ public: /// The first vector. /// The second vector. /// The angle (in radians). - static double Angle(const Double2& from, const Double2& to) - { - const double dot = Math::Clamp(Dot(Normalize(from), Normalize(to)), -1.0, 1.0); - if (Math::Abs(dot) > (1.0 - ZeroTolerance)) - return dot > 0.0 ? 0.0 : PI; - return Math::Acos(dot); - } + static double Angle(const Double2& from, const Double2& to); + }; inline Double2 operator+(double a, const Double2& b) diff --git a/Source/Engine/Core/Math/Double3.cpp b/Source/Engine/Core/Math/Double3.cpp index c31f076dd..cf1afa1a6 100644 --- a/Source/Engine/Core/Math/Double3.cpp +++ b/Source/Engine/Core/Math/Double3.cpp @@ -390,3 +390,11 @@ double Double3::TriangleArea(const Double3& v0, const Double3& v1, const Double3 { return (v2 - v0 ^ v1 - v0).Length() * 0.5; } + +double Double3::Angle(const Double3& from, const Double3& to) +{ + const double dot = Math::Clamp(Dot(Normalize(from), Normalize(to)), -1.0, 1.0); + if (Math::Abs(dot) > (1.0 - ZeroTolerance)) + return dot > 0.0 ? 0.0 : PI; + return Math::Acos(dot); +} diff --git a/Source/Engine/Core/Math/Double3.h b/Source/Engine/Core/Math/Double3.h index 356c2ee26..a48ed0b46 100644 --- a/Source/Engine/Core/Math/Double3.h +++ b/Source/Engine/Core/Math/Double3.h @@ -940,13 +940,8 @@ public: /// The first vector. /// The second vector. /// The angle (in radians). - static double Angle(const Double3& from, const Double3& to) - { - const double dot = Math::Clamp(Dot(Normalize(from), Normalize(to)), -1.0, 1.0); - if (Math::Abs(dot) > (1.0 - ZeroTolerance)) - return dot > 0.0 ? 0.0 : PI; - return Math::Acos(dot); - } + static double Angle(const Double3& from, const Double3& to); + }; inline Double3 operator+(double a, const Double3& b) diff --git a/Source/Engine/Core/Math/Vector2.cpp b/Source/Engine/Core/Math/Vector2.cpp index c0c40b647..75d13ecf9 100644 --- a/Source/Engine/Core/Math/Vector2.cpp +++ b/Source/Engine/Core/Math/Vector2.cpp @@ -107,3 +107,11 @@ float Vector2::TriangleArea(const Vector2& v0, const Vector2& v1, const Vector2& { return Math::Abs((v0.X * (v1.Y - v2.Y) + v1.X * (v2.Y - v0.Y) + v2.X * (v0.Y - v1.Y)) / 2); } + +float Vector2::Angle(const Vector2& from, const Vector2& to) +{ + const float dot = Math::Clamp(Dot(Normalize(from), Normalize(to)), -1.0f, 1.0f); + if (Math::Abs(dot) > (1.0f - ZeroTolerance)) + return dot > 0.0f ? 0.0f : PI; + return Math::Acos(dot); +} diff --git a/Source/Engine/Core/Math/Vector2.h b/Source/Engine/Core/Math/Vector2.h index ea177a14a..c2b7569b4 100644 --- a/Source/Engine/Core/Math/Vector2.h +++ b/Source/Engine/Core/Math/Vector2.h @@ -621,13 +621,8 @@ public: /// The first vector. /// The second vector. /// The angle (in radians). - static float Angle(const Vector2& from, const Vector2& to) - { - const float dot = Math::Clamp(Dot(Normalize(from), Normalize(to)), -1.0f, 1.0f); - if (Math::Abs(dot) > (1.0f - ZeroTolerance)) - return dot > 0.0f ? 0.0f : PI; - return Math::Acos(dot); - } + static float Angle(const Vector2& from, const Vector2& to); + }; inline Vector2 operator+(float a, const Vector2& b) diff --git a/Source/Engine/Core/Math/Vector3.cpp b/Source/Engine/Core/Math/Vector3.cpp index 176819149..4e3f9b7d9 100644 --- a/Source/Engine/Core/Math/Vector3.cpp +++ b/Source/Engine/Core/Math/Vector3.cpp @@ -383,3 +383,11 @@ float Vector3::TriangleArea(const Vector3& v0, const Vector3& v1, const Vector3& { return (v2 - v0 ^ v1 - v0).Length() * 0.5f; } + +float Vector3::Angle(const Vector3& from, const Vector3& to) +{ + const float dot = Math::Clamp(Dot(Normalize(from), Normalize(to)), -1.0f, 1.0f); + if (Math::Abs(dot) > (1.0f - ZeroTolerance)) + return dot > 0.0f ? 0.0f : PI; + return Math::Acos(dot); +} diff --git a/Source/Engine/Core/Math/Vector3.h b/Source/Engine/Core/Math/Vector3.h index 1a83169d2..7b79a9768 100644 --- a/Source/Engine/Core/Math/Vector3.h +++ b/Source/Engine/Core/Math/Vector3.h @@ -935,13 +935,8 @@ public: /// The first vector. /// The second vector. /// The angle (in radians). - static float Angle(const Vector3& from, const Vector3& to) - { - const float dot = Math::Clamp(Dot(Normalize(from), Normalize(to)), -1.0f, 1.0f); - if (Math::Abs(dot) > (1.0f - ZeroTolerance)) - return dot > 0.0f ? 0.0f : PI; - return Math::Acos(dot); - } + static float Angle(const Vector3& from, const Vector3& to); + }; inline Vector3 operator+(float a, const Vector3& b)