2012-03-10 01:44:22 -04:00
|
|
|
/*
|
|
|
|
* quaternion.cpp
|
|
|
|
* Copyright (C) Andrew Tridgell 2012
|
|
|
|
*
|
|
|
|
* This file 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.
|
|
|
|
*
|
|
|
|
* This file 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/>.
|
|
|
|
*/
|
|
|
|
|
2015-10-19 18:27:22 -03:00
|
|
|
#pragma GCC optimize("O3")
|
|
|
|
|
2012-03-10 01:44:22 -04:00
|
|
|
#include "AP_Math.h"
|
|
|
|
|
|
|
|
// return the rotation matrix equivalent for this quaternion
|
2013-12-29 23:07:47 -04:00
|
|
|
void Quaternion::rotation_matrix(Matrix3f &m) const
|
2012-03-10 01:44:22 -04:00
|
|
|
{
|
2012-08-17 03:20:14 -03:00
|
|
|
float q3q3 = q3 * q3;
|
|
|
|
float q3q4 = q3 * q4;
|
|
|
|
float q2q2 = q2 * q2;
|
|
|
|
float q2q3 = q2 * q3;
|
|
|
|
float q2q4 = q2 * q4;
|
|
|
|
float q1q2 = q1 * q2;
|
|
|
|
float q1q3 = q1 * q3;
|
|
|
|
float q1q4 = q1 * q4;
|
2012-03-10 01:44:22 -04:00
|
|
|
float q4q4 = q4 * q4;
|
|
|
|
|
2014-10-13 00:41:13 -03:00
|
|
|
m.a.x = 1.0f-2.0f*(q3q3 + q4q4);
|
2016-06-24 04:12:58 -03:00
|
|
|
m.a.y = 2.0f*(q2q3 - q1q4);
|
|
|
|
m.a.z = 2.0f*(q2q4 + q1q3);
|
|
|
|
m.b.x = 2.0f*(q2q3 + q1q4);
|
2014-10-13 00:41:13 -03:00
|
|
|
m.b.y = 1.0f-2.0f*(q2q2 + q4q4);
|
2016-06-24 04:12:58 -03:00
|
|
|
m.b.z = 2.0f*(q3q4 - q1q2);
|
|
|
|
m.c.x = 2.0f*(q2q4 - q1q3);
|
|
|
|
m.c.y = 2.0f*(q3q4 + q1q2);
|
2014-10-13 00:41:13 -03:00
|
|
|
m.c.z = 1.0f-2.0f*(q2q2 + q3q3);
|
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 Quaternion::rotation_matrix_norm(Matrix3f &m) const
|
|
|
|
{
|
|
|
|
float q1q1 = q1 * q1;
|
|
|
|
float q1q2 = q1 * q2;
|
|
|
|
float q1q3 = q1 * q3;
|
|
|
|
float q1q4 = q1 * q4;
|
|
|
|
float q2q2 = q2 * q2;
|
|
|
|
float q2q3 = q2 * q3;
|
|
|
|
float q2q4 = q2 * q4;
|
|
|
|
float q3q3 = q3 * q3;
|
|
|
|
float q3q4 = q3 * q4;
|
|
|
|
float q4q4 = q4 * q4;
|
|
|
|
float invs = 1.0f / (q1q1 + q2q2 + q3q3 + q4q4);
|
|
|
|
|
|
|
|
m.a.x = ( q2q2 - q3q3 - q4q4 + q1q1)*invs;
|
|
|
|
m.a.y = 2.0f*(q2q3 - q1q4)*invs;
|
|
|
|
m.a.z = 2.0f*(q2q4 + q1q3)*invs;
|
|
|
|
m.b.x = 2.0f*(q2q3 + q1q4)*invs;
|
|
|
|
m.b.y = (-q2q2 + q3q3 - q4q4 + q1q1)*invs;
|
|
|
|
m.b.z = 2.0f*(q3q4 - q1q2)*invs;
|
|
|
|
m.c.x = 2.0f*(q2q4 - q1q3)*invs;
|
|
|
|
m.c.y = 2.0f*(q3q4 + q1q2)*invs;
|
|
|
|
m.c.z = (-q2q2 - q3q3 + q4q4 + q1q1)*invs;
|
|
|
|
}
|
|
|
|
|
2014-02-14 21:20:31 -04:00
|
|
|
// return the rotation matrix equivalent for this quaternion
|
|
|
|
// Thanks to Martin John Baker
|
|
|
|
// http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
|
|
|
|
void Quaternion::from_rotation_matrix(const Matrix3f &m)
|
|
|
|
{
|
|
|
|
const float &m00 = m.a.x;
|
|
|
|
const float &m11 = m.b.y;
|
|
|
|
const float &m22 = m.c.z;
|
|
|
|
const float &m10 = m.b.x;
|
|
|
|
const float &m01 = m.a.y;
|
|
|
|
const float &m20 = m.c.x;
|
|
|
|
const float &m02 = m.a.z;
|
|
|
|
const float &m21 = m.c.y;
|
|
|
|
const float &m12 = m.b.z;
|
|
|
|
float &qw = q1;
|
|
|
|
float &qx = q2;
|
|
|
|
float &qy = q3;
|
|
|
|
float &qz = q4;
|
|
|
|
|
|
|
|
float tr = m00 + m11 + m22;
|
|
|
|
|
|
|
|
if (tr > 0) {
|
|
|
|
float S = sqrtf(tr+1) * 2;
|
|
|
|
qw = 0.25f * S;
|
|
|
|
qx = (m21 - m12) / S;
|
2016-05-04 10:33:57 -03:00
|
|
|
qy = (m02 - m20) / S;
|
|
|
|
qz = (m10 - m01) / S;
|
|
|
|
} else if ((m00 > m11) && (m00 > m22)) {
|
2016-06-24 04:14:52 -03:00
|
|
|
float S = sqrtf(1.0f + m00 - m11 - m22) * 2.0f;
|
2014-02-14 21:20:31 -04:00
|
|
|
qw = (m21 - m12) / S;
|
|
|
|
qx = 0.25f * S;
|
2016-05-04 10:33:57 -03:00
|
|
|
qy = (m01 + m10) / S;
|
|
|
|
qz = (m02 + m20) / S;
|
|
|
|
} else if (m11 > m22) {
|
2016-06-24 04:14:52 -03:00
|
|
|
float S = sqrtf(1.0f + m11 - m00 - m22) * 2.0f;
|
2014-02-14 21:20:31 -04:00
|
|
|
qw = (m02 - m20) / S;
|
2016-05-04 10:33:57 -03:00
|
|
|
qx = (m01 + m10) / S;
|
2014-02-14 21:20:31 -04:00
|
|
|
qy = 0.25f * S;
|
2016-05-04 10:33:57 -03:00
|
|
|
qz = (m12 + m21) / S;
|
|
|
|
} else {
|
2016-06-24 04:14:52 -03:00
|
|
|
float S = sqrtf(1.0f + m22 - m00 - m11) * 2.0f;
|
2014-02-14 21:20:31 -04:00
|
|
|
qw = (m10 - m01) / S;
|
|
|
|
qx = (m02 + m20) / S;
|
|
|
|
qy = (m12 + m21) / S;
|
|
|
|
qz = 0.25f * S;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-10 01:44:22 -04:00
|
|
|
// convert a vector from earth to body frame
|
2013-12-29 23:07:47 -04:00
|
|
|
void Quaternion::earth_to_body(Vector3f &v) const
|
2012-03-10 01:44:22 -04:00
|
|
|
{
|
|
|
|
Matrix3f m;
|
|
|
|
rotation_matrix(m);
|
|
|
|
v = m * v;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a quaternion from Euler angles
|
|
|
|
void Quaternion::from_euler(float roll, float pitch, float yaw)
|
|
|
|
{
|
2013-01-10 14:42:24 -04:00
|
|
|
float cr2 = cosf(roll*0.5f);
|
|
|
|
float cp2 = cosf(pitch*0.5f);
|
|
|
|
float cy2 = cosf(yaw*0.5f);
|
|
|
|
float sr2 = sinf(roll*0.5f);
|
|
|
|
float sp2 = sinf(pitch*0.5f);
|
|
|
|
float sy2 = sinf(yaw*0.5f);
|
2012-03-10 01:44:22 -04:00
|
|
|
|
|
|
|
q1 = cr2*cp2*cy2 + sr2*sp2*sy2;
|
|
|
|
q2 = sr2*cp2*cy2 - cr2*sp2*sy2;
|
|
|
|
q3 = cr2*sp2*cy2 + sr2*cp2*sy2;
|
|
|
|
q4 = cr2*cp2*sy2 - sr2*sp2*cy2;
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:59:38 -03:00
|
|
|
// create a quaternion from Euler angles
|
|
|
|
void Quaternion::from_vector312(float roll ,float pitch, float yaw)
|
|
|
|
{
|
|
|
|
Matrix3f m;
|
2015-05-24 20:08:31 -03:00
|
|
|
m.from_euler312(roll, pitch, yaw);
|
2015-04-08 00:59:38 -03:00
|
|
|
|
|
|
|
from_rotation_matrix(m);
|
|
|
|
}
|
|
|
|
|
2016-05-04 10:33:57 -03:00
|
|
|
void Quaternion::from_axis_angle(Vector3f v)
|
|
|
|
{
|
2014-10-13 00:41:13 -03:00
|
|
|
float theta = v.length();
|
2016-06-24 04:14:52 -03:00
|
|
|
if (is_zero(theta)) {
|
2014-10-13 00:41:13 -03:00
|
|
|
q1 = 1.0f;
|
|
|
|
q2=q3=q4=0.0f;
|
2015-04-08 00:58:20 -03:00
|
|
|
return;
|
2012-03-10 02:05:54 -04:00
|
|
|
}
|
2014-10-13 00:41:13 -03:00
|
|
|
v /= theta;
|
|
|
|
from_axis_angle(v,theta);
|
|
|
|
}
|
|
|
|
|
2016-05-04 10:33:57 -03:00
|
|
|
void Quaternion::from_axis_angle(const Vector3f &axis, float theta)
|
|
|
|
{
|
2016-06-24 04:14:52 -03:00
|
|
|
// axis must be a unit vector as there is no check for length
|
|
|
|
if (is_zero(theta)) {
|
2014-10-13 00:41:13 -03:00
|
|
|
q1 = 1.0f;
|
|
|
|
q2=q3=q4=0.0f;
|
2016-12-05 05:08:08 -04:00
|
|
|
return;
|
2012-03-10 02:05:54 -04:00
|
|
|
}
|
2014-10-13 00:41:13 -03:00
|
|
|
float st2 = sinf(theta/2.0f);
|
|
|
|
|
2015-05-15 13:01:27 -03:00
|
|
|
q1 = cosf(theta/2.0f);
|
2014-10-13 00:41:13 -03:00
|
|
|
q2 = axis.x * st2;
|
|
|
|
q3 = axis.y * st2;
|
|
|
|
q4 = axis.z * st2;
|
|
|
|
}
|
|
|
|
|
2016-05-04 10:33:57 -03:00
|
|
|
void Quaternion::rotate(const Vector3f &v)
|
|
|
|
{
|
2014-10-13 00:41:13 -03:00
|
|
|
Quaternion r;
|
|
|
|
r.from_axis_angle(v);
|
|
|
|
(*this) *= r;
|
|
|
|
}
|
|
|
|
|
2016-05-04 10:33:57 -03:00
|
|
|
void Quaternion::to_axis_angle(Vector3f &v)
|
|
|
|
{
|
2018-05-03 22:19:31 -03:00
|
|
|
float l = sqrtf(sq(q2)+sq(q3)+sq(q4));
|
2014-10-13 00:41:13 -03:00
|
|
|
v = Vector3f(q2,q3,q4);
|
2016-06-24 04:14:52 -03:00
|
|
|
if (!is_zero(l)) {
|
2014-10-13 00:41:13 -03:00
|
|
|
v /= l;
|
2015-05-03 03:04:03 -03:00
|
|
|
v *= wrap_PI(2.0f * atan2f(l,q1));
|
2012-03-10 02:05:54 -04:00
|
|
|
}
|
2012-03-10 01:44:22 -04:00
|
|
|
}
|
2013-12-30 02:31:51 -04:00
|
|
|
|
2016-05-04 10:33:57 -03:00
|
|
|
void Quaternion::from_axis_angle_fast(Vector3f v)
|
|
|
|
{
|
2014-10-13 00:41:13 -03:00
|
|
|
float theta = v.length();
|
2016-06-24 04:14:52 -03:00
|
|
|
if (is_zero(theta)) {
|
2014-10-13 00:41:13 -03:00
|
|
|
q1 = 1.0f;
|
|
|
|
q2=q3=q4=0.0f;
|
2016-12-05 05:08:08 -04:00
|
|
|
return;
|
2014-10-13 00:41:13 -03:00
|
|
|
}
|
|
|
|
v /= theta;
|
|
|
|
from_axis_angle_fast(v,theta);
|
|
|
|
}
|
|
|
|
|
2016-05-04 10:33:57 -03:00
|
|
|
void Quaternion::from_axis_angle_fast(const Vector3f &axis, float theta)
|
|
|
|
{
|
2014-10-13 00:41:13 -03:00
|
|
|
float t2 = theta/2.0f;
|
|
|
|
float sqt2 = sq(t2);
|
|
|
|
float st2 = t2-sqt2*t2/6.0f;
|
|
|
|
|
|
|
|
q1 = 1.0f-(sqt2/2.0f)+sq(sqt2)/24.0f;
|
|
|
|
q2 = axis.x * st2;
|
|
|
|
q3 = axis.y * st2;
|
|
|
|
q4 = axis.z * st2;
|
|
|
|
}
|
|
|
|
|
2016-05-04 10:33:57 -03:00
|
|
|
void Quaternion::rotate_fast(const Vector3f &v)
|
|
|
|
{
|
2014-10-13 00:41:13 -03:00
|
|
|
float theta = v.length();
|
2016-06-24 04:14:52 -03:00
|
|
|
if (is_zero(theta)) {
|
2016-05-04 10:33:57 -03:00
|
|
|
return;
|
|
|
|
}
|
2014-10-13 00:41:13 -03:00
|
|
|
float t2 = theta/2.0f;
|
|
|
|
float sqt2 = sq(t2);
|
|
|
|
float st2 = t2-sqt2*t2/6.0f;
|
|
|
|
st2 /= theta;
|
|
|
|
|
|
|
|
//"rotation quaternion"
|
|
|
|
float w2 = 1.0f-(sqt2/2.0f)+sq(sqt2)/24.0f;
|
|
|
|
float x2 = v.x * st2;
|
|
|
|
float y2 = v.y * st2;
|
|
|
|
float z2 = v.z * st2;
|
|
|
|
|
|
|
|
//copy our quaternion
|
|
|
|
float w1 = q1;
|
|
|
|
float x1 = q2;
|
|
|
|
float y1 = q3;
|
|
|
|
float z1 = q4;
|
|
|
|
|
|
|
|
//do the multiply into our quaternion
|
|
|
|
q1 = w1*w2 - x1*x2 - y1*y2 - z1*z2;
|
|
|
|
q2 = w1*x2 + x1*w2 + y1*z2 - z1*y2;
|
|
|
|
q3 = w1*y2 - x1*z2 + y1*w2 + z1*x2;
|
|
|
|
q4 = w1*z2 + x1*y2 - y1*x2 + z1*w2;
|
|
|
|
}
|
|
|
|
|
2015-04-23 16:44:49 -03:00
|
|
|
// get euler roll angle
|
|
|
|
float Quaternion::get_euler_roll() const
|
|
|
|
{
|
2016-06-24 04:14:52 -03:00
|
|
|
return (atan2f(2.0f*(q1*q2 + q3*q4), 1.0f - 2.0f*(q2*q2 + q3*q3)));
|
2015-04-23 16:44:49 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// get euler pitch angle
|
|
|
|
float Quaternion::get_euler_pitch() const
|
|
|
|
{
|
|
|
|
return safe_asin(2.0f*(q1*q3 - q4*q2));
|
|
|
|
}
|
|
|
|
|
|
|
|
// get euler yaw angle
|
|
|
|
float Quaternion::get_euler_yaw() const
|
|
|
|
{
|
2016-06-24 04:14:52 -03:00
|
|
|
return atan2f(2.0f*(q1*q4 + q2*q3), 1.0f - 2.0f*(q3*q3 + q4*q4));
|
2015-04-23 16:44:49 -03:00
|
|
|
}
|
|
|
|
|
2014-10-13 00:41:13 -03:00
|
|
|
// create eulers from a quaternion
|
|
|
|
void Quaternion::to_euler(float &roll, float &pitch, float &yaw) const
|
|
|
|
{
|
2015-04-23 16:44:49 -03:00
|
|
|
roll = get_euler_roll();
|
|
|
|
pitch = get_euler_pitch();
|
|
|
|
yaw = get_euler_yaw();
|
2014-10-13 00:41:13 -03:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:59:38 -03:00
|
|
|
// create eulers from a quaternion
|
2015-05-24 20:08:31 -03:00
|
|
|
Vector3f Quaternion::to_vector312(void) const
|
2016-05-04 10:33:57 -03:00
|
|
|
{
|
2015-04-08 00:59:38 -03:00
|
|
|
Matrix3f m;
|
|
|
|
rotation_matrix(m);
|
2015-05-24 20:08:31 -03:00
|
|
|
return m.to_euler312();
|
2015-04-08 00:59:38 -03:00
|
|
|
}
|
|
|
|
|
2013-12-30 02:31:51 -04:00
|
|
|
float Quaternion::length(void) const
|
|
|
|
{
|
|
|
|
return sqrtf(sq(q1) + sq(q2) + sq(q3) + sq(q4));
|
|
|
|
}
|
|
|
|
|
2014-10-13 00:41:13 -03:00
|
|
|
Quaternion Quaternion::inverse(void) const
|
|
|
|
{
|
|
|
|
return Quaternion(q1, -q2, -q3, -q4);
|
|
|
|
}
|
|
|
|
|
2013-12-30 02:31:51 -04:00
|
|
|
void Quaternion::normalize(void)
|
|
|
|
{
|
|
|
|
float quatMag = length();
|
2016-06-24 04:14:52 -03:00
|
|
|
if (!is_zero(quatMag)) {
|
2013-12-30 02:31:51 -04:00
|
|
|
float quatMagInv = 1.0f/quatMag;
|
|
|
|
q1 *= quatMagInv;
|
|
|
|
q2 *= quatMagInv;
|
|
|
|
q3 *= quatMagInv;
|
|
|
|
q4 *= quatMagInv;
|
2016-05-04 10:33:57 -03:00
|
|
|
}
|
2013-12-30 02:31:51 -04:00
|
|
|
}
|
2014-10-13 00:41:13 -03:00
|
|
|
|
2016-05-04 10:33:57 -03:00
|
|
|
Quaternion Quaternion::operator*(const Quaternion &v) const
|
|
|
|
{
|
2014-10-13 00:41:13 -03:00
|
|
|
Quaternion ret;
|
2015-04-08 00:58:48 -03:00
|
|
|
const float &w1 = q1;
|
|
|
|
const float &x1 = q2;
|
|
|
|
const float &y1 = q3;
|
|
|
|
const float &z1 = q4;
|
2014-10-13 00:41:13 -03:00
|
|
|
|
2014-10-19 16:13:53 -03:00
|
|
|
float w2 = v.q1;
|
|
|
|
float x2 = v.q2;
|
|
|
|
float y2 = v.q3;
|
|
|
|
float z2 = v.q4;
|
2014-10-13 00:41:13 -03:00
|
|
|
|
|
|
|
ret.q1 = w1*w2 - x1*x2 - y1*y2 - z1*z2;
|
|
|
|
ret.q2 = w1*x2 + x1*w2 + y1*z2 - z1*y2;
|
|
|
|
ret.q3 = w1*y2 - x1*z2 + y1*w2 + z1*x2;
|
|
|
|
ret.q4 = w1*z2 + x1*y2 - y1*x2 + z1*w2;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-05-04 10:33:57 -03:00
|
|
|
Quaternion &Quaternion::operator*=(const Quaternion &v)
|
|
|
|
{
|
2015-12-10 19:05:22 -04:00
|
|
|
float w1 = q1;
|
|
|
|
float x1 = q2;
|
|
|
|
float y1 = q3;
|
|
|
|
float z1 = q4;
|
2014-10-13 00:41:13 -03:00
|
|
|
|
2014-10-19 16:13:53 -03:00
|
|
|
float w2 = v.q1;
|
|
|
|
float x2 = v.q2;
|
|
|
|
float y2 = v.q3;
|
|
|
|
float z2 = v.q4;
|
2014-10-13 00:41:13 -03:00
|
|
|
|
|
|
|
q1 = w1*w2 - x1*x2 - y1*y2 - z1*z2;
|
|
|
|
q2 = w1*x2 + x1*w2 + y1*z2 - z1*y2;
|
|
|
|
q3 = w1*y2 - x1*z2 + y1*w2 + z1*x2;
|
|
|
|
q4 = w1*z2 + x1*y2 - y1*x2 + z1*w2;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
2015-01-30 18:14:46 -04:00
|
|
|
|
2016-05-04 10:33:57 -03:00
|
|
|
Quaternion Quaternion::operator/(const Quaternion &v) const
|
|
|
|
{
|
2015-01-30 18:14:46 -04:00
|
|
|
Quaternion ret;
|
2015-04-08 00:58:48 -03:00
|
|
|
const float &quat0 = q1;
|
|
|
|
const float &quat1 = q2;
|
|
|
|
const float &quat2 = q3;
|
|
|
|
const float &quat3 = q4;
|
2015-01-30 18:14:46 -04:00
|
|
|
|
|
|
|
float rquat0 = v.q1;
|
|
|
|
float rquat1 = v.q2;
|
|
|
|
float rquat2 = v.q3;
|
|
|
|
float rquat3 = v.q4;
|
|
|
|
|
|
|
|
ret.q1 = (rquat0*quat0 + rquat1*quat1 + rquat2*quat2 + rquat3*quat3);
|
2015-01-31 07:03:07 -04:00
|
|
|
ret.q2 = (rquat0*quat1 - rquat1*quat0 - rquat2*quat3 + rquat3*quat2);
|
|
|
|
ret.q3 = (rquat0*quat2 + rquat1*quat3 - rquat2*quat0 - rquat3*quat1);
|
|
|
|
ret.q4 = (rquat0*quat3 - rquat1*quat2 + rquat2*quat1 - rquat3*quat0);
|
2015-01-30 18:14:46 -04:00
|
|
|
return ret;
|
|
|
|
}
|