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/>.
|
|
|
|
*/
|
|
|
|
|
2019-09-27 17:45:12 -03:00
|
|
|
#pragma GCC optimize("O2")
|
2015-10-19 18:27:22 -03:00
|
|
|
|
2012-03-10 01:44:22 -04:00
|
|
|
#include "AP_Math.h"
|
2020-03-26 01:31:11 -03:00
|
|
|
#include <AP_InternalError/AP_InternalError.h>
|
2012-03-10 01:44:22 -04:00
|
|
|
|
2021-07-23 19:09:46 -03:00
|
|
|
#define HALF_SQRT_2_PlUS_SQRT_2 0.92387953251128673848313610506011 // sqrt(2 + sqrt(2)) / 2
|
|
|
|
#define HALF_SQRT_2_MINUS_SQTR_2 0.38268343236508972626808144923416 // sqrt(2 - sqrt(2)) / 2
|
|
|
|
#define HALF_SQRT_HALF_TIMES_TWO_PLUS_SQRT_TWO 0.65328148243818828788676000840496 // sqrt((2 + sqrt(2))/2) / 2
|
|
|
|
#define HALF_SQRT_HALF_TIMES_TWO_MINUS_SQRT_TWO 0.27059805007309845059637609665515 // sqrt((2 - sqrt(2))/2) / 2
|
|
|
|
|
2012-03-10 01:44:22 -04:00
|
|
|
// return the rotation matrix equivalent for this quaternion
|
2021-05-04 08:12:23 -03:00
|
|
|
template <typename T>
|
|
|
|
void QuaternionT<T>::rotation_matrix(Matrix3d &m) const
|
|
|
|
{
|
|
|
|
const T q3q3 = q3 * q3;
|
|
|
|
const T q3q4 = q3 * q4;
|
|
|
|
const T q2q2 = q2 * q2;
|
|
|
|
const T q2q3 = q2 * q3;
|
|
|
|
const T q2q4 = q2 * q4;
|
|
|
|
const T q1q2 = q1 * q2;
|
|
|
|
const T q1q3 = q1 * q3;
|
|
|
|
const T q1q4 = q1 * q4;
|
|
|
|
const T q4q4 = q4 * q4;
|
|
|
|
|
|
|
|
m.a.x = 1.0f-2.0f*(q3q3 + q4q4);
|
|
|
|
m.a.y = 2.0f*(q2q3 - q1q4);
|
|
|
|
m.a.z = 2.0f*(q2q4 + q1q3);
|
|
|
|
m.b.x = 2.0f*(q2q3 + q1q4);
|
|
|
|
m.b.y = 1.0f-2.0f*(q2q2 + q4q4);
|
|
|
|
m.b.z = 2.0f*(q3q4 - q1q2);
|
|
|
|
m.c.x = 2.0f*(q2q4 - q1q3);
|
|
|
|
m.c.y = 2.0f*(q3q4 + q1q2);
|
|
|
|
m.c.z = 1.0f-2.0f*(q2q2 + q3q3);
|
|
|
|
}
|
|
|
|
|
2021-08-17 05:32:49 -03:00
|
|
|
// populate the supplied rotation matrix equivalent from this quaternion
|
2021-05-04 08:12:23 -03:00
|
|
|
template <typename T>
|
|
|
|
void QuaternionT<T>::rotation_matrix(Matrix3f &m) const
|
|
|
|
{
|
|
|
|
const T q3q3 = q3 * q3;
|
|
|
|
const T q3q4 = q3 * q4;
|
|
|
|
const T q2q2 = q2 * q2;
|
|
|
|
const T q2q3 = q2 * q3;
|
|
|
|
const T q2q4 = q2 * q4;
|
|
|
|
const T q1q2 = q1 * q2;
|
|
|
|
const T q1q3 = q1 * q3;
|
|
|
|
const T q1q4 = q1 * q4;
|
|
|
|
const T q4q4 = q4 * q4;
|
2012-03-10 01:44:22 -04:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-08-17 05:32:49 -03:00
|
|
|
// make this quaternion equivalent to the supplied matrix
|
2014-02-14 21:20:31 -04:00
|
|
|
// Thanks to Martin John Baker
|
|
|
|
// http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
|
2021-05-04 08:12:23 -03:00
|
|
|
template <typename T>
|
|
|
|
void QuaternionT<T>::from_rotation_matrix(const Matrix3<T> &m)
|
|
|
|
{
|
|
|
|
const T &m00 = m.a.x;
|
|
|
|
const T &m11 = m.b.y;
|
|
|
|
const T &m22 = m.c.z;
|
|
|
|
const T &m10 = m.b.x;
|
|
|
|
const T &m01 = m.a.y;
|
|
|
|
const T &m20 = m.c.x;
|
|
|
|
const T &m02 = m.a.z;
|
|
|
|
const T &m21 = m.c.y;
|
|
|
|
const T &m12 = m.b.z;
|
|
|
|
T &qw = q1;
|
|
|
|
T &qx = q2;
|
|
|
|
T &qy = q3;
|
|
|
|
T &qz = q4;
|
|
|
|
|
|
|
|
const T tr = m00 + m11 + m22;
|
2014-02-14 21:20:31 -04:00
|
|
|
|
|
|
|
if (tr > 0) {
|
2021-05-04 08:12:23 -03:00
|
|
|
const T S = sqrtF(tr+1) * 2;
|
2014-02-14 21:20:31 -04:00
|
|
|
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)) {
|
2021-05-04 08:12:23 -03:00
|
|
|
const T 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) {
|
2021-05-04 08:12:23 -03:00
|
|
|
const T 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 {
|
2021-05-04 08:12:23 -03:00
|
|
|
const T 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 01:31:11 -03:00
|
|
|
// create a quaternion from a given rotation
|
2021-05-04 08:12:23 -03:00
|
|
|
template <typename T>
|
|
|
|
void QuaternionT<T>::from_rotation(enum Rotation rotation)
|
2020-03-26 01:31:11 -03:00
|
|
|
{
|
|
|
|
// the constants below can be calculated using the following formula:
|
|
|
|
// Matrix3f m_from_rot;
|
|
|
|
// m_from_rot.from_rotation(rotation);
|
|
|
|
// Quaternion q_from_m;
|
|
|
|
// from_rotation_matrix(m_from_rot);
|
|
|
|
|
|
|
|
switch (rotation) {
|
|
|
|
case ROTATION_NONE:
|
|
|
|
q1 = 1;
|
|
|
|
q2 = q3 = q4 = 0;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_YAW_45:
|
2021-07-23 19:09:46 -03:00
|
|
|
q1 = HALF_SQRT_2_PlUS_SQRT_2;
|
2020-03-26 01:31:11 -03:00
|
|
|
q2 = q3 = 0;
|
2021-07-23 19:09:46 -03:00
|
|
|
q4 = HALF_SQRT_2_MINUS_SQTR_2;
|
2020-03-26 01:31:11 -03:00
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_YAW_90:
|
|
|
|
q1 = HALF_SQRT_2;
|
|
|
|
q2 = q3 = 0;
|
|
|
|
q4 = HALF_SQRT_2;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_YAW_135:
|
2021-07-23 19:09:46 -03:00
|
|
|
q1 = HALF_SQRT_2_MINUS_SQTR_2;
|
2020-03-26 01:31:11 -03:00
|
|
|
q2 = q3 = 0;
|
2021-07-23 19:09:46 -03:00
|
|
|
q4 = HALF_SQRT_2_PlUS_SQRT_2;
|
2020-03-26 01:31:11 -03:00
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_YAW_180:
|
|
|
|
q1 = q2 = q3 = 0;
|
|
|
|
q4=1;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_YAW_225:
|
2021-07-23 19:09:46 -03:00
|
|
|
q1 = -HALF_SQRT_2_MINUS_SQTR_2;
|
2020-03-26 01:31:11 -03:00
|
|
|
q2 = q3 = 0;
|
2021-07-23 19:09:46 -03:00
|
|
|
q4 = HALF_SQRT_2_PlUS_SQRT_2;
|
2020-03-26 01:31:11 -03:00
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_YAW_270:
|
|
|
|
q1 = HALF_SQRT_2;
|
|
|
|
q2 = q3 = 0;
|
|
|
|
q4 = -HALF_SQRT_2;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_YAW_315:
|
2021-07-23 19:09:46 -03:00
|
|
|
q1 = HALF_SQRT_2_PlUS_SQRT_2;
|
2020-03-26 01:31:11 -03:00
|
|
|
q2 = q3 = 0;
|
2021-07-23 19:09:46 -03:00
|
|
|
q4 = -HALF_SQRT_2_MINUS_SQTR_2;
|
2020-03-26 01:31:11 -03:00
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_180:
|
|
|
|
q1 = q3 = q4 = 0;
|
|
|
|
q2 = 1;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_180_YAW_45:
|
|
|
|
q1 = q4 = 0;
|
2021-07-23 19:09:46 -03:00
|
|
|
q2 = HALF_SQRT_2_PlUS_SQRT_2;
|
|
|
|
q3 = HALF_SQRT_2_MINUS_SQTR_2;
|
2020-03-26 01:31:11 -03:00
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_180_YAW_90:
|
2021-07-23 16:47:21 -03:00
|
|
|
case ROTATION_PITCH_180_YAW_270:
|
2020-03-26 01:31:11 -03:00
|
|
|
q1 = q4 = 0;
|
|
|
|
q2 = q3 = HALF_SQRT_2;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_180_YAW_135:
|
|
|
|
q1 = q4 = 0;
|
2021-07-23 19:09:46 -03:00
|
|
|
q2 = HALF_SQRT_2_MINUS_SQTR_2;
|
|
|
|
q3 = HALF_SQRT_2_PlUS_SQRT_2;
|
2020-03-26 01:31:11 -03:00
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_PITCH_180:
|
2020-04-07 23:38:42 -03:00
|
|
|
q1 = q2 = q4 = 0;
|
|
|
|
q3 = 1;
|
2020-03-26 01:31:11 -03:00
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_180_YAW_225:
|
|
|
|
q1 = q4 = 0;
|
2021-07-23 19:09:46 -03:00
|
|
|
q2 = -HALF_SQRT_2_MINUS_SQTR_2;
|
|
|
|
q3 = HALF_SQRT_2_PlUS_SQRT_2;
|
2020-03-26 01:31:11 -03:00
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_180_YAW_270:
|
2021-07-23 16:47:21 -03:00
|
|
|
case ROTATION_PITCH_180_YAW_90:
|
2020-03-26 01:31:11 -03:00
|
|
|
q1 = q4 = 0;
|
|
|
|
q2 = -HALF_SQRT_2;
|
|
|
|
q3 = HALF_SQRT_2;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_180_YAW_315:
|
|
|
|
q1 = q4 = 0;
|
2021-07-23 19:09:46 -03:00
|
|
|
q2 = HALF_SQRT_2_PlUS_SQRT_2;
|
|
|
|
q3 = -HALF_SQRT_2_MINUS_SQTR_2;
|
2020-03-26 01:31:11 -03:00
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_90:
|
|
|
|
q1 = q2 = HALF_SQRT_2;
|
|
|
|
q3 = q4 = 0;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_90_YAW_45:
|
2021-07-23 19:09:46 -03:00
|
|
|
q1 = HALF_SQRT_HALF_TIMES_TWO_PLUS_SQRT_TWO;
|
|
|
|
q2 = HALF_SQRT_HALF_TIMES_TWO_PLUS_SQRT_TWO;
|
|
|
|
q3 = q4 = HALF_SQRT_HALF_TIMES_TWO_MINUS_SQRT_TWO;
|
2020-03-26 01:31:11 -03:00
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_90_YAW_90:
|
|
|
|
q1 = q2 = q3 = q4 = 0.5f;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_90_YAW_135:
|
2021-07-23 19:09:46 -03:00
|
|
|
q1 = q2 = HALF_SQRT_HALF_TIMES_TWO_MINUS_SQRT_TWO;
|
|
|
|
q3 = HALF_SQRT_HALF_TIMES_TWO_PLUS_SQRT_TWO;
|
|
|
|
q4 = HALF_SQRT_HALF_TIMES_TWO_PLUS_SQRT_TWO;
|
2020-03-26 01:31:11 -03:00
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_270:
|
|
|
|
q1 = HALF_SQRT_2;
|
|
|
|
q2 = -HALF_SQRT_2;
|
|
|
|
q3 = q4 = 0;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_270_YAW_45:
|
2021-07-23 19:09:46 -03:00
|
|
|
q1 = HALF_SQRT_HALF_TIMES_TWO_PLUS_SQRT_TWO;
|
|
|
|
q2 = -HALF_SQRT_HALF_TIMES_TWO_PLUS_SQRT_TWO;
|
|
|
|
q3 = -HALF_SQRT_HALF_TIMES_TWO_MINUS_SQRT_TWO;
|
|
|
|
q4 = HALF_SQRT_HALF_TIMES_TWO_MINUS_SQRT_TWO;
|
2020-03-26 01:31:11 -03:00
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_270_YAW_90:
|
|
|
|
q1 = q4 = 0.5f;
|
|
|
|
q2 = q3 = -0.5f;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_270_YAW_135:
|
2021-07-23 19:09:46 -03:00
|
|
|
q1 = HALF_SQRT_HALF_TIMES_TWO_MINUS_SQRT_TWO;
|
|
|
|
q2 = -HALF_SQRT_HALF_TIMES_TWO_MINUS_SQRT_TWO;
|
|
|
|
q3 = -HALF_SQRT_HALF_TIMES_TWO_PLUS_SQRT_TWO;
|
|
|
|
q4 = HALF_SQRT_HALF_TIMES_TWO_PLUS_SQRT_TWO;
|
2020-03-26 01:31:11 -03:00
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_PITCH_90:
|
|
|
|
q1 = q3 = HALF_SQRT_2;
|
|
|
|
q2 = q4 = 0;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_PITCH_270:
|
|
|
|
q1 = HALF_SQRT_2;
|
|
|
|
q2 = q4 = 0;
|
|
|
|
q3 = -HALF_SQRT_2;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_90_PITCH_90:
|
|
|
|
q1 = q2 = q3 = -0.5f;
|
|
|
|
q4 = 0.5f;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_180_PITCH_90:
|
|
|
|
q1 = q3 = 0;
|
|
|
|
q2 = -HALF_SQRT_2;
|
|
|
|
q4 = HALF_SQRT_2;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_270_PITCH_90:
|
|
|
|
q1 = q3 = q4 = 0.5f;
|
|
|
|
q2 = -0.5f;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_90_PITCH_180:
|
|
|
|
q1 = q2 = 0;
|
|
|
|
q3 = -HALF_SQRT_2;
|
|
|
|
q4 = HALF_SQRT_2;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_270_PITCH_180:
|
|
|
|
q1 = q2 = 0;
|
|
|
|
q3 = q4 = HALF_SQRT_2;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_90_PITCH_270:
|
|
|
|
q1 = q2 = q4 = 0.5f;
|
|
|
|
q3 = -0.5;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_180_PITCH_270:
|
|
|
|
q1 = q3 = 0;
|
|
|
|
q2 = q4 = HALF_SQRT_2;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_270_PITCH_270:
|
|
|
|
q1 = -0.5f;
|
|
|
|
q2 = q3 = q4 = 0.5f;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_90_PITCH_180_YAW_90:
|
|
|
|
q1 = q3 = -0.5f;
|
|
|
|
q2 = q4 = 0.5f;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_90_YAW_270:
|
|
|
|
q1 = q2 = -0.5f;
|
|
|
|
q3 = q4 = 0.5f;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_90_PITCH_68_YAW_293:
|
2021-07-23 19:09:46 -03:00
|
|
|
q1 = 0.26774500501681575137524760066299;
|
|
|
|
q2 = 0.70698804688952421315661922562867;
|
|
|
|
q3 = 0.012957683254962659713527273197542;
|
|
|
|
q4 = -0.65445596665363614530264158020145;
|
2020-03-26 01:31:11 -03:00
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_PITCH_315:
|
2021-07-23 19:09:46 -03:00
|
|
|
q1 = HALF_SQRT_2_PlUS_SQRT_2;
|
2020-03-26 01:31:11 -03:00
|
|
|
q2 = q4 = 0;
|
2021-07-23 19:09:46 -03:00
|
|
|
q3 = -HALF_SQRT_2_MINUS_SQTR_2;
|
2020-03-26 01:31:11 -03:00
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_90_PITCH_315:
|
2021-07-23 19:09:46 -03:00
|
|
|
q1 = HALF_SQRT_HALF_TIMES_TWO_PLUS_SQRT_TWO;
|
|
|
|
q2 = HALF_SQRT_HALF_TIMES_TWO_PLUS_SQRT_TWO;
|
|
|
|
q3 = -HALF_SQRT_HALF_TIMES_TWO_MINUS_SQRT_TWO;
|
|
|
|
q4 = HALF_SQRT_HALF_TIMES_TWO_MINUS_SQRT_TWO;
|
2020-03-26 01:31:11 -03:00
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_PITCH_7:
|
2021-07-23 19:09:46 -03:00
|
|
|
q1 = 0.99813479842186692003735970502021;
|
2020-03-26 01:31:11 -03:00
|
|
|
q2 = q4 = 0;
|
2021-07-23 19:09:46 -03:00
|
|
|
q3 = 0.061048539534856872956769535676358;
|
2020-03-26 01:31:11 -03:00
|
|
|
return;
|
|
|
|
|
2021-07-23 16:45:00 -03:00
|
|
|
case ROTATION_ROLL_45:
|
2021-07-23 19:09:46 -03:00
|
|
|
q1 = HALF_SQRT_2_PlUS_SQRT_2;
|
|
|
|
q2 = HALF_SQRT_2_MINUS_SQTR_2;
|
2021-07-23 16:45:00 -03:00
|
|
|
q3 = q4 = 0.0;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case ROTATION_ROLL_315:
|
2021-07-23 19:09:46 -03:00
|
|
|
q1 = HALF_SQRT_2_PlUS_SQRT_2;
|
|
|
|
q2 = -HALF_SQRT_2_MINUS_SQTR_2;
|
2021-07-23 16:45:00 -03:00
|
|
|
q3 = q4 = 0.0;
|
|
|
|
return;
|
|
|
|
|
2020-03-26 01:31:11 -03:00
|
|
|
case ROTATION_CUSTOM:
|
2020-04-20 10:45:33 -03:00
|
|
|
// Error; custom rotations not supported
|
2020-04-29 21:40:46 -03:00
|
|
|
INTERNAL_ERROR(AP_InternalError::error_t::flow_of_control);
|
2020-03-26 01:31:11 -03:00
|
|
|
return;
|
2020-04-20 10:45:33 -03:00
|
|
|
|
|
|
|
case ROTATION_MAX:
|
|
|
|
break;
|
2020-03-26 01:31:11 -03:00
|
|
|
}
|
2020-04-20 10:45:33 -03:00
|
|
|
// rotation invalid
|
2020-04-29 21:40:46 -03:00
|
|
|
INTERNAL_ERROR(AP_InternalError::error_t::bad_rotation);
|
2020-03-26 01:31:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// rotate this quaternion by the given rotation
|
2021-05-04 08:12:23 -03:00
|
|
|
template <typename T>
|
|
|
|
void QuaternionT<T>::rotate(enum Rotation rotation)
|
2020-03-26 01:31:11 -03:00
|
|
|
{
|
|
|
|
// create quaternion from rotation matrix
|
2021-05-04 08:12:23 -03:00
|
|
|
QuaternionT<T> q_from_rot;
|
2020-03-26 01:31:11 -03:00
|
|
|
q_from_rot.from_rotation(rotation);
|
|
|
|
|
|
|
|
// rotate this quaternion
|
|
|
|
*this *= q_from_rot;
|
|
|
|
}
|
|
|
|
|
2012-03-10 01:44:22 -04:00
|
|
|
// convert a vector from earth to body frame
|
2021-05-04 08:12:23 -03:00
|
|
|
template <typename T>
|
|
|
|
void QuaternionT<T>::earth_to_body(Vector3<T> &v) const
|
2012-03-10 01:44:22 -04:00
|
|
|
{
|
2021-05-04 08:12:23 -03:00
|
|
|
Matrix3<T> m;
|
2012-03-10 01:44:22 -04:00
|
|
|
rotation_matrix(m);
|
|
|
|
v = m * v;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a quaternion from Euler angles
|
2021-05-04 08:12:23 -03:00
|
|
|
template <typename T>
|
|
|
|
void QuaternionT<T>::from_euler(T roll, T pitch, T yaw)
|
2012-03-10 01:44:22 -04:00
|
|
|
{
|
2021-05-04 08:12:23 -03:00
|
|
|
const T cr2 = cosF(roll*0.5);
|
|
|
|
const T cp2 = cosF(pitch*0.5);
|
|
|
|
const T cy2 = cosF(yaw*0.5);
|
|
|
|
const T sr2 = sinF(roll*0.5);
|
|
|
|
const T sp2 = sinF(pitch*0.5);
|
|
|
|
const T sy2 = sinF(yaw*0.5);
|
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;
|
|
|
|
}
|
2021-07-23 02:44:19 -03:00
|
|
|
template <typename T>
|
|
|
|
void QuaternionT<T>::from_euler(const Vector3<T> &v)
|
|
|
|
{
|
|
|
|
from_euler(v[0], v[1], v[2]);
|
|
|
|
}
|
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
|
|
|
template <typename T>
|
|
|
|
void QuaternionT<T>::from_vector312(T roll, T pitch, T yaw)
|
2015-04-08 00:59:38 -03:00
|
|
|
{
|
2021-05-04 08:12:23 -03:00
|
|
|
Matrix3<T> 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);
|
|
|
|
}
|
|
|
|
|
2020-04-02 00:12:11 -03:00
|
|
|
// create a quaternion from its axis-angle representation
|
2021-05-04 08:12:23 -03:00
|
|
|
template <typename T>
|
|
|
|
void QuaternionT<T>::from_axis_angle(Vector3<T> v)
|
2016-05-04 10:33:57 -03:00
|
|
|
{
|
2021-05-04 08:12:23 -03:00
|
|
|
const T 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);
|
|
|
|
}
|
|
|
|
|
2020-04-02 00:12:11 -03:00
|
|
|
// create a quaternion from its axis-angle representation
|
|
|
|
// the axis vector must be length 1, theta is in radians
|
2021-05-04 08:12:23 -03:00
|
|
|
template <typename T>
|
|
|
|
void QuaternionT<T>::from_axis_angle(const Vector3<T> &axis, T theta)
|
2016-05-04 10:33:57 -03:00
|
|
|
{
|
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
|
|
|
}
|
2021-07-08 23:20:50 -03:00
|
|
|
const T st2 = sinF(0.5*theta);
|
2014-10-13 00:41:13 -03:00
|
|
|
|
2021-07-08 23:20:50 -03:00
|
|
|
q1 = cosF(0.5*theta);
|
2014-10-13 00:41:13 -03:00
|
|
|
q2 = axis.x * st2;
|
|
|
|
q3 = axis.y * st2;
|
|
|
|
q4 = axis.z * st2;
|
|
|
|
}
|
|
|
|
|
2020-04-02 00:12:11 -03:00
|
|
|
// rotate by the provided axis angle
|
2021-05-04 08:12:23 -03:00
|
|
|
template <typename T>
|
|
|
|
void QuaternionT<T>::rotate(const Vector3<T> &v)
|
2016-05-04 10:33:57 -03:00
|
|
|
{
|
2021-05-04 08:12:23 -03:00
|
|
|
QuaternionT<T> r;
|
2014-10-13 00:41:13 -03:00
|
|
|
r.from_axis_angle(v);
|
|
|
|
(*this) *= r;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
template <typename T>
|
|
|
|
void QuaternionT<T>::to_axis_angle(Vector3<T> &v) const
|
2016-05-04 10:33:57 -03:00
|
|
|
{
|
2021-05-04 08:12:23 -03:00
|
|
|
const T l = sqrtF(sq(q2)+sq(q3)+sq(q4));
|
|
|
|
v = Vector3<T>(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;
|
2021-07-08 23:20:50 -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
|
|
|
|
2020-04-02 00:12:11 -03:00
|
|
|
// create a quaternion from its axis-angle representation
|
|
|
|
// 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
|
|
|
template <typename T>
|
|
|
|
void QuaternionT<T>::from_axis_angle_fast(Vector3<T> v)
|
2016-05-04 10:33:57 -03:00
|
|
|
{
|
2021-05-04 08:12:23 -03:00
|
|
|
const T 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);
|
|
|
|
}
|
|
|
|
|
2020-04-02 00:12:11 -03:00
|
|
|
// create a quaternion from its axis-angle representation
|
|
|
|
// theta should less than 0.17 radians (i.e. 10 degrees)
|
2021-05-04 08:12:23 -03:00
|
|
|
template <typename T>
|
|
|
|
void QuaternionT<T>::from_axis_angle_fast(const Vector3<T> &axis, T theta)
|
2016-05-04 10:33:57 -03:00
|
|
|
{
|
2021-07-08 23:20:50 -03:00
|
|
|
const T t2 = 0.5*theta;
|
2021-05-04 08:12:23 -03:00
|
|
|
const T sqt2 = sq(t2);
|
|
|
|
const T st2 = t2-sqt2*t2/6.0f;
|
2014-10-13 00:41:13 -03:00
|
|
|
|
2021-07-08 23:20:50 -03:00
|
|
|
q1 = 1.0f-(0.5*sqt2)+sq(sqt2)/24.0f;
|
2014-10-13 00:41:13 -03:00
|
|
|
q2 = axis.x * st2;
|
|
|
|
q3 = axis.y * st2;
|
|
|
|
q4 = axis.z * st2;
|
|
|
|
}
|
|
|
|
|
2020-04-02 00:12:11 -03:00
|
|
|
// rotate by the provided axis angle
|
|
|
|
// 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
|
|
|
template <typename T>
|
|
|
|
void QuaternionT<T>::rotate_fast(const Vector3<T> &v)
|
2016-05-04 10:33:57 -03:00
|
|
|
{
|
2021-05-04 08:12:23 -03:00
|
|
|
const T theta = v.length();
|
2016-06-24 04:14:52 -03:00
|
|
|
if (is_zero(theta)) {
|
2016-05-04 10:33:57 -03:00
|
|
|
return;
|
|
|
|
}
|
2021-07-08 23:20:50 -03:00
|
|
|
const T t2 = 0.5*theta;
|
2021-05-04 08:12:23 -03:00
|
|
|
const T sqt2 = sq(t2);
|
|
|
|
T st2 = t2-sqt2*t2/6.0f;
|
2014-10-13 00:41:13 -03:00
|
|
|
st2 /= theta;
|
|
|
|
|
|
|
|
//"rotation quaternion"
|
2021-07-08 23:20:50 -03:00
|
|
|
const T w2 = 1.0f-(0.5*sqt2)+sq(sqt2)/24.0f;
|
2021-05-04 08:12:23 -03:00
|
|
|
const T x2 = v.x * st2;
|
|
|
|
const T y2 = v.y * st2;
|
|
|
|
const T z2 = v.z * st2;
|
2014-10-13 00:41:13 -03:00
|
|
|
|
|
|
|
//copy our quaternion
|
2021-05-04 08:12:23 -03:00
|
|
|
const T w1 = q1;
|
|
|
|
const T x1 = q2;
|
|
|
|
const T y1 = q3;
|
|
|
|
const T z1 = q4;
|
2014-10-13 00:41:13 -03:00
|
|
|
|
|
|
|
//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
|
2021-05-04 08:12:23 -03:00
|
|
|
template <typename T>
|
|
|
|
T QuaternionT<T>::get_euler_roll() const
|
2015-04-23 16:44:49 -03:00
|
|
|
{
|
2021-07-08 23:20:50 -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
|
2021-05-04 08:12:23 -03:00
|
|
|
template <typename T>
|
|
|
|
T QuaternionT<T>::get_euler_pitch() const
|
2015-04-23 16:44:49 -03:00
|
|
|
{
|
|
|
|
return safe_asin(2.0f*(q1*q3 - q4*q2));
|
|
|
|
}
|
|
|
|
|
|
|
|
// get euler yaw angle
|
2021-05-04 08:12:23 -03:00
|
|
|
template <typename T>
|
|
|
|
T QuaternionT<T>::get_euler_yaw() const
|
2015-04-23 16:44:49 -03:00
|
|
|
{
|
2021-07-08 23:20:50 -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
|
2021-05-04 08:12:23 -03:00
|
|
|
template <typename T>
|
|
|
|
void QuaternionT<T>::to_euler(double &roll, double &pitch, double &yaw) const
|
|
|
|
{
|
|
|
|
roll = get_euler_roll();
|
|
|
|
pitch = get_euler_pitch();
|
|
|
|
yaw = get_euler_yaw();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void QuaternionT<T>::to_euler(float &roll, float &pitch, float &yaw) const
|
2014-10-13 00:41:13 -03:00
|
|
|
{
|
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
|
2021-05-04 08:12:23 -03:00
|
|
|
template <typename T>
|
|
|
|
Vector3<T> QuaternionT<T>::to_vector312(void) const
|
2016-05-04 10:33:57 -03:00
|
|
|
{
|
2021-05-04 08:12:23 -03:00
|
|
|
Matrix3<T> m;
|
2015-04-08 00:59:38 -03:00
|
|
|
rotation_matrix(m);
|
2015-05-24 20:08:31 -03:00
|
|
|
return m.to_euler312();
|
2015-04-08 00:59:38 -03:00
|
|
|
}
|
|
|
|
|
2021-05-04 08:12:23 -03:00
|
|
|
template <typename T>
|
|
|
|
T QuaternionT<T>::length(void) const
|
2013-12-30 02:31:51 -04:00
|
|
|
{
|
2021-05-04 08:12:23 -03:00
|
|
|
return sqrtF(sq(q1) + sq(q2) + sq(q3) + sq(q4));
|
2013-12-30 02:31:51 -04:00
|
|
|
}
|
|
|
|
|
2020-03-26 01:31:11 -03:00
|
|
|
// return the reverse rotation of this quaternion
|
2021-05-04 08:12:23 -03:00
|
|
|
template <typename T>
|
|
|
|
QuaternionT<T> QuaternionT<T>::inverse(void) const
|
2014-10-13 00:41:13 -03:00
|
|
|
{
|
2021-05-04 08:12:23 -03:00
|
|
|
return QuaternionT<T>(q1, -q2, -q3, -q4);
|
2014-10-13 00:41:13 -03:00
|
|
|
}
|
|
|
|
|
2020-03-26 01:31:11 -03:00
|
|
|
// reverse the rotation of this quaternion
|
2021-05-04 08:12:23 -03:00
|
|
|
template <typename T>
|
|
|
|
void QuaternionT<T>::invert()
|
2020-03-26 01:31:11 -03:00
|
|
|
{
|
|
|
|
q2 = -q2;
|
|
|
|
q3 = -q3;
|
|
|
|
q4 = -q4;
|
|
|
|
}
|
|
|
|
|
2021-05-04 08:12:23 -03:00
|
|
|
template <typename T>
|
|
|
|
void QuaternionT<T>::normalize(void)
|
2013-12-30 02:31:51 -04:00
|
|
|
{
|
2021-05-04 08:12:23 -03:00
|
|
|
const T quatMag = length();
|
2016-06-24 04:14:52 -03:00
|
|
|
if (!is_zero(quatMag)) {
|
2021-05-04 08:12:23 -03:00
|
|
|
const T quatMagInv = 1.0f/quatMag;
|
2013-12-30 02:31:51 -04:00
|
|
|
q1 *= quatMagInv;
|
|
|
|
q2 *= quatMagInv;
|
|
|
|
q3 *= quatMagInv;
|
|
|
|
q4 *= quatMagInv;
|
2021-07-09 00:59:55 -03:00
|
|
|
} else {
|
|
|
|
// The code goes here if the quaternion is [0,0,0,0]. This shouldn't happen.
|
|
|
|
INTERNAL_ERROR(AP_InternalError::error_t::flow_of_control);
|
2016-05-04 10:33:57 -03:00
|
|
|
}
|
2013-12-30 02:31:51 -04:00
|
|
|
}
|
2014-10-13 00:41:13 -03:00
|
|
|
|
2021-05-04 08:12:23 -03:00
|
|
|
template <typename T>
|
|
|
|
QuaternionT<T> QuaternionT<T>::operator*(const QuaternionT<T> &v) const
|
2016-05-04 10:33:57 -03:00
|
|
|
{
|
2021-05-04 08:12:23 -03:00
|
|
|
QuaternionT<T> ret;
|
|
|
|
const T &w1 = q1;
|
|
|
|
const T &x1 = q2;
|
|
|
|
const T &y1 = q3;
|
|
|
|
const T &z1 = q4;
|
2014-10-13 00:41:13 -03:00
|
|
|
|
2021-05-04 08:12:23 -03:00
|
|
|
const T w2 = v.q1;
|
|
|
|
const T x2 = v.q2;
|
|
|
|
const T y2 = v.q3;
|
|
|
|
const T 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;
|
|
|
|
}
|
|
|
|
|
2021-04-14 04:37:30 -03:00
|
|
|
// Optimized quaternion rotation operator, equivalent to converting
|
|
|
|
// (*this) to a rotation matrix then multiplying it to the argument `v`.
|
|
|
|
//
|
|
|
|
// 15 multiplies and 15 add / subtracts. Caches 3 floats
|
2021-05-04 08:12:23 -03:00
|
|
|
template <typename T>
|
|
|
|
Vector3<T> QuaternionT<T>::operator*(const Vector3<T> &v) const
|
2021-04-14 04:37:30 -03:00
|
|
|
{
|
|
|
|
// This uses the formula
|
|
|
|
//
|
|
|
|
// v2 = v1 + 2 q1 * qv x v1 + 2 qv x qv x v1
|
|
|
|
//
|
|
|
|
// where "x" is the cross product (explicitly inlined for performance below),
|
|
|
|
// "q1" is the scalar part and "qv" is the vector part of this quaternion
|
|
|
|
|
2021-05-04 08:12:23 -03:00
|
|
|
Vector3<T> ret = v;
|
2021-04-14 04:37:30 -03:00
|
|
|
|
|
|
|
// Compute and cache "qv x v1"
|
2021-05-04 08:12:23 -03:00
|
|
|
T uv[] = {q3 * v.z - q4 * v.y, q4 * v.x - q2 * v.z, q2 * v.y - q3 * v.x};
|
2021-04-14 04:37:30 -03:00
|
|
|
|
|
|
|
uv[0] += uv[0];
|
|
|
|
uv[1] += uv[1];
|
|
|
|
uv[2] += uv[2];
|
|
|
|
ret.x += q1 * uv[0] + q3 * uv[2] - q4 * uv[1];
|
|
|
|
ret.y += q1 * uv[1] + q4 * uv[0] - q2 * uv[2];
|
|
|
|
ret.z += q1 * uv[2] + q2 * uv[1] - q3 * uv[0];
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-05-04 08:12:23 -03:00
|
|
|
template <typename T>
|
|
|
|
QuaternionT<T> &QuaternionT<T>::operator*=(const QuaternionT<T> &v)
|
2016-05-04 10:33:57 -03:00
|
|
|
{
|
2021-05-04 08:12:23 -03:00
|
|
|
const T w1 = q1;
|
|
|
|
const T x1 = q2;
|
|
|
|
const T y1 = q3;
|
|
|
|
const T z1 = q4;
|
2014-10-13 00:41:13 -03:00
|
|
|
|
2021-05-04 08:12:23 -03:00
|
|
|
const T w2 = v.q1;
|
|
|
|
const T x2 = v.q2;
|
|
|
|
const T y2 = v.q3;
|
|
|
|
const T 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
|
|
|
|
2021-05-04 08:12:23 -03:00
|
|
|
template <typename T>
|
|
|
|
QuaternionT<T> QuaternionT<T>::operator/(const QuaternionT<T> &v) const
|
2016-05-04 10:33:57 -03:00
|
|
|
{
|
2021-05-04 08:12:23 -03:00
|
|
|
QuaternionT<T> ret;
|
|
|
|
const T &quat0 = q1;
|
|
|
|
const T &quat1 = q2;
|
|
|
|
const T &quat2 = q3;
|
|
|
|
const T &quat3 = q4;
|
2015-01-30 18:14:46 -04:00
|
|
|
|
2021-07-09 00:59:55 -03:00
|
|
|
const T quatMag = length();
|
|
|
|
if (is_zero(quatMag)) {
|
|
|
|
// The code goes here if the quaternion is [0,0,0,0]. This shouldn't happen.
|
|
|
|
INTERNAL_ERROR(AP_InternalError::error_t::flow_of_control);
|
|
|
|
}
|
|
|
|
|
2021-05-04 08:12:23 -03:00
|
|
|
const T rquat0 = v.q1;
|
|
|
|
const T rquat1 = v.q2;
|
|
|
|
const T rquat2 = v.q3;
|
|
|
|
const T rquat3 = v.q4;
|
2015-01-30 18:14:46 -04:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2019-02-20 01:17:23 -04:00
|
|
|
|
|
|
|
// angular difference in radians between quaternions
|
2021-05-04 08:12:23 -03:00
|
|
|
template <typename T>
|
|
|
|
QuaternionT<T> QuaternionT<T>::angular_difference(const QuaternionT<T> &v) const
|
2019-02-20 01:17:23 -04:00
|
|
|
{
|
|
|
|
return v.inverse() * *this;
|
|
|
|
}
|
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
|
|
|
template <typename T>
|
|
|
|
T QuaternionT<T>::roll_pitch_difference(const QuaternionT<T> &v) const
|
2021-06-02 02:08:11 -03:00
|
|
|
{
|
|
|
|
// convert Quaternions to rotation matrices
|
2021-05-04 08:12:23 -03:00
|
|
|
Matrix3<T> m, vm;
|
2021-06-02 02:08:11 -03:00
|
|
|
rotation_matrix(m);
|
|
|
|
v.rotation_matrix(vm);
|
|
|
|
|
|
|
|
// rotate earth frame vertical vector by each rotation matrix
|
2021-05-04 08:12:23 -03:00
|
|
|
const Vector3<T> z_unit_vec{0,0,1};
|
|
|
|
const Vector3<T> z_unit_m = m.mul_transpose(z_unit_vec);
|
|
|
|
const Vector3<T> z_unit_vm = vm.mul_transpose(z_unit_vec);
|
|
|
|
const Vector3<T> vec_diff = z_unit_vm - z_unit_m;
|
|
|
|
const T vec_len_div2 = constrain_float(vec_diff.length() * 0.5, 0.0, 1.0);
|
2021-06-02 02:08:11 -03:00
|
|
|
|
|
|
|
// calculate and return angular difference
|
2021-07-08 23:20:50 -03:00
|
|
|
return (2.0 * asinF(vec_len_div2));
|
2021-06-02 02:08:11 -03:00
|
|
|
}
|
2021-05-04 08:12:23 -03:00
|
|
|
|
|
|
|
// define for float and double
|
|
|
|
template class QuaternionT<float>;
|
|
|
|
template class QuaternionT<double>;
|