feat: 3D ray and line primitives added

This commit is contained in:
2025-10-19 00:24:06 +03:00
parent 2f5c04e66b
commit d1b2723a70
2 changed files with 223 additions and 0 deletions

View File

@@ -0,0 +1,114 @@
using System;
namespace Engine.Core;
/// <summary>
/// Represents a 3D line segment defined by two endpoints.
/// </summary>
/// <param name="from">The starting point of the <see cref="Line3D"/> segment.</param>
/// <param name="to">The ending point of the <see cref="Line3D"/> segment.</param>
/// <remarks>
/// Initializes a new instance of the <see cref="Line3D"/> struct with the specified endpoints.
/// </remarks>
[System.Diagnostics.DebuggerDisplay("From: {From.ToString(),nq}, To: {To.ToString(),nq}, Direction: {Direction.ToString(),nq}, Length: {Length}")]
public readonly struct Line3D(Vector3D from, Vector3D to) : IEquatable<Line3D>
{
/// <summary>
/// The starting point of the <see cref="Line3D"/> segment.
/// </summary>
public readonly Vector3D From = from;
/// <summary>
/// The ending point of the <see cref="Line3D"/> segment.
/// </summary>
public readonly Vector3D To = to;
/// <summary>
/// The reversed <see cref="Line3D"/> segment.
/// </summary>
public readonly Line3D Reversed => new(To, From);
/// <summary>
/// The normalized direction <see cref="Vector3D"/> of the <see cref="Line3D"/> segment.
/// </summary>
public readonly Vector3D Direction => From.FromTo(To).Normalize();
/// <summary>
/// The length of the <see cref="Line3D"/> segment.
/// </summary>
public readonly float Length => From.FromTo(To).Length();
/// <summary>
/// The squared length of the <see cref="Line3D"/> segment.
/// </summary>
public readonly float LengthSquared => From.FromTo(To).LengthSquared();
public static bool operator ==(Line3D left, Line3D right) => left.From == right.From && left.To == right.To;
public static bool operator !=(Line3D left, Line3D right) => left.From != right.From || left.To != right.To;
/// <summary>
/// Linearly interpolates between the two endpoints of the <see cref="Line3D"/> segment using parameter 't'.
/// </summary>
public static Vector3D Lerp(Line3D line, float t)
=> Vector3D.Lerp(line.From, line.To, t);
/// <summary>
/// Calculates the closest point on the <see cref="Line3D"/> segment to the specified point.
/// </summary>
public static Vector3D ClosestPointTo(Line3D line, Vector3D point)
{
Vector3D lineRelativeVector = line.From.FromTo(line.To);
Vector3D lineDirection = lineRelativeVector.Normalized;
Vector3D pointVector = line.From.FromTo(point);
float dot = lineDirection.Dot(pointVector).Clamp(0f, lineRelativeVector.Magnitude);
return lineDirection * dot;
}
/// <summary>
/// Checks if two <see cref="Line3D"/> segments are approximately equal.
/// </summary>
/// <param name="left">The first <see cref="Line3D"/>.</param>
/// <param name="right">The second <see cref="Line3D"/>.</param>
/// <param name="epsilon">The epsilon range.</param>
/// <returns><see cref="true"/> if the <see cref="Line3D"/>s are approximately equal; otherwise, <see cref="false"/>.</returns>
public static bool ApproximatelyEquals(Line3D left, Line3D right, float epsilon = float.Epsilon)
=> left.From.ApproximatelyEquals(right.From, epsilon) && left.To.ApproximatelyEquals(right.To, epsilon);
/// <summary>
/// Determines whether the specified object is equal to the current <see cref="Line3D"/>.
/// </summary>
/// <param name="obj">The object to compare with the current <see cref="Line3D"/>.</param>
/// <returns><see cref="true"/> if the specified object is equal to the current <see cref="Line3D"/>; otherwise, <see cref="false"/>.</returns>
public override bool Equals(object? obj) => obj is Line3D line3D && this == line3D;
public bool Equals(Line3D other) => this == other;
/// <summary>
/// Generates a hash code for the <see cref="Line3D"/>.
/// </summary>
/// <returns>A hash code for the <see cref="Line3D"/>.</returns>
public override int GetHashCode() => System.HashCode.Combine(From, To);
/// <summary>
/// Converts the <see cref="Line3D"/> to its string representation.
/// </summary>
/// <returns>A string representation of the <see cref="Line3D"/>.</returns>
public override string ToString() => $"{nameof(Line3D)}({From}, {To})";
}
/// <summary>
/// Provides extension methods for the <see cref="Line3D"/> struct.
/// </summary>
public static class Line3DExtensions
{
/// <inheritdoc cref="Line3D.Lerp(Line3D, float)" />
public static Vector3D Lerp(this Line3D line, float t) => Line3D.Lerp(line, t);
/// <inheritdoc cref="Line3D.ClosestPointTo(Line3D, Vector3D)" />
public static Vector3D ClosestPointTo(this Line3D line, Vector3D point) => Line3D.ClosestPointTo(line, point);
/// <inheritdoc cref="Line3D.ApproximatelyEquals(Line3D, Line3D, float)" />
public static bool ApproximatelyEquals(this Line3D left, Line3D right, float epsilon = float.Epsilon) => Line3D.ApproximatelyEquals(left, right, epsilon);
}

View File

