From 5392948518ef415991feb55e0595bc15b48d737c Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Wed, 13 Dec 2017 17:51:38 +0100 Subject: [PATCH] Use inline constructors for mathematics objects (vectors, quaternion, matrices, ...) --- src/mathematics/Matrix2x2.cpp | 25 ------------------ src/mathematics/Matrix2x2.h | 25 ++++++++++++++++++ src/mathematics/Matrix3x3.cpp | 26 ------------------- src/mathematics/Matrix3x3.h | 26 +++++++++++++++++++ src/mathematics/Quaternion.cpp | 21 ---------------- src/mathematics/Quaternion.h | 25 ++++++++++++++++-- src/mathematics/Transform.cpp | 22 ---------------- src/mathematics/Transform.h | 46 ++++++++++++++++++++++++++++++++-- src/mathematics/Vector2.cpp | 15 ----------- src/mathematics/Vector2.h | 16 ++++++++++++ src/mathematics/Vector3.cpp | 15 ----------- src/mathematics/Vector3.h | 15 +++++++++++ 12 files changed, 149 insertions(+), 128 deletions(-) diff --git a/src/mathematics/Matrix2x2.cpp b/src/mathematics/Matrix2x2.cpp index c501c1db..bf4adbe5 100644 --- a/src/mathematics/Matrix2x2.cpp +++ b/src/mathematics/Matrix2x2.cpp @@ -28,31 +28,6 @@ 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 Matrix2x2& Matrix2x2::operator=(const Matrix2x2& matrix) { diff --git a/src/mathematics/Matrix2x2.h b/src/mathematics/Matrix2x2.h index ee31b07a..8315eb99 100644 --- a/src/mathematics/Matrix2x2.h +++ b/src/mathematics/Matrix2x2.h @@ -147,6 +147,31 @@ class Matrix2x2 { 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 inline void Matrix2x2::setAllValues(decimal a1, decimal a2, decimal b1, decimal b2) { diff --git a/src/mathematics/Matrix3x3.cpp b/src/mathematics/Matrix3x3.cpp index a5491ed4..e86e63a1 100644 --- a/src/mathematics/Matrix3x3.cpp +++ b/src/mathematics/Matrix3x3.cpp @@ -30,32 +30,6 @@ // 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 -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 Matrix3x3& Matrix3x3::operator=(const Matrix3x3& matrix) { diff --git a/src/mathematics/Matrix3x3.h b/src/mathematics/Matrix3x3.h index ebb8792d..6897a2cf 100644 --- a/src/mathematics/Matrix3x3.h +++ b/src/mathematics/Matrix3x3.h @@ -155,6 +155,32 @@ class Matrix3x3 { 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 inline void Matrix3x3::setAllValues(decimal a1, decimal a2, decimal a3, decimal b1, decimal b2, decimal b3, diff --git a/src/mathematics/Quaternion.cpp b/src/mathematics/Quaternion.cpp index d0e1ae61..0e877612 100644 --- a/src/mathematics/Quaternion.cpp +++ b/src/mathematics/Quaternion.cpp @@ -31,27 +31,6 @@ // Namespace 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) Quaternion Quaternion::fromEulerAngles(decimal angleX, decimal angleY, decimal angleZ) { diff --git a/src/mathematics/Quaternion.h b/src/mathematics/Quaternion.h index d5e771e2..dd0726dc 100644 --- a/src/mathematics/Quaternion.h +++ b/src/mathematics/Quaternion.h @@ -169,7 +169,28 @@ struct Quaternion { 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) { x = newX; y = newY; @@ -177,7 +198,7 @@ inline void Quaternion::setAllValues(decimal newX, decimal newY, decimal newZ, d w = newW; } -/// Set the quaternion to zero +// Set the quaternion to zero inline void Quaternion::setToZero() { x = 0; y = 0; diff --git a/src/mathematics/Transform.cpp b/src/mathematics/Transform.cpp index 4bc91253..d5cf0399 100644 --- a/src/mathematics/Transform.cpp +++ b/src/mathematics/Transform.cpp @@ -29,25 +29,3 @@ // Namespaces 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) { - -} diff --git a/src/mathematics/Transform.h b/src/mathematics/Transform.h index 52ee663e..a5248fc2 100644 --- a/src/mathematics/Transform.h +++ b/src/mathematics/Transform.h @@ -118,6 +118,29 @@ class 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 inline const Vector3& Transform::getPosition() const { return mPosition; @@ -199,8 +222,27 @@ inline Vector3 Transform::operator*(const Vector3& vector) const { // Operator of multiplication of a transform with another one 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 diff --git a/src/mathematics/Vector2.cpp b/src/mathematics/Vector2.cpp index 2f225636..271fd82f 100644 --- a/src/mathematics/Vector2.cpp +++ b/src/mathematics/Vector2.cpp @@ -30,21 +30,6 @@ // 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) { - -} - // Return the corresponding unit vector Vector2 Vector2::getUnit() const { decimal lengthVector = length(); diff --git a/src/mathematics/Vector2.h b/src/mathematics/Vector2.h index 4b0e32f1..bc5f8872 100644 --- a/src/mathematics/Vector2.h +++ b/src/mathematics/Vector2.h @@ -156,6 +156,22 @@ struct 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 inline void Vector2::setToZero() { x = 0; diff --git a/src/mathematics/Vector3.cpp b/src/mathematics/Vector3.cpp index ab2d126d..33b760ba 100644 --- a/src/mathematics/Vector3.cpp +++ b/src/mathematics/Vector3.cpp @@ -31,21 +31,6 @@ // Namespaces 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 Vector3 Vector3::getUnit() const { decimal lengthVector = length(); diff --git a/src/mathematics/Vector3.h b/src/mathematics/Vector3.h index 1ca1adfd..95255fdb 100644 --- a/src/mathematics/Vector3.h +++ b/src/mathematics/Vector3.h @@ -168,6 +168,21 @@ struct Vector3 { 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 inline void Vector3::setToZero() { x = 0;