| | | 1 | | using System; |
| | | 2 | | using System.Numerics; |
| | | 3 | | using System.Runtime.CompilerServices; |
| | | 4 | | |
| | | 5 | | namespace SwiftCollections.Query; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Represents an axis-aligned bounding box (AABB) in 3D space. |
| | | 9 | | /// </summary> |
| | | 10 | | public struct BoundVolume : IBoundVolume<BoundVolume>, IEquatable<BoundVolume> |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// The minimum point of the bounding volume. |
| | | 14 | | /// </summary> |
| | | 15 | | private Vector3 _min; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// The maximum point of the bounding volume. |
| | | 19 | | /// </summary> |
| | | 20 | | private Vector3 _max; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// The center of the bounding volume as the midpoint of the minimum and maximum points. |
| | | 24 | | /// </summary> |
| | | 25 | | private Vector3 _center; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// The size of the bounding volume as the difference between the maximum and minimum points. |
| | | 29 | | /// </summary> |
| | | 30 | | private Vector3 _size; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// The volume of the bounding box, calculated as the product of its dimensions. |
| | | 34 | | /// </summary> |
| | | 35 | | private double _volume; |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Marks the bounding volume as dirty, indicating its properties need recalculation. |
| | | 39 | | /// </summary> |
| | | 40 | | private bool _isDirty; |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Initializes a new instance of the <see cref="BoundVolume"/> struct with the specified minimum and maximum points |
| | | 44 | | /// </summary> |
| | | 45 | | /// <param name="min">The minimum point of the bounding volume.</param> |
| | | 46 | | /// <param name="max">The maximum point of the bounding volume.</param> |
| | | 47 | | public BoundVolume(Vector3 min, Vector3 max) |
| | | 48 | | { |
| | 417665 | 49 | | _min = min; |
| | 417665 | 50 | | _max = max; |
| | | 51 | | |
| | 417665 | 52 | | _isDirty = true; |
| | 417665 | 53 | | _center = default; |
| | 417665 | 54 | | _size = default; |
| | 417665 | 55 | | _volume = default; |
| | 417665 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <inheritdoc cref="_min"/> |
| | 2804109 | 59 | | public readonly Vector3 Min => _min; |
| | | 60 | | |
| | | 61 | | /// <inheritdoc cref="_max"/> |
| | 2804077 | 62 | | public readonly Vector3 Max => _max; |
| | | 63 | | |
| | | 64 | | /// <inheritdoc cref="_center"/> |
| | | 65 | | public Vector3 Center |
| | | 66 | | { |
| | | 67 | | get |
| | | 68 | | { |
| | 6 | 69 | | if (_isDirty) |
| | 3 | 70 | | RecalculateMeta(); |
| | 6 | 71 | | return _center; |
| | | 72 | | } |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <inheritdoc cref="_size"/> |
| | | 76 | | public Vector3 Size |
| | | 77 | | { |
| | | 78 | | get |
| | | 79 | | { |
| | 6 | 80 | | if (_isDirty) |
| | 5 | 81 | | RecalculateMeta(); |
| | 6 | 82 | | return _size; |
| | | 83 | | } |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <inheritdoc cref="_volume"/> |
| | | 87 | | public double Volume |
| | | 88 | | { |
| | | 89 | | get |
| | | 90 | | { |
| | 252014 | 91 | | if (_isDirty) |
| | 252013 | 92 | | RecalculateMeta(); |
| | 252014 | 93 | | return _volume; |
| | | 94 | | } |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Forces recalculation of the bounding volume's metadata, such as center, size, and volume. |
| | | 99 | | /// </summary> |
| | | 100 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 101 | | private void RecalculateMeta() |
| | | 102 | | { |
| | 252021 | 103 | | _center = (_min + _max) / 2; |
| | 252021 | 104 | | _size = _max - _min; |
| | 252021 | 105 | | _volume = _size.X * _size.Y * _size.Z; |
| | 252021 | 106 | | _isDirty = false; |
| | 252021 | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <inheritdoc /> |
| | | 110 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 111 | | public readonly BoundVolume Union(BoundVolume other) |
| | | 112 | | { |
| | 404330 | 113 | | return new BoundVolume( |
| | 404330 | 114 | | new Vector3(Math.Min(Min.X, other.Min.X), Math.Min(Min.Y, other.Min.Y), Math.Min(Min.Z, other.Min.Z)), |
| | 404330 | 115 | | new Vector3(Math.Max(Max.X, other.Max.X), Math.Max(Max.Y, other.Max.Y), Math.Max(Max.Z, other.Max.Z)) |
| | 404330 | 116 | | ); |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <inheritdoc /> |
| | | 120 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 121 | | public readonly bool Intersects(BoundVolume other) |
| | | 122 | | { |
| | 21142 | 123 | | return !(Min.X > other.Max.X || Max.X < other.Min.X || |
| | 21142 | 124 | | Min.Y > other.Max.Y || Max.Y < other.Min.Y || |
| | 21142 | 125 | | Min.Z > other.Max.Z || Max.Z < other.Min.Z); |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <inheritdoc /> |
| | | 129 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 130 | | public readonly long GetCost(BoundVolume other) |
| | | 131 | | { |
| | 252013 | 132 | | BoundVolume union = Union(other); |
| | 252013 | 133 | | Vector3 size = Max - Min; |
| | 252013 | 134 | | double volume = size.X * size.Y * size.Z; |
| | 252013 | 135 | | double delta = union.Volume - volume; |
| | 252013 | 136 | | return delta >= (double)long.MaxValue ? long.MaxValue : (long)delta; |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | /// <inheritdoc /> |
| | | 140 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 141 | | public readonly bool BoundsEquals(BoundVolume other) |
| | | 142 | | { |
| | 15 | 143 | | return Min.Equals(other.Min) && Max.Equals(other.Max); |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | /// <inheritdoc /> |
| | 5 | 147 | | public readonly bool Equals(BoundVolume other) => BoundsEquals(other); |
| | | 148 | | |
| | | 149 | | /// <inheritdoc /> |
| | 1 | 150 | | public override readonly bool Equals(object? obj) => obj is BoundVolume other && BoundsEquals(other); |
| | | 151 | | |
| | | 152 | | /// <summary> |
| | | 153 | | /// Determines whether two BoundVolume instances are equal. |
| | | 154 | | /// </summary> |
| | 4 | 155 | | public static bool operator ==(BoundVolume left, BoundVolume right) => left.Equals(right); |
| | | 156 | | |
| | | 157 | | /// <summary> |
| | | 158 | | /// Determines whether two BoundVolume instances are not equal. |
| | | 159 | | /// </summary> |
| | 2 | 160 | | public static bool operator !=(BoundVolume left, BoundVolume right) => !(left == right); |
| | | 161 | | |
| | | 162 | | /// <inheritdoc /> |
| | 2 | 163 | | public override readonly int GetHashCode() => HashCode.Combine(Min, Max); |
| | | 164 | | |
| | | 165 | | /// <summary> |
| | | 166 | | /// Returns a string that represents the current object, including the minimum and maximum values. |
| | | 167 | | /// </summary> |
| | | 168 | | /// <returns>A string in the format "Min: {Min}, Max: {Max}" that displays the minimum and maximum values of the obj |
| | 1 | 169 | | public override readonly string ToString() => $"Min: {Min}, Max: {Max}"; |
| | | 170 | | } |