Skip to content

Commit

Permalink
added tests for NGLPointBake
Browse files Browse the repository at this point in the history
  • Loading branch information
jmacey committed Sep 26, 2023
1 parent eb3531c commit d9202ea
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 250 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
19 changes: 9 additions & 10 deletions include/ngl/NCCAPointBake.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions include/ngl/Random.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
//----------------------------------------------------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion src/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
/// @file Image.cpp
/// @brief implementation files for Image class
//----------------------------------------------------------------------------------------------------------------------
#include <iostream>

#include "Image.h"
#include "NGLassert.h"
#include "pystring.h"
Expand All @@ -40,7 +42,6 @@ enum class ImageLibrary : char { QImage, OIIO, BuiltIn };
#endif


#include <iostream>

namespace ngl
{
Expand Down
93 changes: 3 additions & 90 deletions src/NCCAPointBake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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));

Expand All @@ -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));
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 <char *> (&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
{
Expand Down
46 changes: 22 additions & 24 deletions src/Quaternion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Real>(sqrtf( 1.0f + _m.m_openGL[0] - _m.m_openGL[5] - _m.m_openGL[10] ) * 2.0f);
Expand All @@ -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;
Expand Down Expand Up @@ -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<Real>(sinf( _angle / 2.0f ));
Real cosAngle = static_cast<Real>(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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)
{
Expand All @@ -359,24 +357,24 @@ 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);
}



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);
Expand Down
4 changes: 2 additions & 2 deletions src/Random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit d9202ea

Please sign in to comment.