PointCloudLibrary / pcl

Point Cloud Library (PCL)
https://pointclouds.org/
Other
10.04k stars 4.62k forks source link

getCentroidIndexAt in voxel_grid.h deliver wrong leaf_layout_[idx] #3529

Open zhangliyin126 opened 4 years ago

zhangliyin126 commented 4 years ago

Your Environment

Context

Here is original code

 inline int
      getCentroidIndexAt (const Eigen::Vector3i &ijk) const
      {
        int idx = ((Eigen::Vector4i() << ijk, 0).finished() - min_b_).dot (divb_mul_);
        if (idx < 0 || idx >= static_cast<int> (leaf_layout_.size ())) // this checks also if leaf_layout_.size () == 0 i.e. everything was computed as needed
        {
          //if (verbose)
          //  PCL_ERROR ("[pcl::%s::getCentroidIndexAt] Specified coordinate is outside grid bounds, or leaf layout is not saved, make sure to call setSaveLeafLayout(true) and filter(output) first!\n", getClassName ().c_str ());
          return (-1);
        }
        return (leaf_layout_[idx]);
      }

If you retrieve points out of voxel grids, and let's see its ijk[0] is large, you will get a index with wrong number rather than -1, due to its use of idx.

Solution

Directly check ijk, add following code to getCentroidIndexAt function: if (ijk[0]<minb[0] || ijk[0]>maxb[0] || ijk[1]<minb[1] || ijk[1]> maxb[1] || ijk[2]<minb[2] || ijk[2]>maxb[2]) return (-1);

stale[bot] commented 4 years ago

Marking this as stale due to 30 days of inactivity. It will be closed in 7 days if no further activity occurs.

nilsgr commented 1 year ago

I can confirm this bug. It caused random artifacts using voxel_grid_occlusion_estimation.hpp... The provided solution fixed it for me.

mvieth commented 1 year ago

@nilsgr I checked voxel_grid_occlusion_estimation.hpp, and getCentroidIndexAt is used in three places: one, two, three But in all three places, the for/while loops make sure that the values passed to getCentroidIndexAt are okay with respect to minb and maxb. Can you say which values minb and maxb have in your case?