Change in the repository structure
git-svn-id: https://reactphysics3d.googlecode.com/svn/trunk@390 92aac97c-a6ce-11dd-a772-7fcde58d38e6
This commit is contained in:
parent
0ec7aac17a
commit
b47cd2edd6
|
@ -22,6 +22,8 @@
|
|||
#include "../reactphysics3d/reactphysics3d.h"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "../../libraries/boost_1_43_0/boost/numeric/ublas/matrix.hpp"
|
||||
#include "../../libraries/boost_1_43_0/boost/numeric/ublas/io.hpp"
|
||||
|
||||
// We want to use the ReactPhysics3D namespace
|
||||
using namespace reactphysics3d;
|
||||
|
@ -30,48 +32,38 @@ using namespace reactphysics3d;
|
|||
Context::Context() {
|
||||
|
||||
/*
|
||||
Cube* cube1 = new Cube(Vector3D(0.0, 10.0, 0.0), Quaternion(1.0, 1.0, 0.0, 0.0), 3.0, Kilogram(1.0));
|
||||
Cube* cube2 = new Cube(Vector3D(0.0, 0.0, 0.0), Quaternion(1.0, 1.0, 0.0, 0.0), 3.0, Kilogram(1.0));
|
||||
//cube1->getRigidBody()->setLinearVelocity(Vector3D(0.0, 0.0, 0.0));
|
||||
//cube2->getRigidBody()->setLinearVelocity(Vector3D(0.0, 0.0, 0.0));
|
||||
cube2->getRigidBody()->setIsMotionEnabled(false);
|
||||
cube1->getRigidBody()->setRestitution(0.5);
|
||||
cube2->getRigidBody()->setRestitution(0.5);
|
||||
using namespace boost::numeric::ublas;
|
||||
matrix<double> m (3, 3);
|
||||
for (unsigned i = 0; i < m.size1 (); ++ i)
|
||||
for (unsigned j = 0; j < m.size2 (); ++ j)
|
||||
m (i, j) = 3 * i + j;
|
||||
std::cout << m << std::endl;
|
||||
*/
|
||||
|
||||
|
||||
for (int i=20; i>1; i=i-3) {
|
||||
Cube* cube = new Cube(Vector3D(3.0, i, i*0.2), Quaternion(1.0, 1.0, 0.0, 0.0), 2.0, Kilogram(1.0));
|
||||
cube->getRigidBody()->setRestitution(0.7);
|
||||
/*
|
||||
//Cube* cube1 = new Cube(Vector3D(0, 10.0, 0), Quaternion(1.0, 1.0, 1.0, 0.0), 2.0, 1.0);
|
||||
//Cube* cube2 = new Cube(Vector3D(0.0, 0.0, 0.0), Quaternion(1.0, 1.0, 0.0, 0.0), 4.0, 1.0);
|
||||
Cube* cube1 = new Cube(Vector3D(2.2, 17.0, 2.2), Quaternion(1.0, 1.0, 0.3, 0.0), 2.0, 1.0);
|
||||
//Cube* cube2 = new Cube(Vector3D(0.0, 0.0, 0.0), Quaternion(1.0, 1.0, 0.0, 0.0), 4.0, 1.0);
|
||||
//cube1->getRigidBody()->setLinearVelocity(Vector3D(0.9, -0.9, 0.9));
|
||||
//cube2->getRigidBody()->setLinearVelocity(Vector3D(0.0, 0.0, 0.0));
|
||||
//cube2->getRigidBody()->setIsMotionEnabled(false);
|
||||
cube1->getRigidBody()->setRestitution(0.6);
|
||||
//cube2->getRigidBody()->setRestitution(0.6);
|
||||
addObject(cube1);
|
||||
//addObject(cube2);
|
||||
*/
|
||||
|
||||
for (int i=3; i<30; i=i+3) {
|
||||
Cube* cube = new Cube(Vector3D(1, i, 1+i*0.002), Quaternion(1.0, 1.0, 0.0, 0.0), 2.0, 4.0);
|
||||
cube->getRigidBody()->setRestitution(0.5);
|
||||
addObject(cube);
|
||||
}
|
||||
|
||||
Plane* plane1 = new Plane(Vector3D(0.0, 0.0, 0.0), Quaternion(0.0, 1.0, 0.0 , 0.0), 20.0, 30.0, Vector3D(-1.0, 0.0, 0.0), Vector3D(0.0, 0.0, 1.0), Kilogram(1.0));
|
||||
plane1->getRigidBody()->setRestitution(0.7);
|
||||
Plane* plane1 = new Plane(Vector3D(0.0, 0.0, 0.0), Quaternion(0.0, 1.0, 0.1 , 0.0), 20.0, 30.0, Vector3D(-1.0, 0.0, 0.0), Vector3D(0.0, 0.0, 1.0), 1.0);
|
||||
plane1->getRigidBody()->setRestitution(0.5);
|
||||
plane1->getRigidBody()->setIsMotionEnabled(false);
|
||||
addObject(plane1);
|
||||
|
||||
|
||||
/*
|
||||
Cube* cube1 = new Cube(Vector3D(4.0, 11.0, 5.0), Quaternion(1.0, 0.3, 0.8, 0.0), 2.0, Kilogram(3.0));
|
||||
//Cube* cube2 = new Cube(Vector3D(3.0, 9, 3.0), Quaternion(1.0, 1.0, 0.0, 0.0), 2.0, Kilogram(2.0));
|
||||
cube1->getRigidBody()->setRestitution(0.4);
|
||||
//cube2->getRigidBody()->setRestitution(0.4);
|
||||
|
||||
//Cube* cube3 = new Cube(Vector3D(5.0, 13, 0.0), Quaternion(1.0, 1.0, 0.3, 0.0), 2.0, Kilogram(1.0));
|
||||
//cube3->getRigidBody()->setRestitution(0.8);
|
||||
|
||||
Plane* plane1 = new Plane(Vector3D(0.0, 0.0, 0.0), Quaternion(0.0, 1.0, 0.2, 0.0), 20.0, 30.0, Vector3D(-1.0, 0.0, 0.0), Vector3D(0.0, 0.0, 1.0), Kilogram(1.0));
|
||||
plane1->getRigidBody()->setRestitution(0.4);
|
||||
plane1->getRigidBody()->setIsMotionEnabled(false);
|
||||
|
||||
|
||||
addObject(cube1);
|
||||
//addObject(cube2);
|
||||
//addObject(cube3);
|
||||
addObject(plane1);
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -85,10 +77,9 @@ Context::~Context() {
|
|||
|
||||
// Method to get an object from the context
|
||||
Object& Context::getObject(int objectIndex) const {
|
||||
// TODO : WE HAVE TO ADD HERE AN EXCEPTION IMPLEMENTATION
|
||||
|
||||
// Return the object from the context
|
||||
return (*vectObjects.at(objectIndex)); // TODO : THROWN AN EXCEPTION IF INDEX IS OUT OF THE BOUNDS
|
||||
return (*vectObjects.at(objectIndex));
|
||||
}
|
||||
|
||||
// Method for adding an object into the context
|
||||
|
|
|
@ -30,7 +30,7 @@
|
|||
// ----- Class Object ----- //
|
||||
|
||||
// Constructor of the class Object
|
||||
Object::Object(const Vector3D& position, const Quaternion& orientation, const Kilogram& mass, const Matrix3x3& inertiaTensor, const OBB& obb)
|
||||
Object::Object(const Vector3D& position, const Quaternion& orientation, double mass, const Matrix3x3& inertiaTensor, OBB* obb)
|
||||
:rigidBody(new RigidBody(position, orientation, mass, inertiaTensor, obb)) {
|
||||
|
||||
}
|
||||
|
@ -52,11 +52,11 @@ RigidBody* Object::getRigidBody() {
|
|||
const Matrix3x3 Cube::inertiaTensor;
|
||||
|
||||
// Constructor of the class Cube
|
||||
Cube::Cube(const Vector3D& position, const Quaternion& orientation, float size, const Kilogram& mass)
|
||||
:Object(position, orientation, mass, Matrix3x3(1.0/12.0*mass.getValue()*2*size*size, 0.0, 0.0,
|
||||
0.0, 1.0/12.0*mass.getValue()*2*size*size, 0.0,
|
||||
0.0, 0.0, 1.0/12.0*mass.getValue()*2*size*size), OBB(position, Vector3D(1.0, 0.0, 0.0), Vector3D(0.0, 1.0, 0.0), Vector3D(0.0, 0.0, 1.0),
|
||||
size/2.0, size/2.0, size/2)) {
|
||||
Cube::Cube(const Vector3D& position, const Quaternion& orientation, float size, double mass)
|
||||
:Object(position, orientation, mass, Matrix3x3(1.0/12.0*mass*2*size*size, 0.0, 0.0,
|
||||
0.0, 1.0/12.0*mass*2*size*size, 0.0,
|
||||
0.0, 0.0, 1.0/12.0*mass*2*size*size), new OBB(position, Vector3D(1.0, 0.0, 0.0), Vector3D(0.0, 1.0, 0.0), Vector3D(0.0, 0.0, 1.0),
|
||||
size/2.0, size/2.0, size/2)) { // TODO : Construct automatically the OBB and AABB using the computeFromVertices() method
|
||||
this->size = size;
|
||||
}
|
||||
|
||||
|
@ -69,17 +69,18 @@ Cube::~Cube() {
|
|||
void Cube::draw() const {
|
||||
|
||||
// Get the interpolated state of the rigid body
|
||||
BodyState state = rigidBody->getInterpolatedState();
|
||||
Vector3D position = rigidBody->getInterpolatedPosition();
|
||||
Quaternion orientation = rigidBody->getInterpolatedOrientation();
|
||||
|
||||
// Position of the cube
|
||||
double x = state.getPosition().getX();
|
||||
double y = state.getPosition().getY();
|
||||
double z = state.getPosition().getZ();
|
||||
double x = position.getX();
|
||||
double y = position.getY();
|
||||
double z = position.getZ();
|
||||
|
||||
// Orientation of the cube
|
||||
Vector3D orientationAxis;
|
||||
double orientationAngle;
|
||||
state.getOrientation().getRotationAngleAxis(orientationAngle, orientationAxis);
|
||||
orientation.getRotationAngleAxis(orientationAngle, orientationAxis);
|
||||
|
||||
// Translation of the cube to its position
|
||||
glTranslatef(x, y, z);
|
||||
|
@ -95,18 +96,18 @@ void Cube::draw() const {
|
|||
// ----- Class Plane ----- //
|
||||
|
||||
// Constructor of the class Plane
|
||||
Plane::Plane(const Vector3D& position, const Quaternion& orientation, float width, float height, const Vector3D& d1, const Vector3D& d2, const Kilogram& mass)
|
||||
:Object(position, orientation, mass, Matrix3x3(1.0/12.0*mass.getValue()*height*height, 0.0, 0.0,
|
||||
0.0, 1.0/12.0*mass.getValue()*(width*width+height*height), 0.0,
|
||||
0.0, 0.0, 1.0/12.0*mass.getValue()*width*width), OBB(position, Vector3D(1.0, 0.0, 0.0), Vector3D(0.0, 1.0, 0.0), Vector3D(0.0, 0.0, 1.0),
|
||||
width/2, 0.5, height/2)) { // TODO : Change the height of the OBB
|
||||
Plane::Plane(const Vector3D& position, const Quaternion& orientation, float width, float height, const Vector3D& d1, const Vector3D& d2, double mass)
|
||||
:Object(position, orientation, mass, Matrix3x3(1.0/12.0*mass*height*height, 0.0, 0.0,
|
||||
0.0, 1.0/12.0*mass*(width*width+height*height), 0.0,
|
||||
0.0, 0.0, 1.0/12.0*mass*width*width), new OBB(position, Vector3D(1.0, 0.0, 0.0), Vector3D(0.0, 1.0, 0.0), Vector3D(0.0, 0.0, 1.0),
|
||||
width/2.0, 0.5, height/2.0)) { // TODO : Construct automatically the OBB and AABB using the computeFromVertices() method
|
||||
this->width = width;
|
||||
this->height = height;
|
||||
this->d1 = d1;
|
||||
this->d2 = d2;
|
||||
|
||||
// By default Planes in the demo cannot move
|
||||
rigidBody->setIsMotionEnabled(false);
|
||||
//rigidBody->setIsMotionEnabled(false);
|
||||
|
||||
// Compute the unit normal vector of the plane by a cross product
|
||||
normalVector = d1.crossProduct(d2).getUnit();
|
||||
|
@ -121,12 +122,13 @@ Plane::~Plane() {
|
|||
void Plane::draw() const {
|
||||
|
||||
// Get the interpolated state of the rigid body
|
||||
BodyState state = rigidBody->getInterpolatedState();
|
||||
Vector3D position = rigidBody->getInterpolatedPosition();
|
||||
Quaternion orientation = rigidBody->getInterpolatedOrientation();
|
||||
|
||||
// Get the position of the rigid body
|
||||
double x = state.getPosition().getX();
|
||||
double y = state.getPosition().getY();
|
||||
double z = state.getPosition().getZ();
|
||||
// Position of the cube
|
||||
double x = position.getX();
|
||||
double y = position.getY();
|
||||
double z = position.getZ();
|
||||
|
||||
// Translation of the cube to its position
|
||||
glTranslatef(x, y, z);
|
||||
|
|
|
@ -37,7 +37,7 @@ class Object {
|
|||
RigidBody* rigidBody; // Rigid Body that represents the object
|
||||
|
||||
public :
|
||||
Object(const Vector3D& position, const Quaternion& orientation, const Kilogram& mass, const Matrix3x3& inertiaTensor, const OBB& obb); // Constructor of the class Object
|
||||
Object(const Vector3D& position, const Quaternion& orientation, double mass, const Matrix3x3& inertiaTensor, OBB* obb); // Constructor of the class Object
|
||||
virtual ~Object(); // Destructor of the class Object
|
||||
|
||||
virtual void draw() const =0; // pure virtual method to draw the object
|
||||
|
@ -51,7 +51,7 @@ class Cube : public Object {
|
|||
float size; // Size of a side in the box
|
||||
static const Matrix3x3 inertiaTensor; // Inertia tensor of a box
|
||||
public :
|
||||
Cube(const Vector3D& position, const Quaternion& orientation, float size, const Kilogram& mass); // Constructor of the class cube
|
||||
Cube(const Vector3D& position, const Quaternion& orientation, float size, double mass); // Constructor of the class cube
|
||||
virtual ~Cube(); // Destructor of the class cube
|
||||
virtual void draw() const; // Method to draw the cube
|
||||
};
|
||||
|
@ -68,7 +68,7 @@ class Plane : public Object {
|
|||
Vector3D normalVector; // Unit normal vector of the plane
|
||||
|
||||
public :
|
||||
Plane(const Vector3D& position, const Quaternion& orientation, float width, float height, const Vector3D& d1, const Vector3D& d2, const Kilogram& mass); // Constructor of the class Plane
|
||||
Plane(const Vector3D& position, const Quaternion& orientation, float width, float height, const Vector3D& d1, const Vector3D& d2, double mass); // Constructor of the class Plane
|
||||
virtual ~Plane(); // Destructor of the class Plane
|
||||
virtual void draw() const; // Method to draw the plane
|
||||
};
|
||||
|
|
|
@ -100,7 +100,7 @@ void Scene::display(const Context& context) const {
|
|||
{
|
||||
|
||||
// Copy the active matrix on the matrix stack
|
||||
glPushMatrix();
|
||||
glPushMatrix();
|
||||
|
||||
// Draw the object
|
||||
context.getObject(i).draw();
|
||||
|
@ -108,8 +108,8 @@ void Scene::display(const Context& context) const {
|
|||
glPopMatrix();
|
||||
glPushMatrix();
|
||||
|
||||
// Draw the bounding volume
|
||||
context.getObject(i).getRigidBody()->getOBB()->draw();
|
||||
// Draw the bounding volume
|
||||
context.getObject(i).getRigidBody()->getNarrowBoundingVolume()->draw();
|
||||
|
||||
// Remove the matrix on the top of the matrix stack
|
||||
glPopMatrix();
|
||||
|
|
|
@ -27,8 +27,9 @@ using namespace reactphysics3d;
|
|||
|
||||
// Constructor of the class Simulation
|
||||
Simulation::Simulation()
|
||||
:world(new PhysicsWorld(Vector3D(0.0, -9.8, 0.0))), engine(world, Time(0.01)), scene(this->world) { // TODO : Change the timestep here after debugging
|
||||
:world(new PhysicsWorld(Vector3D(0.0, -9.8, 0.0))), engine(new PhysicsEngine(world, 0.005)), scene(this->world) { // TODO : Change the timestep here after debugging
|
||||
simRunning = false;
|
||||
isStarted = false;
|
||||
mouseButtonPressed = false;
|
||||
nbFrame = 0;
|
||||
lastFrameTime = 0.0;
|
||||
|
@ -39,6 +40,7 @@ Simulation::Simulation()
|
|||
Simulation::~Simulation() {
|
||||
// Delete the physics world object
|
||||
delete world;
|
||||
delete engine;
|
||||
}
|
||||
|
||||
// Method to start the simulation
|
||||
|
@ -56,79 +58,41 @@ void Simulation::start() {
|
|||
|
||||
// Activation of the simulation
|
||||
simRunning = true;
|
||||
isStarted = true;
|
||||
|
||||
// Get the current time
|
||||
lastFrameTime = SDL_GetTicks();
|
||||
|
||||
PhysicsEngine* pEngine = &engine;
|
||||
//lastFrameTime = SDL_GetTicks();
|
||||
|
||||
// Initialize the display time
|
||||
pEngine->initializeDisplayTime(Time(SDL_GetTicks()/1000.0));
|
||||
//engine->initializeDisplayTime(SDL_GetTicks()/1000.0);
|
||||
|
||||
// Start the physics simulation
|
||||
pEngine->start();
|
||||
engine->start();
|
||||
|
||||
//double time = 1.0;
|
||||
|
||||
// Main loop of the simulation
|
||||
while(simRunning) {
|
||||
// Check if an SDL event occured and make the apropriate actions
|
||||
checkEvents();
|
||||
|
||||
double time = SDL_GetTicks()/1000.0;
|
||||
//time += 0.01;
|
||||
|
||||
//std::cout << "************************************************* Time : " << time << std::endl;
|
||||
|
||||
// Update the display time
|
||||
pEngine->updateDisplayTime(Time(time));
|
||||
//engine->updateDisplayTime(time);
|
||||
|
||||
// Update the physics
|
||||
pEngine->update();
|
||||
if (isStarted)
|
||||
engine->update();
|
||||
|
||||
// Check if an SDL event occured and make the apropriate actions
|
||||
checkEvents();
|
||||
|
||||
// Display the actual scene
|
||||
scene.display(context);
|
||||
|
||||
// Compute the fps (framerate)
|
||||
computeFps();
|
||||
//std::cout << "FPS : " << fps << std::endl;
|
||||
|
||||
/*
|
||||
BodyState state = context.getObject(0).getRigidBody()->getInterpolatedState();
|
||||
Vector3D velocity = context.getObject(0).getRigidBody()->getInterpolatedState().getAngularVelocity();
|
||||
//std::cout << "Velocity 0 : " << velocity.getX() << ", " << velocity.getY() << ", " << velocity.getZ() << ")" << std::endl;
|
||||
double x = state.getPosition().getX();
|
||||
double y = state.getPosition().getY();
|
||||
double z = state.getPosition().getZ();
|
||||
std::cout << "Position Cube 0 : (" << x << ", " << y << ", " << z << ")" << std::endl;
|
||||
std::cout << "angular velocity 0 : " << velocity.length() << std::endl;;
|
||||
|
||||
BodyState state1 = context.getObject(1).getRigidBody()->getInterpolatedState();
|
||||
Vector3D velocity1 = context.getObject(1).getRigidBody()->getInterpolatedState().getAngularVelocity();
|
||||
//std::cout << "Velocity 1 : " << velocity1.getX() << ", " << velocity1.getY() << ", " << velocity1.getZ() << ")" << std::endl;
|
||||
double x1 = state1.getPosition().getX();
|
||||
double y1 = state1.getPosition().getY();
|
||||
double z1 = state1.getPosition().getZ();
|
||||
std::cout << "Position Cube 1 : (" << x1 << ", " << y1 << ", " << z1 << ")" << std::endl;
|
||||
std::cout << "angular velocity 1 : " << velocity1.length() << std::endl;
|
||||
|
||||
BodyState state2 = context.getObject(2).getRigidBody()->getInterpolatedState();
|
||||
Quaternion velocity2 = context.getObject(2).getRigidBody()->getInterpolatedState().getOrientation();
|
||||
//std::cout << "Velocity 2 : " << velocity2.getX() << ", " << velocity2.getY() << ", " << velocity2.getZ() << ")" << std::endl;
|
||||
double x2 = state2.getPosition().getX();
|
||||
double y2 = state2.getPosition().getY();
|
||||
double z2 = state2.getPosition().getZ();
|
||||
std::cout << "Position Cube 2: (" << x2 << ", " << y2 << ", " << z2 << ")" << std::endl;
|
||||
std::cout << "quaternion orientation 2 : " << velocity2.getX() << ", " << velocity2.getY() << ", " << velocity2.getZ() << ", " << velocity2.getW() << ")" << std::endl;;
|
||||
*/
|
||||
|
||||
/*
|
||||
double a;
|
||||
if (time > 5.0) {
|
||||
std::cin >> a;
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -155,7 +119,15 @@ void Simulation::checkEvents() {
|
|||
// A keyboard key has been pushed
|
||||
case SDL_KEYDOWN: // The Esc key has been pushed then we end the simulation
|
||||
if (event.key.keysym.sym == SDLK_ESCAPE)
|
||||
simRunning = false;
|
||||
//simRunning = false;
|
||||
if (isStarted) {
|
||||
engine->stop();
|
||||
isStarted = false;
|
||||
}
|
||||
else {
|
||||
engine->start();
|
||||
isStarted = true;
|
||||
}
|
||||
break;
|
||||
|
||||
// The size of the windows changed then we reshape the windows
|
||||
|
|
|
@ -17,34 +17,35 @@
|
|||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef SIMULATION_H
|
||||
#define SIMULATION_H
|
||||
|
||||
// Librairies
|
||||
#include "Context.h"
|
||||
#ifndef SIMULATION_H
|
||||
#define SIMULATION_H
|
||||
|
||||
// Librairies
|
||||
#include "Context.h"
|
||||
#include "Scene.h"
|
||||
#include "../reactphysics3d/reactphysics3d.h"
|
||||
|
||||
// Class Simulation
|
||||
class Simulation {
|
||||
#include "../reactphysics3d/reactphysics3d.h"
|
||||
|
||||
// Class Simulation
|
||||
class Simulation {
|
||||
private :
|
||||
rp3d::PhysicsWorld* world; // Pointer to the collision world that contains bodies of the simulation
|
||||
Scene scene; // Scene object for displaying the simulation
|
||||
rp3d::PhysicsWorld* world; // Pointer to the collision world that contains bodies of the simulation
|
||||
Scene scene; // Scene object for displaying the simulation
|
||||
Context context; // Context of the simulation
|
||||
rp3d::PhysicsEngine engine; // Collision engine for the physics of the simulation
|
||||
bool simRunning; // True if the simulation is running and false otherwise
|
||||
rp3d::PhysicsEngine* engine; // Collision engine for the physics of the simulation
|
||||
bool simRunning; // True if the simulation is running and false otherwise
|
||||
bool isStarted;
|
||||
bool mouseButtonPressed; // True if the left mouse button is pressed
|
||||
double lastFrameTime; // Last frame time
|
||||
int nbFrame; // Number of frame (used to compute the framerate)
|
||||
double fps; // Framerate of the application
|
||||
|
||||
void computeFps(); // Compute the framerate of the application
|
||||
|
||||
public :
|
||||
Simulation(); // Constructor of the class
|
||||
~Simulation(); // Destructor of the class
|
||||
void start(); // Start the simulation
|
||||
void checkEvents(); // Check if SDL events occured and make the apropriate actions
|
||||
};
|
||||
|
||||
#endif
|
||||
void computeFps(); // Compute the framerate of the application
|
||||
|
||||
public :
|
||||
Simulation(); // Constructor of the class
|
||||
~Simulation(); // Destructor of the class
|
||||
void start(); // Start the simulation
|
||||
void checkEvents(); // Check if SDL events occured and make the apropriate actions
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,21 +1,26 @@
|
|||
/****************************************************************************
|
||||
* Copyright (C) 2009 Daniel Chappuis *
|
||||
****************************************************************************
|
||||
* This file is part of ReactPhysics3D. *
|
||||
* *
|
||||
* ReactPhysics3D 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 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* ReactPhysics3D 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 ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
/********************************************************************************
|
||||
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
||||
* Copyright (c) 2010 Daniel Chappuis *
|
||||
*********************************************************************************
|
||||
* *
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy *
|
||||
* of this software and associated documentation files (the "Software"), to deal *
|
||||
* in the Software without restriction, including without limitation the rights *
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
|
||||
* copies of the Software, and to permit persons to whom the Software is *
|
||||
* furnished to do so, subject to the following conditions: *
|
||||
* *
|
||||
* The above copyright notice and this permission notice shall be included in *
|
||||
* all copies or substantial portions of the Software. *
|
||||
* *
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN *
|
||||
* THE SOFTWARE. *
|
||||
********************************************************************************/
|
||||
|
||||
// Libraries
|
||||
#include "LCPProjectedGaussSeidel.h"
|
||||
|
|
|
@ -1,21 +1,26 @@
|
|||
/****************************************************************************
|
||||
* Copyright (C) 2009 Daniel Chappuis *
|
||||
****************************************************************************
|
||||
* This file is part of ReactPhysics3D. *
|
||||
* *
|
||||
* ReactPhysics3D 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 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* ReactPhysics3D 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 ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
/********************************************************************************
|
||||
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
||||
* Copyright (c) 2010 Daniel Chappuis *
|
||||
*********************************************************************************
|
||||
* *
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy *
|
||||
* of this software and associated documentation files (the "Software"), to deal *
|
||||
* in the Software without restriction, including without limitation the rights *
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
|
||||
* copies of the Software, and to permit persons to whom the Software is *
|
||||
* furnished to do so, subject to the following conditions: *
|
||||
* *
|
||||
* The above copyright notice and this permission notice shall be included in *
|
||||
* all copies or substantial portions of the Software. *
|
||||
* *
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN *
|
||||
* THE SOFTWARE. *
|
||||
********************************************************************************/
|
||||
|
||||
#ifndef LCPPROJECTEDGAUSSSEIDEL_H
|
||||
#define LCPPROJECTEDGAUSSSEIDEL_H
|
||||
|
|
|
@ -1,21 +1,26 @@
|
|||
/****************************************************************************
|
||||
* Copyright (C) 2009 Daniel Chappuis *
|
||||
****************************************************************************
|
||||
* This file is part of ReactPhysics3D. *
|
||||
* *
|
||||
* ReactPhysics3D 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 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* ReactPhysics3D 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 ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
/********************************************************************************
|
||||
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
||||
* Copyright (c) 2010 Daniel Chappuis *
|
||||
*********************************************************************************
|
||||
* *
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy *
|
||||
* of this software and associated documentation files (the "Software"), to deal *
|
||||
* in the Software without restriction, including without limitation the rights *
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
|
||||
* copies of the Software, and to permit persons to whom the Software is *
|
||||
* furnished to do so, subject to the following conditions: *
|
||||
* *
|
||||
* The above copyright notice and this permission notice shall be included in *
|
||||
* all copies or substantial portions of the Software. *
|
||||
* *
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN *
|
||||
* THE SOFTWARE. *
|
||||
********************************************************************************/
|
||||
|
||||
// Libraries
|
||||
#include "LCPSolver.h"
|
||||
|
|
|
@ -1,21 +1,26 @@
|
|||
/****************************************************************************
|
||||
* Copyright (C) 2009 Daniel Chappuis *
|
||||
****************************************************************************
|
||||
* This file is part of ReactPhysics3D. *
|
||||
* *
|
||||
* ReactPhysics3D 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 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* ReactPhysics3D 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 ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
/********************************************************************************
|
||||
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
||||
* Copyright (c) 2010 Daniel Chappuis *
|
||||
*********************************************************************************
|
||||
* *
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy *
|
||||
* of this software and associated documentation files (the "Software"), to deal *
|
||||
* in the Software without restriction, including without limitation the rights *
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
|
||||
* copies of the Software, and to permit persons to whom the Software is *
|
||||
* furnished to do so, subject to the following conditions: *
|
||||
* *
|
||||
* The above copyright notice and this permission notice shall be included in *
|
||||
* all copies or substantial portions of the Software. *
|
||||
* *
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN *
|
||||
* THE SOFTWARE. *
|
||||
********************************************************************************/
|
||||
|
||||
#ifndef LCPSOLVER_H
|
||||
#define LCPSOLVER_H
|
||||
|
|
|
@ -1,61 +0,0 @@
|
|||
/****************************************************************************
|
||||
* Copyright (C) 2009 Daniel Chappuis *
|
||||
****************************************************************************
|
||||
* This file is part of ReactPhysics3D. *
|
||||
* *
|
||||
* ReactPhysics3D 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 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* ReactPhysics3D 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 ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
// Libraries
|
||||
#include <iostream>
|
||||
#include "TestSuite/Suite.h"
|
||||
#include "testing_mathematics/MathematicsTest.h"
|
||||
#include "testing_mathematics/VectorTest.h"
|
||||
#include "testing_mathematics/Vector3DTest.h"
|
||||
#include "testing_mathematics/MatrixTest.h"
|
||||
#include "testing_mathematics/Matrix3x3Test.h"
|
||||
#include "testing_mathematics/QuaternionTest.h"
|
||||
#include "testing_physics/TimeTest.h"
|
||||
#include "testing_physics/KilogramTest.h"
|
||||
|
||||
// Namespaces
|
||||
using namespace std;
|
||||
using namespace TestSuite;
|
||||
|
||||
// Main function
|
||||
int main() {
|
||||
|
||||
// ReactPhysics3D TestSuite
|
||||
Suite reactphysics3DTestSuite("ReactPhysics3D TestSuite");
|
||||
|
||||
// Mathematics tests
|
||||
reactphysics3DTestSuite.addTest(new MathematicsTest);
|
||||
reactphysics3DTestSuite.addTest(new VectorTest);
|
||||
reactphysics3DTestSuite.addTest(new Vector3DTest);
|
||||
reactphysics3DTestSuite.addTest(new MatrixTest);
|
||||
reactphysics3DTestSuite.addTest(new Matrix3x3Test);
|
||||
reactphysics3DTestSuite.addTest(new QuaternionTest);
|
||||
|
||||
// Physics tests
|
||||
reactphysics3DTestSuite.addTest(new TimeTest);
|
||||
reactphysics3DTestSuite.addTest(new KilogramTest);
|
||||
|
||||
// Run the ReactPhysics3D TestSuite and display the report
|
||||
reactphysics3DTestSuite.run();
|
||||
long nbFailures = reactphysics3DTestSuite.report();
|
||||
reactphysics3DTestSuite.free();
|
||||
return nbFailures;
|
||||
double inPause;
|
||||
cin >> inPause;
|
||||
}
|
|
@ -1,63 +0,0 @@
|
|||
/****************************************************************************
|
||||
* Copyright (C) 2009 Daniel Chappuis *
|
||||
****************************************************************************
|
||||
* This file is part of ReactPhysics3D. *
|
||||
* *
|
||||
* ReactPhysics3D 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 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* ReactPhysics3D 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 ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef MATHEMATICSTEST_H
|
||||
#define MATHEMATICSTEST_H
|
||||
|
||||
// Libraries
|
||||
#include "../TestSuite/Test.h"
|
||||
#include "../../mathematics/mathematics.h"
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
|
||||
// Namespaces
|
||||
using namespace reactphysics3d;
|
||||
|
||||
// Class MathematicsTest
|
||||
class MathematicsTest : public TestSuite::Test {
|
||||
private :
|
||||
|
||||
public :
|
||||
|
||||
// Constructor
|
||||
MathematicsTest() {
|
||||
|
||||
}
|
||||
|
||||
// Run method of the Test
|
||||
void run() {
|
||||
testEqual();
|
||||
}
|
||||
|
||||
// Test the equal() method
|
||||
void testEqual() {
|
||||
double number1 = 19.13417;
|
||||
double number2 = 19.13417 + EPSILON/2.0;
|
||||
double number3 = 19.13417 + 2*EPSILON;
|
||||
|
||||
test_(equal(number1, number2));
|
||||
test_(equal(number2, number1));
|
||||
test_(!equal(number1, number3));
|
||||
test_(!equal(number3, number1));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -1,417 +0,0 @@
|
|||
/****************************************************************************
|
||||
* Copyright (C) 2009 Daniel Chappuis *
|
||||
****************************************************************************
|
||||
* This file is part of ReactPhysics3D. *
|
||||
* *
|
||||
* ReactPhysics3D 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 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* ReactPhysics3D 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 ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef MATRIX3DTEST_H
|
||||
#define MATRIX3DTEST_H
|
||||
|
||||
// Libraries
|
||||
#include "../TestSuite/Test.h"
|
||||
#include "../../mathematics/Matrix3x3.h"
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
|
||||
// Namespaces
|
||||
using namespace reactphysics3d;
|
||||
|
||||
// Class MatrixTest
|
||||
class Matrix3x3Test : public TestSuite::Test {
|
||||
private :
|
||||
Matrix3x3 matrix1;
|
||||
Matrix3x3 matrix2;
|
||||
public :
|
||||
|
||||
// Constructor
|
||||
Matrix3x3Test() {
|
||||
matrix1.setAllValues(3, 7, -5, 13, -1, 2, 6, 5, 9);
|
||||
matrix2.setAllValues(-13, 8, 2, 5, -25, 11, -7, 6, 21);
|
||||
}
|
||||
|
||||
// Run method of the Test
|
||||
void run() {
|
||||
testConstructors();
|
||||
testGetValue();
|
||||
testSetValue();
|
||||
testSetAllValues() ;
|
||||
testGetTranspose();
|
||||
testGetInverse();
|
||||
testGetDeterminant();
|
||||
testGetTrace();
|
||||
testGetQuaternion();
|
||||
testIdentityMatrix();
|
||||
testOperatorAddition();
|
||||
testOperatorSubstraction();
|
||||
testOperatorMultiplicationWithConstant();
|
||||
testOperatorMultiplicationWithMatrix();
|
||||
testOperatorMultiplicationWithVector();
|
||||
testOperatorAssignment();
|
||||
testOperatorEquality();
|
||||
}
|
||||
|
||||
// Test the constructors
|
||||
void testConstructors() {
|
||||
|
||||
// Constructor without argument
|
||||
Matrix3x3 matrix; // This shouldn't throw an exception
|
||||
test_(matrix.getValue(0,0) == 0);
|
||||
test_(matrix.getValue(1,2) == 0);
|
||||
test_(matrix.getValue(0,2) == 0);
|
||||
|
||||
// Constructor with arguments
|
||||
Matrix3x3 matrix3(1, 2, 3, 4, 5, 6, 7, 8, 9);
|
||||
test_(matrix3.getValue(0,0) == 1);
|
||||
test_(matrix3.getValue(0,1) == 2);
|
||||
test_(matrix3.getValue(0,2) == 3);
|
||||
test_(matrix3.getValue(1,0) == 4);
|
||||
test_(matrix3.getValue(1,1) == 5);
|
||||
test_(matrix3.getValue(1,2) == 6);
|
||||
test_(matrix3.getValue(2,0) == 7);
|
||||
test_(matrix3.getValue(2,1) == 8);
|
||||
test_(matrix3.getValue(2,2) == 9);
|
||||
|
||||
// Copy-constructor
|
||||
Matrix3x3 matrix4 = matrix3;
|
||||
test_(matrix4.getValue(0,0) == 1);
|
||||
test_(matrix4.getValue(0,1) == 2);
|
||||
test_(matrix4.getValue(0,2) == 3);
|
||||
test_(matrix4.getValue(1,0) == 4);
|
||||
test_(matrix4.getValue(1,1) == 5);
|
||||
test_(matrix4.getValue(1,2) == 6);
|
||||
test_(matrix4.getValue(2,0) == 7);
|
||||
test_(matrix4.getValue(2,1) == 8);
|
||||
test_(matrix4.getValue(2,2) == 9);
|
||||
|
||||
// Conversion-constructor (Quaternion --> Matrix3x3)
|
||||
|
||||
// Rotation matrix of a rotation of 180 degrees around x axis
|
||||
Matrix3x3 rotation1(1, 0, 0, 0, -1, 0, 0, 0, -1);
|
||||
Quaternion quaternion1(1, 0, 0, 0);
|
||||
Matrix3x3 matrix5(quaternion1); // Convert the quaternion into a matrix
|
||||
test_(matrix5 == rotation1); // Check if the matrix result and the rotation matrix are the same
|
||||
|
||||
// Rotation matrix of a rotation of 180 degrees around y axis
|
||||
Matrix3x3 rotation2(-1, 0, 0, 0, 1, 0, 0, 0, -1);
|
||||
Quaternion quaternion2(0, 1, 0, 0);
|
||||
Matrix3x3 matrix6(quaternion2); // Convert the quaternion into a matrix
|
||||
test_(matrix6 == rotation2); // Check if the matrix result and the rotation matrix are the same
|
||||
|
||||
// Rotation matrix of a rotation of 180 degrees around z axis
|
||||
Matrix3x3 rotation3(-1, 0, 0, 0, -1, 0, 0, 0, 1);
|
||||
Quaternion quaternion3(0, 0, 1, 0);
|
||||
Matrix3x3 matrix7(quaternion3); // Convert the quaternion into a matrix
|
||||
test_(matrix7 == rotation3); // Check if the matrix result and the rotation matrix are the same
|
||||
}
|
||||
|
||||
// Test getValue()
|
||||
void testGetValue() {
|
||||
// Try a valid getValue()
|
||||
try {
|
||||
test_(matrix1.getValue(0, 0) == 3); // This shouldn't throw an exception
|
||||
test_(matrix1.getValue(1, 0) == 13); // This shouldn't throw an exception
|
||||
test_(matrix1.getValue(1, 2) == 2); // This shouldn't throw an exception
|
||||
test_(matrix1.getValue(1, 1) == -1); // This shouldn't throw an exception
|
||||
succeed_(); // Succeed if no exceptions have been thrown
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
fail_("Valid getValue() call throws an exception"); // Failed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try an invalid getValue() call
|
||||
try {
|
||||
matrix1.getValue(-1, 0); // This should throw an exception
|
||||
fail_("Invalid getValue() call undetected");
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try an invalid getValue() call
|
||||
try {
|
||||
matrix1.getValue(0, 3); // This should throw an exception
|
||||
fail_("Invalid getValue() call undetected");
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
}
|
||||
|
||||
// Test setValue()
|
||||
void testSetValue() {
|
||||
|
||||
Matrix3x3 matrix;
|
||||
|
||||
// Try a valid setValue()
|
||||
try {
|
||||
matrix.setValue(0, 0, 18); // This shouldn't throw an exception
|
||||
matrix.setValue(0, 2, -6); // This shouldn't throw an exception
|
||||
matrix.setValue(1, 0, -44); // This shouldn't throw an exception
|
||||
matrix.setValue(1, 2, 21); // This shouldn't throw an exception
|
||||
matrix.setValue(1, 1, 5); // This shouldn't throw an exception
|
||||
test_(matrix.getValue(0, 0) == 18);
|
||||
test_(matrix.getValue(0, 2) == -6);
|
||||
test_(matrix.getValue(1, 0) == -44);
|
||||
test_(matrix.getValue(1, 2) == 21);
|
||||
test_(matrix.getValue(1, 1) == 5);
|
||||
succeed_(); // Succeed if no exceptions have been thrown
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
fail_("Valid setValue() call throws an exception"); // Failed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try an invalid setValue() call
|
||||
try {
|
||||
matrix.setValue(-1, 0, 42); // This should throw an exception
|
||||
fail_("Invalid setValue() call undetected"); // Failed if no exception have been thrown
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try an invalid setValue() call
|
||||
try {
|
||||
matrix1.setValue(0, 3, 53); // This should throw an exception
|
||||
fail_("Invalid setValue() call undetected"); // Failed if no exception have been thrown
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
}
|
||||
|
||||
// Test setAllValues()
|
||||
void testSetAllValues() {
|
||||
Matrix3x3 matrix;
|
||||
matrix.setAllValues(1,2,3,4,5,6,7,8,9);
|
||||
test_(matrix.getValue(0,0) == 1);
|
||||
test_(matrix.getValue(0,1) == 2);
|
||||
test_(matrix.getValue(0,2) == 3);
|
||||
test_(matrix.getValue(1,0) == 4);
|
||||
test_(matrix.getValue(1,1) == 5);
|
||||
test_(matrix.getValue(1,2) == 6);
|
||||
test_(matrix.getValue(2,0) == 7);
|
||||
test_(matrix.getValue(2,1) == 8);
|
||||
test_(matrix.getValue(2,2) == 9);
|
||||
}
|
||||
|
||||
// Test getTranspose()
|
||||
void testGetTranspose() {
|
||||
// Get the transpose of matrix1
|
||||
Matrix3x3 matrix = matrix1.getTranspose();
|
||||
|
||||
// Test the transpose matrix
|
||||
test_(matrix.getValue(0, 0) == 3);
|
||||
test_(matrix.getValue(0, 1) == 13);
|
||||
test_(matrix.getValue(0, 2) == 6);
|
||||
test_(matrix.getValue(1, 0) == 7);
|
||||
test_(matrix.getValue(1, 1) == -1);
|
||||
test_(matrix.getValue(1, 2) == 5);
|
||||
test_(matrix.getValue(2, 0) == -5);
|
||||
test_(matrix.getValue(2, 1) == 2);
|
||||
test_(matrix.getValue(2, 2) == 9);
|
||||
}
|
||||
|
||||
// Test getInverse()
|
||||
void testGetInverse() {
|
||||
|
||||
// Construct a 3x3 matrix
|
||||
Matrix3x3 matrix(0, 1, 2, 1, 0, 3, 4, -3, 8);
|
||||
|
||||
// Try to inverse a invertible matrix
|
||||
try {
|
||||
Matrix3x3 result = matrix.getInverse(); // This shouldn't thrown an exception
|
||||
test_(result.getValue(0, 0) == -4.5);
|
||||
test_(result.getValue(0, 1) == 7);
|
||||
test_(result.getValue(0, 2) == -3.0/2.0);
|
||||
test_(result.getValue(1, 0) == -2);
|
||||
test_(result.getValue(1, 1) == 4);
|
||||
test_(result.getValue(1, 2) == -1);
|
||||
test_(result.getValue(2, 0) == 3.0/2.0);
|
||||
test_(result.getValue(2, 1) == -2);
|
||||
test_(result.getValue(2, 2) == 1.0/2.0);
|
||||
succeed_(); // Succeed if no exceptions have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
fail_("Valid getInverse() call throws an exception"); // Failed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try to inverse a square non-invertible matrix (determinant equal to zero)
|
||||
try {
|
||||
Matrix3x3 matrix4;
|
||||
matrix4.getInverse(); // This should throw an exception
|
||||
fail_("Invalid getInverse() call undetected (non-invertible matrix)"); // Failed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
}
|
||||
|
||||
// Test getDeterminant()
|
||||
void testGetDeterminant() {
|
||||
Matrix3x3 matrix;
|
||||
test_(matrix.getDeterminant() == 0);
|
||||
test_(matrix1.getDeterminant() == -1147);
|
||||
test_(matrix2.getDeterminant() == 5937);
|
||||
}
|
||||
|
||||
// Test getTrace()
|
||||
void testGetTrace() {
|
||||
Matrix3x3 matrix;
|
||||
test_(matrix.getTrace() == 0);
|
||||
test_(matrix1.getTrace() == 11);
|
||||
test_(matrix2.getTrace() == -17);
|
||||
}
|
||||
|
||||
// Test getQuaternion()
|
||||
void testGetQuaternion() {
|
||||
// Rotation matrix of a rotation of 180 degrees around x axis
|
||||
Matrix3x3 rotation1(1, 0, 0, 0, -1, 0, 0, 0, -1);
|
||||
|
||||
// Convert the matrix into a quaternion
|
||||
Quaternion quaternion1 = rotation1.getQuaternion();
|
||||
test_(quaternion1.getX() == 1);
|
||||
test_(quaternion1.getY() == 0);
|
||||
test_(quaternion1.getZ() == 0);
|
||||
test_(quaternion1.getW() == 0);
|
||||
|
||||
// Rotation matrix of a rotation of 180 degrees around y axis
|
||||
Matrix3x3 rotation2(-1, 0, 0, 0, 1, 0, 0, 0, -1);
|
||||
|
||||
// Convert the matrix into a quaternion
|
||||
Quaternion quaternion2 = rotation2.getQuaternion();
|
||||
test_(quaternion2.getX() == 0);
|
||||
test_(quaternion2.getY() == 1);
|
||||
test_(quaternion2.getZ() == 0);
|
||||
test_(quaternion2.getW() == 0);
|
||||
|
||||
// Rotation matrix of a rotation of 180 degrees around z axis
|
||||
Matrix3x3 rotation3(-1, 0, 0, 0, -1, 0, 0, 0, 1);
|
||||
|
||||
// Convert the matrix into a quaternion
|
||||
Quaternion quaternion3 = rotation3.getQuaternion();
|
||||
test_(quaternion3.getX() == 0);
|
||||
test_(quaternion3.getY() == 0);
|
||||
test_(quaternion3.getZ() == 1);
|
||||
test_(quaternion3.getW() == 0);
|
||||
}
|
||||
|
||||
// Test identityMatrix()
|
||||
void testIdentityMatrix() {
|
||||
Matrix3x3 matrix = Matrix3x3::identity();
|
||||
test_(matrix.getValue(0, 0) == 1);
|
||||
test_(matrix.getValue(0, 1) == 0);
|
||||
test_(matrix.getValue(0, 2) == 0);
|
||||
test_(matrix.getValue(1, 0) == 0);
|
||||
test_(matrix.getValue(1, 1) == 1);
|
||||
test_(matrix.getValue(1, 2) == 0);
|
||||
test_(matrix.getValue(2, 0) == 0);
|
||||
test_(matrix.getValue(2, 1) == 0);
|
||||
test_(matrix.getValue(2, 2) == 1);
|
||||
}
|
||||
|
||||
// Test operator+()
|
||||
void testOperatorAddition() {
|
||||
Matrix3x3 result = matrix1 + matrix2;
|
||||
test_(result.getValue(0,0) == -10);
|
||||
test_(result.getValue(0,1) == 15);
|
||||
test_(result.getValue(0,2) == -3);
|
||||
test_(result.getValue(1,0) == 18);
|
||||
test_(result.getValue(1,1) == -26);
|
||||
test_(result.getValue(1,2) == 13);
|
||||
test_(result.getValue(2,0) == -1);
|
||||
test_(result.getValue(2,1) == 11);
|
||||
test_(result.getValue(2,2) == 30);
|
||||
}
|
||||
|
||||
// Test operator-()
|
||||
void testOperatorSubstraction() {
|
||||
Matrix3x3 result = matrix1 - matrix2;
|
||||
test_(result.getValue(0,0) == 16);
|
||||
test_(result.getValue(0,1) == -1);
|
||||
test_(result.getValue(0,2) == -7);
|
||||
test_(result.getValue(1,0) == 8);
|
||||
test_(result.getValue(1,1) == 24);
|
||||
test_(result.getValue(1,2) == -9);
|
||||
test_(result.getValue(2,0) == 13);
|
||||
test_(result.getValue(2,1) == -1);
|
||||
test_(result.getValue(2,2) == -12);
|
||||
}
|
||||
|
||||
// Test operator* (multiplication with a constant number)
|
||||
void testOperatorMultiplicationWithConstant() {
|
||||
Matrix3x3 result = matrix1 * 2;
|
||||
test_(result.getValue(0,0) == 6);
|
||||
test_(result.getValue(0,1) == 14);
|
||||
test_(result.getValue(0,2) == -10);
|
||||
test_(result.getValue(1,0) == 26);
|
||||
test_(result.getValue(1,1) == -2);
|
||||
test_(result.getValue(1,2) == 4);
|
||||
test_(result.getValue(2,0) == 12);
|
||||
test_(result.getValue(2,1) == 10);
|
||||
test_(result.getValue(2,2) == 18);
|
||||
}
|
||||
|
||||
// Test operator* (multiplication with matrix)
|
||||
void testOperatorMultiplicationWithMatrix() {
|
||||
Matrix3x3 result = matrix1 * matrix2;
|
||||
test_(result.getValue(0,0) == 31);
|
||||
test_(result.getValue(0,1) == -181);
|
||||
test_(result.getValue(0,2) == -22);
|
||||
test_(result.getValue(1,0) == -188);
|
||||
test_(result.getValue(1,1) == 141);
|
||||
test_(result.getValue(1,2) == 57);
|
||||
test_(result.getValue(2,0) == -116);
|
||||
test_(result.getValue(2,1) == -23);
|
||||
test_(result.getValue(2,2) == 256);
|
||||
}
|
||||
|
||||
void testOperatorMultiplicationWithVector() {
|
||||
Vector3D vector(4,7,3);
|
||||
Vector3D result = matrix1 * vector;
|
||||
test_(result.getX() == 46);
|
||||
test_(result.getY() == 51);
|
||||
test_(result.getZ() == 86);
|
||||
}
|
||||
|
||||
// Test operator=()
|
||||
void testOperatorAssignment() {
|
||||
Matrix3x3 matrix;
|
||||
matrix = matrix1;
|
||||
test_(matrix.getValue(0,0) == 3);
|
||||
test_(matrix.getValue(0,1) == 7);
|
||||
test_(matrix.getValue(0,2) == -5);
|
||||
test_(matrix.getValue(1,0) == 13);
|
||||
test_(matrix.getValue(1,1) == -1);
|
||||
test_(matrix.getValue(1,2) == 2);
|
||||
test_(matrix.getValue(2,0) == 6);
|
||||
test_(matrix.getValue(2,1) == 5);
|
||||
test_(matrix.getValue(2,2) == 9);
|
||||
}
|
||||
|
||||
// Test operator==()
|
||||
void testOperatorEquality() {
|
||||
Matrix3x3 matrix(3, 7, -5, 13, -1, 2, 6, 5, 9);
|
||||
test_(matrix == matrix1);
|
||||
test_(matrix1 == matrix);
|
||||
test_(matrix == matrix);
|
||||
matrix.setValue(1,1, 100);
|
||||
test_(!(matrix == matrix1));
|
||||
test_(!(matrix1 == matrix));
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
|
@ -1,648 +0,0 @@
|
|||
/****************************************************************************
|
||||
* Copyright (C) 2009 Daniel Chappuis *
|
||||
****************************************************************************
|
||||
* This file is part of ReactPhysics3D. *
|
||||
* *
|
||||
* ReactPhysics3D 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 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* ReactPhysics3D 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 ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef MATRIXTEST_H
|
||||
#define MATRIXTEST_H
|
||||
|
||||
// Libraries
|
||||
#include "../TestSuite/Test.h"
|
||||
#include "../../mathematics/Matrix.h"
|
||||
#include <stdexcept>
|
||||
|
||||
// Namespaces
|
||||
using namespace reactphysics3d;
|
||||
|
||||
// Class MatrixTest
|
||||
class MatrixTest : public TestSuite::Test {
|
||||
private :
|
||||
Matrix matrix1;
|
||||
Matrix matrix2;
|
||||
Matrix matrix3;
|
||||
public :
|
||||
|
||||
// Constructor
|
||||
MatrixTest() : matrix1(2,3), matrix2(2,3), matrix3(3,2) {
|
||||
matrix1.setValue(0, 0, 4);
|
||||
matrix1.setValue(0, 1, 5);
|
||||
matrix1.setValue(0, 2, 7);
|
||||
matrix1.setValue(1, 0, 2);
|
||||
matrix1.setValue(1, 1, 3);
|
||||
matrix1.setValue(1, 2, -4);
|
||||
|
||||
matrix2.setValue(0, 0, -12);
|
||||
matrix2.setValue(0, 1, 3);
|
||||
matrix2.setValue(0, 2, 16);
|
||||
matrix2.setValue(1, 0, -7);
|
||||
matrix2.setValue(1, 1, 4);
|
||||
matrix2.setValue(1, 2, 6);
|
||||
|
||||
matrix3.setValue(0, 0, -4);
|
||||
matrix3.setValue(0, 1, -2);
|
||||
matrix3.setValue(1, 0, 7);
|
||||
matrix3.setValue(1, 1, 9);
|
||||
matrix3.setValue(2, 0, 18);
|
||||
matrix3.setValue(2, 1, 33);
|
||||
}
|
||||
|
||||
// Run method of the Test
|
||||
void run() {
|
||||
testConstructors();
|
||||
testGetValue();
|
||||
testSetValue();
|
||||
testGetNbRow();
|
||||
testGetNbColumn();
|
||||
testGetCofactor();
|
||||
testGetTranspose();
|
||||
testGetInverse();
|
||||
testGetDeterminant();
|
||||
testGetTrace();
|
||||
testIdentityMatrix();
|
||||
testOperatorAddition();
|
||||
testOperatorSubstraction();
|
||||
testOperatorMultiplicationWithConstant();
|
||||
testOperatorMultiplicationWithMatrix();
|
||||
testOperatorAssignment();
|
||||
testOperatorEquality();
|
||||
}
|
||||
|
||||
// Test the constructors
|
||||
void testConstructors() {
|
||||
|
||||
// Try a valid constructor call
|
||||
try {
|
||||
// Constructor
|
||||
Matrix matrix(4,6); // This shouldn't throw an exception
|
||||
test_(matrix.getNbRow() == 4);
|
||||
test_(matrix.getNbColumn() == 6);
|
||||
test_(matrix.getValue(0,0) == 0);
|
||||
succeed_(); // Succeed if no exceptions have been thrown
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
fail_("Valid constructor call throws an exception"); // Failed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try an invalid constructor call
|
||||
try {
|
||||
// Constructor
|
||||
Matrix matrix(-2,6); // This should throw an exception
|
||||
fail_("Invalid constructor call undetected (argument -2) "); // Failed if no exception have been thrown
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try an invalid constructor call
|
||||
try {
|
||||
// Constructor
|
||||
Matrix matrix(3,0); // This should throw an exception
|
||||
fail_("Invalid constructor call undetected (argument 0)"); // Failed if no exception have been thrown
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Copy-constructor
|
||||
Matrix matrix4 = matrix1;
|
||||
test_(matrix4.getNbRow() == 2);
|
||||
test_(matrix4.getNbColumn() == 3);
|
||||
test_(matrix4.getValue(0, 0) == 4);
|
||||
test_(matrix4.getValue(0, 1) == 5);
|
||||
test_(matrix4.getValue(0, 2) == 7);
|
||||
test_(matrix4.getValue(1, 0) == 2);
|
||||
test_(matrix4.getValue(1, 1) == 3);
|
||||
test_(matrix4.getValue(1, 2) == -4);
|
||||
}
|
||||
|
||||
// Test getValue()
|
||||
void testGetValue() {
|
||||
// Try a valid getValue()
|
||||
try {
|
||||
test_(matrix1.getValue(0, 0) == 4); // This shouldn't throw an exception
|
||||
test_(matrix1.getValue(0, 2) == 7); // This shouldn't throw an exception
|
||||
test_(matrix1.getValue(1, 0) == 2); // This shouldn't throw an exception
|
||||
test_(matrix1.getValue(1, 2) == -4); // This shouldn't throw an exception
|
||||
test_(matrix1.getValue(1, 1) == 3); // This shouldn't throw an exception
|
||||
succeed_(); // Succeed if no exceptions have been thrown
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
fail_("Valid getValue() call throws an exception"); // Failed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try an invalid getValue() call
|
||||
try {
|
||||
matrix1.getValue(-1, 0); // This should throw an exception
|
||||
fail_("Invalid getValue() call undetected");
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try an invalid getValue() call
|
||||
try {
|
||||
matrix1.getValue(0, 3); // This should throw an exception
|
||||
fail_("Invalid getValue() call undetected");
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
}
|
||||
|
||||
// Test setValue()
|
||||
void testSetValue() {
|
||||
|
||||
Matrix matrix(2,3);
|
||||
|
||||
// Try a valid setValue()
|
||||
try {
|
||||
matrix.setValue(0, 0, 18); // This shouldn't throw an exception
|
||||
matrix.setValue(0, 2, -6); // This shouldn't throw an exception
|
||||
matrix.setValue(1, 0, -44); // This shouldn't throw an exception
|
||||
matrix.setValue(1, 2, 21); // This shouldn't throw an exception
|
||||
matrix.setValue(1, 1, 5); // This shouldn't throw an exception
|
||||
test_(matrix.getValue(0, 0) == 18);
|
||||
test_(matrix.getValue(0, 2) == -6);
|
||||
test_(matrix.getValue(1, 0) == -44);
|
||||
test_(matrix.getValue(1, 2) == 21);
|
||||
test_(matrix.getValue(1, 1) == 5);
|
||||
succeed_(); // Succeed if no exceptions have been thrown
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
fail_("Valid setValue() call throws an exception"); // Failed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try an invalid setValue() call
|
||||
try {
|
||||
matrix.setValue(-1, 0, 42); // This should throw an exception
|
||||
fail_("Invalid setValue() call undetected"); // Failed if no exception have been thrown
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try an invalid setValue() call
|
||||
try {
|
||||
matrix1.setValue(0, 3, 53); // This should throw an exception
|
||||
fail_("Invalid setValue() call undetected"); // Failed if no exception have been thrown
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
}
|
||||
|
||||
// Test getNbRow()
|
||||
void testGetNbRow() {
|
||||
test_(matrix1.getNbRow() == 2);
|
||||
test_(matrix3.getNbRow() == 3);
|
||||
}
|
||||
|
||||
// Test getNbColumn()
|
||||
void testGetNbColumn() {
|
||||
test_(matrix1.getNbColumn() == 3);
|
||||
test_(matrix3.getNbColumn() == 2);
|
||||
}
|
||||
|
||||
// Test getCofactor()
|
||||
void testGetCofactor() {
|
||||
|
||||
// Try a valid getCofactor()
|
||||
try {
|
||||
Matrix matrix = matrix1.getCofactor(0,1); // This shouldn't throw an exception
|
||||
|
||||
test_(matrix.getNbRow() == 1);
|
||||
test_(matrix.getNbColumn() == 2);
|
||||
test_(matrix.getValue(0, 0) == 2);
|
||||
test_(matrix.getValue(0, 1) == -4);
|
||||
|
||||
Matrix matrix4 = matrix1.getCofactor(1,2); // This shouldn't throw an exception
|
||||
|
||||
test_(matrix4.getNbRow() == 1);
|
||||
test_(matrix4.getNbColumn() == 2);
|
||||
test_(matrix4.getValue(0, 0) == 4);
|
||||
test_(matrix4.getValue(0, 1) == 5);
|
||||
succeed_(); // Succeed if no exceptions have been thrown
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
fail_("Valid getCofactor() call throws an exception"); // Failed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try an invalid getCofactor() call
|
||||
try {
|
||||
matrix1.getCofactor(-1,0); // This should throw an exception
|
||||
fail_("Invalid getCofactor() call undetected"); // Failed if no exception have been thrown
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try an invalid getCofactor() call
|
||||
try {
|
||||
matrix1.getCofactor(0,3); // This should throw an exception
|
||||
fail_("Invalid getCofactor() call undetected"); // Failed if no exception have been thrown
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
}
|
||||
|
||||
// Test getTranspose()
|
||||
void testGetTranspose() {
|
||||
// Get the transpose of matrix1
|
||||
Matrix matrix = matrix1.getTranspose();
|
||||
|
||||
// Test the transpose matrix
|
||||
test_(matrix.getNbRow() == 3);
|
||||
test_(matrix.getNbColumn() == 2);
|
||||
test_(matrix.getValue(0, 0) == 4);
|
||||
test_(matrix.getValue(0, 1) == 2);
|
||||
test_(matrix.getValue(1, 0) == 5);
|
||||
test_(matrix.getValue(1, 1) == 3);
|
||||
test_(matrix.getValue(2, 0) == 7);
|
||||
test_(matrix.getValue(2, 1) == -4);
|
||||
}
|
||||
|
||||
// Test getInverse()
|
||||
void testGetInverse() {
|
||||
|
||||
// Construct a 3x3 matrix
|
||||
Matrix matrix(3,3);
|
||||
matrix.setValue(0, 0, 0);
|
||||
matrix.setValue(0, 1, 1);
|
||||
matrix.setValue(0, 2, 2);
|
||||
matrix.setValue(1, 0, 1);
|
||||
matrix.setValue(1, 1, 0);
|
||||
matrix.setValue(1, 2, 3);
|
||||
matrix.setValue(2, 0, 4);
|
||||
matrix.setValue(2, 1, -3);
|
||||
matrix.setValue(2, 2, 8);
|
||||
|
||||
// Try to inverse a invertible matrix
|
||||
try {
|
||||
Matrix result = matrix.getInverse(); // This shouldn't thrown an exception
|
||||
test_(result.getValue(0, 0) == -4.5);
|
||||
test_(result.getValue(0, 1) == 7);
|
||||
test_(result.getValue(0, 2) == -3.0/2.0);
|
||||
test_(result.getValue(1, 0) == -2);
|
||||
test_(result.getValue(1, 1) == 4);
|
||||
test_(result.getValue(1, 2) == -1);
|
||||
test_(result.getValue(2, 0) == 3.0/2.0);
|
||||
test_(result.getValue(2, 1) == -2);
|
||||
test_(result.getValue(2, 2) == 1.0/2.0);
|
||||
succeed_(); // Succeed if no exceptions have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
fail_("Valid getInverse() call throws an exception"); // Failed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try to inverse a non-square matrix
|
||||
try {
|
||||
matrix1.getInverse(); // This should throw an exception
|
||||
fail_("Invalid getInverse() call undetected (non-square matrix)"); // Failed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try to inverse a square non-invertible matrix (determinant equal to zero)
|
||||
try {
|
||||
Matrix matrix4(2,2);
|
||||
matrix4.setValue(0, 0, 3);
|
||||
matrix4.setValue(0, 1, 2);
|
||||
matrix4.setValue(1, 0, 3);
|
||||
matrix4.setValue(1, 1, 2);
|
||||
matrix4.getInverse(); // This should throw an exception
|
||||
fail_("Invalid getInverse() call undetected (non-invertible matrix)"); // Failed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
}
|
||||
|
||||
// Test getDeterminant()
|
||||
void testGetDeterminant() {
|
||||
|
||||
// Try to compute the determinant of a square matrix
|
||||
try {
|
||||
Matrix matrix(2,2);
|
||||
test_(matrix.getDeterminant() == 0); // This shouldn't throw an exception
|
||||
matrix.setValue(0,0, 4);
|
||||
matrix.setValue(0,1, -9);
|
||||
matrix.setValue(1,0, 0);
|
||||
matrix.setValue(1,1, 5);
|
||||
test_(matrix.getDeterminant() == 20); // This shouldn't throw an exception
|
||||
matrix.setValue(0,0, 6);
|
||||
matrix.setValue(0,1, -9);
|
||||
matrix.setValue(1,0, -4);
|
||||
matrix.setValue(1,1, 6);
|
||||
test_(matrix.getDeterminant() == 0); // This shouldn't throw an exception
|
||||
succeed_(); // Succeed if no exceptions have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
fail_("Valid getDeterminant() call throws an exception"); // Failed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try to compute the determinant of a non-square matrix
|
||||
try {
|
||||
Matrix matrix5(2,8);
|
||||
matrix5.setValue(0, 2, 3);
|
||||
matrix5.setValue(1, 1, 2);
|
||||
matrix5.getDeterminant(); // This should throw an exception
|
||||
fail_("getDeterminant() call with a non-square matrix undetected"); // Failed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
}
|
||||
|
||||
// Test getTrace()
|
||||
void testGetTrace() {
|
||||
|
||||
// Try to compute the trace of a square matrix
|
||||
try {
|
||||
// Construct a 3x3 matrix
|
||||
Matrix matrix(3,3);
|
||||
matrix.setValue(0, 0, -2);
|
||||
matrix.setValue(0, 1, 1);
|
||||
matrix.setValue(0, 2, 2);
|
||||
matrix.setValue(1, 0, 1);
|
||||
matrix.setValue(1, 1, 5);
|
||||
matrix.setValue(1, 2, 3);
|
||||
matrix.setValue(2, 0, 4);
|
||||
matrix.setValue(2, 1, -3);
|
||||
matrix.setValue(2, 2, 8);
|
||||
test_(matrix.getTrace() == 11); // This shouldn't throw an exception
|
||||
succeed_(); // Succeed if no exceptions have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
fail_("Valid getTrace() call throws an exception"); // Failed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try to compute the trace of a non-square matrix
|
||||
try {
|
||||
Matrix matrix5(2,8);
|
||||
matrix5.getTrace(); // This should throw an exception
|
||||
fail_("getTrace() call with a non-square matrix undetected"); // Failed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
}
|
||||
|
||||
// Test identityMatrix()
|
||||
void testIdentityMatrix() {
|
||||
|
||||
|
||||
// Try to compute a valid identity matrix
|
||||
try {
|
||||
Matrix matrix = Matrix::identity(2); // This shouldn't throw an exception
|
||||
test_(matrix.getNbRow() == 2);
|
||||
test_(matrix.getNbColumn() == 2);
|
||||
test_(matrix.getValue(0, 0) == 1);
|
||||
test_(matrix.getValue(0, 1) == 0);
|
||||
test_(matrix.getValue(1, 0) == 0);
|
||||
test_(matrix.getValue(1, 1) == 1);
|
||||
succeed_(); // Succeed if no exceptions have been thrown
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
fail_("Valid identity() call throws an exception"); // Failed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try to compute an invalid identity matrix
|
||||
try {
|
||||
Matrix matrix5 = Matrix::identity(0); // This should throw an exception
|
||||
fail_("Invalid identity() call (argument 0) undetected"); // Failed if no exception have been thrown
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try to compute an invalid identity matrix
|
||||
try {
|
||||
Matrix matrix5 = Matrix::identity(-1); // This should throw an exception
|
||||
fail_("Invalid identity() call (argument -1) undetected"); // Failed if no exception have been thrown
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
}
|
||||
|
||||
// Test operator+()
|
||||
void testOperatorAddition() {
|
||||
|
||||
// Try to compute a valid addition
|
||||
try {
|
||||
Matrix result = matrix1 + matrix2; // This shouldn't throw an exception
|
||||
test_(result.getNbRow() == 2);
|
||||
test_(result.getNbColumn() == 3);
|
||||
test_(result.getValue(0,0) == -8);
|
||||
test_(result.getValue(0,1) == 8);
|
||||
test_(result.getValue(0,2) == 23);
|
||||
test_(result.getValue(1,0) == -5);
|
||||
test_(result.getValue(1,1) == 7);
|
||||
test_(result.getValue(1,2) == 2);
|
||||
succeed_(); // Succeed if no exceptions have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
fail_("Valid matrix addition throws an exception"); // Failed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try to compute an invalid addition
|
||||
try {
|
||||
Matrix matrix5(2,4);
|
||||
matrix1 + matrix5; // This should throw an exception
|
||||
fail_("Invalid matrix addition undetected"); // Failed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try to compute an invalid addition
|
||||
try {
|
||||
Matrix matrix5(1,3);
|
||||
matrix1 + matrix5; // This should throw an exception
|
||||
fail_("Invalid matrix addition undetected"); // Failed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
}
|
||||
|
||||
// Test operator-()
|
||||
void testOperatorSubstraction() {
|
||||
|
||||
// Try to compute a valid substraction
|
||||
try {
|
||||
Matrix result = matrix1 - matrix2; // This shouldn't throw an exception
|
||||
test_(result.getNbRow() == 2);
|
||||
test_(result.getNbColumn() == 3);
|
||||
test_(result.getValue(0,0) == 16);
|
||||
test_(result.getValue(0,1) == 2);
|
||||
test_(result.getValue(0,2) == -9);
|
||||
test_(result.getValue(1,0) == 9);
|
||||
test_(result.getValue(1,1) == -1);
|
||||
test_(result.getValue(1,2) == -10);
|
||||
succeed_(); // Succeed if no exceptions have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
fail_("Valid matrix substraction throws an exception"); // Failed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try to compute an invalid substraction
|
||||
try {
|
||||
Matrix matrix5(2,4);
|
||||
matrix1 - matrix5; // This should throw an exception
|
||||
fail_("Invalid matrix substraction undetected"); // Failed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try to compute an invalid substraction
|
||||
try {
|
||||
Matrix matrix5(1,3);
|
||||
matrix1 - matrix5; // This should throw an exception
|
||||
fail_("Invalid matrix substraction undetected"); // Failed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
}
|
||||
|
||||
// Test operator* (multiplication with a constant number)
|
||||
void testOperatorMultiplicationWithConstant() {
|
||||
Matrix matrix = matrix1 * 2;
|
||||
test_(matrix.getNbRow() == 2);
|
||||
test_(matrix.getNbColumn() == 3);
|
||||
test_(matrix.getValue(0,0) == 8);
|
||||
test_(matrix.getValue(0,1) == 10);
|
||||
test_(matrix.getValue(0,2) == 14);
|
||||
test_(matrix.getValue(1,0) == 4);
|
||||
test_(matrix.getValue(1,1) == 6);
|
||||
test_(matrix.getValue(1,2) == -8);
|
||||
}
|
||||
|
||||
// Test operator* (multiplication with matrix)
|
||||
void testOperatorMultiplicationWithMatrix() {
|
||||
|
||||
// Try to compute a valid multiplication
|
||||
try {
|
||||
Matrix result = matrix1 * matrix3; // This shouldn't throw an exception
|
||||
test_(result.getNbRow() == 2);
|
||||
test_(result.getNbColumn() == 2);
|
||||
test_(result.getValue(0,0) == 145);
|
||||
test_(result.getValue(0,1) == 268);
|
||||
test_(result.getValue(1,0) == -59);
|
||||
test_(result.getValue(1,1) == -109);
|
||||
succeed_(); // Succeed if no exceptions have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
fail_("Valid matrix multiplication throws an exception"); // Failed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try to compute an invalid multiplication
|
||||
try {
|
||||
Matrix matrix5(1,3);
|
||||
matrix1 * matrix5; // This should throw an exception
|
||||
fail_("Invalid matrix substraction undetected"); // Failed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try to compute an invalid multiplication
|
||||
try {
|
||||
Matrix matrix5(2,2);
|
||||
matrix1 * matrix5; // This should throw an exception
|
||||
fail_("Invalid matrix substraction undetected"); // Failed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
}
|
||||
|
||||
// Test operator=()
|
||||
void testOperatorAssignment() {
|
||||
|
||||
// Try to compute a valid assignment
|
||||
try {
|
||||
Matrix matrix(2,3);
|
||||
matrix = matrix1; // This shouldn't throw an exception
|
||||
test_(matrix.getValue(0, 0) == 4);
|
||||
test_(matrix.getValue(0, 2) == 7);
|
||||
test_(matrix.getValue(1, 0) == 2);
|
||||
test_(matrix.getValue(1, 2) == -4);
|
||||
test_(matrix.getValue(1, 1) == 3);
|
||||
succeed_(); // Succeed if no exceptions have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
fail_("Valid matrix assignment throws an exception"); // Failed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try to compute an invalid assignment
|
||||
try {
|
||||
Matrix matrix(2,2);
|
||||
matrix = matrix1; // This should throw an exception
|
||||
fail_("Invalid matrix assignment undetected"); // Failed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
}
|
||||
|
||||
// Test operator==()
|
||||
void testOperatorEquality() {
|
||||
|
||||
|
||||
// Try to test a valid equality
|
||||
try {
|
||||
Matrix matrix(2,3);
|
||||
matrix.setValue(0, 0, 4);
|
||||
matrix.setValue(0, 1, 5);
|
||||
matrix.setValue(0, 2, 7);
|
||||
matrix.setValue(1, 0, 2);
|
||||
matrix.setValue(1, 1, 3);
|
||||
matrix.setValue(1, 2, -4);
|
||||
test_(matrix == matrix1); // This shouldn't throw an exception
|
||||
test_(matrix1 == matrix); // This shouldn't throw an exception
|
||||
test_(matrix == matrix); // This shouldn't throw an exception
|
||||
matrix.setValue(1,1, 5);
|
||||
test_(!(matrix == matrix1)); // This shouldn't throw an exception
|
||||
test_(!(matrix1 == matrix)); // This shouldn't throw an exception
|
||||
succeed_(); // Succeed if no exceptions have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
fail_("Valid matrix equality test throws an exception"); // Failed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try to test a invalid equality
|
||||
try {
|
||||
Matrix matrix(2,2);
|
||||
matrix == matrix1; // This should throw an exception
|
||||
fail_("Invalid matrix assignment undetected"); // Failed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -1,350 +0,0 @@
|
|||
/****************************************************************************
|
||||
* Copyright (C) 2009 Daniel Chappuis *
|
||||
****************************************************************************
|
||||
* This file is part of ReactPhysics3D. *
|
||||
* *
|
||||
* ReactPhysics3D 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 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* ReactPhysics3D 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 ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef QUATERNIONTEST_H
|
||||
#define QUATERNIONTEST_H
|
||||
|
||||
// Libraries
|
||||
#include "../TestSuite/Test.h"
|
||||
#include "../../mathematics/Quaternion.h"
|
||||
#include "../../mathematics/constants.h"
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
|
||||
// Namespaces
|
||||
using namespace reactphysics3d;
|
||||
|
||||
// Class MatrixTest
|
||||
class QuaternionTest : public TestSuite::Test {
|
||||
private :
|
||||
Quaternion quaternion1;
|
||||
Quaternion quaternion2;
|
||||
public :
|
||||
|
||||
// Constructor
|
||||
QuaternionTest() : quaternion1(2,3,4,5), quaternion2(6,7,8,9) {
|
||||
|
||||
}
|
||||
|
||||
// Run method of the Test
|
||||
void run() {
|
||||
testConstructors();
|
||||
testVectorV();
|
||||
testLength();
|
||||
testGetUnit();
|
||||
testGetConjugate();
|
||||
testGetInverse();
|
||||
testScalarProduct();
|
||||
testgetRotationAngleAxis();
|
||||
testSlerp();
|
||||
testOperatorAddition();
|
||||
testOperatorSubstraction();
|
||||
testOperatorMultiplicationWithConstant();
|
||||
testOperatorMultiplicationWithQuaternion();
|
||||
testOperatorAssignment();
|
||||
testOperatorEquality();
|
||||
}
|
||||
|
||||
// Test the constructors
|
||||
void testConstructors() {
|
||||
// Constructor without argument
|
||||
Quaternion quaternion;
|
||||
test_(quaternion.getX() == 0);
|
||||
test_(quaternion.getY() == 0);
|
||||
test_(quaternion.getZ() == 0);
|
||||
test_(quaternion.getW() == 0);
|
||||
|
||||
// Constructor with argument
|
||||
Quaternion quaternion3(1,2,3,4);
|
||||
test_(quaternion3.getX() == 1);
|
||||
test_(quaternion3.getY() == 2);
|
||||
test_(quaternion3.getZ() == 3);
|
||||
test_(quaternion3.getW() == 4);
|
||||
|
||||
// Constructor with vector
|
||||
Vector3D vector(2,3,4);
|
||||
Quaternion quaternion4(5, vector);
|
||||
test_(quaternion4.getX() == 2);
|
||||
test_(quaternion4.getY() == 3);
|
||||
test_(quaternion4.getZ() == 4);
|
||||
test_(quaternion4.getW() == 5);
|
||||
|
||||
// Copy-constructor
|
||||
Quaternion quaternion5 = quaternion3;
|
||||
test_(quaternion5.getX() == 1);
|
||||
test_(quaternion5.getY() == 2);
|
||||
test_(quaternion5.getZ() == 3);
|
||||
test_(quaternion5.getW() == 4);
|
||||
}
|
||||
|
||||
// Test getX()
|
||||
void testGetX() {
|
||||
test_(quaternion1.getX() == 2);
|
||||
test_(quaternion2.getX() == 6);
|
||||
}
|
||||
|
||||
// Test getY()
|
||||
void testGetY() {
|
||||
test_(quaternion1.getY() == 3);
|
||||
test_(quaternion2.getY() == 7);
|
||||
}
|
||||
|
||||
// Test getZ()
|
||||
void testGetZ() {
|
||||
test_(quaternion1.getZ() == 4);
|
||||
test_(quaternion2.getZ() == 8);
|
||||
}
|
||||
|
||||
// Test getW()
|
||||
void testGetW() {
|
||||
test_(quaternion1.getW() == 5);
|
||||
test_(quaternion2.getW() == 9);
|
||||
}
|
||||
|
||||
// Test setX()
|
||||
void testSetX() {
|
||||
Quaternion quaternion;
|
||||
quaternion.setX(3);
|
||||
test_(quaternion.getX() == 3);
|
||||
test_(quaternion.getY() == 0);
|
||||
test_(quaternion.getZ() == 0);
|
||||
test_(quaternion.getW() == 0);
|
||||
}
|
||||
|
||||
// Test setY()
|
||||
void testSetY() {
|
||||
Quaternion quaternion;
|
||||
quaternion.setY(3);
|
||||
test_(quaternion.getX() == 0);
|
||||
test_(quaternion.getY() == 3);
|
||||
test_(quaternion.getZ() == 0);
|
||||
test_(quaternion.getW() == 0);
|
||||
}
|
||||
|
||||
// Test setZ()
|
||||
void testSetZ() {
|
||||
Quaternion quaternion;
|
||||
quaternion.setZ(3);
|
||||
test_(quaternion.getX() == 0);
|
||||
test_(quaternion.getY() == 0);
|
||||
test_(quaternion.getZ() == 3);
|
||||
test_(quaternion.getW() == 0);
|
||||
}
|
||||
|
||||
// Test setW()
|
||||
void testSetW() {
|
||||
Quaternion quaternion;
|
||||
quaternion.setW(3);
|
||||
test_(quaternion.getX() == 0);
|
||||
test_(quaternion.getY() == 0);
|
||||
test_(quaternion.getZ() == 0);
|
||||
test_(quaternion.getW() == 3);
|
||||
}
|
||||
|
||||
// Test vectorV()
|
||||
void testVectorV() {
|
||||
Vector3D vector1(2,3,4);
|
||||
Vector3D vector2(6,7,8);
|
||||
|
||||
Vector3D vectorTest1 = quaternion1.vectorV();
|
||||
Vector3D vectorTest2 = quaternion2.vectorV();
|
||||
test_(vectorTest1 == vector1);
|
||||
test_(vectorTest2 == vector2);
|
||||
}
|
||||
|
||||
// Test length()
|
||||
void testLength() {
|
||||
Quaternion quaternion;
|
||||
test_(quaternion.length() == 0);
|
||||
|
||||
Quaternion quaternion3(3, 4, 0, 0);
|
||||
test_(quaternion3.length() == 5);
|
||||
|
||||
Quaternion quaternion4(0, 4, 3, 0);
|
||||
test_(quaternion4.length() == 5);
|
||||
|
||||
Quaternion quaternion5(0, 0, 3, 4);
|
||||
test_(quaternion5.length() == 5);
|
||||
}
|
||||
|
||||
// Test getUnit()
|
||||
void testGetUnit() {
|
||||
// Try to compute a valid unit quaternion
|
||||
try {
|
||||
Quaternion quaternion(3, 4, 0, 0);
|
||||
Quaternion unit = quaternion.getUnit(); // This shouldn't throw an exception
|
||||
test_(unit.getX() == 3.0/5.0);
|
||||
test_(unit.getY() == 4.0/5.0);
|
||||
test_(unit.getZ() == 0);
|
||||
test_(unit.getW() == 0);
|
||||
|
||||
Quaternion quaternion3(0, 0, 4, 3);
|
||||
Quaternion unit2 = quaternion3.getUnit(); // This shouldn't throw an exception
|
||||
test_(unit2.getX() == 0);
|
||||
test_(unit2.getY() == 0);
|
||||
test_(unit2.getZ() == 4.0/5.0);
|
||||
test_(unit2.getW() == 3.0/5.0);
|
||||
succeed_(); // Succeed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
fail_("Valid getUnit() call throw an exception"); // Failed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try to compute an invalid unit quaternion
|
||||
try {
|
||||
Quaternion quaternion(0, 0, 0, 0);
|
||||
quaternion.getUnit(); // This should throw an exception
|
||||
fail_("Invalid getUnit() call undetected"); // Failed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
}
|
||||
|
||||
// Test getConjugate()
|
||||
void testGetConjugate() {
|
||||
Quaternion conjugate1 = quaternion1.getConjugate();
|
||||
test_(conjugate1.getX() == -2);
|
||||
test_(conjugate1.getY() == -3);
|
||||
test_(conjugate1.getZ() == -4);
|
||||
test_(conjugate1.getW() == 5);
|
||||
|
||||
Quaternion conjugate2 = quaternion2.getConjugate();
|
||||
test_(conjugate2.getX() == -6);
|
||||
test_(conjugate2.getY() == -7);
|
||||
test_(conjugate2.getZ() == -8);
|
||||
test_(conjugate2.getW() == 9);
|
||||
}
|
||||
|
||||
// Test getInverse()
|
||||
void testGetInverse() {
|
||||
// Try to compute a valid inverse quaternion
|
||||
try {
|
||||
Quaternion quaternion(3, 4, 0, 0);
|
||||
Quaternion inverse = quaternion.getInverse(); // This shouldn't throw an exception
|
||||
test_(inverse.getX() == -3.0/25.0);
|
||||
test_(inverse.getY() == -4.0/25.0);
|
||||
test_(inverse.getZ() == 0);
|
||||
test_(inverse.getW() == 0);
|
||||
|
||||
Quaternion quaternion3(0, 0, 4, 3);
|
||||
Quaternion inverse2 = quaternion3.getInverse(); // This shouldn't throw an exception
|
||||
test_(inverse2.getX() == 0);
|
||||
test_(inverse2.getY() == 0);
|
||||
test_(inverse2.getZ() == -4.0/25.0);
|
||||
test_(inverse2.getW() == 3.0/25.0);
|
||||
succeed_(); // Succeed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
fail_("Valid getInverse() call throw an exception"); // Failed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try to compute an invalid unit quaternion
|
||||
try {
|
||||
Quaternion quaternion(0, 0, 0, 0);
|
||||
quaternion.getInverse(); // This should throw an exception
|
||||
fail_("Invalid getInverse() call undetected"); // Failed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
succeed_(); // Succeed if an exception has been thrown
|
||||
}
|
||||
}
|
||||
|
||||
// Test the scalarProduct() method
|
||||
void testScalarProduct() {
|
||||
double result = quaternion1.scalarProduct(quaternion2);
|
||||
test_(result == 110.0);
|
||||
}
|
||||
|
||||
// Test the getRotationAngleAxis() method
|
||||
void testgetRotationAngleAxis() {
|
||||
Quaternion quaternion(1.0, 2.0, 3.0, 0.0);
|
||||
double invAxisLength = 1.0/sqrt(1.0 + 2.0*2.0 + 3.0*3.0);
|
||||
double angle;
|
||||
Vector3D axis;
|
||||
|
||||
quaternion.getRotationAngleAxis(angle, axis);
|
||||
test_(equal(angle, PI));
|
||||
test_(equal(axis.getX(), 1.0*invAxisLength));
|
||||
test_(equal(axis.getY(), 2.0*invAxisLength));
|
||||
test_(equal(axis.getZ(), 3.0*invAxisLength));
|
||||
}
|
||||
|
||||
// Test the slerp() method
|
||||
void testSlerp() {
|
||||
// TODO : Test the Quaternion::slerp() method
|
||||
}
|
||||
|
||||
// Test operator+()
|
||||
void testOperatorAddition() {
|
||||
Quaternion result = quaternion1 + quaternion2;
|
||||
test_(result.getX() == 8);
|
||||
test_(result.getY() == 10);
|
||||
test_(result.getZ() == 12);
|
||||
test_(result.getW() == 14);
|
||||
}
|
||||
|
||||
// Test operator-()
|
||||
void testOperatorSubstraction() {
|
||||
Quaternion result = quaternion1 - quaternion2;
|
||||
test_(result.getX() == -4);
|
||||
test_(result.getY() == -4);
|
||||
test_(result.getZ() == -4);
|
||||
test_(result.getW() == -4);
|
||||
}
|
||||
|
||||
// Test operator* (multiplication with a constant number)
|
||||
void testOperatorMultiplicationWithConstant() {
|
||||
Quaternion result = quaternion1 * 3;
|
||||
test_(result.getX() == 6);
|
||||
test_(result.getY() == 9);
|
||||
test_(result.getZ() == 12);
|
||||
test_(result.getW() == 15);
|
||||
}
|
||||
|
||||
// Test operator* (multiplication with quaternion)
|
||||
void testOperatorMultiplicationWithQuaternion() {
|
||||
Quaternion result = quaternion1 * quaternion2;
|
||||
test_(result.getX() == 44);
|
||||
test_(result.getY() == 70);
|
||||
test_(result.getZ() == 72);
|
||||
test_(result.getW() == -20);
|
||||
}
|
||||
|
||||
// Test operator=()
|
||||
void testOperatorAssignment() {
|
||||
Quaternion quaternion;
|
||||
quaternion = quaternion1;
|
||||
test_(quaternion.getX() == 2);
|
||||
test_(quaternion.getY() == 3);
|
||||
test_(quaternion.getZ() == 4);
|
||||
test_(quaternion.getW() == 5);
|
||||
}
|
||||
|
||||
// Test operator==()
|
||||
void testOperatorEquality() {
|
||||
Quaternion quaternion(2,3,4,5);
|
||||
test_(quaternion == quaternion1);
|
||||
test_(quaternion1 == quaternion);
|
||||
test_(!(quaternion2 == quaternion1));
|
||||
test_(!(quaternion1 == quaternion2));
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
|
@ -1,272 +0,0 @@
|
|||
/****************************************************************************
|
||||
* Copyright (C) 2009 Daniel Chappuis *
|
||||
****************************************************************************
|
||||
* This file is part of ReactPhysics3D. *
|
||||
* *
|
||||
* ReactPhysics3D 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 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* ReactPhysics3D 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 ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef VECTOR3DTEST_H
|
||||
#define VECTOR3DTEST_H
|
||||
|
||||
// Libraries
|
||||
#include "../TestSuite/Test.h"
|
||||
#include "../../mathematics/Vector3D.h"
|
||||
#include <stdexcept>
|
||||
|
||||
// Namespaces
|
||||
using namespace reactphysics3d;
|
||||
|
||||
// Class Vector3DTest
|
||||
class Vector3DTest : public TestSuite::Test {
|
||||
private :
|
||||
Vector3D vector1;
|
||||
Vector3D vector2;
|
||||
public :
|
||||
|
||||
// Constructor
|
||||
Vector3DTest() : vector1(1,2,3), vector2(-3,5,7) {
|
||||
|
||||
}
|
||||
|
||||
// Run method of the Test
|
||||
void run() {
|
||||
testConstructors();
|
||||
testGetX();
|
||||
testGetY();
|
||||
testGetZ();
|
||||
testSetX();
|
||||
testSetY();
|
||||
testSetZ();
|
||||
testSetAllValues();
|
||||
testLength();
|
||||
testGetUnit();
|
||||
testScalarProduct();
|
||||
testCrossProduct();
|
||||
testOperatorAddition();
|
||||
testOperatorSubstraction();
|
||||
testOperatorConstantMultiplications();
|
||||
testOperatorAssignment();
|
||||
}
|
||||
|
||||
// Test the constructors
|
||||
void testConstructors() {
|
||||
|
||||
// Constructor without arguments
|
||||
Vector3D vector;
|
||||
test_(vector.getX() == 0);
|
||||
test_(vector.getY() == 0);
|
||||
test_(vector.getZ() == 0);
|
||||
|
||||
// Constructor with arguments
|
||||
Vector3D vector3(4, -67, 21);
|
||||
test_(vector3.getX() == 4);
|
||||
test_(vector3.getY() == -67);
|
||||
test_(vector3.getZ() == 21);
|
||||
|
||||
// Copy-constructor
|
||||
Vector3D vector4 = vector1;
|
||||
test_(vector4.getX() == 1);
|
||||
test_(vector4.getY() == 2);
|
||||
test_(vector4.getZ() == 3);
|
||||
}
|
||||
|
||||
// Test getX()
|
||||
void testGetX() {
|
||||
test_(vector1.getX() == 1);
|
||||
}
|
||||
|
||||
// Test getY()
|
||||
void testGetY() {
|
||||
test_(vector1.getY() == 2);
|
||||
}
|
||||
|
||||
// Test getZ()
|
||||
void testGetZ() {
|
||||
test_(vector1.getZ() == 3);
|
||||
}
|
||||
|
||||
// Test setX()
|
||||
void testSetX() {
|
||||
Vector3D vector(5, 6, 7);
|
||||
vector.setX(8);
|
||||
test_(vector.getX() == 8);
|
||||
test_(vector.getY() == 6);
|
||||
test_(vector.getZ() == 7);
|
||||
}
|
||||
|
||||
// Test setY()
|
||||
void testSetY() {
|
||||
Vector3D vector(5, 6, 7);
|
||||
vector.setY(8);
|
||||
test_(vector.getX() == 5);
|
||||
test_(vector.getY() == 8);
|
||||
test_(vector.getZ() == 7);
|
||||
}
|
||||
|
||||
// Test setZ()
|
||||
void testSetZ() {
|
||||
Vector3D vector(5, 6, 7);
|
||||
vector.setZ(8);
|
||||
test_(vector.getX() == 5);
|
||||
test_(vector.getY() == 6);
|
||||
test_(vector.getZ() == 8);
|
||||
}
|
||||
|
||||
|
||||
// Test setAllValues()
|
||||
void testSetAllValues() {
|
||||
Vector3D vector(5, 6, 7);
|
||||
vector1.setAllValues(4,3,9);
|
||||
test_(vector1.getX() == 4);
|
||||
test_(vector1.getY() == 3);
|
||||
test_(vector1.getZ() == 9);
|
||||
}
|
||||
|
||||
// Test length()
|
||||
void testLength() {
|
||||
Vector3D vector3;
|
||||
test_(vector3.length() == 0);
|
||||
vector3.setAllValues(3, 4, 0);
|
||||
test_(vector3.length() == 5);
|
||||
vector3.setAllValues(0, -3, 4);
|
||||
test_(vector3.length() == 5);
|
||||
}
|
||||
|
||||
// Test getUnit()
|
||||
void testGetUnit() {
|
||||
|
||||
Vector3D vector3(-23, 0, 0);
|
||||
test_(vector3.getUnit().length() == 1);
|
||||
test_(vector3.getUnit().getX() == -1);
|
||||
test_(vector3.getUnit().getY() == 0);
|
||||
test_(vector3.getUnit().getZ() == 0);
|
||||
|
||||
vector3.setAllValues(0, 6, 0);
|
||||
test_(vector3.getUnit().length() == 1);
|
||||
test_(vector3.getUnit().getX() == 0);
|
||||
test_(vector3.getUnit().getY() == 1);
|
||||
test_(vector3.getUnit().getZ() == 0);
|
||||
|
||||
vector3.setAllValues(0, 0, 13);
|
||||
test_(vector3.getUnit().length() == 1);
|
||||
test_(vector3.getUnit().getX() == 0);
|
||||
test_(vector3.getUnit().getY() == 0);
|
||||
test_(vector3.getUnit().getZ() == 1);
|
||||
|
||||
vector3.setAllValues(0, 0, 0); // Vector of length equal to zero
|
||||
try {
|
||||
vector3.getUnit(); // This should throw an exception
|
||||
fail_("getUnit() with a vector of length equals to zero undetected"); // The test failed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
succeed_(); // The test succeed if an exception has been thrown
|
||||
}
|
||||
}
|
||||
|
||||
// Test scalarProduct()
|
||||
void testScalarProduct() {
|
||||
Vector3D vector3(2, -3, 5);
|
||||
Vector3D vector4(7, 4, 6);
|
||||
|
||||
// Test the scalar product result
|
||||
test_(vector3.scalarProduct(vector4) == 32);
|
||||
}
|
||||
|
||||
// Test crossProduct()
|
||||
void testCrossProduct() {
|
||||
Vector3D vector3(4, -5, 2);
|
||||
Vector3D vector4(3, 2, 6);
|
||||
|
||||
// Compute the cross product
|
||||
Vector3D result = vector3.crossProduct(vector4);
|
||||
|
||||
// Test the result
|
||||
test_(result.getX() == -34);
|
||||
test_(result.getY() == -18);
|
||||
test_(result.getZ() == 23);
|
||||
}
|
||||
|
||||
// Test operator+()
|
||||
void testOperatorAddition() {
|
||||
Vector3D vector3(4, -5, 2);
|
||||
Vector3D vector4(3, 2, 6);
|
||||
|
||||
// Compute the sum
|
||||
Vector3D result = vector3 + vector4;
|
||||
|
||||
// Test the result
|
||||
test_(result.getX() == 7);
|
||||
test_(result.getY() == -3);
|
||||
test_(result.getZ() == 8);
|
||||
}
|
||||
|
||||
// Test operator-()
|
||||
void testOperatorSubstraction() {
|
||||
Vector3D vector3(4, -5, 2);
|
||||
Vector3D vector4(3, 2, 6);
|
||||
|
||||
// Compute the substraction
|
||||
Vector3D result = vector3 - vector4;
|
||||
|
||||
// Test the result
|
||||
test_(result.getX() == 1);
|
||||
test_(result.getY() == -7);
|
||||
test_(result.getZ() == -4);
|
||||
}
|
||||
|
||||
// Test operator*() (with a constant number)
|
||||
void testOperatorConstantMultiplications() {
|
||||
Vector3D vector3(4, -5, 2);
|
||||
|
||||
// Compute the multiplication
|
||||
Vector3D result = vector3 * 5;
|
||||
|
||||
// Test the result
|
||||
test_(result.getX() == 20);
|
||||
test_(result.getY() == -25);
|
||||
test_(result.getZ() == 10);
|
||||
}
|
||||
|
||||
// Test operator=()
|
||||
void testOperatorAssignment() {
|
||||
Vector3D vector3(4, -5, 2);
|
||||
|
||||
// Assignment
|
||||
Vector3D result;
|
||||
result = vector3;
|
||||
|
||||
// Test the result
|
||||
test_(result.getX() == 4);
|
||||
test_(result.getY() == -5);
|
||||
test_(result.getZ() == 2);
|
||||
}
|
||||
|
||||
// Test operator==()
|
||||
void testOperatorEquality() {
|
||||
Vector3D vector3(4, -5, 2);
|
||||
Vector3D vector4(4, -5, 2);
|
||||
Vector3D vector5(3, -5, -2);
|
||||
|
||||
// Test the equality
|
||||
test_(vector3 == vector4);
|
||||
test_(vector4 == vector3);
|
||||
test_(vector3 == vector3);
|
||||
test_(!(vector3 == vector5));
|
||||
test_(!(vector5 == vector3));
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -1,450 +0,0 @@
|
|||
/****************************************************************************
|
||||
* Copyright (C) 2009 Daniel Chappuis *
|
||||
****************************************************************************
|
||||
* This file is part of ReactPhysics3D. *
|
||||
* *
|
||||
* ReactPhysics3D 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 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* ReactPhysics3D 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 ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef VECTORTEST_H
|
||||
#define VECTORTEST_H
|
||||
|
||||
// Libraries
|
||||
#include "../TestSuite/Test.h"
|
||||
#include "../../mathematics/Vector.h"
|
||||
#include <stdexcept>
|
||||
|
||||
// Namespaces
|
||||
using namespace reactphysics3d;
|
||||
|
||||
// Class VectorTest
|
||||
class VectorTest : public TestSuite::Test {
|
||||
private :
|
||||
Vector vect2; // Vector of dimension 2
|
||||
Vector vect4; // Vector of dimension 4
|
||||
public :
|
||||
|
||||
// Constructor
|
||||
VectorTest() : vect2(2), vect4(4) {
|
||||
vect2.setValue(0, 3);
|
||||
vect2.setValue(1, 5);
|
||||
|
||||
vect4.setValue(0, 1);
|
||||
vect4.setValue(1, -5);
|
||||
vect4.setValue(2, 10);
|
||||
vect4.setValue(3, 3);
|
||||
}
|
||||
|
||||
// Run method of the Test
|
||||
void run() {
|
||||
testConstructors();
|
||||
testGetValue();
|
||||
testSetValue();
|
||||
testGetNbComponent();
|
||||
testLength();
|
||||
testGetUnit();
|
||||
testScalarProduct();
|
||||
testCrossProduct();
|
||||
testOperatorAddition();
|
||||
testOperatorSubstraction();
|
||||
testOperatorConstantMultiplications();
|
||||
testOperatorAssignment();
|
||||
}
|
||||
|
||||
// Test the constructors
|
||||
void testConstructors() {
|
||||
|
||||
// Try a valid constructor call
|
||||
try {
|
||||
// Constructor
|
||||
Vector vector(3); // This shouldn't throw an exception
|
||||
test_(vector.getValue(0) == 0);
|
||||
test_(vector.getValue(1) == 0);
|
||||
test_(vector.getValue(2) == 0);
|
||||
succeed_(); // Test succeed if no exception have been thrown
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
fail_("Valid constructor call throws an exception"); // Test failed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try a invalid constructor call
|
||||
try {
|
||||
// Constructor
|
||||
Vector vector3(-1); // This should throw an exception
|
||||
fail_("Invalid constructor (argument -1) call undetected"); // Test failed if no exception have been thrown
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_(); // Test succeed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try a invalid constructor call
|
||||
try {
|
||||
// Constructor
|
||||
Vector vector4(0); // This should throw an exception
|
||||
fail_("Invalid constructor call (argument 0) undetected"); // Test failed if no exception have been thrown
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_(); // Test succeed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Copy-Constructor
|
||||
Vector vector5 = vect2;
|
||||
test_(vector5.getValue(0) == 3);
|
||||
test_(vector5.getValue(1) == 5);
|
||||
}
|
||||
|
||||
// Test getValue()
|
||||
void testGetValue() {
|
||||
test_(vect2.getValue(0) == 3);
|
||||
test_(vect2.getValue(1) == 5);
|
||||
|
||||
// Try to get an invalid value
|
||||
try {
|
||||
vect2.getValue(-1); // This should throw an exception
|
||||
fail_("Invalid getValue(-1) undetected"); // The test failed if no exception have been thrown
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_(); // The test succeed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try to get an invalid value
|
||||
try {
|
||||
vect2.getValue(2); // This should throw an exception
|
||||
fail_("Invalid getValue(2) undetected"); // The test failed if no exception have been thrown
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_(); // The test succeed if an exception has been thrown
|
||||
}
|
||||
}
|
||||
|
||||
// Test setValue()
|
||||
void testSetValue() {
|
||||
Vector vector(5);
|
||||
|
||||
// Try to set valid values
|
||||
try {
|
||||
vector.setValue(0, 5);
|
||||
vector.setValue(3, -1);
|
||||
vector.setValue(4, 14);
|
||||
test_(vector.getValue(0) == 5);
|
||||
test_(vector.getValue(3) == -1);
|
||||
test_(vector.getValue(4) == 14);
|
||||
succeed_(); // The test succeed if an exception have been thrown
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
fail_("Valid setValue() throws an exception"); // The failed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try to set an invalid value
|
||||
try {
|
||||
vector.setValue(-1, 4); // This should throw an exception
|
||||
fail_("Invalid setValue(-1,4) undetected"); // The test failed if no exception have been thrown
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_(); // The test succeed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try to set an invalid value
|
||||
try {
|
||||
vector.setValue(5, 2); // This should throw an exception
|
||||
fail_("Invalid setValue(5,2) undetected"); // The test failed if no exception have been thrown
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_(); // The test succeed if an exception has been thrown
|
||||
}
|
||||
}
|
||||
|
||||
// Test getNbComponent()
|
||||
void testGetNbComponent() {
|
||||
test_(vect2.getNbComponent() == 2);
|
||||
test_(vect4.getNbComponent() == 4);
|
||||
}
|
||||
|
||||
// Test length()
|
||||
void testLength() {
|
||||
Vector vector1(2);
|
||||
test_(vector1.length() == 0);
|
||||
vector1.setValue(0, 4);
|
||||
test_(vector1.length() == 4);
|
||||
vector1.setValue(1, -3);
|
||||
test_(vector1.length() == 5);
|
||||
}
|
||||
|
||||
// Test getUnit()
|
||||
void testGetUnit() {
|
||||
Vector vector1(3);
|
||||
vector1.setValue(0, 3);
|
||||
test_(vector1.getUnit().length() == 1);
|
||||
test_(vector1.getUnit().getValue(0) == 1);
|
||||
test_(vector1.getUnit().getValue(1) == 0);
|
||||
test_(vector1.getUnit().getValue(2) == 0);
|
||||
|
||||
Vector vector2(8);
|
||||
vector2.setValue(2, 54);
|
||||
test_(vector2.getUnit().length() == 1);
|
||||
test_(vector2.getUnit().getValue(0) == 0);
|
||||
test_(vector2.getUnit().getValue(1) == 0);
|
||||
test_(vector2.getUnit().getValue(2) == 1);
|
||||
|
||||
Vector vector3(7); // Vector of length equal to zero
|
||||
try {
|
||||
vector3.getUnit(); // This should throw an exception
|
||||
fail_("getUnit() with a vector of length equals to zero undetected"); // The test failed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
succeed_(); // The test succeed if an exception has been thrown
|
||||
}
|
||||
}
|
||||
|
||||
// Test scalarProduct()
|
||||
void testScalarProduct() {
|
||||
Vector vector1(2);
|
||||
vector1.setValue(0, 4);
|
||||
vector1.setValue(1, -5);
|
||||
|
||||
Vector vector2(2);
|
||||
vector2.setValue(0, 3);
|
||||
vector2.setValue(1, 2);
|
||||
|
||||
// Try to compute a valid scalar product
|
||||
try {
|
||||
test_(vector1.scalarProduct(vector2) == 2);
|
||||
succeed_(); // The test succeed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
fail_("scalarProduct() thrown an exception during a valid scalar product computation"); // The test failed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try to compute a invalid scalar product
|
||||
Vector vector3(5);
|
||||
try {
|
||||
vector1.scalarProduct(vector3); // This should throw an exception
|
||||
fail_("Invalid dimensions in scalarProduct() undetected"); // The test failed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
succeed_(); // The test succeed if an exception has been thrown
|
||||
}
|
||||
}
|
||||
|
||||
// Test crossProduct()
|
||||
void testCrossProduct() {
|
||||
Vector vector1(3);
|
||||
vector1.setValue(0, 4);
|
||||
vector1.setValue(1, -5);
|
||||
vector1.setValue(2, 2);
|
||||
|
||||
Vector vector2(3);
|
||||
vector2.setValue(0, 3);
|
||||
vector2.setValue(1, 2);
|
||||
vector2.setValue(2, 6);
|
||||
|
||||
// Try to compute a valid scalar product
|
||||
try {
|
||||
Vector result = vector1.crossProduct(vector2);
|
||||
test_(result.getValue(0) == -34);
|
||||
test_(result.getValue(1) == -18);
|
||||
test_(result.getValue(2) == 23);
|
||||
succeed_(); // The test succeed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
fail_("crossProduct() thrown an exception during a valid cross product computation"); // The test failed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try to compute a invalid cross product
|
||||
Vector vector3(5);
|
||||
try {
|
||||
vector1.crossProduct(vector3); // This should throw an exception
|
||||
fail_("Invalid dimensions in crossProduct() undetected"); // The test failed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
succeed_(); // The test succeed if an exception has been thrown
|
||||
}
|
||||
}
|
||||
|
||||
// Test operator+()
|
||||
void testOperatorAddition() {
|
||||
Vector vector1(4);
|
||||
vector1.setValue(0, 3);
|
||||
vector1.setValue(3, -2);
|
||||
|
||||
Vector vector2(4);
|
||||
vector2.setValue(0, 9);
|
||||
vector2.setValue(2, 6);
|
||||
vector2.setValue(3, 9);
|
||||
|
||||
// Try to compute a valid sum (two vectors with the same dimensions)
|
||||
try {
|
||||
// Compute the sum
|
||||
Vector sum = vector1 + vector2; // This shouldn't throw an exception
|
||||
|
||||
test_(sum.getValue(0) == 12);
|
||||
test_(sum.getValue(1) == 0);
|
||||
test_(sum.getValue(2) == 6);
|
||||
test_(sum.getValue(3) == 7);
|
||||
succeed_(); // The test succeed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
fail_("A valid sum of two vectors throws an excception"); // The test failed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try to compute an invalid sum (vectors with different dimensions)
|
||||
Vector vector3(3);
|
||||
Vector vector4(5);
|
||||
try {
|
||||
// Compute the sum
|
||||
Vector sum = vector3 + vector4; // This should throw an exception
|
||||
fail_("An invalid sum of two vectors undetected"); // The test failed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
succeed_(); // The test succeed if an exception has been thrown
|
||||
}
|
||||
}
|
||||
|
||||
// Test operator-()
|
||||
void testOperatorSubstraction() {
|
||||
Vector vector1(4);
|
||||
vector1.setValue(0, 3);
|
||||
vector1.setValue(3, -2);
|
||||
|
||||
Vector vector2(4);
|
||||
vector2.setValue(0, 9);
|
||||
vector2.setValue(2, 6);
|
||||
vector2.setValue(3, 9);
|
||||
|
||||
// Try to compute a valid subtraction (two vectors with the same dimensions)
|
||||
try {
|
||||
// Compute the substraction
|
||||
Vector sub = vector1 - vector2; // This shouldn't throw an exception
|
||||
|
||||
test_(sub.getValue(0) == -6);
|
||||
test_(sub.getValue(1) == 0);
|
||||
test_(sub.getValue(2) == -6);
|
||||
test_(sub.getValue(3) == -11);
|
||||
succeed_(); // The test succeed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
fail_("A valid subtraction of two vectors throws an excception"); // The test failed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try to compute an invalid substraction (vectors with different dimensions)
|
||||
Vector vector3(3);
|
||||
Vector vector4(5);
|
||||
try {
|
||||
// Compute the substraction
|
||||
Vector sub = vector3 - vector4; // This should throw an exception
|
||||
fail_("An invalid substraction of two vectors undetected"); // The test failed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
succeed_(); // The test succeed if an exception has been thrown
|
||||
}
|
||||
}
|
||||
|
||||
// Test operator*() (with a constant number)
|
||||
void testOperatorConstantMultiplications() {
|
||||
Vector vector(4);
|
||||
vector.setValue(0, 3);
|
||||
vector.setValue(3, -2);
|
||||
|
||||
// Compute the multiplication
|
||||
Vector sum = vector * 3.0;
|
||||
test_(sum.getValue(0) == 9);
|
||||
test_(sum.getValue(1) == 0);
|
||||
test_(sum.getValue(2) == 0);
|
||||
test_(sum.getValue(3) == -6);
|
||||
}
|
||||
|
||||
// Test operator=()
|
||||
void testOperatorAssignment() {
|
||||
Vector vector1(2);
|
||||
vector1.setValue(0, 4);
|
||||
vector1.setValue(1, 7);
|
||||
|
||||
Vector vector2(8);
|
||||
|
||||
|
||||
// Try to compute a valid assignment (two vectors with the same dimensions)
|
||||
try {
|
||||
Vector vector(2);
|
||||
vector = vector1; // This shouldn't throw an exception
|
||||
test_(vector == vector1);
|
||||
|
||||
vector = vector; // This shouldn't throw an exception
|
||||
succeed_(); // The test succeed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
fail_("A valid vector assignment throws an excception"); // The test failed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try to compute an invalid assignment (vectors with different dimensions)
|
||||
try {
|
||||
Vector vector3(2);
|
||||
vector3 = vector2; // This should throw an exception
|
||||
fail_("An invalid vector assignment undetected"); // The test failed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
succeed_(); // The test succeed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try to compute an invalid assignment (vectors with different dimensions)
|
||||
try {
|
||||
Vector vector3(2);
|
||||
vector2 = vector3; // This should throw an exception
|
||||
fail_("An invalid vector assignment undetected"); // The test failed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
succeed_(); // The test succeed if an exception has been thrown
|
||||
}
|
||||
}
|
||||
|
||||
// Test operator==()
|
||||
void testOperatorEquality() {
|
||||
Vector vector1(2);
|
||||
vector1.setValue(0, 4);
|
||||
vector1.setValue(1, 7);
|
||||
|
||||
Vector vector2(2);
|
||||
vector2.setValue(0, 4);
|
||||
vector2.setValue(1, 7);
|
||||
|
||||
Vector vector3(2);
|
||||
vector3.setValue(0, 5);
|
||||
vector3.setValue(1, 7);
|
||||
|
||||
Vector vector4(8);
|
||||
|
||||
// Try to test a valid equality (two vectors with the same dimensions)
|
||||
try {
|
||||
test_(vector1 == vector2); // This shouldn't throw an exception
|
||||
test_(vector2 == vector1); // This shouldn't throw an exception
|
||||
test_(vector1 == vector1); // This shouldn't throw an exception
|
||||
test_(!(vector1 == vector3)); // This shouldn't throw an exception
|
||||
test_(!(vector3 == vector1)); // This shouldn't throw an exception
|
||||
succeed_(); // The test succeed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
fail_("A valid vector equality test throws an excception"); // The test failed if an exception has been thrown
|
||||
}
|
||||
|
||||
// Try to test an invalid equality (vectors with different dimensions)
|
||||
try {
|
||||
vector4 == vector1; // This should throw an exception
|
||||
fail_("An invalid equality test of two vectors undetected"); // The test failed if no exception have been thrown
|
||||
}
|
||||
catch(MathematicsException& ex) {
|
||||
succeed_(); // The test succeed if an exception has been thrown
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
|
@ -1,130 +0,0 @@
|
|||
/****************************************************************************
|
||||
* Copyright (C) 2009 Daniel Chappuis *
|
||||
****************************************************************************
|
||||
* This file is part of ReactPhysics3D. *
|
||||
* *
|
||||
* ReactPhysics3D 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 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* ReactPhysics3D 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 ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef KILOGRAMTEST_H
|
||||
#define KILOGRAMTEST_H
|
||||
|
||||
// Libraries
|
||||
#include "../TestSuite/Test.h"
|
||||
#include "../../physics/Kilogram.h"
|
||||
#include <stdexcept>
|
||||
|
||||
// Namespaces
|
||||
using namespace reactphysics3d;
|
||||
|
||||
// Class KilogramTest
|
||||
class KilogramTest : public TestSuite::Test {
|
||||
private :
|
||||
Kilogram mass1;
|
||||
Kilogram mass2;
|
||||
public :
|
||||
|
||||
// Constructor
|
||||
KilogramTest() : mass1(5.0), mass2(10.0) {
|
||||
|
||||
}
|
||||
|
||||
// Run method of the Test
|
||||
void run() {
|
||||
testConstructors();
|
||||
testGetValue();
|
||||
testSetValue();
|
||||
}
|
||||
|
||||
// Test the constructors
|
||||
void testConstructors() {
|
||||
// Try valid constructors calls
|
||||
try {
|
||||
Kilogram mass(30.4); // This should'n throw an exception
|
||||
Kilogram mass3(mass); // This should'n throw an exception
|
||||
Kilogram mass4(0.0); // This should'n throw an exception
|
||||
Kilogram mass5;
|
||||
test_(mass.getValue() == 30.4);
|
||||
test_(mass3.getValue() == 30.4);
|
||||
test_(mass4.getValue() == 0.0);
|
||||
test_(mass5.getValue() == 0.0);
|
||||
succeed_();
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
fail_("Valid constructor call throws an exception");
|
||||
}
|
||||
|
||||
// Try an invalid constructor call
|
||||
try{
|
||||
Kilogram mass4(-0.1); // This should throw an exception
|
||||
fail_("Invalid constructors calls didn't throw an exception");
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_();
|
||||
}
|
||||
|
||||
// Try an invalid constructor call
|
||||
try{
|
||||
Kilogram mass4(-10.); // This should throw an exception
|
||||
fail_("Invalid constructors calls didn't throw an exception");
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_();
|
||||
}
|
||||
}
|
||||
|
||||
// Test the method getValue()
|
||||
void testGetValue() {
|
||||
test_(mass1.getValue() == 5.0);
|
||||
test_(mass2.getValue() == 10.0);
|
||||
}
|
||||
|
||||
// Test the method setValue()
|
||||
void testSetValue() {
|
||||
Kilogram mass(10.0);
|
||||
|
||||
// Try a valid setValue() call
|
||||
try {
|
||||
mass.setValue(0.0); // This should'n throw an exception
|
||||
test_(mass.getValue() == 0.0);
|
||||
mass.setValue(43.0); // This should'n throw an exception
|
||||
test_(mass.getValue() == 43.0);
|
||||
succeed_();
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
fail_("Valid setValue() call throw an exception");
|
||||
}
|
||||
|
||||
// Try an invalid setValue() call
|
||||
try {
|
||||
mass.setValue(-0.1); // This should throw an exception
|
||||
fail_("Invalid setValue() call didn't throw an exception");
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_();
|
||||
}
|
||||
|
||||
// Try an invalid setValue() call
|
||||
try {
|
||||
mass.setValue(-40.0); // This should throw an exception
|
||||
fail_("Invalid setValue() call didn't throw an exception");
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_();
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -1,189 +0,0 @@
|
|||
/****************************************************************************
|
||||
* Copyright (C) 2009 Daniel Chappuis *
|
||||
****************************************************************************
|
||||
* This file is part of ReactPhysics3D. *
|
||||
* *
|
||||
* ReactPhysics3D 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 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* ReactPhysics3D 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 ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef TIMETEST_H
|
||||
#define TIMETEST_H
|
||||
|
||||
// Libraries
|
||||
#include "../TestSuite/Test.h"
|
||||
#include "../../physics/Time.h"
|
||||
#include <stdexcept>
|
||||
|
||||
// Namespaces
|
||||
using namespace reactphysics3d;
|
||||
|
||||
// Class TimeTest
|
||||
class TimeTest : public TestSuite::Test {
|
||||
private :
|
||||
Time time1;
|
||||
Time time2;
|
||||
public :
|
||||
|
||||
// Constructor
|
||||
TimeTest() : time1(5.0), time2(10.0) {
|
||||
|
||||
}
|
||||
|
||||
// Run method of the Test
|
||||
void run() {
|
||||
testConstructors();
|
||||
testGetValue();
|
||||
testSetValue();
|
||||
testOperatorAddition();
|
||||
testOperatorSubstraction();
|
||||
testOperatorMultiplication();
|
||||
}
|
||||
|
||||
// Test the constructors
|
||||
void testConstructors() {
|
||||
// Try valid constructors calls
|
||||
try {
|
||||
Time time(30.4); // This should'n throw an exception
|
||||
Time time3(time); // This should'n throw an exception
|
||||
Time time4(0.0); // This should'n throw an exception
|
||||
Time time5;
|
||||
test_(time.getValue() == 30.4);
|
||||
test_(time3.getValue() == 30.4);
|
||||
test_(time4.getValue() == 0.0);
|
||||
test_(time5.getValue() == 0.0);
|
||||
succeed_();
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
fail_("Valid constructor call throws an exception");
|
||||
}
|
||||
|
||||
// Try an invalid constructor call
|
||||
try{
|
||||
Time time4(-0.1); // This should throw an exception
|
||||
fail_("Invalid constructors calls didn't throw an exception");
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_();
|
||||
}
|
||||
|
||||
// Try an invalid constructor call
|
||||
try{
|
||||
Time time4(-10.); // This should throw an exception
|
||||
fail_("Invalid constructors calls didn't throw an exception");
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_();
|
||||
}
|
||||
}
|
||||
|
||||
// Test the method getValue()
|
||||
void testGetValue() {
|
||||
test_(time1.getValue() == 5.0);
|
||||
test_(time2.getValue() == 10.0);
|
||||
}
|
||||
|
||||
// Test the method setValue()
|
||||
void testSetValue() {
|
||||
Time time(10.0);
|
||||
|
||||
// Try a valid setValue() call
|
||||
try {
|
||||
time.setValue(0.0); // This should'n throw an exception
|
||||
test_(time.getValue() == 0.0);
|
||||
time.setValue(43.0); // This should'n throw an exception
|
||||
test_(time.getValue() == 43.0);
|
||||
succeed_();
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
fail_("Valid setValue() call throw an exception");
|
||||
}
|
||||
|
||||
// Try an invalid setValue() call
|
||||
try {
|
||||
time.setValue(-0.1); // This should throw an exception
|
||||
fail_("Invalid setValue() call didn't throw an exception");
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_();
|
||||
}
|
||||
|
||||
// Try an invalid setValue() call
|
||||
try {
|
||||
time.setValue(-40.0); // This should throw an exception
|
||||
fail_("Invalid setValue() call didn't throw an exception");
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_();
|
||||
}
|
||||
}
|
||||
|
||||
// Test the overloaded addition operator
|
||||
void testOperatorAddition() {
|
||||
Time result;
|
||||
result = time1 + time2;
|
||||
test_(result.getValue() == 15.0);
|
||||
}
|
||||
|
||||
// Test the overloaded substraction operator
|
||||
void testOperatorSubstraction() {
|
||||
Time result;
|
||||
|
||||
// Try a valid substraction
|
||||
try {
|
||||
|
||||
result = time2 - time1; // This should'n throw an exception
|
||||
test_(result.getValue() == 5.0);
|
||||
succeed_();
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
fail_("Valid call to substraction operator throw an exception");
|
||||
}
|
||||
|
||||
// try an invalid substraction
|
||||
try {
|
||||
result = time1 - time2;
|
||||
fail_("Invalid call to substraction didn't throw an exception");
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_();
|
||||
}
|
||||
}
|
||||
|
||||
// Test the overloaded multiplication operator
|
||||
void testOperatorMultiplication() {
|
||||
Time result;
|
||||
|
||||
// Try a valid substraction
|
||||
try {
|
||||
result = time1 * 3.0; // This should'n throw an exception
|
||||
test_(result.getValue() == 15.0);
|
||||
succeed_();
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
fail_("Valid call to multiplication operator throw an exception");
|
||||
}
|
||||
|
||||
// try an invalid substraction
|
||||
try {
|
||||
result = time1 * (-3.0);
|
||||
fail_("Invalid call to multiplication didn't throw an exception");
|
||||
}
|
||||
catch(std::invalid_argument& ex) {
|
||||
succeed_();
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user