git-svn-id: https://reactphysics3d.googlecode.com/svn/trunk@120 92aac97c-a6ce-11dd-a772-7fcde58d38e6

This commit is contained in:
chappuis.daniel 2009-04-10 15:34:42 +00:00
parent 58a7689404
commit c97ed7845a
3 changed files with 26 additions and 19 deletions

View File

@ -18,8 +18,7 @@
***************************************************************************/ ***************************************************************************/
// Libraries // Libraries
#include "Quaternion.h" #include "mathematics.h"
#include "constants.h"
#include <cassert> #include <cassert>
// Namespaces // Namespaces
@ -55,9 +54,8 @@ Quaternion::~Quaternion() {
} }
// Compute the rotation angle (in radians) and the 3D rotation axis // Compute the rotation angle (in radians) and the 3D rotation axis
// This method is used to get the rotation angle and axis of an // This method is used to get the rotation angle (in radian) and the unit
// orientation quaternion. // rotation axis of an orientation quaternion.
// TODO : Test this method
void Quaternion::getRotationAngleAxis(double& angle, Vector3D& axis) const { void Quaternion::getRotationAngleAxis(double& angle, Vector3D& axis) const {
Quaternion quaternion; Quaternion quaternion;
@ -84,26 +82,25 @@ void Quaternion::getRotationAngleAxis(double& angle, Vector3D& axis) const {
} }
// Compute the spherical linear interpolation between two quaternions. // Compute the spherical linear interpolation between two quaternions.
// The t argument has to be such that 0 <= t <= 1. // The t argument has to be such that 0 <= t <= 1. This method is static.
// TODO : Test this method
Quaternion Quaternion::slerp(const Quaternion& quaternion1, const Quaternion& quaternion2, double t) { Quaternion Quaternion::slerp(const Quaternion& quaternion1, const Quaternion& quaternion2, double t) {
assert(t >= 0 && t <= 1); assert(t >= 0.0 && t <= 1.0);
double invert = 1; double invert = 1.0;
// Compute cos(theta) using the quaternion scalar product // Compute cos(theta) using the quaternion scalar product
double cosineTheta = quaternion1.scalarProduct(quaternion2); double cosineTheta = quaternion1.scalarProduct(quaternion2);
// Take care of the sign of cosineTheta // Take care of the sign of cosineTheta
if (cosineTheta < 0) { if (cosineTheta < 0.0) {
cosineTheta = -cosineTheta; cosineTheta = -cosineTheta;
invert = -1; invert = -1.0;
} }
// Because of precision, if cos(theta) is nearly 1, therefore theta is nearly 0 and we can write // Because of precision, if cos(theta) is nearly 1, therefore theta is nearly 0 and we can write
// sin((1-t)*theta) as (1-t) and sin(t*theta) as t // sin((1-t)*theta) as (1-t) and sin(t*theta) as t
if ((1.0-cosineTheta) < epsilon) { if(1-cosineTheta < EPSILON) {
return quaternion1 * (1-t) + quaternion2 * (t * invert); return quaternion1 * (1.0-t) + quaternion2 * (t * invert);
} }
// Compute the theta angle // Compute the theta angle
@ -113,7 +110,7 @@ Quaternion Quaternion::slerp(const Quaternion& quaternion1, const Quaternion& qu
double sineTheta = sin(theta); double sineTheta = sin(theta);
// Compute the two coefficients that are in the spherical linear interpolation formula // Compute the two coefficients that are in the spherical linear interpolation formula
double coeff1 = sin((1-t)*theta) / sineTheta; double coeff1 = sin((1.0-t)*theta) / sineTheta;
double coeff2 = sin(t*theta) / sineTheta * invert; double coeff2 = sin(t*theta) / sineTheta * invert;
// Compute and return the interpolated quaternion // Compute and return the interpolated quaternion

View File

@ -21,9 +21,9 @@
#define CONSTANTS_H #define CONSTANTS_H
// Constant used for the precision // Constant used for the precision
const double epsilon = 0.00001; const double EPSILON = 0.00001;
// Pi constant // Pi constant
const double pi = 3.14159265; const double PI = 3.14159265;
#endif #endif

View File

@ -19,8 +19,8 @@
// TODO : Mathematics library : Check everywhere that in member methods we use attributes access instead of getter and setter. // TODO : Mathematics library : Check everywhere that in member methods we use attributes access instead of getter and setter.
#ifndef DCMATHS_H #ifndef MATHEMATICS_H
#define DCMATHS_H #define MATHEMATICS_H
// Libraries // Libraries
#include "Matrix.h" #include "Matrix.h"
@ -29,6 +29,16 @@
#include "Vector.h" #include "Vector.h"
#include "Vector3D.h" #include "Vector3D.h"
#include "constants.h" #include "constants.h"
#include "exceptions.h" #include "exceptions.h"
#include <cstdio>
// ---------- Mathematics functions ---------- //
// Method to test if two numbers are (almost) equal
// We test if two numbers a and b are such that (a-b) are in [-EPSILON; EPSILON]
inline bool equal(double a, double b) {
double difference = a - b;
return (difference < EPSILON && difference > -EPSILON);
}
#endif #endif