// - edit Plane.tt to modify #nullable enable namespace Brutal.Numerics; /// /// Represents a plane in three-dimensional space, with double components. /// [DebuggerTypeProxy(typeof(PlaneDebugView))] public partial struct doublePlane : IPlane { /// public double3 Normal { get; set; } /// public double D { get; set; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public doublePlane(double x, double y, double z, double d) { Normal = new double3(x, y, z); D = d; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public doublePlane(double3 normal, double d) { Normal = normal; D = d; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public doublePlane(double4 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 doublePlane(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 doublePlane IPlane.Create(ReadOnlySpan span) => new(span); } // Operators public partial struct doublePlane { /// [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; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator !=(doublePlane a, doublePlane b) { return !(a == b); } } // IEquatable public partial struct doublePlane { /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public override readonly bool Equals([NotNullWhen(true)] object? obj) { return obj is doublePlane other && Equals(other); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public readonly bool Equals(doublePlane other) { return Normal.Equals(other.Normal) && D.Equals(other.D); } /// public readonly override int GetHashCode() { return HashCode.Combine(Normal, D); } } // String public partial struct doublePlane { /// 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 double4 AsDouble4() { return Unsafe.BitCast(this); } /// Reinterpret a vector4 as a plane. [MethodImpl(MethodImplOptions.AggressiveInlining)] private static doublePlane FromDouble4(double4 value) { return Unsafe.BitCast(value); } }