diff --git a/CMakeLists.txt b/CMakeLists.txt index ba80b4b5..37016eb6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -256,6 +256,8 @@ if(NOT DEFINED PYNGL_ONLY) ${CMAKE_SOURCE_DIR}/tests/QuaternionTests.cpp ${CMAKE_SOURCE_DIR}/tests/ImageTests.cpp ${CMAKE_SOURCE_DIR}/tests/HashTests.cpp + ${CMAKE_SOURCE_DIR}/tests/PointBakeTests.cpp + ) add_compile_definitions(GLM_ENABLE_EXPERIMENTAL) target_link_libraries(NGLTests PRIVATE glfw PRIVATE GTest::gtest ) diff --git a/include/ngl/NCCAPointBake.h b/include/ngl/NCCAPointBake.h index 6165ca19..c4d04788 100644 --- a/include/ngl/NCCAPointBake.h +++ b/include/ngl/NCCAPointBake.h @@ -84,16 +84,6 @@ class NGL_DLLEXPORT NCCAPointBake //---------------------------------------------------------------------------------------------------------------------- bool loadPointBake(std::string_view _fileName) noexcept; - //---------------------------------------------------------------------------------------------------------------------- - /// @brief method to load a binary point baked file - /// @param[in] _fileName the file to load - //---------------------------------------------------------------------------------------------------------------------- - bool loadBinaryPointBake(std::string_view _fileName) noexcept; - //---------------------------------------------------------------------------------------------------------------------- - /// @brief method to save a binary point baked file basically re-ordered data only - /// @param[in] _fileName the file to load - //---------------------------------------------------------------------------------------------------------------------- - bool saveBinaryPointBake(std::string_view _fileName) noexcept; //---------------------------------------------------------------------------------------------------------------------- /// @brief method to attach a mesh to the data /// this method will check for basic vetex compatibility and then re-order the data @@ -115,6 +105,15 @@ class NGL_DLLEXPORT NCCAPointBake { return m_numFrames - 1; } + size_t getStartFrame() const noexcept + { + return m_startFrame; + } + size_t getEndFrame() const noexcept + { + return m_endFrame; + } + //---------------------------------------------------------------------------------------------------------------------- /// @brief return the number of verts loaded from the PointBake file /// @returns the number of verts diff --git a/include/ngl/Random.h b/include/ngl/Random.h index 6c844a05..11c555dc 100644 --- a/include/ngl/Random.h +++ b/include/ngl/Random.h @@ -112,13 +112,13 @@ class NGL_DLLEXPORT Random /// @param _dist the distribution to add //---------------------------------------------------------------------------------------------------------------------- - static void addIntGenerator(std::string_view _name, std::uniform_int_distribution< int > &_dist); + static void addIntGenerator(std::string_view _name, const std::uniform_int_distribution< int > &_dist); //---------------------------------------------------------------------------------------------------------------------- /// @brief add a generator to the float generators /// @param _name the name of the generator to use for the number /// @param _dist the distribution to add //---------------------------------------------------------------------------------------------------------------------- - static void addFloatGenerator(std::string_view _name, std::uniform_real_distribution< float > &_dist); + static void addFloatGenerator(std::string_view _name, const std::uniform_real_distribution< float > &_dist); //---------------------------------------------------------------------------------------------------------------------- /// @brief get a random vector with componets ranged from +/- 1 //---------------------------------------------------------------------------------------------------------------------- diff --git a/src/Image.cpp b/src/Image.cpp index 95f16019..1f781a85 100644 --- a/src/Image.cpp +++ b/src/Image.cpp @@ -18,6 +18,8 @@ /// @file Image.cpp /// @brief implementation files for Image class //---------------------------------------------------------------------------------------------------------------------- +#include + #include "Image.h" #include "NGLassert.h" #include "pystring.h" @@ -40,7 +42,6 @@ enum class ImageLibrary : char { QImage, OIIO, BuiltIn }; #endif -#include namespace ngl { diff --git a/src/NCCAPointBake.cpp b/src/NCCAPointBake.cpp index 06d4c478..58ce1ae3 100644 --- a/src/NCCAPointBake.cpp +++ b/src/NCCAPointBake.cpp @@ -40,7 +40,7 @@ bool NCCAPointBake::loadPointBake(std::string_view _fileName) noexcept m_endFrame = 0; m_mesh = nullptr; m_binFile = false; - rapidxml::xml_node<> *rootNode; + const rapidxml::xml_node<> *rootNode; // Read the xml file into a vector std::ifstream xmlFile(_fileName.data()); if(!xmlFile.is_open()) @@ -60,7 +60,7 @@ bool NCCAPointBake::loadPointBake(std::string_view _fileName) noexcept return false; } - rapidxml::xml_node<> *child = rootNode->first_node("MeshName"); + auto child = rootNode->first_node("MeshName"); m_meshName = child->value(); NGLMessage::addMessage(fmt::format("found mesh{0} ", m_meshName)); @@ -72,7 +72,6 @@ bool NCCAPointBake::loadPointBake(std::string_view _fileName) noexcept child = rootNode->first_node("StartFrame"); m_startFrame = std::stoul(child->value()); NGLMessage::addMessage(fmt::format("StartFrame {0}", m_startFrame)); - ; child = rootNode->first_node("EndFrame"); m_endFrame = std::stoul(child->value()); NGLMessage::addMessage(fmt::format("EndFrame {0}", m_endFrame)); @@ -108,7 +107,7 @@ bool NCCAPointBake::loadPointBake(std::string_view _fileName) noexcept CurrentFrame = std::stoul(child->first_attribute("number")->value()); CurrentFrame -= m_startFrame; - for(rapidxml::xml_node<> *vertex = child->first_node("Vertex"); vertex; vertex = vertex->next_sibling()) + for(auto vertex = child->first_node("Vertex"); vertex; vertex = vertex->next_sibling()) { size_t index = std::stoul(vertex->first_attribute("number")->value()); lineBuffer = vertex->value(); @@ -143,92 +142,6 @@ void NCCAPointBake::setFrame(const size_t _frame) noexcept m_currFrame = _frame; } -bool NCCAPointBake::loadBinaryPointBake(std::string_view _fileName) noexcept -{ - // open a file stream for ip in binary mode - std::fstream file; - file.open(_fileName.data(), std::ios::in | std::ios::binary); - // see if it worked - if(!file.is_open()) - { - NGLMessage::addError(fmt::format("problems Opening File {0}", _fileName.data())); - return false; - } - // lets read in the header and see if the file is valid - std::string header; - file.read(&header[0], 10 * sizeof(char)); - header[10] = 0; // for strcmp we need \n - // basically I used the magick string ngl::bin (I presume unique in files!) and - // we test against it. - if(header == "ngl::binpb") - { - // best close the file and exit - file.close(); - NGLMessage::addError("this is not an ngl::binpb file "); - return false; - } - - /// The number of vertices in the object - // file.read(reinterpret_cast (&m_nVerts),sizeof(unsigned long int)); - - file.read(reinterpret_cast< char * >(&m_numFrames), sizeof(unsigned int)); - file.read(reinterpret_cast< char * >(&m_currFrame), sizeof(unsigned int)); - file.read(reinterpret_cast< char * >(&m_nVerts), sizeof(unsigned int)); - file.read(reinterpret_cast< char * >(&m_startFrame), sizeof(unsigned int)); - file.read(reinterpret_cast< char * >(&m_binFile), sizeof(bool)); - NGLMessage::addMessage("Loaded header\n"); - - m_data.resize(m_numFrames); - for(unsigned int frame = 0; frame < m_numFrames; ++frame) - { - m_data[frame].resize(m_nVerts); - for(unsigned int v = 0; v < m_nVerts; ++v) - { - file.read(reinterpret_cast< char * >(&m_data[frame][v].m_x), sizeof(Real)); - file.read(reinterpret_cast< char * >(&m_data[frame][v].m_y), sizeof(Real)); - file.read(reinterpret_cast< char * >(&m_data[frame][v].m_z), sizeof(Real)); - } - } - return true; -} - -bool NCCAPointBake::saveBinaryPointBake(std::string_view _fileName) noexcept -{ - // so basically we need to save all the state data from the abstract mesh - // then map the vbo on the gpu and dump that in one go, this means we have to - // call CreateVBO first the Save - std::fstream file; - file.open(_fileName.data(), std::ios::out | std::ios::binary); - if(!file.is_open()) - { - NGLMessage::addError(fmt::format("problems Opening File {0} ", _fileName.data())); - return false; - } - - // lets write out our own Magic Number file ID - const std::string header("ngl::binpb"); - file.write(header.c_str(), static_cast< long >(header.length())); - m_binFile = true; - file.write(reinterpret_cast< char * >(&m_numFrames), sizeof(unsigned int)); - file.write(reinterpret_cast< char * >(&m_currFrame), sizeof(unsigned int)); - file.write(reinterpret_cast< char * >(&m_nVerts), sizeof(unsigned int)); - file.write(reinterpret_cast< char * >(&m_startFrame), sizeof(unsigned int)); - file.write(reinterpret_cast< char * >(&m_binFile), sizeof(bool)); - - // now write out data - for(unsigned int frame = 0; frame < m_numFrames; ++frame) - { - for(unsigned int v = 0; v < m_nVerts; ++v) - { - file.write(reinterpret_cast< char * >(&m_data[frame][v].m_x), sizeof(Real)); - file.write(reinterpret_cast< char * >(&m_data[frame][v].m_y), sizeof(Real)); - file.write(reinterpret_cast< char * >(&m_data[frame][v].m_z), sizeof(Real)); - } - } - - file.close(); - return true; -} void NCCAPointBake::setMeshToFrame(const unsigned int _frame) noexcept { diff --git a/src/Quaternion.cpp b/src/Quaternion.cpp index 78298a20..5bbcb535 100644 --- a/src/Quaternion.cpp +++ b/src/Quaternion.cpp @@ -38,8 +38,7 @@ Quaternion::Quaternion(const Mat4 &_m) noexcept m_z = ( _m.m_openGL[1] - _m.m_openGL[4] ) / S; m_s = 0.25f * S; } - else - if ( _m.m_openGL[0] > _m.m_openGL[5] && + else if ( _m.m_openGL[0] > _m.m_openGL[5] && _m.m_openGL[0] > _m.m_openGL[10] ) { // Column 0: auto S = static_cast(sqrtf( 1.0f + _m.m_openGL[0] - _m.m_openGL[5] - _m.m_openGL[10] ) * 2.0f); @@ -48,8 +47,7 @@ Quaternion::Quaternion(const Mat4 &_m) noexcept m_z = (_m.m_openGL[8] + _m.m_openGL[2] ) / S; m_s = (_m.m_openGL[6] - _m.m_openGL[9] ) / S; } - else - if ( _m.m_openGL[5] > _m.m_openGL[10] ) + else if ( _m.m_openGL[5] > _m.m_openGL[10] ) { // Column 1: auto S = sqrtf( 1.0f + _m.m_openGL[5] - _m.m_openGL[0] - _m.m_openGL[10] ) * 2.0f; m_x = (_m.m_openGL[1] + _m.m_openGL[4] ) / S; @@ -272,8 +270,8 @@ void Quaternion::fromAxisAngle(const Vec3& _axis, Real _angle) noexcept Vec3 axis = _axis; axis.normalize(); _angle=radians(_angle); - Real sinAngle = static_cast(sinf( _angle / 2.0f )); - Real cosAngle = static_cast(cosf( _angle / 2.0f )); + auto sinAngle = sinf( _angle / 2.0f ); + auto cosAngle = cosf( _angle / 2.0f ); m_s = cosAngle; m_x = axis.m_x * sinAngle; m_y = axis.m_y * sinAngle; @@ -283,12 +281,12 @@ void Quaternion::fromAxisAngle(const Vec3& _axis, Real _angle) noexcept void Quaternion::fromEulerAngles(const Real _x,const Real _y,const Real _z) noexcept { - Real sx = sinf(radians(_x/2.0f)); - Real sy = sinf(radians(_y/2.0f)); - Real sz = sinf(radians(_z/2.0f)); - Real cx = cosf(radians(_x/2.0f)); - Real cy = cosf(radians(_y/2.0f)); - Real cz = cosf(radians(_z/2.0f)); + auto sx = sinf(radians(_x/2.0f)); + auto sy = sinf(radians(_y/2.0f)); + auto sz = sinf(radians(_z/2.0f)); + auto cx = cosf(radians(_x/2.0f)); + auto cy = cosf(radians(_y/2.0f)); + auto cz = cosf(radians(_z/2.0f)); m_s=cx*cy*cz + sx*sy*sz; m_x=sx*cy*cz - cx*sy*sz; @@ -341,7 +339,7 @@ void Quaternion::negate() Quaternion Quaternion::slerp( Quaternion _v0, Quaternion _v1, Real _t) noexcept { - float dotp = dot(_v0,_v1); + auto dotp = dot(_v0,_v1); constexpr float thereshold=0.9995f; if( dotp < 0.0f) { @@ -359,7 +357,7 @@ Quaternion Quaternion::slerp( Quaternion _v0, Quaternion _v1, Real _t) noexcep auto theta = theta_0*_t; Quaternion v2 = _v1 - _v0 * dotp; v2.normalise(); - return _v0 * cosf(theta) + v2 * sin(theta); + return _v0 * cosf(theta) + v2 * sinf(theta); } @@ -367,16 +365,16 @@ Quaternion Quaternion::slerp( Quaternion _v0, Quaternion _v1, Real _t) noexcep Mat4 Quaternion::toMat4() const noexcept { // written by Rob Bateman - // sacrafice a few bytes to pre-calculate some values - Real xx = m_x * m_x; - Real xy = m_x * m_y; - Real xz = m_x * m_z; - Real xs = m_x * m_s; - Real yy = m_y * m_y; - Real yz = m_y * m_z; - Real ys = m_y * m_s; - Real zz = m_z * m_z; - Real zs = m_z * m_s; + // sacrifice a few bytes to pre-calculate some values + auto xx = m_x * m_x; + auto xy = m_x * m_y; + auto xz = m_x * m_z; + auto xs = m_x * m_s; + auto yy = m_y * m_y; + auto yz = m_y * m_z; + auto ys = m_y * m_s; + auto zz = m_z * m_z; + auto zs = m_z * m_s; Mat4 o; o.m_openGL[0] = 1.0f - 2.0f * (yy+zz); o.m_openGL[1] = 2.0f * (xy+zs); diff --git a/src/Random.cpp b/src/Random.cpp index 2afcc0c8..04999025 100644 --- a/src/Random.cpp +++ b/src/Random.cpp @@ -92,12 +92,12 @@ int Random::getIntFromGeneratorName(std::string_view _name) } } -void Random::addIntGenerator(std::string_view _name, std::uniform_int_distribution< int > &_dist) +void Random::addIntGenerator(std::string_view _name,const std::uniform_int_distribution< int > &_dist) { m_intGenerators[_name.data()] = _dist; } -void Random::addFloatGenerator(std::string_view _name, std::uniform_real_distribution< float > &_dist) +void Random::addFloatGenerator(std::string_view _name, const std::uniform_real_distribution< float > &_dist) { m_floatGenerators[_name.data()] = _dist; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt deleted file mode 100644 index c62f6d60..00000000 --- a/tests/CMakeLists.txt +++ /dev/null @@ -1,121 +0,0 @@ -cmake_minimum_required(VERSION 3.2) - -# Name of the project -project(NGLTestBuild VERSION 8.0 LANGUAGES CXX) -# Set to C++ 17 -set(CMAKE_CXX_STANDARD 17) - -find_package(GTest CONFIG REQUIRED) -include(GoogleTest) -enable_testing() - -find_package(glfw3 CONFIG REQUIRED) -find_package(OpenImageIO CONFIG REQUIRED) -find_package(IlmBase CONFIG REQUIRED) -find_package(OpenEXR CONFIG REQUIRED) -find_package(glm CONFIG REQUIRED) -find_package(fmt CONFIG REQUIRED) - -#Bring the headers into the project (local ones) -include_directories(include ${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/gl3w ) -add_definitions(-DUSEGLM -DGLM_ENABLE_EXPERIMENTAL) -# define this if you want to include the stanford data sets -# these are very big and make compilation time huge -add_definitions(-DADDLARGEMODELS) -#This defines the image lib to use by default use QIMAGE, I need to fix this for the others -add_definitions(-DUSEOIIO) -#This defines that we are using the header only version of the fmt lib -add_definitions(-DFMT_HEADER_ONLY) -add_definitions(-DUSEGLM) -add_definitions(-DGLM_ENABLE_EXPERIMENTAL) - -#the file(GLOB...) allows for wildcard additions of our src dir -set(SOURCES - ${PROJECT_SOURCE_DIR}/main.cpp - ${PROJECT_SOURCE_DIR}/MessageQueueTests.cpp - ${PROJECT_SOURCE_DIR}/UtilTests.cpp - ${PROJECT_SOURCE_DIR}/Vec4Tests.cpp - ${PROJECT_SOURCE_DIR}/Mat2Tests.cpp - ${PROJECT_SOURCE_DIR}/NGLInitTests.cpp - ${PROJECT_SOURCE_DIR}/VAOPrimitivesTests.cpp - ${PROJECT_SOURCE_DIR}/Mat3Tests.cpp - ${PROJECT_SOURCE_DIR}/ObjTests.cpp - ${PROJECT_SOURCE_DIR}/Vec2Tests.cpp - ${PROJECT_SOURCE_DIR}/Mat4Tests.cpp - ${PROJECT_SOURCE_DIR}/ShaderLibTests.cpp - ${PROJECT_SOURCE_DIR}/Vec3Tests.cpp - ${PROJECT_SOURCE_DIR}/RandomTests.cpp - ${PROJECT_SOURCE_DIR}/QuaternionTests.cpp - ${PROJECT_SOURCE_DIR}/ImageTests.cpp -) - -# see what platform we are on and set platform defines -if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") - message("Mac build") - find_library(MACGL OpenGL) - set ( PROJECT_LINK_LIBS -lNGL ${MACGL}) - -elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux") - set ( PROJECT_LINK_LIBS -L/home/jmacey/NGL/lib -lNGL -lGL -lpthread ) -endif() - - -# Find includes in corresponding build directories -set(CMAKE_INCLUDE_CURRENT_DIR ON) - - -if(WIN32) - #Not defined in msvc compiler for cmaths so must set definition manually here - add_definitions(-D_USE_MATH_DEFINES) - - add_definitions(-DNOMINMAX) - add_definitions(-DNO_DLL) -else() - #Not a defined option for msvc - fine for gcc and clang - set(CMAKE_CXX_FLAGS "-Wno-register") - set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -g") -endif() - - -# now add NGL specific values -# add exe and link libs that must be after the other defines -add_executable(NGLTests ${SOURCES}) - -link_directories( ${NGLROOT}/lib ) -find_package(glfw3 CONFIG REQUIRED) -find_package(OpenEXR CONFIG REQUIRED) - -target_link_libraries(NGLTests PRIVATE glfw PRIVATE GTest::gtest ) -#target_link_libraries(NGLTests PRIVATE OpenImageIO::OpenImageIO OpenImageIO::OpenImageIO_Util) -target_link_libraries(NGLTests PRIVATE OpenImageIO::OpenImageIO) - -# see what platform we are on and set platform defines -if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") - message("Doing mac OpenGL") - find_library(MACGL OpenGL) - add_definitions(-DGL_SILENCE_DEPRECATION) - set(EXTRALIBS ${MACGL}) -elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux") -endif() - - -if(${CMAKE_SYSTEM_NAME} MATCHES "Windows") - target_link_libraries( NGLTests PRIVATE $(HOMEDRIVE)\\$(HOMEPATH)\\NGL\\lib\\NGL.lib ) -else() -target_link_libraries(NGLTests PRIVATE NGL PRIVATE ${EXTRALIBS} ) - -endif() - -#target_link_libraries(${NGLTests} ${PROJECT_LINK_LIBS} ) -gtest_discover_tests(NGLTests) - - -add_custom_target(CopyTestfiles ALL - COMMAND ${CMAKE_COMMAND} -E copy_directory - ${CMAKE_CURRENT_SOURCE_DIR}/files - ${CMAKE_BINARY_DIR}/tests/files - COMMENT "Copy Test files to build directory" - ) - -#gtest_add_tests(TARGET NGLTests TEST_PREFIX Vec3.) -#add_test(NAME monolithic COMMAND NGLTests) \ No newline at end of file diff --git a/tests/PointBakeTests.cpp b/tests/PointBakeTests.cpp new file mode 100644 index 00000000..8d06aa51 --- /dev/null +++ b/tests/PointBakeTests.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include + +TEST(PointBake,defaultctor) +{ + auto pb=ngl::NCCAPointBake(); + EXPECT_EQ(pb.getNumFrames(),-1); + EXPECT_EQ(pb.getNumVerts(),0); + EXPECT_EQ(pb.getStartFrame(),0); + EXPECT_EQ(pb.getEndFrame(),0); +} + +TEST(PointBake,filector) +{ + auto pb=ngl::NCCAPointBake("files/Tri.xml"); + EXPECT_EQ(pb.getNumFrames(),2); + EXPECT_EQ(pb.getNumVerts(),3); + EXPECT_EQ(pb.getStartFrame(),1); + EXPECT_EQ(pb.getEndFrame(),3); +} + + +TEST(PointBake,addMesh) +{ + auto pb=ngl::NCCAPointBake("files/Tri.xml"); + auto mesh=ngl::Obj("models/Tri.obj"); + EXPECT_TRUE(pb.attachMesh(&mesh)); +} + +TEST(PointBake,meshAt) +{ + auto pb=ngl::NCCAPointBake("files/Tri.xml"); + auto verts=pb.getRawDataPointerAtFrame(0); + EXPECT_TRUE(verts.size()==3); + EXPECT_TRUE(verts[0]==ngl::Vec3(2.0f,0.0f,0.0f)); + EXPECT_TRUE(verts[1]==ngl::Vec3(0.0f,4.0f,0.0f)); + EXPECT_TRUE(verts[2]==ngl::Vec3(-2.0f,0.0f,0.0f)); + + verts=pb.getRawDataPointerAtFrame(1); + EXPECT_TRUE(verts[0]==ngl::Vec3(3.0f,0.0f,0.0f)); + EXPECT_TRUE(verts[1]==ngl::Vec3(0.0f,5.0f,0.0f)); + EXPECT_TRUE(verts[2]==ngl::Vec3(-3.0f,0.0f,0.0f)); + + verts=pb.getRawDataPointerAtFrame(2); + EXPECT_TRUE(verts[0]==ngl::Vec3(4.0f,0.0f,0.0f)); + EXPECT_TRUE(verts[1]==ngl::Vec3(0.0f,6.0f,0.0f)); + EXPECT_TRUE(verts[2]==ngl::Vec3(-4.0f,0.0f,0.0f)); + +} + diff --git a/tests/files/Tri.xml b/tests/files/Tri.xml new file mode 100644 index 00000000..a0600dae --- /dev/null +++ b/tests/files/Tri.xml @@ -0,0 +1,24 @@ + + + Triangle + 3 + 1 + 3 + 3 + absolute + + 2.00000 0.00000 0.000000 + 0.0000 4.0000 0.000000 + -2.00000 0.000000 0.000000 + + + 3.00000 0.00000 0.000000 + 0.0000 5.0000 0.000000 + -3.00000 0.000000 0.000000 + + + 4.00000 0.00000 0.000000 + 0.0000 6.0000 0.000000 + -4.00000 0.000000 0.000000 + +