41 #ifndef PCL_FILTERS_IMPL_COVARIANCE_SAMPLING_H_
42 #define PCL_FILTERS_IMPL_COVARIANCE_SAMPLING_H_
44 #include <pcl/filters/covariance_sampling.h>
46 #include <Eigen/Eigenvalues>
49 template<
typename Po
intT,
typename Po
intNT>
bool
55 if (num_samples_ > indices_->size ())
57 PCL_ERROR (
"[pcl::CovarianceSampling::initCompute] The number of samples you asked for (%d) is larger than the number of input indices (%lu)\n",
58 num_samples_, indices_->size ());
64 Eigen::Vector3f centroid (0.f, 0.f, 0.f);
65 for (std::size_t p_i = 0; p_i < indices_->size (); ++p_i)
66 centroid += (*input_)[(*indices_)[p_i]].getVector3fMap ();
67 centroid /=
static_cast<float>(indices_->size ());
69 scaled_points_.resize (indices_->size ());
70 double average_norm = 0.0;
71 for (std::size_t p_i = 0; p_i < indices_->size (); ++p_i)
73 scaled_points_[p_i] = (*input_)[(*indices_)[p_i]].getVector3fMap () - centroid;
74 average_norm += scaled_points_[p_i].norm ();
77 average_norm /=
static_cast<double>(scaled_points_.size ());
78 for (
auto & scaled_point : scaled_points_)
79 scaled_point /=
static_cast<float>(average_norm);
85 template<
typename Po
intT,
typename Po
intNT>
double
88 Eigen::Matrix<double, 6, 6> covariance_matrix;
92 return computeConditionNumber (covariance_matrix);
97 template<
typename Po
intT,
typename Po
intNT>
double
100 const Eigen::SelfAdjointEigenSolver<Eigen::Matrix<double, 6, 6> > solver (covariance_matrix, Eigen::EigenvaluesOnly);
101 const double max_ev = solver.eigenvalues (). maxCoeff ();
102 const double min_ev = solver.eigenvalues (). minCoeff ();
103 return (max_ev / min_ev);
108 template<
typename Po
intT,
typename Po
intNT>
bool
116 Eigen::Matrix<double, 6, Eigen::Dynamic> f_mat = Eigen::Matrix<double, 6, Eigen::Dynamic> (6, indices_->size ());
117 for (std::size_t p_i = 0; p_i < scaled_points_.size (); ++p_i)
119 f_mat.block<3, 1> (0, p_i) = scaled_points_[p_i].cross (
120 (*input_normals_)[(*indices_)[p_i]].getNormalVector3fMap ()).
template cast<double> ();
121 f_mat.block<3, 1> (3, p_i) = (*input_normals_)[(*indices_)[p_i]].getNormalVector3fMap ().template cast<double> ();
125 covariance_matrix = f_mat * f_mat.transpose ();
130 template<
typename Po
intT,
typename Po
intNT>
void
133 Eigen::Matrix<double, 6, 6> c_mat;
138 const Eigen::SelfAdjointEigenSolver<Eigen::Matrix<double, 6, 6> > solver (c_mat);
139 const Eigen::Matrix<double, 6, 6> x = solver.eigenvectors ();
143 std::vector<std::size_t> candidate_indices;
144 candidate_indices.resize (indices_->size ());
145 for (std::size_t p_i = 0; p_i < candidate_indices.size (); ++p_i)
146 candidate_indices[p_i] = p_i;
149 using Vector6d = Eigen::Matrix<double, 6, 1>;
150 std::vector<Vector6d, Eigen::aligned_allocator<Vector6d> > v;
151 v.resize (candidate_indices.size ());
152 for (std::size_t p_i = 0; p_i < candidate_indices.size (); ++p_i)
154 v[p_i].block<3, 1> (0, 0) = scaled_points_[p_i].cross (
155 (*input_normals_)[(*indices_)[candidate_indices[p_i]]].getNormalVector3fMap ()).
template cast<double> ();
156 v[p_i].block<3, 1> (3, 0) = (*input_normals_)[(*indices_)[candidate_indices[p_i]]].getNormalVector3fMap ().template cast<double> ();
161 std::vector<std::list<std::pair<int, double> > > L;
164 for (std::size_t i = 0; i < 6; ++i)
166 for (std::size_t p_i = 0; p_i < candidate_indices.size (); ++p_i)
167 L[i].emplace_back(p_i, std::abs (v[p_i].dot (x.block<6, 1> (0, i))));
170 L[i].sort (sort_dot_list_function);
174 std::vector<double> t (6, 0.0);
176 sampled_indices.resize (num_samples_);
177 std::vector<bool> point_sampled (candidate_indices.size (),
false);
179 for (std::size_t sample_i = 0; sample_i < num_samples_; ++sample_i)
182 std::size_t min_t_i = 0;
183 for (std::size_t i = 0; i < 6; ++i)
185 if (t[min_t_i] > t[i])
190 while (point_sampled [L[min_t_i].front ().first])
191 L[min_t_i].pop_front ();
193 sampled_indices[sample_i] = L[min_t_i].front ().first;
194 point_sampled[L[min_t_i].front ().first] =
true;
195 L[min_t_i].pop_front ();
198 for (std::size_t i = 0; i < 6; ++i)
200 double val = v[sampled_indices[sample_i]].dot (x.block<6, 1> (0, i));
206 for (
auto &sampled_index : sampled_indices)
207 sampled_index = (*indices_)[candidate_indices[sampled_index]];
212 template<
typename Po
intT,
typename Po
intNT>
void
216 applyFilter (sampled_indices);
218 output.resize (sampled_indices.size ());
219 output.header = input_->header;
221 output.width = output.size ();
222 output.is_dense =
true;
223 for (std::size_t i = 0; i < sampled_indices.size (); ++i)
224 output[i] = (*input_)[sampled_indices[i]];
228 #define PCL_INSTANTIATE_CovarianceSampling(T,NT) template class PCL_EXPORTS pcl::CovarianceSampling<T,NT>;
double computeConditionNumber()
Compute the condition number of the input point cloud.
bool computeCovarianceMatrix(Eigen::Matrix< double, 6, 6 > &covariance_matrix)
Computes the covariance matrix of the input cloud.
void applyFilter(Cloud &output) override
Sample of point indices into a separate PointCloud.
FilterIndices represents the base class for filters that are about binary point removal.
unsigned int computeCovarianceMatrix(const pcl::PointCloud< PointT > &cloud, const Eigen::Matrix< Scalar, 4, 1 > ¢roid, Eigen::Matrix< Scalar, 3, 3 > &covariance_matrix)
Compute the 3x3 covariance matrix of a given set of points.
IndicesAllocator<> Indices
Type used for indices in PCL.