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
|
|
|
|
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
|
|
|
|
2016-05-04 10:33:57 -03:00
|
|
|
class Quaternion {
|
2012-03-04 23:31:47 -04:00
|
|
|
public:
|
2012-08-17 03:20:14 -03:00
|
|
|
float 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
|
2016-05-04 10:33:57 -03:00
|
|
|
Quaternion()
|
|
|
|
{
|
|
|
|
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
|
|
|
|
Quaternion(const float _q1, const float _q2, const float _q3, const float _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
|
|
|
|
Quaternion(const float _q[4]) :
|
|
|
|
q1(_q[0]), q2(_q[1]), q3(_q[2]), q4(_q[3])
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-08-17 03:20:14 -03:00
|
|
|
// function call operator
|
2016-05-04 10:33:57 -03:00
|
|
|
void operator()(const float _q1, const float _q2, const float _q3, const float _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
|
2014-02-19 22:20:36 -04:00
|
|
|
bool is_nan(void) const
|
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
|
|
|
|
2012-08-17 03:20:14 -03:00
|
|
|
// return the rotation matrix equivalent for this quaternion
|
2013-12-29 23:07:47 -04:00
|
|
|
void rotation_matrix(Matrix3f &m) const;
|
2012-03-10 01:44:22 -04:00
|
|
|
|
2016-06-24 04:10:35 -03:00
|
|
|
// return the rotation matrix equivalent for this quaternion after normalization
|
|
|
|
void rotation_matrix_norm(Matrix3f &m) const;
|
|
|
|
|
2014-02-14 21:20:31 -04:00
|
|
|
void from_rotation_matrix(const Matrix3f &m);
|
|
|
|
|
2012-08-17 03:20:14 -03:00
|
|
|
// convert a vector from earth to body frame
|
2013-12-29 23:07:47 -04:00
|
|
|
void earth_to_body(Vector3f &v) const;
|
2012-03-10 01:44:22 -04:00
|
|
|
|
|
|
|
// create a quaternion from Euler angles
|
2012-08-17 03:20:14 -03:00
|
|
|
void from_euler(float roll, float pitch, float yaw);
|
2012-03-10 01:44:22 -04:00
|
|
|
|
2015-04-08 00:59:38 -03:00
|
|
|
void from_vector312(float roll ,float pitch, float yaw);
|
|
|
|
|
2014-10-13 00:41:13 -03:00
|
|
|
void to_axis_angle(Vector3f &v);
|
|
|
|
|
|
|
|
void from_axis_angle(Vector3f v);
|
|
|
|
|
2014-10-19 16:13:53 -03:00
|
|
|
void from_axis_angle(const Vector3f &axis, float theta);
|
2014-10-13 00:41:13 -03:00
|
|
|
|
2014-10-19 16:13:53 -03:00
|
|
|
void rotate(const Vector3f &v);
|
2014-10-13 00:41:13 -03:00
|
|
|
|
|
|
|
void from_axis_angle_fast(Vector3f v);
|
|
|
|
|
2014-10-19 16:13:53 -03:00
|
|
|
void from_axis_angle_fast(const Vector3f &axis, float theta);
|
2014-10-13 00:41:13 -03:00
|
|
|
|
|
|
|
void rotate_fast(const Vector3f &v);
|
|
|
|
|
2015-04-23 16:44:49 -03:00
|
|
|
// get euler roll angle
|
|
|
|
float get_euler_roll() const;
|
|
|
|
|
|
|
|
// get euler pitch angle
|
|
|
|
float get_euler_pitch() const;
|
|
|
|
|
|
|
|
// get euler yaw angle
|
|
|
|
float get_euler_yaw() const;
|
2014-10-13 00:41:13 -03:00
|
|
|
|
2012-03-10 01:44:22 -04:00
|
|
|
// create eulers from a quaternion
|
2014-10-13 00:41:13 -03:00
|
|
|
void to_euler(float &roll, float &pitch, float &yaw) const;
|
2013-12-29 22:24:33 -04:00
|
|
|
|
2015-04-23 16:44:49 -03:00
|
|
|
// create eulers from a quaternion
|
2015-05-24 20:08:31 -03:00
|
|
|
Vector3f to_vector312(void) const;
|
2015-04-08 00:59:38 -03:00
|
|
|
|
2013-12-30 02:31:51 -04:00
|
|
|
float length(void) const;
|
|
|
|
void normalize();
|
|
|
|
|
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
|
|
|
|
2014-10-13 00:41:13 -03:00
|
|
|
Quaternion inverse(void) const;
|
|
|
|
|
2013-12-29 22:24:33 -04:00
|
|
|
// allow a quaternion to be used as an array, 0 indexed
|
2016-05-04 10:33:57 -03:00
|
|
|
float & operator[](uint8_t i)
|
|
|
|
{
|
2013-12-29 22:24:33 -04:00
|
|
|
float *_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];
|
|
|
|
}
|
|
|
|
|
2016-05-04 10:33:57 -03:00
|
|
|
const float & operator[](uint8_t i) const
|
|
|
|
{
|
2013-12-29 22:24:33 -04:00
|
|
|
const float *_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
|
|
|
|
2015-04-08 00:58:48 -03:00
|
|
|
Quaternion operator*(const Quaternion &v) const;
|
2014-10-19 16:13:53 -03:00
|
|
|
Quaternion &operator*=(const Quaternion &v);
|
2015-04-08 00:58:48 -03:00
|
|
|
Quaternion operator/(const Quaternion &v) const;
|
2019-02-20 01:17:23 -04:00
|
|
|
|
|
|
|
// angular difference between quaternions
|
|
|
|
Quaternion angular_difference(const Quaternion &v) const;
|
2012-03-04 23:31:47 -04:00
|
|
|
};
|