diff --git a/sources/reactphysics3d/.directory b/sources/reactphysics3d/.directory new file mode 100644 index 00000000..fe105892 --- /dev/null +++ b/sources/reactphysics3d/.directory @@ -0,0 +1,3 @@ +[Dolphin] +Timestamp=2008,12,9,0,4,54 +ViewMode=1 diff --git a/sources/reactphysics3d/Matrix.cpp b/sources/reactphysics3d/Matrix.cpp new file mode 100644 index 00000000..cf48744c --- /dev/null +++ b/sources/reactphysics3d/Matrix.cpp @@ -0,0 +1,415 @@ +/**************************************************************************** + * Copyright (C) 2009 Daniel Chappuis * + **************************************************************************** + * This file is part of ReactPhysics3D. * + * * + * ReactPhysics3D is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * ReactPhysics3D is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with ReactPhysics3D. If not, see . * + ***************************************************************************/ + +// Libraries +#include +#include "Matrix.h" + +// Namespaces +using namespace reactphysics3d; + +// Constructor of the class Matrix +Matrix::Matrix(int nbRow, int nbColumn) throw(std::invalid_argument) + :nbRow(nbRow),nbColumn(nbColumn) { + // Check the arguments + if (nbRow>0 && nbColumn>0) { + + // Create the two dimensional dynamic array + array = new double*[nbRow]; + + assert(array != 0); // Array pointer musn't be null + + for(int i=0; iarray; +} + + +// Function that return the cofactor matrix by removing row i and column j +Matrix Matrix::getCofactor(int i, int j) const throw(std::invalid_argument) { + // If i and j are in the matrix + if (0<= i && i < nbRow && 0<= j && j 0) { + // Create a new matrix + Matrix identityMatrix(dimension,dimension); + + // Fill in the identity matrix + for(int i=0; igetValue(i,j) + matrix2.getValue(i,j)); + } + } + + // Return the sum matrix + return sumMatrix; + } + else { + // We throw an MathematicsException + throw MathematicsException("MathematicsException : Addition of the matrices isn't possible beacause the size of the matrices aren't the same"); + } +} + +// Definition of the operator - for the substraction of two matrices with references +Matrix Matrix::operator-(const Matrix& matrix2) const throw(MathematicsException) { + if (nbRow == matrix2.nbRow && nbColumn == matrix2.nbColumn) { + // Create a new matrix + Matrix sumMatrix(nbRow, nbColumn); + + // Substract the two matrices + for(int i=0; inbColumn; ++j) { + sumMatrix.setValue(i, j, this->getValue(i,j) - matrix2.getValue(i,j)); + } + } + + // Return the sum matrix + return sumMatrix; + } + else { + // We throw a MathematicsException + throw MathematicsException("MathematicsException : Substraction of the matrices isn't possible beacause the size of the matrices aren't the same"); + } +} + +// Overloaded operator * for the multiplication of the matrix with a number +Matrix Matrix::operator*(double nb) const { + // Creation of the result matrix + Matrix result(nbRow,nbColumn); + + // Multiplication of the matrix with the number + for(int i=0; isetValue(i,j, matrix2.getValue(i,j)); + } + } + } + + // Return a reference to the matrix + return *this; + } + else { + // Throw a MathematicsException + throw MathematicsException("MathematicsException : Assignment impossible because the size of the matrices aren't the same !"); + } +} + +// Overloaded operator for equality condition +bool Matrix::operator==(const Matrix& matrix2) const throw(MathematicsException) { + // Check if the matrices dimensions are compatible + if (nbRow == matrix2.nbRow && nbColumn == matrix2.nbColumn) { + for (int i=0; i. * + ***************************************************************************/ + +#ifndef MATRIX_H +#define MATRIX_H + +// Libraries +#include "exceptions.h" +#include +#include + +// ReactPhysics3D namespace +namespace reactphysics3d { + +/* ------------------------------------------------------------------- + Class Matrix : + This class represents a matrix. + ------------------------------------------------------------------- +*/ +class Matrix { + private : + int nbRow; // Number of row in the matrix + int nbColumn; // Number of colum in the matrix + double** array; // Dynamic array that contains the values of the matrix + + public : + Matrix(int nbRow, int nbColum) throw(std::invalid_argument); // Constructor of the class Matrix + Matrix(const Matrix& matrix); // Copy constructor of the class Matrix + virtual ~Matrix(); // Destructor of the class Matrix + double getValue(int i, int j) const throw(std::invalid_argument); // Return a value in the matrix + void setValue(int i, int j, double value) throw(std::invalid_argument); // Set a value in the matrix + int getNbRow() const; // Return the number of row of the matrix + int getNbColumn() const; // Return the number of column of the matrix + Matrix getCofactor(int i, int j) const throw(std::invalid_argument); // Return the cofactor matrix by removing row i and column j + Matrix getTranspose() const; // Return the transposed matrixs + Matrix getInverse() const throw(MathematicsException); // Return the inverse of the matrix if there exists + double getDeterminant() const throw(MathematicsException); // Return the determinant of the matrix + double getTrace() const throw(MathematicsException); // Return the trace of a square matrix + static Matrix identity(int dimension) throw(std::invalid_argument); // Return the identity matrix I of the given dimension + + void display() const; // TO DELETE + + // --- Overloaded operators --- // + Matrix operator+(const Matrix& matrix2) const throw(MathematicsException); // Overloaded operator for addition + Matrix operator-(const Matrix& matrix2) const throw(MathematicsException); // Overloaded operator for substraction + Matrix operator*(double nb) const; // Overloaded operator for multiplication with a number + Matrix operator*(const Matrix& matrix2) const throw(MathematicsException); // Overloaded operator for multiplication with a matrix + Matrix& operator=(const Matrix& matrix2) throw(MathematicsException); // Overloaded operator for assignment + bool operator==(const Matrix& matrix2) const throw(MathematicsException); // Overloaded operator for equality condition +}; + +// Function to get a value in the matrix (inline) +inline double Matrix::getValue(int i, int j) const throw(std::invalid_argument) { + if (0 <= i && i < nbRow && 0 <= j && j < nbColumn) { + // get the value in the matrix + return array[i][j]; + } + else { + // We Throw an out_of_range exception + throw std::invalid_argument("Exception : The index i or j is outside the matrix size !"); + } +} + +// Function to set a value in the matrix (inline) +inline void Matrix::setValue(int i, int j, double value) throw(std::invalid_argument) { + if (0 <= i && i < nbRow && 0 <= j && j < nbColumn) { + // Set the value in the matrix + this->array[i][j] = value; + } + else { + // We Throw an out_of_range exception + throw std::invalid_argument("Exception : The index i or j is outside the matrix size !"); + } +} + +// Function that return the number of row of the matrix (inline) +inline int Matrix::getNbRow() const { + return nbRow; +} + +// Function that return the number of colum of the matrix (inline) +inline int Matrix::getNbColumn() const { + return nbColumn; +} + +// Overloaded operator for multiplication between a number and a Matrix (inline) +inline Matrix operator*(double number, const Matrix& matrix) { + + // Return the result matrix + return matrix * number; +} + +} // End of the ReactPhysics3D namespace + +#endif diff --git a/sources/reactphysics3d/Matrix3x3.cpp b/sources/reactphysics3d/Matrix3x3.cpp new file mode 100644 index 00000000..a2506c98 --- /dev/null +++ b/sources/reactphysics3d/Matrix3x3.cpp @@ -0,0 +1,219 @@ +/**************************************************************************** + * Copyright (C) 2009 Daniel Chappuis * + **************************************************************************** + * This file is part of ReactPhysics3D. * + * * + * ReactPhysics3D is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * ReactPhysics3D is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with ReactPhysics3D. If not, see . * + ***************************************************************************/ + +// Libraries +#include +#include "Matrix3x3.h" + +// Namespaces +using namespace reactphysics3d; + +// Constructor of the class Matrix3x3 +Matrix3x3::Matrix3x3() { + // Initialize all values in the matrix to zero + setAllValues(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0); +} + +// Constructor with arguments +Matrix3x3::Matrix3x3(double a1, double a2, double a3, double b1, double b2, double b3, double c1, double c2, double c3) { + // Initialize the matrix with the values + setAllValues(a1, a2, a3, b1, b2, b3, c1, c2, c3); +} + +// Copy-constructor +Matrix3x3::Matrix3x3(const Matrix3x3& matrix2) { + // Copy the values in the matrix + setAllValues(matrix2.array[0][0], matrix2.array[0][1], matrix2.array[0][2], + matrix2.array[1][0], matrix2.array[1][1], matrix2.array[1][2], + matrix2.array[2][0], matrix2.array[2][1], matrix2.array[2][2]); +} + +// Create a Matrix3x3 from a quaternion (the quaternion can be non-unit) +Matrix3x3::Matrix3x3(const Quaternion& quaternion) { + double x = quaternion.getX(); + double y = quaternion.getY(); + double z = quaternion.getZ(); + double w = quaternion.getW(); + + double nQ = x*x + y*y + z*z + w*w; + double s = 0.0; + + if (nQ > 0.0) { + s = 2.0/nQ; + } + + // Computations used for optimization (less multiplications) + double xs = x*s; + double ys = y*s; + double zs = z*s; + double wxs = w*xs; + double wys = w*ys; + double wzs = w*zs; + double xxs = x*xs; + double xys = x*ys; + double xzs = x*zs; + double yys = y*ys; + double yzs = y*zs; + double zzs = z*zs; + + // Create the matrix corresponding to the quaternion + setAllValues(1.0-yys-zzs, xys-wzs, xzs + wys, + xys + wzs, 1.0-xxs-zzs, yzs-wxs, + xzs-wys, yzs + wxs, 1.0-xxs-yys); +} + +// Destructor +Matrix3x3::~Matrix3x3() { + +} + +// Return the inverse matrix +Matrix3x3 Matrix3x3::getInverse() const throw(MathematicsException) { + // Compute the determinant of the matrix + double determinant = getDeterminant(); + + // Check if the determinant is equal to zero + if (determinant != 0) { + double invDeterminant = 1.0 / determinant; + Matrix3x3 tempMatrix; + + // Compute the inverse of the matrix + tempMatrix.setAllValues((array[1][1]*array[2][2]-array[2][1]*array[1][2]), -(array[1][0]*array[2][2]-array[2][0]*array[1][2]), (array[1][0]*array[2][1]-array[2][0]*array[1][1]), + -(array[0][1]*array[2][2]-array[2][1]*array[0][2]), (array[0][0]*array[2][2]-array[2][0]*array[0][2]), -(array[0][0]*array[2][1]-array[2][0]*array[0][1]), + (array[0][1]*array[1][2]-array[0][2]*array[1][1]), -(array[0][0]*array[1][2]-array[1][0]*array[0][2]), (array[0][0]*array[1][1]-array[0][1]*array[1][0])); + + // Return the inverse matrix + return (invDeterminant * tempMatrix.getTranspose()); + } + else { + // Throw an exception because the inverse of the matrix doesn't exist if the determinant is equal to zero + throw MathematicsException("MathematicsException : Impossible to compute the inverse of the matrix because the determinant is equal to zero"); + } +} + +// Return the quaternion corresponding to the matrix (it returns a unit quaternion) +Quaternion Matrix3x3::getQuaternion() const { + + // Get the trace of the matrix + double trace = getTrace(); + + double r; + double s; + + if (trace < 0.0) { + if (array[1][1] > array[0][0]) { + if(array[2][2] > array[1][1]) { + r = sqrt(array[2][2] - array[0][0] - array[1][1] + 1.0); + s = 0.5 / r; + return Quaternion((array[2][0] + array[0][2])*s, (array[1][2] + array[2][1])*s, 0.5*r, (array[1][0] - array[0][1])*s); + } + else { + r = sqrt(array[1][1] - array[2][2] - array[0][0] + 1.0); + s = 0.5 / r; + return Quaternion((array[0][1] + array[1][0])*s, 0.5 * r, (array[1][2] + array[2][1])*s, (array[0][2] - array[2][0])*s); + } + } + else if (array[2][2] > array[0][0]) { + r = sqrt(array[2][2] - array[0][0] - array[1][1] + 1.0); + s = 0.5 / r; + return Quaternion((array[2][0] + array[0][2])*s, (array[1][2] + array[2][1])*s, 0.5 * r, (array[1][0] - array[0][1])*s); + } + else { + r = sqrt(array[0][0] - array[1][1] - array[2][2] + 1.0); + s = 0.5 / r; + return Quaternion(0.5 * r, (array[0][1] + array[1][0])*s, (array[2][0] - array[0][2])*s, (array[2][1] - array[1][2])*s); + } + } + else { + r = sqrt(trace + 1.0); + s = 0.5/r; + return Quaternion((array[2][1]-array[1][2])*s, (array[0][2]-array[2][0])*s, (array[1][0]-array[0][1])*s, 0.5 * r); + } +} + +// Return the 3x3 identity matrix +Matrix3x3 Matrix3x3::identity() { + // Return the isdentity matrix + return Matrix3x3(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0); +} + +// TO DELETE, THIS IS JUST FOR TESTING MATRICES +void Matrix3x3::display() const { + for(int i=0; i<3; ++i) { + for(int j=0; j<3; ++j) { + std::cout << array[i][j] << " "; + } + + std::cout << std::endl; + } +} + +// Overloaded operator for addition +Matrix3x3 Matrix3x3::operator+(const Matrix3x3& matrix2) const { + // Return the sum matrix + return Matrix3x3(array[0][0] + matrix2.array[0][0], array[0][1] + matrix2.array[0][1], array[0][2] + matrix2.array[0][2], + array[1][0] + matrix2.array[1][0], array[1][1] + matrix2.array[1][1], array[1][2] + matrix2.array[1][2], + array[2][0] + matrix2.array[2][0], array[2][1] + matrix2.array[2][1], array[2][2] + matrix2.array[2][2]); +} + +// Overloaded operator for substraction +Matrix3x3 Matrix3x3::operator-(const Matrix3x3& matrix2) const { + // Return the substraction matrix + return Matrix3x3(array[0][0] - matrix2.array[0][0], array[0][1] - matrix2.array[0][1], array[0][2] - matrix2.array[0][2], + array[1][0] - matrix2.array[1][0], array[1][1] - matrix2.array[1][1], array[1][2] - matrix2.array[1][2], + array[2][0] - matrix2.array[2][0], array[2][1] - matrix2.array[2][1], array[2][2] - matrix2.array[2][2]); +} + +// Overloaded operator for multiplication with a number +Matrix3x3 Matrix3x3::operator*(double nb) const { + // Return multiplied matrix + return Matrix3x3(array[0][0] * nb, array[0][1] * nb, array[0][2] * nb, + array[1][0] * nb, array[1][1] * nb, array[1][2] * nb, + array[2][0] * nb, array[2][1] * nb, array[2][2] * nb); +} + +// Overloaded operator for multiplication with a matrix +Matrix3x3 Matrix3x3::operator*(const Matrix3x3& matrix2) const { + // Compute and return the multiplication of the matrices + return Matrix3x3(array[0][0]*matrix2.array[0][0] + array[0][1]*matrix2.array[1][0] + array[0][2]*matrix2.array[2][0], + array[0][0]*matrix2.array[0][1] + array[0][1]*matrix2.array[1][1] + array[0][2]*matrix2.array[2][1], + array[0][0]*matrix2.array[0][2] + array[0][1]*matrix2.array[1][2] + array[0][2]*matrix2.array[2][2], + array[1][0]*matrix2.array[0][0] + array[1][1]*matrix2.array[1][0] + array[1][2]*matrix2.array[2][0], + array[1][0]*matrix2.array[0][1] + array[1][1]*matrix2.array[1][1] + array[1][2]*matrix2.array[2][1], + array[1][0]*matrix2.array[0][2] + array[1][1]*matrix2.array[1][2] + array[1][2]*matrix2.array[2][2], + array[2][0]*matrix2.array[0][0] + array[2][1]*matrix2.array[1][0] + array[2][2]*matrix2.array[2][0], + array[2][0]*matrix2.array[0][1] + array[2][1]*matrix2.array[1][1] + array[2][2]*matrix2.array[2][1], + array[2][0]*matrix2.array[0][2] + array[2][1]*matrix2.array[1][2] + array[2][2]*matrix2.array[2][2]); +} + +// Overloaded operator for assignment +Matrix3x3& Matrix3x3::operator=(const Matrix3x3& matrix2) { + // Check for self-assignment + if (this != &matrix2) { + setAllValues(matrix2.array[0][0], matrix2.array[0][1], matrix2.array[0][2], + matrix2.array[1][0], matrix2.array[1][1], matrix2.array[1][2], + matrix2.array[2][0], matrix2.array[2][1], matrix2.array[2][2]); + } + + // Return a reference to the matrix + return *this; +} + + + diff --git a/sources/reactphysics3d/Matrix3x3.h b/sources/reactphysics3d/Matrix3x3.h new file mode 100644 index 00000000..ede01190 --- /dev/null +++ b/sources/reactphysics3d/Matrix3x3.h @@ -0,0 +1,150 @@ +/**************************************************************************** + * Copyright (C) 2009 Daniel Chappuis * + **************************************************************************** + * This file is part of ReactPhysics3D. * + * * + * ReactPhysics3D is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * ReactPhysics3D is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with ReactPhysics3D. If not, see . * + ***************************************************************************/ + + +#ifndef MATRIX3X3_H +#define MATRIX3X3_H + +// Libraries +#include "exceptions.h" +#include "Quaternion.h" + +// ReactPhysics3D namespace +namespace reactphysics3d { + + +/* ------------------------------------------------------------------- + Class Matrix3x3 : + This class represents a 3x3 matrix. + ------------------------------------------------------------------- +*/ +class Matrix3x3 { + private : + double array[3][3]; // Array with the values of the matrix + + public : + Matrix3x3(); // Constructor of the class Matrix3x3 + Matrix3x3(double a1, double a2, double a3, double b1, double b2, double b3, + double c1, double c2, double c3); // Constructor with arguments + Matrix3x3(const Matrix3x3& matrix); // Copy-constructor + Matrix3x3(const Quaternion& quaternion); // Create a Matrix3x3 from a Quaternion + virtual ~Matrix3x3(); // Destructor + double getValue(int i, int j) const throw(std::invalid_argument); // Get a value in the matrix + void setValue(int i, int j, double value) throw(std::invalid_argument); // Set a value in the matrix + void setAllValues(double a1, double a2, double a3, double b1, double b2, double b3, + double c1, double c2, double c3); // Set all the values in the matrix + Matrix3x3 getTranspose() const; // Return the transpose matrix + double getDeterminant() const; // Return the determinant of the matrix + double getTrace() const; // Return the trace of the matrix + Matrix3x3 getInverse() const throw(MathematicsException); // Return the inverse matrix + Quaternion getQuaternion() const; // Return the unit quaternion corresponding to the matrix + static Matrix3x3 identity(); // Return the 3x3 identity matrix + + void display() const; // TO DELETE + + // --- Overloaded operators --- // + Matrix3x3 operator+(const Matrix3x3& matrix2) const; // Overloaded operator for addition + Matrix3x3 operator-(const Matrix3x3& matrix2) const ; // Overloaded operator for substraction + Matrix3x3 operator*(double nb) const; // Overloaded operator for multiplication with a number + Matrix3x3 operator*(const Matrix3x3& matrix2) const; // Overloaded operator for multiplication with a matrix + Matrix3x3& operator=(const Matrix3x3& matrix2); // Overloaded operator for assignment + bool operator==(const Matrix3x3& matrix2) const; // Overloaded operator for equality condition +}; + + +// Method to get a value in the matrix (inline) +inline double Matrix3x3::getValue(int i, int j) const throw(std::invalid_argument) { + // Check the argument + if (i>=0 && i<3 && j>=0 && j<3) { + // Return the value + return array[i][j]; + } + else { + // Throw an exception because of the wrong argument + throw std::invalid_argument("Exception : The argument isn't in the bounds of the 3x3 matrix"); + } +} + +// Method to set a value in the matrix (inline) +inline void Matrix3x3::setValue(int i, int j, double value) throw(std::invalid_argument) { + // Check the argument + if (i>=0 && i<3 && j>=0 && j<3) { + // Set the value + array[i][j] = value; + } + else { + // Throw an exception because of the wrong argument + throw std::invalid_argument("Exception : The argument isn't in the bounds of the 3x3 matrix"); + } +} // End of the dcmaths namespace + +// Method to set all the values in the matrix +inline void Matrix3x3::setAllValues(double a1, double a2, double a3, double b1, double b2, double b3, + double c1, double c2, double c3) { + // Set all the values of the matrix + array[0][0] = a1; + array[0][1] = a2; + array[0][2] = a3; + + array[1][0] = b1; + array[1][1] = b2; + array[1][2] = b3; + + array[2][0] = c1; + array[2][1] = c2; + array[2][2] = c3; +} + +// Return the transpose matrix +inline Matrix3x3 Matrix3x3::getTranspose() const { + // Return the transpose matrix + return Matrix3x3(array[0][0], array[1][0], array[2][0], + array[0][1], array[1][1], array[2][1], + array[0][2], array[1][2], array[2][2]); +} + +// Return the determinant of the matrix +inline double Matrix3x3::getDeterminant() const { + // Compute and return the determinant of the matrix + return (array[0][0]*(array[1][1]*array[2][2]-array[2][1]*array[1][2]) - array[0][1]*(array[1][0]*array[2][2]-array[2][0]*array[1][2]) + + array[0][2]*(array[1][0]*array[2][1]-array[2][0]*array[1][1])); +} + +// Return the trace of the matrix +inline double Matrix3x3::getTrace() const { + // Compute and return the trace + return (array[0][0] + array[1][1] + array[2][2]); +} + +// Overloaded operator for multiplication between a number and a Matrix3x3 (inline) +inline Matrix3x3 operator*(double number, const Matrix3x3& matrix) { + // Return the multiplied matrix + return matrix * number; +} + +// Overloaded operator for equality condition +inline bool Matrix3x3::operator==(const Matrix3x3& matrix2) const { + return (array[0][0] == matrix2.array[0][0] && array[0][1] == matrix2.array[0][1] && array[0][2] == matrix2.array[0][2] && + array[1][0] == matrix2.array[1][0] && array[1][1] == matrix2.array[1][1] && array[1][2] == matrix2.array[1][2] && + array[2][0] == matrix2.array[2][0] && array[2][1] == matrix2.array[2][1] && array[2][2] == matrix2.array[2][2]); +} + +} // End of the ReactPhysics3D namespace + +#endif diff --git a/sources/reactphysics3d/Quaternion.cpp b/sources/reactphysics3d/Quaternion.cpp new file mode 100755 index 00000000..32453c90 --- /dev/null +++ b/sources/reactphysics3d/Quaternion.cpp @@ -0,0 +1,55 @@ +/**************************************************************************** + * Copyright (C) 2009 Daniel Chappuis * + **************************************************************************** + * This file is part of ReactPhysics3D. * + * * + * ReactPhysics3D is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * ReactPhysics3D is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with ReactPhysics3D. If not, see . * + ***************************************************************************/ + +// Libraries +#include "Quaternion.h" + +// Namespaces +using namespace reactphysics3d; + +// Constructor of the class +Quaternion::Quaternion() + :x(0.0), y(0.0), z(0.0), w(0.0) { + +} + +// Constructor with arguments +Quaternion::Quaternion(double x, double y, double z, double w) + :x(x), y(y), z(z), w(w) { + +} + +// Constructor with the component w and the vector v=(x y z) +Quaternion::Quaternion(double w, const Vector3D& v) + :x(v.getX()), y(v.getY()), z(v.getZ()), w(w) { + +} + +// Copy-constructor +Quaternion::Quaternion(const Quaternion& quaternion) + :x(quaternion.x), y(quaternion.y), z(quaternion.z), w(quaternion.w) { + +} + +// Destructor +Quaternion::~Quaternion() { + +} + + diff --git a/sources/reactphysics3d/Quaternion.h b/sources/reactphysics3d/Quaternion.h new file mode 100644 index 00000000..59cb96b0 --- /dev/null +++ b/sources/reactphysics3d/Quaternion.h @@ -0,0 +1,209 @@ +/**************************************************************************** + * Copyright (C) 2009 Daniel Chappuis * + **************************************************************************** + * This file is part of ReactPhysics3D. * + * * + * ReactPhysics3D is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * ReactPhysics3D is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with ReactPhysics3D. If not, see . * + ***************************************************************************/ + +#ifndef QUATERNION_H +#define QUATERNION_H + +// Libraries +#include +#include "Vector3D.h" +#include "exceptions.h" + +// ReactPhysics3D namespace +namespace reactphysics3d { + +/* ------------------------------------------------------------------- + Class Quaternion : + This class represents a quaternion. We use the notation : + q = (x*i, y*j, z*k, w) to represent a quaternion. + ------------------------------------------------------------------- +*/ +class Quaternion +{ + private : + double x; // Component x of the quaternion + double y; // Component y of the quaternion + double z; // Component z of the quaternion + double w; // Component w of the quaternion + + public : + Quaternion(); // Constructor + Quaternion(double x, double y, double z, double w); // Constructor with arguments + Quaternion(double w, const Vector3D& v); // Constructor with the component w and the vector v=(x y z) + Quaternion(const Quaternion& quaternion); // Copy-constructor + ~Quaternion(); // Destructor + double getX() const; // Return the component x of the quaternion + double getY() const; // Return the component y of the quaternion + double getZ() const; // Return the component z of the quaternion + double getW() const; // Return the component w of the quaternion + void setX(double x); // Set the value x + void setY(double y); // Set the value y + void setZ(double z); // Set the value z + void setW(double w); // Set the value w + Vector3D vectorV() const; // Return the vector v=(x y z) of the quaternion + double length() const; // Return the length of the quaternion + Quaternion getUnit() const throw (MathematicsException); // Return the unit quaternion + Quaternion getConjugate() const; // Return the conjugate quaternion + Quaternion getInverse() const throw (MathematicsException); // Return the inverse of the quaternion + + // --- Overloaded operators --- // + Quaternion operator+(const Quaternion& quaternion) const; // Overloaded operator for the addition + Quaternion operator-(const Quaternion& quaternion) const; // Overloaded operator for the substraction + Quaternion operator*(double nb) const; // Overloaded operator for the multiplication with a constant + Quaternion operator*(const Quaternion& quaternion) const; // Overloaded operator for the multiplication + Quaternion& operator=(const Quaternion& quaternion); // Overloaded operator for assignment + bool operator==(const Quaternion& quaternion) const; // Overloaded operator for equality condition +}; + + +// Get the value x (inline) +inline double Quaternion::getX() const { + return x; +} + +// Get the value y (inline) +inline double Quaternion::getY() const { + return y; +} + +// Get the value z (inline) +inline double Quaternion::getZ() const { + return z; +} + +// Get the value w (inline) +inline double Quaternion::getW() const { + return w; +} + +// Set the value x (inline) +inline void Quaternion::setX(double x) { + this->x = x; +} + +// Set the value y (inline) +inline void Quaternion::setY(double y) { + this->y = y; +} + +// Set the value z (inline) +inline void Quaternion::setZ(double z) { + this->z = z; +} + +// Set the value w (inline) +inline void Quaternion::setW(double w) { + this->w = w; +} + +// Return the vector v=(x y z) of the quaternion +inline Vector3D Quaternion::vectorV() const { + // Return the vector v + return Vector3D(x, y, z); +} + +// Return the length of the quaternion (inline) +inline double Quaternion::length() const { + return sqrt(x*x + y*y + z*z + w*w); +} + +// Return the unit quaternion +inline Quaternion Quaternion::getUnit() const throw(MathematicsException) { + double lengthQuaternion = length(); + + // Check if the length is not equal to zero + if (lengthQuaternion != 0.0) { + + // Compute and return the unit quaternion + return Quaternion(x/lengthQuaternion, y/lengthQuaternion, z/lengthQuaternion, w/lengthQuaternion); + } + else { + // Throw an exception because it's impossible to compute a unit quaternion if its length is equal to zero + throw MathematicsException("MathematicsException : Impossible to compute the unit quaternion if the length of the quaternion is zero"); + } +} + +// Return the conjugate of the quaternion (inline) +inline Quaternion Quaternion::getConjugate() const { + return Quaternion(-x, -y, -z, w); +} + +// Return the inverse of the quaternion (inline) +inline Quaternion Quaternion::getInverse() const throw(MathematicsException) { + double lengthQuaternion = length(); + lengthQuaternion = lengthQuaternion * lengthQuaternion; + + // Check if the length is not equal to zero + if (lengthQuaternion != 0.0) { + + // Compute and return the inverse quaternion + return Quaternion(-x/lengthQuaternion, -y/lengthQuaternion, -z/lengthQuaternion, w/lengthQuaternion); + } + else { + // Throw an exception because the inverse cannot be computed + throw MathematicsException("MathematicsException : Impossible to compute the inverse of the quaternion because it's length is zero"); + } +} + +// Overloaded operator for the addition of two quaternions +inline Quaternion Quaternion::operator+(const Quaternion& quaternion) const { + // Return the result quaternion + return Quaternion(x + quaternion.x, y + quaternion.y, z + quaternion.z, w + quaternion.w); +} + +// Overloaded operator for the substraction of two quaternions +inline Quaternion Quaternion::operator-(const Quaternion& quaternion) const { + // Return the result of the substraction + return Quaternion(x-quaternion.x, y - quaternion.y, z - quaternion.z, w - quaternion.w); +} + +// Overloaded operator for the multiplication with a constant +inline Quaternion Quaternion::operator*(double nb) const { + // Return the result + return Quaternion(nb*x, nb*y, nb*z, nb*w); +} + +// Overloaded operator for the multiplication of two quaternions +inline Quaternion Quaternion::operator*(const Quaternion& quaternion) const { + // Return the result of the multiplication + return Quaternion(w*quaternion.w - vectorV().scalarProduct(quaternion.vectorV()), w*quaternion.vectorV()+quaternion.w*vectorV() + vectorV().crossProduct(quaternion.vectorV())); +} + +// Overloaded operator for the assignment +inline Quaternion& Quaternion::operator=(const Quaternion& quaternion) { + // Check for self-assignment + if (this != &quaternion) { + x = quaternion.x; + y = quaternion.y; + z = quaternion.z; + w = quaternion.w; + } + + // Return this quaternion + return *this; +} + +// Overloaded operator for equality condition +inline bool Quaternion::operator==(const Quaternion& quaternion) const { + return (x == quaternion.x && y == quaternion.y && z == quaternion.z && w == quaternion.w); +} + +} // End of the ReactPhysics3D namespace + +#endif diff --git a/sources/reactphysics3d/Vector.cpp b/sources/reactphysics3d/Vector.cpp new file mode 100644 index 00000000..017a34e5 --- /dev/null +++ b/sources/reactphysics3d/Vector.cpp @@ -0,0 +1,223 @@ +/**************************************************************************** + * Copyright (C) 2009 Daniel Chappuis * + **************************************************************************** + * This file is part of ReactPhysics3D. * + * * + * ReactPhysics3D is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * ReactPhysics3D is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with ReactPhysics3D. If not, see . * + ***************************************************************************/ + +// Libraries +#include "Vector.h" + +// Namespaces +using namespace reactphysics3d; + +// Constructor of the class Vector +Vector::Vector(int n) throw(std::invalid_argument) { + // Check the argument + if (n > 0) { + // Create the array that contains the values of the vector + nbComponent = n; + tab = new double[nbComponent]; + + + // Fill the array with zero's value + for(int i=0; i. * + ***************************************************************************/ + +#ifndef VECTOR_H +#define VECTOR_H + +// Libraries +#include "exceptions.h" +#include +#include + +// ReactPhysics3D namespace +namespace reactphysics3d { + +/* ------------------------------------------------------------------- + Class Vector : + This class represents a Vector. + ------------------------------------------------------------------- +*/ +class Vector { + private : + double* tab; // Array of the vector's components + int nbComponent; // number of components in the vector + + public : + Vector(int n) throw(std::invalid_argument); // Constructor of the class Vector + Vector(const Vector& vector); // Copy-constructor of the class Vector + virtual ~Vector(); // Destructor of the class Vector + double getValue(int n) const throw(std::invalid_argument); // Get a component of the vector + void setValue(int n, double value) throw(std::invalid_argument); // Set the value of a component of the vector + int getNbComponent() const; // Get the number of components in the vector + double length() const; // Get the length of the vector + Vector getUnit() const throw(MathematicsException); // Return the corresponding unit vector + double scalarProduct(const Vector& vector) const throw(MathematicsException); // Scalar product of two vectors + Vector crossProduct(const Vector& vector) const throw(MathematicsException); // Cross product of two vectors (in 3D only) + + // --- Overloaded operators --- // + Vector operator+(const Vector& vector) const throw(MathematicsException); // Overloaded operator for addition + Vector operator-(const Vector& vector) const throw(MathematicsException); // Overloaded operator for substraction + Vector operator*(double number) const; // Overloaded operator for multiplication with a number + Vector& operator=(const Vector& vector) throw(MathematicsException); // Overloaded operator for the assignement to a Vector + bool operator==(const Vector& vector) const throw(MathematicsException); // Overloaded operator for the equality condition +}; + + +// ------ Definition of inlines functions ------ // + +// Method to get the value of a component of the vector (inline) +inline double Vector::getValue(int n) const throw(std::invalid_argument) { + // Check the argument + if (n>=0 && n= 0 && n. * + ***************************************************************************/ + +// Libraries +#include "Vector3D.h" +#include + +// Namespaces +using namespace reactphysics3d; + +// Constructor of the class Vector3D +Vector3D::Vector3D() + :x(0.0), y(0.0), z(0.0) { + +} + +// Constructor with arguments +Vector3D::Vector3D(double x, double y, double z) + :x(x), y(y), z(z) { + +} + +// Copy-constructor +Vector3D::Vector3D(const Vector3D& vector) + :x(vector.x), y(vector.y), z(vector.z) { + +} + +// Destructor +Vector3D::~Vector3D() { + +} + +// Return the corresponding unit vector +Vector3D Vector3D::getUnit() const throw(MathematicsException) { + double lengthVector = length(); + + // Check if the length is equal to zero + if (lengthVector != 0) { + // Compute and return the unit vector + double lengthInv = 1.0 / lengthVector; + return Vector3D(x * lengthInv, y * lengthInv, z*lengthInv); + } + else { + // Throw an exception because the length of the vector is zero + throw MathematicsException("MathematicsException : Impossible to compute the unit vector because the length of the vector is zero"); + } +} + +// Overloaded operator for addition +Vector3D Vector3D::operator+(const Vector3D& vector) const { + // Compute and return the sum of the two vectors + return Vector3D(x + vector.x, y + vector.y, z + vector.z); +} + +// Overloaded operator for substraction +Vector3D Vector3D::operator-(const Vector3D& vector) const { + // Compute and return the substraction of the two vectors + return Vector3D(x - vector.x, y - vector.y, z - vector.z); +} + +// Overloaded operator for multiplication with a number +Vector3D Vector3D::operator*(double number) const { + // Compute and return the result + return Vector3D(x * number, y * number, z * number); +} + +// Overloaded operator for the assignement to a Vector +Vector3D& Vector3D::operator=(const Vector3D& vector) { + // Check for self-assignment + if (this != &vector) { + // Copy the vector + x = vector.x; + y = vector.y; + z = vector.z; + } + + // Return a reference to the vector + return *this; +} diff --git a/sources/reactphysics3d/Vector3D.h b/sources/reactphysics3d/Vector3D.h new file mode 100644 index 00000000..99fbbb6c --- /dev/null +++ b/sources/reactphysics3d/Vector3D.h @@ -0,0 +1,136 @@ +/**************************************************************************** + * Copyright (C) 2009 Daniel Chappuis * + **************************************************************************** + * This file is part of ReactPhysics3D. * + * * + * ReactPhysics3D is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * ReactPhysics3D is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with ReactPhysics3D. If not, see . * + ***************************************************************************/ + +#ifndef VECTOR3D_H +#define VECTOR3D_H + +// Libraries +#include +#include "exceptions.h" + +// ReactPhysics3D namespace +namespace reactphysics3d { + + +/* ------------------------------------------------------------------- + Class Vector3D : + This classrepresents 3 dimensionnal vector in space. + ------------------------------------------------------------------- +*/ +class Vector3D { + private : + double x; // X component of the vector + double y; // Y component of the vector + double z; // Z component of the vector + + public : + Vector3D(); // Constructor of the class Vector3D + Vector3D(double x, double y, double z); // Constructor with arguments + Vector3D(const Vector3D& vector); // Copy-constructor + virtual ~Vector3D(); // Destructor + double getX() const; // Get the x component of the vector + double getY() const; // Get the y component of the vector + double getZ() const; // Get the z component of the vector + void setX(double x); // Set the x component of the vector + void setY(double y); // Set the y component of the vector + void setZ(double z); // Set the z component of the vector + void setAllValues(double x, double y, double z); // Set all the values of the vector + double length() const; // Return the lenght of the vector + Vector3D getUnit() const throw(MathematicsException); // Return the corresponding unit vector + double scalarProduct(const Vector3D& vector) const; // Scalar product of two vectors + Vector3D crossProduct(const Vector3D& vector) const; // Cross product of two vectors + + // --- Overloaded operators --- // + Vector3D operator+(const Vector3D& vector) const; // Overloaded operator for addition + Vector3D operator-(const Vector3D& vector) const ; // Overloaded operator for substraction + Vector3D operator*(double number) const; // Overloaded operator for multiplication with a number + Vector3D& operator=(const Vector3D& vector); // Overloaded operator for the assignement to a Vector + bool operator==(const Vector3D& vector) const; // Overloaded operator for the equality condition +}; + + +// Get the x component of the vector (inline) +inline double Vector3D::getX() const { + return x; +} + +// Get the y component of the vector (inline) +inline double Vector3D::getY() const { + return y; +} + +// Get the z component of the vector (inline) +inline double Vector3D::getZ() const { + return z; +} + +// Set the x component of the vector (inline) +inline void Vector3D::setX(double x) { + this->x = x; +} + +// Set the y component of the vector (inline) +inline void Vector3D::setY(double y) { + this->y = y; +} + +// Set the z component of the vector (inline) +inline void Vector3D::setZ(double z) { + this->z = z; +} + +// Set all the values of the vector (inline) +inline void Vector3D::setAllValues(double x, double y, double z) { + this->x = x; + this->y = y; + this->z = z; +} + +// Return the length of the vector (inline) +inline double Vector3D::length() const { + // Compute and return the length of the vector + return sqrt(x*x + y*y + z*z); +} + +// Scalar product of two vectors (inline) +inline double Vector3D::scalarProduct(const Vector3D& vector) const { + // Compute and return the result of the scalar product + return (x * vector.x + y * vector.y + z * vector.z); +} + +// Cross product of two vectors (inline) +inline Vector3D Vector3D::crossProduct(const Vector3D& vector) const { + // Compute and return the cross product + return Vector3D(y * vector.z - z * vector.y, z * vector.x - x * vector.z , x * vector.y - y * vector.x); +} + +// Overloaded operator for multiplication between a number and a Vector3D (inline) +inline Vector3D operator * (double number, const Vector3D& vector) { + // Compute and return the result vector + return vector * number; +} + +// Overloaded operator for the equality condition +inline bool Vector3D::operator == (const Vector3D& vector) const { + return (x == vector.x && y == vector.y && z == vector.z); +} + +} // End of the ReactPhysics3D namespace + +#endif diff --git a/sources/reactphysics3d/exceptions.cpp b/sources/reactphysics3d/exceptions.cpp new file mode 100755 index 00000000..d72a9db3 --- /dev/null +++ b/sources/reactphysics3d/exceptions.cpp @@ -0,0 +1,58 @@ +/**************************************************************************** + * Copyright (C) 2009 Daniel Chappuis * + **************************************************************************** + * This file is part of ReactPhysics3D. * + * * + * ReactPhysics3D is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * ReactPhysics3D is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with ReactPhysics3D. If not, see . * + ***************************************************************************/ + +// Libraries +#include "exceptions.h" + +// Namespaces +using namespace reactphysics3d; + + +// Constructor of the MathematicsException class +MathematicsException::MathematicsException(const std::string& msg) + :std::runtime_error(msg) { + +} + +// Destructor of the MathException class +MathematicsException::~MathematicsException() throw() { + +} + +// Overriden exception base class method +const char* MathematicsException::what() const throw() { + return std::runtime_error::what(); +} + +// Constructor of the DivisionByZeroException class +DivisionByZeroException::DivisionByZeroException(const std::string& msg) + :MathematicsException(msg) { + +} + +// Destructor of the DivisionByZeroException class +DivisionByZeroException::~DivisionByZeroException() throw() { + +} + +// Overriden exception base class method +const char* DivisionByZeroException::what() const throw() { + return MathematicsException::what(); +} + diff --git a/sources/reactphysics3d/exceptions.h b/sources/reactphysics3d/exceptions.h new file mode 100755 index 00000000..785f4d6d --- /dev/null +++ b/sources/reactphysics3d/exceptions.h @@ -0,0 +1,52 @@ +/**************************************************************************** + * Copyright (C) 2009 Daniel Chappuis * + **************************************************************************** + * This file is part of ReactPhysics3D. * + * * + * ReactPhysics3D is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * ReactPhysics3D is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with ReactPhysics3D. If not, see . * + ***************************************************************************/ + +#ifndef EXCEPTIONS_H +#define EXCEPTIONS_H + +// Libraries +#include + +// ReactPhysics3D namespace +namespace reactphysics3d { + +/* ------------------------------------------------------------------- + Exception class for the mathematics library + ------------------------------------------------------------------- +*/ + +// Class MathematicsException +class MathematicsException : public std::runtime_error { + public: + MathematicsException(const std::string& msg="MathException"); // Constructor + virtual ~MathematicsException() throw(); // Destructor + virtual const char* what() const throw(); // Overriding the base exception method +}; + +// Class DivisionByZeroException +class DivisionByZeroException : public MathematicsException { + public: + DivisionByZeroException(const std::string& msg="DivisionByZeroException : Division by zero !"); // Constructor + virtual ~DivisionByZeroException() throw(); // Destructor + virtual const char* what() const throw(); // Overriding the base exception method +}; + +} // End of the ReactPhysics3D namespace + +#endif diff --git a/sources/reactphysics3d/main.cpp b/sources/reactphysics3d/main.cpp new file mode 100755 index 00000000..89c34570 --- /dev/null +++ b/sources/reactphysics3d/main.cpp @@ -0,0 +1,25 @@ + +#include "mathematics.h" + +#include +#include + +using namespace std; + +int main(int argc, char *argv[]) +{ + Matrix matrix(3,3); + matrix.setValue(0, 0, 4); + matrix.setValue(0, 1, 64); + matrix.setValue(0, 2, 6); + + matrix.setValue(1, 0, 73); + matrix.setValue(1, 1, -64); + matrix.setValue(1, 2, 5); + + matrix.setValue(2, 0, 3); + matrix.setValue(2, 1, 976); + matrix.setValue(2, 2, 70); + + matrix.getInverse().display(); +} diff --git a/sources/reactphysics3d/mathematics.h b/sources/reactphysics3d/mathematics.h new file mode 100644 index 00000000..bee2d1e1 --- /dev/null +++ b/sources/reactphysics3d/mathematics.h @@ -0,0 +1,34 @@ +/**************************************************************************** + * Copyright (C) 2009 Daniel Chappuis * + **************************************************************************** + * This file is part of ReactPhysics3D. * + * * + * ReactPhysics3D is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * ReactPhysics3D is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with ReactPhysics3D. If not, see . * + ***************************************************************************/ + +// TODO : Mathematics library : We have to use assert to debug +// TODO : Mathematics library : Check everywhere that in member methods we use attributes access instead of getter and setter. + +#ifndef DCMATHS_H +#define DCMATHS_H + +// Libraries +#include "Matrix.h" +#include "Matrix3x3.h" +#include "Quaternion.h" +#include "Vector.h" +#include "Vector3D.h" +#include "exceptions.h" + +#endif