// - edit Plane.tt to modify #nullable enable namespace Brutal.Numerics; /// /// Represents a plane in three-dimensional space, with float components. /// [DebuggerTypeProxy(typeof(PlaneDebugView))] public partial struct floatPlane : IPlane { /// public float3 Normal { get; set; } /// public float D { get; set; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public floatPlane(float x, float y, float z, float d) { Normal = new float3(x, y, z); D = d; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public floatPlane(float3 normal, float d) { Normal = normal; D = d; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public floatPlane(float4 value) { Normal = value.XYZ; D = value.W; } /// A mutable view over the 4 components: normal xyz, then d. /// /// Deliberately NOT a readonly member, because it hands out a writable view. The consequence is that /// calling it on a readonly field or an parameter gets you a span over a /// defensive COPY, so writes there are silently lost - reach for instead. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public Span AsSpan() { return MemoryMarshal.CreateSpan(ref Unsafe.As(ref this), 4); } /// A read-only view over the 4 components, safe in the readonly contexts where would view a copy. [MethodImpl(MethodImplOptions.AggressiveInlining)] public readonly ReadOnlySpan AsReadOnlySpan() { return MemoryMarshal.CreateReadOnlySpan(in Unsafe.As(ref Unsafe.AsRef(in this)), 4); } /// Construct a plane of this type from the leading 4 elements of a span (normal xyz, then d). /// The elements to read; must contain at least 4, and any beyond that are ignored. /// holds fewer than 4 elements. [MethodImpl(MethodImplOptions.AggressiveInlining)] public floatPlane(ReadOnlySpan span) { if (span.Length < 4) throw new ArgumentOutOfRangeException(nameof(span), "Length must be at least 4."); this = Unsafe.ReadUnaligned(ref Unsafe.As(ref MemoryMarshal.GetReference(span))); } /// Construct a plane of this type from a span (normal xyz, then d). [MethodImpl(MethodImplOptions.AggressiveInlining)] static floatPlane IPlane.Create(ReadOnlySpan span) => new(span); } // Operators public partial struct floatPlane { /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator ==(floatPlane a, floatPlane b) { // IEEE like the vector ==; Equals stays NaN-reflexive (see Vector.equality.tt) return a.Normal == b.Normal && a.D == b.D; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator !=(floatPlane a, floatPlane b) { return !(a == b); } } // IEquatable public partial struct floatPlane { /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public override readonly bool Equals([NotNullWhen(true)] object? obj) { return obj is floatPlane other && Equals(other); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public readonly bool Equals(floatPlane other) { return Normal.Equals(other.Normal) && D.Equals(other.D); } /// public readonly override int GetHashCode() { return HashCode.Combine(Normal, D); } } // String public partial struct floatPlane { /// public readonly override string ToString() { return $"{{ Normal:{Normal} D:{D} }}"; } /// public readonly string ToString(string? format, IFormatProvider? formatProvider) { return $"{{ Normal:{Normal.ToString(format, formatProvider)} D:{D.ToString(format, formatProvider)} }}"; } /// Reinterpret as the matching vector4 - xyz+d in the same order. [MethodImpl(MethodImplOptions.AggressiveInlining)] private readonly float4 AsFloat4() { return Unsafe.BitCast(this); } /// Reinterpret a vector4 as a plane. [MethodImpl(MethodImplOptions.AggressiveInlining)] private static floatPlane FromFloat4(float4 value) { return Unsafe.BitCast(value); } }