DirectZ
Loading...
Searching...
No Matches
dz::quat< T > Struct Template Reference

A quaternion class for representing rotations in 3D space. More...

#include <math.hpp>

Inheritance diagram for dz::quat< T >:
dz::vec< T, 4 >

Public Types

using Base = vec<T, 4>

Public Member Functions

 quat ()=default
 Default constructor. Components are uninitialized.
 quat (const quat &)=default
 Copy constructor.
quatoperator= (const quat &)=default
 Copy assignment operator.
 quat (quat &&) noexcept=default
 Move constructor.
quatoperator= (quat &&) noexcept=default
 Move assignment operator.
template<typename... Args>
 quat (Args &&... args)
 Variadic constructor forwarding to vec<T, 4>.
 quat (T w, T x, T y, T z)
 Construct quaternion from explicit components.
quat< Tconjugate () const
 Returns the conjugate of the quaternion.
quat< Tinverse () const
 Returns the inverse of the quaternion.
vec< T, 3 > rotate (const vec< T, 3 > &v) const
 Rotates a 3D vector using this quaternion.
quat< Toperator* (const quat< T > &rhs) const
 Quaternion multiplication.
void to_axis_angle (vec< T, 3 > &out_axis, T &out_angle) const
 Converts the quaternion to axis-angle representation.
mat< T, 4, 4 > to_mat4 () const
 Converts the quaternion to a 4x4 rotation matrix.
vecoperator= (const vec &other)
 Assignment operator for component-wise copy.
vecoperator+= (const O &other)
 Compound addition-assignment operator.
vecoperator-= (const O &other)
 Compound subtraction-assignment operator.
vecoperator*= (const O &other)
 Compound multiplication-assignment operator.
vecoperator/= (const O &other)
 Compound division-assignment operator.
vec operator+ (const O &other) const
 Addition operator returning a new vector.
vec operator- (const O &other) const
 Subtraction operator returning a new vector.
vec operator- () const
 Unary negation operator.
vec operator* (const O &other) const
 Multiplication operator returning a new vector.
vec operator/ (const O &other) const
 Division operator returning a new vector.
Toperator[] (size_t i)
 Index operator for non-const access to vector components.
const Toperator[] (size_t i) const
 Index operator for const access to vector components.
T length () const
 Computes the Euclidean length (magnitude) of the vector.
vec normalize () const
 Returns a normalized (unit length) copy of the vector.
Public Member Functions inherited from dz::vec< T, 4 >
 vec (Args... args)
 Variadic constructor to initialize vector components.
 vec (const vec< OT, ON > &other)
 Copy constructor to convert from vectors of different types and/or sizes.
vecoperator= (const vec &other)
 Assignment operator for component-wise copy.
Toperator[] (size_t i)
 Index operator for non-const access to vector components.
const Toperator[] (size_t i) const
 Index operator for const access to vector components.
vecoperator*= (const O &other)
 Compound multiplication-assignment operator.
vecoperator/= (const O &other)
 Compound division-assignment operator.
vecoperator+= (const O &other)
 Compound addition-assignment operator.
vecoperator-= (const O &other)
 Compound subtraction-assignment operator.
vec operator* (const O &other) const
 Multiplication operator returning a new vector.
vec operator/ (const O &other) const
 Division operator returning a new vector.
vec operator+ (const O &other) const
 Addition operator returning a new vector.
vec operator- (const O &other) const
 Subtraction operator returning a new vector.
vec operator- () const
 Unary negation operator.
T length () const
 Computes the Euclidean length (magnitude) of the vector.
vec normalize () const
 Returns a normalized (unit length) copy of the vector.

Static Public Member Functions

static quat< Tfrom_axis_angle (const vec< T, 3 > &axis, T angle)
 Constructs a quaternion from an axis-angle representation.

Public Attributes

T data [N]
 Underlying array storing vector components.
Public Attributes inherited from dz::vec< T, 4 >
T data [N]
 Underlying array storing vector components.

Detailed Description

template<typename T>
struct dz::quat< T >

A quaternion class for representing rotations in 3D space.

This struct inherits from vec<T, 4> and represents a quaternion with four components: w, x, y, z. It provides operations including construction, conjugation, inversion, quaternion multiplication, and rotation of a 3D vector.

Template Parameters
TUnderlying scalar type (e.g., float, double).

Member Typedef Documentation

