Use inline constructors for mathematics objects (vectors, quaternion, matrices, ...)

This commit is contained in:
Daniel Chappuis 2017-12-13 17:51:38 +01:00
parent 2d0cb27538
commit 5392948518
12 changed files with 149 additions and 128 deletions

View File

@ -28,31 +28,6 @@
using namespace reactphysics3d; 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);
}
// 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 // Assignment operator
Matrix2x2& Matrix2x2::operator=(const Matrix2x2& matrix) { Matrix2x2& Matrix2x2::operator=(const Matrix2x2& matrix) {

View File

@ -147,6 +147,31 @@ class Matrix2x2 {
Vector2& operator[](int row); Vector2& operator[](int row);
}; };
// Constructor of the class Matrix2x2
inline Matrix2x2::Matrix2x2() {
// Initialize all values in the matrix to zero
setAllValues(0.0, 0.0, 0.0, 0.0);
}
// Constructor
inline Matrix2x2::Matrix2x2(decimal value) {
setAllValues(value, value, value, value);
}
// Constructor with arguments
inline Matrix2x2::Matrix2x2(decimal a1, decimal a2, decimal b1, decimal b2) {
// Initialize the matrix with the values
setAllValues(a1, a2, b1, b2);
}
// Copy-constructor
inline Matrix2x2::Matrix2x2(const Matrix2x2& matrix) {
setAllValues(matrix.mRows[0][0], matrix.mRows[0][1],
matrix.mRows[1][0], matrix.mRows[1][1]);
}
// Method to set all the values in the matrix // Method to set all the values in the matrix
inline void Matrix2x2::setAllValues(decimal a1, decimal a2, inline void Matrix2x2::setAllValues(decimal a1, decimal a2,
decimal b1, decimal b2) { decimal b1, decimal b2) {

View File

@ -30,32 +30,6 @@
// Namespaces // Namespaces
using namespace reactphysics3d; 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
Matrix3x3::Matrix3x3(decimal value) {
setAllValues(value, value, value, value, value, value, value, value, value);
}
// Constructor with arguments
Matrix3x3::Matrix3x3(decimal a1, decimal a2, decimal a3,
decimal b1, decimal b2, decimal b3,
decimal c1, decimal c2, decimal c3) {
// Initialize the matrix with the values
setAllValues(a1, a2, a3, b1, b2, b3, c1, c2, c3);
}
// Copy-constructor
Matrix3x3::Matrix3x3(const Matrix3x3& matrix) {
setAllValues(matrix.mRows[0][0], matrix.mRows[0][1], matrix.mRows[0][2],
matrix.mRows[1][0], matrix.mRows[1][1], matrix.mRows[1][2],
matrix.mRows[2][0], matrix.mRows[2][1], matrix.mRows[2][2]);
}
// Assignment operator // Assignment operator
Matrix3x3& Matrix3x3::operator=(const Matrix3x3& matrix) { Matrix3x3& Matrix3x3::operator=(const Matrix3x3& matrix) {

View File

@ -155,6 +155,32 @@ class Matrix3x3 {
Vector3& operator[](int row); Vector3& operator[](int row);
}; };
// Constructor of the class Matrix3x3
inline 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
inline Matrix3x3::Matrix3x3(decimal value) {
setAllValues(value, value, value, value, value, value, value, value, value);
}
// Constructor with arguments
inline Matrix3x3::Matrix3x3(decimal a1, decimal a2, decimal a3,
decimal b1, decimal b2, decimal b3,
decimal c1, decimal c2, decimal c3) {
// Initialize the matrix with the values
setAllValues(a1, a2, a3, b1, b2, b3, c1, c2, c3);
}
// Copy-constructor
inline Matrix3x3::Matrix3x3(const Matrix3x3& matrix) {
setAllValues(matrix.mRows[0][0], matrix.mRows[0][1], matrix.mRows[0][2],
matrix.mRows[1][0], matrix.mRows[1][1], matrix.mRows[1][2],
matrix.mRows[2][0], matrix.mRows[2][1], matrix.mRows[2][2]);
}
// Method to set all the values in the matrix // Method to set all the values in the matrix
inline void Matrix3x3::setAllValues(decimal a1, decimal a2, decimal a3, inline void Matrix3x3::setAllValues(decimal a1, decimal a2, decimal a3,
decimal b1, decimal b2, decimal b3, decimal b1, decimal b2, decimal b3,

View File

@ -31,27 +31,6 @@
// Namespace // Namespace
using namespace reactphysics3d; 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(decimal newX, decimal newY, decimal newZ, decimal newW)
:x(newX), y(newY), z(newZ), w(newW) {
}
// Constructor with the component w and the vector v=(x y z)
Quaternion::Quaternion(decimal newW, const Vector3& v) : x(v.x), y(v.y), z(v.z), w(newW) {
}
// Constructor with the component w and the vector v=(x y z)
Quaternion::Quaternion(const Vector3& v, decimal newW) : x(v.x), y(v.y), z(v.z), w(newW) {
}
// Return a quaternion constructed from Euler angles (in radians) // Return a quaternion constructed from Euler angles (in radians)
Quaternion Quaternion::fromEulerAngles(decimal angleX, decimal angleY, decimal angleZ) { Quaternion Quaternion::fromEulerAngles(decimal angleX, decimal angleY, decimal angleZ) {

View File

@ -169,7 +169,28 @@ struct Quaternion {
void initWithEulerAngles(decimal angleX, decimal angleY, decimal angleZ); void initWithEulerAngles(decimal angleX, decimal angleY, decimal angleZ);
}; };
/// Set all the values // Constructor of the class
inline Quaternion::Quaternion() : x(0.0), y(0.0), z(0.0), w(0.0) {
}
// Constructor with arguments
inline Quaternion::Quaternion(decimal newX, decimal newY, decimal newZ, decimal newW)
:x(newX), y(newY), z(newZ), w(newW) {
}
// Constructor with the component w and the vector v=(x y z)
inline Quaternion::Quaternion(decimal newW, const Vector3& v) : x(v.x), y(v.y), z(v.z), w(newW) {
}
// Constructor with the component w and the vector v=(x y z)
inline Quaternion::Quaternion(const Vector3& v, decimal newW) : x(v.x), y(v.y), z(v.z), w(newW) {
}
// Set all the values
inline void Quaternion::setAllValues(decimal newX, decimal newY, decimal newZ, decimal newW) { inline void Quaternion::setAllValues(decimal newX, decimal newY, decimal newZ, decimal newW) {
x = newX; x = newX;
y = newY; y = newY;
@ -177,7 +198,7 @@ inline void Quaternion::setAllValues(decimal newX, decimal newY, decimal newZ, d
w = newW; w = newW;
} }
/// Set the quaternion to zero // Set the quaternion to zero
inline void Quaternion::setToZero() { inline void Quaternion::setToZero() {
x = 0; x = 0;
y = 0; y = 0;

View File

@ -29,25 +29,3 @@
// Namespaces // Namespaces
using namespace reactphysics3d; using namespace reactphysics3d;
// Constructor
Transform::Transform() : mPosition(Vector3(0.0, 0.0, 0.0)), mOrientation(Quaternion::identity()) {
}
// Constructor
Transform::Transform(const Vector3& position, const Matrix3x3& orientation)
: mPosition(position), mOrientation(Quaternion(orientation)) {
}
// Constructor
Transform::Transform(const Vector3& position, const Quaternion& orientation)
: mPosition(position), mOrientation(orientation) {
}
// Copy-constructor
Transform::Transform(const Transform& transform)
: mPosition(transform.mPosition), mOrientation(transform.mOrientation) {
}

View File

@ -118,6 +118,29 @@ class Transform {
Transform& operator=(const Transform& transform); Transform& operator=(const Transform& transform);
}; };
// Constructor
inline Transform::Transform() : mPosition(Vector3(0.0, 0.0, 0.0)), mOrientation(Quaternion::identity()) {
}
// Constructor
inline Transform::Transform(const Vector3& position, const Matrix3x3& orientation)
: mPosition(position), mOrientation(Quaternion(orientation)) {
}
// Constructor
inline Transform::Transform(const Vector3& position, const Quaternion& orientation)
: mPosition(position), mOrientation(orientation) {
}
// Copy-constructor
inline Transform::Transform(const Transform& transform)
: mPosition(transform.mPosition), mOrientation(transform.mOrientation) {
}
// Return the position of the transform // Return the position of the transform
inline const Vector3& Transform::getPosition() const { inline const Vector3& Transform::getPosition() const {
return mPosition; return mPosition;
@ -199,8 +222,27 @@ inline Vector3 Transform::operator*(const Vector3& vector) const {
// Operator of multiplication of a transform with another one // Operator of multiplication of a transform with another one
inline Transform Transform::operator*(const Transform& transform2) const { inline Transform Transform::operator*(const Transform& transform2) const {
return Transform(mPosition + mOrientation * transform2.mPosition,
mOrientation * transform2.mOrientation); const decimal prodX = mOrientation.w * transform2.mPosition.x + mOrientation.w * transform2.mPosition.z
- mOrientation.z * transform2.mPosition.y;
const decimal prodY = mOrientation.w * transform2.mPosition.y + mOrientation.z * transform2.mPosition.x
- mOrientation.x * transform2.mPosition.z;
const decimal prodZ = mOrientation.w * transform2.mPosition.z + mOrientation.x * transform2.mPosition.y
- mOrientation.y * transform2.mPosition.x;
const decimal prodW = -mOrientation.x * transform2.mPosition.x - mOrientation.y * transform2.mPosition.y
- mOrientation.z * transform2.mPosition.z;
return Transform(Vector3(mPosition.x + mOrientation.w * prodX - prodY * mOrientation.z + prodZ * mOrientation.y - prodW * mOrientation.x,
mPosition.y + mOrientation.w * prodY - prodZ * mOrientation.x + prodX * mOrientation.z - prodW * mOrientation.y,
mPosition.z + mOrientation.w * prodZ - prodX * mOrientation.y + prodY * mOrientation.x - prodW * mOrientation.z),
Quaternion(mOrientation.w * transform2.mOrientation.x + transform2.mOrientation.w * mOrientation.x
+ mOrientation.y * transform2.mOrientation.z - mOrientation.z * transform2.mOrientation.y,
mOrientation.w * transform2.mOrientation.y + transform2.mOrientation.w * mOrientation.y
+ mOrientation.z * transform2.mOrientation.x - mOrientation.x * transform2.mOrientation.z,
mOrientation.w * transform2.mOrientation.z + transform2.mOrientation.w * mOrientation.z
+ mOrientation.x * transform2.mOrientation.y - mOrientation.y * transform2.mOrientation.x,
mOrientation.w * transform2.mOrientation.w - mOrientation.x * transform2.mOrientation.x
- mOrientation.y * transform2.mOrientation.y - mOrientation.z * transform2.mOrientation.z));
} }
// Return true if the two transforms are equal // Return true if the two transforms are equal

View File

@ -30,21 +30,6 @@
// Namespaces // Namespaces
using namespace reactphysics3d; 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) {
}
// Return the corresponding unit vector // Return the corresponding unit vector
Vector2 Vector2::getUnit() const { Vector2 Vector2::getUnit() const {
decimal lengthVector = length(); decimal lengthVector = length();

View File

@ -156,6 +156,22 @@ struct Vector2 {
friend Vector2 operator/(const Vector2& vector1, const Vector2& vector2); friend Vector2 operator/(const Vector2& vector1, const Vector2& vector2);
}; };
// Constructor
inline Vector2::Vector2() : x(0.0), y(0.0) {
}
// Constructor with arguments
inline Vector2::Vector2(decimal newX, decimal newY) : x(newX), y(newY) {
}
// Copy-constructor
inline Vector2::Vector2(const Vector2& vector) : x(vector.x), y(vector.y) {
}
// Set the vector to zero // Set the vector to zero
inline void Vector2::setToZero() { inline void Vector2::setToZero() {
x = 0; x = 0;

View File

@ -31,21 +31,6 @@
// Namespaces // Namespaces
using namespace reactphysics3d; using namespace reactphysics3d;
// Constructor of the class Vector3D
Vector3::Vector3() : x(0.0), y(0.0), z(0.0) {
}
// Constructor with arguments
Vector3::Vector3(decimal newX, decimal newY, decimal newZ) : x(newX), y(newY), z(newZ) {
}
// Copy-constructor
Vector3::Vector3(const Vector3& vector) : x(vector.x), y(vector.y), z(vector.z) {
}
// Return the corresponding unit vector // Return the corresponding unit vector
Vector3 Vector3::getUnit() const { Vector3 Vector3::getUnit() const {
decimal lengthVector = length(); decimal lengthVector = length();

View File

@ -168,6 +168,21 @@ struct Vector3 {
friend Vector3 operator/(const Vector3& vector1, const Vector3& vector2); friend Vector3 operator/(const Vector3& vector1, const Vector3& vector2);
}; };
// Constructor of the class Vector3D
inline Vector3::Vector3() : x(0.0), y(0.0), z(0.0) {
}
// Constructor with arguments
inline Vector3::Vector3(decimal newX, decimal newY, decimal newZ) : x(newX), y(newY), z(newZ) {
}
// Copy-constructor
inline Vector3::Vector3(const Vector3& vector) : x(vector.x), y(vector.y), z(vector.z) {
}
// Set the vector to zero // Set the vector to zero
inline void Vector3::setToZero() { inline void Vector3::setToZero() {
x = 0; x = 0;