# Quat¶

```#include <Imath/ImathQuat.h>
```

The `Quat` class template represents a quaterion rotation/orientation, with predefined typedefs for `float` and `double`.

Example:

```#include <Imath/ImathQuat.h>
#include <cassert>

void
quat_example ()
{
Imath::Quatf q (2.0f, 3.0f, 4.0f, 5.0f);
assert (q.r == 2.0f && q.v == Imath::V3f (3.0f, 4.0f, 5.0f));

Imath::Quatf r (1.0f, 0.0f, 0.0f, 1.0f);
assert (r.inverse () == Imath::Quatf (0.5f, 0.0f, 0.0f, -0.5f));
}
```
typedef Quat<float> Imath::Quatf

Quaternion of type float.

template<class T>
class Quat

The Quat class implements the quaternion numerical type &#8212; you will probably want to use this class to represent orientations in R3 and to convert between various euler angle reps.

You should probably use Imath::Euler<> for that.

T r

The real part.

Vec3<T> v

The imaginary vector.

Constructors

inline constexpr Quat() noexcept

Default constructor is the identity quat.

inline constexpr Quat(const Quat &q) noexcept

Copy constructor.

template<class S>
inline constexpr Quat(const Quat<S> &q) noexcept

Construct from a quaternion of a another base type.

inline constexpr Quat(T s, T i, T j, T k) noexcept

Initialize with real part `s` and imaginary vector 1(i,j,k)`.

inline constexpr Quat(T s, Vec3<T> d) noexcept

Initialize with real part `s` and imaginary vector `d`

inline constexpr const Quat<T> &operator=(const Quat<T> &q) noexcept

Assignment.

~Quat() noexcept = default

Destructor.

static inline constexpr Quat<T> identity() noexcept

The identity quaternion.

Basic Algebra

Note that the operator return values are NOT normalized

inline constexpr const Quat<T> &operator*=(const Quat<T> &q) noexcept

Quaternion multiplication.

inline constexpr const Quat<T> &operator*=(T t) noexcept

Scalar multiplication: multiply both real and imaginary parts by the given scalar.

inline constexpr const Quat<T> &operator/=(const Quat<T> &q) noexcept

Quaterion division, using the inverse()

inline constexpr const Quat<T> &operator/=(T t) noexcept

Scalar division: multiply both real and imaginary parts by the given scalar.

inline constexpr const Quat<T> &operator+=(const Quat<T> &q) noexcept

inline constexpr const Quat<T> &operator-=(const Quat<T> &q) noexcept

Quaternion subtraction.

template<class S>
inline constexpr bool operator==(const Quat<S> &q) const noexcept

Equality.

template<class S>
inline constexpr bool operator!=(const Quat<S> &q) const noexcept

Inequality.

Query

inline constexpr T length() const noexcept

Return the R4 length.

inline constexpr T angle() const noexcept

Return the angle of the axis/angle representation.

inline constexpr Vec3<T> axis() const noexcept

Return the axis of the axis/angle representation.

inline constexpr Matrix33<T> toMatrix33() const noexcept

Return a 3x3 rotation matrix.

inline constexpr Matrix44<T> toMatrix44() const noexcept

Return a 4x4 rotation matrix.

inline Quat<T> log() const noexcept

Return the logarithm of the quaterion.

inline Quat<T> exp() const noexcept

Return the exponent of the quaterion.

Utility Methods

inline constexpr Quat<T> &invert() noexcept

Invert in place: this = 1 / this.

Returns:

const reference to this.

inline constexpr Quat<T> inverse() const noexcept

Return 1/this, leaving this unchanged.

inline constexpr Quat<T> &normalize() noexcept

Normalize in place.

Returns:

const reference to this.

inline constexpr Quat<T> normalized() const noexcept

Return a normalized quaternion, leaving this unmodified.

inline constexpr Vec3<T> rotateVector(const Vec3<T> &original) const noexcept

Rotate the given point by the quaterion.

inline constexpr T euclideanInnerProduct(const Quat<T> &q) const noexcept

Return the Euclidean inner product.

inline constexpr Quat<T> &setAxisAngle(const Vec3<T> &axis, T radians) noexcept

Set the quaterion to be a rotation around the given axis by the given angle.

Returns:

const reference to this.

inline constexpr Quat<T> &setRotation(const Vec3<T> &fromDirection, const Vec3<T> &toDirection) noexcept

Set the quaternion to be a rotation that transforms the direction vector `fromDirection` to `toDirection`

Returns:

const reference to this.

Public Types

typedef T BaseType

The base type: In templates that accept a parameter `V`, you can refer to `T` as `V::BaseType`

Public Functions

inline constexpr T &operator[](int index) noexcept

Element access: q[0] is the real part, (q[1],q[2],q[3]) is the imaginary part.

inline constexpr T operator[](int index) const noexcept

Element access: q[0] is the real part, (q[1],q[2],q[3]) is the imaginary part.

template<class T>
std::ostream &Imath::operator<<(std::ostream &o, const Quat<T> &q)

Stream output as “(r x y z)”.