◆ Base

template<typename T>
using dz::quat< T >::Base = vec<T, 4>

Constructor & Destructor Documentation

◆ quat() [1/5]

template<typename T>
dz::quat< T >::quat ( )
default

Default constructor. Components are uninitialized.

◆ quat() [2/5]

template<typename T>
dz::quat< T >::quat ( const quat< T > & )
default

Copy constructor.

Parameters
otherThe quaternion to copy.

◆ quat() [3/5]

template<typename T>
dz::quat< T >::quat ( quat< T > && )
defaultnoexcept

Move constructor.

Parameters
otherThe quaternion to move.

◆ quat() [4/5]

template<typename T>
template<typename... Args>
dz::quat< T >::quat ( Args &&... args)
inline

Variadic constructor forwarding to vec<T, 4>.

Template Parameters
ArgsVariadic template arguments.
Parameters
argsValues forwarded to base class constructor.

◆ quat() [5/5]

template<typename T>
dz::quat< T >::quat ( T w,
T x,
T y,
T z )
inline

Construct quaternion from explicit components.

Parameters
wScalar component.
xX vector component.
yY vector component.
zZ vector component.

Member Function Documentation

◆ conjugate()

template<typename T>
quat< T > dz::quat< T >::conjugate ( ) const
inline

Returns the conjugate of the quaternion.

The conjugate of (w, x, y, z) is (w, -x, -y, -z).

Returns
Conjugated quaternion.

◆ from_axis_angle()

template<typename T>
quat< T > dz::quat< T >::from_axis_angle ( const vec< T, 3 > & axis,
T angle )
inlinestatic

Constructs a quaternion from an axis-angle representation.

Parameters
axisThe normalized axis of rotation.
angleThe angle of rotation in radians.
Returns
Quaternion representing the rotation.

◆ inverse()

template<typename T>
quat< T > dz::quat< T >::inverse ( ) const
inline

Returns the inverse of the quaternion.

The inverse is the conjugate divided by the squared norm.

Returns
Inverse quaternion.

◆ length()

template<typename T>
T dz::vec< T, N >::length ( ) const
inline

Computes the Euclidean length (magnitude) of the vector.

Calculates the square root of the sum of squares of all components.

Returns
The length of the vector as type T.

◆ normalize()

template<typename T>
vec dz::vec< T, N >::normalize ( ) const
inline

Returns a normalized (unit length) copy of the vector.

Divides all components by the vector length.

Returns
A new vector normalized to length 1.

◆ operator*() [1/2]

template<typename T>
vec dz::vec< T, N >::operator* ( const O & other) const
inline

Multiplication operator returning a new vector.

Supports multiplication by a scalar or component-wise multiplication by another vector.

Template Parameters
OOperand type (scalar T or vec<T, N>).
Parameters
otherThe scalar or vector to multiply by.
Returns
A new vector resulting from the multiplication.

◆ operator*() [2/2]

template<typename T>
quat< T > dz::quat< T >::operator* ( const quat< T > & rhs) const
inline

Quaternion multiplication.

Multiplies this quaternion by another.

Parameters
rhsThe right-hand side quaternion.
Returns
Result of multiplication.

◆ operator*=()

template<typename T>
vec & dz::vec< T, N >::operator*= ( const O & other)
inline

Compound multiplication-assignment operator.

Supports multiplication by a scalar or component-wise multiplication by another vector. Throws std::runtime_error if the operand type is unsupported.

Template Parameters
OOperand type (scalar T or vec<T, N>).
Parameters
otherThe scalar or vector to multiply by.
Returns
Reference to this vector after multiplication.
Exceptions
std::runtime_errorif unsupported operand type is used.

◆ operator+()

template<typename T>
vec dz::vec< T, N >::operator+ ( const O & other) const
inline

Addition operator returning a new vector.

Supports addition by a scalar or component-wise addition by another vector.

Template Parameters
OOperand type (scalar T or vec<T, N>).
Parameters
otherThe scalar or vector to add.
Returns
A new vector resulting from the addition.

◆ operator+=()

template<typename T>
vec & dz::vec< T, N >::operator+= ( const O & other)
inline

Compound addition-assignment operator.

Supports addition by a scalar or component-wise addition by another vector. Throws std::runtime_error if the operand type is unsupported.

