From f1bee775a0d4781ebc3590890f65c57cf435fe84 Mon Sep 17 00:00:00 2001 From: Daniel Agar Date: Mon, 27 Aug 2018 14:05:55 -0400 Subject: [PATCH] use default constructors and skip unnecessary initialization --- matrix/AxisAngle.hpp | 20 ++++++-------------- matrix/Dcm.hpp | 2 +- matrix/Euler.hpp | 9 +++------ matrix/Matrix.hpp | 10 +++++----- matrix/Quaternion.hpp | 18 ++++++------------ matrix/Scalar.hpp | 17 +++++++---------- matrix/SquareMatrix.hpp | 5 +---- matrix/Vector.hpp | 4 +--- matrix/Vector2.hpp | 9 +++------ matrix/Vector3.hpp | 10 +++------- test/matrixAssignment.cpp | 3 +-- 11 files changed, 37 insertions(+), 70 deletions(-) diff --git a/matrix/AxisAngle.hpp b/matrix/AxisAngle.hpp index e1275c3f1d..51d6a3e26a 100644 --- a/matrix/AxisAngle.hpp +++ b/matrix/AxisAngle.hpp @@ -46,10 +46,7 @@ public: /** * Standard constructor */ - AxisAngle() : - Vector() - { - } + AxisAngle() = default; /** * Constructor from Matrix31 @@ -70,8 +67,7 @@ public: * * @param q quaternion */ - AxisAngle(const Quaternion &q) : - Vector() + AxisAngle(const Quaternion &q) { AxisAngle &v = *this; Type ang = Type(2.0f)*acos(q(0)); @@ -95,8 +91,7 @@ public: * * @param dcm dcm to set quaternion to */ - AxisAngle(const Dcm &dcm) : - Vector() + AxisAngle(const Dcm &dcm) { AxisAngle &v = *this; v = Quaternion(dcm); @@ -111,8 +106,7 @@ public: * * @param euler euler angle instance */ - AxisAngle(const Euler &euler) : - Vector() + AxisAngle(const Euler &euler) { AxisAngle &v = *this; v = Quaternion(euler); @@ -125,8 +119,7 @@ public: * @param y r_y*angle * @param z r_z*angle */ - AxisAngle(Type x, Type y, Type z) : - Vector() + AxisAngle(Type x, Type y, Type z) { AxisAngle &v = *this; v(0) = x; @@ -140,8 +133,7 @@ public: * @param axis An axis of rotation, normalized if not unit length * @param angle The amount to rotate */ - AxisAngle(const Matrix31 & axis_, Type angle_) : - Vector() + AxisAngle(const Matrix31 & axis_, Type angle_) { AxisAngle &v = *this; // make sure axis is a unit vector diff --git a/matrix/Dcm.hpp b/matrix/Dcm.hpp index 3f0d969402..43d3cee1d3 100644 --- a/matrix/Dcm.hpp +++ b/matrix/Dcm.hpp @@ -46,7 +46,7 @@ public: * * Initializes to identity */ - Dcm() : SquareMatrix() + Dcm() { (*this) = eye(); } diff --git a/matrix/Euler.hpp b/matrix/Euler.hpp index e308a2f3f5..d16d307ed1 100644 --- a/matrix/Euler.hpp +++ b/matrix/Euler.hpp @@ -43,9 +43,7 @@ public: /** * Standard constructor */ - Euler() : Vector() - { - } + Euler() = default; /** * Copy constructor @@ -95,7 +93,7 @@ public: * * @param dcm Direction cosine matrix */ - Euler(const Dcm &dcm) : Vector() + Euler(const Dcm &dcm) { Type phi_val = Type(atan2(dcm(2, 1), dcm(2, 2))); Type theta_val = Type(asin(-dcm(2, 0))); @@ -126,8 +124,7 @@ public: * * @param q quaternion */ - Euler(const Quaternion &q) : - Vector() + Euler(const Quaternion &q) { *this = Euler(Dcm(q)); } diff --git a/matrix/Matrix.hpp b/matrix/Matrix.hpp index f4972d6834..151b337884 100644 --- a/matrix/Matrix.hpp +++ b/matrix/Matrix.hpp @@ -30,22 +30,22 @@ class Matrix public: - Type _data[M][N]; + Type _data[M][N] {}; // Constructors - Matrix() : _data() {} + Matrix() = default; - Matrix(const Type data_[][N]) : _data() + Matrix(const Type data_[][N]) { memcpy(_data, data_, sizeof(_data)); } - Matrix(const Type *data_) : _data() + Matrix(const Type *data_) { memcpy(_data, data_, sizeof(_data)); } - Matrix(const Matrix &other) : _data() + Matrix(const Matrix &other) { memcpy(_data, other._data, sizeof(_data)); } diff --git a/matrix/Quaternion.hpp b/matrix/Quaternion.hpp index a17205fc12..cbf2c2b85a 100644 --- a/matrix/Quaternion.hpp +++ b/matrix/Quaternion.hpp @@ -70,8 +70,7 @@ public: /** * Standard constructor */ - Quaternion() : - Vector() + Quaternion() { Quaternion &q = *this; q(0) = 1; @@ -98,8 +97,7 @@ public: * * @param dcm dcm to set quaternion to */ - Quaternion(const Dcm &R) : - Vector() + Quaternion(const Dcm &R) { Quaternion &q = *this; Type t = R.trace(); @@ -143,8 +141,7 @@ public: * * @param euler euler angle instance */ - Quaternion(const Euler &euler) : - Vector() + Quaternion(const Euler &euler) { Quaternion &q = *this; Type cosPhi_2 = Type(cos(euler.phi() / Type(2.0))); @@ -168,8 +165,7 @@ public: * * @param aa axis-angle vector */ - Quaternion(const AxisAngle &aa) : - Vector() + Quaternion(const AxisAngle &aa) { Quaternion &q = *this; Type angle = aa.norm(); @@ -194,8 +190,7 @@ public: * @param src source vector (no need to normalize) * @param eps epsilon threshold which decides if a value is considered zero */ - Quaternion(const Vector3 &src, const Vector3 &dst, const Type eps = Type(1e-5)) : - Vector() + Quaternion(const Vector3 &src, const Vector3 &dst, const Type eps = Type(1e-5)) { Quaternion &q = *this; Vector3 cr = src.cross(dst); @@ -242,8 +237,7 @@ public: * @param c set quaternion value 2 * @param d set quaternion value 3 */ - Quaternion(Type a, Type b, Type c, Type d) : - Vector() + Quaternion(Type a, Type b, Type c, Type d) { Quaternion &q = *this; q(0) = a; diff --git a/matrix/Scalar.hpp b/matrix/Scalar.hpp index 302cd70c87..c9765028c3 100644 --- a/matrix/Scalar.hpp +++ b/matrix/Scalar.hpp @@ -17,21 +17,18 @@ template class Scalar { public: - Scalar() : _value() + Scalar() = delete; + + Scalar(const Matrix & other) : + _value{other(0,0)} { } - Scalar(const Matrix & other) + Scalar(Type other) : _value(other) { - _value = other(0,0); } - Scalar(Type other) - { - _value = other; - } - - operator Type &() + operator const Type &() { return _value; } @@ -49,7 +46,7 @@ public: } private: - Type _value; + const Type _value; }; diff --git a/matrix/SquareMatrix.hpp b/matrix/SquareMatrix.hpp index 5b2560dc71..2e348137b1 100644 --- a/matrix/SquareMatrix.hpp +++ b/matrix/SquareMatrix.hpp @@ -24,10 +24,7 @@ template class SquareMatrix : public Matrix { public: - SquareMatrix() : - Matrix() - { - } + SquareMatrix() = default; SquareMatrix(const Type *data_) : Matrix(data_) diff --git a/matrix/Vector.hpp b/matrix/Vector.hpp index a1058f2361..7ea2d0caee 100644 --- a/matrix/Vector.hpp +++ b/matrix/Vector.hpp @@ -22,9 +22,7 @@ class Vector : public Matrix public: typedef Matrix MatrixM1; - Vector() : MatrixM1() - { - } + Vector() = default; Vector(const MatrixM1 & other) : MatrixM1(other) diff --git a/matrix/Vector2.hpp b/matrix/Vector2.hpp index 7badf94b12..7aeeb1d927 100644 --- a/matrix/Vector2.hpp +++ b/matrix/Vector2.hpp @@ -23,10 +23,7 @@ public: typedef Matrix Matrix21; - Vector2() : - Vector() - { - } + Vector2() = default; Vector2(const Matrix21 & other) : Vector(other) @@ -38,14 +35,14 @@ public: { } - Vector2(Type x, Type y) : Vector() + Vector2(Type x, Type y) { Vector2 &v(*this); v(0) = x; v(1) = y; } - Type cross(const Matrix21 & b) const { + Type cross(const Matrix21 & b) const { const Vector2 &a(*this); return a(0)*b(1, 0) - a(1)*b(0, 0); } diff --git a/matrix/Vector3.hpp b/matrix/Vector3.hpp index 3dbaa26c23..989e247032 100644 --- a/matrix/Vector3.hpp +++ b/matrix/Vector3.hpp @@ -29,10 +29,7 @@ public: typedef Matrix Matrix31; - Vector3() : - Vector() - { - } + Vector3() = default; Vector3(const Matrix31 & other) : Vector(other) @@ -44,15 +41,14 @@ public: { } - Vector3(Type x, Type y, Type z) : Vector() - { + Vector3(Type x, Type y, Type z) { Vector3 &v(*this); v(0) = x; v(1) = y; v(2) = z; } - Vector3 cross(const Matrix31 & b) const { + Vector3 cross(const Matrix31 & b) const { const Vector3 &a(*this); Vector3 c; c(0) = a(1)*b(2,0) - a(2)*b(1,0); diff --git a/test/matrixAssignment.cpp b/test/matrixAssignment.cpp index 91b4cc02b0..5ad7de6779 100644 --- a/test/matrixAssignment.cpp +++ b/test/matrixAssignment.cpp @@ -97,8 +97,7 @@ int main() TEST(fabs(m4.min() - 1) < 1e-5); TEST(fabs((-m4).min() + 9) < 1e-5); - Scalar s; - s = 1; + Scalar s = 1; const Vector & s_vect = s; TEST(fabs(s - 1) < 1e-5); TEST(fabs(s_vect(0) - 1.0f) < 1e-5);