1#ifndef PHYTH_QUATERNION_HPP
2#define PHYTH_QUATERNION_HPP
35 "Quaternion only supports Quantity types");
144 return w *
w +
x *
x +
y *
y +
z *
z;
224 using R =
decltype(
w *
other.w);
341 return !(*
this ==
other);
350 template<
typename UnitT>
439 os <<
"(" <<
q.w <<
" + " <<
q.x <<
"i + " <<
q.y <<
"j + " <<
q.z <<
"k)";
470 template<
typename T,
typename U>
Scalar cos(const Scalar q)
Cosine of a dimensionless quantity.
auto sqrt(Quantity< UnitT > q)
Square root of a quantity.
auto abs(Quantity< UnitT > q)
Absolute value of a quantity.
Quantity< UnitT > clamp(Quantity< UnitT > x, Quantity< UnitT > min, Quantity< UnitT > max)
Clamp a quantity between min and max bounds.
Scalar acos(const Scalar q)
Arc cosine (inverse cosine)
Scalar sin(const Scalar q)
Sine of a dimensionless quantity.
Quantity< ScalarUnit > Scalar
constexpr auto operator*(Quantity< U1 > a, Quantity< U2 > b)
Multiply two quantities.
constexpr auto is_quaternion_v
Convenience variable template for is_quaternion.
Quaternion with Quantity components.
constexpr auto operator-() const
Unary negation.
constexpr Quaternion & operator/=(U scalar)
Scalar division assignment.
constexpr Quaternion(const Vector3< T > &v_)
Construct from vector part only (scalar part = 0)
friend std::ostream & operator<<(std::ostream &os, const Quaternion &q)
Stream insertion operator for Quaternion.
Quaternion< Quantity< UnitT > > as() const
Convert all components to a different unit of the same dimension.
constexpr auto operator-(const Quaternion< U > &other) const
Quaternion subtraction.
constexpr bool operator==(const Quaternion< U > &other) const
Equality comparison.
static auto Slerp(const Quaternion &q1, const Quaternion< U > &q2, const Scalar t)
Spherical linear interpolation between two quaternions.
constexpr Quaternion(T w_, const Vector3< T > &v_)
Construct from scalar and vector parts.
constexpr bool operator!=(const Quaternion< U > &other) const
Inequality comparison.
auto LengthSquared() const
Squared magnitude.
constexpr auto operator*(Quantity< U > scalar) const
Scalar multiplication.
constexpr Quaternion Conjugated() const
Conjugate: (w + xi + yj + zk) -> (w - xi - yj - zk)
bool IsUnit() const
Check if the quaternion is a unit quaternion.
constexpr const T & operator[](int i) const
Index-based access (const)
constexpr auto operator*(const Quaternion< U > &other) const
Quaternion multiplication.
constexpr Quaternion(T w_, T x_, T y_, T z_)
Construct from scalar and vector components.
constexpr Vector3< T > ToVector() const
Extract the vector part as a Vector3.
constexpr Quaternion & operator*=(U scalar)
Scalar multiplication assignment.
constexpr Quaternion & operator+=(const Quaternion< U > &other)
Addition assignment.
auto Length() const
Magnitude (sqrt of LengthSquared)
constexpr auto operator+(const Quaternion< U > &other) const
Quaternion addition.
static Quaternion FromAxisAngle(const Vector3< T > &axis, const Scalar theta)
Create a unit quaternion from axis and angle.
constexpr auto Dot(const Quaternion< U > &other) const
Dot product of two quaternions.
static Quaternion FromEuler(const Scalar yaw, const Scalar pitch, const Scalar roll)
Create a unit quaternion from Euler angles (ZYX convention)
constexpr T & operator[](int i)
Index-based access (non-const)
constexpr T ToQuantity() const
Extract the scalar part.
auto Rotated(const Vector3< T > &v) const
Rotate a vector by this quaternion.
auto Normalized() const
Normalized quaternion (unit quaternion)
constexpr auto operator/(U scalar) const
Scalar division.
constexpr Quaternion()
Default constructor - identity quaternion (1 + 0i + 0j + 0k)
auto Inversed() const
Inverse: q^(-1) = conjugate / |q|^2.
constexpr Quaternion & operator-=(const Quaternion< U > &other)
Subtraction assignment.
3D vector with Quantity components
Trait: check if a type is a Quaternion instantiation.