-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
Fix pcd_viewer color handling when invalid fields are present in pcd #2105
Fix pcd_viewer color handling when invalid fields are present in pcd #2105
Conversation
In order to allow SSE optimizations, point cloud fields are aligned. For example, the Please post your point cloud so that we can give it a try. Also, are you using PCL master? |
Yes, I'm using master at 47629c1. My pointcloud is here: https://drive.google.com/file/d/1svASo3UfT-ozJG6QrJLMA0kr_Io5ApA_/view?usp=sharing When you load with master pcd_viewer you will get an warning and ColorHandler for random color. This this patch it should load without warning and with ColorHandler using rgb. Let me know if you want any new test for this. However the bug is in the main() of I have checked the faulty CI: Travis timeouted, because it took too long (but I'm pretty sure this change is not increasing build time considerably). Appveyor faulted even before building for its internal reasons (is it even configured?). I think that none of these is related to this PR. |
Thanks, I'll have a look. CI errors are unrelated. On Travis we've been struggling with timeouts in app building task for a while now. Appveyor is currently in the process of being set up (#2083). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed a bug. Proposed fix looks good, thanks!
visualization/src/pcl_visualizer.cpp
Outdated
@@ -2983,7 +2983,7 @@ pcl::visualization::PCLVisualizer::updateColorHandlerIndex (const std::string &i | |||
int color_handler_size = int (am_it->second.color_handlers.size ()); | |||
if (index >= color_handler_size) | |||
{ | |||
pcl::console::print_warn (stderr, "[updateColorHandlerIndex] Invalid index <%d> given! Maximum range is: 0-%lu.\n", index, static_cast<unsigned long> (am_it->second.color_handlers.size ())); | |||
pcl::console::print_warn (stderr, "[updateColorHandlerIndex] Invalid index <%d> given! Maximum range is: [0-%d).\n", index, color_handler_size); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be color_handler_size - 1
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it was wrong there. I have tried to fix that with right-open interval convention [0-5)
, which makes it now correct, but it is not maybe that natural. I will rework that.
visualization/tools/pcd_viewer.cpp
Outdated
for (size_t f = 0; f < cloud->fields.size (); ++f) | ||
{ | ||
if (!isValidFieldName (cloud->fields[f].name)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please put the curly bracket on a separate line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry about that. I'm not used to this convention. Do you use some clang-format/linter?
btw do you know there are many trailing whitespace issues?
0a4aedb
to
2a2a169
Compare
I have fixed both issues and rebased. Error comment now should be more clear. I have also fix the check to check also for underflows, not it is guaranteed we can use that as an index. |
My two cents diff --git a/visualization/tools/pcd_viewer.cpp b/visualization/tools/pcd_viewer.cpp
index b9d2d5e..7bfdbfb 100644
--- a/visualization/tools/pcd_viewer.cpp
+++ b/visualization/tools/pcd_viewer.cpp
@@ -617,16 +617,16 @@ main (int argc, char** argv)
{
int rgb_idx = 0;
int label_idx = 0;
- for (size_t f = 0; f < cloud->fields.size (); ++f)
+ for (size_t f = 0, i = 0; f < cloud->fields.size (); ++f)
{
if (cloud->fields[f].name == "rgb" || cloud->fields[f].name == "rgba")
{
- rgb_idx = f + 1;
+ rgb_idx = ++i;
color_handler.reset (new pcl::visualization::PointCloudColorHandlerRGBField<pcl::PCLPointCloud2> (cloud));
}
else if (cloud->fields[f].name == "label")
{
- label_idx = f + 1;
+ label_idx = ++i;
color_handler.reset (new pcl::visualization::PointCloudColorHandlerLabelField<pcl::PCLPointCloud2> (cloud, !use_optimal_l_colors));
}
else |
Unfortunately we don't have a clang-format config matching PCL style guide.
Yep. There is also lots of misformatted code in the repo, but the policy is to not touch it (otherwise |
@SergioRAgostinho That patch does not work. Try for yourself with any pcd. As you see it is hard to get it right (after all we have the bug there, so it must be hard, right?). You would need to increment My solution might not seem very elegant to you (matter of opinion), but it has some important properties.
I've added a comment about that |
2a2a169
to
30f29c1
Compare
visualization/src/pcl_visualizer.cpp
Outdated
{ | ||
pcl::console::print_warn (stderr, "[updateColorHandlerIndex] Invalid index <%d> given! Maximum range is: 0-%lu.\n", index, static_cast<unsigned long> (am_it->second.color_handlers.size ())); | ||
pcl::console::print_warn (stderr, "[updateColorHandlerIndex] Invalid index <%d> given! Index must be less than %d.\n", index, int(color_handler_size)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, why casting here? Just use %zu
format.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
%zu
is officially available only from C++11 forwards. is it okay to use?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK it's ANSI C99 feature. But your question reminded me of the issues we had with MSVC (#588). So better use %lu
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, exactly it is ANSI C99 feature. But ISO C++03 has not been rebased on C99, so also %zu
is missing. This has been done in C++11 (rebase on C99) and there is %zu
along with other. Only C++14 is finally based on C11.
However in real world %zu
is pretty much fine on all linuxes, and some recent VS. macs should be fine too. If you turn on -pedantic
it should be reported as standard violation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, good to know!
visualization/src/pcl_visualizer.cpp
Outdated
int color_handler_size = int (am_it->second.color_handlers.size ()); | ||
if (index >= color_handler_size) | ||
size_t color_handler_size = am_it->second.color_handlers.size (); | ||
if (!(size_t(index) < color_handler_size)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, missing space again :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
which space needs to be there? size_t (index)
? something about brackets?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
size_t (index)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
Second try for completeness diff --git a/visualization/tools/pcd_viewer.cpp b/visualization/tools/pcd_viewer.cpp
index b9d2d5e..63d4120 100644
--- a/visualization/tools/pcd_viewer.cpp
+++ b/visualization/tools/pcd_viewer.cpp
@@ -617,22 +617,25 @@ main (int argc, char** argv)
{
int rgb_idx = 0;
int label_idx = 0;
- for (size_t f = 0; f < cloud->fields.size (); ++f)
+ for (size_t f = 0, i = 1; f < cloud->fields.size (); ++f, ++i)
{
if (cloud->fields[f].name == "rgb" || cloud->fields[f].name == "rgba")
{
- rgb_idx = f + 1;
+ rgb_idx = i;
color_handler.reset (new pcl::visualization::PointCloudColorHandlerRGBField<pcl::PCLPointCloud2> (cloud));
}
else if (cloud->fields[f].name == "label")
{
- label_idx = f + 1;
+ label_idx = i;
color_handler.reset (new pcl::visualization::PointCloudColorHandlerLabelField<pcl::PCLPointCloud2> (cloud, !use_optimal_l_colors));
}
else
{
if (!isValidFieldName (cloud->fields[f].name))
+ {
+ --i;
continue;
+ }
color_handler.reset (new pcl::visualization::PointCloudColorHandlerGenericField<pcl::PCLPointCloud2> (cloud, cloud->fields[f].name));
}
// Add the cloud to the renderer
I started making something yesterday. It's still not quite there yet. |
Make sure you use clang-5, it has a number of new settings. |
Yeah. I noticed it already :/ But should we aim already for 5 even though xenial is shipping 3.8? |
Let's move format discussion to #2106. |
* clearly state index must be less than size we are printing * catch also underflow when negative index is given
* invalid fields needs to be skipped otherwise ColorHandler indexes will overflow
30f29c1
to
6fb8dc6
Compare
Hi all,
I'm still very new to the pcl library (few hours), but I think that I have already fixed a bug. Please correct me if I'm wrong, I'm really a newbie here.
I have a .pcd file with dimensions like this:
I have noticed that pcd_viewer does not show this .pcd in rgb colors. After some investigation it seems that it is caused by preceding invalid field before
rgb
field.I'm still wondering what are these invalid fields doing in my pointcloud. Is this supposed to be a normal result of serialization to binary .pcd?
Cheers,
Jiri