| | | 1 | | using MemoryPack; |
| | | 2 | | using System; |
| | | 3 | | using System.Runtime.CompilerServices; |
| | | 4 | | using System.Text.Json.Serialization; |
| | | 5 | | |
| | | 6 | | namespace FixedMathSharp; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Represents a range of values with fixed precision. |
| | | 10 | | /// </summary> |
| | | 11 | | [Serializable] |
| | | 12 | | [MemoryPackable] |
| | | 13 | | public partial struct FixedRange : IEquatable<FixedRange> |
| | | 14 | | { |
| | | 15 | | #region Static Readonly Fields |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// The smallest possible range. |
| | | 19 | | /// </summary> |
| | | 20 | | public static readonly FixedRange MinRange = new FixedRange(Fixed64.MIN_VALUE, Fixed64.MIN_VALUE); |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// The largest possible range. |
| | | 24 | | /// </summary> |
| | | 25 | | public static readonly FixedRange MaxRange = new FixedRange(Fixed64.MAX_VALUE, Fixed64.MAX_VALUE); |
| | | 26 | | |
| | | 27 | | #endregion |
| | | 28 | | |
| | | 29 | | #region Fields |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets the minimum value of the range. |
| | | 33 | | /// </summary> |
| | | 34 | | [JsonInclude] |
| | | 35 | | [MemoryPackOrder(0)] |
| | | 36 | | public Fixed64 Min; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets the maximum value of the range. |
| | | 40 | | /// </summary> |
| | | 41 | | [JsonInclude] |
| | | 42 | | [MemoryPackOrder(1)] |
| | | 43 | | public Fixed64 Max; |
| | | 44 | | |
| | | 45 | | #endregion |
| | | 46 | | |
| | | 47 | | #region Constructors |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Initializes a new instance of the FixedRange structure with the specified minimum and maximum values. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <param name="min">The minimum value of the range.</param> |
| | | 53 | | /// <param name="max">The maximum value of the range.</param> |
| | | 54 | | /// <param name="enforceOrder">If true, ensures that Min is less than or equal to Max.</param> |
| | | 55 | | public FixedRange(Fixed64 min, Fixed64 max, bool enforceOrder = true) |
| | 60 | 56 | | { |
| | 60 | 57 | | if (enforceOrder) |
| | 59 | 58 | | { |
| | 59 | 59 | | Min = min < max ? min : max; |
| | 59 | 60 | | Max = min < max ? max : min; |
| | 59 | 61 | | } |
| | | 62 | | else |
| | 1 | 63 | | { |
| | 1 | 64 | | Min = min; |
| | 1 | 65 | | Max = max; |
| | 1 | 66 | | } |
| | 60 | 67 | | } |
| | | 68 | | |
| | | 69 | | #endregion |
| | | 70 | | |
| | | 71 | | #region Properties |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// The length of the range, computed as Max - Min. |
| | | 75 | | /// </summary> |
| | | 76 | | [JsonIgnore] |
| | | 77 | | [MemoryPackIgnore] |
| | | 78 | | public Fixed64 Length |
| | | 79 | | { |
| | | 80 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 81 | | get => Max - Min; |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// The midpoint of the range. |
| | | 86 | | /// </summary> |
| | | 87 | | [JsonIgnore] |
| | | 88 | | [MemoryPackIgnore] |
| | | 89 | | public Fixed64 MidPoint |
| | | 90 | | { |
| | | 91 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 92 | | get => (Min + Max) * Fixed64.Half; |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | #endregion |
| | | 96 | | |
| | | 97 | | #region Methods (Instance) |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Sets the minimum and maximum values for the range. |
| | | 101 | | /// </summary> |
| | | 102 | | /// <param name="min">The new minimum value.</param> |
| | | 103 | | /// <param name="max">The new maximum value.</param> |
| | | 104 | | public void SetMinMax(Fixed64 min, Fixed64 max) |
| | 1 | 105 | | { |
| | 1 | 106 | | Min = min; |
| | 1 | 107 | | Max = max; |
| | 1 | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Adds a value to both the minimum and maximum of the range. |
| | | 112 | | /// </summary> |
| | | 113 | | /// <param name="val">The value to add.</param> |
| | | 114 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 115 | | public void AddInPlace(Fixed64 val) |
| | 1 | 116 | | { |
| | 1 | 117 | | Min += val; |
| | 1 | 118 | | Max += val; |
| | 1 | 119 | | } |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// Determines whether the specified value is within the range, with an option to include or exclude the upper bound |
| | | 123 | | /// </summary> |
| | | 124 | | /// <param name="x">The value to check.</param> |
| | | 125 | | /// <param name="includeMax">If true, the upper bound (Max) is included in the range check; otherwise, the upper bou |
| | | 126 | | /// <returns>True if the value is within the range; otherwise, false.</returns> |
| | | 127 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 128 | | public bool InRange(Fixed64 x, bool includeMax = false) |
| | 9 | 129 | | { |
| | 9 | 130 | | return includeMax ? x >= Min && x <= Max : x >= Min && x < Max; |
| | 9 | 131 | | } |
| | | 132 | | |
| | | 133 | | /// <inheritdoc cref="InRange(Fixed64, bool)" /> |
| | | 134 | | public bool InRange(double x, bool includeMax = false) |
| | 6 | 135 | | { |
| | 6 | 136 | | long xL = (long)Math.Round((double)x * FixedMath.ONE_L); |
| | 6 | 137 | | return includeMax ? xL >= Min.m_rawValue && xL <= Max.m_rawValue : xL >= Min.m_rawValue && xL < Max.m_rawValue; |
| | 6 | 138 | | } |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Checks whether this range overlaps with the specified range, ensuring no adjacent edges are considered overlaps. |
| | | 142 | | /// </summary> |
| | | 143 | | /// <param name="other">The range to compare.</param> |
| | | 144 | | /// <returns>True if the ranges overlap; otherwise, false.</returns> |
| | | 145 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 146 | | public bool Overlaps(FixedRange other) |
| | 9 | 147 | | { |
| | 9 | 148 | | return Min < other.Max && Max > other.Min; |
| | 9 | 149 | | } |
| | | 150 | | |
| | | 151 | | #endregion |
| | | 152 | | |
| | | 153 | | #region Range Operations |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// Determines the direction from one range to another. |
| | | 157 | | /// If they don't overlap, returns -1 or 1 depending on the relative position. |
| | | 158 | | /// </summary> |
| | | 159 | | /// <param name="range1">The first range.</param> |
| | | 160 | | /// <param name="range2">The second range.</param> |
| | | 161 | | /// <param name="sign">The direction between ranges (-1 or 1).</param> |
| | | 162 | | /// <returns>True if the ranges don't overlap, false if they do.</returns> |
| | | 163 | | public static bool GetDirection(FixedRange range1, FixedRange range2, out Fixed64? sign) |
| | 3 | 164 | | { |
| | 3 | 165 | | sign = null; |
| | 3 | 166 | | if (!range1.Overlaps(range2)) |
| | 2 | 167 | | { |
| | 3 | 168 | | if (range1.Max < range2.Min) sign = -Fixed64.One; |
| | 1 | 169 | | else sign = Fixed64.One; |
| | 2 | 170 | | return true; |
| | | 171 | | } |
| | 1 | 172 | | return false; |
| | 3 | 173 | | } |
| | | 174 | | |
| | | 175 | | /// <summary> |
| | | 176 | | /// Calculates the overlap depth between two ranges. |
| | | 177 | | /// Assumes the ranges are sorted (min and max are correctly assigned). |
| | | 178 | | /// </summary> |
| | | 179 | | /// <param name="rangeA">The first range.</param> |
| | | 180 | | /// <param name="rangeB">The second range.</param> |
| | | 181 | | /// <returns>The depth of the overlap between the ranges.</returns> |
| | | 182 | | public static Fixed64 ComputeOverlapDepth(FixedRange rangeA, FixedRange rangeB) |
| | 6 | 183 | | { |
| | | 184 | | // Check if one range is completely within the other |
| | 6 | 185 | | bool isRangeAInsideB = rangeA.Min >= rangeB.Min && rangeA.Max <= rangeB.Max; |
| | 6 | 186 | | bool isRangeBInsideA = rangeB.Min >= rangeA.Min && rangeB.Max <= rangeA.Max; |
| | 6 | 187 | | if (isRangeAInsideB) |
| | 1 | 188 | | return rangeA.Max - rangeB.Min; // The size of rangeA |
| | 5 | 189 | | else if (isRangeBInsideA) |
| | 1 | 190 | | return rangeB.Max - rangeA.Min; // The size of rangeB |
| | | 191 | | |
| | | 192 | | // Calculate overlap between the two ranges |
| | 4 | 193 | | Fixed64 overlapEnd = FixedMath.Min(rangeA.Max, rangeB.Max); |
| | 4 | 194 | | Fixed64 overlapStart = FixedMath.Max(rangeA.Min, rangeB.Min); |
| | 4 | 195 | | Fixed64 overlap = overlapEnd - overlapStart; |
| | | 196 | | |
| | 4 | 197 | | return overlap > Fixed64.Zero ? overlap : Fixed64.Zero; |
| | 6 | 198 | | } |
| | | 199 | | |
| | | 200 | | /// <summary> |
| | | 201 | | /// Checks for overlap between two ranges and calculates the vector of overlap depth. |
| | | 202 | | /// </summary> |
| | | 203 | | /// <param name="origin">The origin vector.</param> |
| | | 204 | | /// <param name="range1">The first range.</param> |
| | | 205 | | /// <param name="range2">The second range.</param> |
| | | 206 | | /// <param name="limit">The overlap limit to check.</param> |
| | | 207 | | /// <param name="sign">The direction sign to consider.</param> |
| | | 208 | | /// <param name="output">The overlap vector and depth, if any.</param> |
| | | 209 | | /// <returns>True if overlap occurs and is below the limit, otherwise false.</returns> |
| | | 210 | | public static bool CheckOverlap(Vector3d origin, FixedRange range1, FixedRange range2, Fixed64 limit, Fixed64 sign, |
| | 2 | 211 | | { |
| | 2 | 212 | | output = null; |
| | 2 | 213 | | Fixed64 overlap = ComputeOverlapDepth(range1, range2); |
| | | 214 | | |
| | | 215 | | // If the overlap is smaller than the current minimum, update the minimum |
| | 2 | 216 | | if (overlap < limit) |
| | 1 | 217 | | { |
| | 1 | 218 | | output = (origin * overlap * sign, overlap); |
| | 1 | 219 | | return true; |
| | | 220 | | } |
| | 1 | 221 | | return false; |
| | 2 | 222 | | } |
| | | 223 | | |
| | | 224 | | #endregion |
| | | 225 | | |
| | | 226 | | #region Operators |
| | | 227 | | |
| | | 228 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 229 | | public static FixedRange operator +(FixedRange left, FixedRange right) |
| | 1 | 230 | | { |
| | 1 | 231 | | return new FixedRange(left.Min + right.Min, left.Max + right.Max); |
| | 1 | 232 | | } |
| | | 233 | | |
| | | 234 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 235 | | public static FixedRange operator -(FixedRange left, FixedRange right) |
| | 1 | 236 | | { |
| | 1 | 237 | | return new FixedRange(left.Min - right.Min, left.Max - right.Max); |
| | 1 | 238 | | } |
| | | 239 | | |
| | | 240 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 241 | | public static bool operator ==(FixedRange left, FixedRange right) |
| | 1 | 242 | | { |
| | 1 | 243 | | return left.Equals(right); |
| | 1 | 244 | | } |
| | | 245 | | |
| | | 246 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 247 | | public static bool operator !=(FixedRange left, FixedRange right) |
| | 1 | 248 | | { |
| | 1 | 249 | | return !left.Equals(right); |
| | 1 | 250 | | } |
| | | 251 | | |
| | | 252 | | #endregion |
| | | 253 | | |
| | | 254 | | #region Conversion |
| | | 255 | | |
| | | 256 | | /// <summary> |
| | | 257 | | /// Returns a string that represents the FixedRange instance, formatted as "Min - Max". |
| | | 258 | | /// </summary> |
| | | 259 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 260 | | public override string ToString() |
| | 1 | 261 | | { |
| | 1 | 262 | | return $"{Min.ToFormattedDouble()} - {Max.ToFormattedDouble()}"; |
| | 1 | 263 | | } |
| | | 264 | | |
| | | 265 | | #endregion |
| | | 266 | | |
| | | 267 | | #region Equality and HashCode Overrides |
| | | 268 | | |
| | | 269 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 270 | | public override bool Equals(object? obj) |
| | 2 | 271 | | { |
| | 2 | 272 | | return obj is FixedRange other && Equals(other); |
| | 2 | 273 | | } |
| | | 274 | | |
| | | 275 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 276 | | public bool Equals(FixedRange other) |
| | 8 | 277 | | { |
| | 8 | 278 | | return other.Min == Min && other.Max == Max; |
| | 8 | 279 | | } |
| | | 280 | | |
| | | 281 | | /// <summary> |
| | | 282 | | /// Computes the hash code for the FixedRange instance. |
| | | 283 | | /// </summary> |
| | | 284 | | /// <returns>The hash code of the range.</returns> |
| | | 285 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 286 | | public override int GetHashCode() |
| | 2 | 287 | | { |
| | 2 | 288 | | return Min.GetHashCode() ^ Max.GetHashCode(); |
| | 2 | 289 | | } |
| | | 290 | | |
| | | 291 | | #endregion |
| | | 292 | | } |