git-svn-id: https://reactphysics3d.googlecode.com/svn/trunk@53 92aac97c-a6ce-11dd-a772-7fcde58d38e6
This commit is contained in:
parent
cd7a8d1a58
commit
0157f498cb
|
@ -1,34 +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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
// Libraries
|
||||
#include "Camera.h"
|
||||
|
||||
// Initialization of static variables
|
||||
double Camera::speed=0.5;
|
||||
|
||||
// Constructor of the camera
|
||||
Camera::Camera() {
|
||||
|
||||
}
|
||||
|
||||
// Destructor of the camera
|
||||
Camera::~Camera() {
|
||||
|
||||
}
|
|
@ -1,91 +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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef CAMERA_H
|
||||
#define CAMERA_H
|
||||
|
||||
// Libraries
|
||||
#include "../reactphysics3d/reactphysics3d.h" // We want the mathematics stuff of reactphysics3d
|
||||
|
||||
// Namespaces
|
||||
using namespace reactphysics3d;
|
||||
|
||||
// Class Camera (abstract class)
|
||||
// In the project we will use two different camera. This is the superclass of the two
|
||||
// cameras. We will use a OutSideCamera that move arround the scene and an OnBoardCamera
|
||||
// that will simulate the deplacement of a viewer inside the scene
|
||||
class Camera {
|
||||
protected :
|
||||
Vector3D position; // Position of the camera
|
||||
Vector3D lookAtPoint; // Point where the camera is looking
|
||||
Vector3D viewVector; // Vector from the camera position to the view point
|
||||
static double speed; // Speed movement of the camera
|
||||
|
||||
public :
|
||||
Camera(); // Constructor
|
||||
Camera(const Camera& camera); // Copy-constructor
|
||||
virtual ~Camera(); // Destructor
|
||||
virtual Vector3D getPosition() const; // Get the position of the camera
|
||||
virtual void setPosition(const Vector3D& pos); // Set the position of the camera
|
||||
virtual Vector3D getLookAtPoint() const; // Return the point where the camera is looking
|
||||
virtual Vector3D getViewVector() const; // Return the view vector of the camera
|
||||
virtual void updateViewVector()=0; // Update the view vector of the camera
|
||||
static void increaseSpeed(); // Increase the speed of camera movement
|
||||
static void decreaseSpeed(); // Decrease the speed of camera movement
|
||||
};
|
||||
|
||||
// Get the position of the camera (inline function)
|
||||
inline Vector3D Camera::getPosition() const {
|
||||
// Return the position of the camera
|
||||
return position;
|
||||
}
|
||||
|
||||
// Set the position of the camera (inline function)
|
||||
inline void Camera::setPosition(const Vector3D& pos) {
|
||||
// Set the position of the camera
|
||||
position = pos;
|
||||
}
|
||||
|
||||
// Return the point where the camera is looking (inline function)
|
||||
inline Vector3D Camera::getLookAtPoint() const {
|
||||
return lookAtPoint;
|
||||
}
|
||||
|
||||
// Return the view vector of the camera (inline function)
|
||||
inline Vector3D Camera::getViewVector() const {
|
||||
return viewVector;
|
||||
}
|
||||
|
||||
// Increase the speed movement of the camera (inline function)
|
||||
inline void Camera::increaseSpeed()
|
||||
{
|
||||
speed= speed+0.05;
|
||||
if(speed>1)
|
||||
speed=1;
|
||||
}
|
||||
|
||||
// Decrease the speep movement of the camera (inline function)
|
||||
inline void Camera::decreaseSpeed()
|
||||
{
|
||||
speed = speed-0.05;
|
||||
if(speed<=0)
|
||||
speed=0.005;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -1,76 +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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
// Libraries
|
||||
#include "Context.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
|
||||
// Constructor of the class Context
|
||||
Context::Context() {
|
||||
|
||||
// We add some objects in the context at the beginning ---> THESE THINGS WILL BE STORE IN A TEXT FILE
|
||||
Cube* cube1 = new Cube(Object::Position(-2.0, 1.0, -6.0), 2.0);
|
||||
Cube* cube2 = new Cube(Object::Position(0.0, 1.5, 6.0), 3.0);
|
||||
Cube* cube3 = new Cube(Object::Position(4.0, 4.0, -2.0), 2.0);
|
||||
Plane* plane1 = new Plane(Object::Position(0.0, 0.0, 0.0), 20.0, 30.0, Vector3D(-1.0, 0.0, 0.0), Vector3D(0.0, 0.0, 1.0));
|
||||
|
||||
addObject(cube1);
|
||||
addObject(cube2);
|
||||
addObject(cube3);
|
||||
addObject(plane1);
|
||||
}
|
||||
|
||||
|
||||
// Destructor of the class Context
|
||||
Context::~Context() {
|
||||
// Delete all the objects in vectObjects
|
||||
for(int i=0; i<vectObjects.size(); ++i) {
|
||||
delete vectObjects[0];
|
||||
}
|
||||
}
|
||||
|
||||
// Method to get an object from the context
|
||||
Object& Context::getObject(int objectIndex) const {
|
||||
// WE HAVE TO ADD HERE AN EXCEPTION IMPLEMENTATION
|
||||
|
||||
// Return the object from the context
|
||||
return (*vectObjects.at(objectIndex)); // THROWN AN EXCEPTION IF INDEX IS OUT OF THE BOUNDS
|
||||
}
|
||||
|
||||
// Method for adding an object into the context
|
||||
void Context::addObject(Object* object) {
|
||||
if (object != NULL) {
|
||||
// Add the object into the context
|
||||
vectObjects.push_back(object);
|
||||
}
|
||||
}
|
||||
|
||||
// Method to remove an object from the context
|
||||
void Context::removeObject(int objectIndex) {
|
||||
// WE HAVE TO ADD HERE AN EXCEPTION IMPLEMENTATION
|
||||
|
||||
// Restore the memory of the element
|
||||
delete vectObjects[objectIndex];
|
||||
|
||||
// Erase the element in the vector
|
||||
vectObjects.erase(vectObjects.begin()+objectIndex);
|
||||
}
|
|
@ -1,47 +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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef CONTEXT_H
|
||||
#define CONTEXT_H
|
||||
|
||||
// Libraries
|
||||
#include "Objects.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
// Class Context
|
||||
class Context {
|
||||
private :
|
||||
std::vector<Object*> vectObjects; // Vector of Objects in the simulation
|
||||
|
||||
public :
|
||||
Context(); // Constructor of the class
|
||||
~Context(); // Destructor of the class
|
||||
int getNbObjects() const; // Return the number of objects in the context
|
||||
Object& getObject(int objectIndex) const; // Get an object from the context
|
||||
void addObject(Object* object); // Add an object into the context
|
||||
void removeObject(int objectIndex); // Remove an object from the context
|
||||
};
|
||||
|
||||
// Method (inline) to get the number of objects in the context
|
||||
inline int Context::getNbObjects() const {
|
||||
return vectObjects.size();
|
||||
}
|
||||
|
||||
#endif
|
|
@ -1,123 +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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
// Libraries
|
||||
#include "Objects.h"
|
||||
|
||||
//#include <windows.h> // To avoid an error due to the #include <GL/glut.h>
|
||||
#include <GL/freeglut.h>
|
||||
#include <math.h>
|
||||
|
||||
// ----- Class Object ----- //
|
||||
|
||||
// Constructor of the class Object
|
||||
Object::Object(const Position& position) {
|
||||
this->position = position;
|
||||
}
|
||||
|
||||
// Destructor of the class Object
|
||||
Object::~Object() {
|
||||
|
||||
}
|
||||
|
||||
// ----- Structure Position ----- //
|
||||
|
||||
// Constructor without arguments of the structure Position
|
||||
Object::Position::Position() {
|
||||
x = 0.0;
|
||||
y = 0.0;
|
||||
z = 0.0;
|
||||
}
|
||||
|
||||
// Constructor of the structure Position
|
||||
Object::Position::Position(double x, double y, double z) {
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
this->z = z;
|
||||
};
|
||||
|
||||
// ----- Class Cube ----- //
|
||||
|
||||
// Constructor of the class Cube
|
||||
Cube::Cube(const Position& position, float size)
|
||||
:Object(position) {
|
||||
this->size = size;
|
||||
}
|
||||
|
||||
// Destructor of the classe Cube
|
||||
Cube::~Cube() {
|
||||
|
||||
}
|
||||
|
||||
// Method to draw the cube
|
||||
void Cube::draw() const {
|
||||
// Translation of the cube to its position
|
||||
glTranslatef(position.x, position.y, position.z);
|
||||
|
||||
// Draw the cube
|
||||
glutSolidCube(size);
|
||||
}
|
||||
|
||||
|
||||
// ----- Class Plane ----- //
|
||||
|
||||
// Constructor of the class Plane
|
||||
Plane::Plane(const Position& position, float width, float height, const Vector3D& d1, const Vector3D& d2)
|
||||
:Object(position) {
|
||||
this->width = width;
|
||||
this->height = height;
|
||||
this->d1 = d1;
|
||||
this->d2 = d2;
|
||||
|
||||
// Compute the unit normal vector of the plane by a cross product
|
||||
normalVector = d1.crossProduct(d2).getUnit();
|
||||
}
|
||||
|
||||
// Destructor of the class Plane
|
||||
Plane::~Plane() {
|
||||
|
||||
}
|
||||
|
||||
// Method used to draw the plane
|
||||
void Plane::draw() const {
|
||||
// Translation of the cube to its position
|
||||
glTranslatef(position.x, position.y, position.z);
|
||||
|
||||
float halfWidth = width / 2.0;
|
||||
float halfHeight = height / 2.0;
|
||||
|
||||
// Draw the plane
|
||||
glBegin(GL_POLYGON);
|
||||
glColor3f(1.0, 1.0, 1.0);
|
||||
glVertex3f(position.x + d1.getX() * halfWidth + d2.getX() * halfHeight , position.y + d1.getY() * halfWidth + d2.getY() * halfHeight
|
||||
, position.z + d1.getZ() * halfWidth + d2.getZ() * halfHeight);
|
||||
glNormal3f(normalVector.getX(), normalVector.getY(), normalVector.getZ());
|
||||
glVertex3f(position.x + d1.getX() * halfWidth - d2.getX() * halfHeight , position.y + d1.getY() * halfWidth - d2.getY() * halfHeight
|
||||
, position.z + d1.getZ() * halfWidth - d2.getZ() * halfHeight);
|
||||
glNormal3f(normalVector.getX(), normalVector.getY(), normalVector.getZ());
|
||||
glVertex3f(position.x - d1.getX() * halfWidth - d2.getX() * halfHeight , position.y - d1.getY() * halfWidth - d2.getY() * halfHeight
|
||||
, position.z - d1.getZ() * halfWidth - d2.getZ() * halfHeight);
|
||||
glNormal3f(normalVector.getX(), normalVector.getY(), normalVector.getZ());
|
||||
glVertex3f(position.x - d1.getX() * halfWidth + d2.getX() * halfHeight , position.y - d1.getY() * halfWidth + d2.getY() * halfHeight
|
||||
, position.z - d1.getZ() * halfWidth + d2.getZ() * halfHeight);
|
||||
glEnd();
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -1,79 +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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef OBJECTS_H
|
||||
#define OBJECTS_H
|
||||
|
||||
// Libraries
|
||||
#include "../reactphysics3d/reactphysics3d.h"
|
||||
|
||||
// Namespaces
|
||||
using namespace reactphysics3d;
|
||||
|
||||
/*
|
||||
Here we define all the objects that can appear in the simulation like cube, sphere, plane, ...
|
||||
*/
|
||||
|
||||
// ----- Class Object (abstract) ----- //
|
||||
// Represent an object of the simulation
|
||||
class Object {
|
||||
public :
|
||||
// Structure Object::Position
|
||||
struct Position {
|
||||
double x; // x coordinate
|
||||
double y; // y coordinate
|
||||
double z; // z coordinate
|
||||
|
||||
// Methods
|
||||
Position(); // Constructor without arguments of the structure Position
|
||||
Position(double x, double y, double z); // Constructor of the structure Position
|
||||
} position; // Position of the object
|
||||
Object(const Position& position); // Constructor of the class Object
|
||||
virtual ~Object(); // Destructor of the class Object
|
||||
virtual void draw() const =0; // pure virtual method to draw the object
|
||||
};
|
||||
|
||||
// ----- Class Cube ----- //
|
||||
// Represente a Cube in the simulation
|
||||
class Cube : public Object {
|
||||
private :
|
||||
float size; // Size of a side in the cube
|
||||
|
||||
public :
|
||||
Cube(const Position& position, float size); // Constructor of the class cube
|
||||
virtual ~Cube(); // Destructor of the class cube
|
||||
virtual void draw() const; // Method to draw the cube
|
||||
};
|
||||
|
||||
|
||||
// ----- Class Plane ---- //
|
||||
// Represent a plane in the simulation
|
||||
class Plane : public Object {
|
||||
public :
|
||||
float width; // Width of the plane
|
||||
float height; // Height of the plane
|
||||
Vector3D d1; // Unit vector in the plane
|
||||
Vector3D d2; // Unit vector in the plane
|
||||
Vector3D normalVector; // Unit normal vector of the plane
|
||||
Plane(const Position& position, float width, float height, const Vector3D& d1, const Vector3D& d2); // Constructor of the class Plane
|
||||
virtual ~Plane(); // Destructor of the class Plane
|
||||
virtual void draw() const; // Method to draw the plane
|
||||
};
|
||||
|
||||
#endif
|
|
@ -1,115 +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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
// Libraries
|
||||
#include "OutSideCamera.h"
|
||||
#include <cmath>
|
||||
|
||||
// Constants
|
||||
const double PI = 3.141592;
|
||||
|
||||
// Constructor
|
||||
OutSideCamera::OutSideCamera() {
|
||||
|
||||
// Initialize the attributes
|
||||
heightFromFloor = 20.0;
|
||||
horizontalAngleRotation = 0;
|
||||
verticalAngleRotation = 45;
|
||||
distanceFromOrigin = 10.0;
|
||||
lookAtPoint.setAllValues(0.0, 0.0, 0.0);
|
||||
|
||||
// Update the position of the camera
|
||||
updatePosition();
|
||||
|
||||
// Update the view vector of the camera
|
||||
updateViewVector();
|
||||
}
|
||||
|
||||
// Destructor
|
||||
OutSideCamera::~OutSideCamera() {
|
||||
|
||||
}
|
||||
|
||||
// Compute the new position of the camera
|
||||
void OutSideCamera::updatePosition() {
|
||||
|
||||
// Compute the floor distance from origin
|
||||
double floorDistance = distanceFromOrigin * cos(PI/180.0 * verticalAngleRotation);
|
||||
|
||||
// Update the position of the camera
|
||||
position.setAllValues(floorDistance*cos(PI/180.0 * horizontalAngleRotation), distanceFromOrigin*sin(PI/180*verticalAngleRotation),
|
||||
floorDistance*sin(PI/180.0 * horizontalAngleRotation));
|
||||
}
|
||||
|
||||
// Set the camera rotation angle and update the position of the camera
|
||||
void OutSideCamera::modifyHorizontalAngleRotation(int screenDistance, float fps) {
|
||||
|
||||
// Update the horizontal rotation angle of the camera
|
||||
horizontalAngleRotation = (horizontalAngleRotation + int(screenDistance * 60.0 / fps)) % 360;
|
||||
|
||||
// Update the position and the view vector of the camera
|
||||
updatePosition();
|
||||
updateViewVector();
|
||||
}
|
||||
|
||||
// Set the vertical camera rotation angle
|
||||
void OutSideCamera::modifyVerticalAngleRotation(int screenDistance, float fps) {
|
||||
|
||||
// Update the vertical rotation angle of the camera
|
||||
verticalAngleRotation = verticalAngleRotation + (screenDistance * 60.0 / fps);
|
||||
|
||||
// Vertical angle limits
|
||||
if (verticalAngleRotation > 89) {
|
||||
verticalAngleRotation = 89;
|
||||
}
|
||||
if (verticalAngleRotation < 1) {
|
||||
verticalAngleRotation = 1;
|
||||
}
|
||||
|
||||
// Update the position and the view vector of the camera
|
||||
updatePosition();
|
||||
updateViewVector();
|
||||
}
|
||||
|
||||
// Increase the distance from origine of the camera (used for the zoom)
|
||||
void OutSideCamera::increaseDistance(float fps) {
|
||||
|
||||
// Increase the distance from origin
|
||||
distanceFromOrigin = distanceFromOrigin + (speed * 60 / fps);
|
||||
|
||||
// Update the position and the view vector of the camera
|
||||
updatePosition();
|
||||
updateViewVector();
|
||||
}
|
||||
|
||||
// Decrease the distance from origine of the camera (used for the zoom)
|
||||
void OutSideCamera::decreaseDistance(float fps) {
|
||||
|
||||
// Decrease the distance from origin
|
||||
distanceFromOrigin = distanceFromOrigin - (speed * 60 / fps);
|
||||
|
||||
// Limit condition
|
||||
if(distanceFromOrigin < 1) {
|
||||
distanceFromOrigin=1;
|
||||
}
|
||||
|
||||
// Update the position and the view vector of the camera
|
||||
updatePosition();
|
||||
updateViewVector();
|
||||
}
|
|
@ -1,64 +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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef OUTSIDE_CAMERA_H
|
||||
#define OUTSIDE_CAMERA_H
|
||||
|
||||
// Libraries
|
||||
#include "Camera.h"
|
||||
|
||||
// ---- Class OutSideCamera ----
|
||||
// This camera can be move everywhere around the scene to obtain a outside view of the house
|
||||
class OutSideCamera : public Camera {
|
||||
private :
|
||||
double heightFromFloor; // Height from the floor
|
||||
int horizontalAngleRotation; // Horizontal rotation angle (in degree)
|
||||
int verticalAngleRotation; // Vertical rotation angle (in degree)
|
||||
double distanceFromOrigin; // Distance of the camera from the origin (used to zoom)
|
||||
|
||||
public :
|
||||
OutSideCamera(); // Constructor
|
||||
OutSideCamera(const OutSideCamera& camera); // Copy-constructor
|
||||
virtual ~OutSideCamera(); // Destructor
|
||||
void updatePosition(); // Compute the new position of the camera
|
||||
void updateViewVector(); // Update the view vector of the camera
|
||||
double getHeightFromFloor() const; // Get the height of the camera from the floor
|
||||
void setHeightFromFloor(double height); // Set the height of the camera from the floor
|
||||
void modifyHorizontalAngleRotation(int screenDistance, float fps); // Modify the horizontal camera rotation angle
|
||||
void modifyVerticalAngleRotation(int screenDistance, float fps); // Modify the vertical camera rotation angle
|
||||
void increaseDistance(float fps); // Increase the distance of the camera from the origin
|
||||
void decreaseDistance(float fps); // Decrease the distance of the camera from the origin
|
||||
};
|
||||
|
||||
// Compute the new view vector of the camera (inline function)
|
||||
inline void OutSideCamera::updateViewVector() {
|
||||
viewVector = Vector3D(0.0, 0.0, 0.0) - position;
|
||||
}
|
||||
|
||||
// Get the height of the camera from the floor (inline function)
|
||||
inline double OutSideCamera::getHeightFromFloor() const {
|
||||
return heightFromFloor;
|
||||
}
|
||||
|
||||
// Set the height of the camera from the floor (inline function)
|
||||
inline void OutSideCamera::setHeightFromFloor(double height) {
|
||||
heightFromFloor = height;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -1,27 +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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef REACT_DEMO_H
|
||||
#define REACT_DEMO_H
|
||||
|
||||
// Constants
|
||||
const int WINWIDTH = 640; // Width of the OpenGL windows
|
||||
const int WINHEIGHT = 480; // Height of the OpenGL windows
|
||||
|
||||
#endif
|
|
@ -1,118 +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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
// Librairies
|
||||
#include "Scene.h"
|
||||
#include "Objects.h"
|
||||
|
||||
// Constructor of the class Scene
|
||||
Scene::Scene() {
|
||||
// Initialise the material specular color
|
||||
mat_specular[0] = 1.0;
|
||||
mat_specular[1] = 1.0;
|
||||
mat_specular[2] = 1.0;
|
||||
mat_specular[3] = 1.0;
|
||||
|
||||
// Initialize the material shininess
|
||||
mat_shininess[0] = 50.0;
|
||||
|
||||
// Initialise the light source position
|
||||
light_position[0] = 20.0;
|
||||
light_position[1] = 9.0;
|
||||
light_position[2] = 15.0;
|
||||
light_position[3] = 0.0;
|
||||
|
||||
// Initialise the ambient color of the light
|
||||
ambient_color[0] = 1.0;
|
||||
ambient_color[1] = 1.0;
|
||||
ambient_color[2] = 1.0;
|
||||
ambient_color[3] = 0.7;
|
||||
|
||||
// Initialise the diffuse light color
|
||||
white_light[0] = 1.0;
|
||||
white_light[1] = 1.0;
|
||||
white_light[2] = 1.0;
|
||||
white_light[3] = 1.0;
|
||||
}
|
||||
|
||||
// Destructor of the class Scene
|
||||
Scene::~Scene() {
|
||||
|
||||
}
|
||||
|
||||
// Init method
|
||||
void Scene::init() {
|
||||
glClearColor(0.0, 0.0, 0.0, 0.0); // Select the color for the background
|
||||
glShadeModel(GL_SMOOTH);
|
||||
glClearDepth(1.0);
|
||||
|
||||
// Lighting settings
|
||||
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); // Specular color of the material
|
||||
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); // Shininess of the material
|
||||
glLightfv(GL_LIGHT0, GL_POSITION, light_position); // Position of the light source
|
||||
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_color); // Ambient color of the light
|
||||
glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light); // Diffuse color of the light
|
||||
glLightfv(GL_LIGHT0, GL_SPECULAR, white_light); // Specular color of the light
|
||||
|
||||
glEnable(GL_LIGHTING); // Activate the lighting
|
||||
glEnable(GL_LIGHT0); // Activate a light source
|
||||
glEnable(GL_DEPTH_TEST); // Activate the Depth buffer
|
||||
//glEnable(GL_CULL_FACE);
|
||||
}
|
||||
|
||||
// Display method
|
||||
void Scene::display(const Context& context) const {
|
||||
glClearColor(0,0,0,0);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
|
||||
// Define the position and the direction of the camera
|
||||
//gluLookAt(30,10,0,0,0,0,0,1,0);
|
||||
double x = outsideCamera.getPosition().getX();
|
||||
double y = outsideCamera.getPosition().getY();
|
||||
double z = outsideCamera.getPosition().getZ();
|
||||
gluLookAt(x,y,z,0,0,0,0,1,0);
|
||||
|
||||
// Draw all objects in the context
|
||||
for(int i=0; i<context.getNbObjects(); ++i)
|
||||
{
|
||||
// Copy the active matrix on the matrix stack
|
||||
glPushMatrix();
|
||||
|
||||
// Draw the object
|
||||
context.getObject(i).draw();
|
||||
|
||||
// Remove the matrix on the top of the matrix stack
|
||||
glPopMatrix();
|
||||
}
|
||||
|
||||
// Change the buffers
|
||||
SDL_GL_SwapBuffers();
|
||||
}
|
||||
|
||||
// Reshape the window
|
||||
void Scene::reshape(int width, int height) {
|
||||
glViewport(0,0,width,height);
|
||||
glMatrixMode(GL_PROJECTION); // Specify the matrix that will be modified
|
||||
glLoadIdentity(); // Load the identity matrix before the transformations
|
||||
gluPerspective(45.0, (float) width/height, 0.1f, 150.0f);
|
||||
glMatrixMode(GL_MODELVIEW); // Specify the matrix that will be modified
|
||||
glLoadIdentity(); // Load the identity matrix before the transformations
|
||||
}
|
|
@ -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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef SCENE_H
|
||||
#define SCENE_H
|
||||
|
||||
// Libraries
|
||||
#include "Context.h"
|
||||
#include "OutSideCamera.h"
|
||||
|
||||
#include <SDL/SDL.h>
|
||||
#include <GL/freeglut.h> // Used only to draw cubes
|
||||
#include <GL/gl.h>
|
||||
#include <GL/glu.h>
|
||||
|
||||
|
||||
// Scene class
|
||||
class Scene {
|
||||
private :
|
||||
GLfloat mat_specular[4]; // Material specular light color
|
||||
GLfloat mat_shininess[1]; // Material shininess
|
||||
GLfloat light_position[4]; // Position of the light source
|
||||
GLfloat ambient_color[4]; // Ambient color of the light
|
||||
GLfloat white_light[4]; // White light color
|
||||
OutSideCamera outsideCamera; // OutSide camera (Camera that can move around the scene)
|
||||
|
||||
public :
|
||||
Scene(); // constructor of the class
|
||||
~Scene(); // Destructor of the class
|
||||
void init(); // Initialize the values of OpenGL
|
||||
void display(const Context& context) const; // display the scene
|
||||
void reshape(int width, int height); // Reshape the window
|
||||
OutSideCamera& getOutSideCamera() ; // Return a reference to the outside camera
|
||||
float getCameraAngle1() const; // Return the angle of the camera
|
||||
float getCameraAngle2() const; // Return the angle of the camera
|
||||
void setCameraAngle1(float angle); // Set the angle of the camera
|
||||
void setCameraAngle2(float angle); // Set the angle of the camera
|
||||
};
|
||||
|
||||
// Return a reference to the camera
|
||||
inline OutSideCamera& Scene::getOutSideCamera() {
|
||||
return outsideCamera;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -1,117 +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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
// Libraries
|
||||
#include "Simulation.h"
|
||||
#include "ReactDemo.h"
|
||||
#include <iostream>
|
||||
|
||||
// Constructor of the class Simulation
|
||||
Simulation::Simulation() {
|
||||
simRunning = false;
|
||||
mouseButtonPressed = false;
|
||||
fps = 0.0;
|
||||
}
|
||||
|
||||
// Destructor of the class Simulation
|
||||
Simulation::~Simulation() {
|
||||
|
||||
}
|
||||
|
||||
// Method to start the simulation
|
||||
void Simulation::start() {
|
||||
// Initialisation of the OpenGL settings for the scene
|
||||
scene.init();
|
||||
|
||||
// Reshape the windows for the first time
|
||||
scene.reshape(WINWIDTH, WINHEIGHT);
|
||||
|
||||
// Activation of the simulation
|
||||
simRunning = true;
|
||||
|
||||
// Get the current time
|
||||
currentFrameTime = SDL_GetTicks();
|
||||
|
||||
// Main loop of the simulation
|
||||
while(simRunning) {
|
||||
// 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 << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// This method checks if an events occur and call the apropriate method
|
||||
void Simulation::checkEvents() {
|
||||
SDL_Event event; // An SDL event
|
||||
|
||||
// Zoom of the outside camera
|
||||
if (SDL_GetKeyState(NULL)[SDLK_UP]) {
|
||||
scene.getOutSideCamera().decreaseDistance(fps);
|
||||
}
|
||||
else if(SDL_GetKeyState(NULL)[SDLK_DOWN]) {
|
||||
scene.getOutSideCamera().increaseDistance(fps);
|
||||
}
|
||||
|
||||
// Check in the stack of events
|
||||
while(SDL_PollEvent(&event)) {
|
||||
// Check an event
|
||||
switch(event.type) {
|
||||
// An QUIT event occur
|
||||
case SDL_QUIT: simRunning = false;
|
||||
break;
|
||||
|
||||
// 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;
|
||||
break;
|
||||
|
||||
// The size of the windows changed then we reshape the windows
|
||||
case SDL_VIDEORESIZE: scene.reshape(event.resize.w, event.resize.h);
|
||||
break;
|
||||
|
||||
// If the mouse moved
|
||||
case SDL_MOUSEMOTION: if (SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(1)) {
|
||||
// Rotation of the outSideCamera
|
||||
// TODO : Problem here when we try to implement fps indepence (if we try to
|
||||
// replace 60 by the variable fps
|
||||
scene.getOutSideCamera().modifyHorizontalAngleRotation(event.motion.xrel, 30);
|
||||
scene.getOutSideCamera().modifyVerticalAngleRotation(event.motion.yrel, 30);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Compute the framerate (fps) of the application
|
||||
void Simulation::computeFps() {
|
||||
double lastFrameTime = currentFrameTime;
|
||||
|
||||
// Get the current time
|
||||
currentFrameTime = SDL_GetTicks();
|
||||
|
||||
// Compute the new framerate
|
||||
fps = 1000 / double(currentFrameTime - lastFrameTime);
|
||||
}
|
|
@ -1,46 +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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef SIMULATION_H
|
||||
#define SIMULATION_H
|
||||
|
||||
// Librairies
|
||||
#include "Context.h"
|
||||
#include "Scene.h"
|
||||
|
||||
// Class Simulation
|
||||
class Simulation {
|
||||
private :
|
||||
Scene scene; // Scene object for displaying the simulation
|
||||
Context context; // Context of the simulation
|
||||
bool simRunning; // True if the simulation is running and false otherwise
|
||||
bool mouseButtonPressed; // True if the left mouse button is pressed
|
||||
double currentFrameTime; // Current frame time
|
||||
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
|
|
@ -1,106 +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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
// Libraries
|
||||
#include "../reactphysics3d/reactphysics3d.h"
|
||||
#include "Simulation.h"
|
||||
#include "ReactDemo.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <SDL/SDL.h>
|
||||
|
||||
|
||||
// Prototypes
|
||||
int initSDL();
|
||||
|
||||
// global variables
|
||||
SDL_Surface * pScreen; // Pointer to the SDL windows
|
||||
|
||||
// Main function
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
if(initSDL() > 0) {
|
||||
// If initSDL return an error then we exit the program
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// Initialize the glut library. We will use glut only to display shapes like cube or sphere
|
||||
glutInit(&argc, argv);
|
||||
|
||||
// Create a Simulation object used to simulate a physic world
|
||||
Simulation simulation;
|
||||
|
||||
// Start the simulation
|
||||
simulation.start();
|
||||
|
||||
std::cerr << "Fin normale du programme" << std::endl;
|
||||
|
||||
// To avoid warnings notifying that argc et argv aren't used
|
||||
(argc);
|
||||
(argv);
|
||||
|
||||
return (EXIT_SUCCESS);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Methode to initialise the SDL and the windows
|
||||
int initSDL() {
|
||||
// Initialisation of SDL
|
||||
if( SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
std::cerr << "Echec SDL_Init : " << SDL_GetError() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Select the method to exit
|
||||
atexit( SDL_Quit );
|
||||
|
||||
// Active double buffer mode
|
||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||
|
||||
// Select the Depth Buffer size
|
||||
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16);
|
||||
|
||||
// Activation of displaying in windows mode
|
||||
SDL_Surface* pScreen = SDL_SetVideoMode(WINWIDTH, WINHEIGHT, 32, SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_RESIZABLE | SDL_SWSURFACE);
|
||||
|
||||
// Check that displaying is activated
|
||||
if( !pScreen ) {
|
||||
std::cerr << "Echec de creation de la fenetre en 640x480 : " << SDL_GetError() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Define the window title and the window icon
|
||||
SDL_WM_SetCaption("React Demo 0.01", NULL);
|
||||
|
||||
// Get the state of the Double Buffer parameter
|
||||
int nValue;
|
||||
if( SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &nValue) < 0) {
|
||||
std::cerr << "Echec de recuperation du parametre SDL_GL_DOUBLEBUFFER : " << SDL_GetError() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Check that Double Buffer mode is activated
|
||||
if(nValue != 1) {
|
||||
std::cerr << "Erreur : SDL_GL_DOUBLEBUFFER inactif" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,415 +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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
// Libraries
|
||||
#include <cassert>
|
||||
#include "Matrix.h"
|
||||
|
||||
// Namespaces
|
||||
using namespace reactphysics3d;
|
||||
|
||||
// Constructor of the class Matrix
|
||||
Matrix::Matrix(int nbRow, int nbColumn) throw(std::invalid_argument)
|
||||
:nbRow(nbRow),nbColumn(nbColumn) {
|
||||
// Check the arguments
|
||||
if (nbRow>0 && nbColumn>0) {
|
||||
|
||||
// Create the two dimensional dynamic array
|
||||
array = new double*[nbRow];
|
||||
|
||||
assert(array != 0); // Array pointer musn't be null
|
||||
|
||||
for(int i=0; i<nbRow; ++i) {
|
||||
array[i] = new double[nbColumn];
|
||||
}
|
||||
|
||||
// Fill the matrix with zero's
|
||||
for (int i=0; i<nbRow; ++i) {
|
||||
for(int j=0; j<nbColumn; ++j) {
|
||||
setValue(i,j, 0.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Throw an exception
|
||||
throw std::invalid_argument("Exception : The size of the matrix has to be positive !");
|
||||
}
|
||||
}
|
||||
|
||||
// Copy-constructor of the class Matrix
|
||||
Matrix::Matrix(const Matrix& matrix)
|
||||
:nbRow(matrix.nbRow), nbColumn(matrix.nbColumn) {
|
||||
|
||||
// Create the two dimensional dynamic array
|
||||
array = new double*[nbRow];
|
||||
|
||||
assert(array != 0); // Array pointer musn't be null
|
||||
|
||||
for(int i=0; i<nbRow; ++i) {
|
||||
array[i] = new double[nbColumn];
|
||||
}
|
||||
|
||||
// Copy the matrix
|
||||
for (int i=0; i<nbRow; ++i) {
|
||||
for(int j=0; j<nbColumn; ++j) {
|
||||
setValue(i,j, matrix.getValue(i,j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Destructor of the class Matrix
|
||||
Matrix::~Matrix() {
|
||||
// Destruction of the dynamic array
|
||||
for(int i=0; i<nbRow; ++i) {
|
||||
delete array[i];
|
||||
}
|
||||
delete this->array;
|
||||
}
|
||||
|
||||
|
||||
// Function that return the cofactor matrix by removing row i and column j
|
||||
Matrix Matrix::getCofactor(int i, int j) const throw(std::invalid_argument) {
|
||||
// If i and j are in the matrix
|
||||
if (0<= i && i < nbRow && 0<= j && j<nbColumn) {
|
||||
// Create the cofactor matrix
|
||||
Matrix cofactor(nbRow-1,nbColumn-1);
|
||||
|
||||
int u=0; // Row coordinate in the cofactor matrix
|
||||
int v=0; // Column coordinate in the cofactor matrix
|
||||
|
||||
// For every element in the matrix
|
||||
for (int s=0; s<nbColumn; ++s) {
|
||||
for(int r=0; r<nbRow; ++r) {
|
||||
// If the element is not in row i or in column j
|
||||
if (r!=i && s!=j) {
|
||||
// Add the element in the cofactor matrix
|
||||
cofactor.setValue(u,v, getValue(r,s));
|
||||
++u;
|
||||
if (u==cofactor.nbRow) {
|
||||
u = 0;
|
||||
++v;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return the cofactor matrix
|
||||
return cofactor;
|
||||
}
|
||||
else {
|
||||
// We Throw an out_of_range exception
|
||||
throw std::invalid_argument("Exception : The index i or j is outside the matrix size !");
|
||||
}
|
||||
}
|
||||
|
||||
// This function return the transposed matrix
|
||||
Matrix Matrix::getTranspose() const {
|
||||
// Create the new matrix
|
||||
Matrix transposedMatrix(nbColumn, nbRow);
|
||||
|
||||
// Transposition of the matrix
|
||||
for (int i=0; i<nbRow; ++i) {
|
||||
for(int j=0; j<nbColumn; ++j) {
|
||||
transposedMatrix.setValue(j,i, array[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
// Return the transposed matrix
|
||||
return transposedMatrix;
|
||||
}
|
||||
|
||||
|
||||
// Function that return the inverse of the matrix if there exists
|
||||
Matrix Matrix::getInverse() const throw(MathematicsException) {
|
||||
// Check if the matrix is a square-matrix
|
||||
if (nbRow==nbColumn) {
|
||||
// Compute the determinant of the matrix
|
||||
double determinant = getDeterminant();
|
||||
|
||||
// Check if the matrix is invertible
|
||||
if (determinant != 0.0) {
|
||||
// Create a temp matrix
|
||||
Matrix tempMatrix(nbRow, nbColumn);
|
||||
|
||||
double k=1.0;
|
||||
|
||||
// Compute the inverse matrix
|
||||
for(int i=0; i<nbRow; ++i) {
|
||||
for(int j=0; j<nbColumn; ++j) {
|
||||
if ( (i+j) % 2 == 0) {
|
||||
k=1.0;
|
||||
}
|
||||
else {
|
||||
k=-1.0;
|
||||
}
|
||||
|
||||
tempMatrix.setValue(i,j, k * getCofactor(i,j).getDeterminant());
|
||||
}
|
||||
}
|
||||
|
||||
// Create the inverse matrix
|
||||
Matrix inverseMatrix = tempMatrix.getTranspose() * (1.0 / determinant);
|
||||
|
||||
// Return the inverse matrix
|
||||
return inverseMatrix;
|
||||
}
|
||||
else {
|
||||
// We throw a MathematicsException
|
||||
throw MathematicsException("MathematicsException : Inverse of the matrix can't be computed because the determinant is zero !");
|
||||
}
|
||||
}
|
||||
else {
|
||||
// We throw an Matrix Exception
|
||||
throw MathematicsException("MathematicsException : Inverse can't be computed for a non-square matrix !");
|
||||
}
|
||||
}
|
||||
|
||||
// Function that return the determinant of the matrix
|
||||
double Matrix::getDeterminant() const throw(MathematicsException) {
|
||||
// If the matrix is a square matrix
|
||||
if (nbRow == nbColumn) {
|
||||
if(nbRow == 1) {
|
||||
return getValue(0,0);
|
||||
}
|
||||
else if (nbRow == 2) {
|
||||
return (getValue(0,0) * getValue(1,1) - getValue(1,0) * getValue(0,1));
|
||||
}
|
||||
else {
|
||||
double determinant = 0.0;
|
||||
double k=1.0;
|
||||
|
||||
// For every element in the first row
|
||||
for(int j=0; j<nbColumn; ++j) {
|
||||
determinant = determinant + k * getValue(0,j) * getCofactor(0,j).getDeterminant();
|
||||
|
||||
if (k==1.0) {
|
||||
k=-1.0;
|
||||
}
|
||||
else {
|
||||
k=1.0;
|
||||
}
|
||||
}
|
||||
|
||||
// Return the determinant value
|
||||
return determinant;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Throw a MathematicsException
|
||||
throw MathematicsException("MathematicsException : The determinant of a non-square matrix isn't computable !");
|
||||
}
|
||||
}
|
||||
|
||||
// Return the trace of the matrix
|
||||
double Matrix::getTrace() const throw(MathematicsException) {
|
||||
|
||||
// Check if the matrix is a square-matrix
|
||||
if (nbRow == nbColumn) {
|
||||
double sum = 0.0;
|
||||
|
||||
// Compute the trace of the matrix
|
||||
for(int i=0; i<nbRow; ++i) {
|
||||
sum = sum + array[i][i];
|
||||
}
|
||||
|
||||
// Return the trace
|
||||
return sum;
|
||||
}
|
||||
else {
|
||||
// We throw an exception because the matrix is non-square
|
||||
throw MathematicsException("MathematicsException : Impossible to compute the trace for a non-square matrix");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Static function that return a identity matrix of size nxn
|
||||
Matrix Matrix::identity(int dimension) throw(std::invalid_argument) {
|
||||
// Argument verification
|
||||
if (dimension > 0) {
|
||||
// Create a new matrix
|
||||
Matrix identityMatrix(dimension,dimension);
|
||||
|
||||
// Fill in the identity matrix
|
||||
for(int i=0; i<dimension; ++i) {
|
||||
for(int j=0; j<dimension; ++j) {
|
||||
if (i==j) {
|
||||
identityMatrix.setValue(i, j, 1.0);
|
||||
}
|
||||
else {
|
||||
identityMatrix.setValue(i, j, 0.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return the identity matrix
|
||||
return identityMatrix;
|
||||
}
|
||||
else {
|
||||
// Throw an exception
|
||||
throw std::invalid_argument("Exception : The argument of identity() has to be positive !");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Definition of the operator + for the sum of two matrices with references
|
||||
Matrix Matrix::operator+(const Matrix& matrix2) const throw(MathematicsException) {
|
||||
if (nbRow == matrix2.nbRow && nbColumn == matrix2.nbColumn) {
|
||||
// Create a new matrix
|
||||
Matrix sumMatrix(nbRow,nbColumn);
|
||||
|
||||
// Sum the two matrices
|
||||
for(int i=0; i<nbRow; ++i) {
|
||||
for(int j=0; j<nbColumn; ++j) {
|
||||
sumMatrix.setValue(i, j, this->getValue(i,j) + matrix2.getValue(i,j));
|
||||
}
|
||||
}
|
||||
|
||||
// Return the sum matrix
|
||||
return sumMatrix;
|
||||
}
|
||||
else {
|
||||
// We throw an MathematicsException
|
||||
throw MathematicsException("MathematicsException : Addition of the matrices isn't possible beacause the size of the matrices aren't the same");
|
||||
}
|
||||
}
|
||||
|
||||
// Definition of the operator - for the substraction of two matrices with references
|
||||
Matrix Matrix::operator-(const Matrix& matrix2) const throw(MathematicsException) {
|
||||
if (nbRow == matrix2.nbRow && nbColumn == matrix2.nbColumn) {
|
||||
// Create a new matrix
|
||||
Matrix sumMatrix(nbRow, nbColumn);
|
||||
|
||||
// Substract the two matrices
|
||||
for(int i=0; i<nbRow; ++i) {
|
||||
for(int j=0; j<this->nbColumn; ++j) {
|
||||
sumMatrix.setValue(i, j, this->getValue(i,j) - matrix2.getValue(i,j));
|
||||
}
|
||||
}
|
||||
|
||||
// Return the sum matrix
|
||||
return sumMatrix;
|
||||
}
|
||||
else {
|
||||
// We throw a MathematicsException
|
||||
throw MathematicsException("MathematicsException : Substraction of the matrices isn't possible beacause the size of the matrices aren't the same");
|
||||
}
|
||||
}
|
||||
|
||||
// Overloaded operator * for the multiplication of the matrix with a number
|
||||
Matrix Matrix::operator*(double nb) const {
|
||||
// Creation of the result matrix
|
||||
Matrix result(nbRow,nbColumn);
|
||||
|
||||
// Multiplication of the matrix with the number
|
||||
for(int i=0; i<nbRow; ++i) {
|
||||
for(int j=0; j<nbColumn; ++j) {
|
||||
result.setValue(i,j, getValue(i,j) * nb);
|
||||
}
|
||||
}
|
||||
|
||||
// Return the result matrix
|
||||
return result;
|
||||
}
|
||||
|
||||
// Overloaded operator for multiplication with a matrix
|
||||
Matrix Matrix::operator*(const Matrix& matrix2) const throw(MathematicsException) {
|
||||
// Check the sizes of the matrices
|
||||
if (nbColumn == matrix2.nbRow) {
|
||||
// Compute the result of the multiplication
|
||||
Matrix result(nbRow, matrix2.nbColumn);
|
||||
double sum;
|
||||
|
||||
for(int i=0; i<nbRow; ++i) {
|
||||
for(int j=0; j<matrix2.nbColumn; ++j) {
|
||||
sum = 0.0;
|
||||
for(int k=0; k<nbColumn; ++k) {
|
||||
sum = sum + array[i][k] * matrix2.array[k][j];
|
||||
}
|
||||
result.array[i][j] = sum;
|
||||
}
|
||||
}
|
||||
|
||||
// Return the result matrix
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
// Throw an exception because the multiplication is impossible
|
||||
throw MathematicsException("MathematicsException : The sizes of the matrices aren't compatible for the multiplication");
|
||||
}
|
||||
}
|
||||
|
||||
// Overloaded operator = for the assignment
|
||||
Matrix& Matrix::operator=(const Matrix& matrix2) throw(MathematicsException) {
|
||||
|
||||
// Check for self-assignement
|
||||
if(this == &matrix2) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Check the size of the matrix
|
||||
if (nbRow==matrix2.nbRow && nbColumn==matrix2.nbColumn) {
|
||||
// Check for self-assignment
|
||||
if (this != &matrix2) {
|
||||
for(int i=0; i<nbRow; ++i) {
|
||||
for(int j=0; j<nbColumn; ++j) {
|
||||
this->setValue(i,j, matrix2.getValue(i,j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return a reference to the matrix
|
||||
return *this;
|
||||
}
|
||||
else {
|
||||
// Throw a MathematicsException
|
||||
throw MathematicsException("MathematicsException : Assignment impossible because the size of the matrices aren't the same !");
|
||||
}
|
||||
}
|
||||
|
||||
// Overloaded operator for equality condition
|
||||
bool Matrix::operator==(const Matrix& matrix2) const throw(MathematicsException) {
|
||||
// Check if the matrices dimensions are compatible
|
||||
if (nbRow == matrix2.nbRow && nbColumn == matrix2.nbColumn) {
|
||||
for (int i=0; i<nbRow; ++i) {
|
||||
for(int j=0; j<nbColumn; ++j) {
|
||||
if (array[i][j] != matrix2.array[i][j]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
// Throw an exception because the matrices dimensions aren't the same
|
||||
throw MathematicsException("MathematicsException : Impossible to check if the matrices are equal because they don't have the same dimension");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// TO DELETE, THIS IS JUST FOR TESTING MATRICES
|
||||
void Matrix::display() const {
|
||||
for(int i=0; i<nbRow; ++i) {
|
||||
for(int j=0; j<nbColumn; ++j) {
|
||||
std::cout << array[i][j] << " ";
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
|
@ -1,111 +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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef MATRIX_H
|
||||
#define MATRIX_H
|
||||
|
||||
// Libraries
|
||||
#include "exceptions.h"
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
|
||||
// ReactPhysics3D namespace
|
||||
namespace reactphysics3d {
|
||||
|
||||
/* -------------------------------------------------------------------
|
||||
Class Matrix :
|
||||
This class represents a matrix.
|
||||
-------------------------------------------------------------------
|
||||
*/
|
||||
class Matrix {
|
||||
private :
|
||||
int nbRow; // Number of row in the matrix
|
||||
int nbColumn; // Number of colum in the matrix
|
||||
double** array; // Dynamic array that contains the values of the matrix
|
||||
|
||||
public :
|
||||
Matrix(int nbRow, int nbColum) throw(std::invalid_argument); // Constructor of the class Matrix
|
||||
Matrix(const Matrix& matrix); // Copy constructor of the class Matrix
|
||||
virtual ~Matrix(); // Destructor of the class Matrix
|
||||
double getValue(int i, int j) const throw(std::invalid_argument); // Return a value in the matrix
|
||||
void setValue(int i, int j, double value) throw(std::invalid_argument); // Set a value in the matrix
|
||||
int getNbRow() const; // Return the number of row of the matrix
|
||||
int getNbColumn() const; // Return the number of column of the matrix
|
||||
Matrix getCofactor(int i, int j) const throw(std::invalid_argument); // Return the cofactor matrix by removing row i and column j
|
||||
Matrix getTranspose() const; // Return the transposed matrixs
|
||||
Matrix getInverse() const throw(MathematicsException); // Return the inverse of the matrix if there exists
|
||||
double getDeterminant() const throw(MathematicsException); // Return the determinant of the matrix
|
||||
double getTrace() const throw(MathematicsException); // Return the trace of a square matrix
|
||||
static Matrix identity(int dimension) throw(std::invalid_argument); // Return the identity matrix I of the given dimension
|
||||
|
||||
void display() const; // TO DELETE
|
||||
|
||||
// --- Overloaded operators --- //
|
||||
Matrix operator+(const Matrix& matrix2) const throw(MathematicsException); // Overloaded operator for addition
|
||||
Matrix operator-(const Matrix& matrix2) const throw(MathematicsException); // Overloaded operator for substraction
|
||||
Matrix operator*(double nb) const; // Overloaded operator for multiplication with a number
|
||||
Matrix operator*(const Matrix& matrix2) const throw(MathematicsException); // Overloaded operator for multiplication with a matrix
|
||||
Matrix& operator=(const Matrix& matrix2) throw(MathematicsException); // Overloaded operator for assignment
|
||||
bool operator==(const Matrix& matrix2) const throw(MathematicsException); // Overloaded operator for equality condition
|
||||
};
|
||||
|
||||
// Function to get a value in the matrix (inline)
|
||||
inline double Matrix::getValue(int i, int j) const throw(std::invalid_argument) {
|
||||
if (0 <= i && i < nbRow && 0 <= j && j < nbColumn) {
|
||||
// get the value in the matrix
|
||||
return array[i][j];
|
||||
}
|
||||
else {
|
||||
// We Throw an out_of_range exception
|
||||
throw std::invalid_argument("Exception : The index i or j is outside the matrix size !");
|
||||
}
|
||||
}
|
||||
|
||||
// Function to set a value in the matrix (inline)
|
||||
inline void Matrix::setValue(int i, int j, double value) throw(std::invalid_argument) {
|
||||
if (0 <= i && i < nbRow && 0 <= j && j < nbColumn) {
|
||||
// Set the value in the matrix
|
||||
this->array[i][j] = value;
|
||||
}
|
||||
else {
|
||||
// We Throw an out_of_range exception
|
||||
throw std::invalid_argument("Exception : The index i or j is outside the matrix size !");
|
||||
}
|
||||
}
|
||||
|
||||
// Function that return the number of row of the matrix (inline)
|
||||
inline int Matrix::getNbRow() const {
|
||||
return nbRow;
|
||||
}
|
||||
|
||||
// Function that return the number of colum of the matrix (inline)
|
||||
inline int Matrix::getNbColumn() const {
|
||||
return nbColumn;
|
||||
}
|
||||
|
||||
// Overloaded operator for multiplication between a number and a Matrix (inline)
|
||||
inline Matrix operator*(double number, const Matrix& matrix) {
|
||||
|
||||
// Return the result matrix
|
||||
return matrix * number;
|
||||
}
|
||||
|
||||
} // End of the ReactPhysics3D namespace
|
||||
|
||||
#endif
|
|
@ -1,219 +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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
// Libraries
|
||||
#include <iostream>
|
||||
#include "Matrix3x3.h"
|
||||
|
||||
// Namespaces
|
||||
using namespace reactphysics3d;
|
||||
|
||||
// Constructor of the class Matrix3x3
|
||||
Matrix3x3::Matrix3x3() {
|
||||
// Initialize all values in the matrix to zero
|
||||
setAllValues(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
|
||||
}
|
||||
|
||||
// Constructor with arguments
|
||||
Matrix3x3::Matrix3x3(double a1, double a2, double a3, double b1, double b2, double b3, double c1, double c2, double c3) {
|
||||
// Initialize the matrix with the values
|
||||
setAllValues(a1, a2, a3, b1, b2, b3, c1, c2, c3);
|
||||
}
|
||||
|
||||
// Copy-constructor
|
||||
Matrix3x3::Matrix3x3(const Matrix3x3& matrix2) {
|
||||
// Copy the values in the matrix
|
||||
setAllValues(matrix2.array[0][0], matrix2.array[0][1], matrix2.array[0][2],
|
||||
matrix2.array[1][0], matrix2.array[1][1], matrix2.array[1][2],
|
||||
matrix2.array[2][0], matrix2.array[2][1], matrix2.array[2][2]);
|
||||
}
|
||||
|
||||
// Create a Matrix3x3 from a quaternion (the quaternion can be non-unit)
|
||||
Matrix3x3::Matrix3x3(const Quaternion& quaternion) {
|
||||
double x = quaternion.getX();
|
||||
double y = quaternion.getY();
|
||||
double z = quaternion.getZ();
|
||||
double w = quaternion.getW();
|
||||
|
||||
double nQ = x*x + y*y + z*z + w*w;
|
||||
double s = 0.0;
|
||||
|
||||
if (nQ > 0.0) {
|
||||
s = 2.0/nQ;
|
||||
}
|
||||
|
||||
// Computations used for optimization (less multiplications)
|
||||
double xs = x*s;
|
||||
double ys = y*s;
|
||||
double zs = z*s;
|
||||
double wxs = w*xs;
|
||||
double wys = w*ys;
|
||||
double wzs = w*zs;
|
||||
double xxs = x*xs;
|
||||
double xys = x*ys;
|
||||
double xzs = x*zs;
|
||||
double yys = y*ys;
|
||||
double yzs = y*zs;
|
||||
double zzs = z*zs;
|
||||
|
||||
// Create the matrix corresponding to the quaternion
|
||||
setAllValues(1.0-yys-zzs, xys-wzs, xzs + wys,
|
||||
xys + wzs, 1.0-xxs-zzs, yzs-wxs,
|
||||
xzs-wys, yzs + wxs, 1.0-xxs-yys);
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Matrix3x3::~Matrix3x3() {
|
||||
|
||||
}
|
||||
|
||||
// Return the inverse matrix
|
||||
Matrix3x3 Matrix3x3::getInverse() const throw(MathematicsException) {
|
||||
// Compute the determinant of the matrix
|
||||
double determinant = getDeterminant();
|
||||
|
||||
// Check if the determinant is equal to zero
|
||||
if (determinant != 0) {
|
||||
double invDeterminant = 1.0 / determinant;
|
||||
Matrix3x3 tempMatrix;
|
||||
|
||||
// Compute the inverse of the matrix
|
||||
tempMatrix.setAllValues((array[1][1]*array[2][2]-array[2][1]*array[1][2]), -(array[1][0]*array[2][2]-array[2][0]*array[1][2]), (array[1][0]*array[2][1]-array[2][0]*array[1][1]),
|
||||
-(array[0][1]*array[2][2]-array[2][1]*array[0][2]), (array[0][0]*array[2][2]-array[2][0]*array[0][2]), -(array[0][0]*array[2][1]-array[2][0]*array[0][1]),
|
||||
(array[0][1]*array[1][2]-array[0][2]*array[1][1]), -(array[0][0]*array[1][2]-array[1][0]*array[0][2]), (array[0][0]*array[1][1]-array[0][1]*array[1][0]));
|
||||
|
||||
// Return the inverse matrix
|
||||
return (invDeterminant * tempMatrix.getTranspose());
|
||||
}
|
||||
else {
|
||||
// Throw an exception because the inverse of the matrix doesn't exist if the determinant is equal to zero
|
||||
throw MathematicsException("MathematicsException : Impossible to compute the inverse of the matrix because the determinant is equal to zero");
|
||||
}
|
||||
}
|
||||
|
||||
// Return the quaternion corresponding to the matrix (it returns a unit quaternion)
|
||||
Quaternion Matrix3x3::getQuaternion() const {
|
||||
|
||||
// Get the trace of the matrix
|
||||
double trace = getTrace();
|
||||
|
||||
double r;
|
||||
double s;
|
||||
|
||||
if (trace < 0.0) {
|
||||
if (array[1][1] > array[0][0]) {
|
||||
if(array[2][2] > array[1][1]) {
|
||||
r = sqrt(array[2][2] - array[0][0] - array[1][1] + 1.0);
|
||||
s = 0.5 / r;
|
||||
return Quaternion((array[2][0] + array[0][2])*s, (array[1][2] + array[2][1])*s, 0.5*r, (array[1][0] - array[0][1])*s);
|
||||
}
|
||||
else {
|
||||
r = sqrt(array[1][1] - array[2][2] - array[0][0] + 1.0);
|
||||
s = 0.5 / r;
|
||||
return Quaternion((array[0][1] + array[1][0])*s, 0.5 * r, (array[1][2] + array[2][1])*s, (array[0][2] - array[2][0])*s);
|
||||
}
|
||||
}
|
||||
else if (array[2][2] > array[0][0]) {
|
||||
r = sqrt(array[2][2] - array[0][0] - array[1][1] + 1.0);
|
||||
s = 0.5 / r;
|
||||
return Quaternion((array[2][0] + array[0][2])*s, (array[1][2] + array[2][1])*s, 0.5 * r, (array[1][0] - array[0][1])*s);
|
||||
}
|
||||
else {
|
||||
r = sqrt(array[0][0] - array[1][1] - array[2][2] + 1.0);
|
||||
s = 0.5 / r;
|
||||
return Quaternion(0.5 * r, (array[0][1] + array[1][0])*s, (array[2][0] - array[0][2])*s, (array[2][1] - array[1][2])*s);
|
||||
}
|
||||
}
|
||||
else {
|
||||
r = sqrt(trace + 1.0);
|
||||
s = 0.5/r;
|
||||
return Quaternion((array[2][1]-array[1][2])*s, (array[0][2]-array[2][0])*s, (array[1][0]-array[0][1])*s, 0.5 * r);
|
||||
}
|
||||
}
|
||||
|
||||
// Return the 3x3 identity matrix
|
||||
Matrix3x3 Matrix3x3::identity() {
|
||||
// Return the isdentity matrix
|
||||
return Matrix3x3(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0);
|
||||
}
|
||||
|
||||
// TO DELETE, THIS IS JUST FOR TESTING MATRICES
|
||||
void Matrix3x3::display() const {
|
||||
for(int i=0; i<3; ++i) {
|
||||
for(int j=0; j<3; ++j) {
|
||||
std::cout << array[i][j] << " ";
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// Overloaded operator for addition
|
||||
Matrix3x3 Matrix3x3::operator+(const Matrix3x3& matrix2) const {
|
||||
// Return the sum matrix
|
||||
return Matrix3x3(array[0][0] + matrix2.array[0][0], array[0][1] + matrix2.array[0][1], array[0][2] + matrix2.array[0][2],
|
||||
array[1][0] + matrix2.array[1][0], array[1][1] + matrix2.array[1][1], array[1][2] + matrix2.array[1][2],
|
||||
array[2][0] + matrix2.array[2][0], array[2][1] + matrix2.array[2][1], array[2][2] + matrix2.array[2][2]);
|
||||
}
|
||||
|
||||
// Overloaded operator for substraction
|
||||
Matrix3x3 Matrix3x3::operator-(const Matrix3x3& matrix2) const {
|
||||
// Return the substraction matrix
|
||||
return Matrix3x3(array[0][0] - matrix2.array[0][0], array[0][1] - matrix2.array[0][1], array[0][2] - matrix2.array[0][2],
|
||||
array[1][0] - matrix2.array[1][0], array[1][1] - matrix2.array[1][1], array[1][2] - matrix2.array[1][2],
|
||||
array[2][0] - matrix2.array[2][0], array[2][1] - matrix2.array[2][1], array[2][2] - matrix2.array[2][2]);
|
||||
}
|
||||
|
||||
// Overloaded operator for multiplication with a number
|
||||
Matrix3x3 Matrix3x3::operator*(double nb) const {
|
||||
// Return multiplied matrix
|
||||
return Matrix3x3(array[0][0] * nb, array[0][1] * nb, array[0][2] * nb,
|
||||
array[1][0] * nb, array[1][1] * nb, array[1][2] * nb,
|
||||
array[2][0] * nb, array[2][1] * nb, array[2][2] * nb);
|
||||
}
|
||||
|
||||
// Overloaded operator for multiplication with a matrix
|
||||
Matrix3x3 Matrix3x3::operator*(const Matrix3x3& matrix2) const {
|
||||
// Compute and return the multiplication of the matrices
|
||||
return Matrix3x3(array[0][0]*matrix2.array[0][0] + array[0][1]*matrix2.array[1][0] + array[0][2]*matrix2.array[2][0],
|
||||
array[0][0]*matrix2.array[0][1] + array[0][1]*matrix2.array[1][1] + array[0][2]*matrix2.array[2][1],
|
||||
array[0][0]*matrix2.array[0][2] + array[0][1]*matrix2.array[1][2] + array[0][2]*matrix2.array[2][2],
|
||||
array[1][0]*matrix2.array[0][0] + array[1][1]*matrix2.array[1][0] + array[1][2]*matrix2.array[2][0],
|
||||
array[1][0]*matrix2.array[0][1] + array[1][1]*matrix2.array[1][1] + array[1][2]*matrix2.array[2][1],
|
||||
array[1][0]*matrix2.array[0][2] + array[1][1]*matrix2.array[1][2] + array[1][2]*matrix2.array[2][2],
|
||||
array[2][0]*matrix2.array[0][0] + array[2][1]*matrix2.array[1][0] + array[2][2]*matrix2.array[2][0],
|
||||
array[2][0]*matrix2.array[0][1] + array[2][1]*matrix2.array[1][1] + array[2][2]*matrix2.array[2][1],
|
||||
array[2][0]*matrix2.array[0][2] + array[2][1]*matrix2.array[1][2] + array[2][2]*matrix2.array[2][2]);
|
||||
}
|
||||
|
||||
// Overloaded operator for assignment
|
||||
Matrix3x3& Matrix3x3::operator=(const Matrix3x3& matrix2) {
|
||||
// Check for self-assignment
|
||||
if (this != &matrix2) {
|
||||
setAllValues(matrix2.array[0][0], matrix2.array[0][1], matrix2.array[0][2],
|
||||
matrix2.array[1][0], matrix2.array[1][1], matrix2.array[1][2],
|
||||
matrix2.array[2][0], matrix2.array[2][1], matrix2.array[2][2]);
|
||||
}
|
||||
|
||||
// Return a reference to the matrix
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -1,150 +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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#ifndef MATRIX3X3_H
|
||||
#define MATRIX3X3_H
|
||||
|
||||
// Libraries
|
||||
#include "exceptions.h"
|
||||
#include "Quaternion.h"
|
||||
|
||||
// ReactPhysics3D namespace
|
||||
namespace reactphysics3d {
|
||||
|
||||
|
||||
/* -------------------------------------------------------------------
|
||||
Class Matrix3x3 :
|
||||
This class represents a 3x3 matrix.
|
||||
-------------------------------------------------------------------
|
||||
*/
|
||||
class Matrix3x3 {
|
||||
private :
|
||||
double array[3][3]; // Array with the values of the matrix
|
||||
|
||||
public :
|
||||
Matrix3x3(); // Constructor of the class Matrix3x3
|
||||
Matrix3x3(double a1, double a2, double a3, double b1, double b2, double b3,
|
||||
double c1, double c2, double c3); // Constructor with arguments
|
||||
Matrix3x3(const Matrix3x3& matrix); // Copy-constructor
|
||||
Matrix3x3(const Quaternion& quaternion); // Create a Matrix3x3 from a Quaternion
|
||||
virtual ~Matrix3x3(); // Destructor
|
||||
double getValue(int i, int j) const throw(std::invalid_argument); // Get a value in the matrix
|
||||
void setValue(int i, int j, double value) throw(std::invalid_argument); // Set a value in the matrix
|
||||
void setAllValues(double a1, double a2, double a3, double b1, double b2, double b3,
|
||||
double c1, double c2, double c3); // Set all the values in the matrix
|
||||
Matrix3x3 getTranspose() const; // Return the transpose matrix
|
||||
double getDeterminant() const; // Return the determinant of the matrix
|
||||
double getTrace() const; // Return the trace of the matrix
|
||||
Matrix3x3 getInverse() const throw(MathematicsException); // Return the inverse matrix
|
||||
Quaternion getQuaternion() const; // Return the unit quaternion corresponding to the matrix
|
||||
static Matrix3x3 identity(); // Return the 3x3 identity matrix
|
||||
|
||||
void display() const; // TO DELETE
|
||||
|
||||
// --- Overloaded operators --- //
|
||||
Matrix3x3 operator+(const Matrix3x3& matrix2) const; // Overloaded operator for addition
|
||||
Matrix3x3 operator-(const Matrix3x3& matrix2) const ; // Overloaded operator for substraction
|
||||
Matrix3x3 operator*(double nb) const; // Overloaded operator for multiplication with a number
|
||||
Matrix3x3 operator*(const Matrix3x3& matrix2) const; // Overloaded operator for multiplication with a matrix
|
||||
Matrix3x3& operator=(const Matrix3x3& matrix2); // Overloaded operator for assignment
|
||||
bool operator==(const Matrix3x3& matrix2) const; // Overloaded operator for equality condition
|
||||
};
|
||||
|
||||
|
||||
// Method to get a value in the matrix (inline)
|
||||
inline double Matrix3x3::getValue(int i, int j) const throw(std::invalid_argument) {
|
||||
// Check the argument
|
||||
if (i>=0 && i<3 && j>=0 && j<3) {
|
||||
// Return the value
|
||||
return array[i][j];
|
||||
}
|
||||
else {
|
||||
// Throw an exception because of the wrong argument
|
||||
throw std::invalid_argument("Exception : The argument isn't in the bounds of the 3x3 matrix");
|
||||
}
|
||||
}
|
||||
|
||||
// Method to set a value in the matrix (inline)
|
||||
inline void Matrix3x3::setValue(int i, int j, double value) throw(std::invalid_argument) {
|
||||
// Check the argument
|
||||
if (i>=0 && i<3 && j>=0 && j<3) {
|
||||
// Set the value
|
||||
array[i][j] = value;
|
||||
}
|
||||
else {
|
||||
// Throw an exception because of the wrong argument
|
||||
throw std::invalid_argument("Exception : The argument isn't in the bounds of the 3x3 matrix");
|
||||
}
|
||||
} // End of the dcmaths namespace
|
||||
|
||||
// Method to set all the values in the matrix
|
||||
inline void Matrix3x3::setAllValues(double a1, double a2, double a3, double b1, double b2, double b3,
|
||||
double c1, double c2, double c3) {
|
||||
// Set all the values of the matrix
|
||||
array[0][0] = a1;
|
||||
array[0][1] = a2;
|
||||
array[0][2] = a3;
|
||||
|
||||
array[1][0] = b1;
|
||||
array[1][1] = b2;
|
||||
array[1][2] = b3;
|
||||
|
||||
array[2][0] = c1;
|
||||
array[2][1] = c2;
|
||||
array[2][2] = c3;
|
||||
}
|
||||
|
||||
// Return the transpose matrix
|
||||
inline Matrix3x3 Matrix3x3::getTranspose() const {
|
||||
// Return the transpose matrix
|
||||
return Matrix3x3(array[0][0], array[1][0], array[2][0],
|
||||
array[0][1], array[1][1], array[2][1],
|
||||
array[0][2], array[1][2], array[2][2]);
|
||||
}
|
||||
|
||||
// Return the determinant of the matrix
|
||||
inline double Matrix3x3::getDeterminant() const {
|
||||
// Compute and return the determinant of the matrix
|
||||
return (array[0][0]*(array[1][1]*array[2][2]-array[2][1]*array[1][2]) - array[0][1]*(array[1][0]*array[2][2]-array[2][0]*array[1][2]) +
|
||||
array[0][2]*(array[1][0]*array[2][1]-array[2][0]*array[1][1]));
|
||||
}
|
||||
|
||||
// Return the trace of the matrix
|
||||
inline double Matrix3x3::getTrace() const {
|
||||
// Compute and return the trace
|
||||
return (array[0][0] + array[1][1] + array[2][2]);
|
||||
}
|
||||
|
||||
// Overloaded operator for multiplication between a number and a Matrix3x3 (inline)
|
||||
inline Matrix3x3 operator*(double number, const Matrix3x3& matrix) {
|
||||
// Return the multiplied matrix
|
||||
return matrix * number;
|
||||
}
|
||||
|
||||
// Overloaded operator for equality condition
|
||||
inline bool Matrix3x3::operator==(const Matrix3x3& matrix2) const {
|
||||
return (array[0][0] == matrix2.array[0][0] && array[0][1] == matrix2.array[0][1] && array[0][2] == matrix2.array[0][2] &&
|
||||
array[1][0] == matrix2.array[1][0] && array[1][1] == matrix2.array[1][1] && array[1][2] == matrix2.array[1][2] &&
|
||||
array[2][0] == matrix2.array[2][0] && array[2][1] == matrix2.array[2][1] && array[2][2] == matrix2.array[2][2]);
|
||||
}
|
||||
|
||||
} // End of the ReactPhysics3D namespace
|
||||
|
||||
#endif
|
|
@ -1,55 +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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
// Libraries
|
||||
#include "Quaternion.h"
|
||||
|
||||
// Namespaces
|
||||
using namespace reactphysics3d;
|
||||
|
||||
// Constructor of the class
|
||||
Quaternion::Quaternion()
|
||||
:x(0.0), y(0.0), z(0.0), w(0.0) {
|
||||
|
||||
}
|
||||
|
||||
// Constructor with arguments
|
||||
Quaternion::Quaternion(double x, double y, double z, double w)
|
||||
:x(x), y(y), z(z), w(w) {
|
||||
|
||||
}
|
||||
|
||||
// Constructor with the component w and the vector v=(x y z)
|
||||
Quaternion::Quaternion(double w, const Vector3D& v)
|
||||
:x(v.getX()), y(v.getY()), z(v.getZ()), w(w) {
|
||||
|
||||
}
|
||||
|
||||
// Copy-constructor
|
||||
Quaternion::Quaternion(const Quaternion& quaternion)
|
||||
:x(quaternion.x), y(quaternion.y), z(quaternion.z), w(quaternion.w) {
|
||||
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Quaternion::~Quaternion() {
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -1,209 +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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef QUATERNION_H
|
||||
#define QUATERNION_H
|
||||
|
||||
// Libraries
|
||||
#include <cmath>
|
||||
#include "Vector3D.h"
|
||||
#include "exceptions.h"
|
||||
|
||||
// ReactPhysics3D namespace
|
||||
namespace reactphysics3d {
|
||||
|
||||
/* -------------------------------------------------------------------
|
||||
Class Quaternion :
|
||||
This class represents a quaternion. We use the notation :
|
||||
q = (x*i, y*j, z*k, w) to represent a quaternion.
|
||||
-------------------------------------------------------------------
|
||||
*/
|
||||
class Quaternion
|
||||
{
|
||||
private :
|
||||
double x; // Component x of the quaternion
|
||||
double y; // Component y of the quaternion
|
||||
double z; // Component z of the quaternion
|
||||
double w; // Component w of the quaternion
|
||||
|
||||
public :
|
||||
Quaternion(); // Constructor
|
||||
Quaternion(double x, double y, double z, double w); // Constructor with arguments
|
||||
Quaternion(double w, const Vector3D& v); // Constructor with the component w and the vector v=(x y z)
|
||||
Quaternion(const Quaternion& quaternion); // Copy-constructor
|
||||
~Quaternion(); // Destructor
|
||||
double getX() const; // Return the component x of the quaternion
|
||||
double getY() const; // Return the component y of the quaternion
|
||||
double getZ() const; // Return the component z of the quaternion
|
||||
double getW() const; // Return the component w of the quaternion
|
||||
void setX(double x); // Set the value x
|
||||
void setY(double y); // Set the value y
|
||||
void setZ(double z); // Set the value z
|
||||
void setW(double w); // Set the value w
|
||||
Vector3D vectorV() const; // Return the vector v=(x y z) of the quaternion
|
||||
double length() const; // Return the length of the quaternion
|
||||
Quaternion getUnit() const throw (MathematicsException); // Return the unit quaternion
|
||||
Quaternion getConjugate() const; // Return the conjugate quaternion
|
||||
Quaternion getInverse() const throw (MathematicsException); // Return the inverse of the quaternion
|
||||
|
||||
// --- Overloaded operators --- //
|
||||
Quaternion operator+(const Quaternion& quaternion) const; // Overloaded operator for the addition
|
||||
Quaternion operator-(const Quaternion& quaternion) const; // Overloaded operator for the substraction
|
||||
Quaternion operator*(double nb) const; // Overloaded operator for the multiplication with a constant
|
||||
Quaternion operator*(const Quaternion& quaternion) const; // Overloaded operator for the multiplication
|
||||
Quaternion& operator=(const Quaternion& quaternion); // Overloaded operator for assignment
|
||||
bool operator==(const Quaternion& quaternion) const; // Overloaded operator for equality condition
|
||||
};
|
||||
|
||||
|
||||
// Get the value x (inline)
|
||||
inline double Quaternion::getX() const {
|
||||
return x;
|
||||
}
|
||||
|
||||
// Get the value y (inline)
|
||||
inline double Quaternion::getY() const {
|
||||
return y;
|
||||
}
|
||||
|
||||
// Get the value z (inline)
|
||||
inline double Quaternion::getZ() const {
|
||||
return z;
|
||||
}
|
||||
|
||||
// Get the value w (inline)
|
||||
inline double Quaternion::getW() const {
|
||||
return w;
|
||||
}
|
||||
|
||||
// Set the value x (inline)
|
||||
inline void Quaternion::setX(double x) {
|
||||
this->x = x;
|
||||
}
|
||||
|
||||
// Set the value y (inline)
|
||||
inline void Quaternion::setY(double y) {
|
||||
this->y = y;
|
||||
}
|
||||
|
||||
// Set the value z (inline)
|
||||
inline void Quaternion::setZ(double z) {
|
||||
this->z = z;
|
||||
}
|
||||
|
||||
// Set the value w (inline)
|
||||
inline void Quaternion::setW(double w) {
|
||||
this->w = w;
|
||||
}
|
||||
|
||||
// Return the vector v=(x y z) of the quaternion
|
||||
inline Vector3D Quaternion::vectorV() const {
|
||||
// Return the vector v
|
||||
return Vector3D(x, y, z);
|
||||
}
|
||||
|
||||
// Return the length of the quaternion (inline)
|
||||
inline double Quaternion::length() const {
|
||||
return sqrt(x*x + y*y + z*z + w*w);
|
||||
}
|
||||
|
||||
// Return the unit quaternion
|
||||
inline Quaternion Quaternion::getUnit() const throw(MathematicsException) {
|
||||
double lengthQuaternion = length();
|
||||
|
||||
// Check if the length is not equal to zero
|
||||
if (lengthQuaternion != 0.0) {
|
||||
|
||||
// Compute and return the unit quaternion
|
||||
return Quaternion(x/lengthQuaternion, y/lengthQuaternion, z/lengthQuaternion, w/lengthQuaternion);
|
||||
}
|
||||
else {
|
||||
// Throw an exception because it's impossible to compute a unit quaternion if its length is equal to zero
|
||||
throw MathematicsException("MathematicsException : Impossible to compute the unit quaternion if the length of the quaternion is zero");
|
||||
}
|
||||
}
|
||||
|
||||
// Return the conjugate of the quaternion (inline)
|
||||
inline Quaternion Quaternion::getConjugate() const {
|
||||
return Quaternion(-x, -y, -z, w);
|
||||
}
|
||||
|
||||
// Return the inverse of the quaternion (inline)
|
||||
inline Quaternion Quaternion::getInverse() const throw(MathematicsException) {
|
||||
double lengthQuaternion = length();
|
||||
lengthQuaternion = lengthQuaternion * lengthQuaternion;
|
||||
|
||||
// Check if the length is not equal to zero
|
||||
if (lengthQuaternion != 0.0) {
|
||||
|
||||
// Compute and return the inverse quaternion
|
||||
return Quaternion(-x/lengthQuaternion, -y/lengthQuaternion, -z/lengthQuaternion, w/lengthQuaternion);
|
||||
}
|
||||
else {
|
||||
// Throw an exception because the inverse cannot be computed
|
||||
throw MathematicsException("MathematicsException : Impossible to compute the inverse of the quaternion because it's length is zero");
|
||||
}
|
||||
}
|
||||
|
||||
// Overloaded operator for the addition of two quaternions
|
||||
inline Quaternion Quaternion::operator+(const Quaternion& quaternion) const {
|
||||
// Return the result quaternion
|
||||
return Quaternion(x + quaternion.x, y + quaternion.y, z + quaternion.z, w + quaternion.w);
|
||||
}
|
||||
|
||||
// Overloaded operator for the substraction of two quaternions
|
||||
inline Quaternion Quaternion::operator-(const Quaternion& quaternion) const {
|
||||
// Return the result of the substraction
|
||||
return Quaternion(x-quaternion.x, y - quaternion.y, z - quaternion.z, w - quaternion.w);
|
||||
}
|
||||
|
||||
// Overloaded operator for the multiplication with a constant
|
||||
inline Quaternion Quaternion::operator*(double nb) const {
|
||||
// Return the result
|
||||
return Quaternion(nb*x, nb*y, nb*z, nb*w);
|
||||
}
|
||||
|
||||
// Overloaded operator for the multiplication of two quaternions
|
||||
inline Quaternion Quaternion::operator*(const Quaternion& quaternion) const {
|
||||
// Return the result of the multiplication
|
||||
return Quaternion(w*quaternion.w - vectorV().scalarProduct(quaternion.vectorV()), w*quaternion.vectorV()+quaternion.w*vectorV() + vectorV().crossProduct(quaternion.vectorV()));
|
||||
}
|
||||
|
||||
// Overloaded operator for the assignment
|
||||
inline Quaternion& Quaternion::operator=(const Quaternion& quaternion) {
|
||||
// Check for self-assignment
|
||||
if (this != &quaternion) {
|
||||
x = quaternion.x;
|
||||
y = quaternion.y;
|
||||
z = quaternion.z;
|
||||
w = quaternion.w;
|
||||
}
|
||||
|
||||
// Return this quaternion
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Overloaded operator for equality condition
|
||||
inline bool Quaternion::operator==(const Quaternion& quaternion) const {
|
||||
return (x == quaternion.x && y == quaternion.y && z == quaternion.z && w == quaternion.w);
|
||||
}
|
||||
|
||||
} // End of the ReactPhysics3D namespace
|
||||
|
||||
#endif
|
|
@ -1,223 +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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
// Libraries
|
||||
#include "Vector.h"
|
||||
|
||||
// Namespaces
|
||||
using namespace reactphysics3d;
|
||||
|
||||
// Constructor of the class Vector
|
||||
Vector::Vector(int n) throw(std::invalid_argument) {
|
||||
// Check the argument
|
||||
if (n > 0) {
|
||||
// Create the array that contains the values of the vector
|
||||
nbComponent = n;
|
||||
tab = new double[nbComponent];
|
||||
|
||||
|
||||
// Fill the array with zero's value
|
||||
for(int i=0; i<nbComponent; ++i) {
|
||||
tab[i] = 0.0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Throw an exception because of the wrong argument
|
||||
throw std::invalid_argument("Exception : The size of the vector has to be positive !");
|
||||
}
|
||||
}
|
||||
|
||||
// Copy-constructor of the class Vector
|
||||
Vector::Vector(const Vector& vector) {
|
||||
nbComponent = vector.nbComponent;
|
||||
tab = new double[nbComponent];
|
||||
|
||||
// Fill the array with the value of the vector
|
||||
for (int i=0; i<nbComponent; ++i) {
|
||||
tab[i] = vector.tab[i];
|
||||
}
|
||||
}
|
||||
|
||||
// Destructor of the class Vector
|
||||
Vector::~Vector() {
|
||||
// Erase the array with the values of the vector
|
||||
delete [] tab;
|
||||
}
|
||||
|
||||
// Return the corresponding unit vector
|
||||
Vector Vector::getUnit() const throw(MathematicsException) {
|
||||
double lengthVector = length();
|
||||
|
||||
// Check if the length of the vector is equal to zero
|
||||
if (lengthVector!= 0) {
|
||||
double lengthInv = 1.0 / lengthVector;
|
||||
Vector unitVector(nbComponent);
|
||||
|
||||
// Compute the unit vector
|
||||
for(int i=0; i<nbComponent; ++i) {
|
||||
unitVector.setValue(i, getValue(i) * lengthInv);
|
||||
}
|
||||
|
||||
// Return the unit vector
|
||||
return unitVector;
|
||||
|
||||
}
|
||||
else {
|
||||
// Throw an exception because the length of the vector is zero
|
||||
throw MathematicsException("MathematicsException : Impossible to compute the unit vector because the length of the vector is zero");
|
||||
}
|
||||
}
|
||||
|
||||
// Method to compute the scalar product of two vectors
|
||||
double Vector::scalarProduct(const Vector& vector) const throw(MathematicsException) {
|
||||
// Check the sizes of the two vectors
|
||||
if (nbComponent == vector.nbComponent) {
|
||||
double result = 0.0;
|
||||
|
||||
// Compute the scalar product
|
||||
for (int i=0; i<nbComponent; ++i) {
|
||||
result = result + vector.tab[i] * tab[i];
|
||||
}
|
||||
|
||||
// Return the result of the scalar product
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
// Throw an exception because the two vectors haven't the same size
|
||||
throw MathematicsException("MathematicsException : Impossible to compute the scalar product because the vectors haven't the same size");
|
||||
}
|
||||
}
|
||||
|
||||
// Method to compute the cross product of two vectors
|
||||
Vector Vector::crossProduct(const Vector& vector) const throw(MathematicsException) {
|
||||
// Check if the vectors have 3 components
|
||||
if (nbComponent == 3 && vector.nbComponent == 3) {
|
||||
Vector result(3);
|
||||
|
||||
// Compute the cross product
|
||||
result.tab[0] = tab[1] * vector.tab[2] - tab[2] * vector.tab[1];
|
||||
result.tab[1] = tab[2] * vector.tab[0] - tab[0] * vector.tab[2];
|
||||
result.tab[2] = tab[0] * vector.tab[1] - tab[1] * vector.tab[0];
|
||||
|
||||
// Return the result of the cross product
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
// Throw an exception because the vectors haven't three components
|
||||
throw MathematicsException("MathematicsException : Impossible to compute the cross product because the vectors haven't 3 components");
|
||||
}
|
||||
}
|
||||
|
||||
// Overloaded operator for addition
|
||||
Vector Vector::operator+(const Vector& vector) const throw(MathematicsException) {
|
||||
// Check the size of the two vectors
|
||||
if (nbComponent == vector.nbComponent) {
|
||||
Vector sum(nbComponent);
|
||||
|
||||
// Compute the sum of the two vectors
|
||||
for (int i=0; i<nbComponent; ++i) {
|
||||
sum.setValue(i, vector.tab[i] + tab[i]);
|
||||
}
|
||||
|
||||
// Return the sum vector
|
||||
return sum;
|
||||
}
|
||||
else {
|
||||
// Throw an exception because the sizes of the two vectors aren't the same
|
||||
throw MathematicsException("MathematicsException : Impossible two sum the two vectors because the sizes aren't the same !");
|
||||
}
|
||||
}
|
||||
|
||||
// Overloaded operator for substraction
|
||||
Vector Vector::operator-(const Vector& vector) const throw(MathematicsException) {
|
||||
// Check the size of the two vectors
|
||||
if (nbComponent == vector.nbComponent) {
|
||||
Vector substraction(nbComponent);
|
||||
|
||||
// Compute the subraction of the two vectors
|
||||
for (int i=0; i<nbComponent; ++i) {
|
||||
substraction.setValue(i, tab[i] - vector.tab[i]);
|
||||
}
|
||||
|
||||
// Return the subraction vector
|
||||
return substraction;
|
||||
}
|
||||
else {
|
||||
// Throw an exception because the sizes of the two vectors aren't the same
|
||||
throw MathematicsException("MathematicsException : Impossible two substract the two vectors because the sizes aren't the same !");
|
||||
}
|
||||
}
|
||||
|
||||
// Overloaded operator for multiplication with a number
|
||||
Vector Vector::operator*(double number) const {
|
||||
Vector result(nbComponent);
|
||||
|
||||
// Compute the multiplication
|
||||
for (int i=0; i<nbComponent; ++i) {
|
||||
result.setValue(i, number * tab[i]);
|
||||
}
|
||||
|
||||
// Return the result vector
|
||||
return result;
|
||||
}
|
||||
|
||||
// Overloaded operator for assigment to a Vector
|
||||
Vector& Vector::operator=(const Vector& vector) throw(MathematicsException) {
|
||||
|
||||
// Check for self-assignment
|
||||
if (this == &vector) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Check the size of the vectors
|
||||
if (nbComponent == vector.nbComponent) {
|
||||
// Check for self-assignment
|
||||
if (this != &vector) {
|
||||
for (int i=0; i<nbComponent; ++i) {
|
||||
tab[i] = vector.tab[i];
|
||||
}
|
||||
}
|
||||
|
||||
// Return a reference to the vector
|
||||
return *this;
|
||||
}
|
||||
else {
|
||||
// Throw an exception because the sizes of the vectors aren't the same
|
||||
throw MathematicsException("MathematicsException : The assigment to a Vector is impossible because the size of the vectors aren't the same");
|
||||
}
|
||||
}
|
||||
|
||||
// Overloaded operator for the equality condition
|
||||
bool Vector::operator==(const Vector& vector) const throw(MathematicsException) {
|
||||
// Check if the sizes of the vectors are compatible
|
||||
if (nbComponent == vector.nbComponent) {
|
||||
for (int i=0; i<nbComponent; ++i) {
|
||||
if (tab[i] != vector.tab[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
// Throw an exception because the sizes of the vectors aren't the same
|
||||
throw MathematicsException("MathematicsException : Impossible to check if the vectors are equal because they don't have the same size");
|
||||
}
|
||||
}
|
||||
|
|
@ -1,116 +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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef VECTOR_H
|
||||
#define VECTOR_H
|
||||
|
||||
// Libraries
|
||||
#include "exceptions.h"
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
// ReactPhysics3D namespace
|
||||
namespace reactphysics3d {
|
||||
|
||||
/* -------------------------------------------------------------------
|
||||
Class Vector :
|
||||
This class represents a Vector.
|
||||
-------------------------------------------------------------------
|
||||
*/
|
||||
class Vector {
|
||||
private :
|
||||
double* tab; // Array of the vector's components
|
||||
int nbComponent; // number of components in the vector
|
||||
|
||||
public :
|
||||
Vector(int n) throw(std::invalid_argument); // Constructor of the class Vector
|
||||
Vector(const Vector& vector); // Copy-constructor of the class Vector
|
||||
virtual ~Vector(); // Destructor of the class Vector
|
||||
double getValue(int n) const throw(std::invalid_argument); // Get a component of the vector
|
||||
void setValue(int n, double value) throw(std::invalid_argument); // Set the value of a component of the vector
|
||||
int getNbComponent() const; // Get the number of components in the vector
|
||||
double length() const; // Get the length of the vector
|
||||
Vector getUnit() const throw(MathematicsException); // Return the corresponding unit vector
|
||||
double scalarProduct(const Vector& vector) const throw(MathematicsException); // Scalar product of two vectors
|
||||
Vector crossProduct(const Vector& vector) const throw(MathematicsException); // Cross product of two vectors (in 3D only)
|
||||
|
||||
// --- Overloaded operators --- //
|
||||
Vector operator+(const Vector& vector) const throw(MathematicsException); // Overloaded operator for addition
|
||||
Vector operator-(const Vector& vector) const throw(MathematicsException); // Overloaded operator for substraction
|
||||
Vector operator*(double number) const; // Overloaded operator for multiplication with a number
|
||||
Vector& operator=(const Vector& vector) throw(MathematicsException); // Overloaded operator for the assignement to a Vector
|
||||
bool operator==(const Vector& vector) const throw(MathematicsException); // Overloaded operator for the equality condition
|
||||
};
|
||||
|
||||
|
||||
// ------ Definition of inlines functions ------ //
|
||||
|
||||
// Method to get the value of a component of the vector (inline)
|
||||
inline double Vector::getValue(int n) const throw(std::invalid_argument) {
|
||||
// Check the argument
|
||||
if (n>=0 && n<nbComponent) {
|
||||
// Return the value of the component
|
||||
return tab[n];
|
||||
}
|
||||
else {
|
||||
// Throw an exception because of the wrong argument
|
||||
throw std::invalid_argument("The argument is outside the bounds of the Vector");
|
||||
}
|
||||
}
|
||||
|
||||
// Method to set the value of a component of the vector
|
||||
inline void Vector::setValue(int n, double value) throw(std::invalid_argument) {
|
||||
// Check the argument
|
||||
if (n >= 0 && n<nbComponent) {
|
||||
// Set the value
|
||||
tab[n] = value;
|
||||
}
|
||||
else {
|
||||
// Throw an exception because of the wrong argument
|
||||
throw std::invalid_argument("Exception : The argument is outside the bounds of the Vector");
|
||||
}
|
||||
}
|
||||
|
||||
// Method to get the number of components in the vector (inline)
|
||||
inline int Vector::getNbComponent() const {
|
||||
// Return the number of components in the vector
|
||||
return nbComponent;
|
||||
}
|
||||
|
||||
// Method to get the length of the vector
|
||||
inline double Vector::length() const {
|
||||
// Compute the length of the vector
|
||||
double sum = 0.0;
|
||||
for(int i=0; i<nbComponent; ++i) {
|
||||
sum = sum + tab[i] * tab[i];
|
||||
}
|
||||
|
||||
// Return the length of the vector
|
||||
return sqrt(sum);
|
||||
}
|
||||
|
||||
// Overloaded operator for multiplication between a number and a Vector (inline)
|
||||
inline Vector operator*(double number, const Vector& vector) {
|
||||
// Compute and return the result
|
||||
return vector * number;
|
||||
}
|
||||
|
||||
} // End of the ReactPhysics3D namespace
|
||||
|
||||
#endif
|
|
@ -1,96 +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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
// Libraries
|
||||
#include "Vector3D.h"
|
||||
#include <iostream>
|
||||
|
||||
// Namespaces
|
||||
using namespace reactphysics3d;
|
||||
|
||||
// Constructor of the class Vector3D
|
||||
Vector3D::Vector3D()
|
||||
:x(0.0), y(0.0), z(0.0) {
|
||||
|
||||
}
|
||||
|
||||
// Constructor with arguments
|
||||
Vector3D::Vector3D(double x, double y, double z)
|
||||
:x(x), y(y), z(z) {
|
||||
|
||||
}
|
||||
|
||||
// Copy-constructor
|
||||
Vector3D::Vector3D(const Vector3D& vector)
|
||||
:x(vector.x), y(vector.y), z(vector.z) {
|
||||
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Vector3D::~Vector3D() {
|
||||
|
||||
}
|
||||
|
||||
// Return the corresponding unit vector
|
||||
Vector3D Vector3D::getUnit() const throw(MathematicsException) {
|
||||
double lengthVector = length();
|
||||
|
||||
// Check if the length is equal to zero
|
||||
if (lengthVector != 0) {
|
||||
// Compute and return the unit vector
|
||||
double lengthInv = 1.0 / lengthVector;
|
||||
return Vector3D(x * lengthInv, y * lengthInv, z*lengthInv);
|
||||
}
|
||||
else {
|
||||
// Throw an exception because the length of the vector is zero
|
||||
throw MathematicsException("MathematicsException : Impossible to compute the unit vector because the length of the vector is zero");
|
||||
}
|
||||
}
|
||||
|
||||
// Overloaded operator for addition
|
||||
Vector3D Vector3D::operator+(const Vector3D& vector) const {
|
||||
// Compute and return the sum of the two vectors
|
||||
return Vector3D(x + vector.x, y + vector.y, z + vector.z);
|
||||
}
|
||||
|
||||
// Overloaded operator for substraction
|
||||
Vector3D Vector3D::operator-(const Vector3D& vector) const {
|
||||
// Compute and return the substraction of the two vectors
|
||||
return Vector3D(x - vector.x, y - vector.y, z - vector.z);
|
||||
}
|
||||
|
||||
// Overloaded operator for multiplication with a number
|
||||
Vector3D Vector3D::operator*(double number) const {
|
||||
// Compute and return the result
|
||||
return Vector3D(x * number, y * number, z * number);
|
||||
}
|
||||
|
||||
// Overloaded operator for the assignement to a Vector
|
||||
Vector3D& Vector3D::operator=(const Vector3D& vector) {
|
||||
// Check for self-assignment
|
||||
if (this != &vector) {
|
||||
// Copy the vector
|
||||
x = vector.x;
|
||||
y = vector.y;
|
||||
z = vector.z;
|
||||
}
|
||||
|
||||
// Return a reference to the vector
|
||||
return *this;
|
||||
}
|
|
@ -1,136 +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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef VECTOR3D_H
|
||||
#define VECTOR3D_H
|
||||
|
||||
// Libraries
|
||||
#include <cmath>
|
||||
#include "exceptions.h"
|
||||
|
||||
// ReactPhysics3D namespace
|
||||
namespace reactphysics3d {
|
||||
|
||||
|
||||
/* -------------------------------------------------------------------
|
||||
Class Vector3D :
|
||||
This classrepresents 3 dimensionnal vector in space.
|
||||
-------------------------------------------------------------------
|
||||
*/
|
||||
class Vector3D {
|
||||
private :
|
||||
double x; // X component of the vector
|
||||
double y; // Y component of the vector
|
||||
double z; // Z component of the vector
|
||||
|
||||
public :
|
||||
Vector3D(); // Constructor of the class Vector3D
|
||||
Vector3D(double x, double y, double z); // Constructor with arguments
|
||||
Vector3D(const Vector3D& vector); // Copy-constructor
|
||||
virtual ~Vector3D(); // Destructor
|
||||
double getX() const; // Get the x component of the vector
|
||||
double getY() const; // Get the y component of the vector
|
||||
double getZ() const; // Get the z component of the vector
|
||||
void setX(double x); // Set the x component of the vector
|
||||
void setY(double y); // Set the y component of the vector
|
||||
void setZ(double z); // Set the z component of the vector
|
||||
void setAllValues(double x, double y, double z); // Set all the values of the vector
|
||||
double length() const; // Return the lenght of the vector
|
||||
Vector3D getUnit() const throw(MathematicsException); // Return the corresponding unit vector
|
||||
double scalarProduct(const Vector3D& vector) const; // Scalar product of two vectors
|
||||
Vector3D crossProduct(const Vector3D& vector) const; // Cross product of two vectors
|
||||
|
||||
// --- Overloaded operators --- //
|
||||
Vector3D operator+(const Vector3D& vector) const; // Overloaded operator for addition
|
||||
Vector3D operator-(const Vector3D& vector) const ; // Overloaded operator for substraction
|
||||
Vector3D operator*(double number) const; // Overloaded operator for multiplication with a number
|
||||
Vector3D& operator=(const Vector3D& vector); // Overloaded operator for the assignement to a Vector
|
||||
bool operator==(const Vector3D& vector) const; // Overloaded operator for the equality condition
|
||||
};
|
||||
|
||||
|
||||
// Get the x component of the vector (inline)
|
||||
inline double Vector3D::getX() const {
|
||||
return x;
|
||||
}
|
||||
|
||||
// Get the y component of the vector (inline)
|
||||
inline double Vector3D::getY() const {
|
||||
return y;
|
||||
}
|
||||
|
||||
// Get the z component of the vector (inline)
|
||||
inline double Vector3D::getZ() const {
|
||||
return z;
|
||||
}
|
||||
|
||||
// Set the x component of the vector (inline)
|
||||
inline void Vector3D::setX(double x) {
|
||||
this->x = x;
|
||||
}
|
||||
|
||||
// Set the y component of the vector (inline)
|
||||
inline void Vector3D::setY(double y) {
|
||||
this->y = y;
|
||||
}
|
||||
|
||||
// Set the z component of the vector (inline)
|
||||
inline void Vector3D::setZ(double z) {
|
||||
this->z = z;
|
||||
}
|
||||
|
||||
// Set all the values of the vector (inline)
|
||||
inline void Vector3D::setAllValues(double x, double y, double z) {
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
this->z = z;
|
||||
}
|
||||
|
||||
// Return the length of the vector (inline)
|
||||
inline double Vector3D::length() const {
|
||||
// Compute and return the length of the vector
|
||||
return sqrt(x*x + y*y + z*z);
|
||||
}
|
||||
|
||||
// Scalar product of two vectors (inline)
|
||||
inline double Vector3D::scalarProduct(const Vector3D& vector) const {
|
||||
// Compute and return the result of the scalar product
|
||||
return (x * vector.x + y * vector.y + z * vector.z);
|
||||
}
|
||||
|
||||
// Cross product of two vectors (inline)
|
||||
inline Vector3D Vector3D::crossProduct(const Vector3D& vector) const {
|
||||
// Compute and return the cross product
|
||||
return Vector3D(y * vector.z - z * vector.y, z * vector.x - x * vector.z , x * vector.y - y * vector.x);
|
||||
}
|
||||
|
||||
// Overloaded operator for multiplication between a number and a Vector3D (inline)
|
||||
inline Vector3D operator * (double number, const Vector3D& vector) {
|
||||
// Compute and return the result vector
|
||||
return vector * number;
|
||||
}
|
||||
|
||||
// Overloaded operator for the equality condition
|
||||
inline bool Vector3D::operator == (const Vector3D& vector) const {
|
||||
return (x == vector.x && y == vector.y && z == vector.z);
|
||||
}
|
||||
|
||||
} // End of the ReactPhysics3D namespace
|
||||
|
||||
#endif
|
|
@ -1,58 +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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
// Libraries
|
||||
#include "exceptions.h"
|
||||
|
||||
// Namespaces
|
||||
using namespace reactphysics3d;
|
||||
|
||||
|
||||
// Constructor of the MathematicsException class
|
||||
MathematicsException::MathematicsException(const std::string& msg)
|
||||
:std::runtime_error(msg) {
|
||||
|
||||
}
|
||||
|
||||
// Destructor of the MathException class
|
||||
MathematicsException::~MathematicsException() throw() {
|
||||
|
||||
}
|
||||
|
||||
// Overriden exception base class method
|
||||
const char* MathematicsException::what() const throw() {
|
||||
return std::runtime_error::what();
|
||||
}
|
||||
|
||||
// Constructor of the DivisionByZeroException class
|
||||
DivisionByZeroException::DivisionByZeroException(const std::string& msg)
|
||||
:MathematicsException(msg) {
|
||||
|
||||
}
|
||||
|
||||
// Destructor of the DivisionByZeroException class
|
||||
DivisionByZeroException::~DivisionByZeroException() throw() {
|
||||
|
||||
}
|
||||
|
||||
// Overriden exception base class method
|
||||
const char* DivisionByZeroException::what() const throw() {
|
||||
return MathematicsException::what();
|
||||
}
|
||||
|
|
@ -1,52 +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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef EXCEPTIONS_H
|
||||
#define EXCEPTIONS_H
|
||||
|
||||
// Libraries
|
||||
#include <stdexcept>
|
||||
|
||||
// ReactPhysics3D namespace
|
||||
namespace reactphysics3d {
|
||||
|
||||
/* -------------------------------------------------------------------
|
||||
Exception class for the mathematics library
|
||||
-------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
// Class MathematicsException
|
||||
class MathematicsException : public std::runtime_error {
|
||||
public:
|
||||
MathematicsException(const std::string& msg="MathException"); // Constructor
|
||||
virtual ~MathematicsException() throw(); // Destructor
|
||||
virtual const char* what() const throw(); // Overriding the base exception method
|
||||
};
|
||||
|
||||
// Class DivisionByZeroException
|
||||
class DivisionByZeroException : public MathematicsException {
|
||||
public:
|
||||
DivisionByZeroException(const std::string& msg="DivisionByZeroException : Division by zero !"); // Constructor
|
||||
virtual ~DivisionByZeroException() throw(); // Destructor
|
||||
virtual const char* what() const throw(); // Overriding the base exception method
|
||||
};
|
||||
|
||||
} // End of the ReactPhysics3D namespace
|
||||
|
||||
#endif
|
|
@ -1,34 +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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
// TODO : Mathematics library : We have to use assert to debug
|
||||
// TODO : Mathematics library : Check everywhere that in member methods we use attributes access instead of getter and setter.
|
||||
|
||||
#ifndef DCMATHS_H
|
||||
#define DCMATHS_H
|
||||
|
||||
// Libraries
|
||||
#include "Matrix.h"
|
||||
#include "Matrix3x3.h"
|
||||
#include "Quaternion.h"
|
||||
#include "Vector.h"
|
||||
#include "Vector3D.h"
|
||||
#include "exceptions.h"
|
||||
|
||||
#endif
|
|
@ -1,31 +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 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 General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef REACTPHYSICS3D_H
|
||||
#define REACTPHYSICS3D_H
|
||||
|
||||
// ReactPhysics3D namespace
|
||||
//namespace reactphysics3d {
|
||||
|
||||
// Libraries
|
||||
#include "mathematics/mathematics.h"
|
||||
|
||||
//} // End of the reactphysics3d namespace
|
||||
|
||||
#endif
|
|
@ -1,52 +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 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 *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
// Libraries
|
||||
#include <iostream>
|
||||
#include "TestSuite/Suite.h"
|
||||
#include "mathematics/VectorTest.h"
|
||||
#include "mathematics/Vector3DTest.h"
|
||||
#include "mathematics/MatrixTest.h"
|
||||
#include "mathematics/Matrix3x3Test.h"
|
||||
#include "mathematics/QuaternionTest.h"
|
||||
|
||||
// Namespaces
|
||||
using namespace std;
|
||||
using namespace TestSuite;
|
||||
|
||||
// Main function
|
||||
int main() {
|
||||
|
||||
// ReactPhysics3D TestSuite
|
||||
Suite reactphysics3DTestSuite("ReactPhysics3D TestSuite");
|
||||
|
||||
// Mathematics Tests
|
||||
reactphysics3DTestSuite.addTest(new VectorTest);
|
||||
reactphysics3DTestSuite.addTest(new Vector3DTest);
|
||||
reactphysics3DTestSuite.addTest(new MatrixTest);
|
||||
reactphysics3DTestSuite.addTest(new Matrix3x3Test);
|
||||
reactphysics3DTestSuite.addTest(new QuaternionTest);
|
||||
|
||||
// Run the ReactPhysics3D TestSuite and display the report
|
||||
reactphysics3DTestSuite.run();
|
||||
long nbFailures = reactphysics3DTestSuite.report();
|
||||
reactphysics3DTestSuite.free();
|
||||
return nbFailures;
|
||||
double inPause;
|
||||
cin >> inPause;
|
||||
}
|
Loading…
Reference in New Issue
Block a user