2012-01-06 22:15:48 +00:00
|
|
|
/* ResidualVM - A 3D game interpreter
|
2011-09-10 16:11:21 +00:00
|
|
|
*
|
2012-01-06 22:15:48 +00:00
|
|
|
* ResidualVM is the legal property of its developers, whose names
|
2011-09-10 16:11:21 +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.
|
|
|
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
2011-09-10 16:11:21 +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.
|
|
|
|
|
|
|
|
* 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-09-10 16:11:21 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MATH_ROTATION3D_H
|
|
|
|
#define MATH_ROTATION3D_H
|
|
|
|
|
2011-09-14 20:48:24 +00:00
|
|
|
#include "common/streamdebug.h"
|
|
|
|
|
2011-09-10 16:11:21 +00:00
|
|
|
#include "math/utils.h"
|
|
|
|
#include "math/transform.h"
|
2011-09-18 14:23:55 +00:00
|
|
|
#include "math/angle.h"
|
2011-09-10 16:11:21 +00:00
|
|
|
|
|
|
|
namespace Math {
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
class Rotation3D : public Transform<T> {
|
|
|
|
public:
|
2011-09-14 20:48:24 +00:00
|
|
|
Rotation3D();
|
2011-09-10 16:11:21 +00:00
|
|
|
|
2011-09-18 14:23:55 +00:00
|
|
|
void buildFromPitchYawRoll(const Angle &pitch, const Angle &yaw, const Angle &roll);
|
2011-09-10 16:11:21 +00:00
|
|
|
|
2011-09-18 14:23:55 +00:00
|
|
|
void buildAroundPitch(const Angle &pitch);
|
|
|
|
void buildAroundYaw(const Angle &yaw);
|
|
|
|
void buildAroundRoll(const Angle &roll);
|
2011-09-10 16:11:21 +00:00
|
|
|
|
2011-09-18 14:23:55 +00:00
|
|
|
void getPitchYawRoll(Angle* pPitch, Angle* pYaw, Angle* pRoll) const;
|
2011-09-10 16:11:21 +00:00
|
|
|
|
2011-09-18 14:23:55 +00:00
|
|
|
Angle getPitch() const;
|
|
|
|
Angle getYaw() const;
|
|
|
|
Angle getRoll() const;
|
2011-09-10 16:11:21 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<class T>
|
2011-09-14 20:48:24 +00:00
|
|
|
Rotation3D<T>::Rotation3D() :
|
|
|
|
Transform<T>() {
|
2011-09-10 16:11:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-10-29 22:43:39 +00:00
|
|
|
// NOTE: Builds a rotation matrix of form R_Yaw * R_Pitch * R_Roll (i.e. R_z * R_x * R_y).
|
|
|
|
// The order of rotations is of the form Matrix * Vector, so roll is applied first, then pitch, then yaw.
|
2011-09-10 16:11:21 +00:00
|
|
|
template<class T>
|
2011-09-18 14:23:55 +00:00
|
|
|
void Rotation3D<T>::buildFromPitchYawRoll(const Angle &pitch, const Angle &yaw, const Angle &roll) {
|
2011-09-10 16:11:21 +00:00
|
|
|
T temp;
|
|
|
|
|
2011-10-29 22:43:39 +00:00
|
|
|
buildAroundYaw(yaw); // Rotate about +Z
|
|
|
|
temp.buildAroundPitch(pitch); // Rotate about +X
|
2011-09-14 20:48:24 +00:00
|
|
|
this->getMatrix() = this->getMatrix() * temp;
|
2011-10-29 22:43:39 +00:00
|
|
|
temp.buildAroundRoll(roll); // Rotate about +Y
|
2011-09-14 20:48:24 +00:00
|
|
|
this->getMatrix() = this->getMatrix() * temp;
|
2011-10-29 22:43:39 +00:00
|
|
|
// The created matrix has the Euler order ZXY. (M*v)
|
2011-09-10 16:11:21 +00:00
|
|
|
}
|
|
|
|
|
2011-10-29 22:43:39 +00:00
|
|
|
// at. Rotates about the +X axis.
|
2011-09-10 16:11:21 +00:00
|
|
|
template<class T>
|
2011-10-18 15:42:12 +00:00
|
|
|
void Rotation3D<T>::buildAroundPitch(const Angle &pitch) {
|
|
|
|
float cosa = pitch.getCosine();
|
|
|
|
float sina = pitch.getSine();
|
2011-09-10 16:11:21 +00:00
|
|
|
|
2011-09-14 20:48:24 +00:00
|
|
|
this->getMatrix().getRow(0) << 1.f << 0.f << 0.f;
|
|
|
|
this->getMatrix().getRow(1) << 0.f << cosa << -sina;
|
|
|
|
this->getMatrix().getRow(2) << 0.f << sina << cosa;
|
2011-09-10 16:11:21 +00:00
|
|
|
}
|
|
|
|
|
2011-10-29 22:43:39 +00:00
|
|
|
// right. Rotates about the +Y axis.
|
2011-09-10 16:11:21 +00:00
|
|
|
template<class T>
|
2011-10-18 15:42:12 +00:00
|
|
|
void Rotation3D<T>::buildAroundRoll(const Angle &roll) {
|
|
|
|
float cosa = roll.getCosine();
|
|
|
|
float sina = roll.getSine();
|
2011-09-10 16:11:21 +00:00
|
|
|
|
2011-09-14 20:48:24 +00:00
|
|
|
this->getMatrix().getRow(0) << cosa << 0.f << sina;
|
|
|
|
this->getMatrix().getRow(1) << 0.f << 1.f << 0.f;
|
|
|
|
this->getMatrix().getRow(2) << -sina << 0.f << cosa;
|
2011-09-10 16:11:21 +00:00
|
|
|
}
|
|
|
|
|
2011-10-29 22:43:39 +00:00
|
|
|
// up. Rotates about the +Z axis.
|
2011-09-10 16:11:21 +00:00
|
|
|
template<class T>
|
2011-09-18 14:23:55 +00:00
|
|
|
void Rotation3D<T>::buildAroundYaw(const Angle &yaw) {
|
|
|
|
float cosa = yaw.getCosine();
|
|
|
|
float sina = yaw.getSine();
|
2011-09-10 16:11:21 +00:00
|
|
|
|
2011-09-14 20:48:24 +00:00
|
|
|
this->getMatrix().getRow(0) << cosa << -sina << 0.f;
|
|
|
|
this->getMatrix().getRow(1) << sina << cosa << 0.f;
|
|
|
|
this->getMatrix().getRow(2) << 0.f << 0.f << 1.f;
|
2011-09-10 16:11:21 +00:00
|
|
|
}
|
|
|
|
|
2012-03-06 07:25:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Decomposes the matrix M to form M = R_z * R_x * R_y (R_D being the cardinal rotation
|
|
|
|
* matrix about the axis +D), and outputs the angles of rotation in parameters pPitch, pYaw and pRoll.
|
|
|
|
* In the convention of the coordinate system used in Grim Fandango characters:
|
|
|
|
* Pitch is rotation about the X axis (right)
|
|
|
|
* Yaw is rotation about the Z axis (up)
|
|
|
|
* Roll is rotation about the Y axix (out)
|
|
|
|
* This function was adapted from http://www.geometrictools.com/Documentation/EulerAngles.pdf
|
|
|
|
* The matrix M must be orthonormal.
|
|
|
|
*/
|
2011-09-10 16:11:21 +00:00
|
|
|
template<class T>
|
2011-09-18 14:23:55 +00:00
|
|
|
void Rotation3D<T>::getPitchYawRoll(Angle *pPitch, Angle *pYaw, Angle *pRoll) const {
|
2012-03-06 07:25:12 +00:00
|
|
|
const T *m = &(this->getMatrix()); // so dumb
|
|
|
|
|
|
|
|
float x,y,z;
|
|
|
|
if (m->getValue(2, 1) < 1.f) {
|
|
|
|
if (m->getValue(2, 1) > -1.f) {
|
|
|
|
x = asin(m->getValue(2, 1));
|
|
|
|
z = atan2(-m->getValue(0, 1), m->getValue(1, 1));
|
|
|
|
y = atan2(-m->getValue(2, 0), m->getValue(2, 2));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Not a unique solution. Pick an arbitrary one.
|
|
|
|
x = -3.141592654f/2.f;
|
|
|
|
z = -atan2(-m->getValue(0, 2), m->getValue(0, 0));
|
|
|
|
y = 0;
|
|
|
|
}
|
2011-09-10 16:11:21 +00:00
|
|
|
}
|
2012-03-06 07:25:12 +00:00
|
|
|
else {
|
|
|
|
// Not a unique solution. Pick an arbitrary one.
|
|
|
|
x = 3.141592654f/2.f;
|
|
|
|
z = atan2(m->getValue(0, 2), m->getValue(0, 0));
|
|
|
|
y = 0;
|
2011-09-10 16:11:21 +00:00
|
|
|
}
|
2012-03-09 01:13:22 +00:00
|
|
|
|
|
|
|
if (pPitch)
|
|
|
|
*pPitch = Math::Angle::fromRadians(x);
|
|
|
|
if (pRoll)
|
|
|
|
*pRoll = Math::Angle::fromRadians(y);
|
|
|
|
if (pYaw)
|
|
|
|
*pYaw = Math::Angle::fromRadians(z);
|
2011-09-10 16:11:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
2011-09-18 14:23:55 +00:00
|
|
|
Angle Rotation3D<T>::getPitch() const {
|
|
|
|
Angle pitch;
|
2011-09-10 16:11:21 +00:00
|
|
|
|
|
|
|
getPitchYawRoll(&pitch, 0, 0);
|
|
|
|
|
|
|
|
return pitch;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
2011-09-18 14:23:55 +00:00
|
|
|
Angle Rotation3D<T>::getYaw() const {
|
|
|
|
Angle yaw;
|
2011-09-10 16:11:21 +00:00
|
|
|
|
|
|
|
getPitchYawRoll(0, &yaw, 0);
|
|
|
|
|
|
|
|
return yaw;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
2011-09-18 14:23:55 +00:00
|
|
|
Angle Rotation3D<T>::getRoll() const {
|
|
|
|
Angle roll;
|
2011-09-10 16:11:21 +00:00
|
|
|
|
|
|
|
getPitchYawRoll(0, 0, &roll);
|
|
|
|
|
|
|
|
return roll;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|