Arduino Helpers master
Utility library for Arduino
Public Member Functions | Static Public Member Functions | Public Attributes | Related Functions | List of all members
Quaternion Struct Reference

#include <AH/Math/Quaternion.hpp>

Detailed Description

Type for quaternions of floating point numbers.

Quaternions can be multiplied (Hamiltonian product), normalized and can perform rotations of vectors. Quaternion also has an implementation of the following operators:

Examples
Quaternion.ino.

Definition at line 57 of file Quaternion.hpp.

+ Collaboration diagram for Quaternion:

Public Member Functions

 Quaternion ()=default
 Create a quaternion that is initialized to the identity quaternion. More...
 
 Quaternion (float w, float x, float y, float z)
 Create a quaterion with the given values for w, x, y and z. More...
 
Quaternionoperator+= (Quaternion rhs)
 Sum of two quaterions uses quaternion multiplication. More...
 
Quaternion operator+ (Quaternion rhs) const
 Sum of two quaternions uses quaternion multiplication. More...
 
Quaternion conjugated () const
 Complex conjugate (doesn't change the original quaternion). More...
 
Quaternion operator- () const
 Negated quaternion is its conjugate. More...
 
Quaternionoperator-= (Quaternion rhs)
 Difference of two quaternions a and b is the quaternion multiplication of a and the conjugate of b. More...
 
Quaternion operator- (Quaternion rhs) const
 Difference of two quaternions a and b is the quaternion multiplication of a and the conjugate of b. More...
 
Quaternionoperator*= (float rhs)
 Scalar multiplication. More...
 
Quaternion operator* (float rhs) const
 Scalar multiplication. More...
 
Quaternionoperator/= (float rhs)
 Scalar division. More...
 
Quaternion operator/ (float rhs) const
 Scalar division. More...
 
float normSquared () const
 Norm squared. More...
 
float norm () const
 Norm. More...
 
Quaternionnormalize ()
 Normalize this quaternion. More...
 
Quaternion normalized () const
 Normalize a copy of this quaternion (doesn't change the original quaternion). More...
 
Vec3f rotate (Vec3f v) const
 Rotate vector by this quaternion. More...
 
bool operator== (Quaternion rhs) const
 Equality check. More...
 
bool operator!= (Quaternion rhs) const
 Inequality check. More...
 

Static Public Member Functions

static Quaternion identity ()
 Identity quaternion (1,0,0,0). More...
 
static Quaternion fromDirection (Vec3f v)
 Calculate the quaternion that satisfies the following: result.rotate(Vec3f{0, 0, 1}) == v.normalized(). More...
 
static Quaternion fromXYAngle (float xAngle, float yAngle)
 Calculate the quaternion from a vector that makes a given angle with the XZ plane and a given angle with the YZ plane. More...
 
static Quaternion hamiltonianProduct (Quaternion q, Quaternion r)
 Quaternion multiplication. More...
 

Public Attributes

float w = 1.0
 Scalar (real) component. More...
 
float x = 0.0
 First vector (imaginary) component \( \mathbf{i} \). More...
 
float y = 0.0
 Second vector (imaginary) component \( \mathbf{j} \). More...
 
float z = 0.0
 Third vector (imaginary) component \( \mathbf{k} \). More...
 

Related Functions

(Note that these are not member functions.)

Quaternion operator* (float lhs, Quaternion rhs)
 Scalar multiplication. More...
 
Print & operator<< (Print &os, Quaternion e)
 Printing. More...
 

Constructor & Destructor Documentation

◆ Quaternion() [1/2]

Quaternion ( )
default

Create a quaternion that is initialized to the identity quaternion.

◆ Quaternion() [2/2]

Quaternion ( float  w,
float  x,
float  y,
float  z 
)
inline

Create a quaterion with the given values for w, x, y and z.

Definition at line 66 of file Quaternion.hpp.

Member Function Documentation

◆ operator+=()

Quaternion & operator+= ( Quaternion  rhs)
inline

Sum of two quaterions uses quaternion multiplication.

(Composition of the two rotations.)

Definition at line 70 of file Quaternion.hpp.

◆ operator+()

Quaternion operator+ ( Quaternion  rhs) const
inline

Sum of two quaternions uses quaternion multiplication.

(Composition of the two rotations.)

Definition at line 75 of file Quaternion.hpp.

◆ conjugated()

Quaternion conjugated ( ) const
inline

Complex conjugate (doesn't change the original quaternion).

Definition at line 80 of file Quaternion.hpp.

◆ operator-() [1/2]

Quaternion operator- ( ) const
inline

Negated quaternion is its conjugate.

Definition at line 82 of file Quaternion.hpp.

◆ operator-=()

Quaternion & operator-= ( Quaternion  rhs)
inline

Difference of two quaternions a and b is the quaternion multiplication of a and the conjugate of b.

(Composition of the rotation of a and the inverse rotation of b.)

Definition at line 87 of file Quaternion.hpp.

◆ operator-() [2/2]

Quaternion operator- ( Quaternion  rhs) const
inline

Difference of two quaternions a and b is the quaternion multiplication of a and the conjugate of b.

(Composition of the rotation of a and the inverse rotation of b.)

Definition at line 91 of file Quaternion.hpp.

◆ operator*=()

Quaternion & operator*= ( float  rhs)
inline

Scalar multiplication.

Definition at line 98 of file Quaternion.hpp.

◆ operator*()

Quaternion operator* ( float  rhs) const
inline

Scalar multiplication.

Definition at line 106 of file Quaternion.hpp.

◆ operator/=()

Quaternion & operator/= ( float  rhs)
inline

Scalar division.

Definition at line 113 of file Quaternion.hpp.

◆ operator/()

Quaternion operator/ ( float  rhs) const
inline

Scalar division.

Definition at line 121 of file Quaternion.hpp.

◆ normSquared()

float normSquared ( ) const
inline

Norm squared.

Definition at line 128 of file Quaternion.hpp.

◆ norm()

float norm ( ) const
inline

Norm.

Definition at line 130 of file Quaternion.hpp.

◆ normalize()

Quaternion & normalize ( )
inline

Normalize this quaternion.

Definition at line 132 of file Quaternion.hpp.

◆ normalized()

Quaternion normalized ( ) const
inline

Normalize a copy of this quaternion (doesn't change the original quaternion).

Definition at line 135 of file Quaternion.hpp.

◆ rotate()

Vec3f rotate ( Vec3f  v) const
inline

Rotate vector by this quaternion.

This function uses the normalized version of this quaternion.

Note
This function is not the same as quatrotate in MATLAB! MATLAB rotates by the conjugate of the quaternion, while this function rotates by the quaternion itself.
Examples
Quaternion.ino.

Definition at line 146 of file Quaternion.hpp.

◆ operator==()

bool operator== ( Quaternion  rhs) const
inline

Equality check.

Definition at line 176 of file Quaternion.hpp.

◆ operator!=()

bool operator!= ( Quaternion  rhs) const
inline

Inequality check.

Definition at line 181 of file Quaternion.hpp.

◆ identity()

static Quaternion identity ( )
inlinestatic

Identity quaternion (1,0,0,0).

Definition at line 184 of file Quaternion.hpp.

◆ fromDirection()

static Quaternion fromDirection ( Vec3f  v)
inlinestatic

Calculate the quaternion that satisfies the following: result.rotate(Vec3f{0, 0, 1}) == v.normalized().

Examples
Quaternion.ino.

Definition at line 190 of file Quaternion.hpp.

◆ fromXYAngle()

static Quaternion fromXYAngle ( float  xAngle,
float  yAngle 
)
inlinestatic

Calculate the quaternion from a vector that makes a given angle with the XZ plane and a given angle with the YZ plane.

Parameters
xAngleThe angle the vector should make with the XZ plane. A positive value represents a positive rotation about the x-axis.
yAngleThe angle the vector should make with the YZ plane. A positive value represents a positive rotation about the y-axis.
Returns
A quaternion from the vector {tan(yAngle), -tan(xAngle), 1}.

Definition at line 253 of file Quaternion.hpp.

◆ hamiltonianProduct()

static Quaternion hamiltonianProduct ( Quaternion  q,
Quaternion  r 
)
inlinestatic

Quaternion multiplication.

Definition at line 263 of file Quaternion.hpp.

Member Data Documentation

◆ w

float w = 1.0

Scalar (real) component.

Examples
Quaternion.ino.

Definition at line 58 of file Quaternion.hpp.

◆ x

float x = 0.0

First vector (imaginary) component \( \mathbf{i} \).

Examples
Quaternion.ino.

Definition at line 59 of file Quaternion.hpp.

◆ y

float y = 0.0

Second vector (imaginary) component \( \mathbf{j} \).

Examples
Quaternion.ino.

Definition at line 60 of file Quaternion.hpp.

◆ z

float z = 0.0

Third vector (imaginary) component \( \mathbf{k} \).

Examples
Quaternion.ino.

Definition at line 61 of file Quaternion.hpp.


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