// ReSharper disable InconsistentNaming
namespace Brutal.Numerics;
/// Defines a generic quaternion type.
/// The type that implements the interface.
/// The component type.
/* A quaternion IS a component tuple - four lanes of T, indexable, spannable, constructible from a
* span - and used to restate ComponentSize, ComponentCount, the indexer, Create, AsSpan, IEquatable
* and IFormattable verbatim rather than inherit them.
*
* It reaches the function axis the same way, by inheriting rather than redeclaring: a quaternion
* has a dot product and a magnitude, which is exactly why those interfaces carry no family in their
* names. It has NO IDistance, and that omission is the point - Euclidean distance is not the metric
* on rotations. */
public interface IQuaternion : IComponentTuple, IDotProduct, IMagnitude
where TSelf : IQuaternion
{
///
static abstract TSelf Zero { get; }
///
static abstract TSelf Identity { get; }
///
bool IsIdentity { get; }
/* The static algebra surface: exposes the statics that already exist on every quaternion
* type, so generic code can call them through the interface (satisfied implicitly).
* Cross-type members (CreateFromAxisAngle and friends) live on IQuaternionFactories below,
* which is where the axis and matrix type parameters they need are paid for. */
///
static abstract TSelf Add(TSelf left, TSelf right);
///
static abstract TSelf Subtract(TSelf left, TSelf right);
///
static abstract TSelf Multiply(TSelf left, TSelf right);
///
static abstract TSelf Multiply(TSelf left, T right);
///
static abstract TSelf Negate(TSelf value);
///
static abstract TSelf Concatenate(TSelf first, TSelf second);
///
static abstract TSelf Conjugate(TSelf value);
///
static abstract TSelf Inverse(TSelf value);
///
static abstract TSelf Normalize(TSelf value);
///
/// Returns a zero quaternion if the input quaternion is zero
static abstract TSelf NormalizeOrZero(TSelf value);
///
static abstract TSelf Lerp(TSelf a, TSelf b, T t);
///
static abstract TSelf Slerp(TSelf a, TSelf b, T t);
}
internal class QuaternionDebugView(IQuaternion q)
where TSelf : IQuaternion
{
public T X => q[0];
public T Y => q[1];
public T Z => q[2];
public T W => q[3];
public override string? ToString()
{
return q.ToString();
}
}
/// The named ways of constructing a rotation.
/// The type that implements the interface.
/// The component type.
/// The axis type of .
/// The matrix type of .
/* One interface for the three factories rather than one per verb: they are quaternion-only, so by
* family they belong on IQuaternion itself - but they drag in two more type parameters, and making
* every caller that just wants a quaternion name four types is the wrong trade. So they group by
* CONCERN, construction, and the extra parameters are paid only by whoever wants them.
*
* Its one consumer is the quaternion differential suite, which is also why the three interfaces it
* replaces existed - so a suite could reach the members generically. That remains the open question
* about whether "so a test can reach it" justifies an interface; grouping them at least means the
* answer costs one name rather than three. */
public interface IQuaternionFactories
where TSelf : IQuaternionFactories
{
///
static abstract TSelf CreateFromAxisAngle(TVector3 axis, T angle);
///
static abstract TSelf CreateFromYawPitchRoll(T yaw, T pitch, T roll);
///
static abstract TSelf CreateFromRotationMatrix(TMatrix4x4 matrix);
}