git-svn-id: https://reactphysics3d.googlecode.com/svn/trunk@20 92aac97c-a6ce-11dd-a772-7fcde58d38e6
This commit is contained in:
parent
e5a19055c6
commit
5d614981da
3
sources/reactphysics3d/.directory
Normal file
3
sources/reactphysics3d/.directory
Normal file
|
@ -0,0 +1,3 @@
|
|||
[Dolphin]
|
||||
Timestamp=2008,12,9,0,4,54
|
||||
ViewMode=1
|
415
sources/reactphysics3d/Matrix.cpp
Normal file
415
sources/reactphysics3d/Matrix.cpp
Normal file
|
@ -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 <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
// Libraries
|
||||
#include <cassert>
|
||||
#include "Matrix.h"
|
||||
|
||||
// Namespaces
|
||||
using namespace reactphysics3d;
|
||||
|
||||
// Constructor of the class Matrix
|
||||
Matrix::Matrix(int nbRow, int nbColumn) throw(std::invalid_argument)
|
||||
:nbRow(nbRow),nbColumn(nbColumn) {
|
||||
// Check the arguments
|
||||
if (nbRow>0 && nbColumn>0) {
|
||||
|
||||
// Create the two dimensional dynamic array
|
||||
array = new double*[nbRow];
|
||||
|
||||
assert(array != 0); // Array pointer musn't be null
|
||||
|
||||
for(int i=0; i<nbRow; ++i) {
|
||||
array[i] = new double[nbColumn];
|
||||
}
|
||||
|
||||
// Fill the matrix with zero's
|
||||
for (int i=0; i<nbRow; ++i) {
|
||||
for(int j=0; j<nbColumn; ++j) {
|
||||
setValue(i,j, 0.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Throw an exception
|
||||
throw std::invalid_argument("Exception : The size of the matrix has to be positive !");
|
||||
}
|
||||
}
|
||||
|
||||
// Copy-constructor of the class Matrix
|
||||
Matrix::Matrix(const Matrix& matrix)
|
||||
:nbRow(matrix.nbRow), nbColumn(matrix.nbColumn) {
|
||||
|
||||
// Create the two dimensional dynamic array
|
||||
array = new double*[nbRow];
|
||||
|
||||
assert(array != 0); // Array pointer musn't be null
|
||||
|
||||
for(int i=0; i<nbRow; ++i) {
|
||||
array[i] = new double[nbColumn];
|
||||
}
|
||||
|
||||
// Copy the matrix
|
||||
for (int i=0; i<nbRow; ++i) {
|
||||
for(int j=0; j<nbColumn; ++j) {
|
||||
setValue(i,j, matrix.getValue(i,j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Destructor of the class Matrix
|
||||
Matrix::~Matrix() {
|
||||
// Destruction of the dynamic array
|
||||
for(int i=0; i<nbRow; ++i) {
|
||||
delete array[i];
|
||||
}
|
||||
delete this->array;
|
||||
}
|
||||
|
||||
|
||||
// Function that return the cofactor matrix by removing row i and column j
|
||||
Matrix Matrix::getCofactor(int i, int j) const throw(std::invalid_argument) {
|
||||
// If i and j are in the matrix
|
||||
if (0<= i && i < nbRow && 0<= j && j<nbColumn) {
|
||||
// Create the cofactor matrix
|
||||
Matrix cofactor(nbRow-1,nbColumn-1);
|
||||
|
||||
int u=0; // Row coordinate in the cofactor matrix
|
||||
int v=0; // Column coordinate in the cofactor matrix
|
||||
|
||||
// For every element in the matrix
|
||||
for (int s=0; s<nbColumn; ++s) {
|
||||
for(int r=0; r<nbRow; ++r) {
|
||||
// If the element is not in row i or in column j
|
||||
if (r!=i && s!=j) {
|
||||
// Add the element in the cofactor matrix
|
||||
cofactor.setValue(u,v, getValue(r,s));
|
||||
++u;
|
||||
if (u==cofactor.nbRow) {
|
||||
u = 0;
|
||||
++v;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return the cofactor matrix
|
||||
return cofactor;
|
||||
}
|
||||
else {
|
||||
// We Throw an out_of_range exception
|
||||
throw std::invalid_argument("Exception : The index i or j is outside the matrix size !");
|
||||
}
|
||||
}
|
||||
|
||||
// This function return the transposed matrix
|
||||
Matrix Matrix::getTranspose() const {
|
||||
// Create the new matrix
|
||||
Matrix transposedMatrix(nbColumn, nbRow);
|
||||
|
||||
// Transposition of the matrix
|
||||
for (int i=0; i<nbRow; ++i) {
|
||||
for(int j=0; j<nbColumn; ++j) {
|
||||
transposedMatrix.setValue(j,i, array[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
// Return the transposed matrix
|
||||
return transposedMatrix;
|
||||
}
|
||||
|
||||
|
||||
// Function that return the inverse of the matrix if there exists
|
||||
Matrix Matrix::getInverse() const throw(MathematicsException) {
|
||||
// Check if the matrix is a square-matrix
|
||||
if (nbRow==nbColumn) {
|
||||
// Compute the determinant of the matrix
|
||||
double determinant = getDeterminant();
|
||||
|
||||
// Check if the matrix is invertible
|
||||
if (determinant != 0.0) {
|
||||
// Create a temp matrix
|
||||
Matrix tempMatrix(nbRow, nbColumn);
|
||||
|
||||
double k=1.0;
|
||||
|
||||
// Compute the inverse matrix
|
||||
for(int i=0; i<nbRow; ++i) {
|
||||
for(int j=0; j<nbColumn; ++j) {
|
||||
if ( (i+j) % 2 == 0) {
|
||||
k=1.0;
|
||||
}
|
||||
else {
|
||||
k=-1.0;
|
||||
}
|
||||
|
||||
tempMatrix.setValue(i,j, k * getCofactor(i,j).getDeterminant());
|
||||
}
|
||||
}
|
||||
|
||||
// Create the inverse matrix
|
||||
Matrix inverseMatrix = tempMatrix.getTranspose() * (1.0 / determinant);
|
||||
|
||||
// Return the inverse matrix
|
||||
return inverseMatrix;
|
||||
}
|
||||
else {
|
||||
// We throw a MathematicsException
|
||||
throw MathematicsException("MathematicsException : Inverse of the matrix can't be computed because the determinant is zero !");
|
||||
}
|
||||
}
|
||||
else {
|
||||
// We throw an Matrix Exception
|
||||
throw MathematicsException("MathematicsException : Inverse can't be computed for a non-square matrix !");
|
||||
}
|
||||
}
|
||||
|
||||
// Function that return the determinant of the matrix
|
||||
double Matrix::getDeterminant() const throw(MathematicsException) {
|
||||
// If the matrix is a square matrix
|
||||
if (nbRow == nbColumn) {
|
||||
if(nbRow == 1) {
|
||||
return getValue(0,0);
|
||||
}
|
||||
else if (nbRow == 2) {
|
||||
return (getValue(0,0) * getValue(1,1) - getValue(1,0) * getValue(0,1));
|
||||
}
|
||||
else {
|
||||
double determinant = 0.0;
|
||||
double k=1.0;
|
||||
|
||||
// For every element in the first row
|
||||
for(int j=0; j<nbColumn; ++j) {
|
||||
determinant = determinant + k * getValue(0,j) * getCofactor(0,j).getDeterminant();
|
||||
|
||||
if (k==1.0) {
|
||||
k=-1.0;
|
||||
}
|
||||
else {
|
||||
k=1.0;
|
||||
}
|
||||
}
|
||||
|
||||
// Return the determinant value
|
||||
return determinant;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Throw a MathematicsException
|
||||
throw MathematicsException("MathematicsException : The determinant of a non-square matrix isn't computable !");
|
||||
}
|
||||
}
|
||||
|
||||
// Return the trace of the matrix
|
||||
double Matrix::getTrace() const throw(MathematicsException) {
|
||||
|
||||
// Check if the matrix is a square-matrix
|
||||
if (nbRow == nbColumn) {
|
||||
double sum = 0.0;
|
||||
|
||||
// Compute the trace of the matrix
|
||||
for(int i=0; i<nbRow; ++i) {
|
||||
sum = sum + array[i][i];
|
||||
}
|
||||
|
||||
// Return the trace
|
||||
return sum;
|
||||
}
|
||||
else {
|
||||
// We throw an exception because the matrix is non-square
|
||||
throw MathematicsException("MathematicsException : Impossible to compute the trace for a non-square matrix");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Static function that return a identity matrix of size nxn
|
||||
Matrix Matrix::identity(int dimension) throw(std::invalid_argument) {
|
||||
// Argument verification
|
||||
if (dimension > 0) {
|
||||
// Create a new matrix
|
||||
Matrix identityMatrix(dimension,dimension);
|
||||
|
||||
// Fill in the identity matrix
|
||||
for(int i=0; i<dimension; ++i) {
|
||||
for(int j=0; j<dimension; ++j) {
|
||||
if (i==j) {
|
||||
identityMatrix.setValue(i, j, 1.0);
|
||||
}
|
||||
else {
|
||||
identityMatrix.setValue(i, j, 0.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return the identity matrix
|
||||
return identityMatrix;
|
||||
}
|
||||
else {
|
||||
// Throw an exception
|
||||
throw std::invalid_argument("Exception : The argument of identity() has to be positive !");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Definition of the operator + for the sum of two matrices with references
|
||||
Matrix Matrix::operator+(const Matrix& matrix2) const throw(MathematicsException) {
|
||||
if (nbRow == matrix2.nbRow && nbColumn == matrix2.nbColumn) {
|
||||
// Create a new matrix
|
||||
Matrix sumMatrix(nbRow,nbColumn);
|
||||
|
||||
// Sum the two matrices
|
||||
for(int i=0; i<nbRow; ++i) {
|
||||
for(int j=0; j<nbColumn; ++j) {
|
||||
sumMatrix.setValue(i, j, this->getValue(i,j) + matrix2.getValue(i,j));
|
||||
}
|
||||
}
|
||||
|
||||
// Return the sum matrix
|
||||
return sumMatrix;
|
||||
}
|
||||
else {
|
||||
// We throw an MathematicsException
|
||||
throw MathematicsException("MathematicsException : Addition of the matrices isn't possible beacause the size of the matrices aren't the same");
|
||||
}
|
||||
}
|
||||
|
||||
// Definition of the operator - for the substraction of two matrices with references
|
||||
Matrix Matrix::operator-(const Matrix& matrix2) const throw(MathematicsException) {
|
||||
if (nbRow == matrix2.nbRow && nbColumn == matrix2.nbColumn) {
|
||||
// Create a new matrix
|
||||
Matrix sumMatrix(nbRow, nbColumn);
|
||||
|
||||
// Substract the two matrices
|
||||
for(int i=0; i<nbRow; ++i) {
|
||||
for(int j=0; j<this->nbColumn; ++j) {
|
||||
sumMatrix.setValue(i, j, this->getValue(i,j) - matrix2.getValue(i,j));
|
||||
}
|
||||
}
|
||||
|
||||
// Return the sum matrix
|
||||
return sumMatrix;
|
||||
}
|
||||
else {
|
||||
// We throw a MathematicsException
|
||||
throw MathematicsException("MathematicsException : Substraction of the matrices isn't possible beacause the size of the matrices aren't the same");
|
||||
}
|
||||
}
|
||||
|
||||
// Overloaded operator * for the multiplication of the matrix with a number
|
||||
Matrix Matrix::operator*(double nb) const {
|
||||
// Creation of the result matrix
|
||||
Matrix result(nbRow,nbColumn);
|
||||
|
||||
// Multiplication of the matrix with the number
|
||||
for(int i=0; i<nbRow; ++i) {
|
||||
for(int j=0; j<nbColumn; ++j) {
|
||||
result.setValue(i,j, getValue(i,j) * nb);
|
||||
}
|
||||
}
|
||||
|
||||
// Return the result matrix
|
||||
return result;
|
||||
}
|
||||
|
||||
// Overloaded operator for multiplication with a matrix
|
||||
Matrix Matrix::operator*(const Matrix& matrix2) const throw(MathematicsException) {
|
||||
// Check the sizes of the matrices
|
||||
if (nbColumn == matrix2.nbRow) {
|
||||
// Compute the result of the multiplication
|
||||
Matrix result(nbRow, matrix2.nbColumn);
|
||||
double sum;
|
||||
|
||||
for(int i=0; i<nbRow; ++i) {
|
||||
for(int j=0; j<matrix2.nbColumn; ++j) {
|
||||
sum = 0.0;
|
||||
for(int k=0; k<nbColumn; ++k) {
|
||||
sum = sum + array[i][k] * matrix2.array[k][j];
|
||||
}
|
||||
result.array[i][j] = sum;
|
||||
}
|
||||
}
|
||||
|
||||
// Return the result matrix
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
// Throw an exception because the multiplication is impossible
|
||||
throw MathematicsException("MathematicsException : The sizes of the matrices aren't compatible for the multiplication");
|
||||
}
|
||||
}
|
||||
|
||||
// Overloaded operator = for the assignment
|
||||
Matrix& Matrix::operator=(const Matrix& matrix2) throw(MathematicsException) {
|
||||
|
||||
// Check for self-assignement
|
||||
if(this == &matrix2) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Check the size of the matrix
|
||||
if (nbRow==matrix2.nbRow && nbColumn==matrix2.nbColumn) {
|
||||
// Check for self-assignment
|
||||
if (this != &matrix2) {
|
||||
for(int i=0; i<nbRow; ++i) {
|
||||
for(int j=0; j<nbColumn; ++j) {
|
||||
this->setValue(i,j, matrix2.getValue(i,j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return a reference to the matrix
|
||||
return *this;
|
||||
}
|
||||
else {
|
||||
// Throw a MathematicsException
|
||||
throw MathematicsException("MathematicsException : Assignment impossible because the size of the matrices aren't the same !");
|
||||
}
|
||||
}
|
||||
|
||||
// Overloaded operator for equality condition
|
||||
bool Matrix::operator==(const Matrix& matrix2) const throw(MathematicsException) {
|
||||
// Check if the matrices dimensions are compatible
|
||||
if (nbRow == matrix2.nbRow && nbColumn == matrix2.nbColumn) {
|
||||
for (int i=0; i<nbRow; ++i) {
|
||||
for(int j=0; j<nbColumn; ++j) {
|
||||
if (array[i][j] != matrix2.array[i][j]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
// Throw an exception because the matrices dimensions aren't the same
|
||||
throw MathematicsException("MathematicsException : Impossible to check if the matrices are equal because they don't have the same dimension");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// TO DELETE, THIS IS JUST FOR TESTING MATRICES
|
||||
void Matrix::display() const {
|
||||
for(int i=0; i<nbRow; ++i) {
|
||||
for(int j=0; j<nbColumn; ++j) {
|
||||
std::cout << array[i][j] << " ";
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
111
sources/reactphysics3d/Matrix.h
Normal file
111
sources/reactphysics3d/Matrix.h
Normal file
|
@ -0,0 +1,111 @@
|
|||
/****************************************************************************
|
||||
* Copyright (C) 2009 Daniel Chappuis *
|
||||
****************************************************************************
|
||||
* This file is part of ReactPhysics3D. *
|
||||
* *
|
||||
* ReactPhysics3D is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation, either version 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* ReactPhysics3D is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef MATRIX_H
|
||||
#define MATRIX_H
|
||||
|
||||
// Libraries
|
||||
#include "exceptions.h"
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
|
||||
// ReactPhysics3D namespace
|
||||
namespace reactphysics3d {
|
||||
|
||||
/* -------------------------------------------------------------------
|
||||
Class Matrix :
|
||||
This class represents a matrix.
|
||||
-------------------------------------------------------------------
|
||||
*/
|
||||
class Matrix {
|
||||
private :
|
||||
int nbRow; // Number of row in the matrix
|
||||
int nbColumn; // Number of colum in the matrix
|
||||
double** array; // Dynamic array that contains the values of the matrix
|
||||
|
||||
public :
|
||||
Matrix(int nbRow, int nbColum) throw(std::invalid_argument); // Constructor of the class Matrix
|
||||
Matrix(const Matrix& matrix); // Copy constructor of the class Matrix
|
||||
virtual ~Matrix(); // Destructor of the class Matrix
|
||||
double getValue(int i, int j) const throw(std::invalid_argument); // Return a value in the matrix
|
||||
void setValue(int i, int j, double value) throw(std::invalid_argument); // Set a value in the matrix
|
||||
int getNbRow() const; // Return the number of row of the matrix
|
||||
int getNbColumn() const; // Return the number of column of the matrix
|
||||
Matrix getCofactor(int i, int j) const throw(std::invalid_argument); // Return the cofactor matrix by removing row i and column j
|
||||
Matrix getTranspose() const; // Return the transposed matrixs
|
||||
Matrix getInverse() const throw(MathematicsException); // Return the inverse of the matrix if there exists
|
||||
double getDeterminant() const throw(MathematicsException); // Return the determinant of the matrix
|
||||
double getTrace() const throw(MathematicsException); // Return the trace of a square matrix
|
||||
static Matrix identity(int dimension) throw(std::invalid_argument); // Return the identity matrix I of the given dimension
|
||||
|
||||
void display() const; // TO DELETE
|
||||
|
||||
// --- Overloaded operators --- //
|
||||
Matrix operator+(const Matrix& matrix2) const throw(MathematicsException); // Overloaded operator for addition
|
||||
Matrix operator-(const Matrix& matrix2) const throw(MathematicsException); // Overloaded operator for substraction
|
||||
Matrix operator*(double nb) const; // Overloaded operator for multiplication with a number
|
||||
Matrix operator*(const Matrix& matrix2) const throw(MathematicsException); // Overloaded operator for multiplication with a matrix
|
||||
Matrix& operator=(const Matrix& matrix2) throw(MathematicsException); // Overloaded operator for assignment
|
||||
bool operator==(const Matrix& matrix2) const throw(MathematicsException); // Overloaded operator for equality condition
|
||||
};
|
||||
|
||||
// Function to get a value in the matrix (inline)
|
||||
inline double Matrix::getValue(int i, int j) const throw(std::invalid_argument) {
|
||||
if (0 <= i && i < nbRow && 0 <= j && j < nbColumn) {
|
||||
// get the value in the matrix
|
||||
return array[i][j];
|
||||
}
|
||||
else {
|
||||
// We Throw an out_of_range exception
|
||||
throw std::invalid_argument("Exception : The index i or j is outside the matrix size !");
|
||||
}
|
||||
}
|
||||
|
||||
// Function to set a value in the matrix (inline)
|
||||
inline void Matrix::setValue(int i, int j, double value) throw(std::invalid_argument) {
|
||||
if (0 <= i && i < nbRow && 0 <= j && j < nbColumn) {
|
||||
// Set the value in the matrix
|
||||
this->array[i][j] = value;
|
||||
}
|
||||
else {
|
||||
// We Throw an out_of_range exception
|
||||
throw std::invalid_argument("Exception : The index i or j is outside the matrix size !");
|
||||
}
|
||||
}
|
||||
|
||||
// Function that return the number of row of the matrix (inline)
|
||||
inline int Matrix::getNbRow() const {
|
||||
return nbRow;
|
||||
}
|
||||
|
||||
// Function that return the number of colum of the matrix (inline)
|
||||
inline int Matrix::getNbColumn() const {
|
||||
return nbColumn;
|
||||
}
|
||||
|
||||
// Overloaded operator for multiplication between a number and a Matrix (inline)
|
||||
inline Matrix operator*(double number, const Matrix& matrix) {
|
||||
|
||||
// Return the result matrix
|
||||
return matrix * number;
|
||||
}
|
||||
|
||||
} // End of the ReactPhysics3D namespace
|
||||
|
||||
#endif
|
219
sources/reactphysics3d/Matrix3x3.cpp
Normal file
219
sources/reactphysics3d/Matrix3x3.cpp
Normal file
|
@ -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 <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
// Libraries
|
||||
#include <iostream>
|
||||
#include "Matrix3x3.h"
|
||||
|
||||
// Namespaces
|
||||
using namespace reactphysics3d;
|
||||
|
||||
// Constructor of the class Matrix3x3
|
||||
Matrix3x3::Matrix3x3() {
|
||||
// Initialize all values in the matrix to zero
|
||||
setAllValues(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
|
||||
}
|
||||
|
||||
// Constructor with arguments
|
||||
Matrix3x3::Matrix3x3(double a1, double a2, double a3, double b1, double b2, double b3, double c1, double c2, double c3) {
|
||||
// Initialize the matrix with the values
|
||||
setAllValues(a1, a2, a3, b1, b2, b3, c1, c2, c3);
|
||||
}
|
||||
|
||||
// Copy-constructor
|
||||
Matrix3x3::Matrix3x3(const Matrix3x3& matrix2) {
|
||||
// Copy the values in the matrix
|
||||
setAllValues(matrix2.array[0][0], matrix2.array[0][1], matrix2.array[0][2],
|
||||
matrix2.array[1][0], matrix2.array[1][1], matrix2.array[1][2],
|
||||
matrix2.array[2][0], matrix2.array[2][1], matrix2.array[2][2]);
|
||||
}
|
||||
|
||||
// Create a Matrix3x3 from a quaternion (the quaternion can be non-unit)
|
||||
Matrix3x3::Matrix3x3(const Quaternion& quaternion) {
|
||||
double x = quaternion.getX();
|
||||
double y = quaternion.getY();
|
||||
double z = quaternion.getZ();
|
||||
double w = quaternion.getW();
|
||||
|
||||
double nQ = x*x + y*y + z*z + w*w;
|
||||
double s = 0.0;
|
||||
|
||||
if (nQ > 0.0) {
|
||||
s = 2.0/nQ;
|
||||
}
|
||||
|
||||
// Computations used for optimization (less multiplications)
|
||||
double xs = x*s;
|
||||
double ys = y*s;
|
||||
double zs = z*s;
|
||||
double wxs = w*xs;
|
||||
double wys = w*ys;
|
||||
double wzs = w*zs;
|
||||
double xxs = x*xs;
|
||||
double xys = x*ys;
|
||||
double xzs = x*zs;
|
||||
double yys = y*ys;
|
||||
double yzs = y*zs;
|
||||
double zzs = z*zs;
|
||||
|
||||
// Create the matrix corresponding to the quaternion
|
||||
setAllValues(1.0-yys-zzs, xys-wzs, xzs + wys,
|
||||
xys + wzs, 1.0-xxs-zzs, yzs-wxs,
|
||||
xzs-wys, yzs + wxs, 1.0-xxs-yys);
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Matrix3x3::~Matrix3x3() {
|
||||
|
||||
}
|
||||
|
||||
// Return the inverse matrix
|
||||
Matrix3x3 Matrix3x3::getInverse() const throw(MathematicsException) {
|
||||
// Compute the determinant of the matrix
|
||||
double determinant = getDeterminant();
|
||||
|
||||
// Check if the determinant is equal to zero
|
||||
if (determinant != 0) {
|
||||
double invDeterminant = 1.0 / determinant;
|
||||
Matrix3x3 tempMatrix;
|
||||
|
||||
// Compute the inverse of the matrix
|
||||
tempMatrix.setAllValues((array[1][1]*array[2][2]-array[2][1]*array[1][2]), -(array[1][0]*array[2][2]-array[2][0]*array[1][2]), (array[1][0]*array[2][1]-array[2][0]*array[1][1]),
|
||||
-(array[0][1]*array[2][2]-array[2][1]*array[0][2]), (array[0][0]*array[2][2]-array[2][0]*array[0][2]), -(array[0][0]*array[2][1]-array[2][0]*array[0][1]),
|
||||
(array[0][1]*array[1][2]-array[0][2]*array[1][1]), -(array[0][0]*array[1][2]-array[1][0]*array[0][2]), (array[0][0]*array[1][1]-array[0][1]*array[1][0]));
|
||||
|
||||
// Return the inverse matrix
|
||||
return (invDeterminant * tempMatrix.getTranspose());
|
||||
}
|
||||
else {
|
||||
// Throw an exception because the inverse of the matrix doesn't exist if the determinant is equal to zero
|
||||
throw MathematicsException("MathematicsException : Impossible to compute the inverse of the matrix because the determinant is equal to zero");
|
||||
}
|
||||
}
|
||||
|
||||
// Return the quaternion corresponding to the matrix (it returns a unit quaternion)
|
||||
Quaternion Matrix3x3::getQuaternion() const {
|
||||
|
||||
// Get the trace of the matrix
|
||||
double trace = getTrace();
|
||||
|
||||
double r;
|
||||
double s;
|
||||
|
||||
if (trace < 0.0) {
|
||||
if (array[1][1] > array[0][0]) {
|
||||
if(array[2][2] > array[1][1]) {
|
||||
r = sqrt(array[2][2] - array[0][0] - array[1][1] + 1.0);
|
||||
s = 0.5 / r;
|
||||
return Quaternion((array[2][0] + array[0][2])*s, (array[1][2] + array[2][1])*s, 0.5*r, (array[1][0] - array[0][1])*s);
|
||||
}
|
||||
else {
|
||||
r = sqrt(array[1][1] - array[2][2] - array[0][0] + 1.0);
|
||||
s = 0.5 / r;
|
||||
return Quaternion((array[0][1] + array[1][0])*s, 0.5 * r, (array[1][2] + array[2][1])*s, (array[0][2] - array[2][0])*s);
|
||||
}
|
||||
}
|
||||
else if (array[2][2] > array[0][0]) {
|
||||
r = sqrt(array[2][2] - array[0][0] - array[1][1] + 1.0);
|
||||
s = 0.5 / r;
|
||||
return Quaternion((array[2][0] + array[0][2])*s, (array[1][2] + array[2][1])*s, 0.5 * r, (array[1][0] - array[0][1])*s);
|
||||
}
|
||||
else {
|
||||
r = sqrt(array[0][0] - array[1][1] - array[2][2] + 1.0);
|
||||
s = 0.5 / r;
|
||||
return Quaternion(0.5 * r, (array[0][1] + array[1][0])*s, (array[2][0] - array[0][2])*s, (array[2][1] - array[1][2])*s);
|
||||
}
|
||||
}
|
||||
else {
|
||||
r = sqrt(trace + 1.0);
|
||||
s = 0.5/r;
|
||||
return Quaternion((array[2][1]-array[1][2])*s, (array[0][2]-array[2][0])*s, (array[1][0]-array[0][1])*s, 0.5 * r);
|
||||
}
|
||||
}
|
||||
|
||||
// Return the 3x3 identity matrix
|
||||
Matrix3x3 Matrix3x3::identity() {
|
||||
// Return the isdentity matrix
|
||||
return Matrix3x3(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0);
|
||||
}
|
||||
|
||||
// TO DELETE, THIS IS JUST FOR TESTING MATRICES
|
||||
void Matrix3x3::display() const {
|
||||
for(int i=0; i<3; ++i) {
|
||||
for(int j=0; j<3; ++j) {
|
||||
std::cout << array[i][j] << " ";
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// Overloaded operator for addition
|
||||
Matrix3x3 Matrix3x3::operator+(const Matrix3x3& matrix2) const {
|
||||
// Return the sum matrix
|
||||
return Matrix3x3(array[0][0] + matrix2.array[0][0], array[0][1] + matrix2.array[0][1], array[0][2] + matrix2.array[0][2],
|
||||
array[1][0] + matrix2.array[1][0], array[1][1] + matrix2.array[1][1], array[1][2] + matrix2.array[1][2],
|
||||
array[2][0] + matrix2.array[2][0], array[2][1] + matrix2.array[2][1], array[2][2] + matrix2.array[2][2]);
|
||||
}
|
||||
|
||||
// Overloaded operator for substraction
|
||||
Matrix3x3 Matrix3x3::operator-(const Matrix3x3& matrix2) const {
|
||||
// Return the substraction matrix
|
||||
return Matrix3x3(array[0][0] - matrix2.array[0][0], array[0][1] - matrix2.array[0][1], array[0][2] - matrix2.array[0][2],
|
||||
array[1][0] - matrix2.array[1][0], array[1][1] - matrix2.array[1][1], array[1][2] - matrix2.array[1][2],
|
||||
array[2][0] - matrix2.array[2][0], array[2][1] - matrix2.array[2][1], array[2][2] - matrix2.array[2][2]);
|
||||
}
|
||||
|
||||
// Overloaded operator for multiplication with a number
|
||||
Matrix3x3 Matrix3x3::operator*(double nb) const {
|
||||
// Return multiplied matrix
|
||||
return Matrix3x3(array[0][0] * nb, array[0][1] * nb, array[0][2] * nb,
|
||||
array[1][0] * nb, array[1][1] * nb, array[1][2] * nb,
|
||||
array[2][0] * nb, array[2][1] * nb, array[2][2] * nb);
|
||||
}
|
||||
|
||||
// Overloaded operator for multiplication with a matrix
|
||||
Matrix3x3 Matrix3x3::operator*(const Matrix3x3& matrix2) const {
|
||||
// Compute and return the multiplication of the matrices
|
||||
return Matrix3x3(array[0][0]*matrix2.array[0][0] + array[0][1]*matrix2.array[1][0] + array[0][2]*matrix2.array[2][0],
|
||||
array[0][0]*matrix2.array[0][1] + array[0][1]*matrix2.array[1][1] + array[0][2]*matrix2.array[2][1],
|
||||
array[0][0]*matrix2.array[0][2] + array[0][1]*matrix2.array[1][2] + array[0][2]*matrix2.array[2][2],
|
||||
array[1][0]*matrix2.array[0][0] + array[1][1]*matrix2.array[1][0] + array[1][2]*matrix2.array[2][0],
|
||||
array[1][0]*matrix2.array[0][1] + array[1][1]*matrix2.array[1][1] + array[1][2]*matrix2.array[2][1],
|
||||
array[1][0]*matrix2.array[0][2] + array[1][1]*matrix2.array[1][2] + array[1][2]*matrix2.array[2][2],
|
||||
array[2][0]*matrix2.array[0][0] + array[2][1]*matrix2.array[1][0] + array[2][2]*matrix2.array[2][0],
|
||||
array[2][0]*matrix2.array[0][1] + array[2][1]*matrix2.array[1][1] + array[2][2]*matrix2.array[2][1],
|
||||
array[2][0]*matrix2.array[0][2] + array[2][1]*matrix2.array[1][2] + array[2][2]*matrix2.array[2][2]);
|
||||
}
|
||||
|
||||
// Overloaded operator for assignment
|
||||
Matrix3x3& Matrix3x3::operator=(const Matrix3x3& matrix2) {
|
||||
// Check for self-assignment
|
||||
if (this != &matrix2) {
|
||||
setAllValues(matrix2.array[0][0], matrix2.array[0][1], matrix2.array[0][2],
|
||||
matrix2.array[1][0], matrix2.array[1][1], matrix2.array[1][2],
|
||||
matrix2.array[2][0], matrix2.array[2][1], matrix2.array[2][2]);
|
||||
}
|
||||
|
||||
// Return a reference to the matrix
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
150
sources/reactphysics3d/Matrix3x3.h
Normal file
150
sources/reactphysics3d/Matrix3x3.h
Normal file
|
@ -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 <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#ifndef MATRIX3X3_H
|
||||
#define MATRIX3X3_H
|
||||
|
||||
// Libraries
|
||||
#include "exceptions.h"
|
||||
#include "Quaternion.h"
|
||||
|
||||
// ReactPhysics3D namespace
|
||||
namespace reactphysics3d {
|
||||
|
||||
|
||||
/* -------------------------------------------------------------------
|
||||
Class Matrix3x3 :
|
||||
This class represents a 3x3 matrix.
|
||||
-------------------------------------------------------------------
|
||||
*/
|
||||
class Matrix3x3 {
|
||||
private :
|
||||
double array[3][3]; // Array with the values of the matrix
|
||||
|
||||
public :
|
||||
Matrix3x3(); // Constructor of the class Matrix3x3
|
||||
Matrix3x3(double a1, double a2, double a3, double b1, double b2, double b3,
|
||||
double c1, double c2, double c3); // Constructor with arguments
|
||||
Matrix3x3(const Matrix3x3& matrix); // Copy-constructor
|
||||
Matrix3x3(const Quaternion& quaternion); // Create a Matrix3x3 from a Quaternion
|
||||
virtual ~Matrix3x3(); // Destructor
|
||||
double getValue(int i, int j) const throw(std::invalid_argument); // Get a value in the matrix
|
||||
void setValue(int i, int j, double value) throw(std::invalid_argument); // Set a value in the matrix
|
||||
void setAllValues(double a1, double a2, double a3, double b1, double b2, double b3,
|
||||
double c1, double c2, double c3); // Set all the values in the matrix
|
||||
Matrix3x3 getTranspose() const; // Return the transpose matrix
|
||||
double getDeterminant() const; // Return the determinant of the matrix
|
||||
double getTrace() const; // Return the trace of the matrix
|
||||
Matrix3x3 getInverse() const throw(MathematicsException); // Return the inverse matrix
|
||||
Quaternion getQuaternion() const; // Return the unit quaternion corresponding to the matrix
|
||||
static Matrix3x3 identity(); // Return the 3x3 identity matrix
|
||||
|
||||
void display() const; // TO DELETE
|
||||
|
||||
// --- Overloaded operators --- //
|
||||
Matrix3x3 operator+(const Matrix3x3& matrix2) const; // Overloaded operator for addition
|
||||
Matrix3x3 operator-(const Matrix3x3& matrix2) const ; // Overloaded operator for substraction
|
||||
Matrix3x3 operator*(double nb) const; // Overloaded operator for multiplication with a number
|
||||
Matrix3x3 operator*(const Matrix3x3& matrix2) const; // Overloaded operator for multiplication with a matrix
|
||||
Matrix3x3& operator=(const Matrix3x3& matrix2); // Overloaded operator for assignment
|
||||
bool operator==(const Matrix3x3& matrix2) const; // Overloaded operator for equality condition
|
||||
};
|
||||
|
||||
|
||||
// Method to get a value in the matrix (inline)
|
||||
inline double Matrix3x3::getValue(int i, int j) const throw(std::invalid_argument) {
|
||||
// Check the argument
|
||||
if (i>=0 && i<3 && j>=0 && j<3) {
|
||||
// Return the value
|
||||
return array[i][j];
|
||||
}
|
||||
else {
|
||||
// Throw an exception because of the wrong argument
|
||||
throw std::invalid_argument("Exception : The argument isn't in the bounds of the 3x3 matrix");
|
||||
}
|
||||
}
|
||||
|
||||
// Method to set a value in the matrix (inline)
|
||||
inline void Matrix3x3::setValue(int i, int j, double value) throw(std::invalid_argument) {
|
||||
// Check the argument
|
||||
if (i>=0 && i<3 && j>=0 && j<3) {
|
||||
// Set the value
|
||||
array[i][j] = value;
|
||||
}
|
||||
else {
|
||||
// Throw an exception because of the wrong argument
|
||||
throw std::invalid_argument("Exception : The argument isn't in the bounds of the 3x3 matrix");
|
||||
}
|
||||
} // End of the dcmaths namespace
|
||||
|
||||
// Method to set all the values in the matrix
|
||||
inline void Matrix3x3::setAllValues(double a1, double a2, double a3, double b1, double b2, double b3,
|
||||
double c1, double c2, double c3) {
|
||||
// Set all the values of the matrix
|
||||
array[0][0] = a1;
|
||||
array[0][1] = a2;
|
||||
array[0][2] = a3;
|
||||
|
||||
array[1][0] = b1;
|
||||
array[1][1] = b2;
|
||||
array[1][2] = b3;
|
||||
|
||||
array[2][0] = c1;
|
||||
array[2][1] = c2;
|
||||
array[2][2] = c3;
|
||||
}
|
||||
|
||||
// Return the transpose matrix
|
||||
inline Matrix3x3 Matrix3x3::getTranspose() const {
|
||||
// Return the transpose matrix
|
||||
return Matrix3x3(array[0][0], array[1][0], array[2][0],
|
||||
array[0][1], array[1][1], array[2][1],
|
||||
array[0][2], array[1][2], array[2][2]);
|
||||
}
|
||||
|
||||
// Return the determinant of the matrix
|
||||
inline double Matrix3x3::getDeterminant() const {
|
||||
// Compute and return the determinant of the matrix
|
||||
return (array[0][0]*(array[1][1]*array[2][2]-array[2][1]*array[1][2]) - array[0][1]*(array[1][0]*array[2][2]-array[2][0]*array[1][2]) +
|
||||
array[0][2]*(array[1][0]*array[2][1]-array[2][0]*array[1][1]));
|
||||
}
|
||||
|
||||
// Return the trace of the matrix
|
||||
inline double Matrix3x3::getTrace() const {
|
||||
// Compute and return the trace
|
||||
return (array[0][0] + array[1][1] + array[2][2]);
|
||||
}
|
||||
|
||||
// Overloaded operator for multiplication between a number and a Matrix3x3 (inline)
|
||||
inline Matrix3x3 operator*(double number, const Matrix3x3& matrix) {
|
||||
// Return the multiplied matrix
|
||||
return matrix * number;
|
||||
}
|
||||
|
||||
// Overloaded operator for equality condition
|
||||
inline bool Matrix3x3::operator==(const Matrix3x3& matrix2) const {
|
||||
return (array[0][0] == matrix2.array[0][0] && array[0][1] == matrix2.array[0][1] && array[0][2] == matrix2.array[0][2] &&
|
||||
array[1][0] == matrix2.array[1][0] && array[1][1] == matrix2.array[1][1] && array[1][2] == matrix2.array[1][2] &&
|
||||
array[2][0] == matrix2.array[2][0] && array[2][1] == matrix2.array[2][1] && array[2][2] == matrix2.array[2][2]);
|
||||
}
|
||||
|
||||
} // End of the ReactPhysics3D namespace
|
||||
|
||||
#endif
|
55
sources/reactphysics3d/Quaternion.cpp
Executable file
55
sources/reactphysics3d/Quaternion.cpp
Executable file
|
@ -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 <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
// Libraries
|
||||
#include "Quaternion.h"
|
||||
|
||||
// Namespaces
|
||||
using namespace reactphysics3d;
|
||||
|
||||
// Constructor of the class
|
||||
Quaternion::Quaternion()
|
||||
:x(0.0), y(0.0), z(0.0), w(0.0) {
|
||||
|
||||
}
|
||||
|
||||
// Constructor with arguments
|
||||
Quaternion::Quaternion(double x, double y, double z, double w)
|
||||
:x(x), y(y), z(z), w(w) {
|
||||
|
||||
}
|
||||
|
||||
// Constructor with the component w and the vector v=(x y z)
|
||||
Quaternion::Quaternion(double w, const Vector3D& v)
|
||||
:x(v.getX()), y(v.getY()), z(v.getZ()), w(w) {
|
||||
|
||||
}
|
||||
|
||||
// Copy-constructor
|
||||
Quaternion::Quaternion(const Quaternion& quaternion)
|
||||
:x(quaternion.x), y(quaternion.y), z(quaternion.z), w(quaternion.w) {
|
||||
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Quaternion::~Quaternion() {
|
||||
|
||||
}
|
||||
|
||||
|
209
sources/reactphysics3d/Quaternion.h
Normal file
209
sources/reactphysics3d/Quaternion.h
Normal file
|
@ -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 <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef QUATERNION_H
|
||||
#define QUATERNION_H
|
||||
|
||||
// Libraries
|
||||
#include <cmath>
|
||||
#include "Vector3D.h"
|
||||
#include "exceptions.h"
|
||||
|
||||
// ReactPhysics3D namespace
|
||||
namespace reactphysics3d {
|
||||
|
||||
/* -------------------------------------------------------------------
|
||||
Class Quaternion :
|
||||
This class represents a quaternion. We use the notation :
|
||||
q = (x*i, y*j, z*k, w) to represent a quaternion.
|
||||
-------------------------------------------------------------------
|
||||
*/
|
||||
class Quaternion
|
||||
{
|
||||
private :
|
||||
double x; // Component x of the quaternion
|
||||
double y; // Component y of the quaternion
|
||||
double z; // Component z of the quaternion
|
||||
double w; // Component w of the quaternion
|
||||
|
||||
public :
|
||||
Quaternion(); // Constructor
|
||||
Quaternion(double x, double y, double z, double w); // Constructor with arguments
|
||||
Quaternion(double w, const Vector3D& v); // Constructor with the component w and the vector v=(x y z)
|
||||
Quaternion(const Quaternion& quaternion); // Copy-constructor
|
||||
~Quaternion(); // Destructor
|
||||
double getX() const; // Return the component x of the quaternion
|
||||
double getY() const; // Return the component y of the quaternion
|
||||
double getZ() const; // Return the component z of the quaternion
|
||||
double getW() const; // Return the component w of the quaternion
|
||||
void setX(double x); // Set the value x
|
||||
void setY(double y); // Set the value y
|
||||
void setZ(double z); // Set the value z
|
||||
void setW(double w); // Set the value w
|
||||
Vector3D vectorV() const; // Return the vector v=(x y z) of the quaternion
|
||||
double length() const; // Return the length of the quaternion
|
||||
Quaternion getUnit() const throw (MathematicsException); // Return the unit quaternion
|
||||
Quaternion getConjugate() const; // Return the conjugate quaternion
|
||||
Quaternion getInverse() const throw (MathematicsException); // Return the inverse of the quaternion
|
||||
|
||||
// --- Overloaded operators --- //
|
||||
Quaternion operator+(const Quaternion& quaternion) const; // Overloaded operator for the addition
|
||||
Quaternion operator-(const Quaternion& quaternion) const; // Overloaded operator for the substraction
|
||||
Quaternion operator*(double nb) const; // Overloaded operator for the multiplication with a constant
|
||||
Quaternion operator*(const Quaternion& quaternion) const; // Overloaded operator for the multiplication
|
||||
Quaternion& operator=(const Quaternion& quaternion); // Overloaded operator for assignment
|
||||
bool operator==(const Quaternion& quaternion) const; // Overloaded operator for equality condition
|
||||
};
|
||||
|
||||
|
||||
// Get the value x (inline)
|
||||
inline double Quaternion::getX() const {
|
||||
return x;
|
||||
}
|
||||
|
||||
// Get the value y (inline)
|
||||
inline double Quaternion::getY() const {
|
||||
return y;
|
||||
}
|
||||
|
||||
// Get the value z (inline)
|
||||
inline double Quaternion::getZ() const {
|
||||
return z;
|
||||
}
|
||||
|
||||
// Get the value w (inline)
|
||||
inline double Quaternion::getW() const {
|
||||
return w;
|
||||
}
|
||||
|
||||
// Set the value x (inline)
|
||||
inline void Quaternion::setX(double x) {
|
||||
this->x = x;
|
||||
}
|
||||
|
||||
// Set the value y (inline)
|
||||
inline void Quaternion::setY(double y) {
|
||||
this->y = y;
|
||||
}
|
||||
|
||||
// Set the value z (inline)
|
||||
inline void Quaternion::setZ(double z) {
|
||||
this->z = z;
|
||||
}
|
||||
|
||||
// Set the value w (inline)
|
||||
inline void Quaternion::setW(double w) {
|
||||
this->w = w;
|
||||
}
|
||||
|
||||
// Return the vector v=(x y z) of the quaternion
|
||||
inline Vector3D Quaternion::vectorV() const {
|
||||
// Return the vector v
|
||||
return Vector3D(x, y, z);
|
||||
}
|
||||
|
||||
// Return the length of the quaternion (inline)
|
||||
inline double Quaternion::length() const {
|
||||
return sqrt(x*x + y*y + z*z + w*w);
|
||||
}
|
||||
|
||||
// Return the unit quaternion
|
||||
inline Quaternion Quaternion::getUnit() const throw(MathematicsException) {
|
||||
double lengthQuaternion = length();
|
||||
|
||||
// Check if the length is not equal to zero
|
||||
if (lengthQuaternion != 0.0) {
|
||||
|
||||
// Compute and return the unit quaternion
|
||||
return Quaternion(x/lengthQuaternion, y/lengthQuaternion, z/lengthQuaternion, w/lengthQuaternion);
|
||||
}
|
||||
else {
|
||||
// Throw an exception because it's impossible to compute a unit quaternion if its length is equal to zero
|
||||
throw MathematicsException("MathematicsException : Impossible to compute the unit quaternion if the length of the quaternion is zero");
|
||||
}
|
||||
}
|
||||
|
||||
// Return the conjugate of the quaternion (inline)
|
||||
inline Quaternion Quaternion::getConjugate() const {
|
||||
return Quaternion(-x, -y, -z, w);
|
||||
}
|
||||
|
||||
// Return the inverse of the quaternion (inline)
|
||||
inline Quaternion Quaternion::getInverse() const throw(MathematicsException) {
|
||||
double lengthQuaternion = length();
|
||||
lengthQuaternion = lengthQuaternion * lengthQuaternion;
|
||||
|
||||
// Check if the length is not equal to zero
|
||||
if (lengthQuaternion != 0.0) {
|
||||
|
||||
// Compute and return the inverse quaternion
|
||||
return Quaternion(-x/lengthQuaternion, -y/lengthQuaternion, -z/lengthQuaternion, w/lengthQuaternion);
|
||||
}
|
||||
else {
|
||||
// Throw an exception because the inverse cannot be computed
|
||||
throw MathematicsException("MathematicsException : Impossible to compute the inverse of the quaternion because it's length is zero");
|
||||
}
|
||||
}
|
||||
|
||||
// Overloaded operator for the addition of two quaternions
|
||||
inline Quaternion Quaternion::operator+(const Quaternion& quaternion) const {
|
||||
// Return the result quaternion
|
||||
return Quaternion(x + quaternion.x, y + quaternion.y, z + quaternion.z, w + quaternion.w);
|
||||
}
|
||||
|
||||
// Overloaded operator for the substraction of two quaternions
|
||||
inline Quaternion Quaternion::operator-(const Quaternion& quaternion) const {
|
||||
// Return the result of the substraction
|
||||
return Quaternion(x-quaternion.x, y - quaternion.y, z - quaternion.z, w - quaternion.w);
|
||||
}
|
||||
|
||||
// Overloaded operator for the multiplication with a constant
|
||||
inline Quaternion Quaternion::operator*(double nb) const {
|
||||
// Return the result
|
||||
return Quaternion(nb*x, nb*y, nb*z, nb*w);
|
||||
}
|
||||
|
||||
// Overloaded operator for the multiplication of two quaternions
|
||||
inline Quaternion Quaternion::operator*(const Quaternion& quaternion) const {
|
||||
// Return the result of the multiplication
|
||||
return Quaternion(w*quaternion.w - vectorV().scalarProduct(quaternion.vectorV()), w*quaternion.vectorV()+quaternion.w*vectorV() + vectorV().crossProduct(quaternion.vectorV()));
|
||||
}
|
||||
|
||||
// Overloaded operator for the assignment
|
||||
inline Quaternion& Quaternion::operator=(const Quaternion& quaternion) {
|
||||
// Check for self-assignment
|
||||
if (this != &quaternion) {
|
||||
x = quaternion.x;
|
||||
y = quaternion.y;
|
||||
z = quaternion.z;
|
||||
w = quaternion.w;
|
||||
}
|
||||
|
||||
// Return this quaternion
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Overloaded operator for equality condition
|
||||
inline bool Quaternion::operator==(const Quaternion& quaternion) const {
|
||||
return (x == quaternion.x && y == quaternion.y && z == quaternion.z && w == quaternion.w);
|
||||
}
|
||||
|
||||
} // End of the ReactPhysics3D namespace
|
||||
|
||||
#endif
|
223
sources/reactphysics3d/Vector.cpp
Normal file
223
sources/reactphysics3d/Vector.cpp
Normal file
|
@ -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 <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
// Libraries
|
||||
#include "Vector.h"
|
||||
|
||||
// Namespaces
|
||||
using namespace reactphysics3d;
|
||||
|
||||
// Constructor of the class Vector
|
||||
Vector::Vector(int n) throw(std::invalid_argument) {
|
||||
// Check the argument
|
||||
if (n > 0) {
|
||||
// Create the array that contains the values of the vector
|
||||
nbComponent = n;
|
||||
tab = new double[nbComponent];
|
||||
|
||||
|
||||
// Fill the array with zero's value
|
||||
for(int i=0; i<nbComponent; ++i) {
|
||||
tab[i] = 0.0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Throw an exception because of the wrong argument
|
||||
throw std::invalid_argument("Exception : The size of the vector has to be positive !");
|
||||
}
|
||||
}
|
||||
|
||||
// Copy-constructor of the class Vector
|
||||
Vector::Vector(const Vector& vector) {
|
||||
nbComponent = vector.nbComponent;
|
||||
tab = new double[nbComponent];
|
||||
|
||||
// Fill the array with the value of the vector
|
||||
for (int i=0; i<nbComponent; ++i) {
|
||||
tab[i] = vector.tab[i];
|
||||
}
|
||||
}
|
||||
|
||||
// Destructor of the class Vector
|
||||
Vector::~Vector() {
|
||||
// Erase the array with the values of the vector
|
||||
delete [] tab;
|
||||
}
|
||||
|
||||
// Return the corresponding unit vector
|
||||
Vector Vector::getUnit() const throw(MathematicsException) {
|
||||
double lengthVector = length();
|
||||
|
||||
// Check if the length of the vector is equal to zero
|
||||
if (lengthVector!= 0) {
|
||||
double lengthInv = 1.0 / lengthVector;
|
||||
Vector unitVector(nbComponent);
|
||||
|
||||
// Compute the unit vector
|
||||
for(int i=0; i<nbComponent; ++i) {
|
||||
unitVector.setValue(i, getValue(i) * lengthInv);
|
||||
}
|
||||
|
||||
// Return the unit vector
|
||||
return unitVector;
|
||||
|
||||
}
|
||||
else {
|
||||
// Throw an exception because the length of the vector is zero
|
||||
throw MathematicsException("MathematicsException : Impossible to compute the unit vector because the length of the vector is zero");
|
||||
}
|
||||
}
|
||||
|
||||
// Method to compute the scalar product of two vectors
|
||||
double Vector::scalarProduct(const Vector& vector) const throw(MathematicsException) {
|
||||
// Check the sizes of the two vectors
|
||||
if (nbComponent == vector.nbComponent) {
|
||||
double result = 0.0;
|
||||
|
||||
// Compute the scalar product
|
||||
for (int i=0; i<nbComponent; ++i) {
|
||||
result = result + vector.tab[i] * tab[i];
|
||||
}
|
||||
|
||||
// Return the result of the scalar product
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
// Throw an exception because the two vectors haven't the same size
|
||||
throw MathematicsException("MathematicsException : Impossible to compute the scalar product because the vectors haven't the same size");
|
||||
}
|
||||
}
|
||||
|
||||
// Method to compute the cross product of two vectors
|
||||
Vector Vector::crossProduct(const Vector& vector) const throw(MathematicsException) {
|
||||
// Check if the vectors have 3 components
|
||||
if (nbComponent == 3 && vector.nbComponent == 3) {
|
||||
Vector result(3);
|
||||
|
||||
// Compute the cross product
|
||||
result.tab[0] = tab[1] * vector.tab[2] - tab[2] * vector.tab[1];
|
||||
result.tab[1] = tab[2] * vector.tab[0] - tab[0] * vector.tab[2];
|
||||
result.tab[2] = tab[0] * vector.tab[1] - tab[1] * vector.tab[0];
|
||||
|
||||
// Return the result of the cross product
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
// Throw an exception because the vectors haven't three components
|
||||
throw MathematicsException("MathematicsException : Impossible to compute the cross product because the vectors haven't 3 components");
|
||||
}
|
||||
}
|
||||
|
||||
// Overloaded operator for addition
|
||||
Vector Vector::operator+(const Vector& vector) const throw(MathematicsException) {
|
||||
// Check the size of the two vectors
|
||||
if (nbComponent == vector.nbComponent) {
|
||||
Vector sum(nbComponent);
|
||||
|
||||
// Compute the sum of the two vectors
|
||||
for (int i=0; i<nbComponent; ++i) {
|
||||
sum.setValue(i, vector.tab[i] + tab[i]);
|
||||
}
|
||||
|
||||
// Return the sum vector
|
||||
return sum;
|
||||
}
|
||||
else {
|
||||
// Throw an exception because the sizes of the two vectors aren't the same
|
||||
throw MathematicsException("MathematicsException : Impossible two sum the two vectors because the sizes aren't the same !");
|
||||
}
|
||||
}
|
||||
|
||||
// Overloaded operator for substraction
|
||||
Vector Vector::operator-(const Vector& vector) const throw(MathematicsException) {
|
||||
// Check the size of the two vectors
|
||||
if (nbComponent == vector.nbComponent) {
|
||||
Vector substraction(nbComponent);
|
||||
|
||||
// Compute the subraction of the two vectors
|
||||
for (int i=0; i<nbComponent; ++i) {
|
||||
substraction.setValue(i, tab[i] - vector.tab[i]);
|
||||
}
|
||||
|
||||
// Return the subraction vector
|
||||
return substraction;
|
||||
}
|
||||
else {
|
||||
// Throw an exception because the sizes of the two vectors aren't the same
|
||||
throw MathematicsException("MathematicsException : Impossible two substract the two vectors because the sizes aren't the same !");
|
||||
}
|
||||
}
|
||||
|
||||
// Overloaded operator for multiplication with a number
|
||||
Vector Vector::operator*(double number) const {
|
||||
Vector result(nbComponent);
|
||||
|
||||
// Compute the multiplication
|
||||
for (int i=0; i<nbComponent; ++i) {
|
||||
result.setValue(i, number * tab[i]);
|
||||
}
|
||||
|
||||
// Return the result vector
|
||||
return result;
|
||||
}
|
||||
|
||||
// Overloaded operator for assigment to a Vector
|
||||
Vector& Vector::operator=(const Vector& vector) throw(MathematicsException) {
|
||||
|
||||
// Check for self-assignment
|
||||
if (this == &vector) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Check the size of the vectors
|
||||
if (nbComponent == vector.nbComponent) {
|
||||
// Check for self-assignment
|
||||
if (this != &vector) {
|
||||
for (int i=0; i<nbComponent; ++i) {
|
||||
tab[i] = vector.tab[i];
|
||||
}
|
||||
}
|
||||
|
||||
// Return a reference to the vector
|
||||
return *this;
|
||||
}
|
||||
else {
|
||||
// Throw an exception because the sizes of the vectors aren't the same
|
||||
throw MathematicsException("MathematicsException : The assigment to a Vector is impossible because the size of the vectors aren't the same");
|
||||
}
|
||||
}
|
||||
|
||||
// Overloaded operator for the equality condition
|
||||
bool Vector::operator==(const Vector& vector) const throw(MathematicsException) {
|
||||
// Check if the sizes of the vectors are compatible
|
||||
if (nbComponent == vector.nbComponent) {
|
||||
for (int i=0; i<nbComponent; ++i) {
|
||||
if (tab[i] != vector.tab[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
// Throw an exception because the sizes of the vectors aren't the same
|
||||
throw MathematicsException("MathematicsException : Impossible to check if the vectors are equal because they don't have the same size");
|
||||
}
|
||||
}
|
||||
|
116
sources/reactphysics3d/Vector.h
Normal file
116
sources/reactphysics3d/Vector.h
Normal file
|
@ -0,0 +1,116 @@
|
|||
/****************************************************************************
|
||||
* Copyright (C) 2009 Daniel Chappuis *
|
||||
****************************************************************************
|
||||
* This file is part of ReactPhysics3D. *
|
||||
* *
|
||||
* ReactPhysics3D is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation, either version 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* ReactPhysics3D is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef VECTOR_H
|
||||
#define VECTOR_H
|
||||
|
||||
// Libraries
|
||||
#include "exceptions.h"
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
// ReactPhysics3D namespace
|
||||
namespace reactphysics3d {
|
||||
|
||||
/* -------------------------------------------------------------------
|
||||
Class Vector :
|
||||
This class represents a Vector.
|
||||
-------------------------------------------------------------------
|
||||
*/
|
||||
class Vector {
|
||||
private :
|
||||
double* tab; // Array of the vector's components
|
||||
int nbComponent; // number of components in the vector
|
||||
|
||||
public :
|
||||
Vector(int n) throw(std::invalid_argument); // Constructor of the class Vector
|
||||
Vector(const Vector& vector); // Copy-constructor of the class Vector
|
||||
virtual ~Vector(); // Destructor of the class Vector
|
||||
double getValue(int n) const throw(std::invalid_argument); // Get a component of the vector
|
||||
void setValue(int n, double value) throw(std::invalid_argument); // Set the value of a component of the vector
|
||||
int getNbComponent() const; // Get the number of components in the vector
|
||||
double length() const; // Get the length of the vector
|
||||
Vector getUnit() const throw(MathematicsException); // Return the corresponding unit vector
|
||||
double scalarProduct(const Vector& vector) const throw(MathematicsException); // Scalar product of two vectors
|
||||
Vector crossProduct(const Vector& vector) const throw(MathematicsException); // Cross product of two vectors (in 3D only)
|
||||
|
||||
// --- Overloaded operators --- //
|
||||
Vector operator+(const Vector& vector) const throw(MathematicsException); // Overloaded operator for addition
|
||||
Vector operator-(const Vector& vector) const throw(MathematicsException); // Overloaded operator for substraction
|
||||
Vector operator*(double number) const; // Overloaded operator for multiplication with a number
|
||||
Vector& operator=(const Vector& vector) throw(MathematicsException); // Overloaded operator for the assignement to a Vector
|
||||
bool operator==(const Vector& vector) const throw(MathematicsException); // Overloaded operator for the equality condition
|
||||
};
|
||||
|
||||
|
||||
// ------ Definition of inlines functions ------ //
|
||||
|
||||
// Method to get the value of a component of the vector (inline)
|
||||
inline double Vector::getValue(int n) const throw(std::invalid_argument) {
|
||||
// Check the argument
|
||||
if (n>=0 && n<nbComponent) {
|
||||
// Return the value of the component
|
||||
return tab[n];
|
||||
}
|
||||
else {
|
||||
// Throw an exception because of the wrong argument
|
||||
throw std::invalid_argument("The argument is outside the bounds of the Vector");
|
||||
}
|
||||
}
|
||||
|
||||
// Method to set the value of a component of the vector
|
||||
inline void Vector::setValue(int n, double value) throw(std::invalid_argument) {
|
||||
// Check the argument
|
||||
if (n >= 0 && n<nbComponent) {
|
||||
// Set the value
|
||||
tab[n] = value;
|
||||
}
|
||||
else {
|
||||
// Throw an exception because of the wrong argument
|
||||
throw std::invalid_argument("Exception : The argument is outside the bounds of the Vector");
|
||||
}
|
||||
}
|
||||
|
||||
// Method to get the number of components in the vector (inline)
|
||||
inline int Vector::getNbComponent() const {
|
||||
// Return the number of components in the vector
|
||||
return nbComponent;
|
||||
}
|
||||
|
||||
// Method to get the length of the vector
|
||||
inline double Vector::length() const {
|
||||
// Compute the length of the vector
|
||||
double sum = 0.0;
|
||||
for(int i=0; i<nbComponent; ++i) {
|
||||
sum = sum + tab[i] * tab[i];
|
||||
}
|
||||
|
||||
// Return the length of the vector
|
||||
return sqrt(sum);
|
||||
}
|
||||
|
||||
// Overloaded operator for multiplication between a number and a Vector (inline)
|
||||
inline Vector operator*(double number, const Vector& vector) {
|
||||
// Compute and return the result
|
||||
return vector * number;
|
||||
}
|
||||
|
||||
} // End of the ReactPhysics3D namespace
|
||||
|
||||
#endif
|
96
sources/reactphysics3d/Vector3D.cpp
Normal file
96
sources/reactphysics3d/Vector3D.cpp
Normal file
|
@ -0,0 +1,96 @@
|
|||
/****************************************************************************
|
||||
* Copyright (C) 2009 Daniel Chappuis *
|
||||
****************************************************************************
|
||||
* This file is part of ReactPhysics3D. *
|
||||
* *
|
||||
* ReactPhysics3D is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation, either version 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* ReactPhysics3D is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
// Libraries
|
||||
#include "Vector3D.h"
|
||||
#include <iostream>
|
||||
|
||||
// Namespaces
|
||||
using namespace reactphysics3d;
|
||||
|
||||
// Constructor of the class Vector3D
|
||||
Vector3D::Vector3D()
|
||||
:x(0.0), y(0.0), z(0.0) {
|
||||
|
||||
}
|
||||
|
||||
// Constructor with arguments
|
||||
Vector3D::Vector3D(double x, double y, double z)
|
||||
:x(x), y(y), z(z) {
|
||||
|
||||
}
|
||||
|
||||
// Copy-constructor
|
||||
Vector3D::Vector3D(const Vector3D& vector)
|
||||
:x(vector.x), y(vector.y), z(vector.z) {
|
||||
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Vector3D::~Vector3D() {
|
||||
|
||||
}
|
||||
|
||||
// Return the corresponding unit vector
|
||||
Vector3D Vector3D::getUnit() const throw(MathematicsException) {
|
||||
double lengthVector = length();
|
||||
|
||||
// Check if the length is equal to zero
|
||||
if (lengthVector != 0) {
|
||||
// Compute and return the unit vector
|
||||
double lengthInv = 1.0 / lengthVector;
|
||||
return Vector3D(x * lengthInv, y * lengthInv, z*lengthInv);
|
||||
}
|
||||
else {
|
||||
// Throw an exception because the length of the vector is zero
|
||||
throw MathematicsException("MathematicsException : Impossible to compute the unit vector because the length of the vector is zero");
|
||||
}
|
||||
}
|
||||
|
||||
// Overloaded operator for addition
|
||||
Vector3D Vector3D::operator+(const Vector3D& vector) const {
|
||||
// Compute and return the sum of the two vectors
|
||||
return Vector3D(x + vector.x, y + vector.y, z + vector.z);
|
||||
}
|
||||
|
||||
// Overloaded operator for substraction
|
||||
Vector3D Vector3D::operator-(const Vector3D& vector) const {
|
||||
// Compute and return the substraction of the two vectors
|
||||
return Vector3D(x - vector.x, y - vector.y, z - vector.z);
|
||||
}
|
||||
|
||||
// Overloaded operator for multiplication with a number
|
||||
Vector3D Vector3D::operator*(double number) const {
|
||||
// Compute and return the result
|
||||
return Vector3D(x * number, y * number, z * number);
|
||||
}
|
||||
|
||||
// Overloaded operator for the assignement to a Vector
|
||||
Vector3D& Vector3D::operator=(const Vector3D& vector) {
|
||||
// Check for self-assignment
|
||||
if (this != &vector) {
|
||||
// Copy the vector
|
||||
x = vector.x;
|
||||
y = vector.y;
|
||||
z = vector.z;
|
||||
}
|
||||
|
||||
// Return a reference to the vector
|
||||
return *this;
|
||||
}
|
136
sources/reactphysics3d/Vector3D.h
Normal file
136
sources/reactphysics3d/Vector3D.h
Normal file
|
@ -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 <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef VECTOR3D_H
|
||||
#define VECTOR3D_H
|
||||
|
||||
// Libraries
|
||||
#include <cmath>
|
||||
#include "exceptions.h"
|
||||
|
||||
// ReactPhysics3D namespace
|
||||
namespace reactphysics3d {
|
||||
|
||||
|
||||
/* -------------------------------------------------------------------
|
||||
Class Vector3D :
|
||||
This classrepresents 3 dimensionnal vector in space.
|
||||
-------------------------------------------------------------------
|
||||
*/
|
||||
class Vector3D {
|
||||
private :
|
||||
double x; // X component of the vector
|
||||
double y; // Y component of the vector
|
||||
double z; // Z component of the vector
|
||||
|
||||
public :
|
||||
Vector3D(); // Constructor of the class Vector3D
|
||||
Vector3D(double x, double y, double z); // Constructor with arguments
|
||||
Vector3D(const Vector3D& vector); // Copy-constructor
|
||||
virtual ~Vector3D(); // Destructor
|
||||
double getX() const; // Get the x component of the vector
|
||||
double getY() const; // Get the y component of the vector
|
||||
double getZ() const; // Get the z component of the vector
|
||||
void setX(double x); // Set the x component of the vector
|
||||
void setY(double y); // Set the y component of the vector
|
||||
void setZ(double z); // Set the z component of the vector
|
||||
void setAllValues(double x, double y, double z); // Set all the values of the vector
|
||||
double length() const; // Return the lenght of the vector
|
||||
Vector3D getUnit() const throw(MathematicsException); // Return the corresponding unit vector
|
||||
double scalarProduct(const Vector3D& vector) const; // Scalar product of two vectors
|
||||
Vector3D crossProduct(const Vector3D& vector) const; // Cross product of two vectors
|
||||
|
||||
// --- Overloaded operators --- //
|
||||
Vector3D operator+(const Vector3D& vector) const; // Overloaded operator for addition
|
||||
Vector3D operator-(const Vector3D& vector) const ; // Overloaded operator for substraction
|
||||
Vector3D operator*(double number) const; // Overloaded operator for multiplication with a number
|
||||
Vector3D& operator=(const Vector3D& vector); // Overloaded operator for the assignement to a Vector
|
||||
bool operator==(const Vector3D& vector) const; // Overloaded operator for the equality condition
|
||||
};
|
||||
|
||||
|
||||
// Get the x component of the vector (inline)
|
||||
inline double Vector3D::getX() const {
|
||||
return x;
|
||||
}
|
||||
|
||||
// Get the y component of the vector (inline)
|
||||
inline double Vector3D::getY() const {
|
||||
return y;
|
||||
}
|
||||
|
||||
// Get the z component of the vector (inline)
|
||||
inline double Vector3D::getZ() const {
|
||||
return z;
|
||||
}
|
||||
|
||||
// Set the x component of the vector (inline)
|
||||
inline void Vector3D::setX(double x) {
|
||||
this->x = x;
|
||||
}
|
||||
|
||||
// Set the y component of the vector (inline)
|
||||
inline void Vector3D::setY(double y) {
|
||||
this->y = y;
|
||||
}
|
||||
|
||||
// Set the z component of the vector (inline)
|
||||
inline void Vector3D::setZ(double z) {
|
||||
this->z = z;
|
||||
}
|
||||
|
||||
// Set all the values of the vector (inline)
|
||||
inline void Vector3D::setAllValues(double x, double y, double z) {
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
this->z = z;
|
||||
}
|
||||
|
||||
// Return the length of the vector (inline)
|
||||
inline double Vector3D::length() const {
|
||||
// Compute and return the length of the vector
|
||||
return sqrt(x*x + y*y + z*z);
|
||||
}
|
||||
|
||||
// Scalar product of two vectors (inline)
|
||||
inline double Vector3D::scalarProduct(const Vector3D& vector) const {
|
||||
// Compute and return the result of the scalar product
|
||||
return (x * vector.x + y * vector.y + z * vector.z);
|
||||
}
|
||||
|
||||
// Cross product of two vectors (inline)
|
||||
inline Vector3D Vector3D::crossProduct(const Vector3D& vector) const {
|
||||
// Compute and return the cross product
|
||||
return Vector3D(y * vector.z - z * vector.y, z * vector.x - x * vector.z , x * vector.y - y * vector.x);
|
||||
}
|
||||
|
||||
// Overloaded operator for multiplication between a number and a Vector3D (inline)
|
||||
inline Vector3D operator * (double number, const Vector3D& vector) {
|
||||
// Compute and return the result vector
|
||||
return vector * number;
|
||||
}
|
||||
|
||||
// Overloaded operator for the equality condition
|
||||
inline bool Vector3D::operator == (const Vector3D& vector) const {
|
||||
return (x == vector.x && y == vector.y && z == vector.z);
|
||||
}
|
||||
|
||||
} // End of the ReactPhysics3D namespace
|
||||
|
||||
#endif
|
58
sources/reactphysics3d/exceptions.cpp
Executable file
58
sources/reactphysics3d/exceptions.cpp
Executable file
|
@ -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 <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
// Libraries
|
||||
#include "exceptions.h"
|
||||
|
||||
// Namespaces
|
||||
using namespace reactphysics3d;
|
||||
|
||||
|
||||
// Constructor of the MathematicsException class
|
||||
MathematicsException::MathematicsException(const std::string& msg)
|
||||
:std::runtime_error(msg) {
|
||||
|
||||
}
|
||||
|
||||
// Destructor of the MathException class
|
||||
MathematicsException::~MathematicsException() throw() {
|
||||
|
||||
}
|
||||
|
||||
// Overriden exception base class method
|
||||
const char* MathematicsException::what() const throw() {
|
||||
return std::runtime_error::what();
|
||||
}
|
||||
|
||||
// Constructor of the DivisionByZeroException class
|
||||
DivisionByZeroException::DivisionByZeroException(const std::string& msg)
|
||||
:MathematicsException(msg) {
|
||||
|
||||
}
|
||||
|
||||
// Destructor of the DivisionByZeroException class
|
||||
DivisionByZeroException::~DivisionByZeroException() throw() {
|
||||
|
||||
}
|
||||
|
||||
// Overriden exception base class method
|
||||
const char* DivisionByZeroException::what() const throw() {
|
||||
return MathematicsException::what();
|
||||
}
|
||||
|
52
sources/reactphysics3d/exceptions.h
Executable file
52
sources/reactphysics3d/exceptions.h
Executable file
|
@ -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 <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef EXCEPTIONS_H
|
||||
#define EXCEPTIONS_H
|
||||
|
||||
// Libraries
|
||||
#include <stdexcept>
|
||||
|
||||
// ReactPhysics3D namespace
|
||||
namespace reactphysics3d {
|
||||
|
||||
/* -------------------------------------------------------------------
|
||||
Exception class for the mathematics library
|
||||
-------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
// Class MathematicsException
|
||||
class MathematicsException : public std::runtime_error {
|
||||
public:
|
||||
MathematicsException(const std::string& msg="MathException"); // Constructor
|
||||
virtual ~MathematicsException() throw(); // Destructor
|
||||
virtual const char* what() const throw(); // Overriding the base exception method
|
||||
};
|
||||
|
||||
// Class DivisionByZeroException
|
||||
class DivisionByZeroException : public MathematicsException {
|
||||
public:
|
||||
DivisionByZeroException(const std::string& msg="DivisionByZeroException : Division by zero !"); // Constructor
|
||||
virtual ~DivisionByZeroException() throw(); // Destructor
|
||||
virtual const char* what() const throw(); // Overriding the base exception method
|
||||
};
|
||||
|
||||
} // End of the ReactPhysics3D namespace
|
||||
|
||||
#endif
|
25
sources/reactphysics3d/main.cpp
Executable file
25
sources/reactphysics3d/main.cpp
Executable file
|
@ -0,0 +1,25 @@
|
|||
|
||||
#include "mathematics.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
|
||||
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();
|
||||
}
|
34
sources/reactphysics3d/mathematics.h
Normal file
34
sources/reactphysics3d/mathematics.h
Normal file
|
@ -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 <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
// TODO : Mathematics library : We have to use assert to debug
|
||||
// TODO : Mathematics library : Check everywhere that in member methods we use attributes access instead of getter and setter.
|
||||
|
||||
#ifndef DCMATHS_H
|
||||
#define DCMATHS_H
|
||||
|
||||
// Libraries
|
||||
#include "Matrix.h"
|
||||
#include "Matrix3x3.h"
|
||||
#include "Quaternion.h"
|
||||
#include "Vector.h"
|
||||
#include "Vector3D.h"
|
||||
#include "exceptions.h"
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user