1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-22 19:28:48 +00:00

feat: add MathUtility.Bias (#60)

This commit is contained in:
Oliver Booth 2023-04-03 14:41:32 +01:00
parent a748010a38
commit f131c281cf
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
3 changed files with 50 additions and 0 deletions

View File

@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added new library X10D.Hosting. - Added new library X10D.Hosting.
- Added .NET 7 target. - Added .NET 7 target.
- X10D: Added `IntrinsicExtensions` and `IntrinsicUtility` which offer methods for vectors in `System.Runtime.Instrinsics`. (#70) - 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.InverseLerp(float, float, float)` and `MathUtility.InverseLerp(double, double, double)`
- X10D: Added `MathUtility.ScaleRange(float, float, float, float, float)` - X10D: Added `MathUtility.ScaleRange(float, float, float, float, float)`
and `MathUtility.ScaleRange(double, double, double, double, double)` and `MathUtility.ScaleRange(double, double, double, double, double)`

View File

@ -9,6 +9,41 @@ namespace X10D.Tests.Math;
[TestClass] [TestClass]
public class MathUtilityTests 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] [TestMethod]
public void GammaToLinear_ShouldReturnQuarter_GivenQuarterAndGamma1() public void GammaToLinear_ShouldReturnQuarter_GivenQuarterAndGamma1()
{ {

View File

@ -11,6 +11,20 @@ public static class MathUtility
private const double DefaultGamma = 2.2; private const double DefaultGamma = 2.2;
private const float DefaultGammaF = 2.2f; private const float DefaultGammaF = 2.2f;
/// <summary>
/// Applies a simple bias function to value.
/// </summary>
/// <param name="value">The value to which the bias function will be applied.</param>
/// <param name="bias">The bias value. Valid values range from 0-1.</param>
/// <returns>The biased result.</returns>
/// <remarks>
/// If <paramref name="bias" /> is less than 0.5, <paramref name="value" /> will be shifted downward; otherwise, upward.
/// </remarks>
public static float Bias(float value, float bias)
{
return value / ((1.0f / bias - 2.0f) * (1.0f - value) + 1.0f);
}
/// <summary> /// <summary>
/// Converts a gamma-encoded value to a linear value using a gamma value of <c>2.2</c>. /// Converts a gamma-encoded value to a linear value using a gamma value of <c>2.2</c>.
/// </summary> /// </summary>