diff --git a/sources/reactphysics3d/.directory b/sources/reactphysics3d/.directory new file mode 100644 index 00000000..40f07372 --- /dev/null +++ b/sources/reactphysics3d/.directory @@ -0,0 +1,3 @@ +[Dolphin] +Timestamp=2008,12,9,0,4,45 +ViewMode=1 diff --git a/sources/reactphysics3d/mathematics/.directory b/sources/reactphysics3d/mathematics/.directory new file mode 100644 index 00000000..fe105892 --- /dev/null +++ b/sources/reactphysics3d/mathematics/.directory @@ -0,0 +1,3 @@ +[Dolphin] +Timestamp=2008,12,9,0,4,54 +ViewMode=1 diff --git a/sources/reactphysics3d/mathematics/Matrix.cpp b/sources/reactphysics3d/mathematics/Matrix.cpp new file mode 100644 index 00000000..353da87a --- /dev/null +++ b/sources/reactphysics3d/mathematics/Matrix.cpp @@ -0,0 +1,394 @@ +/**************************************************************************** + * Copyright (C) 2008 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 "Matrix.h" + +// 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]; + 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 && j0) { + // 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 Matrix Exception + throw MatrixException("Exception : 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) { + 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 an Matrix Exception + throw MatrixException("Exception : 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 Matrix Exception + throw MatrixException("Exception : 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) { + // 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 + +/* ------------------------------------------------------------------- + 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(MatrixException); // Return the inverse of the matrix if there exists + double getDeterminant() const throw(MatrixException); // 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 +}; + +// 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; +} + +#endif diff --git a/sources/reactphysics3d/mathematics/Matrix3x3.cpp b/sources/reactphysics3d/mathematics/Matrix3x3.cpp new file mode 100644 index 00000000..3dcc8789 --- /dev/null +++ b/sources/reactphysics3d/mathematics/Matrix3x3.cpp @@ -0,0 +1,201 @@ +/**************************************************************************** + * Copyright (C) 2008 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" + + + + +// 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 +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; + } + + // Create the matrix corresponding to the quaternion + Matrix3x3(1.0-y*y*s-z*z*s, x*y*s-w*z*s, z*x*s + w*y*s, x*y*s + w*z*s, 1.0-x*x*s-z*z*s, y*z*s-w*x*s, + z*x*s-w*y*s, y*z*s + w*x*s, 1.0-x*x*s-y*y*s); +} + +// Destructor +Matrix3x3::~Matrix3x3() { + +} + +// Return the inverse matrix +Matrix3x3 Matrix3x3::getInverse() const throw(MatrixException) { + // 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 MatrixException("Exception : 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) { + // 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); + } + 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); + } + } + else if (array[2][2] > array[0][0]) { + + } + else { + + } + } + 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::identityMatrix() { + // Return the identity 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/mathematics/Matrix3x3.h b/sources/reactphysics3d/mathematics/Matrix3x3.h new file mode 100644 index 00000000..3d06e6c4 --- /dev/null +++ b/sources/reactphysics3d/mathematics/Matrix3x3.h @@ -0,0 +1,146 @@ +/**************************************************************************** + * Copyright (C) 2008 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" + + +/* ------------------------------------------------------------------- + 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(MatrixException); // 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 +}; + + +// 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 MatrixException("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 MatrixException("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 +// 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) { + // 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]); +} + +#endif diff --git a/sources/reactphysics3d/mathematics/Quaternion.cpp b/sources/reactphysics3d/mathematics/Quaternion.cpp new file mode 100755 index 00000000..d037d5c8 --- /dev/null +++ b/sources/reactphysics3d/mathematics/Quaternion.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** + * Copyright (C) 2008 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" + +// 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/mathematics/Quaternion.h b/sources/reactphysics3d/mathematics/Quaternion.h new file mode 100644 index 00000000..a6134f3b --- /dev/null +++ b/sources/reactphysics3d/mathematics/Quaternion.h @@ -0,0 +1,200 @@ +/**************************************************************************** + * Copyright (C) 2008 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" + +// 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 + +/* ------------------------------------------------------------------- + Class Quaternion : + This class represents 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 (QuaternionException); // Return the unit quaternion + Quaternion getConjugate() const; // Return the conjugate quaternion + Quaternion getInverse() const throw (QuaternionException); // 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 +}; + + +// 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(QuaternionException) { + 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 QuaternionException("Exception : 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(QuaternionException) { + 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 QuaternionException("Exception : 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 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); +} + +#endif diff --git a/sources/reactphysics3d/mathematics/Vector.cpp b/sources/reactphysics3d/mathematics/Vector.cpp new file mode 100755 index 00000000..c8a6c606 --- /dev/null +++ b/sources/reactphysics3d/mathematics/Vector.cpp @@ -0,0 +1,221 @@ +/**************************************************************************** + * Copyright (C) 2008 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" + +// 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 + +/* ------------------------------------------------------------------- + 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(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) + + 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 +}; + + +// ------ 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 + + +// 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(VectorException) { + 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 VectorException("Exception : Impossible to compute the unit vector because the length of the vector is zero"); + } +} + +// TO DELETE THIS IS JUST FOR TESTS +void Vector3D::display() const { + std::cout << x << std::endl << y << std::endl << z << std::endl; +} + +// 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/mathematics/Vector3D.h b/sources/reactphysics3d/mathematics/Vector3D.h new file mode 100755 index 00000000..3afdc7ec --- /dev/null +++ b/sources/reactphysics3d/mathematics/Vector3D.h @@ -0,0 +1,135 @@ +/**************************************************************************** + * Copyright (C) 2008 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" + + +/* ------------------------------------------------------------------- + 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(VectorException); // 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 +}; + + +// 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); +} + +#endif diff --git a/sources/reactphysics3d/mathematics/exceptions.cpp b/sources/reactphysics3d/mathematics/exceptions.cpp new file mode 100755 index 00000000..d5633b5b --- /dev/null +++ b/sources/reactphysics3d/mathematics/exceptions.cpp @@ -0,0 +1,87 @@ +/**************************************************************************** + * Copyright (C) 2008 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" + + +// Constructor of the MathException class +MathException::MathException(const std::string& msg) + :std::runtime_error(msg) { + +} + +// Destructor of the MathException class +MathException::~MathException() throw() { + +} + +// Overriden exception base class method +const char* MathException::what() const throw() { + return std::runtime_error::what(); +} + +// Constructor of the DivisionByZeroException class +DivisionByZeroException::DivisionByZeroException(const std::string& msg) + :MathException(msg) { + +} + +// Destructor of the DivisionByZeroException class +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(); +} + diff --git a/sources/reactphysics3d/mathematics/exceptions.h b/sources/reactphysics3d/mathematics/exceptions.h new file mode 100755 index 00000000..6a767eac --- /dev/null +++ b/sources/reactphysics3d/mathematics/exceptions.h @@ -0,0 +1,75 @@ +/**************************************************************************** + * Copyright (C) 2008 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 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 + +/* ------------------------------------------------------------------- + Exception class for the mathematics library + ------------------------------------------------------------------- +*/ + +// MathException +class MathException : public std::runtime_error { + public: + MathException(const std::string& msg="MathException"); // Constructor + virtual ~MathException() throw(); // Destructor + virtual const char* what() const throw(); // Overriding the base exception method +}; + + +// DivisionByZeroException +class DivisionByZeroException : public MathException { + 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 +}; + + +#endif diff --git a/sources/reactphysics3d/mathematics/main.cpp b/sources/reactphysics3d/mathematics/main.cpp new file mode 100755 index 00000000..e25b0f52 --- /dev/null +++ b/sources/reactphysics3d/mathematics/main.cpp @@ -0,0 +1,25 @@ + +#include "dcmaths.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/mathematics.h b/sources/reactphysics3d/mathematics/mathematics.h new file mode 100644 index 00000000..698ace36 --- /dev/null +++ b/sources/reactphysics3d/mathematics/mathematics.h @@ -0,0 +1,36 @@ +/**************************************************************************** + * Copyright (C) 2008 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. + +// Mathematics library used in the react project + +#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