| | | 1 | | using MemoryPack; |
| | | 2 | | using System; |
| | | 3 | | using System.Runtime.CompilerServices; |
| | | 4 | | using System.Text.Json.Serialization; |
| | | 5 | | |
| | | 6 | | namespace FixedMathSharp; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Represents an axis-aligned bounding box with fixed-point precision, capable of encapsulating 3D objects for more com |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// The BoundingBox provides a more detailed representation of an object's spatial extent compared to BoundingArea. |
| | | 13 | | /// It is useful in 3D scenarios requiring more precise volume fitting and supports a wider range of operations, includi |
| | | 14 | | /// |
| | | 15 | | /// Use Cases: |
| | | 16 | | /// - Encapsulating objects in 3D space for collision detection, ray intersection, or visibility testing. |
| | | 17 | | /// - Used in physics engines, rendering pipelines, and 3D simulations to represent object boundaries. |
| | | 18 | | /// - Supports more precise intersection tests than BoundingArea, making it ideal for detailed spatial queries. |
| | | 19 | | /// </remarks> |
| | | 20 | | [Serializable] |
| | | 21 | | [MemoryPackable] |
| | | 22 | | public partial struct BoundingBox : IEquatable<BoundingBox> |
| | | 23 | | { |
| | | 24 | | #region Nested Types |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Represents the state of a three-dimensional axis-aligned bounding box using its minimum and maximum coordinates. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <remarks> |
| | | 30 | | /// The bounding box is defined by two points: the minimum and maximum corners in 3D space. This |
| | | 31 | | /// structure is immutable and can be used to describe spatial boundaries for geometric computations, collision |
| | | 32 | | /// detection, or spatial queries. |
| | | 33 | | /// </remarks> |
| | | 34 | | [Serializable] |
| | | 35 | | [MemoryPackable] |
| | | 36 | | public readonly partial struct BoundingBoxState |
| | | 37 | | { |
| | | 38 | | /// <inheritdoc cref="BoundingBox.Min"/> |
| | | 39 | | [JsonInclude] |
| | | 40 | | [MemoryPackInclude] |
| | | 41 | | public readonly Vector3d Min; |
| | | 42 | | |
| | | 43 | | /// <inheritdoc cref="BoundingBox.Max"/> |
| | | 44 | | [JsonInclude] |
| | | 45 | | [MemoryPackInclude] |
| | | 46 | | public readonly Vector3d Max; |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Initializes a new instance of the BoundingBoxState class with the specified minimum and maximum coordinates. |
| | | 50 | | /// </summary> |
| | | 51 | | [JsonConstructor] |
| | | 52 | | public BoundingBoxState(Vector3d min, Vector3d max) |
| | 4 | 53 | | { |
| | 4 | 54 | | Min = min; |
| | 4 | 55 | | Max = max; |
| | 4 | 56 | | } |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | #endregion |
| | | 60 | | |
| | | 61 | | #region Fields |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Vertices of the bounding box. |
| | | 65 | | /// </summary> |
| | | 66 | | private Vector3d[] _vertices; |
| | | 67 | | |
| | | 68 | | private bool _isDirty; |
| | | 69 | | |
| | | 70 | | #endregion |
| | | 71 | | |
| | | 72 | | #region Constructors |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Initializes a new instance of the BoundingBox struct with the specified center and size. |
| | | 76 | | /// </summary> |
| | | 77 | | |
| | | 78 | | public BoundingBox(Vector3d center, Vector3d size) |
| | 75 | 79 | | { |
| | 75 | 80 | | Vector3d half = Vector3d.Abs(size) * Fixed64.Half; |
| | | 81 | | |
| | 75 | 82 | | Min = center - half; |
| | 75 | 83 | | Max = center + half; |
| | | 84 | | |
| | 75 | 85 | | _vertices = new Vector3d[8]; |
| | 75 | 86 | | _isDirty = true; |
| | 75 | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Initializes a new instance of the BoundingBox class with the specified bounding box state. |
| | | 91 | | /// </summary> |
| | | 92 | | /// <param name="state">The state that defines the position, size, and orientation of the bounding box.</param> |
| | | 93 | | [JsonConstructor] |
| | | 94 | | [MemoryPackConstructor] |
| | | 95 | | public BoundingBox(BoundingBoxState state) |
| | 3 | 96 | | { |
| | 3 | 97 | | State = state; |
| | 3 | 98 | | _vertices = new Vector3d[8]; |
| | 3 | 99 | | } |
| | | 100 | | |
| | | 101 | | #endregion |
| | | 102 | | |
| | | 103 | | #region Properties |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// The minimum corner of the bounding box. |
| | | 107 | | /// </summary> |
| | | 108 | | [JsonIgnore] |
| | | 109 | | [MemoryPackIgnore] |
| | | 110 | | public Vector3d Min { get; private set; } |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// The maximum corner of the bounding box. |
| | | 114 | | /// </summary> |
| | | 115 | | [JsonIgnore] |
| | | 116 | | [MemoryPackIgnore] |
| | | 117 | | public Vector3d Max { get; private set; } |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// The center of the bounding box. |
| | | 121 | | /// </summary> |
| | | 122 | | [JsonIgnore] |
| | | 123 | | [MemoryPackIgnore] |
| | | 124 | | public Vector3d Center |
| | | 125 | | { |
| | | 126 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 9 | 127 | | get => (Min + Max) * Fixed64.Half; |
| | | 128 | | |
| | | 129 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 130 | | set |
| | 1 | 131 | | { |
| | 1 | 132 | | Vector3d half = (Max - Min) * Fixed64.Half; |
| | 1 | 133 | | Min = value - half; |
| | 1 | 134 | | Max = value + half; |
| | 1 | 135 | | _isDirty = true; |
| | 1 | 136 | | } |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | /// <summary> |
| | | 140 | | /// The total size of the box (Width, Height, Depth). This is always twice the scope. |
| | | 141 | | /// </summary> |
| | | 142 | | [JsonIgnore] |
| | | 143 | | [MemoryPackIgnore] |
| | | 144 | | public Vector3d Proportions |
| | | 145 | | { |
| | | 146 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 7 | 147 | | get => Max - Min; |
| | | 148 | | |
| | | 149 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 150 | | set |
| | 1 | 151 | | { |
| | 1 | 152 | | Vector3d half = value * Fixed64.Half; |
| | 1 | 153 | | Vector3d center = (Min + Max) * Fixed64.Half; |
| | 1 | 154 | | Min = center - half; |
| | 1 | 155 | | Max = center + half; |
| | 1 | 156 | | _isDirty = true; |
| | 1 | 157 | | } |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | /// <summary> |
| | | 161 | | /// The range (half-size) of the bounding box in all directions. Always half of the total size. |
| | | 162 | | /// </summary> |
| | | 163 | | [JsonIgnore] |
| | | 164 | | [MemoryPackIgnore] |
| | | 165 | | public Vector3d Scope |
| | | 166 | | { |
| | | 167 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 0 | 168 | | get => (Max - Min) * Fixed64.Half; |
| | | 169 | | } |
| | | 170 | | |
| | | 171 | | /// <inheritdoc cref="_vertices" /> |
| | | 172 | | [JsonIgnore] |
| | | 173 | | [MemoryPackIgnore] |
| | | 174 | | public Vector3d[] Vertices |
| | | 175 | | { |
| | | 176 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 29 | 177 | | get => GetOrGenerateVertices(); |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | /// <summary> |
| | | 181 | | /// Gets or sets the current bounding box state, including its minimum and maximum coordinates. |
| | | 182 | | /// </summary> |
| | | 183 | | [JsonInclude] |
| | | 184 | | [MemoryPackInclude] |
| | | 185 | | public BoundingBoxState State |
| | | 186 | | { |
| | 3 | 187 | | get => new(Min, Max); |
| | | 188 | | |
| | | 189 | | |
| | | 190 | | internal set |
| | 3 | 191 | | { |
| | 3 | 192 | | Min = value.Min; |
| | 3 | 193 | | Max = value.Max; |
| | 3 | 194 | | _isDirty = true; |
| | 3 | 195 | | } |
| | | 196 | | } |
| | | 197 | | |
| | | 198 | | #endregion |
| | | 199 | | |
| | | 200 | | #region Mutators |
| | | 201 | | |
| | | 202 | | /// <summary> |
| | | 203 | | /// Orients the bounding box with the given center and size. |
| | | 204 | | /// </summary> |
| | | 205 | | public void Orient(Vector3d center, Vector3d? size) |
| | 2 | 206 | | { |
| | 2 | 207 | | Vector3d half = size.HasValue |
| | 2 | 208 | | ? size.Value * Fixed64.Half |
| | 2 | 209 | | : (Max - Min) * Fixed64.Half; |
| | | 210 | | |
| | 2 | 211 | | Min = center - half; |
| | 2 | 212 | | Max = center + half; |
| | 2 | 213 | | _isDirty = true; |
| | 2 | 214 | | } |
| | | 215 | | |
| | | 216 | | /// <summary> |
| | | 217 | | /// Resizes the bounding box to the specified size, keeping the same center. |
| | | 218 | | /// </summary> |
| | | 219 | | public void Resize(Vector3d size) |
| | 3 | 220 | | { |
| | 3 | 221 | | Vector3d half = size * Fixed64.Half; |
| | 3 | 222 | | Vector3d center = (Min + Max) * Fixed64.Half; |
| | | 223 | | |
| | 3 | 224 | | Min = center - half; |
| | 3 | 225 | | Max = center + half; |
| | 3 | 226 | | _isDirty = true; |
| | 3 | 227 | | } |
| | | 228 | | |
| | | 229 | | /// <summary> |
| | | 230 | | /// Sets the bounds of the bounding box by specifying its minimum and maximum points. |
| | | 231 | | /// </summary> |
| | | 232 | | public void SetMinMax(Vector3d min, Vector3d max) |
| | 1 | 233 | | { |
| | 1 | 234 | | Min = min; |
| | 1 | 235 | | Max = max; |
| | 1 | 236 | | _isDirty = true; |
| | 1 | 237 | | } |
| | | 238 | | |
| | | 239 | | /// <summary> |
| | | 240 | | /// Configures the bounding box with the specified center and scope (half-size). |
| | | 241 | | /// </summary> |
| | | 242 | | public void SetBoundingBox(Vector3d center, Vector3d scope) |
| | 1 | 243 | | { |
| | 1 | 244 | | Min = center - scope; |
| | 1 | 245 | | Max = center + scope; |
| | 1 | 246 | | _isDirty = true; |
| | 1 | 247 | | } |
| | | 248 | | |
| | | 249 | | /// <summary> |
| | | 250 | | /// Determines if a point is inside the bounding box (including boundaries). |
| | | 251 | | /// </summary> |
| | | 252 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 253 | | public bool Contains(Vector3d point) |
| | 51 | 254 | | { |
| | 51 | 255 | | return point.x >= Min.x && point.x <= Max.x |
| | 51 | 256 | | && point.y >= Min.y && point.y <= Max.y |
| | 51 | 257 | | && point.z >= Min.z && point.z <= Max.z; |
| | 51 | 258 | | } |
| | | 259 | | |
| | | 260 | | /// <summary> |
| | | 261 | | /// Tests another bounding box against this bounding box. |
| | | 262 | | /// </summary> |
| | | 263 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 264 | | public ContainmentType Contains(BoundingBox box) => ContainsBoxLike(box.Min, box.Max); |
| | | 265 | | |
| | | 266 | | /// <summary> |
| | | 267 | | /// Tests a bounding area against this bounding box. |
| | | 268 | | /// </summary> |
| | | 269 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 270 | | public ContainmentType Contains(BoundingArea area) => ContainsBoxLike(area.Min, area.Max); |
| | | 271 | | |
| | | 272 | | /// <summary> |
| | | 273 | | /// Tests a bounding sphere against this bounding box. |
| | | 274 | | /// </summary> |
| | | 275 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 276 | | public ContainmentType Contains(BoundingSphere sphere) |
| | 3 | 277 | | { |
| | 3 | 278 | | if (Contains(sphere.Min) && Contains(sphere.Max)) |
| | 1 | 279 | | return ContainmentType.Contains; |
| | | 280 | | |
| | 2 | 281 | | return Intersects(sphere) ? ContainmentType.Intersects : ContainmentType.Disjoint; |
| | 3 | 282 | | } |
| | | 283 | | |
| | | 284 | | /// <summary> |
| | | 285 | | /// Tests a bounding frustum against this bounding box. |
| | | 286 | | /// </summary> |
| | | 287 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 288 | | public ContainmentType Contains(BoundingFrustum frustum) |
| | 4 | 289 | | { |
| | 4 | 290 | | if (frustum == null) |
| | 1 | 291 | | throw new ArgumentNullException(nameof(frustum)); |
| | | 292 | | |
| | 3 | 293 | | if (Contains(frustum.Min) && Contains(frustum.Max)) |
| | 1 | 294 | | return ContainmentType.Contains; |
| | | 295 | | |
| | 2 | 296 | | return Intersects(frustum) ? ContainmentType.Intersects : ContainmentType.Disjoint; |
| | 3 | 297 | | } |
| | | 298 | | |
| | | 299 | | /// <summary> |
| | | 300 | | /// Checks whether another bounding box intersects this bounding box. |
| | | 301 | | /// </summary> |
| | | 302 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 5 | 303 | | public bool Intersects(BoundingBox box) => IntersectsBoxLike(box.Min, box.Max); |
| | | 304 | | |
| | | 305 | | /// <summary> |
| | | 306 | | /// Checks whether a bounding area intersects this bounding box. |
| | | 307 | | /// </summary> |
| | | 308 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 309 | | public bool Intersects(BoundingArea area) => IntersectsBoxLike(area.Min, area.Max); |
| | | 310 | | |
| | | 311 | | /// <summary> |
| | | 312 | | /// Checks whether a bounding sphere intersects this bounding box. |
| | | 313 | | /// </summary> |
| | | 314 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 3 | 315 | | public bool Intersects(BoundingSphere sphere) => IntersectsSphere(sphere); |
| | | 316 | | |
| | | 317 | | /// <summary> |
| | | 318 | | /// Checks whether a bounding frustum intersects this bounding box. |
| | | 319 | | /// </summary> |
| | | 320 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 321 | | public bool Intersects(BoundingFrustum frustum) |
| | 5 | 322 | | { |
| | 5 | 323 | | if (frustum == null) |
| | 1 | 324 | | throw new ArgumentNullException(nameof(frustum)); |
| | | 325 | | |
| | 4 | 326 | | return frustum.Intersects(this); |
| | 4 | 327 | | } |
| | | 328 | | |
| | | 329 | | /// <summary> |
| | | 330 | | /// Projects a point into the bounding box by clamping it to the box extents. |
| | | 331 | | /// </summary> |
| | | 332 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 333 | | public Vector3d ProjectPoint(Vector3d point) |
| | 2 | 334 | | => ClampPoint(point); |
| | | 335 | | |
| | | 336 | | /// <summary> |
| | | 337 | | /// Clamps a point to this bounding box, returning the point unchanged when it is already inside. |
| | | 338 | | /// </summary> |
| | | 339 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 340 | | public Vector3d ClampPoint(Vector3d point) |
| | 6 | 341 | | { |
| | 6 | 342 | | return new Vector3d( |
| | 6 | 343 | | FixedMath.Clamp(point.x, Min.x, Max.x), |
| | 6 | 344 | | FixedMath.Clamp(point.y, Min.y, Max.y), |
| | 6 | 345 | | FixedMath.Clamp(point.z, Min.z, Max.z)); |
| | 6 | 346 | | } |
| | | 347 | | |
| | | 348 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 349 | | private ContainmentType ContainsBoxLike(Vector3d otherMin, Vector3d otherMax) |
| | 3 | 350 | | { |
| | 3 | 351 | | if (Contains(otherMin) && Contains(otherMax)) |
| | 1 | 352 | | return ContainmentType.Contains; |
| | | 353 | | |
| | 2 | 354 | | return IntersectsBoxLike(otherMin, otherMax) |
| | 2 | 355 | | ? ContainmentType.Intersects |
| | 2 | 356 | | : ContainmentType.Disjoint; |
| | 3 | 357 | | } |
| | | 358 | | |
| | | 359 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 360 | | private bool IntersectsBoxLike(Vector3d otherMin, Vector3d otherMax) |
| | 8 | 361 | | { |
| | 8 | 362 | | return Contains(otherMin) && Contains(otherMax) |
| | 8 | 363 | | || HasStrictAxisOverlap(otherMin, otherMax); |
| | 8 | 364 | | } |
| | | 365 | | |
| | | 366 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 367 | | private bool IntersectsSphere(BoundingSphere sphere) |
| | 3 | 368 | | { |
| | 3 | 369 | | return Vector3d.SqrDistance(sphere.Center, ClampPoint(sphere.Center)) <= sphere.SqrRadius; |
| | 3 | 370 | | } |
| | | 371 | | |
| | | 372 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 373 | | private bool HasStrictAxisOverlap(Vector3d otherMin, Vector3d otherMax) |
| | 8 | 374 | | { |
| | 8 | 375 | | return !(Max.x <= otherMin.x || Min.x >= otherMax.x || |
| | 8 | 376 | | Max.y <= otherMin.y || Min.y >= otherMax.y || |
| | 8 | 377 | | Max.z <= otherMin.z || Min.z >= otherMax.z); |
| | 8 | 378 | | } |
| | | 379 | | |
| | | 380 | | /// <summary> |
| | | 381 | | /// Calculates the shortest distance from a given point to the surface of the bounding box. |
| | | 382 | | /// If the point lies inside the box, the distance is zero. |
| | | 383 | | /// </summary> |
| | | 384 | | /// <param name="point">The point from which to calculate the distance.</param> |
| | | 385 | | /// <returns> |
| | | 386 | | /// The shortest distance from the point to the surface of the bounding box. |
| | | 387 | | /// If the point is inside the box, the method returns zero. |
| | | 388 | | /// </returns> |
| | | 389 | | /// <remarks> |
| | | 390 | | /// The method finds the closest point on the box's surface by clamping the given point |
| | | 391 | | /// to the box's bounds and returns the Euclidean distance between them. |
| | | 392 | | /// This ensures accurate distance calculations, even near corners or edges. |
| | | 393 | | /// </remarks> |
| | | 394 | | public Fixed64 DistanceToSurface(Vector3d point) |
| | 2 | 395 | | { |
| | | 396 | | // Clamp the point to the nearest point on the box's surface |
| | 2 | 397 | | Vector3d clampedPoint = new( |
| | 2 | 398 | | FixedMath.Clamp(point.x, Min.x, Max.x), |
| | 2 | 399 | | FixedMath.Clamp(point.y, Min.y, Max.y), |
| | 2 | 400 | | FixedMath.Clamp(point.z, Min.z, Max.z) |
| | 2 | 401 | | ); |
| | | 402 | | |
| | | 403 | | // If the point is inside the box, return 0 |
| | 2 | 404 | | if (Contains(point)) |
| | 1 | 405 | | return Fixed64.Zero; |
| | | 406 | | |
| | | 407 | | // Otherwise, return the Euclidean distance to the clamped point |
| | 1 | 408 | | return Vector3d.Distance(point, clampedPoint); |
| | 2 | 409 | | } |
| | | 410 | | |
| | | 411 | | /// <summary> |
| | | 412 | | /// Finds the closest point on the surface of the bounding box towards a specified object position. |
| | | 413 | | /// </summary> |
| | | 414 | | public Vector3d GetPointOnSurfaceTowardsObject(Vector3d objectPosition) |
| | 1 | 415 | | => ClosestPointOnSurface(ProjectPoint(objectPosition)); |
| | | 416 | | |
| | | 417 | | /// <summary> |
| | | 418 | | /// Finds the closest point on the surface of the bounding box to the specified point. |
| | | 419 | | /// </summary> |
| | | 420 | | public Vector3d ClosestPointOnSurface(Vector3d point) |
| | 16 | 421 | | { |
| | 16 | 422 | | if (Contains(point)) |
| | 7 | 423 | | { |
| | | 424 | | // Calculate distances to each face and return the closest face. |
| | 7 | 425 | | Fixed64 distToMinX = point.x - Min.x; |
| | 7 | 426 | | Fixed64 distToMaxX = Max.x - point.x; |
| | 7 | 427 | | Fixed64 distToMinY = point.y - Min.y; |
| | 7 | 428 | | Fixed64 distToMaxY = Max.y - point.y; |
| | 7 | 429 | | Fixed64 distToMinZ = point.z - Min.z; |
| | 7 | 430 | | Fixed64 distToMaxZ = Max.z - point.z; |
| | | 431 | | |
| | 7 | 432 | | Fixed64 minDistToFace = FixedMath.Min(distToMinX, |
| | 7 | 433 | | FixedMath.Min(distToMaxX, |
| | 7 | 434 | | FixedMath.Min(distToMinY, |
| | 7 | 435 | | FixedMath.Min(distToMaxY, |
| | 7 | 436 | | FixedMath.Min(distToMinZ, distToMaxZ))))); |
| | | 437 | | |
| | | 438 | | // Adjust the closest point based on the face. |
| | 8 | 439 | | if (minDistToFace == distToMinX) point.x = Min.x; |
| | 8 | 440 | | else if (minDistToFace == distToMaxX) point.x = Max.x; |
| | | 441 | | |
| | 8 | 442 | | if (minDistToFace == distToMinY) point.y = Min.y; |
| | 7 | 443 | | else if (minDistToFace == distToMaxY) point.y = Max.y; |
| | | 444 | | |
| | 8 | 445 | | if (minDistToFace == distToMinZ) point.z = Min.z; |
| | 7 | 446 | | else if (minDistToFace == distToMaxZ) point.z = Max.z; |
| | | 447 | | |
| | 7 | 448 | | return point; |
| | | 449 | | } |
| | | 450 | | |
| | | 451 | | // If the point is outside the box, clamp to the nearest surface. |
| | 9 | 452 | | return new Vector3d( |
| | 9 | 453 | | FixedMath.Clamp(point.x, Min.x, Max.x), |
| | 9 | 454 | | FixedMath.Clamp(point.y, Min.y, Max.y), |
| | 9 | 455 | | FixedMath.Clamp(point.z, Min.z, Max.z) |
| | 9 | 456 | | ); |
| | 16 | 457 | | } |
| | | 458 | | |
| | | 459 | | /// <summary> |
| | | 460 | | /// Generates the vertices of the bounding box based on its center and scope. |
| | | 461 | | /// </summary> |
| | | 462 | | /// <remarks> |
| | | 463 | | /// Vertices[0] near Bot left |
| | | 464 | | /// Vertices[1] near Bot right |
| | | 465 | | /// Vertices[2] near Top left |
| | | 466 | | /// Vertices[3] near Top right |
| | | 467 | | /// Vertices[4] far bot left |
| | | 468 | | /// Vertices[5] far bot right |
| | | 469 | | /// Vertices[6] far top left |
| | | 470 | | /// Vertices[7] far top right |
| | | 471 | | /// ---- |
| | | 472 | | /// near quad |
| | | 473 | | /// 0 - 1 Bot left near to bot right near |
| | | 474 | | /// 2 - 3 Top left near to top right near |
| | | 475 | | /// 0 - 2 Bot left near to top left near |
| | | 476 | | /// 1 - 3 Bot right near to top right near |
| | | 477 | | /// far quad |
| | | 478 | | /// 4 - 5 Bot left far to bot right far |
| | | 479 | | /// 6 - 7 Top left far to top right far |
| | | 480 | | /// 4 - 6 Bot left far to top left far |
| | | 481 | | /// 5 - 7 Bot right far to top right far |
| | | 482 | | /// lines connecting near and far quads |
| | | 483 | | /// 0 - 4 Bot left near to bot left far |
| | | 484 | | /// 1 - 5 Bot right near to bot right far |
| | | 485 | | /// 2 - 6 Top left near to top left far |
| | | 486 | | /// 3 - 7 Top right near to top right far |
| | | 487 | | /// </remarks> |
| | | 488 | | private Vector3d[] GetOrGenerateVertices() |
| | 29 | 489 | | { |
| | 29 | 490 | | _vertices ??= new Vector3d[8]; |
| | | 491 | | |
| | 29 | 492 | | if (_isDirty) |
| | 4 | 493 | | { |
| | 4 | 494 | | Vector3d min = Min; |
| | 4 | 495 | | Vector3d max = Max; |
| | | 496 | | |
| | 4 | 497 | | _vertices[0] = new(min.x, min.y, min.z); |
| | 4 | 498 | | _vertices[1] = new(max.x, min.y, min.z); |
| | 4 | 499 | | _vertices[2] = new(min.x, max.y, min.z); |
| | 4 | 500 | | _vertices[3] = new(max.x, max.y, min.z); |
| | 4 | 501 | | _vertices[4] = new(min.x, min.y, max.z); |
| | 4 | 502 | | _vertices[5] = new(max.x, min.y, max.z); |
| | 4 | 503 | | _vertices[6] = new(min.x, max.y, max.z); |
| | 4 | 504 | | _vertices[7] = new(max.x, max.y, max.z); |
| | | 505 | | |
| | 4 | 506 | | _isDirty = false; |
| | 4 | 507 | | } |
| | | 508 | | |
| | 29 | 509 | | return _vertices; |
| | 29 | 510 | | } |
| | | 511 | | |
| | | 512 | | #endregion |
| | | 513 | | |
| | | 514 | | #region Static Ops |
| | | 515 | | |
| | | 516 | | /// <summary> |
| | | 517 | | /// Creates a new bounding box that is the union of two bounding boxes. |
| | | 518 | | /// </summary> |
| | | 519 | | public static BoundingBox Union(BoundingBox a, BoundingBox b) |
| | 1 | 520 | | { |
| | 1 | 521 | | return new BoundingBox |
| | 1 | 522 | | { |
| | 1 | 523 | | Min = Vector3d.Min(a.Min, b.Min), |
| | 1 | 524 | | Max = Vector3d.Max(a.Max, b.Max), |
| | 1 | 525 | | _isDirty = true |
| | 1 | 526 | | }; |
| | 1 | 527 | | } |
| | | 528 | | |
| | | 529 | | /// <summary> |
| | | 530 | | /// Finds the closest points between two bounding boxes. |
| | | 531 | | /// </summary> |
| | | 532 | | public static Vector3d FindClosestPointsBetweenBoxes(BoundingBox a, BoundingBox b) |
| | 1 | 533 | | { |
| | 1 | 534 | | Vector3d closestPoint = Vector3d.Zero; |
| | 1 | 535 | | Fixed64 minDistance = Fixed64.MAX_VALUE; |
| | | 536 | | |
| | 18 | 537 | | for (int i = 0; i < b.Vertices.Length; i++) |
| | 8 | 538 | | { |
| | 8 | 539 | | Vector3d point = a.ClosestPointOnSurface(b.Vertices[i]); |
| | 8 | 540 | | Fixed64 distance = Vector3d.Distance(point, b.Vertices[i]); |
| | 8 | 541 | | if (distance < minDistance) |
| | 1 | 542 | | { |
| | 1 | 543 | | closestPoint = point; |
| | 1 | 544 | | minDistance = distance; |
| | 1 | 545 | | } |
| | 8 | 546 | | } |
| | | 547 | | |
| | 1 | 548 | | return closestPoint; |
| | 1 | 549 | | } |
| | | 550 | | |
| | | 551 | | #endregion |
| | | 552 | | |
| | | 553 | | #region Equality |
| | | 554 | | |
| | | 555 | | /// <summary> |
| | | 556 | | /// Determines whether two BoundingBox instances are equal. |
| | | 557 | | /// </summary> |
| | 2 | 558 | | public static bool operator ==(BoundingBox left, BoundingBox right) => left.Equals(right); |
| | | 559 | | |
| | | 560 | | /// <summary> |
| | | 561 | | /// Determines whether two BoundingBox instances are not equal. |
| | | 562 | | /// </summary> |
| | 1 | 563 | | public static bool operator !=(BoundingBox left, BoundingBox right) => !left.Equals(right); |
| | | 564 | | |
| | | 565 | | #endregion |
| | | 566 | | |
| | | 567 | | #region Equality and HashCode Overrides |
| | | 568 | | |
| | | 569 | | /// <inheritdoc/> |
| | 2 | 570 | | public override bool Equals(object? obj) => obj is BoundingBox other && Equals(other); |
| | | 571 | | |
| | | 572 | | /// <inheritdoc/> |
| | | 573 | | public bool Equals(BoundingBox other) |
| | 6 | 574 | | => Min.Equals(other.Min) && Max.Equals(other.Max); |
| | | 575 | | |
| | | 576 | | /// <inheritdoc/> |
| | | 577 | | public override int GetHashCode() |
| | 2 | 578 | | { |
| | 2 | 579 | | return HashCode.Combine(Min, Max); |
| | 2 | 580 | | } |
| | | 581 | | |
| | | 582 | | #endregion |
| | | 583 | | } |