Skip to content

Commit

Permalink
Adds more extension methods to interval
Browse files Browse the repository at this point in the history
Handy extension methods for doubles and intervals for clamping and normalizing

Also fixes a bug in double.Clamp() - we used the wrong condition to reorder bounds. This is what motivated adding interval clamp methods... because interval's bounds are always ordered
  • Loading branch information
atruskie committed May 12, 2020
1 parent 587d645 commit 10df97a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
70 changes: 69 additions & 1 deletion src/Acoustics.Shared/Extensions/IntervalExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Acoustics.Shared
{
using System;
using System.Runtime.CompilerServices;

public static class IntervalExtensions
{
Expand Down Expand Up @@ -153,11 +154,78 @@ public static Interval<double> Invert(this Interval<double> range)
return new Interval<double>(1 / range.Maximum, 1 / range.Minimum, range.Topology);
}

public static double Normalize(this Interval<double> range, double value)
/// <summary>
/// Normalizes a value as a unit value given the bounds of an interval.
/// </summary>
/// <param name="range">The interval to use as the bounds.</param>
/// <param name="value">The value to normalize.</param>
/// <returns>A value scaled to [0,1]. The value may exceed the bounds [0,1]; i.e. the value is not clamped.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double NormalizeValue(this Interval<double> range, double value)
{
return (value - range.Minimum) / (range.Maximum - range.Minimum);
}

/// <summary>
/// Normalizes a value as a unit value given the bounds of an interval.
/// </summary>
/// <param name="value">The value to normalize.</param>
/// <param name="range">The interval to use as the bounds.</param>
/// <returns>A value scaled to [0,1]. The value may exceed the bounds [0,1]; i.e. the value is not clamped.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Normalize(this double value, Interval<double> range)
{
return (value - range.Minimum) / (range.Maximum - range.Minimum);
}

/// <summary>
/// Restricts a <see cref="double"/> to be within a specified range.
/// </summary>
/// <param name="value">The The value to clamp.</param>
/// <param name="range">The interval to clamp the value to.</param>
/// <returns>
/// The <see cref="double"/> representing the clamped value.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Clamp(this double value, Interval<double> range)
{
if (value >= range.Maximum)
{
return range.Maximum;
}

if (value <= range.Minimum)
{
return range.Minimum;
}

return value;
}

/// <summary>
/// Restricts a <see cref="double"/> to be within a specified range.
/// </summary>
/// <param name="range">The interval to clamp the value to.</param>
/// <param name="value">The The value to clamp.</param>
/// <returns>
/// The <see cref="double"/> representing the clamped value.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double ClampvValue(this Interval<double> range, double value)
{
if (value >= range.Maximum)
{
return range.Maximum;
}

if (value <= range.Minimum)
{
return range.Minimum;
}

return value;
}

public static Interval<T> AsInterval<T>(this (T Minimum, T Maximum) pair, Topology topology = Topology.Default)
where T : struct, IComparable<T>
{
Expand Down
2 changes: 1 addition & 1 deletion src/Acoustics.Shared/Extensions/MathExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public static float Clamp(this float value, float min, float max)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Clamp(this double value, double min, double max)
{
if (min < max)
if (min > max)
{
(min, max) = (max, min);
}
Expand Down
2 changes: 1 addition & 1 deletion src/AudioAnalysisTools/Events/EventCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public abstract class EventCommon : EventBase, IDrawableEvent
/// Gets a score in the range [0, 1].
/// Up to user to determine a suitable range maximum.
/// </summary>
public virtual double ScoreNormalized => this.ScoreRange.Normalize(this.Score);
public virtual double ScoreNormalized => this.ScoreRange.NormalizeValue(this.Score);

/// <summary>
/// Draw this event on an image.
Expand Down

0 comments on commit 10df97a

Please sign in to comment.