41 #ifndef PCL_FEATURES_IMPL_FPFH_H_
42 #define PCL_FEATURES_IMPL_FPFH_H_
44 #include <pcl/features/fpfh.h>
45 #include <pcl/features/pfh_tools.h>
48 template <
typename Po
intInT,
typename Po
intNT,
typename Po
intOutT>
bool
51 int p_idx,
int q_idx,
float &f1,
float &f2,
float &f3,
float &f4)
54 cloud.
points[q_idx].getVector4fMap (), normals.
points[q_idx].getNormalVector4fMap (),
60 template <
typename Po
intInT,
typename Po
intNT,
typename Po
intOutT>
void
63 int p_idx,
int row,
const std::vector<int> &indices,
64 Eigen::MatrixXf &hist_f1, Eigen::MatrixXf &hist_f2, Eigen::MatrixXf &hist_f3)
66 Eigen::Vector4f pfh_tuple;
69 int nr_bins_f1 =
static_cast<int> (hist_f1.cols ());
70 int nr_bins_f2 =
static_cast<int> (hist_f2.cols ());
71 int nr_bins_f3 =
static_cast<int> (hist_f3.cols ());
74 float hist_incr = 100.0f /
static_cast<float>(indices.size () - 1);
77 for (
const auto &index : indices)
84 if (!
computePairFeatures (cloud, normals, p_idx, index, pfh_tuple[0], pfh_tuple[1], pfh_tuple[2], pfh_tuple[3]))
88 int h_index =
static_cast<int> (std::floor (nr_bins_f1 * ((pfh_tuple[0] + M_PI) * d_pi_)));
89 if (h_index < 0) h_index = 0;
90 if (h_index >= nr_bins_f1) h_index = nr_bins_f1 - 1;
91 hist_f1 (row, h_index) += hist_incr;
93 h_index =
static_cast<int> (std::floor (nr_bins_f2 * ((pfh_tuple[1] + 1.0) * 0.5)));
94 if (h_index < 0) h_index = 0;
95 if (h_index >= nr_bins_f2) h_index = nr_bins_f2 - 1;
96 hist_f2 (row, h_index) += hist_incr;
98 h_index =
static_cast<int> (std::floor (nr_bins_f3 * ((pfh_tuple[2] + 1.0) * 0.5)));
99 if (h_index < 0) h_index = 0;
100 if (h_index >= nr_bins_f3) h_index = nr_bins_f3 - 1;
101 hist_f3 (row, h_index) += hist_incr;
106 template <
typename Po
intInT,
typename Po
intNT,
typename Po
intOutT>
void
108 const Eigen::MatrixXf &hist_f1,
const Eigen::MatrixXf &hist_f2,
const Eigen::MatrixXf &hist_f3,
109 const std::vector<int> &indices,
const std::vector<float> &dists, Eigen::VectorXf &fpfh_histogram)
111 assert (indices.size () == dists.size ());
113 double sum_f1 = 0.0, sum_f2 = 0.0, sum_f3 = 0.0;
114 float weight = 0.0, val_f1, val_f2, val_f3;
117 const auto nr_bins_f1 = hist_f1.cols ();
118 const auto nr_bins_f2 = hist_f2.cols ();
119 const auto nr_bins_f3 = hist_f3.cols ();
120 const auto nr_bins_f12 = nr_bins_f1 + nr_bins_f2;
123 fpfh_histogram.setZero (nr_bins_f1 + nr_bins_f2 + nr_bins_f3);
126 for (std::size_t idx = 0; idx < indices.size (); ++idx)
133 weight = 1.0f / dists[idx];
136 for (std::size_t f1_i = 0; f1_i < nr_bins_f1; ++f1_i)
138 val_f1 = hist_f1 (indices[idx], f1_i) * weight;
140 fpfh_histogram[f1_i] += val_f1;
143 for (std::size_t f2_i = 0; f2_i < nr_bins_f2; ++f2_i)
145 val_f2 = hist_f2 (indices[idx], f2_i) * weight;
147 fpfh_histogram[f2_i + nr_bins_f1] += val_f2;
150 for (std::size_t f3_i = 0; f3_i < nr_bins_f3; ++f3_i)
152 val_f3 = hist_f3 (indices[idx], f3_i) * weight;
154 fpfh_histogram[f3_i + nr_bins_f12] += val_f3;
159 sum_f1 = 100.0 / sum_f1;
161 sum_f2 = 100.0 / sum_f2;
163 sum_f3 = 100.0 / sum_f3;
166 const auto denormalize_with = [](
auto factor)
168 return [=](
const auto& data) {
return data * factor; };
171 auto last = fpfh_histogram.data ();
172 last = std::transform(last, last + nr_bins_f1, last, denormalize_with (sum_f1));
173 last = std::transform(last, last + nr_bins_f2, last, denormalize_with (sum_f2));
174 std::transform(last, last + nr_bins_f3, last, denormalize_with (sum_f3));
178 template <
typename Po
intInT,
typename Po
intNT,
typename Po
intOutT>
void
180 Eigen::MatrixXf &hist_f1, Eigen::MatrixXf &hist_f2, Eigen::MatrixXf &hist_f3)
184 std::vector<int> nn_indices (k_);
185 std::vector<float> nn_dists (k_);
187 std::set<int> spfh_indices;
188 spfh_hist_lookup.resize (surface_->points.size ());
192 if (surface_ != input_ ||
193 indices_->size () != surface_->points.size ())
195 for (
const auto& p_idx: *indices_)
197 if (this->searchForNeighbors (p_idx, search_parameter_, nn_indices, nn_dists) == 0)
200 spfh_indices.insert (nn_indices.begin (), nn_indices.end ());
206 for (std::size_t idx = 0; idx < indices_->size (); ++idx)
207 spfh_indices.insert (
static_cast<int> (idx));
211 std::size_t data_size = spfh_indices.size ();
212 hist_f1.setZero (data_size, nr_bins_f1_);
213 hist_f2.setZero (data_size, nr_bins_f2_);
214 hist_f3.setZero (data_size, nr_bins_f3_);
218 for (
const auto& p_idx: spfh_indices)
221 if (this->searchForNeighbors (*surface_, p_idx, search_parameter_, nn_indices, nn_dists) == 0)
225 computePointSPFHSignature (*surface_, *normals_, p_idx, i, nn_indices, hist_f1, hist_f2, hist_f3);
228 spfh_hist_lookup[p_idx] = i;
234 template <
typename Po
intInT,
typename Po
intNT,
typename Po
intOutT>
void
239 std::vector<int> nn_indices (k_);
240 std::vector<float> nn_dists (k_);
242 std::vector<int> spfh_hist_lookup;
243 computeSPFHSignatures (spfh_hist_lookup, hist_f1_, hist_f2_, hist_f3_);
245 output.is_dense =
true;
247 if (input_->is_dense)
250 for (std::size_t idx = 0; idx < indices_->size (); ++idx)
252 if (this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
254 for (Eigen::Index d = 0; d < fpfh_histogram_.size (); ++d)
255 output.points[idx].histogram[d] = std::numeric_limits<float>::quiet_NaN ();
257 output.is_dense =
false;
263 for (
auto &nn_index : nn_indices)
264 nn_index = spfh_hist_lookup[nn_index];
267 weightPointSPFHSignature (hist_f1_, hist_f2_, hist_f3_, nn_indices, nn_dists, fpfh_histogram_);
270 std::copy_n(fpfh_histogram_.data (), fpfh_histogram_.size (), output.points[idx].histogram);
276 for (std::size_t idx = 0; idx < indices_->size (); ++idx)
278 if (!
isFinite ((*input_)[(*indices_)[idx]]) ||
279 this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
281 for (Eigen::Index d = 0; d < fpfh_histogram_.size (); ++d)
282 output.points[idx].histogram[d] = std::numeric_limits<float>::quiet_NaN ();
284 output.is_dense =
false;
290 for (
auto &nn_index : nn_indices)
291 nn_index = spfh_hist_lookup[nn_index];
294 weightPointSPFHSignature (hist_f1_, hist_f2_, hist_f3_, nn_indices, nn_dists, fpfh_histogram_);
297 std::copy_n(fpfh_histogram_.data (), fpfh_histogram_.size (), output.points[idx].histogram);
302 #define PCL_INSTANTIATE_FPFHEstimation(T,NT,OutT) template class PCL_EXPORTS pcl::FPFHEstimation<T,NT,OutT>;
304 #endif // PCL_FEATURES_IMPL_FPFH_H_