2013-03-09 07:34:57 +00:00
|
|
|
|
|
|
|
/********************************************************************************
|
|
|
|
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
|
|
|
* Copyright (c) 2010-2013 Daniel Chappuis *
|
|
|
|
*********************************************************************************
|
|
|
|
* *
|
|
|
|
* This software is provided 'as-is', without any express or implied warranty. *
|
|
|
|
* In no event will the authors be held liable for any damages arising from the *
|
|
|
|
* use of this software. *
|
|
|
|
* *
|
|
|
|
* Permission is granted to anyone to use this software for any purpose, *
|
|
|
|
* including commercial applications, and to alter it and redistribute it *
|
|
|
|
* freely, subject to the following restrictions: *
|
|
|
|
* *
|
|
|
|
* 1. The origin of this software must not be misrepresented; you must not claim *
|
|
|
|
* that you wrote the original software. If you use this software in a *
|
|
|
|
* product, an acknowledgment in the product documentation would be *
|
|
|
|
* appreciated but is not required. *
|
|
|
|
* *
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and must not be *
|
|
|
|
* misrepresented as being the original software. *
|
|
|
|
* *
|
|
|
|
* 3. This notice may not be removed or altered from any source distribution. *
|
|
|
|
* *
|
|
|
|
********************************************************************************/
|
|
|
|
|
|
|
|
#ifndef TEST_QUATERNION_H
|
|
|
|
#define TEST_QUATERNION_H
|
|
|
|
|
|
|
|
// Libraries
|
|
|
|
#include "../../Test.h"
|
|
|
|
#include "../../../src/mathematics/Quaternion.h"
|
|
|
|
|
|
|
|
/// Reactphysics3D namespace
|
|
|
|
namespace reactphysics3d {
|
|
|
|
|
|
|
|
// Class TestQuaternion
|
|
|
|
/**
|
|
|
|
* Unit test for the Quaternion class
|
|
|
|
*/
|
|
|
|
class TestQuaternion : public Test {
|
|
|
|
|
|
|
|
private :
|
|
|
|
|
|
|
|
// ---------- Atributes ---------- //
|
|
|
|
|
|
|
|
/// Identity Quaternion
|
|
|
|
Quaternion mIdentity;
|
|
|
|
|
|
|
|
/// First test quaternion
|
|
|
|
Quaternion mQuaternion1;
|
|
|
|
|
|
|
|
public :
|
|
|
|
|
|
|
|
// ---------- Methods ---------- //
|
|
|
|
|
|
|
|
/// Constructor
|
|
|
|
TestQuaternion() : mIdentity(Quaternion::identity()) {
|
|
|
|
|
2013-03-13 19:52:59 +00:00
|
|
|
decimal sinA = sin(decimal(PI/8.0));
|
|
|
|
decimal cosA = cos(decimal(PI/8.0));
|
|
|
|
Vector3 vector(2, 3, 4);
|
|
|
|
vector.normalize();
|
|
|
|
mQuaternion1 = Quaternion(vector.x * sinA, vector.y * sinA, vector.z * sinA, cosA);
|
|
|
|
mQuaternion1.normalize();
|
2013-03-09 07:34:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Run the tests
|
|
|
|
void run() {
|
|
|
|
testConstructors();
|
2013-03-13 19:52:59 +00:00
|
|
|
testUnitLengthNormalize();
|
|
|
|
testOthersMethods();
|
|
|
|
testOperators();
|
2013-03-09 07:34:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Test the constructors
|
|
|
|
void testConstructors() {
|
2013-03-13 19:52:59 +00:00
|
|
|
|
2013-03-09 07:34:57 +00:00
|
|
|
Quaternion quaternion1(mQuaternion1);
|
|
|
|
test(mQuaternion1== quaternion1);
|
|
|
|
|
|
|
|
Quaternion quaternion2(4, 5, 6, 7);
|
|
|
|
test(quaternion2 == Quaternion(4, 5, 6, 7));
|
|
|
|
|
|
|
|
Quaternion quaternion3(8, Vector3(3, 5, 2));
|
2013-03-13 19:52:59 +00:00
|
|
|
test(quaternion3 == Quaternion(3, 5, 2, 8));
|
|
|
|
|
|
|
|
Quaternion quaternion4(mQuaternion1.getMatrix());
|
|
|
|
test(approxEqual(quaternion4.x, mQuaternion1.x));
|
|
|
|
test(approxEqual(quaternion4.y, mQuaternion1.y));
|
|
|
|
test(approxEqual(quaternion4.z, mQuaternion1.z));
|
|
|
|
test(approxEqual(quaternion4.w, mQuaternion1.w));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Test unit, length, normalize methods
|
|
|
|
void testUnitLengthNormalize() {
|
|
|
|
|
|
|
|
// Test method that returns the length
|
|
|
|
Quaternion quaternion(2, 3, -4, 5);
|
|
|
|
test(approxEqual(quaternion.length(), sqrt(decimal(54.0))));
|
|
|
|
|
|
|
|
// Test method that returns a unit quaternion
|
|
|
|
test(approxEqual(quaternion.getUnit().length(), 1.0));
|
|
|
|
|
|
|
|
// Test the normalization method
|
|
|
|
Quaternion quaternion2(4, 5, 6, 7);
|
|
|
|
quaternion2.normalize();
|
|
|
|
test(approxEqual(quaternion2.length(), 1.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Test others methods
|
|
|
|
void testOthersMethods() {
|
|
|
|
|
|
|
|
// Test the method to set the values
|
|
|
|
Quaternion quaternion;
|
|
|
|
quaternion.setAllValues(1, 2, 3, 4);
|
|
|
|
test(quaternion == Quaternion(1, 2, 3, 4));
|
|
|
|
|
|
|
|
// Test the method to set the quaternion to zero
|
|
|
|
quaternion.setToZero();
|
|
|
|
test(quaternion == Quaternion(0, 0, 0, 0));
|
|
|
|
|
2013-06-09 14:31:01 +00:00
|
|
|
// Tes the methods to get or set to identity
|
|
|
|
Quaternion identity1(1, 2, 3, 4);
|
|
|
|
identity1.setToIdentity();
|
|
|
|
test(identity1 == Quaternion(0, 0, 0, 1));
|
|
|
|
test(Quaternion::identity() == Quaternion(0, 0, 0, 1));
|
|
|
|
|
2013-03-13 19:52:59 +00:00
|
|
|
// Test the method to get the vector (x, y, z)
|
|
|
|
Vector3 v = mQuaternion1.getVectorV();
|
|
|
|
test(v.x == mQuaternion1.x);
|
|
|
|
test(v.y == mQuaternion1.y);
|
|
|
|
test(v.z == mQuaternion1.z);
|
|
|
|
|
|
|
|
// Test the conjugate method
|
|
|
|
Quaternion conjugate = mQuaternion1.getConjugate();
|
|
|
|
test(conjugate.x == -mQuaternion1.x);
|
|
|
|
test(conjugate.y == -mQuaternion1.y);
|
|
|
|
test(conjugate.z == -mQuaternion1.z);
|
|
|
|
test(conjugate.w == mQuaternion1.w);
|
|
|
|
|
2013-06-09 14:31:01 +00:00
|
|
|
// Test the inverse methods
|
|
|
|
Quaternion inverse1 = mQuaternion1.getInverse();
|
|
|
|
Quaternion inverse2(mQuaternion1);
|
|
|
|
inverse2.inverse();
|
|
|
|
test(inverse2 == inverse1);
|
|
|
|
Quaternion product = mQuaternion1 * inverse1;
|
2013-03-13 19:52:59 +00:00
|
|
|
test(approxEqual(product.x, mIdentity.x, decimal(10e-6)));
|
|
|
|
test(approxEqual(product.y, mIdentity.y, decimal(10e-6)));
|
|
|
|
test(approxEqual(product.z, mIdentity.z, decimal(10e-6)));
|
|
|
|
test(approxEqual(product.w, mIdentity.w, decimal(10e-6)));
|
|
|
|
|
|
|
|
// Test the dot product
|
|
|
|
Quaternion quaternion1(2, 3, 4, 5);
|
|
|
|
Quaternion quaternion2(6, 7, 8, 9);
|
|
|
|
decimal dotProduct = quaternion1.dot(quaternion2);
|
|
|
|
test(dotProduct == 110);
|
|
|
|
|
|
|
|
// Test the method that returns the rotation angle and axis
|
|
|
|
Vector3 axis;
|
|
|
|
decimal angle;
|
|
|
|
Vector3 originalAxis = Vector3(2, 3, 4).getUnit();
|
|
|
|
mQuaternion1.getRotationAngleAxis(angle, axis);
|
|
|
|
test(approxEqual(axis.x, originalAxis.x));
|
|
|
|
test(approxEqual(angle, decimal(PI/4.0), decimal(10e-6)));
|
|
|
|
|
|
|
|
// Test the method that returns the corresponding matrix
|
|
|
|
Matrix3x3 matrix = mQuaternion1.getMatrix();
|
|
|
|
Vector3 vector(56, -2, 82);
|
|
|
|
Vector3 vector1 = matrix * vector;
|
|
|
|
Vector3 vector2 = mQuaternion1 * vector;
|
2013-03-18 21:13:04 +00:00
|
|
|
test(approxEqual(vector1.x, vector2.x, decimal(10e-6)));
|
|
|
|
test(approxEqual(vector1.y, vector2.y, decimal(10e-6)));
|
|
|
|
test(approxEqual(vector1.z, vector2.z, decimal(10e-6)));
|
2013-03-13 19:52:59 +00:00
|
|
|
|
|
|
|
// Test slerp method
|
|
|
|
Quaternion quatStart = quaternion1.getUnit();
|
|
|
|
Quaternion quatEnd = quaternion2.getUnit();
|
|
|
|
Quaternion test1 = Quaternion::slerp(quatStart, quatEnd, 0.0);
|
|
|
|
Quaternion test2 = Quaternion::slerp(quatStart, quatEnd, 1.0);
|
|
|
|
test(test1 == quatStart);
|
|
|
|
test(test2 == quatEnd);
|
|
|
|
decimal sinA = sin(decimal(PI/4.0));
|
|
|
|
decimal cosA = cos(decimal(PI/4.0));
|
|
|
|
Quaternion quat(sinA, 0, 0, cosA);
|
|
|
|
Quaternion test3 = Quaternion::slerp(mIdentity, quat, decimal(0.5));
|
|
|
|
test(approxEqual(test3.x, sin(decimal(PI/8.0))));
|
|
|
|
test(approxEqual(test3.y, 0.0));
|
|
|
|
test(approxEqual(test3.z, 0.0));
|
|
|
|
test(approxEqual(test3.w, cos(decimal(PI/8.0)), decimal(10e-6)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Test overloaded operators
|
|
|
|
void testOperators() {
|
|
|
|
|
|
|
|
// Test addition
|
|
|
|
Quaternion quat1(4, 5, 2, 10);
|
|
|
|
Quaternion quat2(-2, 7, 8, 3);
|
|
|
|
Quaternion test1 = quat1 + quat2;
|
2013-06-26 20:28:31 +00:00
|
|
|
Quaternion test11(-6, 52, 2, 8);
|
|
|
|
test11 += quat1;
|
2013-03-13 19:52:59 +00:00
|
|
|
test(test1 == Quaternion(2, 12, 10, 13));
|
2013-06-26 20:28:31 +00:00
|
|
|
test(test11 == Quaternion(-2, 57, 4, 18));
|
2013-03-13 19:52:59 +00:00
|
|
|
|
|
|
|
// Test substraction
|
|
|
|
Quaternion test2 = quat1 - quat2;
|
2013-06-26 20:28:31 +00:00
|
|
|
Quaternion test22(-73, 62, 25, 9);
|
|
|
|
test22 -= quat1;
|
2013-03-13 19:52:59 +00:00
|
|
|
test(test2 == Quaternion(6, -2, -6, 7));
|
2013-06-26 20:28:31 +00:00
|
|
|
test(test22 == Quaternion(-77, 57, 23, -1));
|
2013-03-13 19:52:59 +00:00
|
|
|
|
|
|
|
// Test multiplication with a number
|
|
|
|
Quaternion test3 = quat1 * 3.0;
|
|
|
|
test(test3 == Quaternion(12, 15, 6, 30));
|
|
|
|
|
|
|
|
// Test multiplication between two quaternions
|
|
|
|
Quaternion test4 = quat1 * quat2;
|
|
|
|
Quaternion test5 = mQuaternion1 * mIdentity;
|
|
|
|
test(test4 == Quaternion(18, 49, 124, -13));
|
|
|
|
test(test5 == mQuaternion1);
|
|
|
|
|
|
|
|
// Test multiplication between a quaternion and a point
|
|
|
|
Vector3 point(5, -24, 563);
|
|
|
|
Vector3 vector1 = mIdentity * point;
|
|
|
|
Vector3 vector2 = mQuaternion1 * point;
|
|
|
|
Vector3 testVector2 = mQuaternion1.getMatrix() * point;
|
|
|
|
test(vector1 == point);
|
2013-03-18 21:13:04 +00:00
|
|
|
test(approxEqual(vector2.x, testVector2.x, decimal(10e-5)));
|
|
|
|
test(approxEqual(vector2.y, testVector2.y, decimal(10e-5)));
|
|
|
|
test(approxEqual(vector2.z, testVector2.z, decimal(10e-5)));
|
2013-03-13 19:52:59 +00:00
|
|
|
|
|
|
|
// Test assignment operator
|
|
|
|
Quaternion quaternion;
|
|
|
|
quaternion = mQuaternion1;
|
|
|
|
test(quaternion == mQuaternion1);
|
|
|
|
|
|
|
|
// Test equality operator
|
|
|
|
test(mQuaternion1 == mQuaternion1);
|
2013-03-09 07:34:57 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2013-05-09 17:02:09 +00:00
|
|
|
|
|
|
|
#endif
|