| | | 1 | | using MemoryPack; |
| | | 2 | | using System; |
| | | 3 | | using System.Runtime.CompilerServices; |
| | | 4 | | using System.Text.Json.Serialization; |
| | | 5 | | |
| | | 6 | | namespace FixedMathSharp |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Represents a spherical bounding volume with fixed-point precision, optimized for fast, rotationally invariant sp |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// The BoundingSphere provides a simple yet effective way to represent the spatial extent of objects, especially wh |
| | | 13 | | /// Compared to BoundingBox, it offers faster intersection checks but is less precise in tightly fitting non-spheric |
| | | 14 | | /// |
| | | 15 | | /// Use Cases: |
| | | 16 | | /// - Ideal for broad-phase collision detection, proximity checks, and culling in physics engines and rendering pipe |
| | | 17 | | /// - Useful when fast, rotationally invariant checks are needed, such as detecting overlaps or distances between mo |
| | | 18 | | /// - Suitable for encapsulating objects with roughly spherical shapes or objects that rotate frequently, where the |
| | | 19 | | /// </remarks> |
| | | 20 | | [Serializable] |
| | | 21 | | [MemoryPackable] |
| | | 22 | | public partial struct BoundingSphere : IBound, IEquatable<BoundingSphere> |
| | | 23 | | { |
| | | 24 | | #region Fields |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// The center point of the sphere. |
| | | 28 | | /// </summary> |
| | | 29 | | [JsonInclude] |
| | | 30 | | [MemoryPackOrder(0)] |
| | | 31 | | public Vector3d Center; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// The radius of the sphere. |
| | | 35 | | /// </summary> |
| | | 36 | | [JsonInclude] |
| | | 37 | | [MemoryPackOrder(1)] |
| | | 38 | | public Fixed64 Radius; |
| | | 39 | | |
| | | 40 | | #endregion |
| | | 41 | | |
| | | 42 | | #region Constructors |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Initializes a new instance of the BoundingSphere struct with the specified center and radius. |
| | | 46 | | /// </summary> |
| | | 47 | | [JsonConstructor] |
| | | 48 | | public BoundingSphere(Vector3d center, Fixed64 radius) |
| | 32 | 49 | | { |
| | 32 | 50 | | Center = center; |
| | 32 | 51 | | Radius = radius; |
| | 32 | 52 | | } |
| | | 53 | | |
| | | 54 | | #endregion |
| | | 55 | | |
| | | 56 | | #region Properties |
| | | 57 | | |
| | | 58 | | [JsonIgnore] |
| | | 59 | | [MemoryPackIgnore] |
| | | 60 | | public Vector3d Min |
| | | 61 | | { |
| | | 62 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 63 | | get => Center - new Vector3d(Radius, Radius, Radius); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | [JsonIgnore] |
| | | 67 | | [MemoryPackIgnore] |
| | | 68 | | public Vector3d Max |
| | | 69 | | { |
| | | 70 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 71 | | get => Center + new Vector3d(Radius, Radius, Radius); |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// The squared radius of the sphere. |
| | | 76 | | /// </summary> |
| | | 77 | | [JsonIgnore] |
| | | 78 | | [MemoryPackIgnore] |
| | | 79 | | public Fixed64 SqrRadius |
| | | 80 | | { |
| | | 81 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 7 | 82 | | get => Radius * Radius; |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | #endregion |
| | | 86 | | |
| | | 87 | | #region Methods (Instance) |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Checks if a point is inside the sphere. |
| | | 91 | | /// </summary> |
| | | 92 | | /// <param name="point">The point to check.</param> |
| | | 93 | | /// <returns>True if the point is inside the sphere, otherwise false.</returns> |
| | | 94 | | public bool Contains(Vector3d point) |
| | 3 | 95 | | { |
| | 3 | 96 | | return Vector3d.SqrDistance(Center, point) <= SqrRadius; |
| | 3 | 97 | | } |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Checks if this sphere intersects with another IBound. |
| | | 101 | | /// </summary> |
| | | 102 | | /// <param name="other">The other IBound to check for intersection.</param> |
| | | 103 | | /// <returns>True if the IBounds intersect, otherwise false.</returns> |
| | | 104 | | public bool Intersects(IBound other) |
| | 6 | 105 | | { |
| | 6 | 106 | | switch (other) |
| | | 107 | | { |
| | | 108 | | case BoundingBox or BoundingArea: |
| | | 109 | | // Find the closest point on the BoundingArea to the sphere's center |
| | | 110 | | // Check if the closest point is within the sphere's radius |
| | 2 | 111 | | return Vector3d.SqrDistance(Center, other.ProjectPointWithinBounds(Center)) <= SqrRadius; |
| | | 112 | | case BoundingSphere otherSphere: |
| | 3 | 113 | | { |
| | 3 | 114 | | Fixed64 distanceSquared = Vector3d.SqrDistance(Center, otherSphere.Center); |
| | 3 | 115 | | Fixed64 combinedRadius = Radius + otherSphere.Radius; |
| | 3 | 116 | | return distanceSquared <= combinedRadius * combinedRadius; |
| | | 117 | | } |
| | | 118 | | |
| | 1 | 119 | | default: return false; // Default case for unknown or unsupported types |
| | | 120 | | } |
| | 6 | 121 | | } |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Projects a point onto the bounding sphere. If the point is outside the sphere, it returns the closest point |
| | | 125 | | /// </summary> |
| | | 126 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 127 | | public Vector3d ProjectPoint(Vector3d point) |
| | 2 | 128 | | { |
| | 2 | 129 | | var direction = point - Center; |
| | 3 | 130 | | if (direction.IsZero) return Center; // If the point is the center, return the center itself |
| | | 131 | | |
| | 1 | 132 | | return Center + direction.Normalize() * Radius; |
| | 2 | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Calculates the distance from a point to the surface of the sphere. |
| | | 137 | | /// </summary> |
| | | 138 | | /// <param name="point">The point to calculate the distance from.</param> |
| | | 139 | | /// <returns>The distance from the point to the surface of the sphere.</returns> |
| | | 140 | | public Fixed64 DistanceToSurface(Vector3d point) |
| | 4 | 141 | | { |
| | 4 | 142 | | return Vector3d.Distance(Center, point) - Radius; |
| | 4 | 143 | | } |
| | | 144 | | |
| | | 145 | | #endregion |
| | | 146 | | |
| | | 147 | | #region Operators |
| | | 148 | | |
| | | 149 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 150 | | public static bool operator ==(BoundingSphere left, BoundingSphere right) => left.Equals(right); |
| | | 151 | | |
| | | 152 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 153 | | public static bool operator !=(BoundingSphere left, BoundingSphere right) => !left.Equals(right); |
| | | 154 | | |
| | | 155 | | #endregion |
| | | 156 | | |
| | | 157 | | #region Equality and HashCode Overrides |
| | | 158 | | |
| | | 159 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 1 | 160 | | public override bool Equals(object? obj) => obj is BoundingSphere other && Equals(other); |
| | | 161 | | |
| | | 162 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 5 | 163 | | public bool Equals(BoundingSphere other) => Center.Equals(other.Center) && Radius.Equals(other.Radius); |
| | | 164 | | |
| | | 165 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 166 | | public override int GetHashCode() |
| | 2 | 167 | | { |
| | | 168 | | unchecked |
| | 2 | 169 | | { |
| | 2 | 170 | | int hash = 17; |
| | 2 | 171 | | hash = hash * 23 + Center.GetHashCode(); |
| | 2 | 172 | | hash = hash * 23 + Radius.GetHashCode(); |
| | 2 | 173 | | return hash; |
| | | 174 | | } |
| | 2 | 175 | | } |
| | | 176 | | |
| | | 177 | | #endregion |
| | | 178 | | } |
| | | 179 | | } |