Skip to content

Commit

Permalink
Adds Wextra and improves Matrix33/TransformMatrix/Quaternion
Browse files Browse the repository at this point in the history
  • Loading branch information
sebjameswml committed Nov 22, 2023
1 parent 0e842e8 commit 95ed762
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 12 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ else()
message(STATUS "GCC version ${CMAKE_CXX_COMPILER_VERSION} OK!")
endif()
endif()
set(CMAKE_CXX_FLAGS "${MORPH_HOST_DEFINITION} -Wall -g -Werror=deprecated-copy -Wunused-parameter -Wpedantic -Werror -Wfatal-errors -Wunused-result -Wunknown-pragmas -pedantic-errors -march=native -O3")
set(CMAKE_CXX_FLAGS "${MORPH_HOST_DEFINITION} -Wall -Wextra -g -Werror=deprecated-copy -Wunused-parameter -Wpedantic -Werror -Wfatal-errors -Wunused-result -Wunknown-pragmas -pedantic-errors -march=native -O3")
endif()
else() # Windows
# Set flags for Windows. /EHsc required for use of exceptions
Expand Down
49 changes: 43 additions & 6 deletions morph/Matrix33.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ namespace morph {
public:
//! Default constructor
Matrix33() { this->setToIdentity(); }
//! User-declared destructor
~Matrix33() {}
//! User-declared copy constructor
Matrix33 (const Matrix33<Flt>& other) : mat(other.mat) {}
//! User-declared copy assignment constructor
Matrix33<Flt>& operator= (Matrix33<Flt>& other)
{
std::swap (mat, other.mat);
return *this;
}
//! Explicitly defaulted move constructor
Matrix33(Matrix33<Flt>&& other) = default;
//! Explicitly defaulted move assignment constructor
Matrix33<Flt>& operator=(Matrix33<Flt>&& other) = default;

/*!
* The matrix data, arranged in column major format to be similar to
Expand Down Expand Up @@ -164,6 +178,7 @@ namespace morph {
return this->transpose (this->cofactor());
}

static constexpr bool debug_cofactors = false;
std::array<Flt, 9> cofactor() const
{
std::array<Flt, 9> cofac;
Expand Down Expand Up @@ -223,12 +238,13 @@ namespace morph {
minorElem[2] = this->mat[3];
minorElem[3] = this->mat[4];
cofac[8] = this->determinant (minorElem);
#if 0
std::cout << "cofactor:\n";
std::cout <<"[ "<< cofac[0]<<" , "<<cofac[3]<<" , "<<cofac[6]<<" ;\n";
std::cout <<" "<< cofac[1]<<" , "<<cofac[4]<<" , "<<cofac[7]<<" ;\n";
std::cout <<" "<< cofac[2]<<" , "<<cofac[5]<<" , "<<cofac[8]<<" ;\n";
#endif

if constexpr (debug_cofactors) {
std::cout << "cofactor:\n";
std::cout <<"[ "<< cofac[0]<<" , "<<cofac[3]<<" , "<<cofac[6]<<" ;\n";
std::cout <<" "<< cofac[1]<<" , "<<cofac[4]<<" , "<<cofac[7]<<" ;\n";
std::cout <<" "<< cofac[2]<<" , "<<cofac[5]<<" , "<<cofac[8]<<" ;\n";
}
return cofac;
}

Expand Down Expand Up @@ -445,6 +461,27 @@ namespace morph {
return v;
}

//! Equality operator. True if all elements match
bool operator==(const Matrix33<Flt>& rhs) const
{
unsigned int ndiff = 0;
for (unsigned int i = 0; i < 9 && ndiff == 0; ++i) {
ndiff += this->mat[i] == rhs.mat[i] ? 0 : 1;
}
return ndiff == 0;
}

//! Not equals
bool operator!=(const Matrix33<Flt>& rhs) const
{
unsigned int ndiff = 0;
for (unsigned int i = 0; i < 9 && ndiff == 0; ++i) {
ndiff += this->mat[i] == rhs.mat[i] ? 0 : 1;
}
return ndiff > 0;
}


//! Overload the stream output operator
friend std::ostream& operator<< <Flt> (std::ostream& os, const Matrix33<Flt>& tm);
};
Expand Down
30 changes: 29 additions & 1 deletion morph/Quaternion.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,27 @@ namespace morph {
, y(_y)
, z(_z) {}

Quaternion (const Quaternion& rhs)
//! User-declared destructor
~Quaternion() {}
//! User-declared copy constructor
Quaternion (const Quaternion<Flt>& rhs)
: w(rhs.w)
, x(rhs.x)
, y(rhs.y)
, z(rhs.z) {}
//! User-declared copy assignment constructor
Quaternion<Flt>& operator= (Quaternion<Flt>& other)
{
std::swap (w, other.w);
std::swap (x, other.x);
std::swap (y, other.y);
std::swap (z, other.z);
return *this;
}
//! Explicitly defaulted move constructor
Quaternion(Quaternion<Flt>&& other) = default;
//! Explicitly defaulted move assignment constructor
Quaternion<Flt>& operator=(Quaternion<Flt>&& other) = default;

alignas(Flt) Flt w;
alignas(Flt) Flt x;
Expand Down Expand Up @@ -114,6 +130,18 @@ namespace morph {
this->z = q2.z;
}

//! Equality operator. True if all elements match
bool operator==(const Quaternion<Flt>& rhs) const
{
return (this->w == rhs.w && this->x == rhs.x && this->y == rhs.y && this->z == rhs.z);
}

//! Not equals
bool operator!=(const Quaternion<Flt>& rhs) const
{
return (this->w != rhs.w || this->x != rhs.x || this->y != rhs.y || this->z != rhs.z);
}

//! Overload * operator. q1 is 'this->'
Quaternion<Flt> operator* (const Quaternion<Flt>& q2) const
{
Expand Down
36 changes: 34 additions & 2 deletions morph/TransformMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,20 @@ namespace morph {
public:
//! Default constructor
TransformMatrix() { this->setToIdentity(); }

TransformMatrix (const TransformMatrix& rhs) : mat(rhs.mat) {}
//! User-declared destructor
~TransformMatrix() {}
//! User-declared copy constructor
TransformMatrix (const TransformMatrix<Flt>& other) : mat(other.mat) {}
//! User-declared copy assignment constructor
TransformMatrix<Flt>& operator= (TransformMatrix<Flt>& other)
{
std::swap (mat, other.mat);
return *this;
}
//! Explicitly defaulted move constructor
TransformMatrix(TransformMatrix<Flt>&& other) = default;
//! Explicitly defaulted move assignment constructor
TransformMatrix<Flt>& operator=(TransformMatrix<Flt>&& other) = default;

/*!
* The transformation matrix data, arranged in column major format to be OpenGL
Expand Down Expand Up @@ -952,6 +964,26 @@ namespace morph {
for (unsigned int i = 0; i<16; ++i) { this->mat[i] *= f; }
}

//! Equality operator. True if all elements match
bool operator==(const TransformMatrix<Flt>& rhs) const
{
unsigned int ndiff = 0;
for (unsigned int i = 0; i < 16 && ndiff == 0; ++i) {
ndiff += this->mat[i] == rhs.mat[i] ? 0 : 1;
}
return ndiff == 0;
}

//! Not equals
bool operator!=(const TransformMatrix<Flt>& rhs) const
{
unsigned int ndiff = 0;
for (unsigned int i = 0; i < 16 && ndiff == 0; ++i) {
ndiff += this->mat[i] == rhs.mat[i] ? 0 : 1;
}
return ndiff > 0;
}

//! Transpose this matrix
void transpose()
{
Expand Down
17 changes: 16 additions & 1 deletion tests/testMatrix33.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ int main()
morph::Matrix33<float> mult1save = mult1;
mult1 *= mult2;
std::cout << "mult1 *= mult2 gives\n" << mult1 << std::endl;
mult1 = mult1save;
mult1 = mult1save; // tests copy
mult1 *= mult2.mat;
std::cout << "mult1 *= mult2.mat gives\n" << mult1 << std::endl;

Expand All @@ -118,5 +118,20 @@ int main()
++rtn;
}

// Test copy
morph::Matrix33<double> md1;
md1.mat[0] = 0;
md1.mat[1] = 1;
md1.mat[2] = 2;
md1.mat[3] = 3;
md1.mat[4] = 4;
md1.mat[5] = 5;
md1.mat[6] = 6;
md1.mat[7] = 7;
md1.mat[8] = 8;

morph::Matrix33<double> md2 = md1;
if (md2 != md1) { ++rtn; }

return rtn;
}
8 changes: 7 additions & 1 deletion tests/testQuaternion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

int main()
{
int rtn = 0;

morph::Quaternion<float> q;
std::cout << q << std::endl;
q.renormalize();
Expand All @@ -14,5 +16,9 @@ int main()
rotationQuaternion.initFromAxisAngle (rotationAxis, angularSpeed);
std::cout << rotationQuaternion << std::endl;

return 0;
morph::Quaternion<float> p = q;
if (p == q) { } else { rtn++; }
if (p != q) { rtn++; }

return rtn;
}
4 changes: 4 additions & 0 deletions tests/testTransformMatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,9 @@ int main()
if ((r[0]==15 && r[1]==17 && r[2]==0 && r[3]==0) == false) {
++rtn;
}

morph::TransformMatrix<float> mult4inv_copy = mult4inv;
if (mult4inv_copy != mult4inv) { ++rtn; }

return rtn;
}

0 comments on commit 95ed762

Please sign in to comment.