27#include <AH/STL/cmath>
28#include <AH/STL/limits>
66 Quaternion(
float w,
float x,
float y,
float z) : w(w), x(x), y(y), z(z) {}
71 return *
this = hamiltonianProduct(*
this,
rhs);
76 return hamiltonianProduct(*
this,
rhs);
128 float normSquared()
const {
return w * w + x * x + y * y + z * z; }
130 float norm()
const {
return std::sqrt(normSquared()); }
158 float M11 = 1 - 2 *
q.y *
q.y - 2 *
q.z *
q.z;
159 float M12 = 2 * (
q.x *
q.y -
q.w *
q.z);
160 float M13 = 2 * (
q.x *
q.z +
q.w *
q.y);
161 float M21 = 2 * (
q.x *
q.y +
q.w *
q.z);
162 float M22 = 1 - 2 *
q.x *
q.x - 2 *
q.z *
q.z;
163 float M23 = 2 * (
q.y *
q.z -
q.w *
q.x);
164 float M31 = 2 * (
q.x *
q.z -
q.w *
q.y);
165 float M32 = 2 * (
q.y *
q.z +
q.w *
q.x);
166 float M33 = 1 - 2 *
q.x *
q.x - 2 *
q.y *
q.y;
177 return this->w ==
rhs.w && this->x ==
rhs.x && this->y ==
rhs.y &&
205 float eps = std::numeric_limits<float>::epsilon();
217 if (std::abs(x) <=
eps && std::abs(y) <=
eps)
259 return Quaternion::fromDirection(
v);
265 r.
w *
q.w - r.
x *
q.x - r.
y *
q.y - r.
z *
q.z,
266 r.
w *
q.x + r.
x *
q.w - r.
y *
q.z + r.
z *
q.y,
267 r.
w *
q.y + r.
x *
q.z + r.
y *
q.w - r.
z *
q.x,
268 r.
w *
q.z - r.
x *
q.y + r.
y *
q.x + r.
z *
q.w,
297 : yaw(yaw), pitch(pitch), roll(roll) {}
306 return this->yaw ==
rhs.yaw && this->pitch ==
rhs.pitch &&
307 this->roll ==
rhs.roll;
319 float phi = std::atan2(2 * (
q.w *
q.x +
q.y *
q.z),
320 1 - 2 * (
q.x *
q.x +
q.y *
q.y));
321 float theta = std::asin(2 * (
q.w *
q.y -
q.z *
q.x));
322 float psi = std::atan2(2 * (
q.w *
q.z +
q.x *
q.y),
323 1 - 2 * (
q.y *
q.y +
q.z *
q.z));
351std::ostream &
operator<<(std::ostream &os, Quaternion q);
355std::ostream &
operator<<(std::ostream &os, EulerAngles e);
#define BEGIN_AH_NAMESPACE
Print & operator<<(Print &os, Cable c)
Conversions between radians and degrees.
Definition of Vec2f and Vec3f.
A class for serial-in/parallel-out shift registers, like the 74HC595 that are connected to the SPI bu...
Quaternion operator*(float lhs, Quaternion rhs)
Scalar multiplication.
Struct for Euler angles of floating point numbers.
bool operator==(EulerAngles rhs) const
Equality check.
EulerAngles(Quaternion q)
Create Euler angles from the given quaternion.
static EulerAngles quat2eul(Quaternion q)
Convert the given quaternion to Euler angles.
bool operator!=(EulerAngles rhs) const
Inequality check.
EulerAngles(float yaw, float pitch, float roll)
Create Euler angles with the given values for yaw, pitch and roll.
EulerAngles()=default
Create Euler angles that are initialized to (0 0 0), or upright.
static Quaternion eul2quat(EulerAngles eulerAngles)
Convert the given Euler angles to a quaternion.
Type for quaternions of floating point numbers.
Quaternion & operator+=(Quaternion rhs)
Sum of two quaterions uses quaternion multiplication.
static Quaternion fromDirection(Vec3f v)
Calculate the quaternion that satisfies the following: result.rotate(Vec3f{0, 0, 1}) == v....
Quaternion & operator/=(float rhs)
Scalar division.
Quaternion operator-() const
Negated quaternion is its conjugate.
float w
Scalar (real) component.
static Quaternion hamiltonianProduct(Quaternion q, Quaternion r)
Quaternion multiplication.
float normSquared() const
Norm squared.
bool operator==(Quaternion rhs) const
Equality check.
Quaternion(float w, float x, float y, float z)
Create a quaterion with the given values for w, x, y and z.
bool operator!=(Quaternion rhs) const
Inequality check.
Quaternion operator/(float rhs) const
Scalar division.
float y
Second vector (imaginary) component .
Quaternion()=default
Create a quaternion that is initialized to the identity quaternion.
Quaternion operator+(Quaternion rhs) const
Sum of two quaternions uses quaternion multiplication.
Quaternion & operator-=(Quaternion rhs)
Difference of two quaternions a and b is the quaternion multiplication of a and the conjugate of b.
Quaternion operator*(float rhs) const
Scalar multiplication.
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 w...
Vec3f rotate(Vec3f v) const
Rotate vector by this quaternion.
Quaternion normalized() const
Normalize a copy of this quaternion (doesn't change the original quaternion).
Quaternion & normalize()
Normalize this quaternion.
float x
First vector (imaginary) component .
static Quaternion identity()
Identity quaternion (1,0,0,0).
Quaternion operator-(Quaternion rhs) const
Difference of two quaternions a and b is the quaternion multiplication of a and the conjugate of b.
Quaternion & operator*=(float rhs)
Scalar multiplication.
float z
Third vector (imaginary) component .
Quaternion conjugated() const
Complex conjugate (doesn't change the original quaternion).
Type for 2D vectors of floating point numbers.
Type for 3D vectors of floating point numbers.