This commit is contained in:
Rian Drake 2026-10-15 14:09:11 +13:00
commit b578433e37
8 changed files with 784842 additions and 0 deletions

View file

@ -0,0 +1,28 @@
// <auto-generated /> - edit Vector.copy.tt to modify
#nullable enable
namespace Brutal.Numerics;
public partial struct doubleQuat
{
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void CopyTo(Span<double> destination)
{
if (destination.Length < ComponentCount)
throw new ArgumentException("Length must be at least 4.", nameof(destination));
Unsafe.WriteUnaligned(ref Unsafe.As<double, byte>(ref MemoryMarshal.GetReference(destination)), this);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool TryCopyTo(Span<double> destination)
{
if (destination.Length < ComponentCount)
return false;
Unsafe.WriteUnaligned(ref Unsafe.As<double, byte>(ref MemoryMarshal.GetReference(destination)), this);
return true;
}
}

View file

@ -0,0 +1,41 @@
// <auto-generated /> - edit Vector.equality.tt to modify
#nullable enable
namespace Brutal.Numerics;
public partial struct doubleQuat
{
/// <inheritdoc />
public readonly override int GetHashCode()
{
return HashCode.Combine(X, Y, Z, W);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool Equals(doubleQuat other)
{
return X.Equals(other.X) && Y.Equals(other.Y) && Z.Equals(other.Z) && W.Equals(other.W);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly override bool Equals([NotNullWhen(true)] object? obj)
{
return obj is doubleQuat other && Equals(other);
}
/// <summary>IEEE lane-wise equality over every component: any NaN component makes this false (contrast <see cref="Equals(doubleQuat)" />, which is NaN-reflexive), and -0 equals +0.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(doubleQuat a, doubleQuat b)
{
return a.X == b.X && a.Y == b.Y && a.Z == b.Z && a.W == b.W;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(doubleQuat a, doubleQuat b)
{
return !(a == b);
}
}

View file

@ -0,0 +1,144 @@
// <auto-generated /> - edit Vector.format.tt to modify
#nullable enable
namespace Brutal.Numerics;
public partial struct doubleQuat : ISpanFormattable, IUtf8SpanFormattable, ISpanParsable<doubleQuat>
{
/// <inheritdoc />
public readonly override string ToString()
{
return ToString("G", CultureInfo.CurrentCulture);
}
/// <inheritdoc cref="Vector4.ToString(string?)" />
public readonly string ToString(string? format)
{
return ToString(format, CultureInfo.CurrentCulture);
}
/// <inheritdoc />
public readonly string ToString(string? format, IFormatProvider? formatProvider)
{
var separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator;
return $"<{X.ToString(format, formatProvider)}{separator} {Y.ToString(format, formatProvider)}{separator} {Z.ToString(format, formatProvider)}{separator} {W.ToString(format, formatProvider)}>";
}
/// <inheritdoc />
public readonly bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider)
{
var separator = NumberFormatInfo.GetInstance(provider).NumberGroupSeparator;
// the handler takes a string format (an IFormattable legacy) - null on the default path stays alloc-free
var formatString = format.IsEmpty ? null : format.ToString();
var handler = new MemoryExtensions.TryWriteInterpolatedStringHandler(5, 7, destination, provider, out var shouldAppend);
_ = shouldAppend
&& handler.AppendLiteral("<")
&& handler.AppendFormatted(X, formatString)
&& handler.AppendFormatted(separator) && handler.AppendLiteral(" ")
&& handler.AppendFormatted(Y, formatString)
&& handler.AppendFormatted(separator) && handler.AppendLiteral(" ")
&& handler.AppendFormatted(Z, formatString)
&& handler.AppendFormatted(separator) && handler.AppendLiteral(" ")
&& handler.AppendFormatted(W, formatString)
&& handler.AppendLiteral(">");
return destination.TryWrite(ref handler, out charsWritten);
}
/// <inheritdoc />
public readonly bool TryFormat(Span<byte> destination, out int bytesWritten, ReadOnlySpan<char> format, IFormatProvider? provider)
{
var separator = NumberFormatInfo.GetInstance(provider).NumberGroupSeparator;
// the handler takes a string format (an IFormattable legacy) - null on the default path stays alloc-free
var formatString = format.IsEmpty ? null : format.ToString();
var handler = new Utf8.TryWriteInterpolatedStringHandler(5, 7, destination, provider, out var shouldAppend);
_ = shouldAppend
&& handler.AppendLiteral("<")
&& handler.AppendFormatted(X, formatString)
&& handler.AppendFormatted(separator) && handler.AppendLiteral(" ")
&& handler.AppendFormatted(Y, formatString)
&& handler.AppendFormatted(separator) && handler.AppendLiteral(" ")
&& handler.AppendFormatted(Z, formatString)
&& handler.AppendFormatted(separator) && handler.AppendLiteral(" ")
&& handler.AppendFormatted(W, formatString)
&& handler.AppendLiteral(">");
return Utf8.TryWrite(destination, ref handler, out bytesWritten);
}
/// <inheritdoc cref="Parse(ReadOnlySpan{char}, NumberStyles, IFormatProvider?)" />
public static doubleQuat Parse(string s, IFormatProvider? provider)
{
ArgumentNullException.ThrowIfNull(s);
return Parse(s.AsSpan(), NumberStyles.Float, provider);
}
/// <inheritdoc cref="Parse(ReadOnlySpan{char}, NumberStyles, IFormatProvider?)" />
public static doubleQuat Parse(ReadOnlySpan<char> s, IFormatProvider? provider = null)
{
return Parse(s, NumberStyles.Float, provider);
}
/// <inheritdoc cref="Parse(ReadOnlySpan{char}, NumberStyles, IFormatProvider?)" />
public static doubleQuat Parse(string s, NumberStyles style, IFormatProvider? provider)
{
ArgumentNullException.ThrowIfNull(s);
return Parse(s.AsSpan(), style, provider);
}
/// <summary>Parses from the <see cref="ToString()" /> format, <c>&lt;1, 2, 3&gt;</c>.</summary>
/// <exception cref="FormatException">The input is not in a valid format, or a component fails to parse.</exception>
public static doubleQuat Parse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider)
{
if (!TryParse(s, style, provider, out var result))
throw new FormatException($"'{s}' is not in a valid doubleQuat format.");
return result;
}
/// <inheritdoc cref="TryParse(ReadOnlySpan{char}, NumberStyles, IFormatProvider?, out doubleQuat)" />
public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, out doubleQuat result)
{
return TryParse(s.AsSpan(), NumberStyles.Float, provider, out result);
}
/// <inheritdoc cref="TryParse(ReadOnlySpan{char}, NumberStyles, IFormatProvider?, out doubleQuat)" />
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out doubleQuat result)
{
return TryParse(s, NumberStyles.Float, provider, out result);
}
/// <inheritdoc cref="TryParse(ReadOnlySpan{char}, NumberStyles, IFormatProvider?, out doubleQuat)" />
public static bool TryParse([NotNullWhen(true)] string? s, NumberStyles style, IFormatProvider? provider, out doubleQuat result)
{
return TryParse(s.AsSpan(), style, provider, out result);
}
/// <summary>Parses from the <see cref="ToString()" /> format, <c>&lt;1, 2, 3&gt;</c>.</summary>
/// <returns>true if the input parsed as a doubleQuat.</returns>
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider, out doubleQuat result)
{
result = default;
s = s.Trim();
if (s.Length < 2 || s[0] != '<' || s[^1] != '>')
return false;
s = s[1..^1];
var separator = NumberFormatInfo.GetInstance(provider).NumberGroupSeparator;
var separatorIndex = s.IndexOf(separator);
// trimmed: TryFormat writes "separator + space", which must not demand AllowLeadingWhite
if (separatorIndex < 0 || !double.TryParse(s[..separatorIndex].Trim(), style, provider, out var x))
return false;
s = s[(separatorIndex + separator.Length)..];
separatorIndex = s.IndexOf(separator);
// trimmed: TryFormat writes "separator + space", which must not demand AllowLeadingWhite
if (separatorIndex < 0 || !double.TryParse(s[..separatorIndex].Trim(), style, provider, out var y))
return false;
s = s[(separatorIndex + separator.Length)..];
separatorIndex = s.IndexOf(separator);
// trimmed: TryFormat writes "separator + space", which must not demand AllowLeadingWhite
if (separatorIndex < 0 || !double.TryParse(s[..separatorIndex].Trim(), style, provider, out var z))
return false;
s = s[(separatorIndex + separator.Length)..];
if (!double.TryParse(s.Trim(), style, provider, out var w))
return false;
result = new(x, y, z, w);
return true;
}
}

