| | | 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 System; |
| | | 9 | | using System.Runtime.CompilerServices; |
| | | 10 | | using FixedMathSharp; |
| | | 11 | | using FixedMathSharp.Geometry; |
| | | 12 | | |
| | | 13 | | namespace SwiftCollections.Query; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Represents an axis-aligned bounding box (AABB) in 3D space using fixed-point math. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <remarks> |
| | | 19 | | /// Construction normalizes swapped endpoints. Derived center and size semantics |
| | | 20 | | /// are inherited from <see cref="FixedBoundBox"/>. |
| | | 21 | | /// </remarks> |
| | | 22 | | public struct FixedBoundVolume : IBoundVolume<FixedBoundVolume>, IEquatable<FixedBoundVolume> |
| | | 23 | | { |
| | | 24 | | private FixedBoundBox _bounds; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Initializes a new instance of the <see cref="FixedBoundVolume"/> struct. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="min">One endpoint of the volume.</param> |
| | | 30 | | /// <param name="max">The opposite endpoint of the volume.</param> |
| | | 31 | | public FixedBoundVolume(Vector3d min, Vector3d max) |
| | | 32 | | { |
| | 143 | 33 | | _bounds = FixedBoundBox.FromMinMax(min, max); |
| | 143 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets the minimum point of the bounding volume. |
| | | 38 | | /// </summary> |
| | 896 | 39 | | public Vector3d Min => _bounds.Min; |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Gets the maximum point of the bounding volume. |
| | | 43 | | /// </summary> |
| | 849 | 44 | | public Vector3d Max => _bounds.Max; |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Gets the nearest-even Q32.32 center point of the bounding volume. |
| | | 48 | | /// </summary> |
| | 57 | 49 | | public Vector3d Center => _bounds.Center; |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets the exact axis-aligned size of the bounding volume. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <exception cref="OverflowException"> |
| | | 55 | | /// A positive component span is outside the representable scalar domain. |
| | | 56 | | /// </exception> |
| | 4 | 57 | | public Vector3d Size => _bounds.Proportions; |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Gets the volume of the bounding box. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <exception cref="OverflowException"> |
| | | 63 | | /// A positive component span is outside the representable scalar domain. |
| | | 64 | | /// </exception> |
| | | 65 | | public Fixed64 Volume |
| | | 66 | | { |
| | | 67 | | get |
| | | 68 | | { |
| | 1 | 69 | | Vector3d size = Size; |
| | 1 | 70 | | return size.X * size.Y * size.Z; |
| | | 71 | | } |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Creates a new volume that represents the union of this volume and the specified volume. |
| | | 76 | | /// </summary> |
| | | 77 | | /// <remarks> |
| | | 78 | | /// The resulting volume is the smallest axis-aligned bounding box that fully contains both input volumes. |
| | | 79 | | /// </remarks> |
| | | 80 | | /// <param name="other"> |
| | | 81 | | /// The volume to combine with this volume. |
| | | 82 | | /// The resulting volume will encompass both this volume and the specified volume. |
| | | 83 | | /// </param> |
| | | 84 | | /// <returns>A new FixedBoundVolume that contains both this volume and the specified volume.</returns> |
| | | 85 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 86 | | public FixedBoundVolume Union(FixedBoundVolume other) |
| | | 87 | | { |
| | 7 | 88 | | return new FixedBoundVolume(Vector3d.Min(Min, other.Min), Vector3d.Max(Max, other.Max)); |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Determines whether this volume intersects with the specified volume. |
| | | 93 | | /// </summary> |
| | | 94 | | /// <param name="other">The volume to test for intersection with this volume.</param> |
| | | 95 | | /// <returns>true if the volumes intersect; otherwise, false.</returns> |
| | | 96 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 97 | | public bool Intersects(FixedBoundVolume other) |
| | | 98 | | { |
| | 124 | 99 | | return !(Min.X > other.Max.X || Max.X < other.Min.X || |
| | 124 | 100 | | Min.Y > other.Max.Y || Max.Y < other.Min.Y || |
| | 124 | 101 | | Min.Z > other.Max.Z || Max.Z < other.Min.Z); |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Calculates the additional volume required to expand the current volume to fully contain the specified volume. |
| | | 106 | | /// </summary> |
| | | 107 | | /// <param name="other">The volume to be encompassed by the current volume.</param> |
| | | 108 | | /// <returns> |
| | | 109 | | /// The floored additional volume needed to contain the specified volume, |
| | | 110 | | /// clamped to <see cref="long.MaxValue"/>. Returns 0 when the current volume |
| | | 111 | | /// already contains the specified volume. |
| | | 112 | | /// </returns> |
| | | 113 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 114 | | public long GetCost(FixedBoundVolume other) |
| | | 115 | | { |
| | 4 | 116 | | return _bounds.GetVolumeExpansionCost(other._bounds); |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// Determines whether the bounds of this volume are equal to those of the specified volume. |
| | | 121 | | /// </summary> |
| | | 122 | | /// <param name="other">A <see cref="FixedBoundVolume"/> to compare with the current volume.</param> |
| | | 123 | | /// <returns>true if both the minimum and maximum bounds of the volumes are equal; otherwise, false.</returns> |
| | | 124 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 125 | | public bool BoundsEquals(FixedBoundVolume other) |
| | | 126 | | { |
| | 8 | 127 | | return Min == other.Min && Max == other.Max; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <inheritdoc/> |
| | 4 | 131 | | public bool Equals(FixedBoundVolume other) => BoundsEquals(other); |
| | | 132 | | |
| | | 133 | | /// <inheritdoc/> |
| | 2 | 134 | | public override bool Equals(object obj) => obj is FixedBoundVolume other && BoundsEquals(other); |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Determines whether two BoundVolume instances are equal. |
| | | 138 | | /// </summary> |
| | 4 | 139 | | public static bool operator ==(FixedBoundVolume left, FixedBoundVolume right) => left.Equals(right); |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Determines whether two BoundVolume instances are not equal. |
| | | 143 | | /// </summary> |
| | 2 | 144 | | public static bool operator !=(FixedBoundVolume left, FixedBoundVolume right) => !(left == right); |
| | | 145 | | |
| | | 146 | | /// <inheritdoc/> |
| | 2 | 147 | | public override int GetHashCode() => HashCode.Combine(Min, Max); |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// Returns a string that represents the current object, including the minimum and maximum values. |
| | | 151 | | /// </summary> |
| | | 152 | | /// <returns>A string in the format "Min: {Min}, Max: {Max}" that displays the minimum and maximum values of the obj |
| | 1 | 153 | | public override string ToString() => $"Min: {Min}, Max: {Max}"; |
| | | 154 | | } |