From 34a739829393ec8b1705332ab7ea84830c974fc1 Mon Sep 17 00:00:00 2001 From: Giulio Camuffo Date: Sat, 10 Sep 2011 18:11:21 +0200 Subject: [PATCH] MATH: Moved Rotation3D to its own file. --- engines/grim/lua_v1.cpp | 2 + math/matrix3.h | 128 +------------------------------- math/matrix4.h | 4 +- math/rotation3d.h | 158 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 164 insertions(+), 128 deletions(-) create mode 100644 math/rotation3d.h diff --git a/engines/grim/lua_v1.cpp b/engines/grim/lua_v1.cpp index 55ae8aa58b5..29fc431b01b 100644 --- a/engines/grim/lua_v1.cpp +++ b/engines/grim/lua_v1.cpp @@ -25,6 +25,8 @@ #include "common/endian.h" #include "common/system.h" +#include "math/matrix3.h" + #include "engines/grim/debug.h" #include "engines/grim/lua.h" #include "engines/grim/actor.h" diff --git a/math/matrix3.h b/math/matrix3.h index bf25646dacc..2c6d4189d95 100644 --- a/math/matrix3.h +++ b/math/matrix3.h @@ -25,32 +25,12 @@ #ifndef MATH_MATRIX3_H #define MATH_MATRIX3_H -#include "math/utils.h" +#include "math/rotation3d.h" #include "math/matrix.h" -#include "math/transform.h" #include "math/vector3d.h" namespace Math { -template -class Rotation3D : public Transform { -public: - Rotation3D(T *matrix); - - void buildFromPitchYawRoll(float pitch, float yaw, float roll); - - void constructAroundPitch(float pitch); - void constructAroundYaw(float yaw); - void constructAroundRoll(float roll); - - void getPitchYawRoll(float* pPitch, float* pYaw, float* pRoll) const; - - float getPitch() const; - float getYaw() const; - float getRoll() const; - -}; - class Matrix3x3 : public Matrix<3, 3>, public Rotation3D { public: Matrix3x3(); @@ -60,112 +40,6 @@ public: typedef Matrix3x3 Matrix3; - - -template -Rotation3D::Rotation3D(T *matrix) : - Transform(matrix) { - -} - -template -void Rotation3D::buildFromPitchYawRoll(float pitch, float yaw, float roll) { - T temp; - - constructAroundYaw(yaw); - temp.constructAroundPitch(pitch); - (*this->getMatrix()) *= temp; - temp.constructAroundRoll(roll); - (*this->getMatrix()) *= temp; -} - -// at, around x-axis -template -void Rotation3D::constructAroundRoll(float roll) { - float cosa = (float)cos(degreeToRadian(roll)); - float sina = (float)sin(degreeToRadian(roll)); - - 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; -} - -// right -template -void Rotation3D::constructAroundPitch(float pitch) { - float cosa = (float)cos(degreeToRadian(pitch)); - float sina = (float)sin(degreeToRadian(pitch)); - - 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; -} - -// up -template -void Rotation3D::constructAroundYaw(float yaw) { - float cosa = (float)cos(degreeToRadian(yaw)); - float sina = (float)sin(degreeToRadian(yaw)); - - 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; -} - -/* - 0 * 1 2 3 - 4 5 6 7 - 8 9 10 11 - */ -template -void Rotation3D::getPitchYawRoll(float *pPitch, float *pYaw, float *pRoll) const { - // based on http://planning.cs.uiuc.edu/node103.html - if (pYaw) { - *pYaw = radianToDegree(atan2f(this->getMatrix()->getValue(1, 0), - this->getMatrix()->getValue(0, 0))); - } - - if (pPitch) { - float a = this->getMatrix()->getValue(2, 1); - float b = this->getMatrix()->getValue(2, 2); - float mag = sqrt(a * a + b * b); - *pPitch = radianToDegree(atan2f(-this->getMatrix()->getValue(2, 0), mag)); - } - - if (pRoll) { - *pRoll = radianToDegree(atan2f(this->getMatrix()->getValue(2, 1), - this->getMatrix()->getValue(2, 2))); - } -} - -template -float Rotation3D::getPitch() const { - float pitch; - - getPitchYawRoll(&pitch, 0, 0); - - return pitch; -} - -template -float Rotation3D::getYaw() const { - float yaw; - - getPitchYawRoll(0, &yaw, 0); - - return yaw; -} - -template -float Rotation3D::getRoll() const { - float roll; - - getPitchYawRoll(0, 0, &roll); - - return roll; -} - - } // end of namespace Math #endif diff --git a/math/matrix4.h b/math/matrix4.h index 2be149cfd0e..ce71c13ef42 100644 --- a/math/matrix4.h +++ b/math/matrix4.h @@ -25,7 +25,9 @@ #ifndef MATH_MATRIX4_H #define MATH_MATRIX4_H -#include "math/matrix3.h" +#include "math/rotation3d.h" +#include "math/matrix.h" +#include "math/vector3d.h" namespace Math { diff --git a/math/rotation3d.h b/math/rotation3d.h new file mode 100644 index 00000000000..b00dd7c863b --- /dev/null +++ b/math/rotation3d.h @@ -0,0 +1,158 @@ +/* Residual - A 3D game interpreter + * + * Residual is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + * + * $URL$ + * $Id$ + */ + +#ifndef MATH_ROTATION3D_H +#define MATH_ROTATION3D_H + +#include "math/utils.h" +#include "math/transform.h" + +namespace Math { + +template +class Rotation3D : public Transform { +public: + Rotation3D(T *matrix); + + void buildFromPitchYawRoll(float pitch, float yaw, float roll); + + void constructAroundPitch(float pitch); + void constructAroundYaw(float yaw); + void constructAroundRoll(float roll); + + void getPitchYawRoll(float* pPitch, float* pYaw, float* pRoll) const; + + float getPitch() const; + float getYaw() const; + float getRoll() const; + +}; + + +template +Rotation3D::Rotation3D(T *matrix) : + Transform(matrix) { + +} + +template +void Rotation3D::buildFromPitchYawRoll(float pitch, float yaw, float roll) { + T temp; + + constructAroundYaw(yaw); + temp.constructAroundPitch(pitch); + (*this->getMatrix()) *= temp; + temp.constructAroundRoll(roll); + (*this->getMatrix()) *= temp; +} + +// at, around x-axis +template +void Rotation3D::constructAroundRoll(float roll) { + float cosa = (float)cos(degreeToRadian(roll)); + float sina = (float)sin(degreeToRadian(roll)); + + 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; +} + +// right +template +void Rotation3D::constructAroundPitch(float pitch) { + float cosa = (float)cos(degreeToRadian(pitch)); + float sina = (float)sin(degreeToRadian(pitch)); + + 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; +} + +// up +template +void Rotation3D::constructAroundYaw(float yaw) { + float cosa = (float)cos(degreeToRadian(yaw)); + float sina = (float)sin(degreeToRadian(yaw)); + + 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; +} + +/* + * 0 * 1 2 3 + * 4 5 6 7 + * 8 9 10 11 + */ +template +void Rotation3D::getPitchYawRoll(float *pPitch, float *pYaw, float *pRoll) const { + // based on http://planning.cs.uiuc.edu/node103.html + if (pYaw) { + *pYaw = radianToDegree(atan2f(this->getMatrix()->getValue(1, 0), + this->getMatrix()->getValue(0, 0))); + } + + if (pPitch) { + float a = this->getMatrix()->getValue(2, 1); + float b = this->getMatrix()->getValue(2, 2); + float mag = sqrt(a * a + b * b); + *pPitch = radianToDegree(atan2f(-this->getMatrix()->getValue(2, 0), mag)); + } + + if (pRoll) { + *pRoll = radianToDegree(atan2f(this->getMatrix()->getValue(2, 1), + this->getMatrix()->getValue(2, 2))); + } +} + +template +float Rotation3D::getPitch() const { + float pitch; + + getPitchYawRoll(&pitch, 0, 0); + + return pitch; +} + +template +float Rotation3D::getYaw() const { + float yaw; + + getPitchYawRoll(0, &yaw, 0); + + return yaw; +} + +template +float Rotation3D::getRoll() const { + float roll; + + getPitchYawRoll(0, 0, &roll); + + return roll; +} + +} + +#endif