Template Parameters
OOperand type (scalar T or vec<T, N>).
Parameters
otherThe scalar or vector to add.
Returns
Reference to this vector after addition.
Exceptions
std::runtime_errorif unsupported operand type is used.

◆ operator-() [1/2]

template<typename T>
vec dz::vec< T, N >::operator- ( ) const
inline

Unary negation operator.

Returns a new vector with all components negated.

Returns
A new vector with negated components.

◆ operator-() [2/2]

template<typename T>
vec dz::vec< T, N >::operator- ( const O & other) const
inline

Subtraction operator returning a new vector.

Supports subtraction by a scalar or component-wise subtraction by another vector.

Template Parameters
OOperand type (scalar T or vec<T, N>).
Parameters
otherThe scalar or vector to subtract.
Returns
A new vector resulting from the subtraction.

◆ operator-=()

template<typename T>
vec & dz::vec< T, N >::operator-= ( const O & other)
inline

Compound subtraction-assignment operator.

Supports subtraction by a scalar or component-wise subtraction by another vector. Throws std::runtime_error if the operand type is unsupported.

Template Parameters
OOperand type (scalar T or vec<T, N>).
Parameters
otherThe scalar or vector to subtract.
Returns
Reference to this vector after subtraction.
Exceptions
std::runtime_errorif unsupported operand type is used.

◆ operator/()

template<typename T>
vec dz::vec< T, N >::operator/ ( const O & other) const
inline

Division operator returning a new vector.

Supports division by a scalar or component-wise division by another vector.

Template Parameters
OOperand type (scalar T or vec<T, N>).
Parameters
otherThe scalar or vector to divide by.
Returns
A new vector resulting from the division.

◆ operator/=()

template<typename T>
vec & dz::vec< T, N >::operator/= ( const O & other)
inline

Compound division-assignment operator.

Supports division by a scalar or component-wise division by another vector. Throws std::runtime_error if the operand type is unsupported.

Template Parameters
OOperand type (scalar T or vec<T, N>).
Parameters
otherThe scalar or vector to divide by.
Returns
Reference to this vector after division.
Exceptions
std::runtime_errorif unsupported operand type is used.

◆ operator=() [1/3]

template<typename T>
quat & dz::quat< T >::operator= ( const quat< T > & )
default

Copy assignment operator.

Parameters
otherThe quaternion to assign from.
Returns
Reference to this quaternion.

◆ operator=() [2/3]

template<typename T>
vec & dz::vec< T, N >::operator= ( const vec & other)
inline

Assignment operator for component-wise copy.

Copies all components from the other vector using memcpy for efficiency.

Parameters
otherThe vector to copy from.
Returns
Reference to this vector after assignment.

◆ operator=() [3/3]

template<typename T>
quat & dz::quat< T >::operator= ( quat< T > && )
defaultnoexcept

Move assignment operator.

Parameters
otherThe quaternion to move from.
Returns
Reference to this quaternion.

◆ operator[]() [1/2]

template<typename T>
T & dz::vec< T, N >::operator[] ( size_t i)
inline

Index operator for non-const access to vector components.

No bounds checking is performed.

Parameters
iThe component index to access.
Returns
Reference to the component at index i.

◆ operator[]() [2/2]

template<typename T>
const T & dz::vec< T, N >::operator[] ( size_t i) const
inline

Index operator for const access to vector components.

No bounds checking is performed.

Parameters
iThe component index to access.
Returns
Const reference to the component at index i.

◆ rotate()

template<typename T>
vec< T, 3 > dz::quat< T >::rotate ( const vec< T, 3 > & v) const
inline

Rotates a 3D vector using this quaternion.

Parameters
vThe 3D vector to rotate.
Returns
Rotated vector.

◆ to_axis_angle()

template<typename T>
void dz::quat< T >::to_axis_angle ( vec< T, 3 > & out_axis,
T & out_angle ) const
inline

Converts the quaternion to axis-angle representation.

Parameters
out_axisNormalized axis of rotation (output).
out_angleAngle in radians (output).

◆ to_mat4()

template<typename T>
mat< T, 4, 4 > dz::quat< T >::to_mat4 ( ) const
inline

Converts the quaternion to a 4x4 rotation matrix.

Returns
4x4 rotation matrix.

Member Data Documentation

◆ data

template<typename T>
T dz::vec< T, N >::data[N]

Underlying array storing vector components.


The documentation for this struct was generated from the following file: