< Summary

Information
Class: FixedMathSharp.BoundingSphere
Assembly: FixedMathSharp
File(s): /home/runner/work/FixedMathSharp/FixedMathSharp/src/FixedMathSharp/Bounds/BoundingSphere.cs
Line coverage
100%
Covered lines: 38
Uncovered lines: 0
Coverable lines: 38
Total lines: 179
Line coverage: 100%
Branch coverage
91%
Covered branches: 11
Total branches: 12
Branch coverage: 91.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Min()100%11100%
get_Max()100%11100%
get_SqrRadius()100%11100%
Contains(...)100%11100%
Intersects(...)100%66100%
ProjectPoint(...)100%22100%
DistanceToSurface(...)100%11100%
op_Equality(...)100%11100%
op_Inequality(...)100%11100%
Equals(...)50%22100%
Equals(...)100%22100%
GetHashCode()100%11100%

File(s)

/home/runner/work/FixedMathSharp/FixedMathSharp/src/FixedMathSharp/Bounds/BoundingSphere.cs

#LineLine coverage
 1using MemoryPack;
 2using System;
 3using System.Runtime.CompilerServices;
 4using System.Text.Json.Serialization;
 5
 6namespace 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)
 3249        {
 3250            Center = center;
 3251            Radius = radius;
 3252        }
 53
 54        #endregion
 55
 56        #region Properties
 57
 58        [JsonIgnore]
 59        [MemoryPackIgnore]
 60        public Vector3d Min
 61        {
 62            [MethodImpl(MethodImplOptions.AggressiveInlining)]
 163            get => Center - new Vector3d(Radius, Radius, Radius);
 64        }
 65
 66        [JsonIgnore]
 67        [MemoryPackIgnore]
 68        public Vector3d Max
 69        {
 70            [MethodImpl(MethodImplOptions.AggressiveInlining)]
 171            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)]
 782            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)
 395        {
 396            return Vector3d.SqrDistance(Center, point) <= SqrRadius;
 397        }
 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)
 6105        {
 6106            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
 2111                    return Vector3d.SqrDistance(Center, other.ProjectPointWithinBounds(Center)) <= SqrRadius;
 112                case BoundingSphere otherSphere:
 3113                    {
 3114                        Fixed64 distanceSquared = Vector3d.SqrDistance(Center, otherSphere.Center);
 3115                        Fixed64 combinedRadius = Radius + otherSphere.Radius;
 3116                        return distanceSquared <= combinedRadius * combinedRadius;
 117                    }
 118
 1119                default: return false; // Default case for unknown or unsupported types
 120            }
 6121        }
 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)
 2128        {
 2129            var direction = point - Center;
 3130            if (direction.IsZero) return Center; // If the point is the center, return the center itself
 131
 1132            return Center + direction.Normalize() * Radius;
 2133        }
 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)
 4141        {
 4142            return Vector3d.Distance(Center, point) - Radius;
 4143        }
 144
 145        #endregion
 146
 147        #region Operators
 148
 149        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 2150        public static bool operator ==(BoundingSphere left, BoundingSphere right) => left.Equals(right);
 151
 152        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 1153        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)]
 1160        public override bool Equals(object? obj) => obj is BoundingSphere other && Equals(other);
 161
 162        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 5163        public bool Equals(BoundingSphere other) => Center.Equals(other.Center) && Radius.Equals(other.Radius);
 164
 165        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 166        public override int GetHashCode()
 2167        {
 168            unchecked
 2169            {
 2170                int hash = 17;
 2171                hash = hash * 23 + Center.GetHashCode();
 2172                hash = hash * 23 + Radius.GetHashCode();
 2173                return hash;
 174            }
 2175        }
 176
 177        #endregion
 178    }
 179}