diff --git a/CHANGELOG.md b/CHANGELOG.md index 56a9bc8..f24ec36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added new library X10D.Hosting. - Added .NET 7 target. - X10D: Added `IntrinsicExtensions` and `IntrinsicUtility` which offer methods for vectors in `System.Runtime.Instrinsics`. (#70) +- X10D: Added `MathUtility.Bias(float, float)`. - X10D: Added `MathUtility.InverseLerp(float, float, float)` and `MathUtility.InverseLerp(double, double, double)` - X10D: Added `MathUtility.ScaleRange(float, float, float, float, float)` and `MathUtility.ScaleRange(double, double, double, double, double)` diff --git a/X10D.Tests/src/Math/MathUtilityTests.cs b/X10D.Tests/src/Math/MathUtilityTests.cs index d8b8c7d..e267adf 100644 --- a/X10D.Tests/src/Math/MathUtilityTests.cs +++ b/X10D.Tests/src/Math/MathUtilityTests.cs @@ -9,6 +9,41 @@ namespace X10D.Tests.Math; [TestClass] public class MathUtilityTests { + [TestMethod] + public void Bias_ReturnsCorrectResult_WhenBiasIsLessThanPointFive() + { + const float value = 0.5f; + const float bias = 0.3f; + + const float expected = 0.3f; + float result = MathUtility.Bias(value, bias); + + Assert.AreEqual(expected, result, 1e-6f); + } + + [TestMethod] + public void Bias_ReturnsCorrectResult_WhenBiasIsEqualToPointFive() + { + const float value = 0.5f; + const float bias = 0.5f; + + float result = MathUtility.Bias(value, bias); + + Assert.AreEqual(value, result, 1e-6f); + } + + [TestMethod] + public void Bias_ReturnsCorrectResult_WhenBiasIsGreaterThanPointFive() + { + const float value = 0.5f; + const float bias = 0.8f; + + const float expected = 0.8f; + float result = MathUtility.Bias(value, bias); + + Assert.AreEqual(expected, result, 1e-6f); + } + [TestMethod] public void GammaToLinear_ShouldReturnQuarter_GivenQuarterAndGamma1() { diff --git a/X10D/src/Math/MathUtility.cs b/X10D/src/Math/MathUtility.cs index 561157c..f292b43 100644 --- a/X10D/src/Math/MathUtility.cs +++ b/X10D/src/Math/MathUtility.cs @@ -11,6 +11,20 @@ public static class MathUtility private const double DefaultGamma = 2.2; private const float DefaultGammaF = 2.2f; + /// + /// Applies a simple bias function to value. + /// + /// The value to which the bias function will be applied. + /// The bias value. Valid values range from 0-1. + /// The biased result. + /// + /// If is less than 0.5, will be shifted downward; otherwise, upward. + /// + public static float Bias(float value, float bias) + { + return value / ((1.0f / bias - 2.0f) * (1.0f - value) + 1.0f); + } + /// /// Converts a gamma-encoded value to a linear value using a gamma value of 2.2. ///