| | | 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 : IBound, IEquatable<BoundingBox> |
| | | 23 | | { |
| | | 24 | | #region Nested Types |
| | | 25 | | |
| | | 26 | | [Serializable] |
| | | 27 | | [MemoryPackable] |
| | | 28 | | public readonly partial struct BoundingBoxState |
| | | 29 | | { |
| | | 30 | | [JsonInclude] |
| | | 31 | | [MemoryPackInclude] |
| | | 32 | | public readonly Vector3d Min; |
| | | 33 | | |
| | | 34 | | [JsonInclude] |
| | | 35 | | [MemoryPackInclude] |
| | | 36 | | public readonly Vector3d Max; |
| | | 37 | | |
| | | 38 | | [JsonConstructor] |
| | | 39 | | public BoundingBoxState(Vector3d min, Vector3d max) |
| | 4 | 40 | | { |
| | 4 | 41 | | Min = min; |
| | 4 | 42 | | Max = max; |
| | 4 | 43 | | } |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | #endregion |
| | | 47 | | |
| | | 48 | | #region Fields |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Vertices of the bounding box. |
| | | 52 | | /// </summary> |
| | | 53 | | private Vector3d[] _vertices; |
| | | 54 | | |
| | | 55 | | private bool _isDirty; |
| | | 56 | | |
| | | 57 | | #endregion |
| | | 58 | | |
| | | 59 | | #region Constructors |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Initializes a new instance of the BoundingBox struct with the specified center and size. |
| | | 63 | | /// </summary> |
| | | 64 | | |
| | | 65 | | public BoundingBox(Vector3d center, Vector3d size) |
| | 51 | 66 | | { |
| | 51 | 67 | | Vector3d half = Vector3d.Abs(size) * Fixed64.Half; |
| | | 68 | | |
| | 51 | 69 | | Min = center - half; |
| | 51 | 70 | | Max = center + half; |
| | | 71 | | |
| | 51 | 72 | | _vertices = new Vector3d[8]; |
| | 51 | 73 | | _isDirty = true; |
| | 51 | 74 | | } |
| | | 75 | | |
| | | 76 | | [JsonConstructor] |
| | | 77 | | [MemoryPackConstructor] |
| | | 78 | | public BoundingBox(BoundingBoxState state) |
| | 3 | 79 | | { |
| | 3 | 80 | | State = state; |
| | 3 | 81 | | _vertices = new Vector3d[8]; |
| | 3 | 82 | | } |
| | | 83 | | |
| | | 84 | | #endregion |
| | | 85 | | |
| | | 86 | | #region Properties |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// The minimum corner of the bounding box. |
| | | 90 | | /// </summary> |
| | | 91 | | [JsonIgnore] |
| | | 92 | | [MemoryPackIgnore] |
| | 253 | 93 | | public Vector3d Min { get; private set; } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// The maximum corner of the bounding box. |
| | | 97 | | /// </summary> |
| | | 98 | | [JsonIgnore] |
| | | 99 | | [MemoryPackIgnore] |
| | 248 | 100 | | public Vector3d Max { get; private set; } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// The center of the bounding box. |
| | | 104 | | /// </summary> |
| | | 105 | | [JsonIgnore] |
| | | 106 | | [MemoryPackIgnore] |
| | | 107 | | public Vector3d Center |
| | | 108 | | { |
| | | 109 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 7 | 110 | | get => (Min + Max) * Fixed64.Half; |
| | | 111 | | |
| | | 112 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 113 | | set |
| | 1 | 114 | | { |
| | 1 | 115 | | Vector3d half = (Max - Min) * Fixed64.Half; |
| | 1 | 116 | | Min = value - half; |
| | 1 | 117 | | Max = value + half; |
| | 1 | 118 | | _isDirty = true; |
| | 1 | 119 | | } |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// The total size of the box (Width, Height, Depth). This is always twice the scope. |
| | | 124 | | /// </summary> |
| | | 125 | | [JsonIgnore] |
| | | 126 | | [MemoryPackIgnore] |
| | | 127 | | public Vector3d Proportions |
| | | 128 | | { |
| | | 129 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 7 | 130 | | get => Max - Min; |
| | | 131 | | |
| | | 132 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 133 | | set |
| | 1 | 134 | | { |
| | 1 | 135 | | Vector3d half = value * Fixed64.Half; |
| | 1 | 136 | | Vector3d center = (Min + Max) * Fixed64.Half; |
| | 1 | 137 | | Min = center - half; |
| | 1 | 138 | | Max = center + half; |
| | 1 | 139 | | _isDirty = true; |
| | 1 | 140 | | } |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// The range (half-size) of the bounding box in all directions. Always half of the total size. |
| | | 145 | | /// </summary> |
| | | 146 | | [JsonIgnore] |
| | | 147 | | [MemoryPackIgnore] |
| | | 148 | | public Vector3d Scope |
| | | 149 | | { |
| | | 150 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 0 | 151 | | get => (Max - Min) * Fixed64.Half; |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | /// <inheritdoc cref="_vertices" /> |
| | | 155 | | [JsonIgnore] |
| | | 156 | | [MemoryPackIgnore] |
| | | 157 | | public Vector3d[] Vertices |
| | | 158 | | { |
| | | 159 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 27 | 160 | | get => GetOrGenerateVertices(); |
| | | 161 | | } |
| | | 162 | | |
| | | 163 | | [JsonInclude] |
| | | 164 | | [MemoryPackInclude] |
| | | 165 | | public BoundingBoxState State |
| | | 166 | | { |
| | 3 | 167 | | get => new(Min, Max); |
| | | 168 | | |
| | | 169 | | |
| | | 170 | | internal set |
| | 3 | 171 | | { |
| | 3 | 172 | | Min = value.Min; |
| | 3 | 173 | | Max = value.Max; |
| | 3 | 174 | | _isDirty = true; |
| | 3 | 175 | | } |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | #endregion |
| | | 179 | | |
| | | 180 | | #region Mutators |
| | | 181 | | |
| | | 182 | | /// <summary> |
| | | 183 | | /// Orients the bounding box with the given center and size. |
| | | 184 | | /// </summary> |
| | | 185 | | public void Orient(Vector3d center, Vector3d? size) |
| | 2 | 186 | | { |
| | 2 | 187 | | Vector3d half = size.HasValue |
| | 2 | 188 | | ? size.Value * Fixed64.Half |
| | 2 | 189 | | : (Max - Min) * Fixed64.Half; |
| | | 190 | | |
| | 2 | 191 | | Min = center - half; |
| | 2 | 192 | | Max = center + half; |
| | 2 | 193 | | _isDirty = true; |
| | 2 | 194 | | } |
| | | 195 | | |
| | | 196 | | /// <summary> |
| | | 197 | | /// Resizes the bounding box to the specified size, keeping the same center. |
| | | 198 | | /// </summary> |
| | | 199 | | public void Resize(Vector3d size) |
| | 3 | 200 | | { |
| | 3 | 201 | | Vector3d half = size * Fixed64.Half; |
| | 3 | 202 | | Vector3d center = (Min + Max) * Fixed64.Half; |
| | | 203 | | |
| | 3 | 204 | | Min = center - half; |
| | 3 | 205 | | Max = center + half; |
| | 3 | 206 | | _isDirty = true; |
| | 3 | 207 | | } |
| | | 208 | | |
| | | 209 | | /// <summary> |
| | | 210 | | /// Sets the bounds of the bounding box by specifying its minimum and maximum points. |
| | | 211 | | /// </summary> |
| | | 212 | | public void SetMinMax(Vector3d min, Vector3d max) |
| | 1 | 213 | | { |
| | 1 | 214 | | Min = min; |
| | 1 | 215 | | Max = max; |
| | 1 | 216 | | _isDirty = true; |
| | 1 | 217 | | } |
| | | 218 | | |
| | | 219 | | /// <summary> |
| | | 220 | | /// Configures the bounding box with the specified center and scope (half-size). |
| | | 221 | | /// </summary> |
| | | 222 | | public void SetBoundingBox(Vector3d center, Vector3d scope) |
| | 1 | 223 | | { |
| | 1 | 224 | | Min = center - scope; |
| | 1 | 225 | | Max = center + scope; |
| | 1 | 226 | | _isDirty = true; |
| | 1 | 227 | | } |
| | | 228 | | |
| | | 229 | | /// <summary> |
| | | 230 | | /// Determines if a point is inside the bounding box (including boundaries). |
| | | 231 | | /// </summary> |
| | | 232 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 233 | | public bool Contains(Vector3d point) |
| | 31 | 234 | | { |
| | 31 | 235 | | return point.x >= Min.x && point.x <= Max.x |
| | 31 | 236 | | && point.y >= Min.y && point.y <= Max.y |
| | 31 | 237 | | && point.z >= Min.z && point.z <= Max.z; |
| | 31 | 238 | | } |
| | | 239 | | |
| | | 240 | | /// <summary> |
| | | 241 | | /// Checks if another IBound intersects with this bounding box. |
| | | 242 | | /// Ensures overlap, not just touching boundaries. |
| | | 243 | | /// </summary> |
| | | 244 | | /// <remarks> |
| | | 245 | | /// It checks for overlap on all axes. If there is no overlap on any axis, they do not intersect. |
| | | 246 | | /// </remarks> |
| | | 247 | | public bool Intersects(IBound other) |
| | 8 | 248 | | { |
| | 8 | 249 | | switch (other) |
| | | 250 | | { |
| | | 251 | | case BoundingBox or BoundingArea: |
| | 6 | 252 | | { |
| | 6 | 253 | | if (Contains(other.Min) && Contains(other.Max)) |
| | 0 | 254 | | return true; // Full containment |
| | | 255 | | |
| | | 256 | | // General intersection logic (allowing for overlap) |
| | 6 | 257 | | return !(Max.x <= other.Min.x || Min.x >= other.Max.x || |
| | 6 | 258 | | Max.y <= other.Min.y || Min.y >= other.Max.y || |
| | 6 | 259 | | Max.z <= other.Min.z || Min.z >= other.Max.z); |
| | | 260 | | } |
| | | 261 | | case BoundingSphere sphere: |
| | | 262 | | // project the sphere’s center onto the 3D volume and checks the distance to the surface. |
| | | 263 | | // If the distance from the closest point to the center is less than or equal to the sphere’s radius, th |
| | 1 | 264 | | return Vector3d.SqrDistance(sphere.Center, this.ProjectPointWithinBounds(sphere.Center)) <= sphere.SqrRa |
| | | 265 | | |
| | | 266 | | default: |
| | 1 | 267 | | return false; // Default case for unknown or unsupported types |
| | | 268 | | } |
| | 8 | 269 | | } |
| | | 270 | | |
| | | 271 | | /// <summary> |
| | | 272 | | /// Projects a point onto the bounding box. If the point is outside the box, it returns the closest point on the sur |
| | | 273 | | /// </summary> |
| | | 274 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 275 | | public Vector3d ProjectPoint(Vector3d point) |
| | 2 | 276 | | => this.ProjectPointWithinBounds(point); |
| | | 277 | | |
| | | 278 | | /// <summary> |
| | | 279 | | /// Calculates the shortest distance from a given point to the surface of the bounding box. |
| | | 280 | | /// If the point lies inside the box, the distance is zero. |
| | | 281 | | /// </summary> |
| | | 282 | | /// <param name="point">The point from which to calculate the distance.</param> |
| | | 283 | | /// <returns> |
| | | 284 | | /// The shortest distance from the point to the surface of the bounding box. |
| | | 285 | | /// If the point is inside the box, the method returns zero. |
| | | 286 | | /// </returns> |
| | | 287 | | /// <remarks> |
| | | 288 | | /// The method finds the closest point on the box's surface by clamping the given point |
| | | 289 | | /// to the box's bounds and returns the Euclidean distance between them. |
| | | 290 | | /// This ensures accurate distance calculations, even near corners or edges. |
| | | 291 | | /// </remarks> |
| | | 292 | | public Fixed64 DistanceToSurface(Vector3d point) |
| | 2 | 293 | | { |
| | | 294 | | // Clamp the point to the nearest point on the box's surface |
| | 2 | 295 | | Vector3d clampedPoint = new( |
| | 2 | 296 | | FixedMath.Clamp(point.x, Min.x, Max.x), |
| | 2 | 297 | | FixedMath.Clamp(point.y, Min.y, Max.y), |
| | 2 | 298 | | FixedMath.Clamp(point.z, Min.z, Max.z) |
| | 2 | 299 | | ); |
| | | 300 | | |
| | | 301 | | // If the point is inside the box, return 0 |
| | 2 | 302 | | if (Contains(point)) |
| | 1 | 303 | | return Fixed64.Zero; |
| | | 304 | | |
| | | 305 | | // Otherwise, return the Euclidean distance to the clamped point |
| | 1 | 306 | | return Vector3d.Distance(point, clampedPoint); |
| | 2 | 307 | | } |
| | | 308 | | |
| | | 309 | | /// <summary> |
| | | 310 | | /// Finds the closest point on the surface of the bounding box towards a specified object position. |
| | | 311 | | /// </summary> |
| | | 312 | | public Vector3d GetPointOnSurfaceTowardsObject(Vector3d objectPosition) |
| | 1 | 313 | | => ClosestPointOnSurface(ProjectPoint(objectPosition)); |
| | | 314 | | |
| | | 315 | | /// <summary> |
| | | 316 | | /// Finds the closest point on the surface of the bounding box to the specified point. |
| | | 317 | | /// </summary> |
| | | 318 | | public Vector3d ClosestPointOnSurface(Vector3d point) |
| | 13 | 319 | | { |
| | 13 | 320 | | if (Contains(point)) |
| | 4 | 321 | | { |
| | | 322 | | // Calculate distances to each face and return the closest face. |
| | 4 | 323 | | Fixed64 distToMinX = point.x - Min.x; |
| | 4 | 324 | | Fixed64 distToMaxX = Max.x - point.x; |
| | 4 | 325 | | Fixed64 distToMinY = point.y - Min.y; |
| | 4 | 326 | | Fixed64 distToMaxY = Max.y - point.y; |
| | 4 | 327 | | Fixed64 distToMinZ = point.z - Min.z; |
| | 4 | 328 | | Fixed64 distToMaxZ = Max.z - point.z; |
| | | 329 | | |
| | 4 | 330 | | Fixed64 minDistToFace = FixedMath.Min(distToMinX, |
| | 4 | 331 | | FixedMath.Min(distToMaxX, |
| | 4 | 332 | | FixedMath.Min(distToMinY, |
| | 4 | 333 | | FixedMath.Min(distToMaxY, |
| | 4 | 334 | | FixedMath.Min(distToMinZ, distToMaxZ))))); |
| | | 335 | | |
| | | 336 | | // Adjust the closest point based on the face. |
| | 5 | 337 | | if (minDistToFace == distToMinX) point.x = Min.x; |
| | 4 | 338 | | else if (minDistToFace == distToMaxX) point.x = Max.x; |
| | | 339 | | |
| | 4 | 340 | | if (minDistToFace == distToMinY) point.y = Min.y; |
| | 5 | 341 | | else if (minDistToFace == distToMaxY) point.y = Max.y; |
| | | 342 | | |
| | 5 | 343 | | if (minDistToFace == distToMinZ) point.z = Min.z; |
| | 3 | 344 | | else if (minDistToFace == distToMaxZ) point.z = Max.z; |
| | | 345 | | |
| | 4 | 346 | | return point; |
| | | 347 | | } |
| | | 348 | | |
| | | 349 | | // If the point is outside the box, clamp to the nearest surface. |
| | 9 | 350 | | return new Vector3d( |
| | 9 | 351 | | FixedMath.Clamp(point.x, Min.x, Max.x), |
| | 9 | 352 | | FixedMath.Clamp(point.y, Min.y, Max.y), |
| | 9 | 353 | | FixedMath.Clamp(point.z, Min.z, Max.z) |
| | 9 | 354 | | ); |
| | 13 | 355 | | } |
| | | 356 | | |
| | | 357 | | /// <summary> |
| | | 358 | | /// Generates the vertices of the bounding box based on its center and scope. |
| | | 359 | | /// </summary> |
| | | 360 | | /// <remarks> |
| | | 361 | | /// Vertices[0] near Bot left |
| | | 362 | | /// Vertices[1] near Bot right |
| | | 363 | | /// Vertices[2] near Top left |
| | | 364 | | /// Vertices[3] near Top right |
| | | 365 | | /// Vertices[4] far bot left |
| | | 366 | | /// Vertices[5] far bot right |
| | | 367 | | /// Vertices[6] far top left |
| | | 368 | | /// Vertices[7] far top right |
| | | 369 | | /// ---- |
| | | 370 | | /// near quad |
| | | 371 | | /// 0 - 1 Bot left near to bot right near |
| | | 372 | | /// 2 - 3 Top left near to top right near |
| | | 373 | | /// 0 - 2 Bot left near to top left near |
| | | 374 | | /// 1 - 3 Bot right near to top right near |
| | | 375 | | /// far quad |
| | | 376 | | /// 4 - 5 Bot left far to bot right far |
| | | 377 | | /// 6 - 7 Top left far to top right far |
| | | 378 | | /// 4 - 6 Bot left far to top left far |
| | | 379 | | /// 5 - 7 Bot right far to top right far |
| | | 380 | | /// lines connecting near and far quads |
| | | 381 | | /// 0 - 4 Bot left near to bot left far |
| | | 382 | | /// 1 - 5 Bot right near to bot right far |
| | | 383 | | /// 2 - 6 Top left near to top left far |
| | | 384 | | /// 3 - 7 Top right near to top right far |
| | | 385 | | /// </remarks> |
| | | 386 | | private Vector3d[] GetOrGenerateVertices() |
| | 27 | 387 | | { |
| | 27 | 388 | | _vertices ??= new Vector3d[8]; |
| | | 389 | | |
| | 27 | 390 | | if (_isDirty) |
| | 3 | 391 | | { |
| | 3 | 392 | | Vector3d min = Min; |
| | 3 | 393 | | Vector3d max = Max; |
| | | 394 | | |
| | 3 | 395 | | _vertices[0] = new(min.x, min.y, min.z); |
| | 3 | 396 | | _vertices[1] = new(max.x, min.y, min.z); |
| | 3 | 397 | | _vertices[2] = new(min.x, max.y, min.z); |
| | 3 | 398 | | _vertices[3] = new(max.x, max.y, min.z); |
| | 3 | 399 | | _vertices[4] = new(min.x, min.y, max.z); |
| | 3 | 400 | | _vertices[5] = new(max.x, min.y, max.z); |
| | 3 | 401 | | _vertices[6] = new(min.x, max.y, max.z); |
| | 3 | 402 | | _vertices[7] = new(max.x, max.y, max.z); |
| | | 403 | | |
| | 3 | 404 | | _isDirty = false; |
| | 3 | 405 | | } |
| | | 406 | | |
| | 27 | 407 | | return _vertices; |
| | 27 | 408 | | } |
| | | 409 | | |
| | | 410 | | #endregion |
| | | 411 | | |
| | | 412 | | #region Static Ops |
| | | 413 | | |
| | | 414 | | /// <summary> |
| | | 415 | | /// Creates a new bounding box that is the union of two bounding boxes. |
| | | 416 | | /// </summary> |
| | | 417 | | public static BoundingBox Union(BoundingBox a, BoundingBox b) |
| | 1 | 418 | | { |
| | 1 | 419 | | return new BoundingBox |
| | 1 | 420 | | { |
| | 1 | 421 | | Min = Vector3d.Min(a.Min, b.Min), |
| | 1 | 422 | | Max = Vector3d.Max(a.Max, b.Max), |
| | 1 | 423 | | _isDirty = true |
| | 1 | 424 | | }; |
| | 1 | 425 | | } |
| | | 426 | | |
| | | 427 | | /// <summary> |
| | | 428 | | /// Finds the closest points between two bounding boxes. |
| | | 429 | | /// </summary> |
| | | 430 | | public static Vector3d FindClosestPointsBetweenBoxes(BoundingBox a, BoundingBox b) |
| | 1 | 431 | | { |
| | 1 | 432 | | Vector3d closestPoint = Vector3d.Zero; |
| | 1 | 433 | | Fixed64 minDistance = Fixed64.MAX_VALUE; |
| | | 434 | | |
| | 18 | 435 | | for (int i = 0; i < b.Vertices.Length; i++) |
| | 8 | 436 | | { |
| | 8 | 437 | | Vector3d point = a.ClosestPointOnSurface(b.Vertices[i]); |
| | 8 | 438 | | Fixed64 distance = Vector3d.Distance(point, b.Vertices[i]); |
| | 8 | 439 | | if (distance < minDistance) |
| | 1 | 440 | | { |
| | 1 | 441 | | closestPoint = point; |
| | 1 | 442 | | minDistance = distance; |
| | 1 | 443 | | } |
| | 8 | 444 | | } |
| | | 445 | | |
| | 1 | 446 | | return closestPoint; |
| | 1 | 447 | | } |
| | | 448 | | |
| | | 449 | | #endregion |
| | | 450 | | |
| | | 451 | | #region Equality |
| | | 452 | | |
| | 2 | 453 | | public static bool operator ==(BoundingBox left, BoundingBox right) => left.Equals(right); |
| | 1 | 454 | | public static bool operator !=(BoundingBox left, BoundingBox right) => !left.Equals(right); |
| | | 455 | | |
| | | 456 | | #endregion |
| | | 457 | | |
| | | 458 | | #region Equality and HashCode Overrides |
| | | 459 | | |
| | 1 | 460 | | public override bool Equals(object? obj) => obj is BoundingBox other && Equals(other); |
| | | 461 | | |
| | | 462 | | public bool Equals(BoundingBox other) |
| | 5 | 463 | | => Min.Equals(other.Min) && Max.Equals(other.Max); |
| | | 464 | | |
| | | 465 | | public override int GetHashCode() |
| | 2 | 466 | | { |
| | 2 | 467 | | return HashCode.Combine(Min, Max); |
| | 2 | 468 | | } |
| | | 469 | | |
| | | 470 | | #endregion |
| | | 471 | | } |