Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve test coverage for == operator of std::data #46

Merged
merged 1 commit into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 14 additions & 19 deletions include/meshio/details/stl.inl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace internal {

template<typename T = float>
bool readAsciiSTL(std::vector< meshio::stl::Data<T> > &pObjects,
void readAsciiSTL(std::vector< meshio::stl::Data<T> > &pObjects,
const char* pFileName)
{
auto readNormal = [](const std::string& pLine) -> Vec3<float> {
Expand All @@ -30,13 +30,12 @@ bool readAsciiSTL(std::vector< meshio::stl::Data<T> > &pObjects,
return V;
};

std::ifstream objFile(pFileName);
std::ifstream stlFile(pFileName);
std::string line;

if (!objFile.is_open())
return false;
const bool isFileOpen = stlFile.is_open();

while (std::getline(objFile, line)) {
while (isFileOpen && std::getline(stlFile, line)) {
std::stringstream lineSS(line);
std::string keyWord;

Expand All @@ -47,7 +46,7 @@ bool readAsciiSTL(std::vector< meshio::stl::Data<T> > &pObjects,
bool isFacetRead = false;
unsigned outerCount = 0;

while (std::getline(objFile, line)) {
while (std::getline(stlFile, line)) {
std::stringstream ss(line);
std::string key;

Expand Down Expand Up @@ -85,19 +84,20 @@ bool readAsciiSTL(std::vector< meshio::stl::Data<T> > &pObjects,
pObjects.push_back(currObj);
}
}
objFile.close();

return true;
stlFile.close();
}

/*
* Reads binary STL file assuming the format as described in
* https://en.wikipedia.org/wiki/STL_(file_format). After the end of first
* object, we assume that the next object's information starts with number of
* triangles and the format continues.
*
* Note that this function assumes valid file path and it's existence.
* All necessary checks are carried out in stl::read wrapper.
*/
template<typename T = float>
bool readBinarySTL(std::vector< meshio::stl::Data<T> > &pObjects,
void readBinarySTL(std::vector< meshio::stl::Data<T> > &pObjects,
const char* pFileName)
{
uint32_t numTriangles;
Expand All @@ -110,11 +110,6 @@ bool readBinarySTL(std::vector< meshio::stl::Data<T> > &pObjects,

std::stringstream strErr;
std::ifstream ifs(pFileName, std::ios::binary | std::ios::in);
if (!ifs) {
strErr << "Cannot open file (" << pFileName << ")" << std::endl;
std::cerr << strErr.str() << std::endl;
return false;
}

char header[80];
ifs.read(&header[0], 80);
Expand Down Expand Up @@ -162,8 +157,6 @@ bool readBinarySTL(std::vector< meshio::stl::Data<T> > &pObjects,
}

ifs.close();

return true;
}

template<typename T = float>
Expand Down Expand Up @@ -282,9 +275,11 @@ bool read(std::vector< meshio::stl::Data<T> > &pObjects,
ifs.close();

if (line.substr(0, 5) == "solid")
return internal::readAsciiSTL<T>(pObjects, pFileName);
internal::readAsciiSTL<T>(pObjects, pFileName);
else
return internal::readBinarySTL<T>(pObjects, pFileName);
internal::readBinarySTL<T>(pObjects, pFileName);

return true;
}

template<typename T>
Expand Down
70 changes: 70 additions & 0 deletions test/stl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <gtest/gtest.h>
#include <meshio/stl.hpp>
#include <meshio/vectors.hpp>
#include <testHelpers.hpp>

using namespace std;
Expand Down Expand Up @@ -101,3 +102,72 @@ TEST(STL, CROSS_CHECK)

EXPECT_TRUE(binReadObjs[0] == asciiReadObjs[0]);
}

TEST(STL, PositionsCountMismatch)
{
vector< stl::Data<float> > binReadObjs;
stl::read<float>(binReadObjs, TEST_DIR "/cube_binary.stl");

vector< stl::Data<float> > asciiReadObjs;
stl::read<float>(asciiReadObjs, TEST_DIR "/cube_ascii.stl");

asciiReadObjs[0].mPositions.erase(
asciiReadObjs[0].mPositions.begin() +
asciiReadObjs[0].mPositions.size() - 1);

EXPECT_FALSE(binReadObjs[0] == asciiReadObjs[0]);
}

TEST(STL, NormalsCountMismatch)
{
vector< stl::Data<float> > binReadObjs;
stl::read<float>(binReadObjs, TEST_DIR "/cube_binary.stl");

vector< stl::Data<float> > asciiReadObjs;
stl::read<float>(asciiReadObjs, TEST_DIR "/cube_ascii.stl");

asciiReadObjs[0].mNormals.erase(
asciiReadObjs[0].mNormals.begin() +
asciiReadObjs[0].mNormals.size() - 1);

EXPECT_FALSE(binReadObjs[0] == asciiReadObjs[0]);
}

TEST(STL, PositionsValuesMismatch)
{
vector< stl::Data<float> > binReadObjs;
stl::read<float>(binReadObjs, TEST_DIR "/cube_binary.stl");

vector< stl::Data<float> > asciiReadObjs;
stl::read<float>(asciiReadObjs, TEST_DIR "/cube_ascii.stl");

asciiReadObjs[0].mPositions[0] = meshio::Vec4<float>();

EXPECT_FALSE(binReadObjs[0] == asciiReadObjs[0]);
}

TEST(STL, NormalsValuesMismatch)
{
vector< stl::Data<float> > binReadObjs;
stl::read<float>(binReadObjs, TEST_DIR "/cube_binary.stl");

vector< stl::Data<float> > asciiReadObjs;
stl::read<float>(asciiReadObjs, TEST_DIR "/cube_ascii.stl");

asciiReadObjs[0].mNormals[0] = meshio::Vec3<float>();

EXPECT_FALSE(binReadObjs[0] == asciiReadObjs[0]);
}

TEST(STL, CheckReReadIntoSameObject)
{
vector< stl::Data<float> > binReadObjs;
stl::read<float>(binReadObjs, TEST_DIR "/cube_binary.stl");

size_t numPositions = binReadObjs[0].mPositions.size();

// Re-read into same obj object, which should clear our old data
stl::read<float>(binReadObjs, TEST_DIR "/cube_binary.stl");

EXPECT_TRUE(binReadObjs[0].mPositions.size() == numPositions);
}