| | | 1 | | //======================================================================= |
| | | 2 | | // FixedBoundArea.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 | | using System.Text.Json.Serialization; |
| | | 11 | | using MemoryPack; |
| | | 12 | | |
| | | 13 | | namespace FixedMathSharp.Geometry; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Represents a normalized two-dimensional axis-aligned bounding area. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <remarks> |
| | | 19 | | /// FixedMathSharp 2D geometry is plain <see cref="Vector2d"/> plane math. Use |
| | | 20 | | /// <see cref="FromMinMax"/>, <see cref="FromCenterAndSize"/>, or |
| | | 21 | | /// <see cref="FromCenterAndScope"/> so construction intent is explicit. |
| | | 22 | | /// </remarks> |
| | | 23 | | [Serializable] |
| | | 24 | | [MemoryPackable] |
| | | 25 | | public partial struct FixedBoundArea : IEquatable<FixedBoundArea> |
| | | 26 | | { |
| | | 27 | | #region Nested Types |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Represents the normalized serializable state of a two-dimensional axis-aligned bounding area. |
| | | 31 | | /// </summary> |
| | | 32 | | [Serializable] |
| | | 33 | | [MemoryPackable] |
| | | 34 | | public readonly partial struct BoundingAreaState |
| | | 35 | | { |
| | | 36 | | /// <inheritdoc cref="FixedBoundArea.Min"/> |
| | | 37 | | [JsonInclude] |
| | | 38 | | [MemoryPackInclude] |
| | | 39 | | public readonly Vector2d Min; |
| | | 40 | | |
| | | 41 | | /// <inheritdoc cref="FixedBoundArea.Max"/> |
| | | 42 | | [JsonInclude] |
| | | 43 | | [MemoryPackInclude] |
| | | 44 | | public readonly Vector2d Max; |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Initializes a normalized state from minimum and maximum corners. |
| | | 48 | | /// </summary> |
| | | 49 | | [JsonConstructor] |
| | | 50 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 51 | | public BoundingAreaState(Vector2d min, Vector2d max) |
| | | 52 | | { |
| | 5 | 53 | | Min = ComponentMin(min, max); |
| | 5 | 54 | | Max = ComponentMax(min, max); |
| | 5 | 55 | | } |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | #endregion |
| | | 59 | | |
| | | 60 | | #region Constructors |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Initializes a new instance from serialized or caller-provided state. |
| | | 64 | | /// </summary> |
| | | 65 | | [JsonConstructor] |
| | | 66 | | public FixedBoundArea(BoundingAreaState state) |
| | | 67 | | { |
| | 3 | 68 | | State = state; |
| | 3 | 69 | | } |
| | | 70 | | |
| | | 71 | | #endregion |
| | | 72 | | |
| | | 73 | | #region Properties |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// The minimum corner of the area. |
| | | 77 | | /// </summary> |
| | | 78 | | [JsonIgnore] |
| | | 79 | | [MemoryPackIgnore] |
| | | 80 | | public Vector2d Min { get; private set; } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// The maximum corner of the area. |
| | | 84 | | /// </summary> |
| | | 85 | | [JsonIgnore] |
| | | 86 | | [MemoryPackIgnore] |
| | | 87 | | public Vector2d Max { get; private set; } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// The center of the area, rounded to the nearest-even Q32.32 lattice point. |
| | | 91 | | /// </summary> |
| | | 92 | | /// <remarks> |
| | | 93 | | /// Assigning a different center preserves a conservative half-extent. An |
| | | 94 | | /// odd raw-unit span can therefore expand by one raw unit so the assigned |
| | | 95 | | /// center remains exact and the previous area is not under-represented. |
| | | 96 | | /// </remarks> |
| | | 97 | | /// <exception cref="OverflowException"> |
| | | 98 | | /// An assigned center would place an endpoint outside the scalar domain. |
| | | 99 | | /// </exception> |
| | | 100 | | [JsonIgnore] |
| | | 101 | | [MemoryPackIgnore] |
| | | 102 | | public Vector2d Center |
| | | 103 | | { |
| | | 104 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 16 | 105 | | get => new( |
| | 16 | 106 | | FixedMath.Midpoint(Min.X, Max.X), |
| | 16 | 107 | | FixedMath.Midpoint(Min.Y, Max.Y)); |
| | | 108 | | |
| | | 109 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 110 | | set |
| | | 111 | | { |
| | 4 | 112 | | if (value != Center) |
| | 3 | 113 | | SetCenterAndHalfSize(value, Scope); |
| | 3 | 114 | | } |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// The exact total width and height of the area. |
| | | 119 | | /// </summary> |
| | | 120 | | /// <remarks> |
| | | 121 | | /// Assigned values are normalized by absolute component value and divided |
| | | 122 | | /// outward. An odd raw-unit size therefore expands by one raw unit. Reading |
| | | 123 | | /// this property throws rather than returning a saturated value when an |
| | | 124 | | /// exact component span is not representable by <see cref="Fixed64"/>. |
| | | 125 | | /// </remarks> |
| | | 126 | | /// <exception cref="OverflowException"> |
| | | 127 | | /// A component span is not representable, or an assigned size would place |
| | | 128 | | /// an endpoint outside the scalar domain. |
| | | 129 | | /// </exception> |
| | | 130 | | [JsonIgnore] |
| | | 131 | | [MemoryPackIgnore] |
| | | 132 | | public Vector2d Size |
| | | 133 | | { |
| | | 134 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 11 | 135 | | get => new( |
| | 11 | 136 | | WideGeometry.GetIntervalSize(Min.X, Max.X), |
| | 11 | 137 | | WideGeometry.GetIntervalSize(Min.Y, Max.Y)); |
| | | 138 | | |
| | | 139 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 140 | | set |
| | | 141 | | { |
| | 2 | 142 | | SetCenterAndHalfSize(Center, GetHalfSize(value)); |
| | 1 | 143 | | } |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | /// <summary> |
| | | 147 | | /// The smallest representable half-extent that conservatively contains the |
| | | 148 | | /// area around <see cref="Center"/>. |
| | | 149 | | /// </summary> |
| | | 150 | | /// <exception cref="OverflowException"> |
| | | 151 | | /// A conservative half-extent is outside the representable scalar domain. |
| | | 152 | | /// </exception> |
| | | 153 | | [JsonIgnore] |
| | | 154 | | [MemoryPackIgnore] |
| | | 155 | | public Vector2d Scope |
| | | 156 | | { |
| | | 157 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 10 | 158 | | get => new( |
| | 10 | 159 | | WideGeometry.GetIntervalScope(Min.X, Max.X), |
| | 10 | 160 | | WideGeometry.GetIntervalScope(Min.Y, Max.Y)); |
| | | 161 | | } |
| | | 162 | | |
| | | 163 | | /// <summary> |
| | | 164 | | /// Gets or sets the current normalized state of the area. |
| | | 165 | | /// </summary> |
| | | 166 | | [JsonInclude] |
| | | 167 | | [MemoryPackInclude] |
| | | 168 | | public BoundingAreaState State |
| | | 169 | | { |
| | 2 | 170 | | get => new(Min, Max); |
| | | 171 | | |
| | | 172 | | internal set |
| | | 173 | | { |
| | 3 | 174 | | SetMinMax(value.Min, value.Max); |
| | 3 | 175 | | } |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | #endregion |
| | | 179 | | |
| | | 180 | | #region Factories |
| | | 181 | | |
| | | 182 | | /// <summary> |
| | | 183 | | /// Creates a normalized area from minimum and maximum corners. |
| | | 184 | | /// </summary> |
| | | 185 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 186 | | public static FixedBoundArea FromMinMax(Vector2d min, Vector2d max) |
| | | 187 | | { |
| | 350 | 188 | | var area = default(FixedBoundArea); |
| | 350 | 189 | | area.SetMinMax(min, max); |
| | 350 | 190 | | return area; |
| | | 191 | | } |
| | | 192 | | |
| | | 193 | | /// <summary> |
| | | 194 | | /// Creates an area from a center point and total size. |
| | | 195 | | /// </summary> |
| | | 196 | | /// <remarks> |
| | | 197 | | /// Negative size components are normalized by absolute value. Odd raw-unit |
| | | 198 | | /// sizes are divided outward and therefore expand by one raw unit. |
| | | 199 | | /// </remarks> |
| | | 200 | | /// <exception cref="OverflowException"> |
| | | 201 | | /// The centered area would place an endpoint outside the scalar domain. |
| | | 202 | | /// </exception> |
| | | 203 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 204 | | public static FixedBoundArea FromCenterAndSize(Vector2d center, Vector2d size) |
| | | 205 | | { |
| | 15 | 206 | | var area = default(FixedBoundArea); |
| | 15 | 207 | | area.SetCenterAndHalfSize(center, GetHalfSize(size)); |
| | 13 | 208 | | return area; |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | /// <summary> |
| | | 212 | | /// Creates an area from a center point and half-size scope. |
| | | 213 | | /// </summary> |
| | | 214 | | /// <remarks> |
| | | 215 | | /// Negative scope components are normalized by absolute value. |
| | | 216 | | /// </remarks> |
| | | 217 | | /// <exception cref="OverflowException"> |
| | | 218 | | /// A scope magnitude is not representable, or the centered area would |
| | | 219 | | /// place an endpoint outside the scalar domain. |
| | | 220 | | /// </exception> |
| | | 221 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 222 | | public static FixedBoundArea FromCenterAndScope(Vector2d center, Vector2d scope) |
| | | 223 | | { |
| | 2 | 224 | | var area = default(FixedBoundArea); |
| | 2 | 225 | | area.SetCenterAndHalfSize(center, GetScopeMagnitude(scope)); |
| | 1 | 226 | | return area; |
| | | 227 | | } |
| | | 228 | | |
| | | 229 | | /// <summary> |
| | | 230 | | /// Creates the representable-domain intersection of an area described by a |
| | | 231 | | /// center point and total size. |
| | | 232 | | /// </summary> |
| | | 233 | | /// <remarks> |
| | | 234 | | /// Negative size components are normalized by absolute value and odd raw- |
| | | 235 | | /// unit sizes divide outward. Endpoints outside the scalar domain are |
| | | 236 | | /// explicitly clipped to <see cref="Fixed64.MinValue"/> or |
| | | 237 | | /// <see cref="Fixed64.MaxValue"/>. |
| | | 238 | | /// </remarks> |
| | | 239 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 240 | | public static FixedBoundArea FromCenterAndSizeClippedToDomain(Vector2d center, Vector2d size) |
| | | 241 | | { |
| | 1 | 242 | | var area = default(FixedBoundArea); |
| | 1 | 243 | | area.SetCenterAndHalfSizeClippedToDomain(center, GetHalfSize(size)); |
| | 1 | 244 | | return area; |
| | | 245 | | } |
| | | 246 | | |
| | | 247 | | /// <summary> |
| | | 248 | | /// Creates the representable-domain intersection of an area described by a |
| | | 249 | | /// center point and half-size scope. |
| | | 250 | | /// </summary> |
| | | 251 | | /// <remarks> |
| | | 252 | | /// Negative scope components are normalized by absolute value. Endpoints |
| | | 253 | | /// outside the scalar domain are explicitly clipped to |
| | | 254 | | /// <see cref="Fixed64.MinValue"/> or <see cref="Fixed64.MaxValue"/>. |
| | | 255 | | /// </remarks> |
| | | 256 | | /// <exception cref="OverflowException"> |
| | | 257 | | /// A scope magnitude is not representable. |
| | | 258 | | /// </exception> |
| | | 259 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 260 | | public static FixedBoundArea FromCenterAndScopeClippedToDomain(Vector2d center, Vector2d scope) |
| | | 261 | | { |
| | 5 | 262 | | var area = default(FixedBoundArea); |
| | 5 | 263 | | area.SetCenterAndHalfSizeClippedToDomain(center, GetScopeMagnitude(scope)); |
| | 5 | 264 | | return area; |
| | | 265 | | } |
| | | 266 | | |
| | | 267 | | /// <summary> |
| | | 268 | | /// Creates the representable-domain intersection of bounds described by a |
| | | 269 | | /// center and normalized center-relative minimum and maximum offsets. |
| | | 270 | | /// </summary> |
| | | 271 | | /// <remarks> |
| | | 272 | | /// Each endpoint is formed by one final saturating add, which is the |
| | | 273 | | /// explicit clipping operation. Asymmetric offsets are preserved. |
| | | 274 | | /// </remarks> |
| | | 275 | | /// <exception cref="ArgumentException"> |
| | | 276 | | /// A minimum offset component exceeds the matching maximum component. |
| | | 277 | | /// </exception> |
| | | 278 | | public static FixedBoundArea FromCenterAndOffsetsClippedToDomain( |
| | | 279 | | Vector2d center, |
| | | 280 | | Vector2d minimumOffset, |
| | | 281 | | Vector2d maximumOffset) |
| | | 282 | | { |
| | 3 | 283 | | if (minimumOffset.X > maximumOffset.X |
| | 3 | 284 | | || minimumOffset.Y > maximumOffset.Y) |
| | | 285 | | { |
| | 2 | 286 | | throw new ArgumentException( |
| | 2 | 287 | | "Minimum offsets must not exceed maximum offsets.", |
| | 2 | 288 | | nameof(minimumOffset)); |
| | | 289 | | } |
| | | 290 | | |
| | 1 | 291 | | return FromMinMax( |
| | 1 | 292 | | center + minimumOffset, |
| | 1 | 293 | | center + maximumOffset); |
| | | 294 | | } |
| | | 295 | | |
| | | 296 | | /// <summary> |
| | | 297 | | /// Creates the representable-domain intersection of bounds around rotated |
| | | 298 | | /// local offsets without materializing any transformed point. |
| | | 299 | | /// </summary> |
| | | 300 | | public static FixedBoundArea FromRotatedOffsetsClippedToDomain( |
| | | 301 | | Vector2d origin, |
| | | 302 | | Fixed64 rotation, |
| | | 303 | | ReadOnlySpan<Vector2d> localOffsets) |
| | | 304 | | { |
| | 3 | 305 | | if (localOffsets.IsEmpty) |
| | | 306 | | { |
| | 1 | 307 | | throw new ArgumentException( |
| | 1 | 308 | | "At least one local offset is required.", |
| | 1 | 309 | | nameof(localOffsets)); |
| | | 310 | | } |
| | | 311 | | |
| | 2 | 312 | | return WideConvex2dRelations.GetBoundsClippedToDomain( |
| | 2 | 313 | | origin, |
| | 2 | 314 | | rotation, |
| | 2 | 315 | | localOffsets); |
| | | 316 | | } |
| | | 317 | | |
| | | 318 | | #endregion |
| | | 319 | | |
| | | 320 | | #region Mutators |
| | | 321 | | |
| | | 322 | | /// <summary> |
| | | 323 | | /// Sets the normalized bounds of the area by specifying minimum and maximum points. |
| | | 324 | | /// </summary> |
| | | 325 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 326 | | public void SetMinMax(Vector2d min, Vector2d max) |
| | | 327 | | { |
| | 354 | 328 | | Min = ComponentMin(min, max); |
| | 354 | 329 | | Max = ComponentMax(min, max); |
| | 354 | 330 | | } |
| | | 331 | | |
| | | 332 | | #endregion |
| | | 333 | | |
| | | 334 | | #region Spatial Queries |
| | | 335 | | |
| | | 336 | | /// <summary> |
| | | 337 | | /// Determines whether the point is inside this area, including the boundary. |
| | | 338 | | /// </summary> |
| | | 339 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 340 | | public bool Contains(Vector2d point) |
| | | 341 | | { |
| | 5 | 342 | | return point.X >= Min.X && point.X <= Max.X |
| | 5 | 343 | | && point.Y >= Min.Y && point.Y <= Max.Y; |
| | | 344 | | } |
| | | 345 | | |
| | | 346 | | /// <summary> |
| | | 347 | | /// Classifies another area against this area using boundary-inclusive overlap. |
| | | 348 | | /// </summary> |
| | | 349 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 350 | | public FixedEnclosureType Contains(FixedBoundArea area) |
| | | 351 | | { |
| | 3 | 352 | | if (area.Min.X >= Min.X && area.Max.X <= Max.X |
| | 3 | 353 | | && area.Min.Y >= Min.Y && area.Max.Y <= Max.Y) |
| | 1 | 354 | | return FixedEnclosureType.Contains; |
| | | 355 | | |
| | 2 | 356 | | return Intersects(area) |
| | 2 | 357 | | ? FixedEnclosureType.Intersects |
| | 2 | 358 | | : FixedEnclosureType.Disjoint; |
| | | 359 | | } |
| | | 360 | | |
| | | 361 | | /// <summary> |
| | | 362 | | /// Classifies a circle against this area using boundary-inclusive overlap. |
| | | 363 | | /// </summary> |
| | | 364 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 365 | | public FixedEnclosureType Contains(FixedBoundCircle circle) |
| | | 366 | | { |
| | 9 | 367 | | if (WideGeometry.ContainsCenteredExtent(Min.X, Max.X, circle.Center.X, circle.Radius) |
| | 9 | 368 | | && WideGeometry.ContainsCenteredExtent(Min.Y, Max.Y, circle.Center.Y, circle.Radius)) |
| | 1 | 369 | | return FixedEnclosureType.Contains; |
| | | 370 | | |
| | 8 | 371 | | return Intersects(circle) |
| | 8 | 372 | | ? FixedEnclosureType.Intersects |
| | 8 | 373 | | : FixedEnclosureType.Disjoint; |
| | | 374 | | } |
| | | 375 | | |
| | | 376 | | /// <summary> |
| | | 377 | | /// Determines whether this area overlaps another area, including boundary-only contact. |
| | | 378 | | /// </summary> |
| | | 379 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 380 | | public bool Intersects(FixedBoundArea area) |
| | | 381 | | { |
| | 6 | 382 | | return Min.X <= area.Max.X && Max.X >= area.Min.X |
| | 6 | 383 | | && Min.Y <= area.Max.Y && Max.Y >= area.Min.Y; |
| | | 384 | | } |
| | | 385 | | |
| | | 386 | | /// <summary> |
| | | 387 | | /// Determines whether this area overlaps a circle, including boundary-only contact. |
| | | 388 | | /// </summary> |
| | | 389 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 12 | 390 | | public bool Intersects(FixedBoundCircle circle) => circle.Intersects(this); |
| | | 391 | | |
| | | 392 | | /// <summary> |
| | | 393 | | /// Determines whether this area overlaps another area with positive area on both axes. |
| | | 394 | | /// </summary> |
| | | 395 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 396 | | public bool IntersectsStrict(FixedBoundArea area) |
| | | 397 | | { |
| | 4 | 398 | | return HasPositiveArea() && area.HasPositiveArea() |
| | 4 | 399 | | && Min.X < area.Max.X && Max.X > area.Min.X |
| | 4 | 400 | | && Min.Y < area.Max.Y && Max.Y > area.Min.Y; |
| | | 401 | | } |
| | | 402 | | |
| | | 403 | | /// <summary> |
| | | 404 | | /// Determines whether this area overlaps a circle with positive area. |
| | | 405 | | /// </summary> |
| | | 406 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 4 | 407 | | public bool IntersectsStrict(FixedBoundCircle circle) => circle.IntersectsStrict(this); |
| | | 408 | | |
| | | 409 | | /// <summary> |
| | | 410 | | /// Clamps a point to the area boundary or interior. |
| | | 411 | | /// </summary> |
| | | 412 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 413 | | public Vector2d ClampPoint(Vector2d point) |
| | | 414 | | { |
| | 27 | 415 | | return new Vector2d( |
| | 27 | 416 | | FixedMath.Clamp(point.X, Min.X, Max.X), |
| | 27 | 417 | | FixedMath.Clamp(point.Y, Min.Y, Max.Y)); |
| | | 418 | | } |
| | | 419 | | |
| | | 420 | | /// <summary> |
| | | 421 | | /// Projects a point onto this area by clamping it to the boundary or interior. |
| | | 422 | | /// </summary> |
| | | 423 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 424 | | public Vector2d ProjectPoint(Vector2d point) => ClampPoint(point); |
| | | 425 | | |
| | | 426 | | #endregion |
| | | 427 | | |
| | | 428 | | #region Deconstruction |
| | | 429 | | |
| | | 430 | | /// <summary> |
| | | 431 | | /// Deconstructs the area into normalized minimum and maximum corners. |
| | | 432 | | /// </summary> |
| | | 433 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 434 | | public void Deconstruct(out Vector2d min, out Vector2d max) |
| | | 435 | | { |
| | 1 | 436 | | min = Min; |
| | 1 | 437 | | max = Max; |
| | 1 | 438 | | } |
| | | 439 | | |
| | | 440 | | #endregion |
| | | 441 | | |
| | | 442 | | #region Equality |
| | | 443 | | |
| | | 444 | | /// <inheritdoc /> |
| | | 445 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 446 | | public bool Equals(FixedBoundArea other) |
| | | 447 | | { |
| | 34 | 448 | | return Min == other.Min && Max == other.Max; |
| | | 449 | | } |
| | | 450 | | |
| | | 451 | | /// <inheritdoc /> |
| | | 452 | | public override bool Equals(object? obj) |
| | | 453 | | { |
| | 2 | 454 | | return obj is FixedBoundArea other && Equals(other); |
| | | 455 | | } |
| | | 456 | | |
| | | 457 | | /// <inheritdoc /> |
| | | 458 | | public override int GetHashCode() |
| | | 459 | | { |
| | | 460 | | unchecked |
| | | 461 | | { |
| | 3 | 462 | | int hash = 17; |
| | 3 | 463 | | hash = (hash * 31) + Min.StateHash; |
| | 3 | 464 | | hash = (hash * 31) + Max.StateHash; |
| | 3 | 465 | | return hash; |
| | | 466 | | } |
| | | 467 | | } |
| | | 468 | | |
| | | 469 | | /// <summary> |
| | | 470 | | /// Determines whether two areas have the same normalized bounds. |
| | | 471 | | /// </summary> |
| | | 472 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 473 | | public static bool operator ==(FixedBoundArea left, FixedBoundArea right) => left.Equals(right); |
| | | 474 | | |
| | | 475 | | /// <summary> |
| | | 476 | | /// Determines whether two areas have different normalized bounds. |
| | | 477 | | /// </summary> |
| | | 478 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 479 | | public static bool operator !=(FixedBoundArea left, FixedBoundArea right) => !left.Equals(right); |
| | | 480 | | |
| | | 481 | | #endregion |
| | | 482 | | |
| | | 483 | | #region Static Operations |
| | | 484 | | |
| | | 485 | | /// <summary> |
| | | 486 | | /// Creates a new area that contains both input areas. |
| | | 487 | | /// </summary> |
| | | 488 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 489 | | public static FixedBoundArea Union(FixedBoundArea a, FixedBoundArea b) |
| | | 490 | | { |
| | 1 | 491 | | return FromMinMax(ComponentMin(a.Min, b.Min), ComponentMax(a.Max, b.Max)); |
| | | 492 | | } |
| | | 493 | | |
| | | 494 | | #endregion |
| | | 495 | | |
| | | 496 | | #region Helpers |
| | | 497 | | |
| | | 498 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 499 | | private bool HasPositiveArea() |
| | | 500 | | { |
| | 8 | 501 | | return Min.X < Max.X && Min.Y < Max.Y; |
| | | 502 | | } |
| | | 503 | | |
| | | 504 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 505 | | private static Vector2d ComponentMin(Vector2d a, Vector2d b) |
| | | 506 | | { |
| | 360 | 507 | | return new Vector2d(FixedMath.Min(a.X, b.X), FixedMath.Min(a.Y, b.Y)); |
| | | 508 | | } |
| | | 509 | | |
| | | 510 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 511 | | private static Vector2d ComponentMax(Vector2d a, Vector2d b) |
| | | 512 | | { |
| | 360 | 513 | | return new Vector2d(FixedMath.Max(a.X, b.X), FixedMath.Max(a.Y, b.Y)); |
| | | 514 | | } |
| | | 515 | | |
| | | 516 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 517 | | private void SetCenterAndHalfSize(Vector2d center, Vector2d halfSize) |
| | | 518 | | { |
| | 21 | 519 | | if (!Vector2d.TrySubtract(center, halfSize, out Vector2d min) |
| | 21 | 520 | | || !Vector2d.TryAdd(center, halfSize, out Vector2d max)) |
| | | 521 | | { |
| | 4 | 522 | | throw CreateUnrepresentableBoundsException(); |
| | | 523 | | } |
| | | 524 | | |
| | 17 | 525 | | Min = min; |
| | 17 | 526 | | Max = max; |
| | 17 | 527 | | } |
| | | 528 | | |
| | | 529 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 530 | | private void SetCenterAndHalfSizeClippedToDomain(Vector2d center, Vector2d halfSize) |
| | | 531 | | { |
| | 6 | 532 | | Vector2d min = center - halfSize; |
| | 6 | 533 | | Vector2d max = center + halfSize; |
| | 6 | 534 | | Min = min; |
| | 6 | 535 | | Max = max; |
| | 6 | 536 | | } |
| | | 537 | | |
| | | 538 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 18 | 539 | | private static Vector2d GetHalfSize(Vector2d size) => new( |
| | 18 | 540 | | WideGeometry.GetHalfSizeMagnitude(size.X), |
| | 18 | 541 | | WideGeometry.GetHalfSizeMagnitude(size.Y)); |
| | | 542 | | |
| | | 543 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 7 | 544 | | private static Vector2d GetScopeMagnitude(Vector2d scope) => new( |
| | 7 | 545 | | WideGeometry.GetExtentMagnitude(scope.X), |
| | 7 | 546 | | WideGeometry.GetExtentMagnitude(scope.Y)); |
| | | 547 | | |
| | | 548 | | private static OverflowException CreateUnrepresentableBoundsException() => |
| | 4 | 549 | | new("The centered area places at least one endpoint outside the representable Fixed64 range."); |
| | | 550 | | |
| | | 551 | | #endregion |
| | | 552 | | |
| | | 553 | | /// <summary> |
| | | 554 | | /// Creates the representable-domain intersection of a centered capsule's |
| | | 555 | | /// tight axis-aligned bounds from its scalar frame rotation. |
| | | 556 | | /// </summary> |
| | | 557 | | /// <remarks> |
| | | 558 | | /// The capsule's local positive Y axis is its center axis. Sine and cosine |
| | | 559 | | /// remain authoritative through the exact finite-axis bound calculation; |
| | | 560 | | /// no rounded normalized world axis is fed back into the geometry. |
| | | 561 | | /// </remarks> |
| | | 562 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | | 563 | | /// <paramref name="fullAxisLength"/> or <paramref name="radius"/> is negative. |
| | | 564 | | /// </exception> |
| | | 565 | | public static FixedBoundArea FromCenteredRotatedCapsuleClippedToDomain( |
| | | 566 | | Vector2d center, |
| | | 567 | | Fixed64 rotation, |
| | | 568 | | Fixed64 fullAxisLength, |
| | | 569 | | Fixed64 radius) |
| | | 570 | | { |
| | 6 | 571 | | if (fullAxisLength < Fixed64.Zero) |
| | 1 | 572 | | throw new ArgumentOutOfRangeException(nameof(fullAxisLength)); |
| | 5 | 573 | | if (radius < Fixed64.Zero) |
| | 1 | 574 | | throw new ArgumentOutOfRangeException(nameof(radius)); |
| | | 575 | | |
| | 4 | 576 | | Fixed64 axisX = -FixedMath.Sin(rotation); |
| | 4 | 577 | | Fixed64 axisY = FixedMath.Cos(rotation); |
| | 4 | 578 | | return FromMinMax( |
| | 4 | 579 | | new Vector2d( |
| | 4 | 580 | | WideGeometry.GetCenteredFiniteAxisBoundClippedToDomain( |
| | 4 | 581 | | center.X, |
| | 4 | 582 | | axisX, |
| | 4 | 583 | | fullAxisLength, |
| | 4 | 584 | | radius, |
| | 4 | 585 | | minimum: true), |
| | 4 | 586 | | WideGeometry.GetCenteredFiniteAxisBoundClippedToDomain( |
| | 4 | 587 | | center.Y, |
| | 4 | 588 | | axisY, |
| | 4 | 589 | | fullAxisLength, |
| | 4 | 590 | | radius, |
| | 4 | 591 | | minimum: true)), |
| | 4 | 592 | | new Vector2d( |
| | 4 | 593 | | WideGeometry.GetCenteredFiniteAxisBoundClippedToDomain( |
| | 4 | 594 | | center.X, |
| | 4 | 595 | | axisX, |
| | 4 | 596 | | fullAxisLength, |
| | 4 | 597 | | radius, |
| | 4 | 598 | | minimum: false), |
| | 4 | 599 | | WideGeometry.GetCenteredFiniteAxisBoundClippedToDomain( |
| | 4 | 600 | | center.Y, |
| | 4 | 601 | | axisY, |
| | 4 | 602 | | fullAxisLength, |
| | 4 | 603 | | radius, |
| | 4 | 604 | | minimum: false))); |
| | | 605 | | } |
| | | 606 | | |
| | | 607 | | /// <summary> |
| | | 608 | | /// Creates the representable-domain intersection of a centered capsule's |
| | | 609 | | /// tight axis-aligned bounds from its full center-axis length. |
| | | 610 | | /// </summary> |
| | | 611 | | /// <param name="center">The center of the capsule's axis segment.</param> |
| | | 612 | | /// <param name="axisDirection">The normalized center-axis direction.</param> |
| | | 613 | | /// <param name="fullAxisLength">The nonnegative full center-axis length.</param> |
| | | 614 | | /// <param name="radius">The nonnegative capsule radius.</param> |
| | | 615 | | /// <remarks> |
| | | 616 | | /// Each exact endpoint is clipped to the scalar domain and rounded outward. |
| | | 617 | | /// Zero length and zero radius are valid degenerate capsules. |
| | | 618 | | /// </remarks> |
| | | 619 | | /// <exception cref="ArgumentException"> |
| | | 620 | | /// <paramref name="axisDirection"/> is zero or not normalized. |
| | | 621 | | /// </exception> |
| | | 622 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | | 623 | | /// <paramref name="fullAxisLength"/> or <paramref name="radius"/> is negative. |
| | | 624 | | /// </exception> |
| | | 625 | | public static FixedBoundArea FromCenteredCapsuleClippedToDomain( |
| | | 626 | | Vector2d center, |
| | | 627 | | Vector2d axisDirection, |
| | | 628 | | Fixed64 fullAxisLength, |
| | | 629 | | Fixed64 radius) |
| | | 630 | | { |
| | 272 | 631 | | if (!axisDirection.IsNormalized()) |
| | | 632 | | { |
| | 2 | 633 | | throw new ArgumentException( |
| | 2 | 634 | | "Finite-axis direction must be normalized.", |
| | 2 | 635 | | nameof(axisDirection)); |
| | | 636 | | } |
| | 270 | 637 | | if (fullAxisLength < Fixed64.Zero) |
| | 1 | 638 | | throw new ArgumentOutOfRangeException(nameof(fullAxisLength)); |
| | 269 | 639 | | if (radius < Fixed64.Zero) |
| | 1 | 640 | | throw new ArgumentOutOfRangeException(nameof(radius)); |
| | | 641 | | |
| | 268 | 642 | | return FromMinMax( |
| | 268 | 643 | | new Vector2d( |
| | 268 | 644 | | WideGeometry.GetCenteredFiniteAxisBoundClippedToDomain( |
| | 268 | 645 | | center.X, |
| | 268 | 646 | | axisDirection.X, |
| | 268 | 647 | | fullAxisLength, |
| | 268 | 648 | | radius, |
| | 268 | 649 | | minimum: true), |
| | 268 | 650 | | WideGeometry.GetCenteredFiniteAxisBoundClippedToDomain( |
| | 268 | 651 | | center.Y, |
| | 268 | 652 | | axisDirection.Y, |
| | 268 | 653 | | fullAxisLength, |
| | 268 | 654 | | radius, |
| | 268 | 655 | | minimum: true)), |
| | 268 | 656 | | new Vector2d( |
| | 268 | 657 | | WideGeometry.GetCenteredFiniteAxisBoundClippedToDomain( |
| | 268 | 658 | | center.X, |
| | 268 | 659 | | axisDirection.X, |
| | 268 | 660 | | fullAxisLength, |
| | 268 | 661 | | radius, |
| | 268 | 662 | | minimum: false), |
| | 268 | 663 | | WideGeometry.GetCenteredFiniteAxisBoundClippedToDomain( |
| | 268 | 664 | | center.Y, |
| | 268 | 665 | | axisDirection.Y, |
| | 268 | 666 | | fullAxisLength, |
| | 268 | 667 | | radius, |
| | 268 | 668 | | minimum: false))); |
| | | 669 | | } |
| | | 670 | | } |