@@ -0,0 +1,109 @@
using System;
namespace Engine.Core;
/// <summary>
/// Represents an infinite ray in 3D space.
/// </summary>
/// <param name="Origin">The <see cref="Vector3D"/> in 3D space where the ray starts from.</param>
/// <param name="Direction">Normalized <see cref="Vector3D"/> indicating the ray's is direction.</param>
public readonly struct Ray3D(Vector3D Origin, Vector3D Direction) : IEquatable<Ray3D>
{
/// <summary>
/// The starting point of the <see cref="Ray3D"/>.
/// </summary>
public readonly Vector3D Origin = Origin;
/// <summary>
/// The direction in which the <see cref="Ray3D"/> points. Should be a normalized vector.
/// </summary>
public readonly Vector3D Direction = Direction;
/// <summary>
/// Gets a <see cref="Ray3D"/> with the same origin but with the direction reversed.
/// </summary>
public readonly Ray3D Reversed => new(Origin, -Direction);
public static bool operator ==(Ray3D left, Ray3D right) => left.Origin == right.Origin && left.Direction == right.Direction;
public static bool operator !=(Ray3D left, Ray3D right) => left.Origin != right.Origin || left.Direction != right.Direction;
public static implicit operator Ray3D(Line3D line) => new(line.From, line.From.FromTo(line.To).Normalized);
/// <summary>
/// Constructs a <see cref="Line3D"/> from a <see cref="Ray3D"/>, extending from its origin in the <see cref="Ray3D"/>'s direction for a given distance.
/// </summary>
/// <param name="ray">The source <see cref="Ray3D"/>.</param>
/// <param name="distance">The length of the line segment to create from the <see cref="Ray3D"/>.</param>
/// <returns>A <see cref="Line3D"/> representing the segment of the <see cref="Ray3D"/>.</returns>
public static Line3D GetLine(Ray3D ray, float distance)
=> new(ray.Origin, ray.Origin + ray.Direction * distance);
/// <summary>
/// Evaluates the point on the <see cref="Ray3D"/> at a specified distance from its origin.
/// </summary>
/// <param name="ray">The <see cref="Ray3D"/> to evaluate.</param>
/// <param name="distanceFromOrigin">The distance from the origin along the <see cref="Ray3D"/>'s direction.</param>
/// <returns>A <see cref="Vector3D"/> representing the point at the given distance on the <see cref="Ray3D"/>.</returns>
public static Vector3D Evaluate(Ray3D ray, float distanceFromOrigin)
=> ray.Origin + ray.Direction * distanceFromOrigin;
/// <summary>
/// Calculates the closest point on the <see cref="Ray3D"/> to the specified point.
/// </summary>
public static Vector3D ClosestPointTo(Ray3D ray, Vector3D point)
{
Vector3D originToPoint = ray.Origin.FromTo(point);
float dot = ray.Direction.Dot(originToPoint);
return ray.Origin + ray.Direction * dot;
}
/// <summary>
/// Checks if two <see cref="Ray3D"/>s are approximately equal within a specified epsilon range.
/// </summary>
/// <param name="left">The first <see cref="Ray3D"/>.</param>
/// <param name="right">The second <see cref="Ray3D"/>.</param>
/// <param name="epsilon">The epsilon range.</param>
/// <returns><see cref="true"/> if the <see cref="Ray3D"/>s are approximately equal; otherwise, <see cref="false"/>.</returns>
public static bool ApproximatelyEquals(Ray3D left, Ray3D right, float epsilon = float.Epsilon)
=> left.Origin.ApproximatelyEquals(right.Origin, epsilon) && left.Direction.ApproximatelyEquals(right.Direction, epsilon);
/// <summary>
/// Determines whether the specified object is equal to the current <see cref="Ray3D"/>.
/// </summary>
/// <param name="obj">The object to compare with the current <see cref="Ray3D"/>.</param>
/// <returns><see cref="true"/> if the specified object is equal to the current <see cref="Ray3D"/>; otherwise, <see cref="false"/>.</returns>
public override bool Equals(object? obj) => obj is Ray3D ray3D && this == ray3D;
public bool Equals(Ray3D other) => this == other;
/// <summary>
/// Generates a hash code for the <see cref="Ray3D"/>.
/// </summary>
/// <returns>A hash code for the <see cref="Ray3D"/>.</returns>
public override int GetHashCode() => System.HashCode.Combine(Origin, Direction);
/// <summary>
/// Converts the <see cref="Ray3D"/> to its string representation.
/// </summary>
/// <returns>A string representation of the <see cref="Ray3D"/>.</returns>
public override string ToString() => $"{nameof(Ray3D)}({Origin}, {Direction})";
}
/// <summary>
/// Provides extension methods for the <see cref="Ray3D"/> struct.
/// </summary>
public static class Ray3DExtensions
{
/// <inheritdoc cref="Ray3D.GetLine(Ray3D, float) />
public static Line3D ToLine(this Ray3D ray, float distance) => Ray3D.GetLine(ray, distance);
/// <inheritdoc cref="Ray3D.Evaluate(Ray3D, float) />
public static Vector3D Evaluate(this Ray3D ray, float distanceFromOrigin) => Ray3D.Evaluate(ray, distanceFromOrigin);
/// <inheritdoc cref="Ray3D.ClosestPointTo(Ray3D, Vector3D) />
public static Vector3D ClosestPointTo(this Ray3D ray, Vector3D point) => Ray3D.ClosestPointTo(ray, point);
/// <inheritdoc cref="Ray3D.ApproximatelyEquals(Ray3D, Ray3D, float)" />
public static bool ApproximatelyEquals(this Ray3D left, Ray3D right, float epsilon = float.Epsilon) => Ray3D.ApproximatelyEquals(left, right, epsilon);
}