| | | 1 | | //======================================================================= |
| | | 2 | | // FixedBoundVolume.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024–present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using FixedMathSharp; |
| | | 9 | | using System; |
| | | 10 | | using System.Runtime.CompilerServices; |
| | | 11 | | |
| | | 12 | | namespace SwiftCollections.Query; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Represents an axis-aligned bounding box (AABB) in 3D space using fixed-point math. |
| | | 16 | | /// </summary> |
| | | 17 | | public struct FixedBoundVolume : IBoundVolume<FixedBoundVolume>, IEquatable<FixedBoundVolume> |
| | | 18 | | { |
| | | 19 | | private Vector3d _min; |
| | | 20 | | private Vector3d _max; |
| | | 21 | | private Vector3d _center; |
| | | 22 | | private Vector3d _size; |
| | | 23 | | private Fixed64 _volume; |
| | | 24 | | private bool _isDirty; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Initializes a new instance of the <see cref="FixedBoundVolume"/> struct. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="min">The minimum point of the volume.</param> |
| | | 30 | | /// <param name="max">The maximum point of the volume.</param> |
| | | 31 | | public FixedBoundVolume(Vector3d min, Vector3d max) |
| | | 32 | | { |
| | 117 | 33 | | _min = min; |
| | 117 | 34 | | _max = max; |
| | 117 | 35 | | _center = default; |
| | 117 | 36 | | _size = default; |
| | 117 | 37 | | _volume = default; |
| | 117 | 38 | | _isDirty = true; |
| | 117 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Gets the minimum point of the bounding volume. |
| | | 43 | | /// </summary> |
| | 814 | 44 | | public readonly Vector3d Min => _min; |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Gets the maximum point of the bounding volume. |
| | | 48 | | /// </summary> |
| | 770 | 49 | | public readonly Vector3d Max => _max; |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets the center point of the bounding volume. |
| | | 53 | | /// </summary> |
| | | 54 | | public Vector3d Center |
| | | 55 | | { |
| | | 56 | | get |
| | | 57 | | { |
| | 2 | 58 | | if (_isDirty) |
| | 2 | 59 | | RecalculateMeta(); |
| | 2 | 60 | | return _center; |
| | | 61 | | } |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Gets the axis-aligned size of the bounding volume. |
| | | 66 | | /// </summary> |
| | | 67 | | public Vector3d Size |
| | | 68 | | { |
| | | 69 | | get |
| | | 70 | | { |
| | 5 | 71 | | if (_isDirty) |
| | 4 | 72 | | RecalculateMeta(); |
| | 5 | 73 | | return _size; |
| | | 74 | | } |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Gets the volume of the bounding box. |
| | | 79 | | /// </summary> |
| | | 80 | | public Fixed64 Volume |
| | | 81 | | { |
| | | 82 | | get |
| | | 83 | | { |
| | 5 | 84 | | if (_isDirty) |
| | 3 | 85 | | RecalculateMeta(); |
| | 5 | 86 | | return _volume; |
| | | 87 | | } |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 91 | | private void RecalculateMeta() |
| | | 92 | | { |
| | 9 | 93 | | _center = (_min + _max) * Fixed64.Half; |
| | 9 | 94 | | _size = _max - _min; |
| | 9 | 95 | | _volume = _size.X * _size.Y * _size.Z; |
| | 9 | 96 | | _isDirty = false; |
| | 9 | 97 | | } |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Creates a new volume that represents the union of this volume and the specified volume. |
| | | 101 | | /// </summary> |
| | | 102 | | /// <remarks> |
| | | 103 | | /// The resulting volume is the smallest axis-aligned bounding box that fully contains both input volumes. |
| | | 104 | | /// </remarks> |
| | | 105 | | /// <param name="other"> |
| | | 106 | | /// The volume to combine with this volume. |
| | | 107 | | /// The resulting volume will encompass both this volume and the specified volume. |
| | | 108 | | /// </param> |
| | | 109 | | /// <returns>A new FixedBoundVolume that contains both this volume and the specified volume.</returns> |
| | | 110 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 111 | | public readonly FixedBoundVolume Union(FixedBoundVolume other) |
| | | 112 | | { |
| | 6 | 113 | | return new FixedBoundVolume(Vector3d.Min(Min, other.Min), Vector3d.Max(Max, other.Max)); |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Determines whether this volume intersects with the specified volume. |
| | | 118 | | /// </summary> |
| | | 119 | | /// <param name="other">The volume to test for intersection with this volume.</param> |
| | | 120 | | /// <returns>true if the volumes intersect; otherwise, false.</returns> |
| | | 121 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 122 | | public readonly bool Intersects(FixedBoundVolume other) |
| | | 123 | | { |
| | 117 | 124 | | return !(Min.X > other.Max.X || Max.X < other.Min.X || |
| | 117 | 125 | | Min.Y > other.Max.Y || Max.Y < other.Min.Y || |
| | 117 | 126 | | Min.Z > other.Max.Z || Max.Z < other.Min.Z); |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Calculates the additional volume required to expand the current volume to fully contain the specified volume. |
| | | 131 | | /// </summary> |
| | | 132 | | /// <param name="other">The volume to be encompassed by the current volume.</param> |
| | | 133 | | /// <returns> |
| | | 134 | | /// The minimum additional volume needed to contain the specified volume. |
| | | 135 | | /// Returns 0 if the current volume already contains the specified volume. |
| | | 136 | | /// </returns> |
| | | 137 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 138 | | public long GetCost(FixedBoundVolume other) |
| | | 139 | | { |
| | 2 | 140 | | Fixed64 delta = Union(other).Volume - Volume; |
| | 2 | 141 | | return delta <= Fixed64.Zero ? 0L : (long)delta.FloorToInt(); |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// Determines whether the bounds of this volume are equal to those of the specified volume. |
| | | 146 | | /// </summary> |
| | | 147 | | /// <param name="other">A <see cref="FixedBoundVolume"/> to compare with the current volume.</param> |
| | | 148 | | /// <returns>true if both the minimum and maximum bounds of the volumes are equal; otherwise, false.</returns> |
| | | 149 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 150 | | public readonly bool BoundsEquals(FixedBoundVolume other) |
| | | 151 | | { |
| | 10 | 152 | | return Min == other.Min && Max == other.Max; |
| | | 153 | | } |
| | | 154 | | |
| | | 155 | | /// <inheritdoc/> |
| | 5 | 156 | | public readonly bool Equals(FixedBoundVolume other) => BoundsEquals(other); |
| | | 157 | | |
| | | 158 | | /// <inheritdoc/> |
| | 2 | 159 | | public override readonly bool Equals(object obj) => obj is FixedBoundVolume other && BoundsEquals(other); |
| | | 160 | | |
| | | 161 | | /// <summary> |
| | | 162 | | /// Determines whether two BoundVolume instances are equal. |
| | | 163 | | /// </summary> |
| | 4 | 164 | | public static bool operator ==(FixedBoundVolume left, FixedBoundVolume right) => left.Equals(right); |
| | | 165 | | |
| | | 166 | | /// <summary> |
| | | 167 | | /// Determines whether two BoundVolume instances are not equal. |
| | | 168 | | /// </summary> |
| | 2 | 169 | | public static bool operator !=(FixedBoundVolume left, FixedBoundVolume right) => !(left == right); |
| | | 170 | | |
| | | 171 | | /// <inheritdoc/> |
| | 2 | 172 | | public override readonly int GetHashCode() => HashCode.Combine(Min, Max); |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// Returns a string that represents the current object, including the minimum and maximum values. |
| | | 176 | | /// </summary> |
| | | 177 | | /// <returns>A string in the format "Min: {Min}, Max: {Max}" that displays the minimum and maximum values of the obj |
| | 1 | 178 | | public override readonly string ToString() => $"Min: {Min}, Max: {Max}"; |
| | | 179 | | } |