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

Allow STL files bigger than expected (printing a warning message). #951

Merged
merged 1 commit into from
Dec 22, 2015
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
15 changes: 11 additions & 4 deletions src/rviz/ogre_helpers/stl_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ bool STLLoader::load(uint8_t* buffer, const size_t num_bytes, const std::string&
static const size_t binary_stl_header_len = 84;
if (num_bytes <= binary_stl_header_len)
{
ROS_ERROR_STREAM("The STL file '" << origin <<"' is malformed. It "
ROS_ERROR_STREAM("The STL file '" << origin << "' is malformed. It "
"appears to be a binary STL file but does not contain "
"enough data for the 80 byte header and 32-bit integer "
"triangle count.");
Expand All @@ -134,15 +134,22 @@ bool STLLoader::load(uint8_t* buffer, const size_t num_bytes, const std::string&
unsigned int num_triangles = *(reinterpret_cast<uint32_t *>(buffer + 80));
static const size_t number_of_bytes_per_triangle = 50;
size_t expected_size = binary_stl_header_len + num_triangles * number_of_bytes_per_triangle;
if (num_bytes != expected_size)
if (num_bytes < expected_size)
{
ROS_ERROR_STREAM("The STL file '" << origin << "' is malformed. According "
"to the binary STL header it should have '" <<
num_triangles << "' triangles, but it has too " <<
(num_bytes > expected_size ? "much" : "little") <<
num_triangles << "' triangles, but it has too little" <<
" data for that to be the case.");
return false;
}
else if (num_bytes > expected_size)
{
ROS_WARN_STREAM("The STL file '" << origin << "' is malformed. According "
"to the binary STL header it should have '" <<
num_triangles << "' triangles, but it has too much" <<
" data for that to be the case. The extra data will be" <<
" ignored.");
}

// load the binary STL data
return this->load_binary(buffer);
Expand Down
Binary file modified src/test/meshes/invalid.stl
Binary file not shown.
Binary file added src/test/meshes/valid_extra.stl
Binary file not shown.
6 changes: 6 additions & 0 deletions src/test/stl_loader_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ TEST( STLLoader, load )
// Load a valid STL binary file.
meshFilePath = meshDir / "valid.stl";
EXPECT_TRUE(loader.load(meshFilePath.string()));

// Load a "potentially" valid STL binary file with bigger size than the
// expected. The extra "unexpected" data at the end of the file should be
// ignored.
meshFilePath = meshDir / "valid_extra.stl";
EXPECT_TRUE(loader.load(meshFilePath.string()));
}

int main( int argc, char **argv ) {
Expand Down