Skip to content

Commit

Permalink
feat: replace Mathf.Epsilon and MathD.Epsilon for float.Epsilon and d…
Browse files Browse the repository at this point in the history
…ouble.Epsilon
  • Loading branch information
brmassa committed Sep 17, 2024
1 parent f56b778 commit cc1009f
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Prowl.Runtime/GUI/Gui.Interaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public Interactable GetInteractable(LayoutNode target, Rect? interactArea = null
/// </summary>
public bool IsBlockedByInteractable(Vector2 pos, double zIndex = -1, ulong ignoreID = 0)
{
if (Math.Abs(zIndex - (-1)) < Mathf.Epsilon)
if (MathD.ApproximatelyEquals(zIndex, -1))
{
if (!_zInteractableCounter.TryGetValue(CurrentZIndex, out int count))
count = 0;
Expand Down
7 changes: 2 additions & 5 deletions Prowl.Runtime/Math/MathD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Prowl.Runtime;

public static class MathD
{
private const MethodImplOptions IN = MethodImplOptions.AggressiveInlining;
public const MethodImplOptions IN = MethodImplOptions.AggressiveInlining;

#region Constants

Expand Down Expand Up @@ -38,9 +38,6 @@ public static class MathD
/// A small but not tiny value, Used in places like ApproximatelyEquals, where there is some tolerance (0.00001)
public static readonly double Small = 0.000001;

/// <inheritdoc cref="double.MinValue"/>
public static readonly double Epsilon = double.MinValue;

/// <inheritdoc cref="double.PositiveInfinity"/>
public const double Infinity = double.PositiveInfinity;

Expand Down Expand Up @@ -194,7 +191,7 @@ public static double LerpAngle(double aRad, double bRad, double t)

[MethodImpl(IN)] public static int ComputeMipLevels(int width, int height) => (int)Math.Log2(Math.Max(width, height));

[MethodImpl(IN)] public static bool ApproximatelyEquals(double a, double b) => Abs(a - b) < Epsilon;
[MethodImpl(IN)] public static bool ApproximatelyEquals(double a, double b) => Abs(a - b) < double.Epsilon;
[MethodImpl(IN)] public static bool ApproximatelyEquals(Vector2 a, Vector2 b) => ApproximatelyEquals(a.x, b.x) && ApproximatelyEquals(a.y, b.y);
[MethodImpl(IN)] public static bool ApproximatelyEquals(Vector3 a, Vector3 b) => ApproximatelyEquals(a.x, b.x) && ApproximatelyEquals(a.y, b.y) && ApproximatelyEquals(a.z, b.z);
[MethodImpl(IN)] public static bool ApproximatelyEquals(Vector4 a, Vector4 b) => ApproximatelyEquals(a.x, b.x) && ApproximatelyEquals(a.y, b.y) && ApproximatelyEquals(a.z, b.z) && ApproximatelyEquals(a.w, b.w);
Expand Down
8 changes: 5 additions & 3 deletions Prowl.Runtime/Math/Mathf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
// Licensed under the MIT License. See the LICENSE file in the project root for details.

using System;
using System.Runtime.CompilerServices;

namespace Prowl.Runtime;

public static class Mathf
{
public static readonly float Epsilon = float.MinValue;

public static bool ApproximatelyEquals(float value1, float value2) => Math.Abs(value1 - value2) < Epsilon;
public static bool ApproximatelyEquals(float value1, float value2) => Math.Abs(value1 - value2) < float.Epsilon;
[MethodImpl(MathD.IN)] public static bool ApproximatelyEquals(System.Numerics.Vector2 a, System.Numerics.Vector2 b) => ApproximatelyEquals(a.X, b.X) && ApproximatelyEquals(a.Y, b.Y);
[MethodImpl(MathD.IN)] public static bool ApproximatelyEquals(System.Numerics.Vector3 a, System.Numerics.Vector3 b) => ApproximatelyEquals(a.X, b.X) && ApproximatelyEquals(a.Y, b.Y) && ApproximatelyEquals(a.Z, b.Z);
[MethodImpl(MathD.IN)] public static bool ApproximatelyEquals(System.Numerics.Vector4 a, System.Numerics.Vector4 b) => ApproximatelyEquals(a.X, b.X) && ApproximatelyEquals(a.Y, b.Y) && ApproximatelyEquals(a.Z, b.Z) && ApproximatelyEquals(a.W, b.W);
}
12 changes: 6 additions & 6 deletions Prowl.Runtime/Math/Matrix4x4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public static Matrix4x4 CreateBillboard(Vector3 objectPosition, Vector3 cameraPo

double norm = zaxis.sqrMagnitude;

if (norm < MathD.Epsilon)
if (norm < double.Epsilon)
{
zaxis = -cameraForwardVector;
}
Expand Down Expand Up @@ -288,7 +288,7 @@ public static Matrix4x4 CreateConstrainedBillboard(Vector3 objectPosition, Vecto

double norm = faceDir.sqrMagnitude;

if (norm < MathD.Epsilon)
if (norm < double.Epsilon)
{
faceDir = -cameraForwardVector;
}
Expand Down Expand Up @@ -1420,14 +1420,14 @@ public static bool Decompose(Matrix4x4 matrix, out Vector3 scale, out Quaternion
}
#endregion

if (pfScales[a] < MathD.Epsilon)
if (pfScales[a] < double.Epsilon)
{
*(pVectorBasis[a]) = pCanonicalBasis[a];
}

*pVectorBasis[a] = Vector3.Normalize(*pVectorBasis[a]);

if (pfScales[b] < MathD.Epsilon)
if (pfScales[b] < double.Epsilon)
{
uint cc;
double fAbsX, fAbsY, fAbsZ;
Expand Down Expand Up @@ -1480,7 +1480,7 @@ public static bool Decompose(Matrix4x4 matrix, out Vector3 scale, out Quaternion

*pVectorBasis[b] = Vector3.Normalize(*pVectorBasis[b]);

if (pfScales[c] < MathD.Epsilon)
if (pfScales[c] < double.Epsilon)
{
*pVectorBasis[c] = Vector3.Cross(*pVectorBasis[a], *pVectorBasis[b]);
}
Expand All @@ -1502,7 +1502,7 @@ public static bool Decompose(Matrix4x4 matrix, out Vector3 scale, out Quaternion
det -= 1.0;
det *= det;

if ((det > MathD.Epsilon))
if ((det > double.Epsilon))
{
// Non-SRT matrix encountered
rotation = Quaternion.identity;
Expand Down
4 changes: 2 additions & 2 deletions Prowl.Runtime/Math/Quaternion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public double Magnitude()
public static Quaternion NormalizeSafe(Quaternion q)
{
double mag = q.Magnitude();
if (mag < MathD.Epsilon)
if (mag < double.Epsilon)
return identity;
else
return q / mag;
Expand Down Expand Up @@ -334,7 +334,7 @@ public static Quaternion Slerp(Quaternion quaternion1, Quaternion quaternion2, d

double s1, s2;

if (cosOmega > (1.0 - MathD.Epsilon))
if (cosOmega > (1.0 - double.Epsilon))
{
// Too close, do straight linear interpolation.
s1 = 1.0 - t;
Expand Down
6 changes: 3 additions & 3 deletions Prowl.Runtime/Math/Ray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public override int GetHashCode()
{
double? tMin = null, tMax = null;

if (Math.Abs(direction.x) < MathD.Epsilon)
if (Math.Abs(direction.x) < double.Epsilon)
{
if (origin.x < box.min.x || origin.x > box.max.x)
return null;
Expand All @@ -99,7 +99,7 @@ public override int GetHashCode()
}
}

if (Math.Abs(direction.y) < MathD.Epsilon)
if (Math.Abs(direction.y) < double.Epsilon)
{
if (origin.y < box.min.y || origin.y > box.max.y)
return null;
Expand All @@ -123,7 +123,7 @@ public override int GetHashCode()
if (!tMax.HasValue || tMaxY < tMax) tMax = tMaxY;
}

if (Math.Abs(direction.z) < MathD.Epsilon)
if (Math.Abs(direction.z) < double.Epsilon)
{
if (origin.z < box.min.z || origin.z > box.max.z)
return null;
Expand Down
4 changes: 2 additions & 2 deletions Prowl.Runtime/Math/Transform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public void RotateAround(Vector3 point, Vector3 axis, double angle)
internal void RotateAroundInternal(Vector3 worldAxis, double rad)
{
Vector3 localAxis = InverseTransformDirection(worldAxis);
if (localAxis.sqrMagnitude > MathD.Epsilon)
if (localAxis.sqrMagnitude > double.Epsilon)
{
localAxis.Normalize();
Quaternion q = Quaternion.AngleAxis(rad, localAxis);
Expand Down Expand Up @@ -356,6 +356,6 @@ public Matrix4x4 GetWorldRotationAndScale()
return ret;
}

static double InverseSafe(double f) => MathD.Abs(f) > MathD.Epsilon ? 1.0F / f : 0.0F;
static double InverseSafe(double f) => MathD.Abs(f) > double.Epsilon ? 1.0F / f : 0.0F;
static Vector3 InverseSafe(Vector3 v) => new Vector3(InverseSafe(v.x), InverseSafe(v.y), InverseSafe(v.z));
}

0 comments on commit cc1009f

Please sign in to comment.