| | | 1 | | //======================================================================= |
| | | 2 | | // Fixed64.Extensions.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024–present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | using System.Runtime.CompilerServices; |
| | | 10 | | |
| | | 11 | | namespace FixedMathSharp; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Provides extension methods for the Fixed64 type, enabling additional mathematical, conversion, and comparison operat |
| | | 15 | | /// </summary> |
| | | 16 | | public static class Fixed64Extensions |
| | | 17 | | { |
| | | 18 | | #region Fixed64 Operations |
| | | 19 | | |
| | | 20 | | /// <inheritdoc cref="Fixed64.Sign(Fixed64)" /> |
| | | 21 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 14 | 22 | | public static int Sign(this Fixed64 value) => Fixed64.Sign(value); |
| | | 23 | | |
| | | 24 | | /// <inheritdoc cref="Fixed64.IsInteger(Fixed64)" /> |
| | | 25 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 6 | 26 | | public static bool IsInteger(this Fixed64 value) => Fixed64.IsInteger(value); |
| | | 27 | | |
| | | 28 | | /// <inheritdoc cref="FixedMath.Squared(Fixed64)" /> |
| | | 29 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 30 | | public static Fixed64 Squared(this Fixed64 value) => FixedMath.Squared(value); |
| | | 31 | | |
| | | 32 | | /// <inheritdoc cref="FixedMath.Round(Fixed64, MidpointRounding)" /> |
| | | 33 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 34 | | public static Fixed64 Round(this Fixed64 value, MidpointRounding mode = MidpointRounding.ToEven) => |
| | 2 | 35 | | FixedMath.Round(value, mode); |
| | | 36 | | |
| | | 37 | | /// <inheritdoc cref="FixedMath.RoundToPrecision(Fixed64, int, MidpointRounding)" /> |
| | | 38 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 39 | | public static Fixed64 RoundToPrecision(this Fixed64 value, int places, MidpointRounding mode = MidpointRounding.ToEv |
| | 1 | 40 | | FixedMath.RoundToPrecision(value, places, mode); |
| | | 41 | | |
| | | 42 | | /// <inheritdoc cref="FixedMath.Clamp(Fixed64, Fixed64, Fixed64)" /> |
| | | 43 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 44 | | public static Fixed64 Clamp(this Fixed64 value, Fixed64 min, Fixed64 max) => FixedMath.Clamp(value, min, max); |
| | | 45 | | |
| | | 46 | | /// <inheritdoc cref="FixedMath.ClampOne(Fixed64)" /> |
| | | 47 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 48 | | public static Fixed64 ClampOne(this Fixed64 f1) => FixedMath.ClampOne(f1); |
| | | 49 | | |
| | | 50 | | /// <inheritdoc cref="FixedMath.Clamp01(Fixed64)" /> |
| | | 51 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 52 | | public static Fixed64 Clamp01(this Fixed64 f1) => FixedMath.Clamp01(f1); |
| | | 53 | | |
| | | 54 | | /// <inheritdoc cref="FixedMath.Abs(Fixed64)" /> |
| | | 55 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 3649 | 56 | | public static Fixed64 Abs(this Fixed64 value) => FixedMath.Abs(value); |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Checks if the absolute value of x is less than y. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="x">The value to compare.</param> |
| | | 62 | | /// <param name="y">The comparison threshold.</param> |
| | | 63 | | /// <returns>True if |x| < y; otherwise false.</returns> |
| | | 64 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 65 | | public static bool AbsLessThan(this Fixed64 x, Fixed64 y) => Abs(x) < y; |
| | | 66 | | |
| | | 67 | | /// <inheritdoc cref="FixedMath.FastAdd(Fixed64, Fixed64)" /> |
| | | 68 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 69 | | public static Fixed64 FastAdd(this Fixed64 a, Fixed64 b) => FixedMath.FastAdd(a, b); |
| | | 70 | | |
| | | 71 | | /// <inheritdoc cref="FixedMath.FastSub(Fixed64, Fixed64)" /> |
| | | 72 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 73 | | public static Fixed64 FastSub(this Fixed64 a, Fixed64 b) => FixedMath.FastSub(a, b); |
| | | 74 | | |
| | | 75 | | /// <inheritdoc cref="FixedMath.FastMul(Fixed64, Fixed64)" /> |
| | | 76 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 77 | | public static Fixed64 FastMul(this Fixed64 a, Fixed64 b) => FixedMath.FastMul(a, b); |
| | | 78 | | |
| | | 79 | | /// <inheritdoc cref="FixedMath.FastDiv(Fixed64, Fixed64)" /> |
| | | 80 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 81 | | public static Fixed64 FastDiv(this Fixed64 a, Fixed64 b) => FixedMath.FastDiv(a, b); |
| | | 82 | | |
| | | 83 | | /// <inheritdoc cref="FixedMath.FastMod(Fixed64, Fixed64)" /> |
| | | 84 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 85 | | public static Fixed64 FastMod(this Fixed64 a, Fixed64 b) => FixedMath.FastMod(a, b); |
| | | 86 | | |
| | | 87 | | /// <inheritdoc cref="FixedMath.Lerp(Fixed64, Fixed64, Fixed64)" /> |
| | | 88 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 89 | | public static Fixed64 Lerp(this Fixed64 a, Fixed64 b, Fixed64 t) => FixedMath.Lerp(a, b, t); |
| | | 90 | | |
| | | 91 | | /// <inheritdoc cref="FixedMath.SmoothStep(Fixed64, Fixed64, Fixed64)" /> |
| | | 92 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 93 | | public static Fixed64 SmoothStep(this Fixed64 a, Fixed64 b, Fixed64 t) => FixedMath.SmoothStep(a, b, t); |
| | | 94 | | |
| | | 95 | | /// <inheritdoc cref="FixedMath.CubicInterpolate(Fixed64, Fixed64, Fixed64, Fixed64, Fixed64)" /> |
| | | 96 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 97 | | public static Fixed64 CubicInterpolate(this Fixed64 p0, Fixed64 p1, Fixed64 m0, Fixed64 m1, Fixed64 t) => |
| | 1 | 98 | | FixedMath.CubicInterpolate(p0, p1, m0, m1, t); |
| | | 99 | | |
| | | 100 | | /// <inheritdoc cref="FixedMath.Floor(Fixed64)" /> |
| | | 101 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 39 | 102 | | public static Fixed64 Floor(this Fixed64 value) => FixedMath.Floor(value); |
| | | 103 | | |
| | | 104 | | /// <inheritdoc cref="FixedMath.Ceil(Fixed64)" /> |
| | | 105 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 106 | | public static Fixed64 Ceil(this Fixed64 value) => FixedMath.Ceil(value); |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Rounds the Fixed64 value to the nearest integer. |
| | | 110 | | /// </summary> |
| | | 111 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 14 | 112 | | public static int RoundToInt(this Fixed64 x) => (int)FixedMath.Round(x); |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Rounds the Fixed64 value to the nearest long. |
| | | 116 | | /// </summary> |
| | | 117 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 118 | | public static long RoundToLong(this Fixed64 x) => (long)FixedMath.Round(x); |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Rounds up the Fixed64 value to the nearest integer. |
| | | 122 | | /// </summary> |
| | | 123 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 124 | | public static int CeilToInt(this Fixed64 x) => |
| | 1 | 125 | | (int)(FixedMath.Ceil(x).m_rawValue >> FixedMath.SHIFT_AMOUNT_I); |
| | | 126 | | |
| | | 127 | | /// <summary> |
| | | 128 | | /// Rounds up the Fixed64 value to the nearest long. |
| | | 129 | | /// </summary> |
| | | 130 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 131 | | public static long CeilToLong(this Fixed64 x) => |
| | 1 | 132 | | (long)(FixedMath.Ceil(x).m_rawValue >> FixedMath.SHIFT_AMOUNT_I); |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Rounds down the Fixed64 value to the nearest integer. |
| | | 136 | | /// </summary> |
| | | 137 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 138 | | public static int FloorToInt(this Fixed64 x) => |
| | 1 | 139 | | (int)(FixedMath.Floor(x).m_rawValue >> FixedMath.SHIFT_AMOUNT_I); |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Rounds down the Fixed64 value to the nearest long. |
| | | 143 | | /// </summary> |
| | | 144 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 145 | | public static long FloorToLong(this Fixed64 x) => |
| | 1 | 146 | | (long)(FixedMath.Floor(x).m_rawValue >> FixedMath.SHIFT_AMOUNT_I); |
| | | 147 | | |
| | | 148 | | #endregion |
| | | 149 | | |
| | | 150 | | #region Fixed64 Trigonometry |
| | | 151 | | |
| | | 152 | | /// <inheritdoc cref="FixedMath.Sqrt(Fixed64)" /> |
| | | 153 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 154 | | public static Fixed64 Sqrt(this Fixed64 f) => FixedMath.Sqrt(f); |
| | | 155 | | |
| | | 156 | | /// <inheritdoc cref="FixedMath.Pow(Fixed64, Fixed64)" /> |
| | | 157 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 158 | | public static Fixed64 Pow(this Fixed64 f, Fixed64 exponent) => FixedMath.Pow(f, exponent); |
| | | 159 | | |
| | | 160 | | /// <inheritdoc cref="FixedMath.Pow2(Fixed64)" /> |
| | | 161 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 162 | | public static Fixed64 Pow2(this Fixed64 f) => FixedMath.Pow2(f); |
| | | 163 | | |
| | | 164 | | /// <inheritdoc cref="FixedMath.Log2(Fixed64)" /> |
| | | 165 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 166 | | public static Fixed64 Log2(this Fixed64 f) => FixedMath.Log2(f); |
| | | 167 | | |
| | | 168 | | /// <inheritdoc cref="FixedMath.Ln(Fixed64)" /> |
| | | 169 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 170 | | public static Fixed64 Ln(this Fixed64 f) => FixedMath.Ln(f); |
| | | 171 | | |
| | | 172 | | /// <inheritdoc cref="FixedMath.Acos(Fixed64)" /> |
| | | 173 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 174 | | public static Fixed64 Acos(this Fixed64 f) => FixedMath.Acos(f); |
| | | 175 | | |
| | | 176 | | /// <inheritdoc cref="FixedMath.Asin(Fixed64)" /> |
| | | 177 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 178 | | public static Fixed64 Asin(this Fixed64 f) => FixedMath.Asin(f); |
| | | 179 | | |
| | | 180 | | /// <inheritdoc cref="FixedMath.Atan(Fixed64)" /> |
| | | 181 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 182 | | public static Fixed64 Atan(this Fixed64 f) => FixedMath.Atan(f); |
| | | 183 | | |
| | | 184 | | /// <inheritdoc cref="FixedMath.Atan2(Fixed64, Fixed64)" /> |
| | | 185 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 186 | | public static Fixed64 Atan2(this Fixed64 a, Fixed64 b) => FixedMath.Atan2(a, b); |
| | | 187 | | |
| | | 188 | | /// <inheritdoc cref="FixedMath.Sin(Fixed64)" /> |
| | | 189 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 190 | | public static Fixed64 Sin(this Fixed64 f) => FixedMath.Sin(f); |
| | | 191 | | |
| | | 192 | | /// <inheritdoc cref="FixedMath.Cos(Fixed64)" /> |
| | | 193 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 194 | | public static Fixed64 Cos(this Fixed64 f) => FixedMath.Cos(f); |
| | | 195 | | |
| | | 196 | | /// <inheritdoc cref="FixedMath.Tan(Fixed64)" /> |
| | | 197 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 198 | | public static Fixed64 Tan(this Fixed64 f) => FixedMath.Tan(f); |
| | | 199 | | |
| | | 200 | | /// <inheritdoc cref="FixedMath.GetHypotenuse(Fixed64, Fixed64)" /> |
| | | 201 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 202 | | public static Fixed64 Hypot(this Fixed64 a, Fixed64 b) => FixedMath.GetHypotenuse(a, b); |
| | | 203 | | |
| | | 204 | | #endregion |
| | | 205 | | |
| | | 206 | | #region Conversion |
| | | 207 | | |
| | | 208 | | /// <summary> |
| | | 209 | | /// Converts the angle in degrees to radians. |
| | | 210 | | /// </summary> |
| | | 211 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 212 | | public static Fixed64 ToRadians(this Fixed64 angleInDegrees) => FixedMath.DegToRad(angleInDegrees); |
| | | 213 | | |
| | | 214 | | /// <summary> |
| | | 215 | | /// Converts the angle in radians to degrees. |
| | | 216 | | /// </summary> |
| | | 217 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 218 | | public static Fixed64 ToDegrees(this Fixed64 angleInRadians) => FixedMath.RadToDeg(angleInRadians); |
| | | 219 | | |
| | | 220 | | #endregion |
| | | 221 | | |
| | | 222 | | #region Equality |
| | | 223 | | |
| | | 224 | | /// <summary> |
| | | 225 | | /// Checks if the value is greater than epsilon (positive or negative). |
| | | 226 | | /// Useful for determining if a value is effectively non-zero with a given precision. |
| | | 227 | | /// </summary> |
| | | 228 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 229 | | public static bool MoreThanEpsilon(this Fixed64 d) => d.Abs() > Fixed64.Epsilon; |
| | | 230 | | |
| | | 231 | | /// <summary> |
| | | 232 | | /// Checks if the value is less than epsilon (i.e., effectively zero). |
| | | 233 | | /// Useful for determining if a value is close enough to zero with a given precision. |
| | | 234 | | /// </summary> |
| | | 235 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 677 | 236 | | public static bool LessThanEpsilon(this Fixed64 d) => (d.Abs() < Fixed64.Epsilon); |
| | | 237 | | |
| | | 238 | | /// <summary> |
| | | 239 | | /// Helper method to compare individual vector components for approximate equality, allowing a fractional difference |
| | | 240 | | /// Handles zero components by only using the allowed percentage difference. |
| | | 241 | | /// </summary> |
| | | 242 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 243 | | public static bool FuzzyComponentEqual(this Fixed64 a, Fixed64 b, Fixed64 percentage) |
| | | 244 | | { |
| | 664 | 245 | | var diff = (a - b).Abs(); |
| | 664 | 246 | | var allowedErr = a.Abs() * percentage; |
| | | 247 | | // Compare directly to percentage if a is zero |
| | | 248 | | // Otherwise, use percentage of a's magnitude |
| | 664 | 249 | | return a.LessThanEpsilon() ? diff <= percentage : diff <= allowedErr; |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | #endregion |
| | | 253 | | } |