Skip to content

Commit

Permalink
added extra tests for Mat4
Browse files Browse the repository at this point in the history
  • Loading branch information
jmacey committed Sep 29, 2023
1 parent afb5261 commit 327f2e7
Show file tree
Hide file tree
Showing 18 changed files with 137 additions and 158 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ find_package(fmt CONFIG REQUIRED)
find_package(gl3w CONFIG REQUIRED)
find_package(OpenMP)
find_package(OpenImageIO CONFIG)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
if(OpenImageIO_FOUND AND(NOT NO_OIIO AND NOT USE_QT))
message("Found OpenImageIO so using that")
add_compile_definitions(USEOIIO)
Expand Down
14 changes: 0 additions & 14 deletions CMakePresets.json

This file was deleted.

2 changes: 1 addition & 1 deletion include/ngl/NGLassert.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
if(!(X)) \
{ \
std::cerr << "Assertion failed :"<< #X \
<< "\nFailure occured on line " \
<< "\nFailure occurred on line " \
<< __LINE__ << " of source file :" \
<< "\n \""<<__FILE__<<"\"\n"; \
}
Expand Down
170 changes: 84 additions & 86 deletions include/ngl/Quaternion.h

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/AABB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void AABB::set(const Vec4 &_corner,Real _x,Real _y, Real _z ) noexcept

