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);
}

View File

@@ -158,6 +158,7 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, INetworkCommunicat
netPacketProcessor.RegisterNestedType(Ray2DNetPacker.Write, Ray2DNetPacker.Read);
netPacketProcessor.RegisterNestedType(Ray3DNetPacker.Write, Ray3DNetPacker.Read);
netPacketProcessor.RegisterNestedType(Shape2DNetPacker.Write, Shape2DNetPacker.Read);
netPacketProcessor.RegisterNestedType(Sphere3DNetPacker.Write, Sphere3DNetPacker.Read);
netPacketProcessor.RegisterNestedType(TriangleNetPacker.Write, TriangleNetPacker.Read);
netPacketProcessor.RegisterNestedType(Vector2DNetPacker.Write, Vector2DNetPacker.Read);
netPacketProcessor.RegisterNestedType(Vector2DIntNetPacker.Write, Vector2DIntNetPacker.Read);

View File

@@ -0,0 +1,22 @@
using LiteNetLib.Utils;
using Engine.Core;
namespace Engine.Systems.Network;
internal static class Sphere3DNetPacker
{
internal static void Write(NetDataWriter writer, Sphere3D data)
{
Vector3DNetPacker.Write(writer, data.Center);
writer.Put(data.Radius);
}
internal static Sphere3D Read(NetDataReader reader)
{
Vector3D center = Vector3DNetPacker.Read(reader);
float radius = reader.GetFloat();
return new Sphere3D(center, radius);
}
}

View File

@@ -0,0 +1,41 @@
using System;
using Engine.Core;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class Sphere3DConverter : EngineTypeYamlSerializerBase<Sphere3D>
{
public override Sphere3D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{
parser.Consume<MappingStart>();
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Sphere3D.Center)) != 0)
throw new ArgumentException($"{nameof(Sphere3D)} mapping must start with {nameof(Sphere3D.Center)}");
Vector3D lowerBoundary = (Vector3D)rootDeserializer(typeof(Vector3D))!;
if (parser.Consume<Scalar>().Value.CompareTo(nameof(Sphere3D.Radius)) != 0)
throw new ArgumentException($"{nameof(Sphere3D)} mapping must end with {nameof(Sphere3D.Radius)}");
float radius = (float)rootDeserializer(typeof(float))!;
parser.Consume<MappingEnd>();
return new Sphere3D(lowerBoundary, radius);
}
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
{
Sphere3D sphere = (Sphere3D)value!;
emitter.Emit(new MappingStart());
emitter.Emit(new Scalar(nameof(Sphere3D.Center)));
serializer(sphere.Center, typeof(Vector3D));
emitter.Emit(new Scalar(nameof(Sphere3D.Radius)));
serializer(sphere.Radius, typeof(float));
emitter.Emit(new MappingEnd());
}
}

View File

@@ -0,0 +1,31 @@
using Engine.Core;
namespace Engine.Systems.Tween;
public static class TweenSphere3DExtensions
{
private static readonly BoxedPool<Sphere3D> boxedSphere3DPool = new(2);
public static ITween TweenSphere3D(this Sphere3D initialSphere3D, ITweenManager tweenManager, float duration, Sphere3D targetSphere3D, System.Action<Sphere3D> setMethod)
{
Boxed<Sphere3D> boxedInitial = boxedSphere3DPool.Get(initialSphere3D);
Boxed<Sphere3D> boxedTarget = boxedSphere3DPool.Get(targetSphere3D);
ITween tween = tweenManager.StartTween(duration,
t => setMethod?.Invoke(
new Sphere3D(
boxedInitial.Value.Center.Lerp(boxedTarget.Value.Center, t),
boxedInitial.Value.Diameter.Lerp(boxedTarget.Value.Diameter, t)
)
)
);
tween.OnComplete(() =>
{
boxedSphere3DPool.Return(boxedInitial);
boxedSphere3DPool.Return(boxedTarget);
});
return tween;
}
}