2013-08-29 02:34:34 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2012-03-04 23:31:47 -04:00
|
|
|
|
2013-08-29 02:34:34 -03:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2012-03-04 23:31:47 -04:00
|
|
|
|
2013-08-29 02:34:34 -03:00
|
|
|
// Copyright 2012 Andrew Tridgell, all rights reserved.
|
2014-10-13 00:41:13 -03:00
|
|
|
// Refactored by Jonathan Challinger
|
2016-02-17 21:25:33 -04:00
|
|
|
#pragma once
|
2012-03-04 23:31:47 -04:00
|
|
|
|
2021-11-05 13:12:24 -03:00
|
|
|
#include "definitions.h"
|
|
|
|
#include "matrix3.h"
|
2016-03-31 18:43:36 -03:00
|
|
|
#include <cmath>
|
2016-02-14 18:22:23 -04:00
|
|
|
#if MATH_CHECK_INDEXES
|
2013-12-29 22:24:33 -04:00
|
|
|
#include <assert.h>
|
|
|
|
#endif
|
2018-07-01 19:42:23 -03:00
|
|
|
#include <math.h>
|
2012-03-04 23:31:47 -04:00
|
|
|
|
2021-05-04 08:12:23 -03:00
|
|
|
template <typename T>
|
|
|
|
class QuaternionT {
|
2012-03-04 23:31:47 -04:00
|
|
|
public:
|
2021-05-04 08:12:23 -03:00
|
|
|
T q1, q2, q3, q4;
|
2012-03-04 23:31:47 -04:00
|
|
|
|
2012-08-17 03:20:14 -03:00
|
|
|
// constructor creates a quaternion equivalent
|
|
|
|
// to roll=0, pitch=0, yaw=0
|
2022-06-27 00:56:24 -03:00
|
|
|
QuaternionT()
|
2016-05-04 10:33:57 -03:00
|
|
|
{
|
|
|
|
q1 = 1;
|
|
|
|
q2 = q3 = q4 = 0;
|
2012-08-17 03:20:14 -03:00
|
|
|
}
|
2012-03-04 23:31:47 -04:00
|
|
|
|
2012-08-17 03:20:14 -03:00
|
|
|
// setting constructor
|
2022-06-27 00:56:24 -03:00
|
|
|
QuaternionT(const T _q1, const T _q2, const T _q3, const T _q4) :
|
2016-05-04 10:33:57 -03:00
|
|
|
q1(_q1), q2(_q2), q3(_q3), q4(_q4)
|
|
|
|
{
|
2012-08-17 03:20:14 -03:00
|
|
|
}
|
2012-03-04 23:31:47 -04:00
|
|
|
|
2018-03-07 20:45:24 -04:00
|
|
|
// setting constructor
|
2022-06-27 00:56:24 -03:00
|
|
|
QuaternionT(const T _q[4]) :
|
2018-03-07 20:45:24 -04:00
|
|
|
q1(_q[0]), q2(_q[1]), q3(_q[2]), q4(_q[3])
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-08-17 03:20:14 -03:00
|
|
|
// function call operator
|
2021-05-04 08:12:23 -03:00
|
|
|
void operator()(const T _q1, const T _q2, const T _q3, const T _q4)
|
2012-08-17 03:20:14 -03:00
|
|
|
{
|
2016-05-04 10:33:57 -03:00
|
|
|
q1 = _q1;
|
|
|
|
q2 = _q2;
|
|
|
|
q3 = _q3;
|
|
|
|
q4 = _q4;
|
2012-08-17 03:20:14 -03:00
|
|
|
}
|
2012-03-04 23:31:47 -04:00
|
|
|
|
2012-08-17 03:20:14 -03:00
|
|
|
// check if any elements are NAN
|
2019-07-24 20:25:06 -03:00
|
|
|
bool is_nan(void) const WARN_IF_UNUSED
|
2012-08-17 03:20:14 -03:00
|
|
|
{
|
|
|
|
return isnan(q1) || isnan(q2) || isnan(q3) || isnan(q4);
|
|
|
|
}
|
2012-03-04 23:31:47 -04:00
|
|
|
|
2021-08-17 05:32:49 -03:00
|
|
|
// populate the supplied rotation matrix equivalent from this quaternion
|
2013-12-29 23:07:47 -04:00
|
|
|
void rotation_matrix(Matrix3f &m) const;
|
2021-05-04 08:12:23 -03:00
|
|
|
void rotation_matrix(Matrix3d &m) const;
|
2012-03-10 01:44:22 -04:00
|
|
|
|
2021-08-17 05:32:49 -03:00
|
|
|
// make this quaternion equivalent to the supplied matrix
|
2021-05-04 08:12:23 -03:00
|
|
|
void from_rotation_matrix(const Matrix3<T> &m);
|
2014-02-14 21:20:31 -04:00
|
|
|
|
2020-03-26 01:31:11 -03:00
|
|
|
// create a quaternion from a given rotation
|
|
|
|
void from_rotation(enum Rotation rotation);
|
|
|
|
|
|
|
|
// rotate this quaternion by the given rotation
|
|
|
|
void rotate(enum Rotation rotation);
|
|
|
|
|
2012-08-17 03:20:14 -03:00
|
|
|
// convert a vector from earth to body frame
|
2021-05-04 08:12:23 -03:00
|
|
|
void earth_to_body(Vector3<T> &v) const;
|
2012-03-10 01:44:22 -04:00
|
|
|
|
|
|
|
// create a quaternion from Euler angles
|
2021-05-04 08:12:23 -03:00
|
|
|
void from_euler(T roll, T pitch, T yaw);
|
2021-07-23 02:44:19 -03:00
|
|
|
void from_euler(const Vector3<T> &v);
|
2012-03-10 01:44:22 -04:00
|
|
|
|
2020-04-02 00:12:11 -03:00
|
|
|
// create a quaternion from Euler angles applied in yaw, roll, pitch order
|
|
|
|
// instead of the normal yaw, pitch, roll order
|
2021-05-04 08:12:23 -03:00
|
|
|
void from_vector312(T roll, T pitch, T yaw);
|
2015-04-08 00:59:38 -03:00
|
|
|
|
2020-04-02 00:12:11 -03:00
|
|
|
// convert this quaternion to a rotation vector where the direction of the vector represents
|
|
|
|
// the axis of rotation and the length of the vector represents the angle of rotation
|
2021-05-04 08:12:23 -03:00
|
|
|
void to_axis_angle(Vector3<T> &v) const;
|
2014-10-13 00:41:13 -03:00
|
|
|
|
2020-04-02 00:12:11 -03:00
|
|
|
// create a quaternion from a rotation vector where the direction of the vector represents
|
|
|
|
// the axis of rotation and the length of the vector represents the angle of rotation
|
2021-05-04 08:12:23 -03:00
|
|
|
void from_axis_angle(Vector3<T> v);
|
2014-10-13 00:41:13 -03:00
|
|
|
|
2020-04-02 00:12:11 -03:00
|
|
|
// create a quaternion from its axis-angle representation
|
|
|
|
// the axis vector must be length 1. the rotation angle theta is in radians
|
2021-05-04 08:12:23 -03:00
|
|
|
void from_axis_angle(const Vector3<T> &axis, T theta);
|
2014-10-13 00:41:13 -03:00
|
|
|
|
2020-04-02 00:12:11 -03:00
|
|
|
// rotate by the provided rotation vector
|
2021-05-04 08:12:23 -03:00
|
|
|
void rotate(const Vector3<T> &v);
|
2014-10-13 00:41:13 -03:00
|
|
|
|
2020-04-02 00:12:11 -03:00
|
|
|
// create a quaternion from a rotation vector
|
|
|
|
// only use with small angles. I.e. length of v should less than 0.17 radians (i.e. 10 degrees)
|
2021-05-04 08:12:23 -03:00
|
|
|
void from_axis_angle_fast(Vector3<T> v);
|
2014-10-13 00:41:13 -03:00
|
|
|
|
2020-04-02 00:12:11 -03:00
|
|
|
// create a quaternion from its axis-angle representation
|
|
|
|
// the axis vector must be length 1, theta should less than 0.17 radians (i.e. 10 degrees)
|
2021-05-04 08:12:23 -03:00
|
|
|
void from_axis_angle_fast(const Vector3<T> &axis, T theta);
|
2014-10-13 00:41:13 -03:00
|
|
|
|
2022-09-22 02:56:49 -03:00
|
|
|
// create a quaternion by integrating an angular velocity over some time_delta, which is
|
|
|
|
// assumed to be small
|
2022-09-15 00:51:06 -03:00
|
|
|
void from_angular_velocity(const Vector3<T>& angular_velocity, float time_delta);
|
|
|
|
|
2020-04-02 00:12:11 -03:00
|
|
|
// rotate by the provided rotation vector
|
|
|
|
// only use with small angles. I.e. length of v should less than 0.17 radians (i.e. 10 degrees)
|
2021-05-04 08:12:23 -03:00
|
|
|
void rotate_fast(const Vector3<T> &v);
|
2014-10-13 00:41:13 -03:00
|
|
|
|
2022-09-20 02:30:16 -03:00
|
|
|
// get euler roll angle in radians
|
2021-05-04 08:12:23 -03:00
|
|
|
T get_euler_roll() const;
|
2015-04-23 16:44:49 -03:00
|
|
|
|
2022-09-20 02:30:16 -03:00
|
|
|
// get euler pitch angle in radians
|
2021-05-04 08:12:23 -03:00
|
|
|
T get_euler_pitch() const;
|
2015-04-23 16:44:49 -03:00
|
|
|
|
2022-09-20 02:30:16 -03:00
|
|
|
// get euler yaw angle in radians
|
2021-05-04 08:12:23 -03:00
|
|
|
T get_euler_yaw() const;
|
2014-10-13 00:41:13 -03:00
|
|
|
|
2022-09-20 02:30:16 -03:00
|
|
|
// create eulers (in radians) from a quaternion
|
2014-10-13 00:41:13 -03:00
|
|
|
void to_euler(float &roll, float &pitch, float &yaw) const;
|
2021-05-04 08:12:23 -03:00
|
|
|
void to_euler(double &roll, double &pitch, double &yaw) const;
|
2013-12-29 22:24:33 -04:00
|
|
|
|
2015-04-23 16:44:49 -03:00
|
|
|
// create eulers from a quaternion
|
2021-05-04 08:12:23 -03:00
|
|
|
Vector3<T> to_vector312(void) const;
|
2015-04-08 00:59:38 -03:00
|
|
|
|
2022-01-12 00:36:09 -04:00
|
|
|
T length_squared(void) const;
|
2021-05-04 08:12:23 -03:00
|
|
|
T length(void) const;
|
2013-12-30 02:31:51 -04:00
|
|
|
void normalize();
|
|
|
|
|
2022-01-12 00:36:09 -04:00
|
|
|
// Checks if each element of the quaternion is zero
|
|
|
|
bool is_zero(void) const;
|
|
|
|
|
|
|
|
// zeros the quaternion to [0, 0, 0, 0], an invalid quaternion
|
|
|
|
// See initialize() if you want the zero rotation quaternion
|
|
|
|
void zero(void);
|
|
|
|
|
|
|
|
// Checks if the quaternion is unit_length within a tolerance
|
|
|
|
// Returns True: if its magnitude is close to unit length +/- 1E-3
|
|
|
|
// This limit is somewhat greater than sqrt(FLT_EPSL)
|
|
|
|
bool is_unit_length(void) const;
|
|
|
|
|
2015-05-01 03:54:54 -03:00
|
|
|
// initialise the quaternion to no rotation
|
2016-05-04 10:33:57 -03:00
|
|
|
void initialise()
|
|
|
|
{
|
|
|
|
q1 = 1.0f;
|
|
|
|
q2 = q3 = q4 = 0.0f;
|
|
|
|
}
|
2015-05-01 03:54:54 -03:00
|
|
|
|
2021-05-04 08:12:23 -03:00
|
|
|
QuaternionT<T> inverse(void) const;
|
2014-10-13 00:41:13 -03:00
|
|
|
|
2020-03-26 01:31:11 -03:00
|
|
|
// reverse the rotation of this quaternion
|
|
|
|
void invert();
|
|
|
|
|
2013-12-29 22:24:33 -04:00
|
|
|
// allow a quaternion to be used as an array, 0 indexed
|
2021-05-04 08:12:23 -03:00
|
|
|
T & operator[](uint8_t i)
|
2016-05-04 10:33:57 -03:00
|
|
|
{
|
2021-05-04 08:12:23 -03:00
|
|
|
T *_v = &q1;
|
2016-02-14 18:22:23 -04:00
|
|
|
#if MATH_CHECK_INDEXES
|
2015-01-08 20:04:50 -04:00
|
|
|
assert(i < 4);
|
2013-12-29 22:24:33 -04:00
|
|
|
#endif
|
2013-12-29 22:24:33 -04:00
|
|
|
return _v[i];
|
|
|
|
}
|
|
|
|
|
2021-05-04 08:12:23 -03:00
|
|
|
const T & operator[](uint8_t i) const
|
2016-05-04 10:33:57 -03:00
|
|
|
{
|
2021-05-04 08:12:23 -03:00
|
|
|
const T *_v = &q1;
|
2016-02-14 18:22:23 -04:00
|
|
|
#if MATH_CHECK_INDEXES
|
2015-01-08 20:04:50 -04:00
|
|
|
assert(i < 4);
|
2013-12-29 22:24:33 -04:00
|
|
|
#endif
|
2013-12-29 22:24:33 -04:00
|
|
|
return _v[i];
|
|
|
|
}
|
2014-10-13 00:41:13 -03:00
|
|
|
|
2021-05-04 08:12:23 -03:00
|
|
|
QuaternionT<T> operator*(const QuaternionT<T> &v) const;
|
|
|
|
Vector3<T> operator*(const Vector3<T> &v) const;
|
|
|
|
QuaternionT<T> &operator*=(const QuaternionT<T> &v);
|
|
|
|
QuaternionT<T> operator/(const QuaternionT<T> &v) const;
|
2019-02-20 01:17:23 -04:00
|
|
|
|
|
|
|
// angular difference between quaternions
|
2021-05-04 08:12:23 -03:00
|
|
|
QuaternionT<T> angular_difference(const QuaternionT<T> &v) const;
|
2021-06-02 02:08:11 -03:00
|
|
|
|
|
|
|
// absolute (e.g. always positive) earth-frame roll-pitch difference (in radians) between this Quaternion and another
|
2021-05-04 08:12:23 -03:00
|
|
|
T roll_pitch_difference(const QuaternionT<T> &v) const;
|
|
|
|
|
|
|
|
// double/float conversion
|
|
|
|
QuaternionT<double> todouble(void) const {
|
|
|
|
return QuaternionT<double>(q1,q2,q3,q4);
|
|
|
|
}
|
|
|
|
QuaternionT<float> tofloat(void) const {
|
|
|
|
return QuaternionT<float>(q1,q2,q3,q4);
|
|
|
|
}
|
2012-03-04 23:31:47 -04:00
|
|
|
};
|
2021-05-04 08:12:23 -03:00
|
|
|
|
|
|
|
typedef QuaternionT<float> Quaternion;
|
|
|
|
typedef QuaternionT<double> QuaternionD;
|
|
|
|
|
|
|
|
|
|
|
|
|