mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-26 04:58:24 +01:00
149 lines
5.2 KiB
C#
149 lines
5.2 KiB
C#
// <auto-generated /> - edit Plane.tt to modify
|
|
#nullable enable
|
|
|
|
namespace Brutal.Numerics;
|
|
|
|
/// <summary>
|
|
/// Represents a plane in three-dimensional space, with double components.
|
|
/// </summary>
|
|
[DebuggerTypeProxy(typeof(PlaneDebugView<doublePlane, double3, double>))]
|
|
public partial struct doublePlane : IPlane<doublePlane, double3, double>
|
|
{
|
|
/// <inheritdoc />
|
|
public double3 Normal { get; set; }
|
|
|
|
/// <inheritdoc />
|
|
public double D { get; set; }
|
|
|
|
/// <inheritdoc cref="Plane(float, float, float, float)" />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public doublePlane(double x, double y, double z, double d)
|
|
{
|
|
Normal = new double3(x, y, z);
|
|
D = d;
|
|
}
|
|
|
|
/// <inheritdoc cref="Plane(Vector3, float)" />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public doublePlane(double3 normal, double d)
|
|
{
|
|
Normal = normal;
|
|
D = d;
|
|
}
|
|
|
|
/// <inheritdoc cref="Plane(Vector4)" />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public doublePlane(double4 value)
|
|
{
|
|
Normal = value.XYZ;
|
|
D = value.W;
|
|
}
|
|
|
|
/// <summary>A mutable view over the 4 components: normal xyz, then d.</summary>
|
|
/// <remarks>
|
|
/// Deliberately NOT a readonly member, because it hands out a writable view. The consequence is that
|
|
/// calling it on a readonly field or an <see langword="in" /> parameter gets you a span over a
|
|
/// defensive COPY, so writes there are silently lost - reach for <see cref="AsReadOnlySpan" /> instead.
|
|
/// </remarks>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public Span<double> AsSpan()
|
|
{
|
|
return MemoryMarshal.CreateSpan(ref Unsafe.As<doublePlane, double>(ref this), 4);
|
|
}
|
|
|
|
/// <summary>A read-only view over the 4 components, safe in the readonly contexts where <see cref="AsSpan" /> would view a copy.</summary>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public readonly ReadOnlySpan<double> AsReadOnlySpan()
|
|
{
|
|
return MemoryMarshal.CreateReadOnlySpan(in Unsafe.As<doublePlane, double>(ref Unsafe.AsRef(in this)), 4);
|
|
}
|
|
|
|
/// <summary>Construct a plane of this type from the leading 4 elements of a span (normal xyz, then d).</summary>
|
|
/// <param name="span">The elements to read; must contain at least 4, and any beyond that are ignored.</param>
|
|
/// <exception cref="ArgumentOutOfRangeException"><paramref name="span" /> holds fewer than 4 elements.</exception>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public doublePlane(ReadOnlySpan<double> span)
|
|
{
|
|
if (span.Length < 4)
|
|
throw new ArgumentOutOfRangeException(nameof(span), "Length must be at least 4.");
|
|
|
|
this = Unsafe.ReadUnaligned<doublePlane>(ref Unsafe.As<double, byte>(ref MemoryMarshal.GetReference(span)));
|
|
}
|
|
|
|
/// <summary>Construct a plane of this type from a span (normal xyz, then d).</summary>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
static doublePlane IPlane<doublePlane, double3, double>.Create(ReadOnlySpan<double> span) => new(span);
|
|
}
|
|
|
|
// Operators
|
|
public partial struct doublePlane
|
|
{
|
|
/// <inheritdoc cref="Plane.op_Equality(Plane, Plane)" />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static bool operator ==(doublePlane a, doublePlane b)
|
|
{
|
|
// IEEE like the vector ==; Equals stays NaN-reflexive (see Vector.equality.tt)
|
|
return a.Normal == b.Normal && a.D == b.D;
|
|
}
|
|
|
|
/// <inheritdoc cref="Plane.op_Inequality(Plane, Plane)" />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static bool operator !=(doublePlane a, doublePlane b)
|
|
{
|
|
return !(a == b);
|
|
}
|
|
}
|
|
|
|
// IEquatable
|
|
public partial struct doublePlane
|
|
{
|
|
/// <inheritdoc cref="Plane.Equals(object?)" />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public override readonly bool Equals([NotNullWhen(true)] object? obj)
|
|
{
|
|
return obj is doublePlane other && Equals(other);
|
|
}
|
|
|
|
/// <inheritdoc cref="Plane.Equals(Plane)" />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public readonly bool Equals(doublePlane other)
|
|
{
|
|
return Normal.Equals(other.Normal) && D.Equals(other.D);
|
|
}
|
|
|
|
/// <inheritdoc cref="Plane.GetHashCode" />
|
|
public readonly override int GetHashCode()
|
|
{
|
|
return HashCode.Combine(Normal, D);
|
|
}
|
|
}
|
|
|
|
// String
|
|
public partial struct doublePlane
|
|
{
|
|
/// <inheritdoc cref="Plane.ToString" />
|
|
public readonly override string ToString()
|
|
{
|
|
return $"{{ Normal:{Normal} D:{D} }}";
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public readonly string ToString(string? format, IFormatProvider? formatProvider)
|
|
{
|
|
return $"{{ Normal:{Normal.ToString(format, formatProvider)} D:{D.ToString(format, formatProvider)} }}";
|
|
}
|
|
|
|
/// <summary>Reinterpret as the matching vector4 - xyz+d in the same order.</summary>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
private readonly double4 AsDouble4()
|
|
{
|
|
return Unsafe.BitCast<doublePlane, double4>(this);
|
|
}
|
|
|
|
/// <summary>Reinterpret a vector4 as a plane.</summary>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
private static doublePlane FromDouble4(double4 value)
|
|
{
|
|
return Unsafe.BitCast<double4, doublePlane>(value);
|
|
}
|
|
}
|