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. *
* *
@ -20,6 +20,9 @@
// Libraries
#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) {
@ -49,7 +52,7 @@ Matrix::Matrix(int nbRow, int nbColumn) throw(std::invalid_argument)
Matrix::Matrix(const Matrix& matrix)
: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];
for(int i=0; i<nbRow; ++i) {
array[i] = new double[nbColumn];
@ -126,7 +129,7 @@ Matrix Matrix::getTranspose() const {
// 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
if (nbRow==nbColumn) {
// Compute the determinant of the matrix
@ -160,18 +163,18 @@ Matrix Matrix::getInverse() const throw(MatrixException) {
return inverseMatrix;
}
else {
// We throw an Matrix Exception
throw MatrixException("Exception : Inverse of the matrix can't be computed because the determinant is zero !");
// 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 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
double Matrix::getDeterminant() const throw(MatrixException) {
double Matrix::getDeterminant() const throw(MathematicsException) {
// If the matrix is a square matrix
if (nbRow == nbColumn) {
if(nbRow == 1) {
@ -201,13 +204,12 @@ double Matrix::getDeterminant() const throw(MatrixException) {
}
}
else {
// Throw a Matrix Multiplication Exception
throw MatrixException("Exception : The determinant of a non-square matrix isn't computable !");
// Throw a MathematicsException
throw MathematicsException("MathematicsException : The determinant of a non-square matrix isn't computable !");
}
}
// Return the trace of the matrix
// TODO Matrix::getTrace() : Test this method
double Matrix::getTrace() const {
double sum = 0.0;
@ -246,13 +248,13 @@ Matrix Matrix::identityMatrix(int dimension) throw(std::invalid_argument) {
}
else {
// 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
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) {
// Create a new matrix
Matrix sumMatrix(nbRow,nbColumn);
@ -268,13 +270,13 @@ Matrix Matrix::operator + (const Matrix& matrix2) const throw(MatrixException) {
return sumMatrix;
}
else {
// We throw an Matrix Exception
throw MatrixException("Exception : Addition of the matrices isn't possible beacause the size of the matrices aren't the same");
// 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(MatrixException) {
Matrix Matrix::operator-(const Matrix& matrix2) const throw(MathematicsException) {
if (nbRow == matrix2.nbRow && nbColumn == matrix2.nbColumn) {
// Create a new matrix
Matrix sumMatrix(nbRow, nbColumn);
@ -290,13 +292,13 @@ Matrix Matrix::operator - (const Matrix& matrix2) const throw(MatrixException) {
return sumMatrix;
}
else {
// We throw an Matrix Exception
throw MatrixException("Exception : Substraction of the matrices isn't possible beacause the size of the matrices aren't the same");
// 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 {
Matrix Matrix::operator*(double nb) const {
// Creation of the result matrix
Matrix result(nbRow,nbColumn);
@ -312,7 +314,7 @@ Matrix Matrix::operator * (double nb) const {
}
// 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
if (nbColumn == matrix2.nbRow) {
// Compute the result of the multiplication
@ -334,12 +336,18 @@ Matrix Matrix::operator * (const Matrix& matrix2) const throw(MatrixException) {
}
else {
// 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
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
if (nbRow==matrix2.nbRow && nbColumn==matrix2.nbColumn) {
// Check for self-assignment
@ -355,13 +363,13 @@ Matrix& Matrix::operator = (const Matrix& matrix2) throw(MatrixException) {
return *this;
}
else {
// Throw a Matrix Exception
throw MatrixException("Exception : Assignment impossible because the size of the matrices aren't the same !");
// 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(MatrixException) {
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) {
@ -376,7 +384,7 @@ bool Matrix::operator == (const Matrix& matrix2) const throw(MatrixException) {
}
else {
// 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;
}
}

View File

@ -1,5 +1,5 @@
/****************************************************************************
* Copyright (C) 2008 Daniel Chappuis *
* Copyright (C) 2009 Daniel Chappuis *
****************************************************************************
* This file is part of ReactPhysics3D. *
* *
@ -26,6 +26,9 @@
#include <stdexcept>
#include <iostream>
// ReactPhysics3D namespace
namespace reactphysics3d {
/* -------------------------------------------------------------------
Class Matrix :
This class represents a matrix.
@ -43,24 +46,24 @@ 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
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(MatrixException); // Return the inverse of the matrix if there exists
double getDeterminant() const throw(MatrixException); // Return the determinant of the matrix
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; // Return the trace of the matrix
static Matrix identityMatrix(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(MatrixException); // Overloaded operator for addition
Matrix operator - (const Matrix& matrix2) const throw(MatrixException); // Overloaded operator for substraction
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) throw(MatrixException); // Overloaded operator for assignment
bool operator == (const Matrix& matrix2) const throw(MatrixException); // Overloaded operator for equality condition
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)
@ -98,10 +101,12 @@ inline int Matrix::getNbColumn() const {
}
// Overloaded operator for multiplication between a number and a Matrix (inline)
inline Matrix operator * (double number, const Matrix& matrix) {
inline Matrix operator*(double number, const Matrix& matrix) {
// Return the result matrix
return matrix * number;
}
} // End of the ReactPhysics3D namespace
#endif

View File

@ -1,5 +1,5 @@
/****************************************************************************
* Copyright (C) 2008 Daniel Chappuis *
* Copyright (C) 2009 Daniel Chappuis *
****************************************************************************
* This file is part of ReactPhysics3D. *
* *
@ -19,11 +19,10 @@
// Libraries
#include <iostream>
#include "Matrix3x3.h"
// Namespaces
using namespace reactphysics3d;
// Constructor of the class Matrix3x3
Matrix3x3::Matrix3x3() {
@ -70,7 +69,7 @@ Matrix3x3::~Matrix3x3() {
}
// Return the inverse matrix
Matrix3x3 Matrix3x3::getInverse() const throw(MatrixException) {
Matrix3x3 Matrix3x3::getInverse() const throw(MathematicsException) {
// Compute the determinant of the matrix
double determinant = getDeterminant();
@ -89,12 +88,13 @@ Matrix3x3 Matrix3x3::getInverse() const throw(MatrixException) {
}
else {
// 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)
Quaternion Matrix3x3::getQuaternion() const {
// Get the trace of the matrix
double trace = getTrace();
@ -102,30 +102,33 @@ Quaternion Matrix3x3::getQuaternion() const {
double s;
if (trace < 0.0) {
// TODO Matrix3x3::getQuaternion() : End the implementation of this ...
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[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 {
r = sqrt(array[1][1] - array[2][2] - array[0][0] + 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);
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 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);
}
}
@ -147,7 +150,7 @@ void Matrix3x3::display() const {
}
// Overloaded operator for addition
Matrix3x3 Matrix3x3::operator + (const Matrix3x3& matrix2) const {
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],
@ -155,7 +158,7 @@ Matrix3x3 Matrix3x3::operator + (const Matrix3x3& matrix2) const {
}
// Overloaded operator for substraction
Matrix3x3 Matrix3x3::operator - (const Matrix3x3& matrix2) const {
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],
@ -163,7 +166,7 @@ Matrix3x3 Matrix3x3::operator - (const Matrix3x3& matrix2) const {
}
// Overloaded operator for multiplication with a number
Matrix3x3 Matrix3x3::operator * (double nb) const {
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,
@ -171,7 +174,7 @@ Matrix3x3 Matrix3x3::operator * (double nb) const {
}
// Overloaded operator for multiplication with a matrix
Matrix3x3 Matrix3x3::operator * (const Matrix3x3& matrix2) const {
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],
@ -185,7 +188,7 @@ Matrix3x3 Matrix3x3::operator * (const Matrix3x3& matrix2) const {
}
// Overloaded operator for assignment
Matrix3x3& Matrix3x3::operator = (const Matrix3x3& matrix2) {
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],

View File

@ -1,5 +1,5 @@
/****************************************************************************
* Copyright (C) 2008 Daniel Chappuis *
* Copyright (C) 2009 Daniel Chappuis *
****************************************************************************
* This file is part of ReactPhysics3D. *
* *
@ -25,6 +25,9 @@
#include "exceptions.h"
#include "Quaternion.h"
// ReactPhysics3D namespace
namespace reactphysics3d {
/* -------------------------------------------------------------------
Class Matrix3x3 :
@ -33,7 +36,7 @@
*/
class Matrix3x3 {
private :
double array[3][3]; // Array with the values of the matrix
double array[3][3]; // Array with the values of the matrix
public :
Matrix3x3(); // Constructor of the class Matrix3x3
@ -49,19 +52,19 @@ class Matrix3x3 {
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(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)
static Matrix3x3 identityMatrix(); // 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
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
};
@ -74,7 +77,7 @@ inline double Matrix3x3::getValue(int i, int j) const throw(std::invalid_argumen
}
else {
// 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 {
// 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
@ -124,23 +127,24 @@ inline double Matrix3x3::getDeterminant() const {
}
// Return the trace of the matrix
// TODO Matrix3x3::getTrace() : Test this method
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) {
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 {
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

View File

@ -1,5 +1,5 @@
/****************************************************************************
* Copyright (C) 2008 Daniel Chappuis *
* Copyright (C) 2009 Daniel Chappuis *
****************************************************************************
* This file is part of ReactPhysics3D. *
* *
@ -20,6 +20,9 @@
// 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) {

View File

@ -1,5 +1,5 @@
/****************************************************************************
* Copyright (C) 2008 Daniel Chappuis *
* Copyright (C) 2009 Daniel Chappuis *
****************************************************************************
* This file is part of ReactPhysics3D. *
* *
@ -17,19 +17,16 @@
* 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"
// TODO : Test the Quaternion implementation
// TODO : Test == operator of all classes
// TODO : In every operator= overloading, we don't check for self-assignment, it's an error
// ReactPhysics3D namespace
namespace reactphysics3d {
/* -------------------------------------------------------------------
Class Quaternion :
@ -60,16 +57,16 @@ class Quaternion
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 (QuaternionException); // Return the unit quaternion
Quaternion getUnit() const throw (MathematicsException); // Return the unit 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 --- //
Quaternion operator + (const Quaternion& quaternion) const; // Overloaded operator for the addition
Quaternion operator - (const Quaternion& quaternion) const; // Overloaded operator for the substraction
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
Quaternion operator+(const Quaternion& quaternion) const; // Overloaded operator for the addition
Quaternion operator-(const Quaternion& quaternion) const; // Overloaded operator for the substraction
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
};
@ -125,7 +122,7 @@ inline double Quaternion::length() const {
}
// Return the unit quaternion
inline Quaternion Quaternion::getUnit() const throw(QuaternionException) {
inline Quaternion Quaternion::getUnit() const throw(MathematicsException) {
double lengthQuaternion = length();
// Check if the length is not equal to zero
@ -135,7 +132,7 @@ inline Quaternion Quaternion::getUnit() const throw(QuaternionException) {
}
else {
// 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)
inline Quaternion Quaternion::getInverse() const throw(QuaternionException) {
inline Quaternion Quaternion::getInverse() const throw(MathematicsException) {
double lengthQuaternion = length();
lengthQuaternion = lengthQuaternion * lengthQuaternion;
@ -156,30 +153,30 @@ inline Quaternion Quaternion::getInverse() const throw(QuaternionException) {
}
else {
// 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");
}
}
// Overloaded operator for the addition of two quaternions
inline Quaternion Quaternion::operator + (const Quaternion& quaternion) const {
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 {
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 of two quaternions
inline Quaternion Quaternion::operator * (const Quaternion& quaternion) const {
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) {
inline Quaternion& Quaternion::operator=(const Quaternion& quaternion) {
// Check for self-assignment
if (this != &quaternion) {
x = quaternion.x;
@ -193,8 +190,10 @@ inline Quaternion& Quaternion::operator = (const Quaternion& quaternion) {
}
// Overloaded operator for equality condition
inline bool Quaternion::operator == (const Quaternion& quaternion) const {
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

View File

@ -1,5 +1,5 @@
/****************************************************************************
* Copyright (C) 2008 Daniel Chappuis *
* Copyright (C) 2009 Daniel Chappuis *
****************************************************************************
* This file is part of ReactPhysics3D. *
* *
@ -20,6 +20,9 @@
// Libraries
#include "Vector.h"
// Namespaces
using namespace reactphysics3d;
// Constructor of the class Vector
Vector::Vector(int n) throw(std::invalid_argument) {
// Check the argument
@ -58,7 +61,7 @@ Vector::~Vector() {
}
// Return the corresponding unit vector
Vector Vector::getUnit() const throw(VectorException) {
Vector Vector::getUnit() const throw(MathematicsException) {
double lengthVector = length();
// Check if the length of the vector is equal to zero
@ -77,12 +80,12 @@ Vector Vector::getUnit() const throw(VectorException) {
}
else {
// 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
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
if (nbComponent == vector.nbComponent) {
double result = 0.0;
@ -97,12 +100,12 @@ double Vector::scalarProduct(const Vector& vector) const throw(VectorException)
}
else {
// 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
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
if (nbComponent == 3 && vector.nbComponent == 3) {
Vector result(3);
@ -117,7 +120,7 @@ Vector Vector::crossProduct(const Vector& vector) const throw(VectorException) {
}
else {
// 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
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
if (nbComponent == vector.nbComponent) {
Vector sum(nbComponent);
@ -144,12 +147,12 @@ Vector Vector::operator + (const Vector& vector) const throw(VectorException) {
}
else {
// 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
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
if (nbComponent == vector.nbComponent) {
Vector substraction(nbComponent);
@ -164,12 +167,12 @@ Vector Vector::operator - (const Vector& vector) const throw(VectorException) {
}
else {
// 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 !");
}
}
// Overloaded operator for multiplication with a number
Vector Vector::operator * (double number) const {
Vector Vector::operator*(double number) const {
Vector result(nbComponent);
// Compute the multiplication
@ -182,7 +185,13 @@ Vector Vector::operator * (double number) const {
}
// 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
if (nbComponent == vector.nbComponent) {
// Check for self-assignment
@ -197,12 +206,12 @@ Vector& Vector::operator = (const Vector& vector) throw(VectorException) {
}
else {
// 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
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
if (nbComponent == vector.nbComponent) {
for (int i=0; i<nbComponent; ++i) {
@ -215,7 +224,7 @@ bool Vector::operator == (const Vector& vector) const throw(VectorException) {
}
else {
// 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. *
* *
@ -17,16 +17,17 @@
* 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.
@ -34,29 +35,29 @@
*/
class Vector {
private :
double* tab; // Array of the vector's components
int nbComponent; // number of components in the vector
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(VectorException); // Return the corresponding unit vector
double scalarProduct(const Vector& vector) const throw(VectorException); // Scalar product of two vectors
Vector crossProduct(const Vector& vector) const throw(VectorException); // Cross product of two vectors (in 3D only)
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)
void display() const; // TO DELETE
void display() const; // TO DELETE
// --- Overloaded operators --- //
Vector operator + (const Vector& vector) const throw(VectorException); // Overloaded operator for addition
Vector operator - (const Vector& vector) const throw(VectorException); // Overloaded operator for substraction
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
bool operator == (const Vector& vector) const throw(VectorException); // Overloaded operator for the equality condition
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
};
@ -107,10 +108,11 @@ inline double Vector::length() const {
}
// Overloaded operator for multiplication between a number and a Vector (inline)
inline Vector operator * (double number, const Vector& vector) {
inline Vector operator*(double number, const Vector& vector) {
// Compute and return the result
return vector * number;
}
} // End of the ReactPhysics3D namespace
#endif

View File

@ -1,5 +1,5 @@
/****************************************************************************
* Copyright (C) 2008 Daniel Chappuis *
* Copyright (C) 2009 Daniel Chappuis *
****************************************************************************
* This file is part of ReactPhysics3D. *
* *
@ -19,9 +19,10 @@
// Libraries
#include "Vector3D.h"
#include <iostream>
// Namespaces
using namespace reactphysics3d;
// Constructor of the class Vector3D
Vector3D::Vector3D()
@ -47,7 +48,7 @@ Vector3D::~Vector3D() {
}
// Return the corresponding unit vector
Vector3D Vector3D::getUnit() const throw(VectorException) {
Vector3D Vector3D::getUnit() const throw(MathematicsException) {
double lengthVector = length();
// Check if the length is equal to zero
@ -58,7 +59,7 @@ Vector3D Vector3D::getUnit() const throw(VectorException) {
}
else {
// 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");
}
}
@ -68,25 +69,25 @@ void Vector3D::display() const {
}
// Overloaded operator for addition
Vector3D Vector3D::operator + (const Vector3D& vector) const {
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 {
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 {
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) {
Vector3D& Vector3D::operator=(const Vector3D& vector) {
// Check for self-assignment
if (this != &vector) {
// Copy the vector

View File

@ -1,5 +1,5 @@
/****************************************************************************
* Copyright (C) 2008 Daniel Chappuis *
* Copyright (C) 2009 Daniel Chappuis *
****************************************************************************
* This file is part of ReactPhysics3D. *
* *
@ -17,15 +17,16 @@
* 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 :
@ -51,18 +52,18 @@ class Vector3D {
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(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
Vector3D crossProduct(const Vector3D& vector) const; // Cross product of two vectors
void display() const; // TO DELETE
// --- 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
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
};
@ -132,4 +133,6 @@ inline bool Vector3D::operator == (const Vector3D& vector) const {
return (x == vector.x && y == vector.y && z == vector.z);
}
} // End of the ReactPhysics3D namespace
#endif

View File

@ -1,5 +1,5 @@
/****************************************************************************
* Copyright (C) 2008 Daniel Chappuis *
* Copyright (C) 2009 Daniel Chappuis *
****************************************************************************
* This file is part of ReactPhysics3D. *
* *
@ -20,26 +20,29 @@
// Libraries
#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) {
}
// Destructor of the MathException class
MathException::~MathException() throw() {
MathematicsException::~MathematicsException() throw() {
}
// Overriden exception base class method
const char* MathException::what() const throw() {
const char* MathematicsException::what() const throw() {
return std::runtime_error::what();
}
// Constructor of the DivisionByZeroException class
DivisionByZeroException::DivisionByZeroException(const std::string& msg)
:MathException(msg) {
:MathematicsException(msg) {
}
@ -50,38 +53,6 @@ DivisionByZeroException::~DivisionByZeroException() throw() {
// Overriden exception base class method
const char* DivisionByZeroException::what() const throw() {
return MathException::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();
return MathematicsException::what();
}

View File

@ -1,5 +1,5 @@
/****************************************************************************
* Copyright (C) 2008 Daniel Chappuis *
* Copyright (C) 2009 Daniel Chappuis *
****************************************************************************
* This file is part of ReactPhysics3D. *
* *
@ -17,59 +17,36 @@
* 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
#define EXCEPTIONS_H
// Libraries
#include <stdexcept>
// ReactPhysics3D namespace
namespace reactphysics3d {
/* -------------------------------------------------------------------
Exception class for the mathematics library
-------------------------------------------------------------------
*/
// MathException
class MathException : public std::runtime_error {
// Class MathematicsException
class MathematicsException : public std::runtime_error {
public:
MathException(const std::string& msg="MathException"); // Constructor
virtual ~MathException() throw(); // Destructor
MathematicsException(const std::string& msg="MathException"); // Constructor
virtual ~MathematicsException() throw(); // Destructor
virtual const char* what() const throw(); // Overriding the base exception method
};
// DivisionByZeroException
class DivisionByZeroException : public MathException {
// 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
};
// Matrix Exception class
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
};
} // End of the ReactPhysics3D namespace
#endif

View File

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

View File

@ -1,5 +1,5 @@
/****************************************************************************
* Copyright (C) 2008 Daniel Chappuis *
* Copyright (C) 2009 Daniel Chappuis *
****************************************************************************
* This file is part of ReactPhysics3D. *
* *
@ -19,8 +19,6 @@
// 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.
// Mathematics library used in the react project
#ifndef DCMATHS_H
#define DCMATHS_H