using System; using System.Diagnostics; namespace Engine.Core; /// /// Represents a 3D sphere. /// /// The center of the sphere. /// The radius of the sphere. /// /// Initializes a new instance of the struct with the specified center and radius. /// [DebuggerDisplay("Center: {Center.ToString(),nq}, Radius: {Radius}")] public readonly struct Sphere3D(Vector3D center, float radius) : IEquatable { /// /// The center of the . /// public readonly Vector3D Center = center; /// /// The radius of the . /// public readonly float Radius = radius; /// /// Gets the squared radius of the . /// public readonly float RadiusSquared => Radius * Radius; /// /// Gets the diameter of the . /// public readonly float Diameter => 2f * Radius; /// /// A predefined unit with a center at the origin and a radius of 1. /// public static readonly Sphere3D UnitSphere = new(Vector3D.Zero, 1f); public static bool operator ==(Sphere3D left, Sphere3D right) => left.Center == right.Center && left.Radius == right.Radius; public static bool operator !=(Sphere3D left, Sphere3D right) => left.Center != right.Center || left.Radius != right.Radius; /// /// Sets the center of the . /// public static Sphere3D SetCenter(Sphere3D sphere, Vector3D center) => new(center, sphere.Radius); /// /// Sets the radius of the . /// public static Sphere3D SetRadius(Sphere3D sphere, float radius) => new(sphere.Center, radius); /// /// Displaces the by the specified . /// public static Sphere3D Displace(Sphere3D sphere, Vector3D displaceVector) => new(sphere.Center + displaceVector, sphere.Radius); /// /// Projects the onto the specified . /// public static Projection1D Project(Sphere3D sphere, Vector3D projectionVector) { float projectedCenter = sphere.Center.Dot(projectionVector); return new(projectedCenter - sphere.Radius, projectedCenter + sphere.Radius); } /// /// Transforms the by the specified . /// public static Sphere3D Transform(ITransform3D transform, Sphere3D sphere) => new(transform.Transform(sphere.Center), sphere.Radius * (transform.Scale.Magnitude / Vector3D.One.Magnitude)); /// /// Checks if two s are approximately equal. /// /// The first . /// The second . /// The epsilon range. /// if the s are approximately equal; otherwise, . public static bool ApproximatelyEquals(Sphere3D left, Sphere3D right, float epsilon = float.Epsilon) => left.Center.ApproximatelyEquals(right.Center, epsilon) && left.Radius.ApproximatelyEquals(right.Radius, epsilon); /// /// Determines whether the specified object is equal to the current . /// /// The object to compare with the current . /// if the specified object is equal to the current ; otherwise, . public override bool Equals(object? obj) => obj is Sphere3D sphere && this == sphere; public bool Equals(Sphere3D other) => this == other; /// /// Generates a hash code for the . /// /// A hash code for the . public override int GetHashCode() => System.HashCode.Combine(Center, Radius); /// /// Converts the to its string representation. /// /// A string representation of the . public override string ToString() => $"{nameof(Sphere3D)}({Center}, {Radius})"; } /// /// Provides extension methods for the struct. /// public static class SphereExtensions { /// public static Sphere3D SetCenter(this Sphere3D sphere, Vector3D center) => Sphere3D.SetCenter(sphere, center); /// public static Sphere3D SetRadius(this Sphere3D sphere, float radius) => Sphere3D.SetRadius(sphere, radius); /// public static Sphere3D Displace(this Sphere3D sphere, Vector3D displaceVector) => Sphere3D.Displace(sphere, displaceVector); /// public static Projection1D ProjectTo(this Sphere3D sphere, Vector3D projectionVector) => Sphere3D.Project(sphere, projectionVector); /// public static Sphere3D Transform(this ITransform3D transform, Sphere3D sphere) => Sphere3D.Transform(transform, sphere); /// public static Sphere3D Transform(this Sphere3D sphere, ITransform3D transform) => Sphere3D.Transform(transform, sphere); /// public static bool ApproximatelyEquals(this Sphere3D left, Sphere3D right, float epsilon = float.Epsilon) => Sphere3D.ApproximatelyEquals(left, right, epsilon); }