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