27#include <AH/STL/cmath>
28#include <AH/STL/limits>
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;
169 M11 * v.
x + M12 * v.
y + M13 * v.
z,
170 M21 * v.
x + M22 * v.
y + M23 * v.
z,
171 M31 * v.
x + M32 * v.
y + M33 * v.
z,
177 return this->w == rhs.
w && this->x == rhs.
x && this->y == rhs.
y &&
205 float eps = std::numeric_limits<float>::epsilon();
208 float v_norm = v.
norm();
213 float x = v.
x / v_norm;
214 float y = v.
y / v_norm;
217 if (std::abs(
x) <= eps && std::abs(
y) <= eps)
223 float crossNorm =
min(cross.
norm(), 1);
227 float angle = std::asin(crossNorm);
229 angle = float(180_deg) - angle;
234 std::sin(angle / 2) * cross.
x,
235 std::sin(angle / 2) * cross.
y,
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,
276 return {lhs * rhs.
w, lhs * rhs.
x, lhs * rhs.
y, lhs * rhs.
z};
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));
324 return {psi, theta, phi};
331 float cy = std::cos(eulerAngles.
yaw / 2);
332 float sy = std::sin(eulerAngles.
yaw / 2);
333 float cp = std::cos(eulerAngles.
pitch / 2);
334 float sp = std::sin(eulerAngles.
pitch / 2);
335 float cr = std::cos(eulerAngles.
roll / 2);
336 float sr = std::sin(eulerAngles.
roll / 2);
339 cy * cp * cr + sy * sp * sr,
340 cy * cp * sr - sy * sp * cr,
341 sy * cp * sr + cy * sp * cr,
342 sy * cp * cr - cy * sp * sr,
Conversions between radians and degrees.
#define BEGIN_AH_NAMESPACE
Definition of Vec2f and Vec3f.
constexpr auto min(const T &a, const U &b) -> decltype(b< a ? b :a)
Return the smaller of two numbers/objects.
Print & operator<<(Print &os, Quaternion e)
Printing.
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.
float roll
X": drone X = world +Y.
float pitch
Y': drone Y = world -X.
static EulerAngles quat2eul(Quaternion q)
Convert the given quaternion to Euler angles.
bool operator!=(EulerAngles rhs) const
Inequality check.
float yaw
Z : drone Z = world +Z.
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.
float y
The y component of the vector.
float x
The x component of the vector.
Type for 3D vectors of floating point numbers.
float y
The y component of the vector.
float x
The x component of the vector.
float z
The z component of the vector.