| | | 1 | | //======================================================================= |
| | | 2 | | // FixedBoundBox.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 three-dimensional axis-aligned bounding box. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <remarks> |
| | | 19 | | /// Use <see cref="FromMinMax"/>, <see cref="FromCenterAndSize"/>, or |
| | | 20 | | /// <see cref="FromCenterAndScope"/> so construction intent is explicit at the call site. |
| | | 21 | | /// </remarks> |
| | | 22 | | [Serializable] |
| | | 23 | | [MemoryPackable] |
| | | 24 | | public partial struct FixedBoundBox : IEquatable<FixedBoundBox> |
| | | 25 | | { |
| | | 26 | | /// <summary> |
| | | 27 | | /// The number of stable corners exposed by <see cref="GetCorner"/> and <see cref="CopyCorners"/>. |
| | | 28 | | /// </summary> |
| | | 29 | | public const int CornerCount = 8; |
| | | 30 | | |
| | | 31 | | #region Nested Types |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Represents the state of a three-dimensional axis-aligned bounding box using its minimum and maximum coordinates. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <remarks> |
| | | 37 | | /// The bounding box is defined by two points: the minimum and maximum corners in 3D space. This |
| | | 38 | | /// structure is immutable and can be used to describe spatial boundaries for geometric computations, collision |
| | | 39 | | /// detection, or spatial queries. |
| | | 40 | | /// </remarks> |
| | | 41 | | [Serializable] |
| | | 42 | | [MemoryPackable] |
| | | 43 | | public readonly partial struct BoundingBoxState |
| | | 44 | | { |
| | | 45 | | /// <inheritdoc cref="FixedBoundBox.Min"/> |
| | | 46 | | [JsonInclude] |
| | | 47 | | [MemoryPackInclude] |
| | | 48 | | public readonly Vector3d Min; |
| | | 49 | | |
| | | 50 | | /// <inheritdoc cref="FixedBoundBox.Max"/> |
| | | 51 | | [JsonInclude] |
| | | 52 | | [MemoryPackInclude] |
| | | 53 | | public readonly Vector3d Max; |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Initializes a new instance of the BoundingBoxState class with the specified minimum and maximum coordinates. |
| | | 57 | | /// </summary> |
| | | 58 | | [JsonConstructor] |
| | | 59 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 60 | | public BoundingBoxState(Vector3d min, Vector3d max) |
| | | 61 | | { |
| | 7 | 62 | | Min = Vector3d.Min(min, max); |
| | 7 | 63 | | Max = Vector3d.Max(min, max); |
| | 7 | 64 | | } |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | #endregion |
| | | 68 | | |
| | | 69 | | #region Constructors |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Initializes a new instance of the FixedBoundBox class with the specified bounding box state. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <param name="state">The state that defines the position, size, and orientation of the bounding box.</param> |
| | | 75 | | [JsonConstructor] |
| | | 76 | | public FixedBoundBox(BoundingBoxState state) |
| | | 77 | | { |
| | 4 | 78 | | State = state; |
| | 4 | 79 | | } |
| | | 80 | | |
| | | 81 | | #endregion |
| | | 82 | | |
| | | 83 | | #region Properties |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// The minimum corner of the bounding box. |
| | | 87 | | /// </summary> |
| | | 88 | | [JsonIgnore] |
| | | 89 | | [MemoryPackIgnore] |
| | | 90 | | public Vector3d Min { get; private set; } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// The maximum corner of the bounding box. |
| | | 94 | | /// </summary> |
| | | 95 | | [JsonIgnore] |
| | | 96 | | [MemoryPackIgnore] |
| | | 97 | | public Vector3d Max { get; private set; } |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// The center of the bounding box, rounded to the nearest-even Q32.32 lattice point. |
| | | 101 | | /// </summary> |
| | | 102 | | /// <remarks> |
| | | 103 | | /// Assigning a different center preserves a conservative half-extent. An |
| | | 104 | | /// odd raw-unit span can therefore expand by one raw unit so the assigned |
| | | 105 | | /// center remains exact and the previous box is not under-represented. |
| | | 106 | | /// </remarks> |
| | | 107 | | /// <exception cref="OverflowException"> |
| | | 108 | | /// An assigned center would place an endpoint outside the scalar domain. |
| | | 109 | | /// </exception> |
| | | 110 | | [JsonIgnore] |
| | | 111 | | [MemoryPackIgnore] |
| | | 112 | | public Vector3d Center |
| | | 113 | | { |
| | | 114 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 27 | 115 | | get => Vector3d.Midpoint(Min, Max); |
| | | 116 | | |
| | | 117 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 118 | | set |
| | | 119 | | { |
| | 6 | 120 | | if (value != Center) |
| | 5 | 121 | | SetCenterAndHalfSize(value, Scope); |
| | 4 | 122 | | } |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// The exact total size of the box. |
| | | 127 | | /// </summary> |
| | | 128 | | /// <remarks> |
| | | 129 | | /// Assigned values are normalized by absolute component value and divided |
| | | 130 | | /// outward. An odd raw-unit size therefore expands by one raw unit. Reading |
| | | 131 | | /// this property throws rather than returning a saturated value when an |
| | | 132 | | /// exact component span is not representable by <see cref="Fixed64"/>. |
| | | 133 | | /// </remarks> |
| | | 134 | | /// <exception cref="OverflowException"> |
| | | 135 | | /// A component span is not representable, or an assigned size would place |
| | | 136 | | /// an endpoint outside the scalar domain. |
| | | 137 | | /// </exception> |
| | | 138 | | [JsonIgnore] |
| | | 139 | | [MemoryPackIgnore] |
| | | 140 | | public Vector3d Proportions |
| | | 141 | | { |
| | | 142 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 16 | 143 | | get => new( |
| | 16 | 144 | | WideGeometry.GetIntervalSize(Min.X, Max.X), |
| | 16 | 145 | | WideGeometry.GetIntervalSize(Min.Y, Max.Y), |
| | 16 | 146 | | WideGeometry.GetIntervalSize(Min.Z, Max.Z)); |
| | | 147 | | |
| | | 148 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 149 | | set |
| | | 150 | | { |
| | 1 | 151 | | SetCenterAndHalfSize(Center, GetHalfSize(value)); |
| | 1 | 152 | | } |
| | | 153 | | } |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// The smallest representable half-extent that conservatively contains the |
| | | 157 | | /// box around <see cref="Center"/>. |
| | | 158 | | /// </summary> |
| | | 159 | | /// <exception cref="OverflowException"> |
| | | 160 | | /// A conservative half-extent is outside the representable scalar domain. |
| | | 161 | | /// </exception> |
| | | 162 | | [JsonIgnore] |
| | | 163 | | [MemoryPackIgnore] |
| | | 164 | | public Vector3d Scope |
| | | 165 | | { |
| | | 166 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 11 | 167 | | get => new( |
| | 11 | 168 | | WideGeometry.GetIntervalScope(Min.X, Max.X), |
| | 11 | 169 | | WideGeometry.GetIntervalScope(Min.Y, Max.Y), |
| | 11 | 170 | | WideGeometry.GetIntervalScope(Min.Z, Max.Z)); |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | /// <summary> |
| | | 174 | | /// Gets or sets the current normalized bounding box state, including its minimum and maximum coordinates. |
| | | 175 | | /// </summary> |
| | | 176 | | [JsonInclude] |
| | | 177 | | [MemoryPackInclude] |
| | | 178 | | public BoundingBoxState State |
| | | 179 | | { |
| | 3 | 180 | | get => new(Min, Max); |
| | | 181 | | |
| | | 182 | | internal set |
| | | 183 | | { |
| | 4 | 184 | | SetMinMax(value.Min, value.Max); |
| | 4 | 185 | | } |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | #endregion |
| | | 189 | | |
| | | 190 | | #region Factories |
| | | 191 | | |
| | | 192 | | /// <summary> |
| | | 193 | | /// Creates a normalized bounding box from minimum and maximum corners. |
| | | 194 | | /// </summary> |
| | | 195 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 196 | | public static FixedBoundBox FromMinMax(Vector3d min, Vector3d max) |
| | | 197 | | { |
| | 966 | 198 | | var box = default(FixedBoundBox); |
| | 966 | 199 | | box.SetMinMax(min, max); |
| | 966 | 200 | | return box; |
| | | 201 | | } |
| | | 202 | | |
| | | 203 | | /// <summary> |
| | | 204 | | /// Creates a bounding box from a center point and total size. |
| | | 205 | | /// </summary> |
| | | 206 | | /// <remarks> |
| | | 207 | | /// Negative size components are normalized by absolute value. Odd raw-unit |
| | | 208 | | /// sizes are divided outward and therefore expand by one raw unit. |
| | | 209 | | /// </remarks> |
| | | 210 | | /// <exception cref="OverflowException"> |
| | | 211 | | /// The centered box would place an endpoint outside the scalar domain. |
| | | 212 | | /// </exception> |
| | | 213 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 214 | | public static FixedBoundBox FromCenterAndSize(Vector3d center, Vector3d size) |
| | | 215 | | { |
| | 128 | 216 | | var box = default(FixedBoundBox); |
| | 128 | 217 | | box.SetCenterAndHalfSize(center, GetHalfSize(size)); |
| | 126 | 218 | | return box; |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | /// <summary> |
| | | 222 | | /// Creates a bounding box from a center point and half-size scope. |
| | | 223 | | /// </summary> |
| | | 224 | | /// <remarks> |
| | | 225 | | /// Negative scope components are normalized by absolute value. |
| | | 226 | | /// </remarks> |
| | | 227 | | /// <exception cref="OverflowException"> |
| | | 228 | | /// A scope magnitude is not representable, or the centered box would place |
| | | 229 | | /// an endpoint outside the scalar domain. |
| | | 230 | | /// </exception> |
| | | 231 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 232 | | public static FixedBoundBox FromCenterAndScope(Vector3d center, Vector3d scope) |
| | | 233 | | { |
| | 8 | 234 | | var box = default(FixedBoundBox); |
| | 8 | 235 | | box.SetCenterAndHalfSize(center, GetScopeMagnitude(scope)); |
| | 7 | 236 | | return box; |
| | | 237 | | } |
| | | 238 | | |
| | | 239 | | /// <summary> |
| | | 240 | | /// Creates the representable-domain intersection of a box described by a |
| | | 241 | | /// center point and total size. |
| | | 242 | | /// </summary> |
| | | 243 | | /// <remarks> |
| | | 244 | | /// Negative size components are normalized by absolute value and odd raw- |
| | | 245 | | /// unit sizes divide outward. Endpoints outside the scalar domain are |
| | | 246 | | /// explicitly clipped to <see cref="Fixed64.MinValue"/> or |
| | | 247 | | /// <see cref="Fixed64.MaxValue"/>. |
| | | 248 | | /// </remarks> |
| | | 249 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 250 | | public static FixedBoundBox FromCenterAndSizeClippedToDomain(Vector3d center, Vector3d size) |
| | | 251 | | { |
| | 1 | 252 | | var box = default(FixedBoundBox); |
| | 1 | 253 | | box.SetCenterAndHalfSizeClippedToDomain(center, GetHalfSize(size)); |
| | 1 | 254 | | return box; |
| | | 255 | | } |
| | | 256 | | |
| | | 257 | | /// <summary> |
| | | 258 | | /// Creates the representable-domain intersection of a box described by a |
| | | 259 | | /// center point and half-size scope. |
| | | 260 | | /// </summary> |
| | | 261 | | /// <remarks> |
| | | 262 | | /// Negative scope components are normalized by absolute value. Endpoints |
| | | 263 | | /// outside the scalar domain are explicitly clipped to |
| | | 264 | | /// <see cref="Fixed64.MinValue"/> or <see cref="Fixed64.MaxValue"/>. |
| | | 265 | | /// </remarks> |
| | | 266 | | /// <exception cref="OverflowException"> |
| | | 267 | | /// A scope magnitude is not representable. |
| | | 268 | | /// </exception> |
| | | 269 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 270 | | public static FixedBoundBox FromCenterAndScopeClippedToDomain(Vector3d center, Vector3d scope) |
| | | 271 | | { |
| | 1 | 272 | | var box = default(FixedBoundBox); |
| | 1 | 273 | | box.SetCenterAndHalfSizeClippedToDomain(center, GetScopeMagnitude(scope)); |
| | 1 | 274 | | return box; |
| | | 275 | | } |
| | | 276 | | |
| | | 277 | | /// <summary> |
| | | 278 | | /// Creates conservative target-frame bounds for a rotated source-frame |
| | | 279 | | /// box, clipping only final target-frame endpoints to the scalar domain. |
| | | 280 | | /// </summary> |
| | | 281 | | /// <remarks> |
| | | 282 | | /// Conceptually transforms every source local point as |
| | | 283 | | /// <c>targetRotation^-1 * (sourceOrigin + sourceRotation * point - targetOrigin)</c>. |
| | | 284 | | /// Source endpoints are normalized component-wise before the exact |
| | | 285 | | /// projection. Intermediate world points are never materialized. |
| | | 286 | | /// </remarks> |
| | | 287 | | public static FixedBoundBox FromRelativeRotatedBoundsClippedToDomain( |
| | | 288 | | Vector3d sourceOrigin, |
| | | 289 | | FixedQuaternion sourceRotation, |
| | | 290 | | Vector3d sourceLocalMin, |
| | | 291 | | Vector3d sourceLocalMax, |
| | | 292 | | Vector3d targetOrigin, |
| | | 293 | | FixedQuaternion targetRotation) |
| | | 294 | | { |
| | 72 | 295 | | if (!sourceRotation.IsNormalized()) |
| | | 296 | | { |
| | 1 | 297 | | throw new ArgumentException( |
| | 1 | 298 | | "Source rotation must be normalized.", |
| | 1 | 299 | | nameof(sourceRotation)); |
| | | 300 | | } |
| | 71 | 301 | | if (!targetRotation.IsNormalized()) |
| | | 302 | | { |
| | 1 | 303 | | throw new ArgumentException( |
| | 1 | 304 | | "Target rotation must be normalized.", |
| | 1 | 305 | | nameof(targetRotation)); |
| | | 306 | | } |
| | | 307 | | |
| | 70 | 308 | | return WideOrientedBox.GetRelativeRotatedBoundsClippedToDomain( |
| | 70 | 309 | | sourceOrigin, |
| | 70 | 310 | | sourceRotation, |
| | 70 | 311 | | sourceLocalMin, |
| | 70 | 312 | | sourceLocalMax, |
| | 70 | 313 | | targetOrigin, |
| | 70 | 314 | | targetRotation); |
| | | 315 | | } |
| | | 316 | | |
| | | 317 | | #endregion |
| | | 318 | | |
| | | 319 | | #region Mutators |
| | | 320 | | |
| | | 321 | | /// <summary> |
| | | 322 | | /// Orients the bounding box with the given center and size. |
| | | 323 | | /// </summary> |
| | | 324 | | /// <exception cref="OverflowException"> |
| | | 325 | | /// The requested bounds would place an endpoint outside the scalar domain. |
| | | 326 | | /// </exception> |
| | | 327 | | public void Orient(Vector3d center, Vector3d? size) |
| | | 328 | | { |
| | 3 | 329 | | if (size.HasValue) |
| | | 330 | | { |
| | 1 | 331 | | SetCenterAndHalfSize(center, GetHalfSize(size.Value)); |
| | 1 | 332 | | return; |
| | | 333 | | } |
| | | 334 | | |
| | 2 | 335 | | Center = center; |
| | 1 | 336 | | } |
| | | 337 | | |
| | | 338 | | /// <summary> |
| | | 339 | | /// Resizes the bounding box to the specified size, keeping the same center. |
| | | 340 | | /// </summary> |
| | | 341 | | /// <exception cref="OverflowException"> |
| | | 342 | | /// The requested size would place an endpoint outside the scalar domain. |
| | | 343 | | /// </exception> |
| | | 344 | | public void Resize(Vector3d size) |
| | | 345 | | { |
| | 4 | 346 | | SetCenterAndHalfSize(Center, GetHalfSize(size)); |
| | 3 | 347 | | } |
| | | 348 | | |
| | | 349 | | /// <summary> |
| | | 350 | | /// Sets the normalized bounds of the bounding box by specifying its minimum and maximum points. |
| | | 351 | | /// </summary> |
| | | 352 | | public void SetMinMax(Vector3d min, Vector3d max) |
| | | 353 | | { |
| | 972 | 354 | | Min = Vector3d.Min(min, max); |
| | 972 | 355 | | Max = Vector3d.Max(min, max); |
| | 972 | 356 | | } |
| | | 357 | | |
| | | 358 | | /// <summary> |
| | | 359 | | /// Configures the bounding box with the specified center and scope (half-size). |
| | | 360 | | /// </summary> |
| | | 361 | | /// <remarks> |
| | | 362 | | /// Negative scope components are normalized by absolute value. |
| | | 363 | | /// </remarks> |
| | | 364 | | /// <exception cref="OverflowException"> |
| | | 365 | | /// A scope magnitude is not representable, or the requested bounds would |
| | | 366 | | /// place an endpoint outside the scalar domain. |
| | | 367 | | /// </exception> |
| | | 368 | | public void SetBoundingBox(Vector3d center, Vector3d scope) |
| | | 369 | | { |
| | 3 | 370 | | SetCenterAndHalfSize(center, GetScopeMagnitude(scope)); |
| | 3 | 371 | | } |
| | | 372 | | |
| | | 373 | | /// <summary> |
| | | 374 | | /// Determines if a point is inside the bounding box (including boundaries). |
| | | 375 | | /// </summary> |
| | | 376 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 377 | | public bool Contains(Vector3d point) |
| | | 378 | | { |
| | 58 | 379 | | return point.X >= Min.X && point.X <= Max.X |
| | 58 | 380 | | && point.Y >= Min.Y && point.Y <= Max.Y |
| | 58 | 381 | | && point.Z >= Min.Z && point.Z <= Max.Z; |
| | | 382 | | } |
| | | 383 | | |
| | | 384 | | /// <summary> |
| | | 385 | | /// Tests another bounding box against this bounding box. |
| | | 386 | | /// </summary> |
| | | 387 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 6 | 388 | | public FixedEnclosureType Contains(FixedBoundBox box) => ContainsBoxLike(box.Min, box.Max); |
| | | 389 | | |
| | | 390 | | /// <summary> |
| | | 391 | | /// Tests a bounding sphere against this bounding box. |
| | | 392 | | /// </summary> |
| | | 393 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 394 | | public FixedEnclosureType Contains(FixedBoundSphere sphere) |
| | | 395 | | { |
| | 4 | 396 | | if (WideGeometry.ContainsCenteredExtent(Min.X, Max.X, sphere.Center.X, sphere.Radius) |
| | 4 | 397 | | && WideGeometry.ContainsCenteredExtent(Min.Y, Max.Y, sphere.Center.Y, sphere.Radius) |
| | 4 | 398 | | && WideGeometry.ContainsCenteredExtent(Min.Z, Max.Z, sphere.Center.Z, sphere.Radius)) |
| | 1 | 399 | | return FixedEnclosureType.Contains; |
| | | 400 | | |
| | 3 | 401 | | return Intersects(sphere) ? FixedEnclosureType.Intersects : FixedEnclosureType.Disjoint; |
| | | 402 | | } |
| | | 403 | | |
| | | 404 | | /// <summary> |
| | | 405 | | /// Tests a bounding frustum against this bounding box. |
| | | 406 | | /// </summary> |
| | | 407 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 408 | | public FixedEnclosureType Contains(FixedBoundFrustum frustum) |
| | | 409 | | { |
| | 3 | 410 | | if (Contains(frustum.Min) && Contains(frustum.Max)) |
| | 1 | 411 | | return FixedEnclosureType.Contains; |
| | | 412 | | |
| | 2 | 413 | | return Intersects(frustum) ? FixedEnclosureType.Intersects : FixedEnclosureType.Disjoint; |
| | | 414 | | } |
| | | 415 | | |
| | | 416 | | /// <summary> |
| | | 417 | | /// Checks whether another bounding box intersects this bounding box, including boundary-only contact. |
| | | 418 | | /// </summary> |
| | | 419 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 19 | 420 | | public bool Intersects(FixedBoundBox box) => IntersectsBoxLike(box.Min, box.Max); |
| | | 421 | | |
| | | 422 | | /// <summary> |
| | | 423 | | /// Checks whether a bounding sphere intersects this bounding box, including boundary-only contact. |
| | | 424 | | /// </summary> |
| | | 425 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 21 | 426 | | public bool Intersects(FixedBoundSphere sphere) => IntersectsSphere(sphere); |
| | | 427 | | |
| | | 428 | | /// <summary> |
| | | 429 | | /// Checks whether another bounding box overlaps this bounding box with positive volume on every axis. |
| | | 430 | | /// </summary> |
| | | 431 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 12 | 432 | | public bool IntersectsStrict(FixedBoundBox box) => HasStrictAxisOverlap(box.Min, box.Max); |
| | | 433 | | |
| | | 434 | | /// <summary> |
| | | 435 | | /// Checks whether a bounding sphere overlaps this bounding box with positive volume. |
| | | 436 | | /// </summary> |
| | | 437 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 6 | 438 | | public bool IntersectsStrict(FixedBoundSphere sphere) => IntersectsSphereStrict(sphere); |
| | | 439 | | |
| | | 440 | | /// <summary> |
| | | 441 | | /// Checks whether a bounding frustum intersects this bounding box. |
| | | 442 | | /// </summary> |
| | | 443 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 444 | | public bool Intersects(FixedBoundFrustum frustum) |
| | | 445 | | { |
| | 4 | 446 | | return frustum.Intersects(this); |
| | | 447 | | } |
| | | 448 | | |
| | | 449 | | /// <summary> |
| | | 450 | | /// Projects a point into the bounding box by clamping it to the box extents. |
| | | 451 | | /// </summary> |
| | | 452 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 453 | | public Vector3d ProjectPoint(Vector3d point) |
| | 2 | 454 | | => ClampPoint(point); |
| | | 455 | | |
| | | 456 | | /// <summary> |
| | | 457 | | /// Clamps a point to this bounding box, returning the point unchanged when it is already inside. |
| | | 458 | | /// </summary> |
| | | 459 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 460 | | public Vector3d ClampPoint(Vector3d point) |
| | | 461 | | { |
| | 28 | 462 | | return new Vector3d( |
| | 28 | 463 | | FixedMath.Clamp(point.X, Min.X, Max.X), |
| | 28 | 464 | | FixedMath.Clamp(point.Y, Min.Y, Max.Y), |
| | 28 | 465 | | FixedMath.Clamp(point.Z, Min.Z, Max.Z)); |
| | | 466 | | } |
| | | 467 | | |
| | | 468 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 469 | | private FixedEnclosureType ContainsBoxLike(Vector3d otherMin, Vector3d otherMax) |
| | | 470 | | { |
| | 6 | 471 | | if (Contains(otherMin) && Contains(otherMax)) |
| | 2 | 472 | | return FixedEnclosureType.Contains; |
| | | 473 | | |
| | 4 | 474 | | return IntersectsBoxLike(otherMin, otherMax) |
| | 4 | 475 | | ? FixedEnclosureType.Intersects |
| | 4 | 476 | | : FixedEnclosureType.Disjoint; |
| | | 477 | | } |
| | | 478 | | |
| | | 479 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 480 | | private bool IntersectsBoxLike(Vector3d otherMin, Vector3d otherMax) |
| | | 481 | | { |
| | 23 | 482 | | return Min.X <= otherMax.X && Max.X >= otherMin.X |
| | 23 | 483 | | && Min.Y <= otherMax.Y && Max.Y >= otherMin.Y |
| | 23 | 484 | | && Min.Z <= otherMax.Z && Max.Z >= otherMin.Z; |
| | | 485 | | } |
| | | 486 | | |
| | | 487 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 488 | | private bool IntersectsSphere(FixedBoundSphere sphere) |
| | | 489 | | { |
| | 21 | 490 | | return WideGeometry.CompareDistanceToRadiusSum( |
| | 21 | 491 | | sphere.Center, |
| | 21 | 492 | | ClampPoint(sphere.Center), |
| | 21 | 493 | | sphere.Radius, |
| | 21 | 494 | | Fixed64.Zero) <= 0; |
| | | 495 | | } |
| | | 496 | | |
| | | 497 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 498 | | private bool IntersectsSphereStrict(FixedBoundSphere sphere) |
| | | 499 | | { |
| | 6 | 500 | | return HasPositiveVolume() |
| | 6 | 501 | | && sphere.Radius > Fixed64.Zero |
| | 6 | 502 | | && WideGeometry.CompareDistanceToRadiusSum( |
| | 6 | 503 | | sphere.Center, |
| | 6 | 504 | | ClampPoint(sphere.Center), |
| | 6 | 505 | | sphere.Radius, |
| | 6 | 506 | | Fixed64.Zero) < 0; |
| | | 507 | | } |
| | | 508 | | |
| | | 509 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 510 | | private bool HasPositiveVolume() |
| | | 511 | | { |
| | 18 | 512 | | return Min.X < Max.X && Min.Y < Max.Y && Min.Z < Max.Z; |
| | | 513 | | } |
| | | 514 | | |
| | | 515 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 516 | | private bool HasStrictAxisOverlap(Vector3d otherMin, Vector3d otherMax) |
| | | 517 | | { |
| | 12 | 518 | | return HasPositiveVolume() |
| | 12 | 519 | | && otherMin.X < otherMax.X |
| | 12 | 520 | | && otherMin.Y < otherMax.Y |
| | 12 | 521 | | && otherMin.Z < otherMax.Z |
| | 12 | 522 | | && Min.X < otherMax.X && Max.X > otherMin.X |
| | 12 | 523 | | && Min.Y < otherMax.Y && Max.Y > otherMin.Y |
| | 12 | 524 | | && Min.Z < otherMax.Z && Max.Z > otherMin.Z; |
| | | 525 | | } |
| | | 526 | | |
| | | 527 | | /// <summary> |
| | | 528 | | /// Calculates the shortest distance from a given point to the surface of the bounding box. |
| | | 529 | | /// If the point lies inside the box, the distance is zero. |
| | | 530 | | /// </summary> |
| | | 531 | | /// <param name="point">The point from which to calculate the distance.</param> |
| | | 532 | | /// <returns> |
| | | 533 | | /// The shortest distance from the point to the surface of the bounding box. |
| | | 534 | | /// If the point is inside the box, the method returns zero. |
| | | 535 | | /// </returns> |
| | | 536 | | /// <remarks> |
| | | 537 | | /// The method finds the closest point on the box's surface by clamping the given point |
| | | 538 | | /// to the box's bounds and returns the Euclidean distance between them. |
| | | 539 | | /// This ensures accurate distance calculations, even near corners or edges. |
| | | 540 | | /// </remarks> |
| | | 541 | | public Fixed64 DistanceToSurface(Vector3d point) |
| | | 542 | | { |
| | | 543 | | // Clamp the point to the nearest point on the box's surface |
| | 2 | 544 | | Vector3d clampedPoint = new( |
| | 2 | 545 | | FixedMath.Clamp(point.X, Min.X, Max.X), |
| | 2 | 546 | | FixedMath.Clamp(point.Y, Min.Y, Max.Y), |
| | 2 | 547 | | FixedMath.Clamp(point.Z, Min.Z, Max.Z) |
| | 2 | 548 | | ); |
| | | 549 | | |
| | | 550 | | // If the point is inside the box, return 0 |
| | 2 | 551 | | if (Contains(point)) |
| | 1 | 552 | | return Fixed64.Zero; |
| | | 553 | | |
| | | 554 | | // Otherwise, return the Euclidean distance to the clamped point |
| | 1 | 555 | | return Vector3d.Distance(point, clampedPoint); |
| | | 556 | | } |
| | | 557 | | |
| | | 558 | | /// <summary> |
| | | 559 | | /// Finds the closest point on the surface of the bounding box towards a specified object position. |
| | | 560 | | /// </summary> |
| | | 561 | | public Vector3d GetPointOnSurfaceTowardsObject(Vector3d objectPosition) |
| | 1 | 562 | | => ClosestPointOnSurface(ProjectPoint(objectPosition)); |
| | | 563 | | |
| | | 564 | | /// <summary> |
| | | 565 | | /// Finds the closest point on the surface of the bounding box to the specified point. |
| | | 566 | | /// </summary> |
| | | 567 | | public Vector3d ClosestPointOnSurface(Vector3d point) |
| | | 568 | | { |
| | 16 | 569 | | if (Contains(point)) |
| | | 570 | | { |
| | | 571 | | // Calculate distances to each face and return the closest face. |
| | 7 | 572 | | Fixed64 distToMinX = point.X - Min.X; |
| | 7 | 573 | | Fixed64 distToMaxX = Max.X - point.X; |
| | 7 | 574 | | Fixed64 distToMinY = point.Y - Min.Y; |
| | 7 | 575 | | Fixed64 distToMaxY = Max.Y - point.Y; |
| | 7 | 576 | | Fixed64 distToMinZ = point.Z - Min.Z; |
| | 7 | 577 | | Fixed64 distToMaxZ = Max.Z - point.Z; |
| | | 578 | | |
| | 7 | 579 | | Fixed64 minDistToFace = FixedMath.Min(distToMinX, |
| | 7 | 580 | | FixedMath.Min(distToMaxX, |
| | 7 | 581 | | FixedMath.Min(distToMinY, |
| | 7 | 582 | | FixedMath.Min(distToMaxY, |
| | 7 | 583 | | FixedMath.Min(distToMinZ, distToMaxZ))))); |
| | | 584 | | |
| | | 585 | | // Adjust the closest point based on the face. |
| | 8 | 586 | | if (minDistToFace == distToMinX) point.X = Min.X; |
| | 8 | 587 | | else if (minDistToFace == distToMaxX) point.X = Max.X; |
| | | 588 | | |
| | 8 | 589 | | if (minDistToFace == distToMinY) point.Y = Min.Y; |
| | 7 | 590 | | else if (minDistToFace == distToMaxY) point.Y = Max.Y; |
| | | 591 | | |
| | 8 | 592 | | if (minDistToFace == distToMinZ) point.Z = Min.Z; |
| | 7 | 593 | | else if (minDistToFace == distToMaxZ) point.Z = Max.Z; |
| | | 594 | | |
| | 7 | 595 | | return point; |
| | | 596 | | } |
| | | 597 | | |
| | | 598 | | // If the point is outside the box, clamp to the nearest surface. |
| | 9 | 599 | | return new Vector3d( |
| | 9 | 600 | | FixedMath.Clamp(point.X, Min.X, Max.X), |
| | 9 | 601 | | FixedMath.Clamp(point.Y, Min.Y, Max.Y), |
| | 9 | 602 | | FixedMath.Clamp(point.Z, Min.Z, Max.Z) |
| | 9 | 603 | | ); |
| | | 604 | | } |
| | | 605 | | |
| | | 606 | | /// <summary> |
| | | 607 | | /// Gets a stable corner without allocating. |
| | | 608 | | /// </summary> |
| | | 609 | | /// <remarks> |
| | | 610 | | /// Corner order is: min/min/min, max/min/min, min/max/min, max/max/min, |
| | | 611 | | /// min/min/max, max/min/max, min/max/max, max/max/max. |
| | | 612 | | /// </remarks> |
| | | 613 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 614 | | public Vector3d GetCorner(int index) |
| | | 615 | | { |
| | 173 | 616 | | return index switch |
| | 173 | 617 | | { |
| | 24 | 618 | | 0 => new Vector3d(Min.X, Min.Y, Min.Z), |
| | 21 | 619 | | 1 => new Vector3d(Max.X, Min.Y, Min.Z), |
| | 21 | 620 | | 2 => new Vector3d(Min.X, Max.Y, Min.Z), |
| | 21 | 621 | | 3 => new Vector3d(Max.X, Max.Y, Min.Z), |
| | 21 | 622 | | 4 => new Vector3d(Min.X, Min.Y, Max.Z), |
| | 21 | 623 | | 5 => new Vector3d(Max.X, Min.Y, Max.Z), |
| | 21 | 624 | | 6 => new Vector3d(Min.X, Max.Y, Max.Z), |
| | 21 | 625 | | 7 => new Vector3d(Max.X, Max.Y, Max.Z), |
| | 2 | 626 | | _ => throw new ArgumentOutOfRangeException(nameof(index), $"Corner index must be between 0 and {CornerCount |
| | 173 | 627 | | }; |
| | | 628 | | } |
| | | 629 | | |
| | | 630 | | /// <summary> |
| | | 631 | | /// Copies this box's corners into the destination span in <see cref="GetCorner"/> order. |
| | | 632 | | /// </summary> |
| | | 633 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 634 | | public void CopyCorners(Span<Vector3d> destination) |
| | | 635 | | { |
| | 131 | 636 | | if (destination.Length < CornerCount) |
| | 1 | 637 | | throw new ArgumentException($"The destination span must contain at least {CornerCount} elements.", nameof(de |
| | | 638 | | |
| | 130 | 639 | | destination[0] = new Vector3d(Min.X, Min.Y, Min.Z); |
| | 130 | 640 | | destination[1] = new Vector3d(Max.X, Min.Y, Min.Z); |
| | 130 | 641 | | destination[2] = new Vector3d(Min.X, Max.Y, Min.Z); |
| | 130 | 642 | | destination[3] = new Vector3d(Max.X, Max.Y, Min.Z); |
| | 130 | 643 | | destination[4] = new Vector3d(Min.X, Min.Y, Max.Z); |
| | 130 | 644 | | destination[5] = new Vector3d(Max.X, Min.Y, Max.Z); |
| | 130 | 645 | | destination[6] = new Vector3d(Min.X, Max.Y, Max.Z); |
| | 130 | 646 | | destination[7] = new Vector3d(Max.X, Max.Y, Max.Z); |
| | 130 | 647 | | } |
| | | 648 | | |
| | | 649 | | /// <summary> |
| | | 650 | | /// Gets the exact additional volume required for this box's union with |
| | | 651 | | /// <paramref name="other"/>, floored to integer world units and clamped to |
| | | 652 | | /// <see cref="long.MaxValue"/>. |
| | | 653 | | /// </summary> |
| | | 654 | | /// <remarks> |
| | | 655 | | /// The three endpoint spans and both volumes remain in exact unsigned |
| | | 656 | | /// 192-bit arithmetic. This metric is suitable for spatial-index insertion |
| | | 657 | | /// heuristics even when either box has an unrepresentable |
| | | 658 | | /// <see cref="Proportions"/> component. |
| | | 659 | | /// </remarks> |
| | | 660 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 661 | | public long GetVolumeExpansionCost(FixedBoundBox other) => |
| | 5 | 662 | | WideGeometry.GetVolumeExpansionCost(Min, Max, other.Min, other.Max); |
| | | 663 | | |
| | | 664 | | #endregion |
| | | 665 | | |
| | | 666 | | #region Static Ops |
| | | 667 | | |
| | | 668 | | /// <summary> |
| | | 669 | | /// Creates a new bounding box that is the union of two bounding boxes. |
| | | 670 | | /// </summary> |
| | | 671 | | public static FixedBoundBox Union(FixedBoundBox a, FixedBoundBox b) |
| | | 672 | | { |
| | 1 | 673 | | return FromMinMax(Vector3d.Min(a.Min, b.Min), Vector3d.Max(a.Max, b.Max)); |
| | | 674 | | } |
| | | 675 | | |
| | | 676 | | /// <summary> |
| | | 677 | | /// Finds the closest points between two bounding boxes. |
| | | 678 | | /// </summary> |
| | | 679 | | public static Vector3d FindClosestPointsBetweenBoxes(FixedBoundBox a, FixedBoundBox b) |
| | | 680 | | { |
| | 1 | 681 | | Vector3d closestPoint = Vector3d.Zero; |
| | 1 | 682 | | Fixed64 minDistance = Fixed64.MaxValue; |
| | | 683 | | |
| | 18 | 684 | | for (int i = 0; i < CornerCount; i++) |
| | | 685 | | { |
| | 8 | 686 | | Vector3d corner = b.GetCorner(i); |
| | 8 | 687 | | Vector3d point = a.ClosestPointOnSurface(corner); |
| | 8 | 688 | | Fixed64 distance = Vector3d.Distance(point, corner); |
| | 8 | 689 | | if (distance < minDistance) |
| | | 690 | | { |
| | 1 | 691 | | closestPoint = point; |
| | 1 | 692 | | minDistance = distance; |
| | | 693 | | } |
| | | 694 | | } |
| | | 695 | | |
| | 1 | 696 | | return closestPoint; |
| | | 697 | | } |
| | | 698 | | |
| | | 699 | | #endregion |
| | | 700 | | |
| | | 701 | | #region Equality |
| | | 702 | | |
| | | 703 | | /// <summary> |
| | | 704 | | /// Determines whether two FixedBoundBox instances are equal. |
| | | 705 | | /// </summary> |
| | 2 | 706 | | public static bool operator ==(FixedBoundBox left, FixedBoundBox right) => left.Equals(right); |
| | | 707 | | |
| | | 708 | | /// <summary> |
| | | 709 | | /// Determines whether two FixedBoundBox instances are not equal. |
| | | 710 | | /// </summary> |
| | 1 | 711 | | public static bool operator !=(FixedBoundBox left, FixedBoundBox right) => !left.Equals(right); |
| | | 712 | | |
| | | 713 | | #endregion |
| | | 714 | | |
| | | 715 | | #region Equality and HashCode Overrides |
| | | 716 | | |
| | | 717 | | /// <inheritdoc/> |
| | 2 | 718 | | public override bool Equals(object? obj) => obj is FixedBoundBox other && Equals(other); |
| | | 719 | | |
| | | 720 | | /// <inheritdoc/> |
| | | 721 | | public bool Equals(FixedBoundBox other) |
| | 44 | 722 | | => Min.Equals(other.Min) && Max.Equals(other.Max); |
| | | 723 | | |
| | | 724 | | /// <inheritdoc/> |
| | | 725 | | public override int GetHashCode() |
| | | 726 | | { |
| | | 727 | | unchecked |
| | | 728 | | { |
| | 513 | 729 | | int hash = 17; |
| | 513 | 730 | | hash = (hash * 31) + Min.StateHash; |
| | 513 | 731 | | hash = (hash * 31) + Max.StateHash; |
| | 513 | 732 | | return hash; |
| | | 733 | | } |
| | | 734 | | } |
| | | 735 | | |
| | | 736 | | #endregion |
| | | 737 | | |
| | | 738 | | #region Helpers |
| | | 739 | | |
| | | 740 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 741 | | private void SetCenterAndHalfSize(Vector3d center, Vector3d halfSize) |
| | | 742 | | { |
| | 149 | 743 | | if (!Vector3d.TrySubtract(center, halfSize, out Vector3d min) |
| | 149 | 744 | | || !Vector3d.TryAdd(center, halfSize, out Vector3d max)) |
| | | 745 | | { |
| | 5 | 746 | | throw new OverflowException("The centered box places at least one endpoint outside the representable Fixed64 |
| | | 747 | | } |
| | | 748 | | |
| | 144 | 749 | | Min = min; |
| | 144 | 750 | | Max = max; |
| | 144 | 751 | | } |
| | | 752 | | |
| | | 753 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 754 | | private void SetCenterAndHalfSizeClippedToDomain(Vector3d center, Vector3d halfSize) |
| | | 755 | | { |
| | 2 | 756 | | Vector3d min = center - halfSize; |
| | 2 | 757 | | Vector3d max = center + halfSize; |
| | 2 | 758 | | Min = min; |
| | 2 | 759 | | Max = max; |
| | 2 | 760 | | } |
| | | 761 | | |
| | | 762 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 135 | 763 | | private static Vector3d GetHalfSize(Vector3d size) => new( |
| | 135 | 764 | | WideGeometry.GetHalfSizeMagnitude(size.X), |
| | 135 | 765 | | WideGeometry.GetHalfSizeMagnitude(size.Y), |
| | 135 | 766 | | WideGeometry.GetHalfSizeMagnitude(size.Z)); |
| | | 767 | | |
| | | 768 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 12 | 769 | | private static Vector3d GetScopeMagnitude(Vector3d scope) => new( |
| | 12 | 770 | | WideGeometry.GetExtentMagnitude(scope.X), |
| | 12 | 771 | | WideGeometry.GetExtentMagnitude(scope.Y), |
| | 12 | 772 | | WideGeometry.GetExtentMagnitude(scope.Z)); |
| | | 773 | | |
| | | 774 | | #endregion |
| | | 775 | | } |