From 6e7209f8101c6d714745b150eef0bd938074a555 Mon Sep 17 00:00:00 2001 From: Moritz Roetner Date: Fri, 29 Apr 2022 14:50:05 +0200 Subject: [PATCH 1/2] Added checks for smaller zero, int2/3/4 normalize returns an int2/3/4 after normalize --- src/Math/Core/double2.cs | 4 +++- src/Math/Core/double3.cs | 15 ++++++------ src/Math/Core/double4.cs | 7 ++++-- src/Math/Core/float2.cs | 4 +++- src/Math/Core/float3.cs | 15 ++++++------ src/Math/Core/float4.cs | 7 ++++-- src/Math/Core/int2.cs | 26 ++++++++++---------- src/Math/Core/int3.cs | 33 ++++++++++++++------------ src/Math/Core/int4.cs | 51 ++++++++++++++++++++++------------------ 9 files changed, 90 insertions(+), 72 deletions(-) diff --git a/src/Math/Core/double2.cs b/src/Math/Core/double2.cs index e141f98d5b..d9c41162ac 100644 --- a/src/Math/Core/double2.cs +++ b/src/Math/Core/double2.cs @@ -425,6 +425,7 @@ public static double2 Clamp(double2 vec, double2 min, double2 max) /// 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; @@ -444,6 +445,7 @@ public static double2 Normalize(double2 vec) /// 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; @@ -514,7 +516,7 @@ public static double2 Lerp(double2 a, double2 b, double blend) /// /// First input vector /// Second input vector - /// The blend factor. a when blend=0, b when blend=1. + /// The blend factor. a when blend=0, b when blend=1. public static double2 Lerp(double2 a, double2 b, double2 blend) { a.x = blend.x * (b.x - a.x) + a.x; diff --git a/src/Math/Core/double3.cs b/src/Math/Core/double3.cs index ce593cd778..c3431903d7 100644 --- a/src/Math/Core/double3.cs +++ b/src/Math/Core/double3.cs @@ -539,14 +539,12 @@ public static double3 Clamp(double3 vec, double3 min, double3 max) /// 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; } @@ -586,6 +584,7 @@ public static double3[] OrthoNormalize(double3 normal, double3 tangent) /// 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; @@ -679,7 +678,7 @@ public static double3 Lerp(double3 a, double3 b, double blend) /// /// First input vector /// Second input vector - /// The blend factor. a when blend=0, b when blend=1. + /// The blend factor. a when blend=0, b when blend=1. public static double3 Lerp(double3 a, double3 b, double3 blend) { a.x = blend.x * (b.x - a.x) + a.x; diff --git a/src/Math/Core/double4.cs b/src/Math/Core/double4.cs index 0e9959b5d1..2bf0ac80d7 100644 --- a/src/Math/Core/double4.cs +++ b/src/Math/Core/double4.cs @@ -316,7 +316,7 @@ public double4 Round() #endregion public Round() /// - /// 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. /// /// public double4 LinearColorFromSRgb() @@ -492,6 +492,7 @@ public static double4 Clamp(double4 vec, double4 min, double4 max) /// The normalized vector 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; @@ -511,6 +512,7 @@ public static double4 Normalize(double4 vec) /// The scaled vector. 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; @@ -530,6 +532,7 @@ public static double4 Normalize1(double4 vec) /// The normalized vector 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; @@ -600,7 +603,7 @@ public static double4 Lerp(double4 a, double4 b, double blend) /// /// First input vector /// Second input vector - /// The blend factor. a when blend=0, b when blend=1. + /// The blend factor. a when blend=0, b when blend=1. public static double4 Lerp(double4 a, double4 b, double4 blend) { a.x = blend.x * (b.x - a.x) + a.x; diff --git a/src/Math/Core/float2.cs b/src/Math/Core/float2.cs index 038610e4ae..0df58b5dbd 100644 --- a/src/Math/Core/float2.cs +++ b/src/Math/Core/float2.cs @@ -425,6 +425,7 @@ public static float2 Clamp(float2 vec, float2 min, float2 max) /// 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; @@ -444,6 +445,7 @@ public static float2 Normalize(float2 vec) /// 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; @@ -514,7 +516,7 @@ public static float2 Lerp(float2 a, float2 b, float blend) /// /// First input vector /// Second input vector - /// The blend factor. a when blend=0, b when blend=1. + /// The blend factor. a when blend=0, b when blend=1. public static float2 Lerp(float2 a, float2 b, float2 blend) { a.x = blend.x * (b.x - a.x) + a.x; diff --git a/src/Math/Core/float3.cs b/src/Math/Core/float3.cs index db71006497..57caa77ec0 100644 --- a/src/Math/Core/float3.cs +++ b/src/Math/Core/float3.cs @@ -539,14 +539,12 @@ public static float3 Clamp(float3 vec, float3 min, float3 max) /// 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; } @@ -586,6 +584,7 @@ public static float3[] OrthoNormalize(float3 vecOne, float3 vecTwo) /// 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; @@ -679,7 +678,7 @@ public static float3 Lerp(float3 a, float3 b, float blend) /// /// First input vector /// Second input vector - /// The blend factor. a when blend=0, b when blend=1. + /// The blend factor. a when blend=0, b when blend=1. public static float3 Lerp(float3 a, float3 b, float3 blend) { a.x = blend.x * (b.x - a.x) + a.x; diff --git a/src/Math/Core/float4.cs b/src/Math/Core/float4.cs index 857d8d358f..03bbf11234 100644 --- a/src/Math/Core/float4.cs +++ b/src/Math/Core/float4.cs @@ -316,7 +316,7 @@ public float4 Round() #endregion public Round() /// - /// 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. /// /// public float4 LinearColorFromSRgb() @@ -492,6 +492,7 @@ public static float4 Clamp(float4 vec, float4 min, float4 max) /// The normalized vector 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; @@ -511,6 +512,7 @@ public static float4 Normalize(float4 vec) /// The scaled vector. 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; @@ -530,6 +532,7 @@ public static float4 Normalize1(float4 vec) /// The normalized vector 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; @@ -600,7 +603,7 @@ public static float4 Lerp(float4 a, float4 b, float blend) /// /// First input vector /// Second input vector - /// The blend factor. a when blend=0, b when blend=1. + /// The blend factor. a when blend=0, b when blend=1. public static float4 Lerp(float4 a, float4 b, float4 blend) { a.x = blend.x * (b.x - a.x) + a.x; diff --git a/src/Math/Core/int2.cs b/src/Math/Core/int2.cs index 27ab9081df..69cdfd10bb 100644 --- a/src/Math/Core/int2.cs +++ b/src/Math/Core/int2.cs @@ -157,7 +157,7 @@ public int this[int idx] /// /// Scales the int2 to unit length. /// - public float2 Normalize() + public int2 Normalize() { return Normalize(this); } @@ -169,7 +169,7 @@ public float2 Normalize() /// /// Scales the int2 to approximately unit length. /// - public float2 NormalizeFast() + public int2 NormalizeFast() { return NormalizeFast(this); } @@ -416,14 +416,15 @@ public static int2 Clamp(int2 vec, int2 min, int2 max) /// /// The input vector /// The normalized vector - public static float2 Normalize(int2 vec) + public static int2 Normalize(int2 vec) { + if (vec.Length <= M.EpsilonFloat) return Zero; float scale = 1.0f / vec.Length; - return new float2() + return new int2() { - x = vec.x * scale, - y = vec.y * scale + x = (int)(vec.x * scale), + y = (int)(vec.y * scale) }; } #endregion Normalize @@ -435,13 +436,14 @@ public static float2 Normalize(int2 vec) /// /// The input vector /// The normalized vector - public static float2 NormalizeFast(int2 vec) + public static int2 NormalizeFast(int2 vec) { + if (vec.Length <= M.EpsilonFloat) return Zero; float scale = M.InverseSqrtFast(vec.x * vec.x + vec.y * vec.y); - return new float2() + return new int2() { - x = vec.x * scale, - y = vec.y * scale + x = (int)(vec.x * scale), + y = (int)(vec.y * scale) }; } @@ -493,7 +495,7 @@ public static int2 Pow(int2 val, int exp) /// /// First input vector /// Second input vector - /// The blend factor. a when blend=0, b when blend=1. + /// The blend factor. a when blend=0, b when blend=1. public static float2 Lerp(int2 a, int2 b, float blend) { return new float2() @@ -509,7 +511,7 @@ public static float2 Lerp(int2 a, int2 b, float blend) /// /// First input vector /// Second input vector - /// The blend factor. a when blend=0, b when blend=1. + /// The blend factor. a when blend=0, b when blend=1. public static float2 Lerp(int2 a, int2 b, float2 blend) { return new float2() diff --git a/src/Math/Core/int3.cs b/src/Math/Core/int3.cs index 875ee35190..95ec6e3280 100644 --- a/src/Math/Core/int3.cs +++ b/src/Math/Core/int3.cs @@ -202,7 +202,7 @@ public int this[int idx] /// /// Scales the int3 to unit length. /// - public float3 Normalize() + public int3 Normalize() { return Normalize(this); } @@ -214,7 +214,7 @@ public float3 Normalize() /// /// Scales the int3 to approximately unit length. /// - public float3 NormalizeFast() + public int3 NormalizeFast() { return NormalizeFast(this); } @@ -515,15 +515,17 @@ public static int3 Clamp(int3 vec, int3 min, int3 max) /// /// The input vector /// The normalized vector - public static float3 Normalize(int3 vec) + public static int3 Normalize(int3 vec) { + if (vec.Length <= M.EpsilonFloat) return Zero; + float scale = 1.0f / vec.Length; - return new float3() + return new int3() { - x = vec.x * scale, - y = vec.y * scale, - z = vec.z * scale + x = (int)(vec.x * scale), + y = (int)(vec.y * scale), + z = (int)(vec.z * scale) }; } #endregion Normalize @@ -535,14 +537,15 @@ public static float3 Normalize(int3 vec) /// /// The input vector /// The normalized vector - public static float3 NormalizeFast(int3 vec) + public static int3 NormalizeFast(int3 vec) { + if (vec.Length <= M.EpsilonFloat) return Zero; float scale = M.InverseSqrtFast(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z); - return new float3() + return new int3() { - x = vec.x * scale, - y = vec.y * scale, - z = vec.z * scale, + x = (int)(vec.x * scale), + y = (int)(vec.y * scale), + z = (int)(vec.z * scale) }; } @@ -615,7 +618,7 @@ public static int3 Pow(int3 val, int exp) /// /// First input vector /// Second input vector - /// The blend factor. a when blend=0, b when blend=1. + /// The blend factor. a when blend=0, b when blend=1. public static float3 Lerp(int3 a, int3 b, float blend) { return new float3() @@ -632,7 +635,7 @@ public static float3 Lerp(int3 a, int3 b, float blend) /// /// First input vector /// Second input vector - /// The blend factor. a when blend=0, b when blend=1. + /// The blend factor. a when blend=0, b when blend=1. public static float3 Lerp(int3 a, int3 b, float3 blend) { return new float3() @@ -643,7 +646,7 @@ public static float3 Lerp(int3 a, int3 b, float3 blend) }; } - #endregion Lerp + #endregion Lerp #region CalculateAngle diff --git a/src/Math/Core/int4.cs b/src/Math/Core/int4.cs index 155ebf745a..51f2ef27d8 100644 --- a/src/Math/Core/int4.cs +++ b/src/Math/Core/int4.cs @@ -253,7 +253,7 @@ public int this[int idx] /// /// Scales the int4 to unit length. /// - public float4 Normalize() + public int4 Normalize() { return Normalize(this); } @@ -265,7 +265,7 @@ public float4 Normalize() /// /// Scales the int4 to unit length in 1-norm. /// - public float4 Normalize1() + public int4 Normalize1() { return Normalize1(this); } @@ -277,7 +277,7 @@ public float4 Normalize1() /// /// Scales the int4 to approximately unit length. /// - public float4 NormalizeFast() + public int4 NormalizeFast() { return NormalizeFast(this); } @@ -298,7 +298,7 @@ public int[] ToArray() #endregion public int[] ToArray() /// - /// 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. /// /// public int4 LinearColorFromSRgb() @@ -472,16 +472,18 @@ public static int4 Clamp(int4 vec, int4 min, int4 max) /// /// The input vector /// The normalized vector - public static float4 Normalize(int4 vec) + public static int4 Normalize(int4 vec) { + if (vec.Length <= M.EpsilonFloat) return Zero; + float scale = 1.0f / vec.Length; - return new float4() + return new int4() { - x = vec.x * scale, - y = vec.y * scale, - z = vec.z * scale, - w = vec.w * scale + x = (int)(vec.x * scale), + y = (int)(vec.y * scale), + z = (int)(vec.z * scale), + w = (int)(vec.w * scale) }; } @@ -494,16 +496,18 @@ public static float4 Normalize(int4 vec) /// /// The input vector. /// The scaled vector. - public static float4 Normalize1(int4 vec) + public static int4 Normalize1(int4 vec) { + if (vec.Length <= M.EpsilonFloat) return Zero; + float scale = 1.0f / vec.Length1; - return new float4() + return new int4() { - x = vec.x * scale, - y = vec.y * scale, - z = vec.z * scale, - w = vec.w * scale + x = (int)(vec.x * scale), + y = (int)(vec.y * scale), + z = (int)(vec.z * scale), + w = (int)(vec.w * scale) }; } @@ -516,15 +520,16 @@ public static float4 Normalize1(int4 vec) /// /// The input vector /// The normalized vector - public static float4 NormalizeFast(int4 vec) + public static int4 NormalizeFast(int4 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); - return new float4() + return new int4() { - x = vec.x * scale, - y = vec.y * scale, - z = vec.z * scale, - w = vec.w * scale + x = (int)(vec.x * scale), + y = (int)(vec.y * scale), + z = (int)(vec.z * scale), + w = (int)(vec.w * scale) }; } @@ -592,7 +597,7 @@ public static float4 Lerp(int4 a, int4 b, float blend) /// /// First input vector /// Second input vector - /// The blend factor. a when blend=0, b when blend=1. + /// The blend factor. a when blend=0, b when blend=1. public static float4 Lerp(int4 a, int4 b, float4 blend) { return new float4() From 89b02ef91afd1b014991e2b46518c408e7f3af2e Mon Sep 17 00:00:00 2001 From: Moritz Roetner Date: Fri, 29 Apr 2022 14:59:43 +0200 Subject: [PATCH 2/2] Reversed int2/3/4 normalize, added unit tests --- src/Math/Core/int2.cs | 26 ++++++++-------- src/Math/Core/int3.cs | 29 +++++++++--------- src/Math/Core/int4.cs | 49 ++++++++++++++++--------------- src/Tests/Math/Core/Float2Test.cs | 1 + src/Tests/Math/Core/Float3Test.cs | 1 + src/Tests/Math/Core/Float4Test.cs | 1 + src/Tests/Math/Core/Int2Test.cs | 2 ++ src/Tests/Math/Core/Int3Test.cs | 1 + src/Tests/Math/Core/Int4Test.cs | 1 + 9 files changed, 61 insertions(+), 50 deletions(-) diff --git a/src/Math/Core/int2.cs b/src/Math/Core/int2.cs index 69cdfd10bb..f1d2b11222 100644 --- a/src/Math/Core/int2.cs +++ b/src/Math/Core/int2.cs @@ -157,7 +157,7 @@ public int this[int idx] /// /// Scales the int2 to unit length. /// - public int2 Normalize() + public float2 Normalize() { return Normalize(this); } @@ -169,7 +169,7 @@ public int2 Normalize() /// /// Scales the int2 to approximately unit length. /// - public int2 NormalizeFast() + public float2 NormalizeFast() { return NormalizeFast(this); } @@ -416,15 +416,16 @@ public static int2 Clamp(int2 vec, int2 min, int2 max) /// /// The input vector /// The normalized vector - public static int2 Normalize(int2 vec) + public static float2 Normalize(int2 vec) { - if (vec.Length <= M.EpsilonFloat) return Zero; + if (vec.Length <= M.EpsilonFloat) return float2.Zero; + float scale = 1.0f / vec.Length; - return new int2() + return new float2() { - x = (int)(vec.x * scale), - y = (int)(vec.y * scale) + x = vec.x * scale, + y = vec.y * scale }; } #endregion Normalize @@ -436,14 +437,15 @@ public static int2 Normalize(int2 vec) /// /// The input vector /// The normalized vector - public static int2 NormalizeFast(int2 vec) + public static float2 NormalizeFast(int2 vec) { - if (vec.Length <= M.EpsilonFloat) return Zero; + if (vec.Length <= M.EpsilonFloat) return float2.Zero; + float scale = M.InverseSqrtFast(vec.x * vec.x + vec.y * vec.y); - return new int2() + return new float2() { - x = (int)(vec.x * scale), - y = (int)(vec.y * scale) + x = vec.x * scale, + y = vec.y * scale }; } diff --git a/src/Math/Core/int3.cs b/src/Math/Core/int3.cs index 95ec6e3280..54033be92b 100644 --- a/src/Math/Core/int3.cs +++ b/src/Math/Core/int3.cs @@ -202,7 +202,7 @@ public int this[int idx] /// /// Scales the int3 to unit length. /// - public int3 Normalize() + public float3 Normalize() { return Normalize(this); } @@ -214,7 +214,7 @@ public int3 Normalize() /// /// Scales the int3 to approximately unit length. /// - public int3 NormalizeFast() + public float3 NormalizeFast() { return NormalizeFast(this); } @@ -515,17 +515,17 @@ public static int3 Clamp(int3 vec, int3 min, int3 max) /// /// The input vector /// The normalized vector - public static int3 Normalize(int3 vec) + public static float3 Normalize(int3 vec) { - if (vec.Length <= M.EpsilonFloat) return Zero; + if (vec.Length <= M.EpsilonFloat) return float3.Zero; float scale = 1.0f / vec.Length; - return new int3() + return new float3() { - x = (int)(vec.x * scale), - y = (int)(vec.y * scale), - z = (int)(vec.z * scale) + x = vec.x * scale, + y = vec.y * scale, + z = vec.z * scale }; } #endregion Normalize @@ -537,15 +537,16 @@ public static int3 Normalize(int3 vec) /// /// The input vector /// The normalized vector - public static int3 NormalizeFast(int3 vec) + public static float3 NormalizeFast(int3 vec) { - if (vec.Length <= M.EpsilonFloat) return Zero; + 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 int3() + return new float3() { - x = (int)(vec.x * scale), - y = (int)(vec.y * scale), - z = (int)(vec.z * scale) + x = vec.x * scale, + y = vec.y * scale, + z = vec.z * scale, }; } diff --git a/src/Math/Core/int4.cs b/src/Math/Core/int4.cs index 51f2ef27d8..a3900f4a3d 100644 --- a/src/Math/Core/int4.cs +++ b/src/Math/Core/int4.cs @@ -253,7 +253,7 @@ public int this[int idx] /// /// Scales the int4 to unit length. /// - public int4 Normalize() + public float4 Normalize() { return Normalize(this); } @@ -265,7 +265,7 @@ public int4 Normalize() /// /// Scales the int4 to unit length in 1-norm. /// - public int4 Normalize1() + public float4 Normalize1() { return Normalize1(this); } @@ -277,7 +277,7 @@ public int4 Normalize1() /// /// Scales the int4 to approximately unit length. /// - public int4 NormalizeFast() + public float4 NormalizeFast() { return NormalizeFast(this); } @@ -472,18 +472,18 @@ public static int4 Clamp(int4 vec, int4 min, int4 max) /// /// The input vector /// The normalized vector - public static int4 Normalize(int4 vec) + public static float4 Normalize(int4 vec) { - if (vec.Length <= M.EpsilonFloat) return Zero; + if (vec.Length <= M.EpsilonFloat) return float4.Zero; float scale = 1.0f / vec.Length; - return new int4() + return new float4() { - x = (int)(vec.x * scale), - y = (int)(vec.y * scale), - z = (int)(vec.z * scale), - w = (int)(vec.w * scale) + x = vec.x * scale, + y = vec.y * scale, + z = vec.z * scale, + w = vec.w * scale }; } @@ -496,18 +496,18 @@ public static int4 Normalize(int4 vec) /// /// The input vector. /// The scaled vector. - public static int4 Normalize1(int4 vec) + public static float4 Normalize1(int4 vec) { - if (vec.Length <= M.EpsilonFloat) return Zero; + if (vec.Length <= M.EpsilonFloat) return float4.Zero; float scale = 1.0f / vec.Length1; - return new int4() + return new float4() { - x = (int)(vec.x * scale), - y = (int)(vec.y * scale), - z = (int)(vec.z * scale), - w = (int)(vec.w * scale) + x = vec.x * scale, + y = vec.y * scale, + z = vec.z * scale, + w = vec.w * scale }; } @@ -520,16 +520,17 @@ public static int4 Normalize1(int4 vec) /// /// The input vector /// The normalized vector - public static int4 NormalizeFast(int4 vec) + public static float4 NormalizeFast(int4 vec) { - if (vec.Length <= M.EpsilonFloat) return Zero; + 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 int4() + return new float4() { - x = (int)(vec.x * scale), - y = (int)(vec.y * scale), - z = (int)(vec.z * scale), - w = (int)(vec.w * scale) + x = vec.x * scale, + y = vec.y * scale, + z = vec.z * scale, + w = vec.w * scale }; } diff --git a/src/Tests/Math/Core/Float2Test.cs b/src/Tests/Math/Core/Float2Test.cs index bcb5463e2c..7771c77ee4 100644 --- a/src/Tests/Math/Core/Float2Test.cs +++ b/src/Tests/Math/Core/Float2Test.cs @@ -576,6 +576,7 @@ public static IEnumerable 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 GetAddition() diff --git a/src/Tests/Math/Core/Float3Test.cs b/src/Tests/Math/Core/Float3Test.cs index 721878152d..bfdf0738e2 100644 --- a/src/Tests/Math/Core/Float3Test.cs +++ b/src/Tests/Math/Core/Float3Test.cs @@ -759,6 +759,7 @@ public static IEnumerable 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 GetAddition() diff --git a/src/Tests/Math/Core/Float4Test.cs b/src/Tests/Math/Core/Float4Test.cs index 87c9114de4..e81f822f45 100644 --- a/src/Tests/Math/Core/Float4Test.cs +++ b/src/Tests/Math/Core/Float4Test.cs @@ -654,6 +654,7 @@ public static IEnumerable 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 GetAddition() diff --git a/src/Tests/Math/Core/Int2Test.cs b/src/Tests/Math/Core/Int2Test.cs index 456ddc32ff..e0c0d23d2c 100644 --- a/src/Tests/Math/Core/Int2Test.cs +++ b/src/Tests/Math/Core/Int2Test.cs @@ -525,6 +525,8 @@ public static IEnumerable 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 GetAddition() diff --git a/src/Tests/Math/Core/Int3Test.cs b/src/Tests/Math/Core/Int3Test.cs index 858dba8d54..e08c6e4473 100644 --- a/src/Tests/Math/Core/Int3Test.cs +++ b/src/Tests/Math/Core/Int3Test.cs @@ -721,6 +721,7 @@ public static IEnumerable 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 GetAddition() diff --git a/src/Tests/Math/Core/Int4Test.cs b/src/Tests/Math/Core/Int4Test.cs index c2e59b79b4..0025d8240b 100644 --- a/src/Tests/Math/Core/Int4Test.cs +++ b/src/Tests/Math/Core/Int4Test.cs @@ -617,6 +617,7 @@ public static IEnumerable 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 GetAddition()