View file

@ -0,0 +1,116 @@
// <auto-generated /> - edit Vector.tt to modify
#nullable enable
namespace Brutal.Numerics;
/// <summary>
/// Implements a quaternion with double components.
/// </summary>
[DebuggerTypeProxy(typeof(QuaternionDebugView<doubleQuat, double>))]
public partial struct doubleQuat : IQuaternion<doubleQuat, double>
, IMultiplyOperators<doubleQuat, double2, double2>
, IMultiplyOperators<doubleQuat, double3, double3>
, IMultiplyOperators<doubleQuat, double4, double4>
, IQuaternionFactories<doubleQuat, double, double3, double4x4>
{
/// <inheritdoc />
public static int ComponentSize => sizeof(double);
/// <summary>Gets the number of components.</summary>
public static int ComponentCount => 4;
/// <inheritdoc cref="Quaternion.X" />
[XmlAttribute]
public double X;
/// <inheritdoc cref="Quaternion.Y" />
[XmlAttribute]
public double Y;
/// <inheritdoc cref="Quaternion.Z" />
[XmlAttribute]
public double Z;
/// <inheritdoc cref="Quaternion.W" />
[XmlAttribute]
public double W;
/// <inheritdoc cref="Quaternion(float, float, float, float)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public doubleQuat(double x, double y, double z, double w)
{
X = x;
Y = y;
Z = z;
W = w;
}
/// <inheritdoc cref="Quaternion(Vector3, float)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public doubleQuat(double3 vectorPart, double scalarPart)
{
X = vectorPart.X;
Y = vectorPart.Y;
Z = vectorPart.Z;
W = scalarPart;
}
/// <summary>Creates a quaternion from the leading 4 elements of a span.</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 doubleQuat(ReadOnlySpan<double> span)
{
if (span.Length < 4)
throw new ArgumentOutOfRangeException(nameof(span), "Length must be at least 4.");
this = Unsafe.ReadUnaligned<doubleQuat>(ref Unsafe.As<double, byte>(ref MemoryMarshal.GetReference(span)));
}
/// <inheritdoc cref="doubleQuat(ReadOnlySpan{double})" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static doubleQuat IComponentTuple<doubleQuat, double>.Create(ReadOnlySpan<double> span) => new(span);
/// <inheritdoc />
public double this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
readonly get
{
if ((uint)index >= ComponentCount)
throw new ArgumentOutOfRangeException(nameof(index));
return Unsafe.Add(ref Unsafe.AsRef(in X), index);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
if ((uint)index >= ComponentCount)
throw new ArgumentOutOfRangeException(nameof(index));
Unsafe.Add(ref X, index) = value;
}
}
public readonly void Deconstruct(out double x, out double y, out double z, out double w)
{
x = X;
y = Y;
z = Z;
w = W;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Span<double> AsSpan()
{
return MemoryMarshal.CreateSpan(ref Unsafe.As<doubleQuat, double>(ref this), 4);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly ReadOnlySpan<double> AsReadOnlySpan()
{
return MemoryMarshal.CreateReadOnlySpan(in Unsafe.As<doubleQuat, double>(ref Unsafe.AsRef(in this)), 4);
}
}

View file

@ -0,0 +1,48 @@
// <auto-generated /> - edit Vector.math-metric.tt to modify
#nullable enable
namespace Brutal.Numerics;
public partial struct doubleQuat
{
/// <inheritdoc cref="Quaternion.Length" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly double Length()
{
return double.Sqrt(LengthSquared());
}
/// <inheritdoc cref="Quaternion.LengthSquared" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly double LengthSquared()
{
return Dot(this, this);
}
/// <inheritdoc cref="Quaternion.Dot" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Dot(doubleQuat a, doubleQuat b)
{
return a.X * b.X + a.Y * b.Y + a.Z * b.Z + a.W * b.W;
}
/// <inheritdoc cref="Quaternion.Normalize" />
/// <exception cref="DivideByZeroException">Thrown when the input quaternion is a zero quaternion.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat Normalize(doubleQuat value)
{
var lengthSquared = Dot(value, value);
if (lengthSquared == 0)
throw new DivideByZeroException();
return value / double.Sqrt(lengthSquared);
}
/// <inheritdoc cref="Quaternion.Normalize" />
/// <remarks>Returns zero if the input quaternion is zero.</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat NormalizeOrZero(doubleQuat value)
{
var length = double.Sqrt(Dot(value, value));
return length == 0 ? Zero : value / length;
}
}

View file

@ -0,0 +1,200 @@
// <auto-generated /> - edit Quat.math.tt to modify
#nullable enable
namespace Brutal.Numerics;
public partial struct doubleQuat
{
/// <inheritdoc cref="Quaternion.IsIdentity" />
public readonly bool IsIdentity => this == Identity;
/// <inheritdoc cref="Quaternion.Concatenate(Quaternion, Quaternion)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat Concatenate(doubleQuat a, doubleQuat b)
{
return b * a;
}
/// <inheritdoc cref="Quaternion.Conjugate(Quaternion)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat Conjugate(doubleQuat value)
{
if (!Vector256.IsHardwareAccelerated)
return new(-value.X, -value.Y, -value.Z, value.W);
// -x, -y, -z, w
return Unsafe.BitCast<Vector256<double>, doubleQuat>(Unsafe.BitCast<doubleQuat, Vector256<double>>(value) ^ Vector256.Create(-0.0, -0.0, -0.0, 0.0));
}
/// <inheritdoc cref="Quaternion.CreateFromAxisAngle(Vector3, float)" />
public static doubleQuat CreateFromAxisAngle(double3 axis, double angle)
{
doubleQuat ans;
var halfAngle = angle * 0.5;
var (s, c) = double.SinCos(halfAngle);
ans.X = axis.X * s;
ans.Y = axis.Y * s;
ans.Z = axis.Z * s;
ans.W = c;
return ans;
}
/// <inheritdoc cref="Quaternion.CreateFromRotationMatrix(Matrix4x4)" />
public static doubleQuat CreateFromRotationMatrix(double4x4 matrix)
{
var trace = matrix.M11 + matrix.M22 + matrix.M33;
doubleQuat q = default;
if (trace > 0.0)
{
var s = double.Sqrt(trace + 1);
q.W = s * 0.5;
s = 0.5 / s;
q.X = (matrix.M23 - matrix.M32) * s;
q.Y = (matrix.M31 - matrix.M13) * s;
q.Z = (matrix.M12 - matrix.M21) * s;
}
else
{
if (matrix.M11 >= matrix.M22 && matrix.M11 >= matrix.M33)
{
var s = double.Sqrt(1 + matrix.M11 - matrix.M22 - matrix.M33);
var invS = 0.5 / s;
q.X = 0.5 * s;
q.Y = (matrix.M12 + matrix.M21) * invS;
q.Z = (matrix.M13 + matrix.M31) * invS;
q.W = (matrix.M23 - matrix.M32) * invS;
}
else if (matrix.M22 > matrix.M33)
{
var s = double.Sqrt(1 + matrix.M22 - matrix.M11 - matrix.M33);
var invS = 0.5 / s;
q.X = (matrix.M21 + matrix.M12) * invS;
q.Y = 0.5 * s;
q.Z = (matrix.M32 + matrix.M23) * invS;
q.W = (matrix.M31 - matrix.M13) * invS;
}
else
{
var s = double.Sqrt(1 + matrix.M33 - matrix.M11 - matrix.M22);
var invS = 0.5 / s;
q.X = (matrix.M31 + matrix.M13) * invS;
q.Y = (matrix.M32 + matrix.M23) * invS;
q.Z = 0.5 * s;
q.W = (matrix.M12 - matrix.M21) * invS;
}
}
return q;
}
/// <inheritdoc cref="Quaternion.CreateFromYawPitchRoll(float, float, float)" />
public static doubleQuat CreateFromYawPitchRoll(double yaw, double pitch, double roll)
{
// Roll first, about axis the object is facing, then
// pitch upward, then yaw to face into the new heading
var (sr, cr) = double.SinCos(roll * 0.5);
var (sp, cp) = double.SinCos(pitch * 0.5);
var (sy, cy) = double.SinCos(yaw * 0.5);
doubleQuat result;
result.X = cy * sp * cr + sy * cp * sr;
result.Y = sy * cp * cr - cy * sp * sr;
result.Z = cy * cp * sr - sy * sp * cr;
result.W = cy * cp * cr + sy * sp * sr;
return result;
}
/// <inheritdoc cref="Quaternion.Inverse(Quaternion)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat Inverse(doubleQuat value)
{
// -1 ( a -v )
// q = ( ------------- ------------- )
// ( a^2 + |v|^2 , a^2 + |v|^2 )
return Conjugate(value) / value.LengthSquared();
}
/// <inheritdoc cref="Quaternion.Lerp(Quaternion, Quaternion, float)" />
public static doubleQuat Lerp(doubleQuat a, doubleQuat b, double amount)
{
var t = amount;
var t1 = 1 - t;
var dot = Dot(a, b);
var r = dot >= 0
? a * t1 + b * t
: a * t1 - b * t;
// Normalize it.
var invNorm = 1 / double.Sqrt(r.LengthSquared());
return r * invNorm;
}
private const double SlerpEpsilon = 1e-12;
/// <inheritdoc cref="Quaternion.Slerp(Quaternion, Quaternion, float)" />
public static doubleQuat Slerp(doubleQuat a, doubleQuat b, double amount)
{
var t = amount;
var cosOmega = Dot(a, b);
var flip = false;
if (cosOmega < 0)
{
flip = true;
cosOmega = -cosOmega;
}
double s1,
s2;
if (cosOmega > 1 - SlerpEpsilon)
{
// Too close, do straight linear interpolation.
s1 = 1 - t;
s2 = flip ? -t : t;
}
else
{
var omega = double.Acos(cosOmega);
var invSinOmega = 1 / double.Sin(omega);
s1 = double.Sin((1 - t) * omega) * invSinOmega;
s2 = flip ? -double.Sin(t * omega) * invSinOmega : double.Sin(t * omega) * invSinOmega;
}
return a * s1 + b * s2;
}
/// <inheritdoc cref="double2.Transform(double2, doubleQuat)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double2 operator *(doubleQuat rotation, double2 vector)
{
return double2.Transform(vector, rotation);
}
/// <inheritdoc cref="double3.Transform(double3, doubleQuat)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double3 operator *(doubleQuat rotation, double3 vector)
{
return double3.Transform(vector, rotation);
}
/// <inheritdoc cref="double4.Transform(double4, doubleQuat)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double4 operator *(doubleQuat rotation, double4 vector)
{
return double4.Transform(vector, rotation);
}
}

View file

@ -0,0 +1,515 @@
// <auto-generated /> - edit Quat.numerics.tt to modify
#nullable enable
namespace Brutal.Numerics;
// INumberBase
public partial struct doubleQuat : INumberBase<doubleQuat>
{
/// <inheritdoc cref="Quaternion.Zero" />
public static doubleQuat Zero => default;
/// <inheritdoc cref="Quaternion.Identity" />
public static doubleQuat Identity => new(0, 0, 0, 1);
/* One is the multiplicative identity - the identity quaternion - which the concrete API
* already names Identity; a public One would read as all-ones. */
static doubleQuat INumberBase<doubleQuat>.One => Identity;
static doubleQuat IAdditiveIdentity<doubleQuat, doubleQuat>.AdditiveIdentity => Zero;
static doubleQuat IMultiplicativeIdentity<doubleQuat, doubleQuat>.MultiplicativeIdentity => Identity;
static int INumberBase<doubleQuat>.Radix => 2;
// Abs is the norm, embedded on the real axis as a scalar quaternion (cf. Complex.Abs)
static doubleQuat INumberBase<doubleQuat>.Abs(doubleQuat value) => new(0, 0, 0, value.Length());
static bool INumberBase<doubleQuat>.IsCanonical(doubleQuat value) => true;
static bool INumberBase<doubleQuat>.IsComplexNumber(doubleQuat value) =>
value.W != 0 && !(value.X == 0 && value.Y == 0 && value.Z == 0);
static bool INumberBase<doubleQuat>.IsImaginaryNumber(doubleQuat value) =>
value.W == 0 && double.IsRealNumber(value.X) && double.IsRealNumber(value.Y) && double.IsRealNumber(value.Z);
static bool INumberBase<doubleQuat>.IsRealNumber(doubleQuat value) =>
value.X == 0 && value.Y == 0 && value.Z == 0 && double.IsRealNumber(value.W);
/// <summary>Determines if a value is zero.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsZero(doubleQuat value)
{
return value == Zero;
}
static bool INumberBase<doubleQuat>.IsEvenInteger(doubleQuat value) =>
value.X == 0 && value.Y == 0 && value.Z == 0 && double.IsEvenInteger(value.W);
static bool INumberBase<doubleQuat>.IsOddInteger(doubleQuat value) =>
value.X == 0 && value.Y == 0 && value.Z == 0 && double.IsOddInteger(value.W);
static bool INumberBase<doubleQuat>.IsInteger(doubleQuat value) =>
value.X == 0 && value.Y == 0 && value.Z == 0 && double.IsInteger(value.W);
static bool INumberBase<doubleQuat>.IsNegative(doubleQuat value) =>
value.X == 0 && value.Y == 0 && value.Z == 0 && double.IsRealNumber(value.W) && double.IsNegative(value.W);
static bool INumberBase<doubleQuat>.IsPositive(doubleQuat value) =>
value.X == 0 && value.Y == 0 && value.Z == 0 && double.IsRealNumber(value.W) && double.IsPositive(value.W);
static bool INumberBase<doubleQuat>.IsFinite(doubleQuat value) =>
double.IsFinite(value.X) && double.IsFinite(value.Y) && double.IsFinite(value.Z) && double.IsFinite(value.W);
static bool INumberBase<doubleQuat>.IsInfinity(doubleQuat value) =>
double.IsInfinity(value.X) || double.IsInfinity(value.Y) || double.IsInfinity(value.Z) || double.IsInfinity(value.W);
static bool INumberBase<doubleQuat>.IsNegativeInfinity(doubleQuat value) =>
value.X == 0 && value.Y == 0 && value.Z == 0 && double.IsNegativeInfinity(value.W);
static bool INumberBase<doubleQuat>.IsPositiveInfinity(doubleQuat value) =>
value.X == 0 && value.Y == 0 && value.Z == 0 && double.IsPositiveInfinity(value.W);
// NaN present and no infinity - infinity dominates, per Complex
static bool INumberBase<doubleQuat>.IsNaN(doubleQuat value) =>
(double.IsNaN(value.X) || double.IsNaN(value.Y) || double.IsNaN(value.Z) || double.IsNaN(value.W))
&& !(double.IsInfinity(value.X) || double.IsInfinity(value.Y) || double.IsInfinity(value.Z) || double.IsInfinity(value.W));
static bool INumberBase<doubleQuat>.IsNormal(doubleQuat value) =>
double.IsNormal(value.W)
&& (value.X == 0 && value.Y == 0 && value.Z == 0 || (double.IsNormal(value.X) && double.IsNormal(value.Y) && double.IsNormal(value.Z)));
static bool INumberBase<doubleQuat>.IsSubnormal(doubleQuat value) =>
double.IsSubnormal(value.X) || double.IsSubnormal(value.Y) || double.IsSubnormal(value.Z) || double.IsSubnormal(value.W);
static doubleQuat INumberBase<doubleQuat>.MaxMagnitude(doubleQuat a, doubleQuat b)
{
var magnitudeA = a.LengthSquared();
var magnitudeB = b.LengthSquared();
if (double.IsNaN(magnitudeA))
return a;
if (double.IsNaN(magnitudeB))
return b;
return magnitudeA >= magnitudeB ? a : b;
}
static doubleQuat INumberBase<doubleQuat>.MaxMagnitudeNumber(doubleQuat a, doubleQuat b)
{
var magnitudeA = a.LengthSquared();
var magnitudeB = b.LengthSquared();
if (double.IsNaN(magnitudeA))
return b;
if (double.IsNaN(magnitudeB))
return a;
return magnitudeA >= magnitudeB ? a : b;
}
static doubleQuat INumberBase<doubleQuat>.MinMagnitude(doubleQuat a, doubleQuat b)
{
var magnitudeA = a.LengthSquared();
var magnitudeB = b.LengthSquared();
if (double.IsNaN(magnitudeA))
return a;
if (double.IsNaN(magnitudeB))
return b;
return magnitudeA <= magnitudeB ? a : b;
}
static doubleQuat INumberBase<doubleQuat>.MinMagnitudeNumber(doubleQuat a, doubleQuat b)
{
var magnitudeA = a.LengthSquared();
var magnitudeB = b.LengthSquared();
if (double.IsNaN(magnitudeA))
return b;
if (double.IsNaN(magnitudeB))
return a;
return magnitudeA <= magnitudeB ? a : b;
}
/* ++/-- add/subtract One = Identity, bumping W - exactly what Complex does. Generic-plane
* only: a footgun on a concrete quat. */
static doubleQuat IIncrementOperators<doubleQuat>.operator ++(doubleQuat value) => value + Identity;
static doubleQuat IDecrementOperators<doubleQuat>.operator --(doubleQuat value) => value - Identity;
static bool INumberBase<doubleQuat>.TryConvertFromChecked<TOther>(TOther value, out doubleQuat result)
{
if (typeof(TOther) == typeof(doubleQuat))
{
result = (doubleQuat)(object)value!;
return true;
}
if (typeof(TOther) == typeof(floatQuat))
{
var other = (floatQuat)(object)value!;
result = new(double.CreateChecked(other.X), double.CreateChecked(other.Y), double.CreateChecked(other.Z), double.CreateChecked(other.W));
return true;
}
if (typeof(TOther) == typeof(sbyte))
{
result = new(0, 0, 0, double.CreateChecked((sbyte)(object)value!));
return true;
}
if (typeof(TOther) == typeof(byte))
{
result = new(0, 0, 0, double.CreateChecked((byte)(object)value!));
return true;
}
if (typeof(TOther) == typeof(short))
{
result = new(0, 0, 0, double.CreateChecked((short)(object)value!));
return true;
}
if (typeof(TOther) == typeof(ushort))
{
result = new(0, 0, 0, double.CreateChecked((ushort)(object)value!));
return true;
}
if (typeof(TOther) == typeof(int))
{
result = new(0, 0, 0, double.CreateChecked((int)(object)value!));
return true;
}
if (typeof(TOther) == typeof(uint))
{
result = new(0, 0, 0, double.CreateChecked((uint)(object)value!));
return true;
}
if (typeof(TOther) == typeof(long))
{
result = new(0, 0, 0, double.CreateChecked((long)(object)value!));
return true;
}
if (typeof(TOther) == typeof(ulong))
{
result = new(0, 0, 0, double.CreateChecked((ulong)(object)value!));
return true;
}
if (typeof(TOther) == typeof(float))
{
result = new(0, 0, 0, double.CreateChecked((float)(object)value!));
return true;
}
if (typeof(TOther) == typeof(double))
{
result = new(0, 0, 0, double.CreateChecked((double)(object)value!));
return true;
}
result = default;
return false;
}
static bool INumberBase<doubleQuat>.TryConvertToChecked<TOther>(doubleQuat value, [MaybeNullWhen(false)] out TOther result)
{
result = default;
return false;
}
static bool INumberBase<doubleQuat>.TryConvertFromSaturating<TOther>(TOther value, out doubleQuat result)
{
if (typeof(TOther) == typeof(doubleQuat))
{
result = (doubleQuat)(object)value!;
return true;
}
if (typeof(TOther) == typeof(floatQuat))
{
var other = (floatQuat)(object)value!;
result = new(double.CreateSaturating(other.X), double.CreateSaturating(other.Y), double.CreateSaturating(other.Z), double.CreateSaturating(other.W));
return true;
}
if (typeof(TOther) == typeof(sbyte))
{
result = new(0, 0, 0, double.CreateSaturating((sbyte)(object)value!));
return true;
}
if (typeof(TOther) == typeof(byte))
{
result = new(0, 0, 0, double.CreateSaturating((byte)(object)value!));
return true;
}
if (typeof(TOther) == typeof(short))
{
result = new(0, 0, 0, double.CreateSaturating((short)(object)value!));
return true;
}
if (typeof(TOther) == typeof(ushort))
{
result = new(0, 0, 0, double.CreateSaturating((ushort)(object)value!));
return true;
}
if (typeof(TOther) == typeof(int))
{
result = new(0, 0, 0, double.CreateSaturating((int)(object)value!));
return true;
}
if (typeof(TOther) == typeof(uint))
{
result = new(0, 0, 0, double.CreateSaturating((uint)(object)value!));
return true;
}
if (typeof(TOther) == typeof(long))
{
result = new(0, 0, 0, double.CreateSaturating((long)(object)value!));
return true;
}
if (typeof(TOther) == typeof(ulong))
{
result = new(0, 0, 0, double.CreateSaturating((ulong)(object)value!));
return true;
}
if (typeof(TOther) == typeof(float))
{
result = new(0, 0, 0, double.CreateSaturating((float)(object)value!));
return true;
}
if (typeof(TOther) == typeof(double))
{
result = new(0, 0, 0, double.CreateSaturating((double)(object)value!));
return true;
}
result = default;
return false;
}
static bool INumberBase<doubleQuat>.TryConvertToSaturating<TOther>(doubleQuat value, [MaybeNullWhen(false)] out TOther result)
{
result = default;
return false;
}
static bool INumberBase<doubleQuat>.TryConvertFromTruncating<TOther>(TOther value, out doubleQuat result)
{
if (typeof(TOther) == typeof(doubleQuat))
{
result = (doubleQuat)(object)value!;
return true;
}
if (typeof(TOther) == typeof(floatQuat))
{
var other = (floatQuat)(object)value!;
result = new(double.CreateTruncating(other.X), double.CreateTruncating(other.Y), double.CreateTruncating(other.Z), double.CreateTruncating(other.W));
return true;
}
if (typeof(TOther) == typeof(sbyte))
{
result = new(0, 0, 0, double.CreateTruncating((sbyte)(object)value!));
return true;
}
if (typeof(TOther) == typeof(byte))
{
result = new(0, 0, 0, double.CreateTruncating((byte)(object)value!));
return true;
}
if (typeof(TOther) == typeof(short))
{
result = new(0, 0, 0, double.CreateTruncating((short)(object)value!));
return true;
}
if (typeof(TOther) == typeof(ushort))
{
result = new(0, 0, 0, double.CreateTruncating((ushort)(object)value!));
return true;
}
if (typeof(TOther) == typeof(int))
{
result = new(0, 0, 0, double.CreateTruncating((int)(object)value!));
return true;
}
if (typeof(TOther) == typeof(uint))
{
result = new(0, 0, 0, double.CreateTruncating((uint)(object)value!));
return true;
}
if (typeof(TOther) == typeof(long))
{
result = new(0, 0, 0, double.CreateTruncating((long)(object)value!));
return true;
}
if (typeof(TOther) == typeof(ulong))
{
result = new(0, 0, 0, double.CreateTruncating((ulong)(object)value!));
return true;
}
if (typeof(TOther) == typeof(float))
{
result = new(0, 0, 0, double.CreateTruncating((float)(object)value!));
return true;
}
if (typeof(TOther) == typeof(double))
{
result = new(0, 0, 0, double.CreateTruncating((double)(object)value!));
return true;
}
result = default;
return false;
}
static bool INumberBase<doubleQuat>.TryConvertToTruncating<TOther>(doubleQuat value, [MaybeNullWhen(false)] out TOther result)
{
result = default;
return false;
}
/// <inheritdoc cref="Quaternion.op_Addition(Quaternion, Quaternion)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat Add(doubleQuat a, doubleQuat b)
{
return a + b;
}
/// <inheritdoc cref="Quaternion.Divide(Quaternion, Quaternion)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat Divide(doubleQuat a, doubleQuat b)
{
return a / b;
}
/// <inheritdoc cref="Quaternion.Multiply(Quaternion, Quaternion)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat Multiply(doubleQuat a, doubleQuat b)
{
return a * b;
}
/// <inheritdoc cref="Quaternion.Negate(Quaternion)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat Negate(doubleQuat value)
{
return -value;
}
/// <inheritdoc cref="Quaternion.Subtract(Quaternion, Quaternion)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat Subtract(doubleQuat a, doubleQuat b)
{
return a - b;
}
/// <inheritdoc cref="Quaternion.op_Addition(Quaternion, Quaternion)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat operator +(doubleQuat a, doubleQuat b)
{
if (!Vector256.IsHardwareAccelerated)
return new(a.X + b.X, a.Y + b.Y, a.Z + b.Z, a.W + b.W);
return Unsafe.BitCast<Vector256<double>, doubleQuat>(Unsafe.BitCast<doubleQuat, Vector256<double>>(a) + Unsafe.BitCast<doubleQuat, Vector256<double>>(b));
}
/// <inheritdoc cref="Quaternion.op_Division(Quaternion, Quaternion)" />
public static doubleQuat operator /(doubleQuat a, doubleQuat b)
{
doubleQuat ans;
var ax = a.X;
var ay = a.Y;
var az = a.Z;
var aw = a.W;
//-------------------------------------
// Inverse part.
var ls = b.X * b.X + b.Y * b.Y + b.Z * b.Z + b.W * b.W;
var invNorm = 1 / ls;
var bx = -b.X * invNorm;
var by = -b.Y * invNorm;
var bz = -b.Z * invNorm;
var bw = b.W * invNorm;
//-------------------------------------
// Multiply part.
// cross(av, bv)
var cx = ay * bz - az * by;
var cy = az * bx - ax * bz;
var cz = ax * by - ay * bx;
var dot = ax * bx + ay * by + az * bz;
ans.X = ax * bw + bx * aw + cx;
ans.Y = ay * bw + by * aw + cy;
ans.Z = az * bw + bz * aw + cz;
ans.W = aw * bw - dot;
return ans;
}
/// <inheritdoc cref="Quaternion.op_Multiply(Quaternion, Quaternion)" />
public static doubleQuat operator *(doubleQuat a, doubleQuat b)
{
// off AVX2: scalar arm, in the kernel's accumulation order
if (!Vector256.IsHardwareAccelerated)
return new doubleQuat(
b.X * a.W + b.W * a.X + b.Z * a.Y - b.Y * a.Z,
b.Y * a.W - b.Z * a.X + b.W * a.Y + b.X * a.Z,
b.Z * a.W + b.Y * a.X - b.X * a.Y + b.W * a.Z,
b.W * a.W - b.X * a.X - b.Y * a.Y - b.Z * a.Z);
var left = Unsafe.BitCast<doubleQuat, Vector256<double>>(a);
var right = Unsafe.BitCast<doubleQuat, Vector256<double>>(b);
var result = right * left.GetElement(3);
result = Vector256.MultiplyAddEstimate(Vector256.ShuffleNative(right, Vector256.Create(3L, 2L, 1L, 0L)) * left.GetElement(0), Vector256.Create(+1.0, -1.0, +1.0, -1.0), result);
result = Vector256.MultiplyAddEstimate(Vector256.ShuffleNative(right, Vector256.Create(2L, 3L, 0L, 1L)) * left.GetElement(1), Vector256.Create(+1.0, +1.0, -1.0, -1.0), result);
result = Vector256.MultiplyAddEstimate(Vector256.ShuffleNative(right, Vector256.Create(1L, 0L, 3L, 2L)) * left.GetElement(2), Vector256.Create(-1.0, +1.0, +1.0, -1.0), result);
return Unsafe.BitCast<Vector256<double>, doubleQuat>(result);
}
/// <inheritdoc cref="Quaternion.op_Subtraction(Quaternion, Quaternion)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat operator -(doubleQuat a, doubleQuat b)
{
if (!Vector256.IsHardwareAccelerated)
return new(a.X - b.X, a.Y - b.Y, a.Z - b.Z, a.W - b.W);
return Unsafe.BitCast<Vector256<double>, doubleQuat>(Unsafe.BitCast<doubleQuat, Vector256<double>>(a) - Unsafe.BitCast<doubleQuat, Vector256<double>>(b));
}
/// <inheritdoc cref="Quaternion.op_UnaryNegation(Quaternion)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat operator -(doubleQuat value)
{
return Zero - value;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat operator +(doubleQuat value)
{
return value;
}
}
// IMultiplyOperators<TSelf, TComponent, TSelf>, IDivisionOperators<TSelf, TComponent, TSelf>
public partial struct doubleQuat
: IMultiplyOperators<doubleQuat, double, doubleQuat>
, IDivisionOperators<doubleQuat, double, doubleQuat>
{
/// <inheritdoc cref="Quaternion.Multiply(Quaternion, float)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat Multiply(doubleQuat a, double b)
{
return a * b;
}
/// <inheritdoc cref="Quaternion.op_Multiply(Quaternion, float)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat operator *(doubleQuat a, double b)
{
if (!Vector256.IsHardwareAccelerated)
return new(a.X * b, a.Y * b, a.Z * b, a.W * b);
return Unsafe.BitCast<Vector256<double>, doubleQuat>(Unsafe.BitCast<doubleQuat, Vector256<double>>(a) * Vector256.Create(b));
}
/// <summary>Divides a quaternion by a scalar, componentwise.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static doubleQuat operator /(doubleQuat a, double b)
{
if (!Vector256.IsHardwareAccelerated)
return new(a.X / b, a.Y / b, a.Z / b, a.W / b);
return Unsafe.BitCast<Vector256<double>, doubleQuat>(Unsafe.BitCast<doubleQuat, Vector256<double>>(a) / Vector256.Create(b));
}
}

View file

@ -0,0 +1,36 @@
// <auto-generated /> - edit Vector.pack.tt to modify
#nullable enable
namespace Brutal.Numerics;
public partial struct doubleQuat
{
public static doubleQuat Unpack(in floatQuat vector, Func<float, double> elementConverter)
{
QuaternionPack.Pack(vector, out doubleQuat result, elementConverter);
return result;
}
public static doubleQuat Unpack(in floatQuat vector, Unpack.Float mode = Numerics.Unpack.Float.Cast)
{
return Unpack(vector, PackUtils.Float.ToDouble(mode));
}
public static void Unpack(
in ReadOnlySpan<floatQuat> source,
Span<doubleQuat> destination,
Func<float, double> elementConverter
)
{
QuaternionPack.Pack(source, destination, elementConverter);
}
public static void Unpack(
in ReadOnlySpan<floatQuat> source,
Span<doubleQuat> destination,
Unpack.Float mode = Numerics.Unpack.Float.Cast
)
{
Unpack(source, destination, PackUtils.Float.ToDouble(mode));
}
}