42 #include <pcl/filters/filter.h>
51 inline Eigen::MatrixXi
54 Eigen::MatrixXi relative_coordinates (3, 13);
58 for (
int i = -1; i < 2; i++)
60 for (
int j = -1; j < 2; j++)
62 relative_coordinates (0, idx) = i;
63 relative_coordinates (1, idx) = j;
64 relative_coordinates (2, idx) = -1;
69 for (
int i = -1; i < 2; i++)
71 relative_coordinates (0, idx) = i;
72 relative_coordinates (1, idx) = -1;
73 relative_coordinates (2, idx) = 0;
77 relative_coordinates (0, idx) = -1;
78 relative_coordinates (1, idx) = 0;
79 relative_coordinates (2, idx) = 0;
81 return (relative_coordinates);
88 inline Eigen::MatrixXi
92 Eigen::MatrixXi relative_coordinates_all( 3, 26);
93 relative_coordinates_all.block<3, 13> (0, 0) = relative_coordinates;
94 relative_coordinates_all.block<3, 13> (0, 13) = -relative_coordinates;
95 return (relative_coordinates_all);
108 template <
typename T>
void
110 Eigen::Matrix<T, 4, 1> &min_pt, Eigen::Matrix<T, 4, 1> &max_pt);
123 template <
typename T>
void
125 Eigen::Matrix<T, 4, 1> &min_pt, Eigen::Matrix<T, 4, 1> &max_pt);
145 template <
typename T,
typename D>
void
147 const std::string &distance_field_name, D min_distance, D max_distance,
148 Eigen::Matrix<T, 4, 1> &min_pt, Eigen::Matrix<T, 4, 1> &max_pt,
bool limit_negative =
false);
169 template <
typename T,
typename D>
void
171 Eigen::Matrix<T, 4, 1> &min_pt, Eigen::Matrix<T, 4, 1> &max_pt,
bool limit_negative =
false);
185 template <
typename Po
intT>
void
187 const std::string &distance_field_name,
float min_distance,
float max_distance,
188 Eigen::Vector4f &min_pt, Eigen::Vector4f &max_pt,
bool limit_negative =
false);
202 template <
typename Po
intT>
void
205 const std::string &distance_field_name,
float min_distance,
float max_distance,
206 Eigen::Vector4f &min_pt, Eigen::Vector4f &max_pt,
bool limit_negative =
false);
220 template <
typename Po
intT>
235 using Ptr = shared_ptr<VoxelGrid<PointT> >;
236 using ConstPtr = shared_ptr<const VoxelGrid<PointT> >;
286 inline Eigen::Vector3f
325 inline Eigen::Vector3i
331 inline Eigen::Vector3i
337 inline Eigen::Vector3i
343 inline Eigen::Vector3i
368 inline std::vector<int>
371 Eigen::Vector4i ijk (
static_cast<int> (std::floor (reference_point.x *
inverse_leaf_size_[0])),
374 Eigen::Array4i diff2min =
min_b_ - ijk;
375 Eigen::Array4i diff2max =
max_b_ - ijk;
376 std::vector<int> neighbors (relative_coordinates.cols());
377 for (Eigen::Index ni = 0; ni < relative_coordinates.cols (); ni++)
379 Eigen::Vector4i displacement = (Eigen::Vector4i() << relative_coordinates.col(ni), 0).finished();
381 if ((diff2min <= displacement.array()).all() && (diff2max >= displacement.array()).all())
392 inline std::vector<int>
400 inline Eigen::Vector3i
414 int idx = ((Eigen::Vector4i() << ijk, 0).finished() -
min_b_).dot (
divb_mul_);
415 if (idx < 0 || idx >=
static_cast<int> (
leaf_layout_.size ()))
435 inline std::string
const
477 PCL_DEPRECATED(1, 16,
"use bool getFilterLimitsNegative() instead")
527 using FieldList =
typename pcl::traits::fieldList<PointT>::type;
561 leaf_size_ (
Eigen::Vector4f::Zero ()),
562 inverse_leaf_size_ (
Eigen::Array4f::Zero ()),
564 min_b_ (
Eigen::Vector4i::Zero ()),
565 max_b_ (
Eigen::Vector4i::Zero ()),
566 div_b_ (
Eigen::Vector4i::Zero ()),
567 divb_mul_ (
Eigen::Vector4i::Zero ())
569 filter_name_ =
"VoxelGrid";
581 leaf_size_ = leaf_size;
583 if (leaf_size_[3] == 0)
586 inverse_leaf_size_ = Eigen::Array4f::Ones () / leaf_size_.array ();
597 leaf_size_[0] = lx; leaf_size_[1] = ly; leaf_size_[2] = lz;
599 if (leaf_size_[3] == 0)
602 inverse_leaf_size_ = Eigen::Array4f::Ones () / leaf_size_.array ();
606 inline Eigen::Vector3f
645 inline Eigen::Vector3i
651 inline Eigen::Vector3i
657 inline Eigen::Vector3i
663 inline Eigen::Vector3i
676 return (leaf_layout_.at ((Eigen::Vector4i (
static_cast<int> (std::floor (x * inverse_leaf_size_[0])),
677 static_cast<int> (std::floor (y * inverse_leaf_size_[1])),
678 static_cast<int> (std::floor (z * inverse_leaf_size_[2])),
680 - min_b_).dot (divb_mul_)));
691 inline std::vector<int>
694 Eigen::Vector4i ijk (
static_cast<int> (std::floor (x * inverse_leaf_size_[0])),
695 static_cast<int> (std::floor (y * inverse_leaf_size_[1])),
696 static_cast<int> (std::floor (z * inverse_leaf_size_[2])), 0);
697 Eigen::Array4i diff2min = min_b_ - ijk;
698 Eigen::Array4i diff2max = max_b_ - ijk;
699 std::vector<int> neighbors (relative_coordinates.cols());
700 for (Eigen::Index ni = 0; ni < relative_coordinates.cols (); ni++)
702 Eigen::Vector4i displacement = (Eigen::Vector4i() << relative_coordinates.col(ni), 0).finished();
704 if ((diff2min <= displacement.array()).all() && (diff2max >= displacement.array()).all())
705 neighbors[ni] = leaf_layout_[((ijk + displacement - min_b_).dot (divb_mul_))];
720 inline std::vector<int>
721 getNeighborCentroidIndices (
float x,
float y,
float z,
const std::vector<Eigen::Vector3i, Eigen::aligned_allocator<Eigen::Vector3i> > &relative_coordinates)
const
723 Eigen::Vector4i ijk (
static_cast<int> (std::floor (x * inverse_leaf_size_[0])),
static_cast<int> (std::floor (y * inverse_leaf_size_[1])),
static_cast<int> (std::floor (z * inverse_leaf_size_[2])), 0);
724 std::vector<int> neighbors;
725 neighbors.reserve (relative_coordinates.size ());
726 for (
const auto &relative_coordinate : relative_coordinates)
727 neighbors.push_back (leaf_layout_[(ijk + (Eigen::Vector4i() << relative_coordinate, 0).finished() - min_b_).dot (divb_mul_)]);
734 inline std::vector<int>
742 inline Eigen::Vector3i
745 return {
static_cast<int> (std::floor (x * inverse_leaf_size_[0])),
746 static_cast<int> (std::floor (y * inverse_leaf_size_[1])),
747 static_cast<int> (std::floor (z * inverse_leaf_size_[2]))};
756 int idx = ((Eigen::Vector4i() << ijk, 0).finished() - min_b_).dot (divb_mul_);
757 if (idx < 0 || idx >=
static_cast<int> (leaf_layout_.size ()))
763 return (leaf_layout_[idx]);
773 filter_field_name_ = field_name;
777 inline std::string
const
780 return (filter_field_name_);
790 filter_limit_min_ = limit_min;
791 filter_limit_max_ = limit_max;
802 limit_min = filter_limit_min_;
803 limit_max = filter_limit_max_;
813 filter_limit_negative_ = limit_negative;
819 PCL_DEPRECATED(1, 16,
"use bool getFilterLimitsNegative() instead")
821 getFilterLimitsNegative (
bool &limit_negative)
const
823 limit_negative = filter_limit_negative_;
832 return (filter_limit_negative_);
843 bool downsample_all_data_{
true};
848 bool save_leaf_layout_{
false};
858 Eigen::Vector4i min_b_, max_b_,
div_b_, divb_mul_;
864 double filter_limit_min_{std::numeric_limits<float>::lowest()};
867 double filter_limit_max_{std::numeric_limits<float>::max()};
870 bool filter_limit_negative_{
false};
873 unsigned int min_points_per_voxel_{0};
898 #ifdef PCL_NO_PRECOMPILE
899 #include <pcl/filters/impl/voxel_grid.hpp>
Filter represents the base filter class.
shared_ptr< Filter< PointT > > Ptr
shared_ptr< const Filter< PointT > > ConstPtr
std::string filter_name_
The filter name.
PCLPointCloud2::Ptr PCLPointCloud2Ptr
PCLPointCloud2::ConstPtr PCLPointCloud2ConstPtr
typename PointCloud::Ptr PointCloudPtr
typename PointCloud::ConstPtr PointCloudConstPtr
PointCloud represents the base class in PCL for storing collections of 3D points.
shared_ptr< PointCloud< PointT > > Ptr
shared_ptr< const PointCloud< PointT > > ConstPtr
int getCentroidIndexAt(const Eigen::Vector3i &ijk) const
Returns the index in the downsampled cloud corresponding to a given set of coordinates.
Eigen::Vector3i getDivisionMultiplier() const
Get the multipliers to be applied to the grid coordinates in order to find the centroid index (after ...
bool getFilterLimitsNegative() const
Get whether the data outside the interval (min/max) is to be returned (true) or inside (false).
std::vector< int > getNeighborCentroidIndices(float x, float y, float z, const Eigen::MatrixXi &relative_coordinates) const
Returns the indices in the resulting downsampled cloud of the points at the specified grid coordinate...
std::string filter_field_name_
The desired user filter field name.
std::vector< int > leaf_layout_
The leaf layout information for fast access to cells relative to current position.
void setDownsampleAllData(bool downsample)
Set to true if all fields need to be downsampled, or false if just XYZ.
void setFilterLimits(const double &limit_min, const double &limit_max)
Set the field filter limits.
Eigen::Vector3f getLeafSize() const
Get the voxel grid leaf size.
Eigen::Vector3i getNrDivisions() const
Get the number of divisions along all 3 axes (after filtering is performed).
Eigen::Vector3i getMinBoxCoordinates() const
Get the minimum coordinates of the bounding box (after filtering is performed).
void applyFilter(PCLPointCloud2 &output) override
Downsample a Point Cloud using a voxelized grid approach.
unsigned int getMinimumPointsNumberPerVoxel() const
Return the minimum number of points required for a voxel to be used.
void setLeafSize(const Eigen::Vector4f &leaf_size)
Set the voxel grid leaf size.
void getFilterLimits(double &limit_min, double &limit_max) const
Get the field filter limits (min/max) set by the user.
VoxelGrid()
Empty constructor.
std::vector< int > getNeighborCentroidIndices(float x, float y, float z, const std::vector< Eigen::Vector3i, Eigen::aligned_allocator< Eigen::Vector3i > > &relative_coordinates) const
Returns the indices in the resulting downsampled cloud of the points at the specified grid coordinate...
Eigen::Vector3i getGridCoordinates(float x, float y, float z) const
Returns the corresponding (i,j,k) coordinates in the grid of point (x,y,z).
bool getDownsampleAllData() const
Get the state of the internal downsampling parameter (true if all fields need to be downsampled,...
bool getSaveLeafLayout() const
Returns true if leaf layout information will to be saved for later access.
std::vector< int > getLeafLayout() const
Returns the layout of the leafs for fast access to cells relative to current position.
std::string const getFilterFieldName() const
Get the name of the field used for filtering.
Eigen::Vector3i getMaxBoxCoordinates() const
Get the minimum coordinates of the bounding box (after filtering is performed).
void setFilterFieldName(const std::string &field_name)
Provide the name of the field to be used for filtering data.
void setSaveLeafLayout(bool save_leaf_layout)
Set to true if leaf layout information needs to be saved for later access.
void setFilterLimitsNegative(const bool limit_negative)
Set to true if we want to return the data outside the interval specified by setFilterLimits (min,...
void setLeafSize(float lx, float ly, float lz)
Set the voxel grid leaf size.
Eigen::Array4f inverse_leaf_size_
Internal leaf sizes stored as 1/leaf_size_ for efficiency reasons.
~VoxelGrid() override=default
Destructor.
void setMinimumPointsNumberPerVoxel(unsigned int min_points_per_voxel)
Set the minimum number of points required for a voxel to be used.
int getCentroidIndex(float x, float y, float z) const
Returns the index in the resulting downsampled cloud of the specified point.
Eigen::Vector4f leaf_size_
The size of a leaf.
VoxelGrid assembles a local 3D grid over a given PointCloud, and downsamples + filters the data.
Eigen::Vector3i getGridCoordinates(float x, float y, float z) const
Returns the corresponding (i,j,k) coordinates in the grid of point (x,y,z).
void setFilterFieldName(const std::string &field_name)
Provide the name of the field to be used for filtering data.
void setDownsampleAllData(bool downsample)
Set to true if all fields need to be downsampled, or false if just XYZ.
bool downsample_all_data_
Set to true if all fields need to be downsampled, or false if just XYZ.
Eigen::Vector3i getDivisionMultiplier() const
Get the multipliers to be applied to the grid coordinates in order to find the centroid index (after ...
bool getFilterLimitsNegative() const
Get whether the data outside the interval (min/max) is to be returned (true) or inside (false).
void setFilterLimitsNegative(const bool limit_negative)
Set to true if we want to return the data outside the interval specified by setFilterLimits (min,...
Eigen::Vector3i getMaxBoxCoordinates() const
Get the minimum coordinates of the bounding box (after filtering is performed).
unsigned int min_points_per_voxel_
Minimum number of points per voxel for the centroid to be computed.
void applyFilter(PointCloud &output) override
Downsample a Point Cloud using a voxelized grid approach.
unsigned int getMinimumPointsNumberPerVoxel() const
Return the minimum number of points required for a voxel to be used.
bool getDownsampleAllData() const
Get the state of the internal downsampling parameter (true if all fields need to be downsampled,...
std::vector< int > leaf_layout_
The leaf layout information for fast access to cells relative to current position.
void getFilterLimits(double &limit_min, double &limit_max) const
Get the field filter limits (min/max) set by the user.
Eigen::Vector4i divb_mul_
typename pcl::traits::fieldList< PointT >::type FieldList
int getCentroidIndex(const PointT &p) const
Returns the index in the resulting downsampled cloud of the specified point.
Eigen::Vector3i getNrDivisions() const
Get the number of divisions along all 3 axes (after filtering is performed).
bool save_leaf_layout_
Set to true if leaf layout information needs to be saved in leaf_layout_.
bool filter_limit_negative_
Set to true if we want to return the data outside (filter_limit_min_;filter_limit_max_).
std::vector< int > getLeafLayout() const
Returns the layout of the leafs for fast access to cells relative to current position.
int getCentroidIndexAt(const Eigen::Vector3i &ijk) const
Returns the index in the downsampled cloud corresponding to a given set of coordinates.
void setLeafSize(const Eigen::Vector4f &leaf_size)
Set the voxel grid leaf size.
Eigen::Vector4i min_b_
The minimum and maximum bin coordinates, the number of divisions, and the division multiplier.
void setSaveLeafLayout(bool save_leaf_layout)
Set to true if leaf layout information needs to be saved for later access.
double filter_limit_max_
The maximum allowed filter value a point will be considered from.
void setMinimumPointsNumberPerVoxel(unsigned int min_points_per_voxel)
Set the minimum number of points required for a voxel to be used.
std::vector< int > getNeighborCentroidIndices(const PointT &reference_point, const Eigen::MatrixXi &relative_coordinates) const
Returns the indices in the resulting downsampled cloud of the points at the specified grid coordinate...
PCL_MAKE_ALIGNED_OPERATOR_NEW VoxelGrid()
Empty constructor.
std::string filter_field_name_
The desired user filter field name.
Eigen::Vector4f leaf_size_
The size of a leaf.
double filter_limit_min_
The minimum allowed filter value a point will be considered from.
Eigen::Vector3f getLeafSize() const
Get the voxel grid leaf size.
void setLeafSize(float lx, float ly, float lz)
Set the voxel grid leaf size.
std::string const getFilterFieldName() const
Get the name of the field used for filtering.
Eigen::Vector3i getMinBoxCoordinates() const
Get the minimum coordinates of the bounding box (after filtering is performed).
Eigen::Array4f inverse_leaf_size_
Internal leaf sizes stored as 1/leaf_size_ for efficiency reasons.
~VoxelGrid() override=default
Destructor.
bool getSaveLeafLayout() const
Returns true if leaf layout information will to be saved for later access.
void setFilterLimits(const double &limit_min, const double &limit_max)
Set the field filter limits.
void getMinMax3D(const pcl::PointCloud< PointT > &cloud, PointT &min_pt, PointT &max_pt)
Get the minimum and maximum values on each of the 3 (x-y-z) dimensions in a given pointcloud.
#define PCL_MAKE_ALIGNED_OPERATOR_NEW
Macro to signal a class requires a custom allocator.
Eigen::MatrixXi getAllNeighborCellIndices()
Get the relative cell indices of all the 26 neighbors.
Eigen::MatrixXi getHalfNeighborCellIndices()
Get the relative cell indices of the "upper half" 13 neighbors.
IndicesAllocator<> Indices
Type used for indices in PCL.
PCLPointCloud2::ConstPtr PCLPointCloud2ConstPtr
#define PCL_DEPRECATED(Major, Minor, Message)
macro for compatibility across compilers and help remove old deprecated items for the Major....
shared_ptr< ::pcl::PCLPointCloud2 > Ptr
shared_ptr< const ::pcl::PCLPointCloud2 > ConstPtr
A point structure representing Euclidean xyz coordinates, and the RGB color.
Used internally in voxel grid classes.
unsigned int cloud_point_index
bool operator<(const cloud_point_index_idx &p) const
cloud_point_index_idx(unsigned int idx_, unsigned int cloud_point_index_)
cloud_point_index_idx()=default