| | 1 | | using System.Runtime.InteropServices; |
| | 2 | |
|
| | 3 | | namespace ReFlex.Core.Common.Components |
| | 4 | | { |
| | 5 | | /// <summary> |
| | 6 | | /// Offers some useful and fast mathematical functions |
| | 7 | | /// </summary> |
| | 8 | | public class Math |
| | 9 | | { |
| | 10 | | #region methods |
| | 11 | |
|
| | 12 | | /// <summary> |
| | 13 | | /// very roughly Approximates the square root of a number z. Variance: approx. 5%. |
| | 14 | | /// </summary> |
| | 15 | | /// <param name="z">The radicand</param> |
| | 16 | | /// <returns>The square root of a number z, <see cref="float.NaN"/> if z is negative.</returns> |
| | 17 | | public static float FastSqrt(float z) |
| 13 | 18 | | { |
| 14 | 19 | | if (z == 0) return 0; |
| | 20 | | FloatIntUnion u; |
| 12 | 21 | | u.tmp = 0; |
| 12 | 22 | | u.f = z; |
| 12 | 23 | | u.tmp -= 1 << 23; |
| 12 | 24 | | u.tmp >>= 1; |
| 12 | 25 | | u.tmp += 1 << 29; |
| 12 | 26 | | return u.f; |
| 13 | 27 | | } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Approximates the square root of a number z. Variance: approx. 2% |
| | 31 | | /// It’s a tad slower than the first (though still nearly 2x as fast as Math.Sqrt()), but much more accurate. |
| | 32 | | /// </summary> |
| | 33 | | /// <param name="z">The radicand</param> |
| | 34 | | /// <returns>The square root of a number z, <see cref="float.NegativeInfinity"/> if z is negative.</returns> |
| | 35 | | public static float InverseSqrt(float z) |
| 13 | 36 | | { |
| 14 | 37 | | if (z == 0) return 0; |
| | 38 | | FloatIntUnion u; |
| 12 | 39 | | u.tmp = 0; |
| 12 | 40 | | var half = 0.5f * z; |
| 12 | 41 | | u.f = z; |
| 12 | 42 | | u.tmp = 0x5f375a86 - (u.tmp >> 1); |
| 12 | 43 | | u.f = u.f * (1.5f - half * u.f * u.f); |
| 12 | 44 | | return u.f * z; |
| 13 | 45 | | } |
| | 46 | |
|
| | 47 | | #endregion |
| | 48 | |
|
| | 49 | | [StructLayout(LayoutKind.Explicit)] |
| | 50 | | private struct FloatIntUnion |
| | 51 | | { |
| | 52 | | [FieldOffset(0)] |
| | 53 | | public float f; |
| | 54 | |
|
| | 55 | | [FieldOffset(0)] |
| | 56 | | public int tmp; |
| | 57 | | } |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// Remaps the specified value from one range to another range. |
| | 61 | | /// </summary> |
| | 62 | | /// <param name="value">The value.</param> |
| | 63 | | /// <param name="from1">The from1.</param> |
| | 64 | | /// <param name="to1">The to1.</param> |
| | 65 | | /// <param name="from2">The from2.</param> |
| | 66 | | /// <param name="to2">The to2.</param> |
| | 67 | | /// <returns>The remapped value.</returns> |
| | 68 | | public static double Remap(double value, double from1, double to1, double from2, double to2) |
| 7 | 69 | | => (value - from1) / (to1 - from1) * (to2 - from2) + from2; |
| | 70 | |
|
| | 71 | | public static double ExponentialMapping(double input) => |
| 4 | 72 | | System.Math.Exp((input) - 1) / (System.Math.E - 1); |
| | 73 | |
|
| | 74 | | public static double LogarithmicMapping(double input) => |
| 6 | 75 | | System.Math.Log(input + 1, System.Math.E) / System.Math.Log(2, System.Math.E); |
| | 76 | | } |
| | 77 | | } |