6#ifndef THEORETICA_QUATERNION_H
7#define THEORETICA_QUATERNION_H
9#ifndef THEORETICA_NO_PRINT
14#include "../core/real_analysis.h"
15#include "../algebra/algebra.h"
22 template<
typename Type = real>
43 :
a(
a),
b(
b), c(c), d(d) {}
121 return quat(
a, -
b, -c, -d);
127 return a *
a +
b *
b + c * c + d * d;
161 return (*
this =
quat(
178 return quat(-
a, -
b, -c, -d);
229 r.
a =
a *
q.a -
b *
q.b - c *
q.c - d *
q.d;
230 r.b =
a *
q.b +
b *
q.a + c *
q.d - d *
q.c;
231 r.c =
a *
q.c -
b *
q.d + c *
q.a + d *
q.b;
232 r.d =
a *
q.d +
b *
q.c - c *
q.b + d *
q.a;
246 return (*
this =
operator*(
k));
288 return (*
this =
operator*(
other));
299 template<
typename Vector>
306 TH_MATH_ERROR(
"quat::transform", v.size(), INVALID_ARGUMENT);
311 const quat q =
quat(0, v.get(0), v.get(1), v.get(2));
324 template<
typename Vector>
335 template<
typename Vector>
341 if(
axis.size() != 3) {
362 const quat p =
quat(0, v.get(0), v.get(1), v.get(2));
377 return z +
quat(
r, 0, 0, 0);
381 return (
z * -1) +
quat(
r, 0, 0, 0);
389 return quat(
r, 0, 0, 0) /
z;
394#ifndef THEORETICA_NO_PRINT
399 std::stringstream
res;
402 res << (
b >= 0 ?
" + " :
" - ") <<
abs(
b) <<
"i";
403 res << (c >= 0 ?
" + " :
" - ") <<
abs(c) <<
"j";
404 res << (d >= 0 ?
" + " :
" - ") <<
abs(d) <<
"k";
411 inline operator std::string() {
419 return out <<
obj.to_string();
Quaternion class in the form .
Definition quat.h:23
quat operator-() const
Get the opposite of the quaternion.
Definition quat.h:177
static quat rotation(Type rad, const Vector &axis)
Construct a quaternion which represents a rotation of <rad> radians around the <axis> arbitrary axis.
Definition quat.h:325
quat & operator+=(const quat &other)
Add a quaternion to this one.
Definition quat.h:267
quat()
Construct a quaternion with all zero components.
Definition quat.h:34
quat operator/(Type k) const
Divide a quaternion by a scalar value.
Definition quat.h:201
friend Type Im2(const quat &q)
Extract the second imaginary part of the quaternion.
Definition quat.h:102
Type Im1() const
Get the first imaginary part of the quaternion.
Definition quat.h:84
friend Type Re(const quat &q)
Extract the real part of the quaternion.
Definition quat.h:78
quat(const quat &q)
Copy constructor.
Definition quat.h:47
quat operator-(const quat &other) const
Subtract two quaternions.
Definition quat.h:219
friend Type Im3(const quat &q)
Extract the third imaginary part of the quaternion.
Definition quat.h:114
quat & invert()
Invert the quaternion.
Definition quat.h:152
quat operator+(Type k) const
Sum a quaternion and a scalar value.
Definition quat.h:183
Type b
Imaginary parts.
Definition quat.h:30
static Vector rotate(const Vector &v, Type rad, const Vector &axis)
Rotate a 3D vector <v> by <rad> radians around the <axis> arbitrary axis.
Definition quat.h:336
quat & operator=(const quat &q)
Assignment operator.
Definition quat.h:51
quat operator+() const
Identity (for consistency)
Definition quat.h:171
quat inverse() const
Compute the inverse of the quaternion.
Definition quat.h:138
quat & operator*=(Type k)
Multiply this quaternion by a scalar value.
Definition quat.h:245
quat conjugate() const
Compute the conjugate of the quaternion.
Definition quat.h:120
quat & operator=(const std::array< T, 4 > &v)
Assignment operator from a 4D array.
Definition quat.h:62
quat operator+(const quat &other) const
Add two quaternions.
Definition quat.h:213
quat operator*(const quat &q) const
Multiply two quaternions.
Definition quat.h:225
quat(Type a, Type b, Type c, Type d)
Construct a quaternion from its components.
Definition quat.h:42
Type Re() const
Get the real part of the quaternion.
Definition quat.h:72
quat(Type r)
Construct a quaternion from a real number.
Definition quat.h:38
quat operator*(Type k) const
Multiply a quaternion by a scalar value.
Definition quat.h:195
quat operator/(const quat &other) const
Divide two quaternions.
Definition quat.h:239
std::string to_string() const
Convert the quaternion to string representation.
Definition quat.h:397
quat & operator/=(Type k)
Divide this quaternion by a scalar value.
Definition quat.h:251
Type Im3() const
Get the third imaginary part of the quaternion.
Definition quat.h:108
Type norm() const
Compute the norm of the quaternion.
Definition quat.h:132
quat & operator*=(const quat &other)
Multiply this quaternion by another one.
Definition quat.h:287
quat & operator/=(const quat &other)
Divide this quaternion by another one.
Definition quat.h:293
Type sqr_norm() const
Compute the square norm of the quaternion.
Definition quat.h:126
friend std::ostream & operator<<(std::ostream &out, const quat &obj)
Stream the quaternion in string representation to an output stream (std::ostream)
Definition quat.h:418
Type Im2() const
Get the second imaginary part of the quaternion.
Definition quat.h:96
friend Type Im1(const quat &q)
Extract the first imaginary part of the quaternion.
Definition quat.h:90
quat & operator-=(const quat &other)
Subtract a quaternion to this one.
Definition quat.h:277
Type a
Real part.
Definition quat.h:27
quat operator-(Type k) const
Subtract a quaternion and a scalar value.
Definition quat.h:189
Vector transform(const Vector &v) const
Transform a 3D vector.
Definition quat.h:300
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compiling opti...
Definition error.h:219
Vector & vec_error(Vector &v)
Overwrite the given vector with the error vector with NaN values.
Definition algebra.h:62
Vector normalize(const Vector &v)
Returns the normalized vector.
Definition algebra.h:302
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
dual2 sqrt(dual2 x)
Compute the square root of a second order dual number.
Definition dual2_functions.h:54
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition dual2_functions.h:198
std::remove_reference_t< decltype(std::declval< Structure >()[0])> vector_element_t
Extract the type of a vector (or any indexable container) from its operator[].
Definition core_traits.h:134
dual2 cos(dual2 x)
Compute the cosine of a second order dual number.
Definition dual2_functions.h:86
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition constants.h:207
dual2 sin(dual2 x)
Compute the sine of a second order dual number.
Definition dual2_functions.h:72
real nan()
Return a quiet NaN number in floating point representation.
Definition error.h:54