git-svn-id: https://reactphysics3d.googlecode.com/svn/trunk@15 92aac97c-a6ce-11dd-a772-7fcde58d38e6
This commit is contained in:
chappuis.daniel 2009-02-02 13:53:56 +00:00
parent 7c1e0d64b5
commit e70ef78a8e
14 changed files with 206 additions and 224 deletions

View File

@ -1,5 +1,5 @@
/**************************************************************************** /****************************************************************************
* Copyright (C) 2008 Daniel Chappuis * * Copyright (C) 2009 Daniel Chappuis *
**************************************************************************** ****************************************************************************
* This file is part of ReactPhysics3D. * * This file is part of ReactPhysics3D. *
* * * *
@ -20,6 +20,9 @@
// Libraries // Libraries
#include "Matrix.h" #include "Matrix.h"
// Namespaces
using namespace reactphysics3d;
// Constructor of the class Matrix // Constructor of the class Matrix
Matrix::Matrix(int nbRow, int nbColumn) throw(std::invalid_argument) Matrix::Matrix(int nbRow, int nbColumn) throw(std::invalid_argument)
:nbRow(nbRow),nbColumn(nbColumn) { :nbRow(nbRow),nbColumn(nbColumn) {
@ -49,7 +52,7 @@ Matrix::Matrix(int nbRow, int nbColumn) throw(std::invalid_argument)
Matrix::Matrix(const Matrix& matrix) Matrix::Matrix(const Matrix& matrix)
:nbRow(matrix.nbRow), nbColumn(matrix.nbColumn) { :nbRow(matrix.nbRow), nbColumn(matrix.nbColumn) {
// Create the two dimensional dynamic array // Create the two dimensioCompiling: ../sources/reactphysics3d/mathematics/Matrix3x3.cpp
array = new double*[nbRow]; array = new double*[nbRow];
for(int i=0; i<nbRow; ++i) { for(int i=0; i<nbRow; ++i) {
array[i] = new double[nbColumn]; array[i] = new double[nbColumn];
@ -126,7 +129,7 @@ Matrix Matrix::getTranspose() const {
// Function that return the inverse of the matrix if there exists // Function that return the inverse of the matrix if there exists
Matrix Matrix::getInverse() const throw(MatrixException) { Matrix Matrix::getInverse() const throw(MathematicsException) {
// Check if the matrix is a square-matrix // Check if the matrix is a square-matrix
if (nbRow==nbColumn) { if (nbRow==nbColumn) {
// Compute the determinant of the matrix // Compute the determinant of the matrix
@ -160,18 +163,18 @@ Matrix Matrix::getInverse() const throw(MatrixException) {
return inverseMatrix; return inverseMatrix;
} }
else { else {
// We throw an Matrix Exception // We throw a MathematicsException
throw MatrixException("Exception : Inverse of the matrix can't be computed because the determinant is zero !"); throw MathematicsException("MathematicsException : Inverse of the matrix can't be computed because the determinant is zero !");
} }
} }
else { else {
// We throw an Matrix Exception // We throw an Matrix Exception
throw MatrixException("Exception : Inverse can't be computed for a non-square matrix !"); throw MathematicsException("MathematicsException : Inverse can't be computed for a non-square matrix !");
} }
} }
// Function that return the determinant of the matrix // Function that return the determinant of the matrix
double Matrix::getDeterminant() const throw(MatrixException) { double Matrix::getDeterminant() const throw(MathematicsException) {
// If the matrix is a square matrix // If the matrix is a square matrix
if (nbRow == nbColumn) { if (nbRow == nbColumn) {
if(nbRow == 1) { if(nbRow == 1) {
@ -201,13 +204,12 @@ double Matrix::getDeterminant() const throw(MatrixException) {
} }
} }
else { else {
// Throw a Matrix Multiplication Exception // Throw a MathematicsException
throw MatrixException("Exception : The determinant of a non-square matrix isn't computable !"); throw MathematicsException("MathematicsException : The determinant of a non-square matrix isn't computable !");
} }
} }
// Return the trace of the matrix // Return the trace of the matrix
// TODO Matrix::getTrace() : Test this method
double Matrix::getTrace() const { double Matrix::getTrace() const {
double sum = 0.0; double sum = 0.0;
@ -246,13 +248,13 @@ Matrix Matrix::identityMatrix(int dimension) throw(std::invalid_argument) {
} }
else { else {
// Throw an exception // Throw an exception
throw std::invalid_argument("Exception : The argument of identityMatrix has to be positive !"); throw std::invalid_argument("Exception : The argument of identityMatrix() has to be positive !");
} }
} }
// Definition of the operator + for the sum of two matrices with references // Definition of the operator + for the sum of two matrices with references
Matrix Matrix::operator + (const Matrix& matrix2) const throw(MatrixException) { Matrix Matrix::operator+(const Matrix& matrix2) const throw(MathematicsException) {
if (nbRow == matrix2.nbRow && nbColumn == matrix2.nbColumn) { if (nbRow == matrix2.nbRow && nbColumn == matrix2.nbColumn) {
// Create a new matrix // Create a new matrix
Matrix sumMatrix(nbRow,nbColumn); Matrix sumMatrix(nbRow,nbColumn);
@ -268,13 +270,13 @@ Matrix Matrix::operator + (const Matrix& matrix2) const throw(MatrixException) {
return sumMatrix; return sumMatrix;
} }
else { else {
// We throw an Matrix Exception // We throw an MathematicsException
throw MatrixException("Exception : Addition of the matrices isn't possible beacause the size of the matrices aren't the same"); 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 // Definition of the operator - for the substraction of two matrices with references
Matrix Matrix::operator - (const Matrix& matrix2) const throw(MatrixException) { Matrix Matrix::operator-(const Matrix& matrix2) const throw(MathematicsException) {
if (nbRow == matrix2.nbRow && nbColumn == matrix2.nbColumn) { if (nbRow == matrix2.nbRow && nbColumn == matrix2.nbColumn) {
// Create a new matrix // Create a new matrix
Matrix sumMatrix(nbRow, nbColumn); Matrix sumMatrix(nbRow, nbColumn);
@ -290,8 +292,8 @@ Matrix Matrix::operator - (const Matrix& matrix2) const throw(MatrixException) {
return sumMatrix; return sumMatrix;
} }
else { else {
// We throw an Matrix Exception // We throw a MathematicsException
throw MatrixException("Exception : Substraction of the matrices isn't possible beacause the size of the matrices aren't the same"); throw MathematicsException("MathematicsException : Substraction of the matrices isn't possible beacause the size of the matrices aren't the same");
} }
} }
@ -312,7 +314,7 @@ Matrix Matrix::operator * (double nb) const {
} }
// Overloaded operator for multiplication with a matrix // Overloaded operator for multiplication with a matrix
Matrix Matrix::operator * (const Matrix& matrix2) const throw(MatrixException) { Matrix Matrix::operator*(const Matrix& matrix2) const throw(MathematicsException) {
// Check the sizes of the matrices // Check the sizes of the matrices
if (nbColumn == matrix2.nbRow) { if (nbColumn == matrix2.nbRow) {
// Compute the result of the multiplication // Compute the result of the multiplication
@ -334,12 +336,18 @@ Matrix Matrix::operator * (const Matrix& matrix2) const throw(MatrixException) {
} }
else { else {
// Throw an exception because the multiplication is impossible // Throw an exception because the multiplication is impossible
throw MatrixException("Exception : The sizes of the matrices aren't compatible for the multiplication"); throw MathematicsException("MathematicsException : The sizes of the matrices aren't compatible for the multiplication");
} }
} }
// Overloaded operator = for the assignment // Overloaded operator = for the assignment
Matrix& Matrix::operator = (const Matrix& matrix2) throw(MatrixException) { Matrix& Matrix::operator=(const Matrix& matrix2) throw(MathematicsException) {
// Check for self-assignement
if(this == &matrix2) {
return *this;
}
// Check the size of the matrix // Check the size of the matrix
if (nbRow==matrix2.nbRow && nbColumn==matrix2.nbColumn) { if (nbRow==matrix2.nbRow && nbColumn==matrix2.nbColumn) {
// Check for self-assignment // Check for self-assignment
@ -355,13 +363,13 @@ Matrix& Matrix::operator = (const Matrix& matrix2) throw(MatrixException) {
return *this; return *this;
} }
else { else {
// Throw a Matrix Exception // Throw a MathematicsException
throw MatrixException("Exception : Assignment impossible because the size of the matrices aren't the same !"); throw MathematicsException("MathematicsException : Assignment impossible because the size of the matrices aren't the same !");
} }
} }
// Overloaded operator for equality condition // Overloaded operator for equality condition
bool Matrix::operator == (const Matrix& matrix2) const throw(MatrixException) { bool Matrix::operator==(const Matrix& matrix2) const throw(MathematicsException) {
// Check if the matrices dimensions are compatible // Check if the matrices dimensions are compatible
if (nbRow == matrix2.nbRow && nbColumn == matrix2.nbColumn) { if (nbRow == matrix2.nbRow && nbColumn == matrix2.nbColumn) {
for (int i=0; i<nbRow; ++i) { for (int i=0; i<nbRow; ++i) {
@ -376,7 +384,7 @@ bool Matrix::operator == (const Matrix& matrix2) const throw(MatrixException) {
} }
else { else {
// Throw an exception because the matrices dimensions aren't the same // Throw an exception because the matrices dimensions aren't the same
throw MatrixException("Exception : Impossible to check if the matrices are equal because they don't have the same dimension"); throw MathematicsException("MathematicsException : Impossible to check if the matrices are equal because they don't have the same dimension");
} }
} }
@ -391,4 +399,3 @@ void Matrix::display() const {
std::cout << std::endl; std::cout << std::endl;
} }
} }

View File

@ -1,5 +1,5 @@
/**************************************************************************** /****************************************************************************
* Copyright (C) 2008 Daniel Chappuis * * Copyright (C) 2009 Daniel Chappuis *
**************************************************************************** ****************************************************************************
* This file is part of ReactPhysics3D. * * This file is part of ReactPhysics3D. *
* * * *
@ -26,6 +26,9 @@
#include <stdexcept> #include <stdexcept>
#include <iostream> #include <iostream>
// ReactPhysics3D namespace
namespace reactphysics3d {
/* ------------------------------------------------------------------- /* -------------------------------------------------------------------
Class Matrix : Class Matrix :
This class represents a matrix. This class represents a matrix.
@ -47,20 +50,20 @@ class Matrix {
int getNbColumn() const; // Return the number of column 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 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 getTranspose() const; // Return the transposed matrixs
Matrix getInverse() const throw(MatrixException); // Return the inverse of the matrix if there exists Matrix getInverse() const throw(MathematicsException); // Return the inverse of the matrix if there exists
double getDeterminant() const throw(MatrixException); // Return the determinant of the matrix double getDeterminant() const throw(MathematicsException); // Return the determinant of the matrix
double getTrace() const; // Return the trace of the matrix double getTrace() const; // Return the trace of the matrix
static Matrix identityMatrix(int dimension) throw(std::invalid_argument); // Return the identity matrix I of the given dimension static Matrix identityMatrix(int dimension) throw(std::invalid_argument); // Return the identity matrix I of the given dimension
void display() const; // TO DELETE void display() const; // TO DELETE
// --- Overloaded operators --- // // --- Overloaded operators --- //
Matrix operator + (const Matrix& matrix2) const throw(MatrixException); // Overloaded operator for addition Matrix operator+(const Matrix& matrix2) const throw(MathematicsException); // Overloaded operator for addition
Matrix operator - (const Matrix& matrix2) const throw(MatrixException); // Overloaded operator for substraction 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*(double nb) const; // Overloaded operator for multiplication with a number
Matrix operator * (const Matrix& matrix2) const throw(MatrixException); // Overloaded operator for multiplication with a matrix Matrix operator*(const Matrix& matrix2) const throw(MathematicsException); // Overloaded operator for multiplication with a matrix
Matrix& operator = (const Matrix& matrix2) throw(MatrixException); // Overloaded operator for assignment Matrix& operator=(const Matrix& matrix2) throw(MathematicsException); // Overloaded operator for assignment
bool operator == (const Matrix& matrix2) const throw(MatrixException); // Overloaded operator for equality condition bool operator==(const Matrix& matrix2) const throw(MathematicsException); // Overloaded operator for equality condition
}; };
// Function to get a value in the matrix (inline) // Function to get a value in the matrix (inline)
@ -104,4 +107,6 @@ inline Matrix operator * (double number, const Matrix& matrix) {
return matrix * number; return matrix * number;
} }
} // End of the ReactPhysics3D namespace
#endif #endif

View File

@ -1,5 +1,5 @@
/**************************************************************************** /****************************************************************************
* Copyright (C) 2008 Daniel Chappuis * * Copyright (C) 2009 Daniel Chappuis *
**************************************************************************** ****************************************************************************
* This file is part of ReactPhysics3D. * * This file is part of ReactPhysics3D. *
* * * *
@ -19,11 +19,10 @@
// Libraries // Libraries
#include <iostream> #include <iostream>
#include "Matrix3x3.h" #include "Matrix3x3.h"
// Namespaces
using namespace reactphysics3d;
// Constructor of the class Matrix3x3 // Constructor of the class Matrix3x3
Matrix3x3::Matrix3x3() { Matrix3x3::Matrix3x3() {
@ -70,7 +69,7 @@ Matrix3x3::~Matrix3x3() {
} }
// Return the inverse matrix // Return the inverse matrix
Matrix3x3 Matrix3x3::getInverse() const throw(MatrixException) { Matrix3x3 Matrix3x3::getInverse() const throw(MathematicsException) {
// Compute the determinant of the matrix // Compute the determinant of the matrix
double determinant = getDeterminant(); double determinant = getDeterminant();
@ -89,12 +88,13 @@ Matrix3x3 Matrix3x3::getInverse() const throw(MatrixException) {
} }
else { else {
// Throw an exception because the inverse of the matrix doesn't exist if the determinant is equal to zero // Throw an exception because the inverse of the matrix doesn't exist if the determinant is equal to zero
throw MatrixException("Exception : Impossible to compute the inverse of the matrix because 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) // Return the quaternion corresponding to the matrix (it returns a unit quaternion)
Quaternion Matrix3x3::getQuaternion() const { Quaternion Matrix3x3::getQuaternion() const {
// Get the trace of the matrix // Get the trace of the matrix
double trace = getTrace(); double trace = getTrace();
@ -102,30 +102,33 @@ Quaternion Matrix3x3::getQuaternion() const {
double s; double s;
if (trace < 0.0) { if (trace < 0.0) {
// TODO Matrix3x3::getQuaternion() : End the implementation of this ...
if (array[1][1] > array[0][0]) { if (array[1][1] > array[0][0]) {
if(array[2][2] > array[1][1]) { if(array[2][2] > array[1][1]) {
r = sqrt(array[2][2] - array[0][0] - array[1][1] + 1.0); r = sqrt(array[2][2] - array[0][0] - array[1][1] + 1.0);
s = 0.5 / r; s = 0.5 / r;
return Quaternion((array[0][1] + array[1][0])*s, , (array[1][2] + array[2][1])*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 { else {
r = sqrt(array[1][1] - array[2][2] - array[0][0] + 1.0); r = sqrt(array[1][1] - array[2][2] - array[0][0] + 1.0);
s = 0.5 / r; 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); 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]) { 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 { 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 { else {
r = sqrt(trace + 1.0); r = sqrt(trace + 1.0);
s = 0.5/r; 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 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);
} }
} }

View File

@ -1,5 +1,5 @@
/**************************************************************************** /****************************************************************************
* Copyright (C) 2008 Daniel Chappuis * * Copyright (C) 2009 Daniel Chappuis *
**************************************************************************** ****************************************************************************
* This file is part of ReactPhysics3D. * * This file is part of ReactPhysics3D. *
* * * *
@ -25,6 +25,9 @@
#include "exceptions.h" #include "exceptions.h"
#include "Quaternion.h" #include "Quaternion.h"
// ReactPhysics3D namespace
namespace reactphysics3d {
/* ------------------------------------------------------------------- /* -------------------------------------------------------------------
Class Matrix3x3 : Class Matrix3x3 :
@ -49,7 +52,7 @@ class Matrix3x3 {
Matrix3x3 getTranspose() const; // Return the transpose matrix Matrix3x3 getTranspose() const; // Return the transpose matrix
double getDeterminant() const; // Return the determinant of the matrix double getDeterminant() const; // Return the determinant of the matrix
double getTrace() const; // Return the trace of the matrix double getTrace() const; // Return the trace of the matrix
Matrix3x3 getInverse() const throw(MatrixException); // Return the inverse matrix Matrix3x3 getInverse() const throw(MathematicsException); // Return the inverse matrix
Quaternion getQuaternion() const; // Return the quaternion corresponding to the matrix (it returns a unit quaternion) Quaternion getQuaternion() const; // Return the quaternion corresponding to the matrix (it returns a unit quaternion)
static Matrix3x3 identityMatrix(); // Return the 3x3 identity matrix static Matrix3x3 identityMatrix(); // Return the 3x3 identity matrix
@ -74,7 +77,7 @@ inline double Matrix3x3::getValue(int i, int j) const throw(std::invalid_argumen
} }
else { else {
// Throw an exception because of the wrong argument // Throw an exception because of the wrong argument
throw MatrixException("Exception : The argument isn't in the bounds of the 3x3 matrix"); throw std::invalid_argument("Exception : The argument isn't in the bounds of the 3x3 matrix");
} }
} }
@ -87,7 +90,7 @@ inline void Matrix3x3::setValue(int i, int j, double value) throw(std::invalid_a
} }
else { else {
// Throw an exception because of the wrong argument // Throw an exception because of the wrong argument
throw MatrixException("Exception : The argument isn't in the bounds of the 3x3 matrix"); throw std::invalid_argument("Exception : The argument isn't in the bounds of the 3x3 matrix");
} }
} // End of the dcmaths namespace } // End of the dcmaths namespace
@ -124,7 +127,6 @@ inline double Matrix3x3::getDeterminant() const {
} }
// Return the trace of the matrix // Return the trace of the matrix
// TODO Matrix3x3::getTrace() : Test this method
inline double Matrix3x3::getTrace() const { inline double Matrix3x3::getTrace() const {
// Compute and return the trace // Compute and return the trace
return (array[0][0] + array[1][1] + array[2][2]); return (array[0][0] + array[1][1] + array[2][2]);
@ -143,4 +145,6 @@ inline bool Matrix3x3::operator == (const Matrix3x3& matrix2) const {
array[2][0] == matrix2.array[2][0] && array[2][1] == matrix2.array[2][1] && array[2][2] == matrix2.array[2][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 #endif

View File

@ -1,5 +1,5 @@
/**************************************************************************** /****************************************************************************
* Copyright (C) 2008 Daniel Chappuis * * Copyright (C) 2009 Daniel Chappuis *
**************************************************************************** ****************************************************************************
* This file is part of ReactPhysics3D. * * This file is part of ReactPhysics3D. *
* * * *
@ -20,6 +20,9 @@
// Libraries // Libraries
#include "Quaternion.h" #include "Quaternion.h"
// Namespaces
using namespace reactphysics3d;
// Constructor of the class // Constructor of the class
Quaternion::Quaternion() Quaternion::Quaternion()
:x(0.0), y(0.0), z(0.0), w(0.0) { :x(0.0), y(0.0), z(0.0), w(0.0) {

View File

@ -1,5 +1,5 @@
/**************************************************************************** /****************************************************************************
* Copyright (C) 2008 Daniel Chappuis * * Copyright (C) 2009 Daniel Chappuis *
**************************************************************************** ****************************************************************************
* This file is part of ReactPhysics3D. * * This file is part of ReactPhysics3D. *
* * * *
@ -17,19 +17,16 @@
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. * * along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/ ***************************************************************************/
#ifndef QUATERNION_H #ifndef QUATERNION_H
#define QUATERNION_H #define QUATERNION_H
// Libraries // Libraries
#include <cmath> #include <cmath>
#include "Vector3D.h" #include "Vector3D.h"
#include "exceptions.h" #include "exceptions.h"
// TODO : Test the Quaternion implementation // ReactPhysics3D namespace
// TODO : Test == operator of all classes namespace reactphysics3d {
// TODO : In every operator= overloading, we don't check for self-assignment, it's an error
/* ------------------------------------------------------------------- /* -------------------------------------------------------------------
Class Quaternion : Class Quaternion :
@ -60,9 +57,9 @@ class Quaternion
void setW(double w); // Set the value w void setW(double w); // Set the value w
Vector3D vectorV() const; // Return the vector v=(x y z) of the quaternion Vector3D vectorV() const; // Return the vector v=(x y z) of the quaternion
double length() const; // Return the length of the quaternion double length() const; // Return the length of the quaternion
Quaternion getUnit() const throw (QuaternionException); // Return the unit quaternion Quaternion getUnit() const throw (MathematicsException); // Return the unit quaternion
Quaternion getConjugate() const; // Return the conjugate quaternion Quaternion getConjugate() const; // Return the conjugate quaternion
Quaternion getInverse() const throw (QuaternionException); // Return the inverse of the quaternion Quaternion getInverse() const throw (MathematicsException); // Return the inverse of the quaternion
// --- Overloaded operators --- // // --- Overloaded operators --- //
Quaternion operator+(const Quaternion& quaternion) const; // Overloaded operator for the addition Quaternion operator+(const Quaternion& quaternion) const; // Overloaded operator for the addition
@ -125,7 +122,7 @@ inline double Quaternion::length() const {
} }
// Return the unit quaternion // Return the unit quaternion
inline Quaternion Quaternion::getUnit() const throw(QuaternionException) { inline Quaternion Quaternion::getUnit() const throw(MathematicsException) {
double lengthQuaternion = length(); double lengthQuaternion = length();
// Check if the length is not equal to zero // Check if the length is not equal to zero
@ -135,7 +132,7 @@ inline Quaternion Quaternion::getUnit() const throw(QuaternionException) {
} }
else { else {
// Throw an exception because it's impossible to compute a unit quaternion if its length is equal to zero // Throw an exception because it's impossible to compute a unit quaternion if its length is equal to zero
throw QuaternionException("Exception : Impossible to compute the unit quaternion if the length of the quaternion is zero"); throw MathematicsException("MathematicsException : Impossible to compute the unit quaternion if the length of the quaternion is zero");
} }
} }
@ -145,7 +142,7 @@ inline Quaternion Quaternion::getConjugate() const {
} }
// Return the inverse of the quaternion (inline) // Return the inverse of the quaternion (inline)
inline Quaternion Quaternion::getInverse() const throw(QuaternionException) { inline Quaternion Quaternion::getInverse() const throw(MathematicsException) {
double lengthQuaternion = length(); double lengthQuaternion = length();
lengthQuaternion = lengthQuaternion * lengthQuaternion; lengthQuaternion = lengthQuaternion * lengthQuaternion;
@ -156,7 +153,7 @@ inline Quaternion Quaternion::getInverse() const throw(QuaternionException) {
} }
else { else {
// Throw an exception because the inverse cannot be computed // Throw an exception because the inverse cannot be computed
throw QuaternionException("Exception : Impossible to compute the inverse of the quaternion because it's length is zero"); throw MathematicsException("MathematicsException : Impossible to compute the inverse of the quaternion because it's length is zero");
} }
} }
@ -197,4 +194,6 @@ inline bool Quaternion::operator == (const Quaternion& quaternion) const {
return (x == quaternion.x && y == quaternion.y && z == quaternion.z && w == quaternion.w); return (x == quaternion.x && y == quaternion.y && z == quaternion.z && w == quaternion.w);
} }
} // End of the ReactPhysics3D namespace
#endif #endif

View File

@ -1,5 +1,5 @@
/**************************************************************************** /****************************************************************************
* Copyright (C) 2008 Daniel Chappuis * * Copyright (C) 2009 Daniel Chappuis *
**************************************************************************** ****************************************************************************
* This file is part of ReactPhysics3D. * * This file is part of ReactPhysics3D. *
* * * *
@ -20,6 +20,9 @@
// Libraries // Libraries
#include "Vector.h" #include "Vector.h"
// Namespaces
using namespace reactphysics3d;
// Constructor of the class Vector // Constructor of the class Vector
Vector::Vector(int n) throw(std::invalid_argument) { Vector::Vector(int n) throw(std::invalid_argument) {
// Check the argument // Check the argument
@ -58,7 +61,7 @@ Vector::~Vector() {
} }
// Return the corresponding unit vector // Return the corresponding unit vector
Vector Vector::getUnit() const throw(VectorException) { Vector Vector::getUnit() const throw(MathematicsException) {
double lengthVector = length(); double lengthVector = length();
// Check if the length of the vector is equal to zero // Check if the length of the vector is equal to zero
@ -77,12 +80,12 @@ Vector Vector::getUnit() const throw(VectorException) {
} }
else { else {
// Throw an exception because the length of the vector is zero // Throw an exception because the length of the vector is zero
throw VectorException("Exception : Impossible to compute the unit vector 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 // Method to compute the scalar product of two vectors
double Vector::scalarProduct(const Vector& vector) const throw(VectorException) { double Vector::scalarProduct(const Vector& vector) const throw(MathematicsException) {
// Check the sizes of the two vectors // Check the sizes of the two vectors
if (nbComponent == vector.nbComponent) { if (nbComponent == vector.nbComponent) {
double result = 0.0; double result = 0.0;
@ -97,12 +100,12 @@ double Vector::scalarProduct(const Vector& vector) const throw(VectorException)
} }
else { else {
// Throw an exception because the two vectors haven't the same size // Throw an exception because the two vectors haven't the same size
throw VectorException("Exception : Impossible to compute the scalar product because the 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 // Method to compute the cross product of two vectors
Vector Vector::crossProduct(const Vector& vector) const throw(VectorException) { Vector Vector::crossProduct(const Vector& vector) const throw(MathematicsException) {
// Check if the vectors have 3 components // Check if the vectors have 3 components
if (nbComponent == 3 && vector.nbComponent == 3) { if (nbComponent == 3 && vector.nbComponent == 3) {
Vector result(3); Vector result(3);
@ -117,7 +120,7 @@ Vector Vector::crossProduct(const Vector& vector) const throw(VectorException) {
} }
else { else {
// Throw an exception because the vectors haven't three components // Throw an exception because the vectors haven't three components
throw VectorException("Exception : Impossible to compute the cross product because the vectors haven't 3 components"); throw MathematicsException("MathematicsException : Impossible to compute the cross product because the vectors haven't 3 components");
} }
} }
@ -129,7 +132,7 @@ void Vector::display() const {
} }
// Overloaded operator for addition // Overloaded operator for addition
Vector Vector::operator + (const Vector& vector) const throw(VectorException) { Vector Vector::operator+(const Vector& vector) const throw(MathematicsException) {
// Check the size of the two vectors // Check the size of the two vectors
if (nbComponent == vector.nbComponent) { if (nbComponent == vector.nbComponent) {
Vector sum(nbComponent); Vector sum(nbComponent);
@ -144,12 +147,12 @@ Vector Vector::operator + (const Vector& vector) const throw(VectorException) {
} }
else { else {
// Throw an exception because the sizes of the two vectors aren't the same // Throw an exception because the sizes of the two vectors aren't the same
throw VectorException("Exception : Impossible two sum the two vectors because the sizes aren't the same !"); throw MathematicsException("MathematicsException : Impossible two sum the two vectors because the sizes aren't the same !");
} }
} }
// Overloaded operator for substraction // Overloaded operator for substraction
Vector Vector::operator - (const Vector& vector) const throw(VectorException) { Vector Vector::operator-(const Vector& vector) const throw(MathematicsException) {
// Check the size of the two vectors // Check the size of the two vectors
if (nbComponent == vector.nbComponent) { if (nbComponent == vector.nbComponent) {
Vector substraction(nbComponent); Vector substraction(nbComponent);
@ -164,7 +167,7 @@ Vector Vector::operator - (const Vector& vector) const throw(VectorException) {
} }
else { else {
// Throw an exception because the sizes of the two vectors aren't the same // Throw an exception because the sizes of the two vectors aren't the same
throw VectorException("Exception : Impossible two substract the two vectors because the sizes aren't the same !"); throw MathematicsException("MathematicsException : Impossible two substract the two vectors because the sizes aren't the same !");
} }
} }
@ -182,7 +185,13 @@ Vector Vector::operator * (double number) const {
} }
// Overloaded operator for assigment to a Vector // Overloaded operator for assigment to a Vector
Vector& Vector::operator = (const Vector& vector) throw(VectorException) { Vector& Vector::operator=(const Vector& vector) throw(MathematicsException) {
// Check for self-assignment
if (this == &vector) {
return *this;
}
// Check the size of the vectors // Check the size of the vectors
if (nbComponent == vector.nbComponent) { if (nbComponent == vector.nbComponent) {
// Check for self-assignment // Check for self-assignment
@ -197,12 +206,12 @@ Vector& Vector::operator = (const Vector& vector) throw(VectorException) {
} }
else { else {
// Throw an exception because the sizes of the vectors aren't the same // Throw an exception because the sizes of the vectors aren't the same
throw VectorException("Exception : The assigment to a Vector is impossible because the size 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 // Overloaded operator for the equality condition
bool Vector::operator == (const Vector& vector) const throw(VectorException) { bool Vector::operator==(const Vector& vector) const throw(MathematicsException) {
// Check if the sizes of the vectors are compatible // Check if the sizes of the vectors are compatible
if (nbComponent == vector.nbComponent) { if (nbComponent == vector.nbComponent) {
for (int i=0; i<nbComponent; ++i) { for (int i=0; i<nbComponent; ++i) {
@ -215,7 +224,7 @@ bool Vector::operator == (const Vector& vector) const throw(VectorException) {
} }
else { else {
// Throw an exception because the sizes of the vectors aren't the same // Throw an exception because the sizes of the vectors aren't the same
throw VectorException("Exception : Impossible to check if the vectors are equal because they don't have the same size"); throw MathematicsException("MathematicsException : Impossible to check if the vectors are equal because they don't have the same size");
} }
} }

View File

@ -1,5 +1,5 @@
/**************************************************************************** /****************************************************************************
* Copyright (C) 2008 Daniel Chappuis * * Copyright (C) 2009 Daniel Chappuis *
**************************************************************************** ****************************************************************************
* This file is part of ReactPhysics3D. * * This file is part of ReactPhysics3D. *
* * * *
@ -17,16 +17,17 @@
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. * * along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/ ***************************************************************************/
#ifndef VECTOR_H #ifndef VECTOR_H
#define VECTOR_H #define VECTOR_H
// Libraries // Libraries
#include "exceptions.h" #include "exceptions.h"
#include <cmath> #include <cmath>
#include <iostream> #include <iostream>
// ReactPhysics3D namespace
namespace reactphysics3d {
/* ------------------------------------------------------------------- /* -------------------------------------------------------------------
Class Vector : Class Vector :
This class represents a Vector. This class represents a Vector.
@ -45,18 +46,18 @@ class Vector {
void setValue(int n, double value) throw(std::invalid_argument); // Set the value of 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 int getNbComponent() const; // Get the number of components in the vector
double length() const; // Get the length of the vector double length() const; // Get the length of the vector
Vector getUnit() const throw(VectorException); // Return the corresponding unit vector Vector getUnit() const throw(MathematicsException); // Return the corresponding unit vector
double scalarProduct(const Vector& vector) const throw(VectorException); // Scalar product of two vectors double scalarProduct(const Vector& vector) const throw(MathematicsException); // Scalar product of two vectors
Vector crossProduct(const Vector& vector) const throw(VectorException); // Cross product of two vectors (in 3D only) Vector crossProduct(const Vector& vector) const throw(MathematicsException); // Cross product of two vectors (in 3D only)
void display() const; // TO DELETE void display() const; // TO DELETE
// --- Overloaded operators --- // // --- Overloaded operators --- //
Vector operator + (const Vector& vector) const throw(VectorException); // Overloaded operator for addition Vector operator+(const Vector& vector) const throw(MathematicsException); // Overloaded operator for addition
Vector operator - (const Vector& vector) const throw(VectorException); // Overloaded operator for substraction 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*(double number) const; // Overloaded operator for multiplication with a number
Vector& operator = (const Vector& vector) throw(VectorException); // Overloaded operator for the assignement to a Vector Vector& operator=(const Vector& vector) throw(MathematicsException); // Overloaded operator for the assignement to a Vector
bool operator == (const Vector& vector) const throw(VectorException); // Overloaded operator for the equality condition bool operator==(const Vector& vector) const throw(MathematicsException); // Overloaded operator for the equality condition
}; };
@ -112,5 +113,6 @@ inline Vector operator * (double number, const Vector& vector) {
return vector * number; return vector * number;
} }
} // End of the ReactPhysics3D namespace
#endif #endif

View File

@ -1,5 +1,5 @@
/**************************************************************************** /****************************************************************************
* Copyright (C) 2008 Daniel Chappuis * * Copyright (C) 2009 Daniel Chappuis *
**************************************************************************** ****************************************************************************
* This file is part of ReactPhysics3D. * * This file is part of ReactPhysics3D. *
* * * *
@ -19,9 +19,10 @@
// Libraries // Libraries
#include "Vector3D.h" #include "Vector3D.h"
#include <iostream> #include <iostream>
// Namespaces
using namespace reactphysics3d;
// Constructor of the class Vector3D // Constructor of the class Vector3D
Vector3D::Vector3D() Vector3D::Vector3D()
@ -47,7 +48,7 @@ Vector3D::~Vector3D() {
} }
// Return the corresponding unit vector // Return the corresponding unit vector
Vector3D Vector3D::getUnit() const throw(VectorException) { Vector3D Vector3D::getUnit() const throw(MathematicsException) {
double lengthVector = length(); double lengthVector = length();
// Check if the length is equal to zero // Check if the length is equal to zero
@ -58,7 +59,7 @@ Vector3D Vector3D::getUnit() const throw(VectorException) {
} }
else { else {
// Throw an exception because the length of the vector is zero // Throw an exception because the length of the vector is zero
throw VectorException("Exception : Impossible to compute the unit vector 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");
} }
} }

View File

@ -1,5 +1,5 @@
/**************************************************************************** /****************************************************************************
* Copyright (C) 2008 Daniel Chappuis * * Copyright (C) 2009 Daniel Chappuis *
**************************************************************************** ****************************************************************************
* This file is part of ReactPhysics3D. * * This file is part of ReactPhysics3D. *
* * * *
@ -17,15 +17,16 @@
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. * * along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/ ***************************************************************************/
#ifndef VECTOR3D_H #ifndef VECTOR3D_H
#define VECTOR3D_H #define VECTOR3D_H
// Libraries // Libraries
#include <cmath> #include <cmath>
#include "exceptions.h" #include "exceptions.h"
// ReactPhysics3D namespace
namespace reactphysics3d {
/* ------------------------------------------------------------------- /* -------------------------------------------------------------------
Class Vector3D : Class Vector3D :
@ -51,7 +52,7 @@ class Vector3D {
void setZ(double z); // Set the z 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 void setAllValues(double x, double y, double z); // Set all the values of the vector
double length() const; // Return the lenght of the vector double length() const; // Return the lenght of the vector
Vector3D getUnit() const throw(VectorException); // Return the corresponding unit vector Vector3D getUnit() const throw(MathematicsException); // Return the corresponding unit vector
double scalarProduct(const Vector3D& vector) const; // Scalar product of two vectors double scalarProduct(const Vector3D& vector) const; // Scalar product of two vectors
Vector3D crossProduct(const Vector3D& vector) const; // Cross product of two vectors Vector3D crossProduct(const Vector3D& vector) const; // Cross product of two vectors
@ -132,4 +133,6 @@ inline bool Vector3D::operator == (const Vector3D& vector) const {
return (x == vector.x && y == vector.y && z == vector.z); return (x == vector.x && y == vector.y && z == vector.z);
} }
} // End of the ReactPhysics3D namespace
#endif #endif

View File

@ -1,5 +1,5 @@
/**************************************************************************** /****************************************************************************
* Copyright (C) 2008 Daniel Chappuis * * Copyright (C) 2009 Daniel Chappuis *
**************************************************************************** ****************************************************************************
* This file is part of ReactPhysics3D. * * This file is part of ReactPhysics3D. *
* * * *
@ -20,26 +20,29 @@
// Libraries // Libraries
#include "exceptions.h" #include "exceptions.h"
// Namespaces
using namespace reactphysics3d;
// Constructor of the MathException class
MathException::MathException(const std::string& msg) // Constructor of the MathematicsException class
MathematicsException::MathematicsException(const std::string& msg)
:std::runtime_error(msg) { :std::runtime_error(msg) {
} }
// Destructor of the MathException class // Destructor of the MathException class
MathException::~MathException() throw() { MathematicsException::~MathematicsException() throw() {
} }
// Overriden exception base class method // Overriden exception base class method
const char* MathException::what() const throw() { const char* MathematicsException::what() const throw() {
return std::runtime_error::what(); return std::runtime_error::what();
} }
// Constructor of the DivisionByZeroException class // Constructor of the DivisionByZeroException class
DivisionByZeroException::DivisionByZeroException(const std::string& msg) DivisionByZeroException::DivisionByZeroException(const std::string& msg)
:MathException(msg) { :MathematicsException(msg) {
} }
@ -50,38 +53,6 @@ DivisionByZeroException::~DivisionByZeroException() throw() {
// Overriden exception base class method // Overriden exception base class method
const char* DivisionByZeroException::what() const throw() { const char* DivisionByZeroException::what() const throw() {
return MathException::what(); return MathematicsException::what();
}
// Construtor of the MatrixException class
MatrixException::MatrixException(const std::string& msg)
:MathException(msg) {
}
// Destructor of the MatrixException class
MatrixException::~MatrixException() throw() {
}
// Overriden exception base class method
const char* MatrixException::what() const throw() {
return MathException::what();
}
// Constructor of the VectorException class
VectorException::VectorException(const std::string& msg)
:std::runtime_error(msg) {
}
// Destructor of the VectorException class
VectorException::~VectorException() throw() {
}
// Overidden exception base class method
const char* VectorException::what() const throw() {
return std::runtime_error::what();
} }

View File

@ -1,5 +1,5 @@
/**************************************************************************** /****************************************************************************
* Copyright (C) 2008 Daniel Chappuis * * Copyright (C) 2009 Daniel Chappuis *
**************************************************************************** ****************************************************************************
* This file is part of ReactPhysics3D. * * This file is part of ReactPhysics3D. *
* * * *
@ -17,59 +17,36 @@
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. * * along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/ ***************************************************************************/
// TODO exceptions.h : Check if all expressions are usefull and are correct (in Matrix3x3.h methods throw std::invalid_argument MatrixException doesn't inherit from
// std::invalid_argument
#ifndef EXCEPTIONS_H #ifndef EXCEPTIONS_H
#define EXCEPTIONS_H #define EXCEPTIONS_H
// Libraries // Libraries
#include <stdexcept> #include <stdexcept>
// ReactPhysics3D namespace
namespace reactphysics3d {
/* ------------------------------------------------------------------- /* -------------------------------------------------------------------
Exception class for the mathematics library Exception class for the mathematics library
------------------------------------------------------------------- -------------------------------------------------------------------
*/ */
// MathException // Class MathematicsException
class MathException : public std::runtime_error { class MathematicsException : public std::runtime_error {
public: public:
MathException(const std::string& msg="MathException"); // Constructor MathematicsException(const std::string& msg="MathException"); // Constructor
virtual ~MathException() throw(); // Destructor virtual ~MathematicsException() throw(); // Destructor
virtual const char* what() const throw(); // Overriding the base exception method virtual const char* what() const throw(); // Overriding the base exception method
}; };
// Class DivisionByZeroException
// DivisionByZeroException class DivisionByZeroException : public MathematicsException {
class DivisionByZeroException : public MathException {
public: public:
DivisionByZeroException(const std::string& msg="DivisionByZeroException : Division by zero !"); // Constructor DivisionByZeroException(const std::string& msg="DivisionByZeroException : Division by zero !"); // Constructor
virtual ~DivisionByZeroException() throw(); // Destructor virtual ~DivisionByZeroException() throw(); // Destructor
virtual const char* what() const throw(); // Overriding the base exception method virtual const char* what() const throw(); // Overriding the base exception method
}; };
// Matrix Exception class } // End of the ReactPhysics3D namespace
class MatrixException : public MathException {
public:
MatrixException(const std::string& msg="MatrixException"); // Constructor
virtual ~MatrixException() throw(); // Destructor
virtual const char* what() const throw(); // Overriden exception base class method
};
// VectorException class
class VectorException : public std::runtime_error {
public :
VectorException(const std::string& msg="VectorException"); // Constructor of the VectorException class
virtual ~VectorException() throw(); // Destructor of the VectorException class
virtual const char* what() const throw(); // Overriding the base exception method
};
// QuaternionException class
class QuaternionException : public std::runtime_error {
public :
QuaternionException(const std::string& msg="QuaternionException"); // Constructor of the QuaternionException class
virtual ~QuaternionException() throw(); // Destructor of the QuaternionException class
virtual const char* what() const throw(); // Overriding the base exception method
};
#endif #endif

View File

@ -1,5 +1,5 @@
#include "dcmaths.h" #include "mathematics.h"
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>

View File

@ -1,5 +1,5 @@
/**************************************************************************** /****************************************************************************
* Copyright (C) 2008 Daniel Chappuis * * Copyright (C) 2009 Daniel Chappuis *
**************************************************************************** ****************************************************************************
* This file is part of ReactPhysics3D. * * This file is part of ReactPhysics3D. *
* * * *
@ -20,8 +20,6 @@
// TODO : Mathematics library : We have to use assert to debug // 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. // TODO : Mathematics library : Check everywhere that in member methods we use attributes access instead of getter and setter.
// Mathematics library used in the react project
#ifndef DCMATHS_H #ifndef DCMATHS_H
#define DCMATHS_H #define DCMATHS_H