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

Add test to ensure binary STL files from SOLIDWORKS get imported correctly (backport #917) #931

Merged
merged 1 commit into from
Dec 5, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,17 @@ bool STLLoader::load(uint8_t * buffer, const size_t num_bytes, const std::string
// check for ascii since we can only load binary types with this class
std::string buffer_str = std::string(reinterpret_cast<char *>(buffer), num_bytes);

if (buffer_str.substr(0, 5) == std::string("solid")) {
// file says that it is ascii, but why should we trust it?

// check for "endsolid" as well
if (buffer_str.find("endsolid", 5) != std::string::npos) {
RVIZ_RENDERING_LOG_ERROR_STREAM(
"The STL file '" << origin << "' is malformed. It "
"starts with the word 'solid' and also contains the "
"word 'endsolid', indicating that it's an ASCII STL "
"file, but rviz can only load binary STL files so it "
"will not be loaded. Please convert it to a "
"binary STL file.");
return false;
}

// chastise the user for malformed files
RVIZ_RENDERING_LOG_WARNING_STREAM(
"The STL file '" << origin << "' is malformed. It starts "
"with the word 'solid', indicating that it's an ASCII "
"STL file, but it does not contain the word 'endsolid' so "
"it is either a malformed ASCII STL file or it is actually "
"a binary STL file. Trying to interpret it as a binary "
"STL file instead.");
if (buffer_str.substr(0, 5) == std::string("solid") &&
buffer_str.find("endsolid", 5) != std::string::npos)
{
RVIZ_RENDERING_LOG_ERROR_STREAM(
"The STL file '" << origin << "' is malformed. It "
"starts with the word 'solid' and also contains the "
"word 'endsolid', indicating that it's an ASCII STL "
"file, but rviz can only load binary STL files so it "
"will not be loaded. Please convert it to a "
"binary STL file.");
return false;
}

// make sure there's enough data for a binary STL header and triangle count
Expand Down
Binary file not shown.
13 changes: 13 additions & 0 deletions rviz_rendering_tests/test/mesh_loader_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,16 @@ TEST_F(MeshLoaderTestFixture, assimp_loader_reads_size_correctly) {
ASSERT_FLOAT_EQ(expected_bounding_radius, mesh->getBoundingSphereRadius());
assertBoundingBoxEquality(expected_bounding_box, mesh->getBounds());
}

TEST_F(MeshLoaderTestFixture, loading_solidworks_binary_stl) {
// In general, binary STL files should not start with "solid" as this hints ASCII STL files.
// Annoyingly, STL files exported from Solidworks don't follow this guideline and contain
// "solid" at the start of binary STL files.
// However, they don't finish with "endsolid" like ASCII STL files, so we can still detect
// them as binary STL files.
// This test checks that SOLIDWORKS binary STL files get loaded correctly and don't get treated
// as ASCII STL files.
std::string mesh_path = "package://rviz_rendering_tests/test_meshes/solidworks.stl";

ASSERT_TRUE(rviz_rendering::loadMeshFromResource(mesh_path));
}