feat: added sphere primitive

This commit is contained in:
2025-10-21 19:06:58 +03:00
parent 896f7876c1
commit 0db2cae1bb
7 changed files with 230 additions and 0 deletions

View File

@@ -42,6 +42,8 @@ public readonly struct AABB3D(Vector3D lowerBoundary, Vector3D upperBoundary) :
public static bool operator ==(AABB3D left, AABB3D right) => left.UpperBoundary == right.UpperBoundary && left.LowerBoundary == right.LowerBoundary;
public static bool operator !=(AABB3D left, AABB3D right) => left.UpperBoundary != right.UpperBoundary || left.LowerBoundary != right.LowerBoundary;
public static implicit operator AABB3D(Sphere3D sphere) => new(sphere.Center - new Vector3D(sphere.Radius, sphere.Radius, sphere.Radius), sphere.Center + new Vector3D(sphere.Radius, sphere.Radius, sphere.Radius));
/// <summary>
/// Creates an <see cref="AABB3D"/> from a collection of <see cref="Vector3D"/>s.
/// </summary>

View File

@@ -42,6 +42,8 @@ public readonly struct Circle(Vector2D center, float radius) : IEquatable<Circle
public static bool operator ==(Circle left, Circle right) => left.Center == right.Center && left.Radius == right.Radius;
public static bool operator !=(Circle left, Circle right) => left.Center != right.Center || left.Radius != right.Radius;
public static implicit operator Circle(Sphere3D sphere) => new(sphere.Center, sphere.Radius);
/// <summary>
/// Sets the center of the <see cref="Circle"/>.
/// </summary>

View File

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