Vec3 AABB::getVertexP(const Vec3 &_normal) const noexcept
{
Vec3 res = m_corner.toVec3();
auto res = m_corner.toVec3();

if (_normal.m_x > 0.0f)
{
Expand All @@ -81,7 +81,7 @@ Vec3 AABB::getVertexP(const Vec3 &_normal) const noexcept

Vec3 AABB::getVertexN(const Vec3 &_normal) const noexcept
{
Vec3 res = m_corner.toVec3();
auto res = m_corner.toVec3();

if (_normal.m_x < 0.0f)
{
Expand Down
6 changes: 0 additions & 6 deletions src/AbstractMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
namespace ngl
{





void AbstractMesh::drawBBox() const noexcept
{
m_ext->draw();
Expand Down Expand Up @@ -267,7 +263,6 @@ Real *AbstractMesh::mapVAOVerts() noexcept
{

Real *ptr = nullptr;

// bind our VBO data
m_vaoMesh->bind();
glBindBuffer(GL_ARRAY_BUFFER, m_vaoMesh->getBufferID(0));
Expand All @@ -279,7 +274,6 @@ Real *AbstractMesh::mapVAOVerts() noexcept

void AbstractMesh::unMapVAO() noexcept
{

if(m_vboMapped == true)
{
glUnmapBuffer(GL_ARRAY_BUFFER); // unmap it after use
Expand Down
10 changes: 2 additions & 8 deletions src/BBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ BBox::BBox( const Vec3& _center, Real _width, Real _height, Real _depth, bool _
m_maxY = m_center.m_y+m_height/2.0f;
m_minZ = m_center.m_z-m_depth/2.0f;
m_maxZ = m_center.m_z+m_depth/2.0f;



setVAO();
}

Expand Down Expand Up @@ -138,7 +135,6 @@ BBox::BBox( Real _minX, Real _maxX, Real _minY, Real _maxY, Real _minZ, Real _m

void BBox::setExtents( Real _minX, Real _maxX, Real _minY, Real _maxY, Real _minZ, Real _maxZ ) noexcept
{

m_minX=_minX;
m_maxX=_maxX;
m_minY=_minY;
Expand Down Expand Up @@ -211,7 +207,6 @@ void BBox::setVAO()

void BBox::draw() const noexcept
{

glPolygonMode(GL_FRONT_AND_BACK,m_drawMode);
m_vao->bind();
m_vao->draw();
Expand Down Expand Up @@ -288,8 +283,6 @@ void BBox::recalculate() noexcept
m_maxY=m_height/2.0f;
m_minZ=-m_depth/2.0f;
m_maxZ=m_depth/2.0f;


setVAO();
}

Expand All @@ -298,10 +291,11 @@ void BBox::recalculate() noexcept
BBox::~BBox() noexcept
{
if(m_vao)
{
m_vao->removeVAO();
}
}


} // end namespace ngl


Expand Down
2 changes: 0 additions & 2 deletions src/BezierCurve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ m_vaoCurve->unbind();
Vec3 BezierCurve::getPointOnCurve( Real _value ) const noexcept
{
Vec3 p;

// sum the effect of all CV's on the curve at this point to
// get the evaluated curve point
//
Expand All @@ -150,7 +149,6 @@ Vec3 BezierCurve::getPointOnCurve( Real _value ) const noexcept
p+=val*m_cp[i];
}
}

return p;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Mat2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ Mat2::Mat2(const Real _m) noexcept
#ifdef USEGLM
Mat2::Mat2(const glm::mat2 &_m)
{
memcpy(&m_m[0][0], glm::value_ptr(_m), 4 * sizeof(GLfloat));
memcpy(&m_m[0][0], glm::value_ptr(_m), 4 * sizeof(Real));
}

glm::mat2 Mat2::toGLM() const
{
glm::mat2 result;
memcpy(glm::value_ptr(result), &m_m[0][0], 4 * sizeof(GLfloat));
memcpy(glm::value_ptr(result), &m_m[0][0], 4 * sizeof(Real));
return result;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Mat4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ Mat4::Mat4(const Mat4 &_m) noexcept
#ifdef USEGLM
Mat4::Mat4(const glm::mat4 &_m)
{
memcpy(&m_m[0][0], &_m[0][0], 16 * sizeof(GLfloat));
memcpy(&m_m[0][0], &_m[0][0], 16 * sizeof(Real));
}

glm::mat4 Mat4::toGLM() const
{
glm::mat4 result;
memcpy(&result[0][0], &m_m[0][0], 16 * sizeof(GLfloat));
memcpy(&result[0][0], &m_m[0][0], 16 * sizeof(Real));

return result;
}
Expand Down
6 changes: 0 additions & 6 deletions src/MultiBufferVAO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ namespace ngl

}




void MultiBufferVAO::setData(size_t _size, const std::vector<Vec3> &_data)
{
if(m_bound == false)
Expand Down Expand Up @@ -206,7 +203,4 @@ namespace ngl
}
return ptr;
}



}
1 change: 0 additions & 1 deletion src/Obj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,4 +520,3 @@ bool Obj::parseFaceVertexNormalUV(std::vector< std::string > &_tokens) noexcept
}

} // namespace ngl
//----------------------------------------------------------------------------------------------------------------------
3 changes: 2 additions & 1 deletion src/Plane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ Plane::Plane( const Vec3 &_v1, const Vec3 &_v2, const Vec3 &_v3) noexcept

void Plane::setPoints(const Vec3 &_v1, const Vec3 &_v2, const Vec3 &_v3) noexcept
{
Vec3 aux1, aux2;
Vec3 aux1;
Vec3 aux2;

aux1 = _v1 - _v2;
aux2 = _v3 - _v2;
Expand Down
28 changes: 8 additions & 20 deletions src/Quaternion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@
namespace ngl
{

//----------------------------------------------------------------------------------------------------------------------
Quaternion::Quaternion(const Mat4 &_m) noexcept
{

{
if ( auto T = 1 + _m.m_openGL[0] + _m.m_openGL[5] + _m.m_openGL[10]; T > 0.00000001f ) //to avoid large distortions!
{
auto S = sqrtf(T) * 2.0f;
Expand Down Expand Up @@ -82,7 +80,6 @@ Quaternion::Quaternion(const Vec3 &_rot) noexcept

}

//----------------------------------------------------------------------------------------------------------------------
Quaternion Quaternion::operator *(const Quaternion& _q)const noexcept
{
Quaternion ret(0.0,0.0,0.0,0.0);
Expand All @@ -104,15 +101,14 @@ Quaternion Quaternion::operator *(const Quaternion& _q)const noexcept
return ret;
}

//----------------------------------------------------------------------------------------------------------------------

Quaternion & Quaternion::operator *=(const Quaternion& _q) noexcept
{
// as we have already written the code to do the mult above re-use
*this=*this*_q;
return *this;
}

//----------------------------------------------------------------------------------------------------------------------
Quaternion Quaternion::operator +(const Quaternion& _q) const noexcept
{
Quaternion ret;
Expand All @@ -123,7 +119,6 @@ Quaternion Quaternion::operator +(const Quaternion& _q) const noexcept
return ret;
}

//----------------------------------------------------------------------------------------------------------------------
Quaternion Quaternion::operator -(const Quaternion& _q) const noexcept
{
Quaternion ret;
Expand All @@ -134,8 +129,6 @@ Quaternion Quaternion::operator -(const Quaternion& _q) const noexcept
return ret;
}


//----------------------------------------------------------------------------------------------------------------------
Quaternion & Quaternion::operator +=(const Quaternion& _q) noexcept
{
m_s=m_s+_q.m_s;
Expand All @@ -145,7 +138,7 @@ Quaternion & Quaternion::operator +=(const Quaternion& _q) noexcept
return *this;

}
//----------------------------------------------------------------------------------------------------------------------

Quaternion &Quaternion::operator -=(const Quaternion& _q) noexcept
{
m_s=m_s-_q.m_s;
Expand All @@ -155,14 +148,14 @@ Quaternion &Quaternion::operator -=(const Quaternion& _q) noexcept
return *this;
}

//----------------------------------------------------------------------------------------------------------------------

Quaternion Quaternion::operator *(Real _s) const noexcept
{
return Quaternion(m_s*_s,m_x*_s,m_y*_s,m_z*_s);

}

//----------------------------------------------------------------------------------------------------------------------

void Quaternion::operator *=(Real _s) noexcept
{
m_s*=_s;
Expand All @@ -171,9 +164,6 @@ void Quaternion::operator *=(Real _s) noexcept
m_z*=_s;
}



//----------------------------------------------------------------------------------------------------------------------
void Quaternion::normalise() noexcept
{
Real inverseOverOne = 1.0f/magnitude();
Expand All @@ -194,13 +184,12 @@ Quaternion Quaternion::inverse() const noexcept
}


//----------------------------------------------------------------------------------------------------------------------

Real Quaternion::magnitude()const noexcept
{
return static_cast<Real>( sqrtf(m_s*m_s + m_x*m_x + m_y*m_y + m_z*m_z) );
}

//----------------------------------------------------------------------------------------------------------------------
bool Quaternion::operator == (const Quaternion& _q)const noexcept
{
return (
Expand All @@ -212,7 +201,7 @@ bool Quaternion::operator == (const Quaternion& _q)const noexcept
}


//----------------------------------------------------------------------------------------------------------------------

Vec4 Quaternion::operator* (const Vec4 &_vec) const noexcept
{
Quaternion temp=-*this;
Expand Down Expand Up @@ -264,7 +253,6 @@ void Quaternion::rotateZ(Real _angle) noexcept

}


void Quaternion::fromAxisAngle(const Vec3& _axis, Real _angle) noexcept
{
Vec3 axis = _axis;
Expand Down Expand Up @@ -444,7 +432,7 @@ Mat4 Quaternion::toMat4Transpose() const noexcept


} // end ngl namespace
//----------------------------------------------------------------------------------------------------------------------




2 changes: 1 addition & 1 deletion tests/Mat2Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ TEST(Mat2, Vec2xMat2)
TEST(Mat2, glm)
{
glm::mat2 g1(1, 2, 3, 4);
ngl::Mat2 n1(1, 2, 3, 4);
ngl::Mat2 n1(g1);
EXPECT_TRUE(g1[0][0] == n1.m_00);
EXPECT_TRUE(g1[0][1] == n1.m_01);
EXPECT_TRUE(g1[1][0] == n1.m_10);
Expand Down
20 changes: 20 additions & 0 deletions tests/Mat4Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <glm/mat4x4.hpp>
#include <gtest/gtest.h>
#include <ngl/Mat4.h>
#include <ngl/Quaternion.h>
#include <ngl/Types.h>
#include <ngl/Vec4.h>
#include <sstream>
Expand Down Expand Up @@ -300,3 +301,22 @@ TEST(Mat4, toGLM)
EXPECT_TRUE(test == result);
}


TEST(Mat4,asQuaternion)
{
ngl::Mat4 test(1.0f);
auto q=test.asQuaternion();
EXPECT_FLOAT_EQ(q.m_s,0.0f);
EXPECT_FLOAT_EQ(q.m_x,0.0f);
EXPECT_FLOAT_EQ(q.m_y,0.0f);
EXPECT_FLOAT_EQ(q.m_z,1.0f);

auto toQuat=ngl::Mat4::rotateX(90.0f);
auto q2=toQuat.asQuaternion();
EXPECT_FLOAT_EQ(q2.m_s,0.70710677f);
EXPECT_FLOAT_EQ(q2.m_x,0.0f);
EXPECT_FLOAT_EQ(q2.m_y,0.0f);
EXPECT_FLOAT_EQ(q2.m_z,0.707107f);
auto res=ngl::Quaternion(0.70710677f,0.0f,0.0f,0.70710677f);
EXPECT_TRUE(q2==res);
}

0 comments on commit 327f2e7

Please sign in to comment.