Add Vector2 and Matrix2x3 classes
This commit is contained in:
parent
b87f981827
commit
7a2c2bdbd5
89
src/mathematics/Matrix2x2.cpp
Normal file
89
src/mathematics/Matrix2x2.cpp
Normal file
|
@ -0,0 +1,89 @@
|
|||
/********************************************************************************
|
||||
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
||||
* Copyright (c) 2010-2013 Daniel Chappuis *
|
||||
*********************************************************************************
|
||||
* *
|
||||
* This software is provided 'as-is', without any express or implied warranty. *
|
||||
* In no event will the authors be held liable for any damages arising from the *
|
||||
* use of this software. *
|
||||
* *
|
||||
* Permission is granted to anyone to use this software for any purpose, *
|
||||
* including commercial applications, and to alter it and redistribute it *
|
||||
* freely, subject to the following restrictions: *
|
||||
* *
|
||||
* 1. The origin of this software must not be misrepresented; you must not claim *
|
||||
* that you wrote the original software. If you use this software in a *
|
||||
* product, an acknowledgment in the product documentation would be *
|
||||
* appreciated but is not required. *
|
||||
* *
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be *
|
||||
* misrepresented as being the original software. *
|
||||
* *
|
||||
* 3. This notice may not be removed or altered from any source distribution. *
|
||||
* *
|
||||
********************************************************************************/
|
||||
|
||||
// Libraries
|
||||
#include "Matrix2x2.h"
|
||||
|
||||
using namespace reactphysics3d;
|
||||
|
||||
// Constructor of the class Matrix2x2
|
||||
Matrix2x2::Matrix2x2() {
|
||||
|
||||
// Initialize all values in the matrix to zero
|
||||
setAllValues(0.0, 0.0, 0.0, 0.0);
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Matrix2x2::Matrix2x2(decimal value) {
|
||||
setAllValues(value, value, value, value);
|
||||
}
|
||||
|
||||
// Constructor with arguments
|
||||
Matrix2x2::Matrix2x2(decimal a1, decimal a2, decimal b1, decimal b2) {
|
||||
|
||||
// Initialize the matrix with the values
|
||||
setAllValues(a1, a2, b1, b2);
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Matrix2x2::~Matrix2x2() {
|
||||
|
||||
}
|
||||
|
||||
// Copy-constructor
|
||||
Matrix2x2::Matrix2x2(const Matrix2x2& matrix) {
|
||||
setAllValues(matrix.mRows[0][0], matrix.mRows[0][1],
|
||||
matrix.mRows[1][0], matrix.mRows[1][1]);
|
||||
}
|
||||
|
||||
// Assignment operator
|
||||
Matrix2x2& Matrix2x2::operator=(const Matrix2x2& matrix) {
|
||||
|
||||
// Check for self-assignment
|
||||
if (&matrix != this) {
|
||||
setAllValues(matrix.mRows[0][0], matrix.mRows[0][1],
|
||||
matrix.mRows[1][0], matrix.mRows[1][1]);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Return the inverse matrix
|
||||
Matrix2x2 Matrix2x2::getInverse() const {
|
||||
|
||||
// Compute the determinant of the matrix
|
||||
decimal determinant = getDeterminant();
|
||||
|
||||
// Check if the determinant is equal to zero
|
||||
assert(std::abs(determinant) > MACHINE_EPSILON);
|
||||
|
||||
decimal invDeterminant = decimal(1.0) / determinant;
|
||||
|
||||
// TODO : Implement this
|
||||
assert(false);
|
||||
Matrix2x2 tempMatrix;
|
||||
|
||||
// Return the inverse matrix
|
||||
return (invDeterminant * tempMatrix);
|
||||
}
|
312
src/mathematics/Matrix2x2.h
Normal file
312
src/mathematics/Matrix2x2.h
Normal file
|
@ -0,0 +1,312 @@
|
|||
/********************************************************************************
|
||||
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
||||
* Copyright (c) 2010-2013 Daniel Chappuis *
|
||||
*********************************************************************************
|
||||
* *
|
||||
* This software is provided 'as-is', without any express or implied warranty. *
|
||||
* In no event will the authors be held liable for any damages arising from the *
|
||||
* use of this software. *
|
||||
* *
|
||||
* Permission is granted to anyone to use this software for any purpose, *
|
||||
* including commercial applications, and to alter it and redistribute it *
|
||||
* freely, subject to the following restrictions: *
|
||||
* *
|
||||
* 1. The origin of this software must not be misrepresented; you must not claim *
|
||||
* that you wrote the original software. If you use this software in a *
|
||||
* product, an acknowledgment in the product documentation would be *
|
||||
* appreciated but is not required. *
|
||||
* *
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be *
|
||||
* misrepresented as being the original software. *
|
||||
* *
|
||||
* 3. This notice may not be removed or altered from any source distribution. *
|
||||
* *
|
||||
********************************************************************************/
|
||||
|
||||
#ifndef REACTPHYSICS3D_MATRIX2X2_H
|
||||
#define REACTPHYSICS3D_MATRIX2X2_H
|
||||
|
||||
// Libraries
|
||||
#include <cassert>
|
||||
#include "Vector2.h"
|
||||
|
||||
/// ReactPhysics3D namespace
|
||||
namespace reactphysics3d {
|
||||
|
||||
// Class Matrix2x2
|
||||
/**
|
||||
* This class represents a 2x2 matrix.
|
||||
*/
|
||||
class Matrix2x2 {
|
||||
|
||||
private :
|
||||
|
||||
// -------------------- Attributes -------------------- //
|
||||
|
||||
/// Rows of the matrix;
|
||||
Vector2 mRows[3];
|
||||
|
||||
public :
|
||||
|
||||
// -------------------- Methods -------------------- //
|
||||
|
||||
/// Constructor
|
||||
Matrix2x2();
|
||||
|
||||
/// Constructor
|
||||
Matrix2x2(decimal value);
|
||||
|
||||
/// Constructor
|
||||
Matrix2x2(decimal a1, decimal a2, decimal b1, decimal b2);
|
||||
|
||||
/// Destructor
|
||||
virtual ~Matrix2x2();
|
||||
|
||||
/// Copy-constructor
|
||||
Matrix2x2(const Matrix2x2& matrix);
|
||||
|
||||
/// Assignment operator
|
||||
Matrix2x2& operator=(const Matrix2x2& matrix);
|
||||
|
||||
/// Set all the values in the matrix
|
||||
void setAllValues(decimal a1, decimal a2, decimal b1, decimal b2);
|
||||
|
||||
/// Set the matrix to zero
|
||||
void setToZero();
|
||||
|
||||
/// Return a column
|
||||
Vector2 getColumn(int i) const;
|
||||
|
||||
/// Return a row
|
||||
Vector2 getRow(int i) const;
|
||||
|
||||
/// Return the transpose matrix
|
||||
Matrix2x2 getTranspose() const;
|
||||
|
||||
/// Return the determinant of the matrix
|
||||
decimal getDeterminant() const;
|
||||
|
||||
/// Return the trace of the matrix
|
||||
decimal getTrace() const;
|
||||
|
||||
/// Return the inverse matrix
|
||||
Matrix2x2 getInverse() const;
|
||||
|
||||
/// Return the matrix with absolute values
|
||||
Matrix2x2 getAbsoluteMatrix() const;
|
||||
|
||||
/// Set the matrix to the identity matrix
|
||||
void setToIdentity();
|
||||
|
||||
/// Return the 2x2 identity matrix
|
||||
static Matrix2x2 identity();
|
||||
|
||||
/// Overloaded operator for addition
|
||||
friend Matrix2x2 operator+(const Matrix2x2& matrix1, const Matrix2x2& matrix2);
|
||||
|
||||
/// Overloaded operator for substraction
|
||||
friend Matrix2x2 operator-(const Matrix2x2& matrix1, const Matrix2x2& matrix2);
|
||||
|
||||
/// Overloaded operator for the negative of the matrix
|
||||
friend Matrix2x2 operator-(const Matrix2x2& matrix);
|
||||
|
||||
/// Overloaded operator for multiplication with a number
|
||||
friend Matrix2x2 operator*(decimal nb, const Matrix2x2& matrix);
|
||||
|
||||
/// Overloaded operator for multiplication with a matrix
|
||||
friend Matrix2x2 operator*(const Matrix2x2& matrix, decimal nb);
|
||||
|
||||
/// Overloaded operator for matrix multiplication
|
||||
friend Matrix2x2 operator*(const Matrix2x2& matrix1, const Matrix2x2& matrix2);
|
||||
|
||||
/// Overloaded operator for multiplication with a vector
|
||||
friend Vector2 operator*(const Matrix2x2& matrix, const Vector2& vector);
|
||||
|
||||
/// Overloaded operator for equality condition
|
||||
bool operator==(const Matrix2x2& matrix) const;
|
||||
|
||||
/// Overloaded operator for the is different condition
|
||||
bool operator!= (const Matrix2x2& matrix) const;
|
||||
|
||||
/// Overloaded operator for addition with assignment
|
||||
Matrix2x2& operator+=(const Matrix2x2& matrix);
|
||||
|
||||
/// Overloaded operator for substraction with assignment
|
||||
Matrix2x2& operator-=(const Matrix2x2& matrix);
|
||||
|
||||
/// Overloaded operator for multiplication with a number with assignment
|
||||
Matrix2x2& operator*=(decimal nb);
|
||||
|
||||
/// Overloaded operator to read element of the matrix.
|
||||
const Vector2& operator[](int row) const;
|
||||
|
||||
/// Overloaded operator to read/write element of the matrix.
|
||||
Vector2& operator[](int row);
|
||||
};
|
||||
|
||||
// Method to set all the values in the matrix
|
||||
inline void Matrix2x2::setAllValues(decimal a1, decimal a2,
|
||||
decimal b1, decimal b2) {
|
||||
mRows[0][0] = a1; mRows[0][1] = a2;
|
||||
mRows[1][0] = b1; mRows[1][1] = b2;
|
||||
}
|
||||
|
||||
// Set the matrix to zero
|
||||
inline void Matrix2x2::setToZero() {
|
||||
mRows[0].setToZero();
|
||||
mRows[1].setToZero();
|
||||
}
|
||||
|
||||
// Return a column
|
||||
inline Vector2 Matrix2x2::getColumn(int i) const {
|
||||
assert(i>= 0 && i<2);
|
||||
return Vector2(mRows[0][i], mRows[1][i]);
|
||||
}
|
||||
|
||||
// Return a row
|
||||
inline Vector2 Matrix2x2::getRow(int i) const {
|
||||
assert(i>= 0 && i<2);
|
||||
return mRows[i];
|
||||
}
|
||||
|
||||
// Return the transpose matrix
|
||||
inline Matrix2x2 Matrix2x2::getTranspose() const {
|
||||
|
||||
// Return the transpose matrix
|
||||
return Matrix2x2(mRows[0][0], mRows[1][0],
|
||||
mRows[0][1], mRows[1][1]);
|
||||
}
|
||||
|
||||
// Return the determinant of the matrix
|
||||
inline decimal Matrix2x2::getDeterminant() const {
|
||||
|
||||
// Compute and return the determinant of the matrix
|
||||
return mRows[0][0] * mRows[1][1] - mRows[1][0] * mRows[0][1];
|
||||
}
|
||||
|
||||
// Return the trace of the matrix
|
||||
inline decimal Matrix2x2::getTrace() const {
|
||||
|
||||
// Compute and return the trace
|
||||
return (mRows[0][0] + mRows[1][1]);
|
||||
}
|
||||
|
||||
// Set the matrix to the identity matrix
|
||||
inline void Matrix2x2::setToIdentity() {
|
||||
mRows[0][0] = 1.0; mRows[0][1] = 0.0;
|
||||
mRows[1][0] = 0.0; mRows[1][1] = 1.0;
|
||||
}
|
||||
|
||||
// Return the 2x2 identity matrix
|
||||
inline Matrix2x2 Matrix2x2::identity() {
|
||||
|
||||
// Return the isdentity matrix
|
||||
return Matrix2x2(1.0, 0.0, 0.0, 1.0);
|
||||
}
|
||||
|
||||
// Return the matrix with absolute values
|
||||
inline Matrix2x2 Matrix2x2::getAbsoluteMatrix() const {
|
||||
return Matrix2x2(fabs(mRows[0][0]), fabs(mRows[0][1]),
|
||||
fabs(mRows[1][0]), fabs(mRows[1][1]));
|
||||
}
|
||||
|
||||
// Overloaded operator for addition
|
||||
inline Matrix2x2 operator+(const Matrix2x2& matrix1, const Matrix2x2& matrix2) {
|
||||
return Matrix2x2(matrix1.mRows[0][0] + matrix2.mRows[0][0],
|
||||
matrix1.mRows[0][1] + matrix2.mRows[0][1],
|
||||
matrix1.mRows[1][0] + matrix2.mRows[1][0],
|
||||
matrix1.mRows[1][1] + matrix2.mRows[1][1]);
|
||||
}
|
||||
|
||||
// Overloaded operator for substraction
|
||||
inline Matrix2x2 operator-(const Matrix2x2& matrix1, const Matrix2x2& matrix2) {
|
||||
return Matrix2x2(matrix1.mRows[0][0] - matrix2.mRows[0][0],
|
||||
matrix1.mRows[0][1] - matrix2.mRows[0][1],
|
||||
matrix1.mRows[1][0] - matrix2.mRows[1][0],
|
||||
matrix1.mRows[1][1] - matrix2.mRows[1][1]);
|
||||
}
|
||||
|
||||
// Overloaded operator for the negative of the matrix
|
||||
inline Matrix2x2 operator-(const Matrix2x2& matrix) {
|
||||
return Matrix2x2(-matrix.mRows[0][0], -matrix.mRows[0][1],
|
||||
-matrix.mRows[1][0], -matrix.mRows[1][1]);
|
||||
}
|
||||
|
||||
// Overloaded operator for multiplication with a number
|
||||
inline Matrix2x2 operator*(decimal nb, const Matrix2x2& matrix) {
|
||||
return Matrix2x2(matrix.mRows[0][0] * nb, matrix.mRows[0][1] * nb,
|
||||
matrix.mRows[1][0] * nb, matrix.mRows[1][1] * nb);
|
||||
}
|
||||
|
||||
// Overloaded operator for multiplication with a matrix
|
||||
inline Matrix2x2 operator*(const Matrix2x2& matrix, decimal nb) {
|
||||
return nb * matrix;
|
||||
}
|
||||
|
||||
// Overloaded operator for matrix multiplication
|
||||
inline Matrix2x2 operator*(const Matrix2x2& matrix1, const Matrix2x2& matrix2) {
|
||||
return Matrix2x2(matrix1.mRows[0][0] * matrix2.mRows[0][0] + matrix1.mRows[0][1] *
|
||||
matrix2.mRows[1][0],
|
||||
matrix1.mRows[0][0] * matrix2.mRows[0][1] + matrix1.mRows[0][1] *
|
||||
matrix2.mRows[1][1],
|
||||
matrix1.mRows[1][0] * matrix2.mRows[0][0] + matrix1.mRows[1][1] *
|
||||
matrix2.mRows[1][0],
|
||||
matrix1.mRows[1][0] * matrix2.mRows[0][1] + matrix1.mRows[1][1] *
|
||||
matrix2.mRows[1][1]);
|
||||
}
|
||||
|
||||
// Overloaded operator for multiplication with a vector
|
||||
inline Vector2 operator*(const Matrix2x2& matrix, const Vector2& vector) {
|
||||
return Vector2(matrix.mRows[0][0]*vector.x + matrix.mRows[0][1]*vector.y,
|
||||
matrix.mRows[1][0]*vector.x + matrix.mRows[1][1]*vector.y);
|
||||
}
|
||||
|
||||
// Overloaded operator for equality condition
|
||||
inline bool Matrix2x2::operator==(const Matrix2x2& matrix) const {
|
||||
return (mRows[0][0] == matrix.mRows[0][0] && mRows[0][1] == matrix.mRows[0][1] &&
|
||||
mRows[1][0] == matrix.mRows[1][0] && mRows[1][1] == matrix.mRows[1][1]);
|
||||
}
|
||||
|
||||
// Overloaded operator for the is different condition
|
||||
inline bool Matrix2x2::operator!= (const Matrix2x2& matrix) const {
|
||||
return !(*this == matrix);
|
||||
}
|
||||
|
||||
// Overloaded operator for addition with assignment
|
||||
inline Matrix2x2& Matrix2x2::operator+=(const Matrix2x2& matrix) {
|
||||
mRows[0][0] += matrix.mRows[0][0]; mRows[0][1] += matrix.mRows[0][1];
|
||||
mRows[1][0] += matrix.mRows[1][0]; mRows[1][1] += matrix.mRows[1][1];
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Overloaded operator for substraction with assignment
|
||||
inline Matrix2x2& Matrix2x2::operator-=(const Matrix2x2& matrix) {
|
||||
mRows[0][0] -= matrix.mRows[0][0]; mRows[0][1] -= matrix.mRows[0][1];
|
||||
mRows[1][0] -= matrix.mRows[1][0]; mRows[1][1] -= matrix.mRows[1][1];
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Overloaded operator for multiplication with a number with assignment
|
||||
inline Matrix2x2& Matrix2x2::operator*=(decimal nb) {
|
||||
mRows[0][0] *= nb; mRows[0][1] *= nb;
|
||||
mRows[1][0] *= nb; mRows[1][1] *= nb;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Overloaded operator to return a row of the matrix.
|
||||
/// This operator is also used to access a matrix value using the syntax
|
||||
/// matrix[row][col].
|
||||
inline const Vector2& Matrix2x2::operator[](int row) const {
|
||||
return mRows[row];
|
||||
}
|
||||
|
||||
// Overloaded operator to return a row of the matrix.
|
||||
/// This operator is also used to access a matrix value using the syntax
|
||||
/// matrix[row][col].
|
||||
inline Vector2& Matrix2x2::operator[](int row) {
|
||||
return mRows[row];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
71
src/mathematics/Vector2.cpp
Normal file
71
src/mathematics/Vector2.cpp
Normal file
|
@ -0,0 +1,71 @@
|
|||
/********************************************************************************
|
||||
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
||||
* Copyright (c) 2010-2013 Daniel Chappuis *
|
||||
*********************************************************************************
|
||||
* *
|
||||
* This software is provided 'as-is', without any express or implied warranty. *
|
||||
* In no event will the authors be held liable for any damages arising from the *
|
||||
* use of this software. *
|
||||
* *
|
||||
* Permission is granted to anyone to use this software for any purpose, *
|
||||
* including commercial applications, and to alter it and redistribute it *
|
||||
* freely, subject to the following restrictions: *
|
||||
* *
|
||||
* 1. The origin of this software must not be misrepresented; you must not claim *
|
||||
* that you wrote the original software. If you use this software in a *
|
||||
* product, an acknowledgment in the product documentation would be *
|
||||
* appreciated but is not required. *
|
||||
* *
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be *
|
||||
* misrepresented as being the original software. *
|
||||
* *
|
||||
* 3. This notice may not be removed or altered from any source distribution. *
|
||||
* *
|
||||
********************************************************************************/
|
||||
|
||||
// Libraries
|
||||
#include "Vector2.h"
|
||||
#include <vector>
|
||||
|
||||
// Namespaces
|
||||
using namespace reactphysics3d;
|
||||
|
||||
// Constructor
|
||||
Vector2::Vector2() : x(0.0), y(0.0) {
|
||||
|
||||
}
|
||||
|
||||
// Constructor with arguments
|
||||
Vector2::Vector2(decimal newX, decimal newY) : x(newX), y(newY) {
|
||||
|
||||
}
|
||||
|
||||
// Copy-constructor
|
||||
Vector2::Vector2(const Vector2& vector) : x(vector.x), y(vector.y) {
|
||||
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Vector2::~Vector2() {
|
||||
|
||||
}
|
||||
|
||||
// Return the corresponding unit vector
|
||||
Vector2 Vector2::getUnit() const {
|
||||
decimal lengthVector = length();
|
||||
|
||||
assert(lengthVector > MACHINE_EPSILON);
|
||||
|
||||
// Compute and return the unit vector
|
||||
decimal lengthInv = decimal(1.0) / lengthVector;
|
||||
return Vector2(x * lengthInv, y * lengthInv);
|
||||
}
|
||||
|
||||
// Return one unit orthogonal vector of the current vector
|
||||
Vector2 Vector2::getOneUnitOrthogonalVector() const {
|
||||
|
||||
decimal l = length();
|
||||
assert(l > MACHINE_EPSILON);
|
||||
|
||||
return Vector2(-y / l, x / l);
|
||||
}
|
296
src/mathematics/Vector2.h
Normal file
296
src/mathematics/Vector2.h
Normal file
|
@ -0,0 +1,296 @@
|
|||
/********************************************************************************
|
||||
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
||||
* Copyright (c) 2010-2013 Daniel Chappuis *
|
||||
*********************************************************************************
|
||||
* *
|
||||
* This software is provided 'as-is', without any express or implied warranty. *
|
||||
* In no event will the authors be held liable for any damages arising from the *
|
||||
* use of this software. *
|
||||
* *
|
||||
* Permission is granted to anyone to use this software for any purpose, *
|
||||
* including commercial applications, and to alter it and redistribute it *
|
||||
* freely, subject to the following restrictions: *
|
||||
* *
|
||||
* 1. The origin of this software must not be misrepresented; you must not claim *
|
||||
* that you wrote the original software. If you use this software in a *
|
||||
* product, an acknowledgment in the product documentation would be *
|
||||
* appreciated but is not required. *
|
||||
* *
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be *
|
||||
* misrepresented as being the original software. *
|
||||
* *
|
||||
* 3. This notice may not be removed or altered from any source distribution. *
|
||||
* *
|
||||
********************************************************************************/
|
||||
|
||||
#ifndef REACTPHYSICS3D_VECTOR2_H
|
||||
#define REACTPHYSICS3D_VECTOR2_H
|
||||
|
||||
// Libraries
|
||||
#include <cmath>
|
||||
#include <cassert>
|
||||
#include "mathematics_functions.h"
|
||||
#include "../decimal.h"
|
||||
|
||||
|
||||
/// ReactPhysics3D namespace
|
||||
namespace reactphysics3d {
|
||||
|
||||
// Class Vector2
|
||||
/**
|
||||
* This class represents a 2D vector.
|
||||
*/
|
||||
struct Vector2 {
|
||||
|
||||
public:
|
||||
|
||||
// -------------------- Attributes -------------------- //
|
||||
|
||||
/// Component x
|
||||
decimal x;
|
||||
|
||||
/// Component y
|
||||
decimal y;
|
||||
|
||||
// -------------------- Methods -------------------- //
|
||||
|
||||
/// Constructor of the class Vector3D
|
||||
Vector2();
|
||||
|
||||
/// Constructor with arguments
|
||||
Vector2(decimal newX, decimal newY);
|
||||
|
||||
/// Copy-constructor
|
||||
Vector2(const Vector2& vector);
|
||||
|
||||
/// Destructor
|
||||
~Vector2();
|
||||
|
||||
/// Set all the values of the vector
|
||||
void setAllValues(decimal newX, decimal newY);
|
||||
|
||||
/// Set the vector to zero
|
||||
void setToZero();
|
||||
|
||||
/// Return the length of the vector
|
||||
decimal length() const;
|
||||
|
||||
/// Return the square of the length of the vector
|
||||
decimal lengthSquare() const;
|
||||
|
||||
/// Return the corresponding unit vector
|
||||
Vector2 getUnit() const;
|
||||
|
||||
/// Return one unit orthogonal vector of the current vector
|
||||
Vector2 getOneUnitOrthogonalVector() const;
|
||||
|
||||
/// Return true if the vector is unit and false otherwise
|
||||
bool isUnit() const;
|
||||
|
||||
/// Return true if the current vector is the zero vector
|
||||
bool isZero() const;
|
||||
|
||||
/// Dot product of two vectors
|
||||
decimal dot(const Vector2& vector) const;
|
||||
|
||||
/// Normalize the vector
|
||||
void normalize();
|
||||
|
||||
/// Return the corresponding absolute value vector
|
||||
Vector2 getAbsoluteVector() const;
|
||||
|
||||
/// Return the axis with the minimal value
|
||||
int getMinAxis() const;
|
||||
|
||||
/// Return the axis with the maximal value
|
||||
int getMaxAxis() const;
|
||||
|
||||
/// Overloaded operator for the equality condition
|
||||
bool operator== (const Vector2& vector) const;
|
||||
|
||||
/// Overloaded operator for the is different condition
|
||||
bool operator!= (const Vector2& vector) const;
|
||||
|
||||
/// Overloaded operator for addition with assignment
|
||||
Vector2& operator+=(const Vector2& vector);
|
||||
|
||||
/// Overloaded operator for substraction with assignment
|
||||
Vector2& operator-=(const Vector2& vector);
|
||||
|
||||
/// Overloaded operator for multiplication with a number with assignment
|
||||
Vector2& operator*=(decimal number);
|
||||
|
||||
/// Overloaded operator for division by a number with assignment
|
||||
Vector2& operator/=(decimal number);
|
||||
|
||||
/// Overloaded operator for value access
|
||||
decimal& operator[] (int index);
|
||||
|
||||
/// Overloaded operator for value access
|
||||
const decimal& operator[] (int index) const;
|
||||
|
||||
/// Overloaded operator
|
||||
Vector2& operator=(const Vector2& vector);
|
||||
|
||||
// -------------------- Friends -------------------- //
|
||||
|
||||
friend Vector2 operator+(const Vector2& vector1, const Vector2& vector2);
|
||||
friend Vector2 operator-(const Vector2& vector1, const Vector2& vector2);
|
||||
friend Vector2 operator-(const Vector2& vector);
|
||||
friend Vector2 operator*(const Vector2& vector, decimal number);
|
||||
friend Vector2 operator*(decimal number, const Vector2& vector);
|
||||
friend Vector2 operator/(const Vector2& vector, decimal number);
|
||||
};
|
||||
|
||||
// Set the vector to zero
|
||||
inline void Vector2::setToZero() {
|
||||
x = 0;
|
||||
y = 0;
|
||||
}
|
||||
|
||||
// Set all the values of the vector
|
||||
inline void Vector2::setAllValues(decimal newX, decimal newY) {
|
||||
x = newX;
|
||||
y = newY;
|
||||
}
|
||||
|
||||
// Return the length of the vector
|
||||
inline decimal Vector2::length() const {
|
||||
return sqrt(x*x + y*y);
|
||||
}
|
||||
|
||||
// Return the square of the length of the vector
|
||||
inline decimal Vector2::lengthSquare() const {
|
||||
return x*x + y*y;
|
||||
}
|
||||
|
||||
// Scalar product of two vectors (inline)
|
||||
inline decimal Vector2::dot(const Vector2& vector) const {
|
||||
return (x*vector.x + y*vector.y);
|
||||
}
|
||||
|
||||
// Normalize the vector
|
||||
inline void Vector2::normalize() {
|
||||
decimal l = length();
|
||||
assert(l > std::numeric_limits<decimal>::epsilon());
|
||||
x /= l;
|
||||
y /= l;
|
||||
}
|
||||
|
||||
// Return the corresponding absolute value vector
|
||||
inline Vector2 Vector2::getAbsoluteVector() const {
|
||||
return Vector2(std::abs(x), std::abs(y));
|
||||
}
|
||||
|
||||
// Return the axis with the minimal value
|
||||
inline int Vector2::getMinAxis() const {
|
||||
return (x < y ? 0 : 1);
|
||||
}
|
||||
|
||||
// Return the axis with the maximal value
|
||||
inline int Vector2::getMaxAxis() const {
|
||||
return (x < y ? 1 : 0);
|
||||
}
|
||||
|
||||
// Return true if the vector is unit and false otherwise
|
||||
inline bool Vector2::isUnit() const {
|
||||
return approxEqual(lengthSquare(), 1.0);
|
||||
}
|
||||
|
||||
// Return true if the vector is the zero vector
|
||||
inline bool Vector2::isZero() const {
|
||||
return approxEqual(lengthSquare(), 0.0);
|
||||
}
|
||||
|
||||
// Overloaded operator for the equality condition
|
||||
inline bool Vector2::operator== (const Vector2& vector) const {
|
||||
return (x == vector.x && y == vector.y);
|
||||
}
|
||||
|
||||
// Overloaded operator for the is different condition
|
||||
inline bool Vector2::operator!= (const Vector2& vector) const {
|
||||
return !(*this == vector);
|
||||
}
|
||||
|
||||
// Overloaded operator for addition with assignment
|
||||
inline Vector2& Vector2::operator+=(const Vector2& vector) {
|
||||
x += vector.x;
|
||||
y += vector.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Overloaded operator for substraction with assignment
|
||||
inline Vector2& Vector2::operator-=(const Vector2& vector) {
|
||||
x -= vector.x;
|
||||
y -= vector.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Overloaded operator for multiplication with a number with assignment
|
||||
inline Vector2& Vector2::operator*=(decimal number) {
|
||||
x *= number;
|
||||
y *= number;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Overloaded operator for division by a number with assignment
|
||||
inline Vector2& Vector2::operator/=(decimal number) {
|
||||
assert(number > std::numeric_limits<decimal>::epsilon());
|
||||
x /= number;
|
||||
y /= number;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Overloaded operator for value access
|
||||
inline decimal& Vector2::operator[] (int index) {
|
||||
return (&x)[index];
|
||||
}
|
||||
|
||||
// Overloaded operator for value access
|
||||
inline const decimal& Vector2::operator[] (int index) const {
|
||||
return (&x)[index];
|
||||
}
|
||||
|
||||
// Overloaded operator for addition
|
||||
inline Vector2 operator+(const Vector2& vector1, const Vector2& vector2) {
|
||||
return Vector2(vector1.x + vector2.x, vector1.y + vector2.y);
|
||||
}
|
||||
|
||||
// Overloaded operator for substraction
|
||||
inline Vector2 operator-(const Vector2& vector1, const Vector2& vector2) {
|
||||
return Vector2(vector1.x - vector2.x, vector1.y - vector2.y);
|
||||
}
|
||||
|
||||
// Overloaded operator for the negative of a vector
|
||||
inline Vector2 operator-(const Vector2& vector) {
|
||||
return Vector2(-vector.x, -vector.y);
|
||||
}
|
||||
|
||||
// Overloaded operator for multiplication with a number
|
||||
inline Vector2 operator*(const Vector2& vector, decimal number) {
|
||||
return Vector2(number * vector.x, number * vector.y);
|
||||
}
|
||||
|
||||
// Overloaded operator for division by a number
|
||||
inline Vector2 operator/(const Vector2& vector, decimal number) {
|
||||
assert(number > MACHINE_EPSILON);
|
||||
return Vector2(vector.x / number, vector.y / number);
|
||||
}
|
||||
|
||||
// Overloaded operator for multiplication with a number
|
||||
inline Vector2 operator*(decimal number, const Vector2& vector) {
|
||||
return vector * number;
|
||||
}
|
||||
|
||||
// Assignment operator
|
||||
inline Vector2& Vector2::operator=(const Vector2& vector) {
|
||||
if (&vector != this) {
|
||||
x = vector.x;
|
||||
y = vector.y;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
|
@ -111,9 +111,6 @@ struct Vector3 {
|
|||
/// Return the axis with the maximal value
|
||||
int getMaxAxis() const;
|
||||
|
||||
/// Return true if two vectors are parallel
|
||||
bool isParallelWith(const Vector3& vector) const;
|
||||
|
||||
/// Overloaded operator for the equality condition
|
||||
bool operator== (const Vector3& vector) const;
|
||||
|
||||
|
|
239
test/tests/mathematics/TestMatrix2x2.h
Normal file
239
test/tests/mathematics/TestMatrix2x2.h
Normal file
|
@ -0,0 +1,239 @@
|
|||
/********************************************************************************
|
||||
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
||||
* Copyright (c) 2010-2013 Daniel Chappuis *
|
||||
*********************************************************************************
|
||||
* *
|
||||
* This software is provided 'as-is', without any express or implied warranty. *
|
||||
* In no event will the authors be held liable for any damages arising from the *
|
||||
* use of this software. *
|
||||
* *
|
||||
* Permission is granted to anyone to use this software for any purpose, *
|
||||
* including commercial applications, and to alter it and redistribute it *
|
||||
* freely, subject to the following restrictions: *
|
||||
* *
|
||||
* 1. The origin of this software must not be misrepresented; you must not claim *
|
||||
* that you wrote the original software. If you use this software in a *
|
||||
* product, an acknowledgment in the product documentation would be *
|
||||
* appreciated but is not required. *
|
||||
* *
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be *
|
||||
* misrepresented as being the original software. *
|
||||
* *
|
||||
* 3. This notice may not be removed or altered from any source distribution. *
|
||||
* *
|
||||
********************************************************************************/
|
||||
|
||||
#ifndef TEST_MATRIX2X2_H
|
||||
#define TEST_MATRIX2X2_H
|
||||
|
||||
// Libraries
|
||||
#include "../../Test.h"
|
||||
#include "../../../src/mathematics/Matrix2x2.h"
|
||||
|
||||
/// Reactphysics3D namespace
|
||||
namespace reactphysics3d {
|
||||
|
||||
// Class TestMatrix2x2
|
||||
/**
|
||||
* Unit test for the Matrix2x2 class
|
||||
*/
|
||||
class TestMatrix2x2 : public Test {
|
||||
|
||||
private :
|
||||
|
||||
// ---------- Atributes ---------- //
|
||||
|
||||
/// Identity transform
|
||||
Matrix2x2 mIdentity;
|
||||
|
||||
/// First example matrix
|
||||
Matrix2x2 mMatrix1;
|
||||
|
||||
public :
|
||||
|
||||
// ---------- Methods ---------- //
|
||||
|
||||
/// Constructor
|
||||
TestMatrix2x2() : mIdentity(Matrix2x2::identity()),
|
||||
mMatrix1(2, 24, -4, 5) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// Run the tests
|
||||
void run() {
|
||||
testConstructors();
|
||||
testGetSet();
|
||||
testIdentity();
|
||||
testOthersMethods();
|
||||
testOperators();
|
||||
}
|
||||
|
||||
/// Test the constructors
|
||||
void testConstructors() {
|
||||
|
||||
Matrix2x2 test1(5.0);
|
||||
Matrix2x2 test2(2, 3, 4, 5);
|
||||
Matrix2x2 test3(mMatrix1);
|
||||
|
||||
test(test1[0][0] == 5);
|
||||
test(test1[0][1] == 5);
|
||||
test(test1[1][0] == 5);
|
||||
test(test1[1][1] == 5);
|
||||
|
||||
test(test2[0][0] == 2);
|
||||
test(test2[0][1] == 3);
|
||||
test(test2[1][0] == 4);
|
||||
test(test2[1][1] == 5);
|
||||
|
||||
test(test3 == mMatrix1);
|
||||
}
|
||||
|
||||
/// Test the getter and setter methods
|
||||
void testGetSet() {
|
||||
|
||||
// Test method to set all the values
|
||||
Matrix2x2 test2;
|
||||
test2.setAllValues(2, 24, -4, 5);
|
||||
test(test2 == mMatrix1);
|
||||
|
||||
// Test method to set to zero
|
||||
test2.setToZero();
|
||||
test(test2 == Matrix2x2(0, 0, 0, 0));
|
||||
|
||||
// Test method that returns a column
|
||||
Vector2 column1 = mMatrix1.getColumn(0);
|
||||
Vector2 column2 = mMatrix1.getColumn(1);
|
||||
test(column1 == Vector2(2, -4));
|
||||
test(column2 == Vector2(24, 5));
|
||||
|
||||
// Test method that returns a row
|
||||
Vector3 row1 = mMatrix1.getRow(0);
|
||||
Vector3 row2 = mMatrix1.getRow(1);
|
||||
test(row1 == Vector3(2, 24));
|
||||
test(row2 == Vector3(-4, 5));
|
||||
}
|
||||
|
||||
/// Test the identity methods
|
||||
void testIdentity() {
|
||||
|
||||
Matrix2x2 identity = Matrix2x2::identity();
|
||||
Matrix2x2 test1;
|
||||
test1.setToIdentity();
|
||||
|
||||
test(identity[0][0] == 1);
|
||||
test(identity[0][1] == 0);
|
||||
test(identity[1][0] == 0);
|
||||
test(identity[1][1] == 1);
|
||||
|
||||
test(test1 == Matrix2x2::identity());
|
||||
}
|
||||
|
||||
/// Test others methods
|
||||
void testOthersMethods() {
|
||||
|
||||
// Test transpose
|
||||
Matrix2x2 transpose = mMatrix1.getTranspose();
|
||||
test(transpose == Matrix2x2(2, -4, 24, 5));
|
||||
|
||||
// Test trace
|
||||
test(mMatrix1.getTrace() == 10);
|
||||
test(Matrix2x2::identity().getTrace() == 2);
|
||||
|
||||
// Test determinant
|
||||
Matrix2x2 matrix(-24, 64, 253, -35);
|
||||
test(mMatrix1.getDeterminant() == 106);
|
||||
test(matrix.getDeterminant() == -15352);
|
||||
test(mIdentity.getDeterminant() == 1);
|
||||
|
||||
// Test inverse
|
||||
Matrix2x2 inverseMatrix = matrix.getInverse();
|
||||
test(approxEqual(inverseMatrix[0][0], decimal(0.056369), decimal(10e-6)));
|
||||
test(approxEqual(inverseMatrix[0][1], decimal(-0.049549), decimal(10e-6)));
|
||||
test(approxEqual(inverseMatrix[1][0], decimal(0.029460), decimal(10e-6)));
|
||||
test(approxEqual(inverseMatrix[1][1], decimal(0.038575), decimal(10e-6)));
|
||||
Matrix2x2 inverseMatrix1 = mMatrix1.getInverse();
|
||||
test(approxEqual(inverseMatrix1[0][0], decimal(0.030232), decimal(10e-6)));
|
||||
test(approxEqual(inverseMatrix1[0][1], decimal(0.015676), decimal(10e-6)));
|
||||
test(approxEqual(inverseMatrix1[1][0], decimal(-0.057410), decimal(10e-6)));
|
||||
test(approxEqual(inverseMatrix1[1][1], decimal(0.039088), decimal(10e-6)));
|
||||
|
||||
// Test absolute matrix
|
||||
Matrix2x2 matrix2(-2, -3, -4, -5);
|
||||
test(matrix.getAbsoluteMatrix() == Matrix2x2(24, 64, 253, 35));
|
||||
Matrix2x2 absoluteMatrix = matrix2.getAbsoluteMatrix();
|
||||
test(absoluteMatrix == Matrix2x2(2, 3, 4, 5));
|
||||
}
|
||||
|
||||
/// Test the operators
|
||||
void testOperators() {
|
||||
|
||||
// Test addition
|
||||
Matrix2x2 matrix1(2, 3, 4, 5);
|
||||
Matrix2x2 matrix2(-2, 3, -5, 10);
|
||||
Matrix2x2 addition1 = matrix1 + matrix2;
|
||||
Matrix2x2 addition2(matrix1);
|
||||
addition2 += matrix2;
|
||||
test(addition1 == Matrix2x2(0, 6, -1, 15));
|
||||
test(addition2 == Matrix2x2(0, 6, -1, 15));
|
||||
|
||||
// Test substraction
|
||||
Matrix2x2 substraction1 = matrix1 - matrix2;
|
||||
Matrix2x2 substraction2(matrix1);
|
||||
substraction2 -= matrix2;
|
||||
test(substraction1 == Matrix2x2(4, 0, 9, -5));
|
||||
test(substraction2 == Matrix2x2(4, 0, 9, -5));
|
||||
|
||||
// Test negative operator
|
||||
Matrix2x2 negative = -matrix1;
|
||||
test(negative == Matrix2x2(-2, -3, -4, -5));
|
||||
|
||||
// Test multiplication with a number
|
||||
Matrix2x2 multiplication1 = 3 * matrix1;
|
||||
Matrix2x2 multiplication2 = matrix1 * 3;
|
||||
Matrix2x2 multiplication3(matrix1);
|
||||
multiplication3 *= 3;
|
||||
test(multiplication1 == Matrix2x2(6, 9, 12, 15));
|
||||
test(multiplication2 == Matrix2x2(6, 9, 12, 15));
|
||||
test(multiplication3 == Matrix2x2(6, 9, 12, 15));
|
||||
|
||||
// Test multiplication with a matrix
|
||||
Matrix2x2 multiplication4 = matrix1 * matrix2;
|
||||
Matrix2x2 multiplication5 = matrix2 * matrix1;
|
||||
test(multiplication4 == Matrix2x2(-19, 36, -33, 62));
|
||||
test(multiplication5 == Matrix2x2(8, 9, 30, 35));
|
||||
|
||||
// Test multiplication with a vector
|
||||
Vector2 vector1(3, -32);
|
||||
Vector2 vector2(-31, -422);
|
||||
Vector2 test1 = matrix1 * vector1;
|
||||
Vector2 test2 = matrix2 * vector2;
|
||||
test(test1 == Vector2(-762, -182));
|
||||
test(test2 == Vector2(-10190, -1986));
|
||||
|
||||
// Test equality operators
|
||||
test(Matrix2x2(34, 38, 43, 64) ==
|
||||
Matrix2x2(34, 38, 43, 64));
|
||||
test(Matrix2x2(34, 64, 43, 7) !=
|
||||
Matrix2x2(34, 38, 43, 64));
|
||||
|
||||
// Test operator to read a value
|
||||
test(mMatrix1[0][0] == 2);
|
||||
test(mMatrix1[0][1] == 24);
|
||||
test(mMatrix1[1][0] == -4);
|
||||
test(mMatrix1[1][1] == 5);
|
||||
|
||||
// Test operator to set a value
|
||||
Matrix2x2 test3;
|
||||
test3[0][0] = 2;
|
||||
test3[0][1] = 24;
|
||||
test3[1][0] = -4;
|
||||
test3[1][1] = 5;
|
||||
test(test3 == mMatrix1);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
/********************************************************************************
|
||||
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
||||
* Copyright (c) 2010-2013 Daniel Chappuis *
|
||||
|
@ -27,14 +26,10 @@
|
|||
#ifndef TEST_MATRIX3X3_H
|
||||
#define TEST_MATRIX3X3_H
|
||||
|
||||
#endif
|
||||
|
||||
// Libraries
|
||||
#include "../../Test.h"
|
||||
#include "../../../src/mathematics/Matrix3x3.h"
|
||||
|
||||
using namespace reactphysics3d;
|
||||
|
||||
/// Reactphysics3D namespace
|
||||
namespace reactphysics3d {
|
||||
|
||||
|
@ -287,3 +282,5 @@ class TestMatrix3x3 : public Test {
|
|||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -27,14 +27,10 @@
|
|||
#ifndef TEST_QUATERNION_H
|
||||
#define TEST_QUATERNION_H
|
||||
|
||||
#endif
|
||||
|
||||
// Libraries
|
||||
#include "../../Test.h"
|
||||
#include "../../../src/mathematics/Quaternion.h"
|
||||
|
||||
using namespace reactphysics3d;
|
||||
|
||||
/// Reactphysics3D namespace
|
||||
namespace reactphysics3d {
|
||||
|
||||
|
@ -229,3 +225,5 @@ class TestQuaternion : public Test {
|
|||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -27,14 +27,10 @@
|
|||
#ifndef TEST_TRANSFORM_H
|
||||
#define TEST_TRANSFORM_H
|
||||
|
||||
#endif
|
||||
|
||||
// Libraries
|
||||
#include "../../Test.h"
|
||||
#include "../../../src/mathematics/Transform.h"
|
||||
|
||||
using namespace reactphysics3d;
|
||||
|
||||
/// Reactphysics3D namespace
|
||||
namespace reactphysics3d {
|
||||
|
||||
|
@ -216,3 +212,5 @@ class TestTransform : public Test {
|
|||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
208
test/tests/mathematics/TestVector2.h
Normal file
208
test/tests/mathematics/TestVector2.h
Normal file
|
@ -0,0 +1,208 @@
|
|||
/********************************************************************************
|
||||
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
||||
* Copyright (c) 2010-2013 Daniel Chappuis *
|
||||
*********************************************************************************
|
||||
* *
|
||||
* This software is provided 'as-is', without any express or implied warranty. *
|
||||
* In no event will the authors be held liable for any damages arising from the *
|
||||
* use of this software. *
|
||||
* *
|
||||
* Permission is granted to anyone to use this software for any purpose, *
|
||||
* including commercial applications, and to alter it and redistribute it *
|
||||
* freely, subject to the following restrictions: *
|
||||
* *
|
||||
* 1. The origin of this software must not be misrepresented; you must not claim *
|
||||
* that you wrote the original software. If you use this software in a *
|
||||
* product, an acknowledgment in the product documentation would be *
|
||||
* appreciated but is not required. *
|
||||
* *
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be *
|
||||
* misrepresented as being the original software. *
|
||||
* *
|
||||
* 3. This notice may not be removed or altered from any source distribution. *
|
||||
* *
|
||||
********************************************************************************/
|
||||
|
||||
#ifndef TEST_VECTOR2_H
|
||||
#define TEST_VECTOR2_H
|
||||
|
||||
// Libraries
|
||||
#include "../../Test.h"
|
||||
#include "../../../src/mathematics/Vector2.h"
|
||||
|
||||
/// Reactphysics3D namespace
|
||||
namespace reactphysics3d {
|
||||
|
||||
// Class TestVector2
|
||||
/**
|
||||
* Unit test for the Vector2 class
|
||||
*/
|
||||
class TestVector2 : public Test {
|
||||
|
||||
private :
|
||||
|
||||
// ---------- Atributes ---------- //
|
||||
|
||||
/// Zero vector
|
||||
Vector2 mVectorZero;
|
||||
|
||||
// Vector (3, 4)
|
||||
Vector2 mVector34;
|
||||
|
||||
public :
|
||||
|
||||
// ---------- Methods ---------- //
|
||||
|
||||
/// Constructor
|
||||
TestVector1() : mVectorZero(0, 0), mVector34(3, 4) {}
|
||||
|
||||
/// Run the tests
|
||||
void run() {
|
||||
testConstructors();
|
||||
testLengthMethods();
|
||||
testDotProduct();
|
||||
testOthersMethods();
|
||||
testOperators();
|
||||
}
|
||||
|
||||
/// Test the constructors, getter and setter
|
||||
void testConstructors() {
|
||||
|
||||
// Test constructor
|
||||
test(mVectorZero.x == 0.0);
|
||||
test(mVectorZero.y == 0.0);
|
||||
test(mVector34.x == 3.0);
|
||||
test(mVector34.y == 4.0);
|
||||
|
||||
// Test copy-constructor
|
||||
Vector2 newVector(mVector34);
|
||||
test(newVector.x == 3.0);
|
||||
test(newVector.y == 4.0);
|
||||
|
||||
// Test method to set values
|
||||
Vector2 newVector2;
|
||||
newVector2.setAllValues(decimal(6.1), decimal(7.2));
|
||||
test(approxEqual(newVector2.x, decimal(6.1)));
|
||||
test(approxEqual(newVector2.y, decimal(7.2)));
|
||||
|
||||
// Test method to set to zero
|
||||
newVector2.setToZero();
|
||||
test(newVector2 == Vector2(0, 0));
|
||||
}
|
||||
|
||||
/// Test the length, unit vector and normalize methods
|
||||
void testLengthMethods() {
|
||||
|
||||
// Test length methods
|
||||
test(mVectorZero.length() == 0.0);
|
||||
test(mVectorZero.lengthSquare() == 0.0);
|
||||
test(Vector2(1, 0).length() == 1.0);
|
||||
test(Vector2(0, 1).length() == 1.0);
|
||||
test(mVector345.lengthSquare() == 50.0);
|
||||
|
||||
// Test unit vector methods
|
||||
test(Vector2(1, 0).isUnit());
|
||||
test(Vector2(0, 1).isUnit());
|
||||
test(!mVector34.isUnit());
|
||||
test(Vector2(5, 0).getUnit() == Vector3(1, 0));
|
||||
test(Vector2(0, 5).getUnit() == Vector3(0, 1));
|
||||
|
||||
test(!mVector34.isZero());
|
||||
test(mVectorZero.isZero());
|
||||
|
||||
// Test normalization method
|
||||
Vector2 mVector10(1, 0);
|
||||
Vector2 mVector01(0, 1);
|
||||
Vector2 mVector50(5, 0);
|
||||
Vector2 mVector05(0, 5);
|
||||
mVector10.normalize();
|
||||
mVector01.normalize();
|
||||
mVector50.normalize();
|
||||
mVector05.normalize();
|
||||
test(mVector10 == Vector2(1, 0));
|
||||
test(mVector01 == Vector2(0, 1));
|
||||
test(mVector50 == Vector2(1, 0));
|
||||
test(mVector05 == Vector2(0, 1));
|
||||
}
|
||||
|
||||
/// Test the dot product
|
||||
void testDotProduct() {
|
||||
|
||||
// Test the dot product
|
||||
test(Vector2(5, 0).dot(Vector2(0, 8)) == 0);
|
||||
test(Vector2(5, 8).dot(Vector2(0, 0)) == 0);
|
||||
test(Vector2(12, 45).dot(Vector2(0, 0)) == 0);
|
||||
test(Vector2(5, 7).dot(Vector2(5, 7)) == 74);
|
||||
test(Vector2(3, 6).dot(Vector2(-3, -6)) == -45);
|
||||
test(Vector2(2, 3).dot(Vector2(-7, 4)) == -2);
|
||||
test(Vector2(4, 3).dot(Vector2(8, 9)) == 59);
|
||||
}
|
||||
|
||||
/// Test others methods
|
||||
void testOthersMethods() {
|
||||
|
||||
// Test the method that returns the absolute vector
|
||||
test(Vector2(4, 5).getAbsoluteVector() == Vector2(4, 5));
|
||||
test(Vector2(-7, -24).getAbsoluteVector() == Vector2(7, 24));
|
||||
|
||||
// Test the method that returns the minimal element
|
||||
test(Vector2(6, 35).getMinAxis() == 0);
|
||||
test(Vector2(564, 45).getMinAxis() == 1);
|
||||
test(Vector2(98, 23).getMinAxis() == 1);
|
||||
test(Vector2(-53, -25).getMinAxis() == 0);
|
||||
|
||||
// Test the method that returns the maximal element
|
||||
test(Vector2(6, 35).getMaxAxis() == 1);
|
||||
test(Vector2(7, 537).getMaxAxis() == 1);
|
||||
test(Vector2(98, 23).getMaxAxis() == 0);
|
||||
test(Vector2(-53, -25).getMaxAxis() == 1);
|
||||
}
|
||||
|
||||
/// Test the operators
|
||||
void testOperators() {
|
||||
|
||||
// Test the [] operator
|
||||
test(mVector34[0] == 3);
|
||||
test(mVector34[1] == 4);
|
||||
|
||||
// Assignment operator
|
||||
Vector2 newVector(6, 4);
|
||||
newVector = Vector2(7, 8);
|
||||
test(newVector == Vector2(7, 8));
|
||||
|
||||
// Equality, inequality operators
|
||||
test(Vector2(5, 7) == Vector2(5, 7));
|
||||
test(Vector2(63, 64) != Vector2(63, 84));
|
||||
test(Vector2(63, 64) != Vector2(12, 64));
|
||||
|
||||
// Addition, substraction
|
||||
Vector2 vector1(6, 33);
|
||||
Vector2 vector2(7, 68);
|
||||
test(Vector2(63, 24) + Vector2(3, 4) == Vector2(66, 28));
|
||||
test(Vector2(63, 24) - Vector2(3, 4) == Vector2(60, 20));
|
||||
vector1 += Vector2(5, 10);
|
||||
vector2 -= Vector2(10, 21);
|
||||
test(vector1 == Vector2(11, 43));
|
||||
test(vector2 == Vector2(-3, 47));
|
||||
|
||||
// Multiplication, division
|
||||
Vector2 vector3(6, 33);
|
||||
Vector2 vector4(15, 60);
|
||||
test(Vector2(63, 24) * 3 == Vector2(189, 72));
|
||||
test(3 * Vector2(63, 24) == Vector2(189, 72));
|
||||
test(Vector2(14, 8) / 2 == Vector2(7, 4));
|
||||
vector3 *= 10;
|
||||
vector4 /= 3;
|
||||
test(vector3 == Vector2(60, 330));
|
||||
test(vector4 == Vector2(5, 20));
|
||||
|
||||
// Negative operator
|
||||
Vector2 vector5(-34, 5);
|
||||
Vector2 negative = -vector5;
|
||||
test(negative == Vector2(34, -5));
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
|
@ -26,14 +26,10 @@
|
|||
#ifndef TEST_VECTOR3_H
|
||||
#define TEST_VECTOR3_H
|
||||
|
||||
#endif
|
||||
|
||||
// Libraries
|
||||
#include "../../Test.h"
|
||||
#include "../../../src/mathematics/Vector3.h"
|
||||
|
||||
using namespace reactphysics3d;
|
||||
|
||||
/// Reactphysics3D namespace
|
||||
namespace reactphysics3d {
|
||||
|
||||
|
@ -232,3 +228,5 @@ class TestVector3 : public Test {
|
|||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue
Block a user