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