Skip to content

[Filters] PointCloud::reserve should be added in VoxelGridCovariance::getDisplayCloud #4931

@KotaYonezawa

Description

@KotaYonezawa

Describe the bug

Thanks a lot for great library.
In VoxelGridCovariance::getDisplayCloud, very huge number of points are added to PointCloud by push_back function.
To improve performance, PointCloud::reserve should be added, I think.

Context

Following is current VoxelGridCovariance::getDisplayCloud loop part code. (in voxel_grid_covariance.hpp)

  // Generate points for each occupied voxel with sufficient points.
  for (typename std::map<std::size_t, Leaf>::iterator it = leaves_.begin (); it != leaves_.end (); ++it)
  {
    Leaf& leaf = it->second;

    if (leaf.nr_points >= min_points_per_voxel_)
    {
      cell_mean = leaf.mean_;
      llt_of_cov.compute (leaf.cov_);
      cholesky_decomp = llt_of_cov.matrixL ();

      // Random points generated by sampling the normal distribution given by voxel mean and covariance matrix
      for (int i = 0; i < pnt_per_cell; i++)
      {
        rand_point = Eigen::Vector3d (var_nor (), var_nor (), var_nor ());
        dist_point = cell_mean + cholesky_decomp * rand_point;
        cell_cloud.push_back (PointXYZ (static_cast<float> (dist_point (0)), static_cast<float> (dist_point (1)), static_cast<float> (dist_point (2))));
      }
    }
  }

In this loop, many points are created and added to PointCloud instance (cell_cloud) using push_back function.
But on above code, many memory allocation will be occurred by push_back.
I think it is common technique that it is better to reserve memory before many push_back call.

  cell_cloud.reserve (pnt_per_cell * leaves_.size ()); // *** Should be Added ***

  // Generate points for each occupied voxel with sufficient points.
  for (typename std::map<std::size_t, Leaf>::iterator it = leaves_.begin (); it != leaves_.end (); ++it)
  {
    Leaf& leaf = it->second;

In addition, in our other environment using Ubuntu, ROS and Docker,
getDisplayCloud sometimes causes application crash.
Above modification seems to prevent it.
We are not sure why push_back call without reserve can cause application crash...
But anyway, adding reserve is basically correct, I think.

Expected behavior

Memory allocation in VoxelGridCovariance::getDisplayCloud can finish faster, without memory fragmentation.

Current Behavior

Memory allocation in VoxelGridCovariance::getDisplayCloud is not efficient. (time consuming, and memory can be fragmented)

Your Environment (please complete the following information):

  • OS: Windows10
  • Compiler: Compiler: Visual Studio 2019 Build Tools
  • PCL Version: HEAD

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions