| | | 1 | | |
| | | 2 | | using System; |
| | | 3 | | using System.Numerics; |
| | | 4 | | using System.Runtime.CompilerServices; |
| | | 5 | | |
| | | 6 | | namespace SwiftCollections.Query |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Represents an axis-aligned bounding box (AABB) in 3D space. |
| | | 10 | | /// </summary> |
| | | 11 | | public struct BoundVolume : IBoundVolume |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// The minimum point of the bounding volume. |
| | | 15 | | /// </summary> |
| | | 16 | | private Vector3 _min; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// The maximum point of the bounding volume. |
| | | 20 | | /// </summary> |
| | | 21 | | private Vector3 _max; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// The center of the bounding volume as the midpoint of the minimum and maximum points. |
| | | 25 | | /// </summary> |
| | | 26 | | private Vector3 _center; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// The size of the bounding volume as the difference between the maximum and minimum points. |
| | | 30 | | /// </summary> |
| | | 31 | | private Vector3 _size; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// The volume of the bounding box, calculated as the product of its dimensions. |
| | | 35 | | /// </summary> |
| | | 36 | | private double _volume; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Marks the bounding volume as dirty, indicating its properties need recalculation. |
| | | 40 | | /// </summary> |
| | | 41 | | private bool _isDirty; |
| | | 42 | | |
| | | 43 | | public BoundVolume(Vector3 min, Vector3 max) |
| | 366977 | 44 | | { |
| | 366977 | 45 | | _min = min; |
| | 366977 | 46 | | _max = max; |
| | | 47 | | |
| | 366977 | 48 | | _isDirty = true; |
| | 366977 | 49 | | _center = default; |
| | 366977 | 50 | | _size = default; |
| | 366977 | 51 | | _volume = default; |
| | 366977 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <inheritdoc cref="_min"/> |
| | | 55 | | public Vector3 Min |
| | | 56 | | { |
| | 2241543 | 57 | | get => _min; |
| | | 58 | | private set |
| | 0 | 59 | | { |
| | 0 | 60 | | _isDirty = true; |
| | 0 | 61 | | _min = value; |
| | 0 | 62 | | } |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <inheritdoc cref="_max"/> |
| | | 66 | | public Vector3 Max |
| | | 67 | | { |
| | 2241543 | 68 | | get => _max; |
| | | 69 | | private set |
| | 0 | 70 | | { |
| | 0 | 71 | | _isDirty = true; |
| | 0 | 72 | | _max = value; |
| | 0 | 73 | | } |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <inheritdoc cref="_center"/> |
| | | 77 | | public Vector3 Center |
| | | 78 | | { |
| | | 79 | | get |
| | 4 | 80 | | { |
| | 4 | 81 | | if (_isDirty) |
| | 1 | 82 | | RecalculateMeta(); |
| | 4 | 83 | | return _center; |
| | 4 | 84 | | } |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <inheritdoc cref="_size"/> |
| | | 88 | | public Vector3 Size |
| | | 89 | | { |
| | | 90 | | get |
| | 2 | 91 | | { |
| | 2 | 92 | | if (_isDirty) |
| | 1 | 93 | | RecalculateMeta(); |
| | 2 | 94 | | return _size; |
| | 2 | 95 | | } |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <inheritdoc cref="_volume"/> |
| | | 99 | | public double Volume |
| | | 100 | | { |
| | | 101 | | get |
| | 356793 | 102 | | { |
| | 356793 | 103 | | if (_isDirty) |
| | 356792 | 104 | | RecalculateMeta(); |
| | 356793 | 105 | | return _volume; |
| | 356793 | 106 | | } |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Forces recalculation of the bounding volume's metadata, such as center, size, and volume. |
| | | 111 | | /// </summary> |
| | | 112 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 113 | | private void RecalculateMeta() |
| | 356794 | 114 | | { |
| | 356794 | 115 | | _center = (_min + _max) / 2; |
| | 356794 | 116 | | _size = _max - _min; |
| | 356794 | 117 | | _volume = _size.X * _size.Y * _size.Z; |
| | 356794 | 118 | | _isDirty = false; |
| | 356794 | 119 | | } |
| | | 120 | | |
| | | 121 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 122 | | public IBoundVolume Union(IBoundVolume other) |
| | 173163 | 123 | | { |
| | 173163 | 124 | | if (other is not BoundVolume otherBV) |
| | 1 | 125 | | throw new ArgumentException($"Mismatched bounding volume type detected!: {nameof(other)}"); |
| | | 126 | | |
| | 173162 | 127 | | return Union(otherBV); |
| | 173162 | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <inheritdoc cref="Union(IBoundVolume)"/> |
| | | 131 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 132 | | public BoundVolume Union(BoundVolume other) |
| | 351559 | 133 | | { |
| | 351559 | 134 | | return new BoundVolume( |
| | 351559 | 135 | | new Vector3(Math.Min(Min.X, other.Min.X), Math.Min(Min.Y, other.Min.Y), Math.Min(Min.Z, other.Min.Z)), |
| | 351559 | 136 | | new Vector3(Math.Max(Max.X, other.Max.X), Math.Max(Max.Y, other.Max.Y), Math.Max(Max.Z, other.Max.Z)) |
| | 351559 | 137 | | ); |
| | 351559 | 138 | | } |
| | | 139 | | |
| | | 140 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 141 | | public bool Intersects(IBoundVolume other) |
| | 22286 | 142 | | { |
| | 22286 | 143 | | if (other is not BoundVolume otherBVH) |
| | 1 | 144 | | throw new ArgumentException($"Mismatched bounding volume type detected!: {nameof(other)}"); |
| | | 145 | | |
| | 22285 | 146 | | return !(Min.X > otherBVH.Max.X || Max.X < otherBVH.Min.X || |
| | 22285 | 147 | | Min.Y > otherBVH.Max.Y || Max.Y < otherBVH.Min.Y || |
| | 22285 | 148 | | Min.Z > otherBVH.Max.Z || Max.Z < otherBVH.Min.Z); |
| | 22285 | 149 | | } |
| | | 150 | | |
| | | 151 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 152 | | public int GetCost(IBoundVolume other) |
| | 178397 | 153 | | { |
| | 178397 | 154 | | if (other is not BoundVolume otherBVH) |
| | 1 | 155 | | throw new ArgumentException($"Mismatched bounding volume type detected!: {nameof(other)}"); |
| | | 156 | | |
| | 178396 | 157 | | return (int)Math.Floor(Union(otherBVH).Volume - otherBVH.Volume); |
| | 178396 | 158 | | } |
| | | 159 | | |
| | 1 | 160 | | public override string ToString() => $"Min: {Min}, Max: {Max}"; |
| | | 161 | | } |
| | | 162 | | } |