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 e1ee3ef46..bf8b51045 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,15 @@ 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); + }; 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 4c0a31df7..cf1afa1a6 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) @@ -383,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 f57608190..a48ed0b46 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 @@ -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); @@ -931,6 +934,14 @@ 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); + }; inline Double3 operator+(double a, const Double3& b) 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 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) 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: 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) 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