Skip to content

Commit

Permalink
Merge pull request #481 from FUSEEProjectTeam/feature/461-fix-normalize
Browse files Browse the repository at this point in the history
float3/4.Normalize.Zero returns zero
  • Loading branch information
wrestledBearOnce authored May 4, 2022
2 parents e4d11bd + 89b02ef commit e5da1c0
Show file tree
Hide file tree
Showing 15 changed files with 58 additions and 29 deletions.
4 changes: 3 additions & 1 deletion src/Math/Core/double2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ public static double2 Clamp(double2 vec, double2 min, double2 max)
/// </returns>
public static double2 Normalize(double2 vec)
{
if (vec.Length <= M.EpsilonDouble) return Zero;
double scale = 1.0 / vec.Length;
vec.x *= scale;
vec.y *= scale;
Expand All @@ -444,6 +445,7 @@ public static double2 Normalize(double2 vec)
/// </returns>
public static double2 NormalizeFast(double2 vec)
{
if (vec.Length <= M.EpsilonDouble) return Zero;
double scale = M.InverseSqrtFast(vec.x * vec.x + vec.y * vec.y);
vec.x *= scale;
vec.y *= scale;
Expand Down Expand Up @@ -514,7 +516,7 @@ public static double2 Lerp(double2 a, double2 b, double blend)
/// </summary>
/// <param name="a">First input vector</param>
/// <param name="b">Second input vector</param>
/// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
/// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
public static double2 Lerp(double2 a, double2 b, double2 blend)
{
a.x = blend.x * (b.x - a.x) + a.x;
Expand Down
15 changes: 7 additions & 8 deletions src/Math/Core/double3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -539,14 +539,12 @@ public static double3 Clamp(double3 vec, double3 min, double3 max)
/// </returns>
public static double3 Normalize(double3 vec)
{
if (vec.Length > M.EpsilonDouble)
{
var scale = 1.0 / vec.Length;
if (vec.Length <= M.EpsilonDouble) return Zero;
var scale = 1.0 / vec.Length;
vec.x *= scale;
vec.y *= scale;
vec.z *= scale;

vec.x *= scale;
vec.y *= scale;
vec.z *= scale;
}

return vec;
}
Expand Down Expand Up @@ -586,6 +584,7 @@ public static double3[] OrthoNormalize(double3 normal, double3 tangent)
/// </returns>
public static double3 NormalizeFast(double3 vec)
{
if (vec.Length <= M.EpsilonDouble) return Zero;
var scale = M.InverseSqrtFast(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z);
vec.x *= scale;
vec.y *= scale;
Expand Down Expand Up @@ -679,7 +678,7 @@ public static double3 Lerp(double3 a, double3 b, double blend)
/// </summary>
/// <param name="a">First input vector</param>
/// <param name="b">Second input vector</param>
/// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
/// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
public static double3 Lerp(double3 a, double3 b, double3 blend)
{
a.x = blend.x * (b.x - a.x) + a.x;
Expand Down
7 changes: 5 additions & 2 deletions src/Math/Core/double4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ public double4 Round()
#endregion public Round()

/// <summary>
/// Converts this double4 - which is interpreted as a color - from sRgb space to linear color space.
/// Converts this double4 - which is interpreted as a color - from sRgb space to linear color space.
/// </summary>
/// <returns></returns>
public double4 LinearColorFromSRgb()
Expand Down Expand Up @@ -492,6 +492,7 @@ public static double4 Clamp(double4 vec, double4 min, double4 max)
/// <returns>The normalized vector</returns>
public static double4 Normalize(double4 vec)
{
if (vec.Length <= M.EpsilonDouble) return Zero;
double scale = 1.0 / vec.Length;
vec.x *= scale;
vec.y *= scale;
Expand All @@ -511,6 +512,7 @@ public static double4 Normalize(double4 vec)
/// <returns>The scaled vector.</returns>
public static double4 Normalize1(double4 vec)
{
if (vec.Length <= M.EpsilonDouble) return Zero;
double scale = 1.0 / vec.Length1;
vec.x *= scale;
vec.y *= scale;
Expand All @@ -530,6 +532,7 @@ public static double4 Normalize1(double4 vec)
/// <returns>The normalized vector</returns>
public static double4 NormalizeFast(double4 vec)
{
if (vec.Length <= M.EpsilonDouble) return Zero;
double scale = M.InverseSqrtFast(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z + vec.w * vec.w);
vec.x *= scale;
vec.y *= scale;
Expand Down Expand Up @@ -600,7 +603,7 @@ public static double4 Lerp(double4 a, double4 b, double blend)
/// </summary>
/// <param name="a">First input vector</param>
/// <param name="b">Second input vector</param>
/// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
/// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
public static double4 Lerp(double4 a, double4 b, double4 blend)
{
a.x = blend.x * (b.x - a.x) + a.x;
Expand Down
4 changes: 3 additions & 1 deletion src/Math/Core/float2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ public static float2 Clamp(float2 vec, float2 min, float2 max)
/// </returns>
public static float2 Normalize(float2 vec)
{
if (vec.Length <= M.EpsilonFloat) return Zero;
float scale = 1.0f / vec.Length;
vec.x *= scale;
vec.y *= scale;
Expand All @@ -444,6 +445,7 @@ public static float2 Normalize(float2 vec)
/// </returns>
public static float2 NormalizeFast(float2 vec)
{
if (vec.Length <= M.EpsilonFloat) return Zero;
float scale = M.InverseSqrtFast(vec.x * vec.x + vec.y * vec.y);
vec.x *= scale;
vec.y *= scale;
Expand Down Expand Up @@ -514,7 +516,7 @@ public static float2 Lerp(float2 a, float2 b, float blend)
/// </summary>
/// <param name="a">First input vector</param>
/// <param name="b">Second input vector</param>
/// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
/// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
public static float2 Lerp(float2 a, float2 b, float2 blend)
{
a.x = blend.x * (b.x - a.x) + a.x;
Expand Down
15 changes: 7 additions & 8 deletions src/Math/Core/float3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -539,14 +539,12 @@ public static float3 Clamp(float3 vec, float3 min, float3 max)
/// </returns>
public static float3 Normalize(float3 vec)
{
if (vec.Length > M.EpsilonFloat)
{
var scale = 1.0f / vec.Length;
if (vec.Length <= M.EpsilonFloat) return Zero;
var scale = 1.0f / vec.Length;
vec.x *= scale;
vec.y *= scale;
vec.z *= scale;

vec.x *= scale;
vec.y *= scale;
vec.z *= scale;
}

return vec;
}
Expand Down Expand Up @@ -586,6 +584,7 @@ public static float3[] OrthoNormalize(float3 vecOne, float3 vecTwo)
/// </returns>
public static float3 NormalizeFast(float3 vec)
{
if (vec.Length <= M.EpsilonFloat) return Zero;
var scale = M.InverseSqrtFast(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z);
vec.x *= scale;
vec.y *= scale;
Expand Down Expand Up @@ -679,7 +678,7 @@ public static float3 Lerp(float3 a, float3 b, float blend)
/// </summary>
/// <param name="a">First input vector</param>
/// <param name="b">Second input vector</param>
/// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
/// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
public static float3 Lerp(float3 a, float3 b, float3 blend)
{
a.x = blend.x * (b.x - a.x) + a.x;
Expand Down
7 changes: 5 additions & 2 deletions src/Math/Core/float4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ public float4 Round()
#endregion public Round()

/// <summary>
/// Converts this float4 - which is interpreted as a color - from sRgb space to linear color space.
/// Converts this float4 - which is interpreted as a color - from sRgb space to linear color space.
/// </summary>
/// <returns></returns>
public float4 LinearColorFromSRgb()
Expand Down Expand Up @@ -492,6 +492,7 @@ public static float4 Clamp(float4 vec, float4 min, float4 max)
/// <returns>The normalized vector</returns>
public static float4 Normalize(float4 vec)
{
if (vec.Length <= M.EpsilonFloat) return Zero;
float scale = 1.0f / vec.Length;
vec.x *= scale;
vec.y *= scale;
Expand All @@ -511,6 +512,7 @@ public static float4 Normalize(float4 vec)
/// <returns>The scaled vector.</returns>
public static float4 Normalize1(float4 vec)
{
if (vec.Length <= M.EpsilonFloat) return Zero;
float scale = 1.0f / vec.Length1;
vec.x *= scale;
vec.y *= scale;
Expand All @@ -530,6 +532,7 @@ public static float4 Normalize1(float4 vec)
/// <returns>The normalized vector</returns>
public static float4 NormalizeFast(float4 vec)
{
if (vec.Length <= M.EpsilonFloat) return Zero;
float scale = M.InverseSqrtFast(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z + vec.w * vec.w);
vec.x *= scale;
vec.y *= scale;
Expand Down Expand Up @@ -600,7 +603,7 @@ public static float4 Lerp(float4 a, float4 b, float blend)
/// </summary>
/// <param name="a">First input vector</param>
/// <param name="b">Second input vector</param>
/// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
/// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
public static float4 Lerp(float4 a, float4 b, float4 blend)
{
a.x = blend.x * (b.x - a.x) + a.x;
Expand Down
8 changes: 6 additions & 2 deletions src/Math/Core/int2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,8 @@ public static int2 Clamp(int2 vec, int2 min, int2 max)
/// <returns>The normalized vector</returns>
public static float2 Normalize(int2 vec)
{
if (vec.Length <= M.EpsilonFloat) return float2.Zero;

float scale = 1.0f / vec.Length;

return new float2()
Expand All @@ -437,6 +439,8 @@ public static float2 Normalize(int2 vec)
/// <returns>The normalized vector</returns>
public static float2 NormalizeFast(int2 vec)
{
if (vec.Length <= M.EpsilonFloat) return float2.Zero;

float scale = M.InverseSqrtFast(vec.x * vec.x + vec.y * vec.y);
return new float2()
{
Expand Down Expand Up @@ -493,7 +497,7 @@ public static int2 Pow(int2 val, int exp)
/// </summary>
/// <param name="a">First input vector</param>
/// <param name="b">Second input vector</param>
/// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
/// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
public static float2 Lerp(int2 a, int2 b, float blend)
{
return new float2()
Expand All @@ -509,7 +513,7 @@ public static float2 Lerp(int2 a, int2 b, float blend)
/// </summary>
/// <param name="a">First input vector</param>
/// <param name="b">Second input vector</param>
/// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
/// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
public static float2 Lerp(int2 a, int2 b, float2 blend)
{
return new float2()
Expand Down
10 changes: 7 additions & 3 deletions src/Math/Core/int3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,8 @@ public static int3 Clamp(int3 vec, int3 min, int3 max)
/// <returns>The normalized vector</returns>
public static float3 Normalize(int3 vec)
{
if (vec.Length <= M.EpsilonFloat) return float3.Zero;

float scale = 1.0f / vec.Length;

return new float3()
Expand All @@ -537,6 +539,8 @@ public static float3 Normalize(int3 vec)
/// <returns>The normalized vector</returns>
public static float3 NormalizeFast(int3 vec)
{
if (vec.Length <= M.EpsilonFloat) return float3.Zero;

float scale = M.InverseSqrtFast(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z);
return new float3()
{
Expand Down Expand Up @@ -615,7 +619,7 @@ public static int3 Pow(int3 val, int exp)
/// </summary>
/// <param name="a">First input vector</param>
/// <param name="b">Second input vector</param>
/// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
/// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
public static float3 Lerp(int3 a, int3 b, float blend)
{
return new float3()
Expand All @@ -632,7 +636,7 @@ public static float3 Lerp(int3 a, int3 b, float blend)
/// </summary>
/// <param name="a">First input vector</param>
/// <param name="b">Second input vector</param>
/// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
/// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
public static float3 Lerp(int3 a, int3 b, float3 blend)
{
return new float3()
Expand All @@ -643,7 +647,7 @@ public static float3 Lerp(int3 a, int3 b, float3 blend)
};
}

#endregion Lerp
#endregion Lerp

#region CalculateAngle

Expand Down
10 changes: 8 additions & 2 deletions src/Math/Core/int4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public int[] ToArray()
#endregion public int[] ToArray()

/// <summary>
/// Converts this int4 - which is interpreted as a color - from sRgb space to linear color space.
/// Converts this int4 - which is interpreted as a color - from sRgb space to linear color space.
/// </summary>
/// <returns></returns>
public int4 LinearColorFromSRgb()
Expand Down Expand Up @@ -474,6 +474,8 @@ public static int4 Clamp(int4 vec, int4 min, int4 max)
/// <returns>The normalized vector</returns>
public static float4 Normalize(int4 vec)
{
if (vec.Length <= M.EpsilonFloat) return float4.Zero;

float scale = 1.0f / vec.Length;

return new float4()
Expand All @@ -496,6 +498,8 @@ public static float4 Normalize(int4 vec)
/// <returns>The scaled vector.</returns>
public static float4 Normalize1(int4 vec)
{
if (vec.Length <= M.EpsilonFloat) return float4.Zero;

float scale = 1.0f / vec.Length1;

return new float4()
Expand All @@ -518,6 +522,8 @@ public static float4 Normalize1(int4 vec)
/// <returns>The normalized vector</returns>
public static float4 NormalizeFast(int4 vec)
{
if (vec.Length <= M.EpsilonFloat) return float4.Zero;

float scale = M.InverseSqrtFast(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z + vec.w * vec.w);
return new float4()
{
Expand Down Expand Up @@ -592,7 +598,7 @@ public static float4 Lerp(int4 a, int4 b, float blend)
/// </summary>
/// <param name="a">First input vector</param>
/// <param name="b">Second input vector</param>
/// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
/// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
public static float4 Lerp(int4 a, int4 b, float4 blend)
{
return new float4()
Expand Down
1 change: 1 addition & 0 deletions src/Tests/Math/Core/Float2Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ public static IEnumerable<object[]> GetNormalize()
yield return new object[] { new float2(4, 0), new float2(1, 0) };
yield return new object[] { new float2(0, 4), new float2(0, 1) };
yield return new object[] { new float2(1, 1), new float2((float)System.Math.Sqrt(0.5), (float)System.Math.Sqrt(0.5)) };
yield return new object[] { float2.Zero, float2.Zero };
}

public static IEnumerable<object[]> GetAddition()
Expand Down
1 change: 1 addition & 0 deletions src/Tests/Math/Core/Float3Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,7 @@ public static IEnumerable<object[]> GetNormalize()
new float3(1, 1, 1),
new float3((float)System.Math.Sqrt(1d / 3d), (float)System.Math.Sqrt(1d / 3d), (float)System.Math.Sqrt(1d / 3d))
};
yield return new object[] { float3.Zero, float3.Zero };
}

public static IEnumerable<object[]> GetAddition()
Expand Down
1 change: 1 addition & 0 deletions src/Tests/Math/Core/Float4Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ public static IEnumerable<object[]> GetNormalize()
yield return new object[] { new float4(0, 0, 4, 0), new float4(0, 0, 1, 0) };
yield return new object[] { new float4(0, 0, 0, 4), new float4(0, 0, 0, 1) };
yield return new object[] { new float4(1, 1, 1, 1), new float4(0.5f, 0.5f, 0.5f, 0.5f) };
yield return new object[] { float4.Zero, float4.Zero };
}

public static IEnumerable<object[]> GetAddition()
Expand Down
2 changes: 2 additions & 0 deletions src/Tests/Math/Core/Int2Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,8 @@ public static IEnumerable<object[]> GetNormalize()
yield return new object[] { new int2(4, 0), new float2(1, 0) };
yield return new object[] { new int2(0, 4), new float2(0, 1) };
yield return new object[] { new int2(1, 1), new float2((float)System.Math.Sqrt(0.5), (float)System.Math.Sqrt(0.5)) };
yield return new object[] { int2.Zero, float2.Zero };

}

public static IEnumerable<object[]> GetAddition()
Expand Down
1 change: 1 addition & 0 deletions src/Tests/Math/Core/Int3Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@ public static IEnumerable<object[]> GetNormalize()
new int3(1, 1, 1),
new float3((float)System.Math.Sqrt(1d / 3d), (float)System.Math.Sqrt(1d / 3d), (float)System.Math.Sqrt(1d / 3d))
};
yield return new object[] { int3.Zero, float3.Zero };
}

public static IEnumerable<object[]> GetAddition()
Expand Down
1 change: 1 addition & 0 deletions src/Tests/Math/Core/Int4Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ public static IEnumerable<object[]> GetNormalize()
yield return new object[] { new int4(0, 0, 4, 0), new float4(0, 0, 1, 0) };
yield return new object[] { new int4(0, 0, 0, 4), new float4(0, 0, 0, 1) };
yield return new object[] { new int4(1, 1, 1, 1), new float4(0.5f, 0.5f, 0.5f, 0.5f) };
yield return new object[] { int4.Zero, float4.Zero };
}

public static IEnumerable<object[]> GetAddition()
Expand Down

0 comments on commit e5da1c0

Please sign in to comment.