From 10df97aea763469465d10e63047a4ae143aac2a2 Mon Sep 17 00:00:00 2001 From: Anthony Truskinger Date: Tue, 12 May 2020 20:27:54 +1000 Subject: [PATCH] Adds more extension methods to interval 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 --- .../Extensions/IntervalExtensions.cs | 70 ++++++++++++++++++- .../Extensions/MathExtensions.cs | 2 +- src/AudioAnalysisTools/Events/EventCommon.cs | 2 +- 3 files changed, 71 insertions(+), 3 deletions(-) diff --git a/src/Acoustics.Shared/Extensions/IntervalExtensions.cs b/src/Acoustics.Shared/Extensions/IntervalExtensions.cs index cd4ce9b5b..b2a7cea80 100644 --- a/src/Acoustics.Shared/Extensions/IntervalExtensions.cs +++ b/src/Acoustics.Shared/Extensions/IntervalExtensions.cs @@ -6,6 +6,7 @@ namespace Acoustics.Shared { using System; + using System.Runtime.CompilerServices; public static class IntervalExtensions { @@ -153,11 +154,78 @@ public static Interval Invert(this Interval range) return new Interval(1 / range.Maximum, 1 / range.Minimum, range.Topology); } - public static double Normalize(this Interval range, double value) + /// + /// Normalizes a value as a unit value given the bounds of an interval. + /// + /// The interval to use as the bounds. + /// The value to normalize. + /// A value scaled to [0,1]. The value may exceed the bounds [0,1]; i.e. the value is not clamped. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static double NormalizeValue(this Interval range, double value) + { + return (value - range.Minimum) / (range.Maximum - range.Minimum); + } + + /// + /// Normalizes a value as a unit value given the bounds of an interval. + /// + /// The value to normalize. + /// The interval to use as the bounds. + /// A value scaled to [0,1]. The value may exceed the bounds [0,1]; i.e. the value is not clamped. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static double Normalize(this double value, Interval range) { return (value - range.Minimum) / (range.Maximum - range.Minimum); } + /// + /// Restricts a to be within a specified range. + /// + /// The The value to clamp. + /// The interval to clamp the value to. + /// + /// The representing the clamped value. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static double Clamp(this double value, Interval range) + { + if (value >= range.Maximum) + { + return range.Maximum; + } + + if (value <= range.Minimum) + { + return range.Minimum; + } + + return value; + } + + /// + /// Restricts a to be within a specified range. + /// + /// The interval to clamp the value to. + /// The The value to clamp. + /// + /// The representing the clamped value. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static double ClampvValue(this Interval range, double value) + { + if (value >= range.Maximum) + { + return range.Maximum; + } + + if (value <= range.Minimum) + { + return range.Minimum; + } + + return value; + } + public static Interval AsInterval(this (T Minimum, T Maximum) pair, Topology topology = Topology.Default) where T : struct, IComparable { diff --git a/src/Acoustics.Shared/Extensions/MathExtensions.cs b/src/Acoustics.Shared/Extensions/MathExtensions.cs index 00855215f..6ad017985 100644 --- a/src/Acoustics.Shared/Extensions/MathExtensions.cs +++ b/src/Acoustics.Shared/Extensions/MathExtensions.cs @@ -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); } diff --git a/src/AudioAnalysisTools/Events/EventCommon.cs b/src/AudioAnalysisTools/Events/EventCommon.cs index e3ddcab7e..2696dbffb 100644 --- a/src/AudioAnalysisTools/Events/EventCommon.cs +++ b/src/AudioAnalysisTools/Events/EventCommon.cs @@ -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. /// - public virtual double ScoreNormalized => this.ScoreRange.Normalize(this.Score); + public virtual double ScoreNormalized => this.ScoreRange.NormalizeValue(this.Score); /// /// Draw this event on an image.