2012-01-06 22:15:48 +00:00
|
|
|
/* ResidualVM - A 3D game interpreter
|
2011-12-30 13:49:01 +00:00
|
|
|
*
|
2012-01-06 22:15:48 +00:00
|
|
|
* ResidualVM is the legal property of its developers, whose names
|
2011-12-30 13:49:01 +00:00
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
2012-12-19 22:15:43 +00: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 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2014-04-05 16:18:42 +00:00
|
|
|
*
|
2012-12-19 22:15:43 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2011-12-30 13:49:01 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-12-19 22:15:43 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2014-04-05 16:18:42 +00:00
|
|
|
*
|
2012-12-19 22:15:43 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2011-12-30 13:49:01 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-01-30 22:48:14 +00:00
|
|
|
/*
|
2015-01-17 20:08:27 +00:00
|
|
|
* Quaternion-math originally borrowed from plib http://plib.sourceforge.net/index.html
|
|
|
|
* This code was originally made available under the LGPLv2 license (or later).
|
|
|
|
*
|
|
|
|
* Quaternion routines are Copyright (C) 1999
|
|
|
|
* Kevin B. Thompson <kevinbthompson@yahoo.com>
|
|
|
|
* Modified by Sylvan W. Clebsch <sylvan@stanford.edu>
|
|
|
|
* Largely rewritten by "Negative0" <negative0@earthlink.net>
|
|
|
|
*
|
|
|
|
* This code (and our modifications) is made available here under the GPLv2 (or later).
|
|
|
|
*
|
|
|
|
* Additional changes written based on the math presented in
|
|
|
|
* http://www.swarthmore.edu/NatSci/mzucker1/e27/diebel2006attitude.pdf
|
|
|
|
*
|
2012-01-30 22:48:14 +00:00
|
|
|
*/
|
2011-12-30 13:49:01 +00:00
|
|
|
|
|
|
|
#include "common/streamdebug.h"
|
|
|
|
|
2014-06-20 11:42:52 +00:00
|
|
|
#include "common/math.h"
|
2011-12-30 13:49:01 +00:00
|
|
|
#include "math/quat.h"
|
|
|
|
|
|
|
|
namespace Math {
|
|
|
|
|
2014-06-20 11:42:52 +00:00
|
|
|
Quaternion::Quaternion(const Matrix3 &m) {
|
|
|
|
fromMatrix(m);
|
|
|
|
normalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
Quaternion::Quaternion(const Matrix4 &m) {
|
|
|
|
fromMatrix(m.getRotation());
|
|
|
|
}
|
|
|
|
|
|
|
|
Quaternion::Quaternion(const Vector3d &axis, const Angle &angle) {
|
|
|
|
float s = (angle / 2).getSine();
|
|
|
|
float c = (angle / 2).getCosine();
|
|
|
|
set(axis.x() * s, axis.y() * s, axis.z() * s, c);
|
|
|
|
}
|
2012-01-30 20:45:22 +00:00
|
|
|
|
2014-06-20 11:42:52 +00:00
|
|
|
Quaternion Quaternion::xAxis(const Angle &angle) {
|
|
|
|
Quaternion q(Vector3d(1.0f, 0.0f, 0.0f), angle);
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
|
|
|
Quaternion Quaternion::yAxis(const Angle &angle) {
|
|
|
|
Quaternion q(Vector3d(0.0f, 1.0f, 0.0f), angle);
|
|
|
|
return q;
|
|
|
|
}
|
2012-01-30 20:45:22 +00:00
|
|
|
|
2014-06-20 11:42:52 +00:00
|
|
|
Quaternion Quaternion::zAxis(const Angle &angle) {
|
|
|
|
Quaternion q(Vector3d(0.0f, 0.0f, 1.0f), angle);
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
2016-04-09 06:41:21 +00:00
|
|
|
Quaternion Quaternion::slerpQuat(const Quaternion& to, const float t) const {
|
2014-06-20 11:42:52 +00:00
|
|
|
Quaternion dst;
|
|
|
|
float scale0, scale1;
|
|
|
|
float flip = 1.0f;
|
|
|
|
float angle = this->dotProduct(to);
|
2012-01-30 20:45:22 +00:00
|
|
|
|
2014-06-20 11:42:52 +00:00
|
|
|
// Make sure the rotation is the short one
|
|
|
|
if (angle < 0.0f) {
|
|
|
|
angle = -angle;
|
|
|
|
flip = -1.0f;
|
2011-12-30 13:49:01 +00:00
|
|
|
}
|
2012-01-30 20:45:22 +00:00
|
|
|
|
2014-06-20 11:42:52 +00:00
|
|
|
// Spherical Interpolation
|
|
|
|
// Threshold of 1e-6
|
|
|
|
if (angle < 1.0f - (float) 1E-6f) {
|
|
|
|
float theta = acosf(angle);
|
|
|
|
float invSineTheta = 1.0f / sinf(theta);
|
2012-01-30 20:45:22 +00:00
|
|
|
|
2014-06-20 11:42:52 +00:00
|
|
|
scale0 = sinf((1.0f - t) * theta) * invSineTheta;
|
|
|
|
scale1 = (sinf(t * theta) * invSineTheta) * flip;
|
|
|
|
// Linear Interpolation
|
2011-12-30 13:49:01 +00:00
|
|
|
} else {
|
|
|
|
scale0 = 1.0f - t;
|
2014-06-20 11:42:52 +00:00
|
|
|
scale1 = t * flip;
|
2011-12-30 13:49:01 +00:00
|
|
|
}
|
2012-01-30 20:45:22 +00:00
|
|
|
|
2014-06-20 11:42:52 +00:00
|
|
|
// Apply the interpolation
|
|
|
|
dst = (*this * scale0) + (to * scale1);
|
2012-01-30 22:33:03 +00:00
|
|
|
return dst;
|
2011-12-30 13:49:01 +00:00
|
|
|
}
|
|
|
|
|
2014-06-20 11:42:52 +00:00
|
|
|
Quaternion& Quaternion::normalize() {
|
|
|
|
const float scale = sqrtf(square(x()) + square(y()) + square(z()) + square(w()));
|
|
|
|
|
|
|
|
// Already normalized if the scale is 1.0
|
2014-09-29 20:42:36 +00:00
|
|
|
if (scale != 1.0f && scale != 0.0f)
|
2014-06-20 11:42:52 +00:00
|
|
|
set(x() / scale, y() / scale, z() / scale, w() / scale);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-02-21 11:35:55 +00:00
|
|
|
void Quaternion::transform(Vector3d &v) const {
|
|
|
|
const Vector3d im = Vector3d(x(), y(), z());
|
|
|
|
v += 2.0 * Vector3d::crossProduct(im, Vector3d::crossProduct(im, v) + w() * v);
|
|
|
|
}
|
|
|
|
|
2014-06-20 11:42:52 +00:00
|
|
|
void Quaternion::fromMatrix(const Matrix3 &m) {
|
2014-12-25 20:35:30 +00:00
|
|
|
float qx, qy, qz, qw;
|
|
|
|
float tr = m.getValue(0, 0) + m.getValue(1, 1) + m.getValue(2, 2);
|
|
|
|
float s;
|
|
|
|
|
|
|
|
if (tr > 0.0f) {
|
|
|
|
s = sqrtf(tr + 1.0f);
|
|
|
|
qw = s * 0.5f;
|
|
|
|
s = 0.5f / s;
|
|
|
|
qx = (m.getValue(2, 1) - m.getValue(1, 2)) * s;
|
|
|
|
qy = (m.getValue(0, 2) - m.getValue(2, 0)) * s;
|
|
|
|
qz = (m.getValue(1, 0) - m.getValue(0, 1)) * s;
|
2014-06-20 11:42:52 +00:00
|
|
|
} else {
|
2014-12-25 20:35:30 +00:00
|
|
|
int h = 0;
|
|
|
|
if (m.getValue(1, 1) > m.getValue(0, 0))
|
|
|
|
h = 1;
|
|
|
|
if (m.getValue(2, 2) > m.getValue(h, h))
|
|
|
|
h = 2;
|
|
|
|
|
|
|
|
if (h == 0) {
|
|
|
|
s = sqrt(m.getValue(0, 0) - (m.getValue(1,1) + m.getValue(2, 2)) + 1.0f);
|
|
|
|
qx = s * 0.5f;
|
|
|
|
s = 0.5f / s;
|
|
|
|
qy = (m.getValue(0, 1) + m.getValue(1, 0)) * s;
|
|
|
|
qz = (m.getValue(2, 0) + m.getValue(0, 2)) * s;
|
|
|
|
qw = (m.getValue(2, 1) - m.getValue(1, 2)) * s;
|
|
|
|
} else if (h == 1) {
|
|
|
|
s = sqrt(m.getValue(1, 1) - (m.getValue(2,2) + m.getValue(0, 0)) + 1.0f);
|
|
|
|
qy = s * 0.5f;
|
|
|
|
s = 0.5f / s;
|
|
|
|
qz = (m.getValue(1, 2) + m.getValue(2, 1)) * s;
|
|
|
|
qx = (m.getValue(0, 1) + m.getValue(1, 0)) * s;
|
|
|
|
qw = (m.getValue(0, 2) - m.getValue(2, 0)) * s;
|
2014-06-20 11:42:52 +00:00
|
|
|
} else {
|
2014-12-25 20:35:30 +00:00
|
|
|
s = sqrt(m.getValue(2, 2) - (m.getValue(0,0) + m.getValue(1, 1)) + 1.0f);
|
|
|
|
qz = s * 0.5f;
|
|
|
|
s = 0.5f / s;
|
|
|
|
qx = (m.getValue(2, 0) + m.getValue(0, 2)) * s;
|
|
|
|
qy = (m.getValue(1, 2) + m.getValue(2, 1)) * s;
|
|
|
|
qw = (m.getValue(1, 0) - m.getValue(0, 1)) * s;
|
2014-06-20 11:42:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
set(qx, qy, qz, qw);
|
|
|
|
}
|
|
|
|
|
2012-07-07 18:33:33 +00:00
|
|
|
void Quaternion::toMatrix(Matrix4 &dst) const {
|
2011-12-30 13:49:01 +00:00
|
|
|
float two_xx = x() * (x() + x());
|
|
|
|
float two_xy = x() * (y() + y());
|
|
|
|
float two_xz = x() * (z() + z());
|
2012-01-30 20:45:22 +00:00
|
|
|
|
2011-12-30 13:49:01 +00:00
|
|
|
float two_wx = w() * (x() + x());
|
|
|
|
float two_wy = w() * (y() + y());
|
|
|
|
float two_wz = w() * (z() + z());
|
2012-01-30 20:45:22 +00:00
|
|
|
|
2011-12-30 13:49:01 +00:00
|
|
|
float two_yy = y() * (y() + y());
|
|
|
|
float two_yz = y() * (z() + z());
|
2012-01-30 20:45:22 +00:00
|
|
|
|
2011-12-30 13:49:01 +00:00
|
|
|
float two_zz = z() * (z() + z());
|
2012-01-30 20:45:22 +00:00
|
|
|
|
|
|
|
float newMat[16] = {
|
2014-06-20 11:42:52 +00:00
|
|
|
1.0f - (two_yy + two_zz), two_xy - two_wz, two_xz + two_wy, 0.0f,
|
|
|
|
two_xy + two_wz, 1.0f - (two_xx + two_zz), two_yz - two_wx, 0.0f,
|
|
|
|
two_xz - two_wy, two_yz + two_wx, 1.0f - (two_xx + two_yy), 0.0f,
|
|
|
|
0.0f, 0.0f, 0.0f, 1.0f
|
2011-12-30 13:49:01 +00:00
|
|
|
};
|
|
|
|
dst.setData(newMat);
|
2012-01-30 22:48:14 +00:00
|
|
|
}
|
|
|
|
|
2012-07-07 18:33:33 +00:00
|
|
|
Matrix4 Quaternion::toMatrix() const {
|
2012-01-30 22:48:14 +00:00
|
|
|
Matrix4 dst;
|
|
|
|
toMatrix(dst);
|
2011-12-30 13:49:01 +00:00
|
|
|
return dst;
|
|
|
|
}
|
2012-01-30 20:04:08 +00:00
|
|
|
|
2015-07-28 19:25:37 +00:00
|
|
|
Quaternion Quaternion::inverse() const {
|
|
|
|
Quaternion q = *this;
|
|
|
|
q.normalize();
|
|
|
|
q.x() = -q.x();
|
|
|
|
q.y() = -q.y();
|
|
|
|
q.z() = -q.z();
|
|
|
|
return q;
|
2014-06-20 11:42:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector3d Quaternion::directionVector(const int col) const {
|
|
|
|
Matrix4 dirMat = toMatrix();
|
|
|
|
return Vector3d(dirMat.getValue(0, col), dirMat.getValue(1, col), dirMat.getValue(2, col));
|
|
|
|
}
|
|
|
|
|
|
|
|
Angle Quaternion::getAngleBetween(const Quaternion &to) {
|
|
|
|
Quaternion q = this->inverse() * to;
|
|
|
|
Angle diff(Common::rad2deg(2 * acos(q.w())));
|
|
|
|
return diff;
|
2014-05-28 11:49:11 +00:00
|
|
|
}
|
|
|
|
|
2014-08-18 15:44:48 +00:00
|
|
|
Quaternion Quaternion::fromEuler(const Angle &first, const Angle &second, const Angle &third, EulerOrder order) {
|
2014-06-20 11:42:52 +00:00
|
|
|
// First create a matrix with the rotation
|
2014-08-18 15:44:48 +00:00
|
|
|
Matrix4 rot(first, second, third, order);
|
2014-06-20 11:42:52 +00:00
|
|
|
|
|
|
|
// Convert this rotation matrix to a Quaternion
|
|
|
|
return Quaternion(rot);
|
|
|
|
}
|
|
|
|
|
2014-08-19 12:13:16 +00:00
|
|
|
void Quaternion::getEuler(Angle *first, Angle *second, Angle *third, EulerOrder order) const {
|
2014-06-20 11:42:52 +00:00
|
|
|
// Create a matrix from the Quaternion
|
|
|
|
Matrix4 rot = toMatrix();
|
|
|
|
|
|
|
|
// Convert the matrix to Euler Angles
|
2014-08-18 15:44:48 +00:00
|
|
|
Angle f, s, t;
|
|
|
|
rot.getEuler(&f, &s, &t, order);
|
2014-06-20 11:42:52 +00:00
|
|
|
|
|
|
|
// Assign the Angles if we have a reference
|
2014-08-18 15:44:48 +00:00
|
|
|
if (first != nullptr)
|
|
|
|
*first = f;
|
|
|
|
if (second != nullptr)
|
|
|
|
*second = s;
|
|
|
|
if (third != nullptr)
|
|
|
|
*third = t;
|
2014-06-20 11:42:52 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 05:41:05 +00:00
|
|
|
Quaternion& Quaternion::operator=(const Quaternion& quat) {
|
|
|
|
x() = quat.x();
|
|
|
|
y() = quat.y();
|
|
|
|
z() = quat.z();
|
|
|
|
w() = quat.w();
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-06-19 23:58:15 +00:00
|
|
|
Quaternion Quaternion::operator*(const Quaternion &o) const {
|
|
|
|
return Quaternion(
|
|
|
|
w() * o.x() + x() * o.w() + y() * o.z() - z() * o.y(),
|
|
|
|
w() * o.y() - x() * o.z() + y() * o.w() + z() * o.x(),
|
|
|
|
w() * o.z() + x() * o.y() - y() * o.x() + z() * o.w(),
|
|
|
|
w() * o.w() - x() * o.x() - y() * o.y() - z() * o.z()
|
2014-06-20 11:42:52 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Quaternion Quaternion::operator*(const float c) const {
|
|
|
|
return Quaternion(x() * c, y() * c, z() * c, w() * c);
|
|
|
|
}
|
|
|
|
|
|
|
|
Quaternion& Quaternion::operator*=(const Quaternion &o) {
|
|
|
|
*this = *this * o;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Quaternion Quaternion::operator+(const Quaternion &o) const {
|
|
|
|
return Quaternion(x() + o.x(), y() + o.y(), z() + o.z(), w() + o.w());
|
|
|
|
}
|
|
|
|
|
|
|
|
Quaternion& Quaternion::operator+=(const Quaternion &o) {
|
|
|
|
*this = *this + o;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Quaternion::operator==(const Quaternion &o) const {
|
|
|
|
float dw = fabs(w() - o.w());
|
|
|
|
float dx = fabs(x() - o.x());
|
|
|
|
float dy = fabs(y() - o.y());
|
|
|
|
float dz = fabs(z() - o.z());
|
|
|
|
// Threshold of equality
|
|
|
|
float th = 1E-5f;
|
|
|
|
|
|
|
|
if ((dw < th) && (dx < th) && (dy < th) && (dz < th)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Quaternion::operator!=(const Quaternion &o) const {
|
|
|
|
return !(*this == o);
|
2014-06-19 23:58:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // End namespace Math
|