38 #ifndef PCL_SURFACE_IMPL_SURFEL_SMOOTHING_H_
39 #define PCL_SURFACE_IMPL_SURFEL_SMOOTHING_H_
41 #include <pcl/search/auto.h>
42 #include <pcl/surface/surfel_smoothing.h>
44 #include <pcl/console/print.h>
47 template <
typename Po
intT,
typename Po
intNT>
bool
55 PCL_ERROR (
"SurfelSmoothing: normal cloud not set\n");
59 if (input_->size () != normals_->size ())
61 PCL_ERROR (
"SurfelSmoothing: number of input points different from the number of given normals\n");
80 template <
typename Po
intT,
typename Po
intNT>
float
87 output_positions->points.resize (interm_cloud_->size ());
89 output_normals->points.resize (interm_cloud_->size ());
92 std::vector<float> nn_distances;
94 std::vector<float> diffs (interm_cloud_->size ());
95 float total_residual = 0.0f;
97 for (std::size_t i = 0; i < interm_cloud_->size (); ++i)
99 Eigen::Vector4f smoothed_point = Eigen::Vector4f::Zero ();
100 Eigen::Vector4f smoothed_normal = Eigen::Vector4f::Zero ();
104 tree_->radiusSearch ((*interm_cloud_)[i], 5*scale_, nn_indices, nn_distances);
106 float theta_normalization_factor = 0.0;
107 std::vector<float> theta (nn_indices.size ());
108 for (std::size_t nn_index_i = 0; nn_index_i < nn_indices.size (); ++nn_index_i)
111 float theta_i = std::exp ( (-1) * dist / scale_squared_);
112 theta_normalization_factor += theta_i;
114 smoothed_normal += theta_i * (*interm_normals_)[nn_indices[nn_index_i]].getNormalVector4fMap ();
116 theta[nn_index_i] = theta_i;
119 smoothed_normal /= theta_normalization_factor;
120 smoothed_normal(3) = 0.0f;
121 smoothed_normal.normalize ();
126 smoothed_point = (*interm_cloud_)[i].getVector4fMap ();
130 smoothed_point(3) = 0.0f;
131 for (std::size_t nn_index_i = 0; nn_index_i < nn_indices.size (); ++nn_index_i)
133 Eigen::Vector4f neighbor = (*input_)[nn_indices[nn_index_i]].getVector4fMap ();
135 float dot_product = smoothed_normal.dot (neighbor - smoothed_point);
136 e_residual += theta[nn_index_i] * dot_product;
138 e_residual /= theta_normalization_factor;
139 if (e_residual < 1e-5)
break;
141 smoothed_point += e_residual * smoothed_normal;
144 total_residual += e_residual;
146 (*output_positions)[i].getVector4fMap () = smoothed_point;
147 (*output_normals)[i].getNormalVector4fMap () = (*normals_)[i].getNormalVector4fMap ();
152 return total_residual;
156 template <
typename Po
intT,
typename Po
intNT>
void
159 PointNT &output_normal)
161 Eigen::Vector4f average_normal = Eigen::Vector4f::Zero ();
162 Eigen::Vector4f result_point = (*input_)[point_index].getVector4fMap ();
163 result_point(3) = 0.0f;
166 float error_residual_threshold_ = 1e-3f;
167 float error_residual = error_residual_threshold_ + 1;
168 float last_error_residual = error_residual + 100.0f;
171 std::vector<float> nn_distances;
173 int big_iterations = 0;
174 int max_big_iterations = 500;
176 while (std::fabs (error_residual) < std::fabs (last_error_residual) -error_residual_threshold_ &&
177 big_iterations < max_big_iterations)
179 average_normal = Eigen::Vector4f::Zero ();
181 PointT aux_point; aux_point.x = result_point(0); aux_point.y = result_point(1); aux_point.z = result_point(2);
182 tree_->radiusSearch (aux_point, 5*scale_, nn_indices, nn_distances);
184 float theta_normalization_factor = 0.0;
185 std::vector<float> theta (nn_indices.size ());
186 for (std::size_t nn_index_i = 0; nn_index_i < nn_indices.size (); ++nn_index_i)
188 float dist = nn_distances[nn_index_i];
189 float theta_i = std::exp ( (-1) * dist / scale_squared_);
190 theta_normalization_factor += theta_i;
192 average_normal += theta_i * (*normals_)[nn_indices[nn_index_i]].getNormalVector4fMap ();
193 theta[nn_index_i] = theta_i;
195 average_normal /= theta_normalization_factor;
196 average_normal(3) = 0.0f;
197 average_normal.normalize ();
200 float e_residual_along_normal = 2, last_e_residual_along_normal = 3;
201 int small_iterations = 0;
202 int max_small_iterations = 10;
203 while ( std::fabs (e_residual_along_normal) < std::fabs (last_e_residual_along_normal) &&
204 small_iterations < max_small_iterations)
208 e_residual_along_normal = 0.0f;
209 for (std::size_t nn_index_i = 0; nn_index_i < nn_indices.size (); ++nn_index_i)
211 Eigen::Vector4f neighbor = (*input_)[nn_indices[nn_index_i]].getVector4fMap ();
213 float dot_product = average_normal.dot (neighbor - result_point);
214 e_residual_along_normal += theta[nn_index_i] * dot_product;
216 e_residual_along_normal /= theta_normalization_factor;
217 if (e_residual_along_normal < 1e-3)
break;
219 result_point += e_residual_along_normal * average_normal;
225 last_error_residual = error_residual;
226 error_residual = e_residual_along_normal;
231 output_point.x = result_point(0);
232 output_point.y = result_point(1);
233 output_point.z = result_point(2);
234 output_normal = (*normals_)[point_index];
236 if (big_iterations == max_big_iterations)
237 PCL_DEBUG (
"[pcl::SurfelSmoothing::smoothPoint] Passed the number of BIG iterations: %d\n", big_iterations);
242 template <
typename Po
intT,
typename Po
intNT>
void
248 PCL_ERROR (
"[pcl::SurfelSmoothing::computeSmoothedCloud]: SurfelSmoothing not initialized properly, skipping computeSmoothedCloud ().\n");
252 tree_->setInputCloud (input_);
254 output_positions->header = input_->header;
255 output_positions->height = input_->height;
256 output_positions->width = input_->width;
258 output_normals->header = input_->header;
259 output_normals->height = input_->height;
260 output_normals->width = input_->width;
262 output_positions->points.resize (input_->size ());
263 output_normals->points.resize (input_->size ());
264 for (std::size_t i = 0; i < input_->size (); ++i)
266 smoothPoint (i, (*output_positions)[i], (*output_normals)[i]);
271 template <
typename Po
intT,
typename Po
intNT>
void
276 if (interm_cloud_->size () != cloud2->size () ||
277 cloud2->size () != cloud2_normals->size ())
279 PCL_ERROR (
"[pcl::SurfelSmoothing::extractSalientFeaturesBetweenScales]: Number of points in the clouds does not match.\n");
283 std::vector<float> diffs (cloud2->size ());
284 for (std::size_t i = 0; i < cloud2->size (); ++i)
285 diffs[i] = (*cloud2_normals)[i].getNormalVector4fMap ().dot ((*cloud2)[i].getVector4fMap () -
286 (*interm_cloud_)[i].getVector4fMap ());
289 std::vector<float> nn_distances;
291 output_features->resize (cloud2->size ());
292 for (
int point_i = 0; point_i < static_cast<int> (cloud2->size ()); ++point_i)
295 tree_->radiusSearch (point_i, scale_, nn_indices, nn_distances);
298 bool smallest =
true;
299 for (
const auto &nn_index : nn_indices)
301 if (diffs[point_i] < diffs[nn_index])
307 if (largest || smallest)
308 (*output_features)[point_i] = point_i;
314 #define PCL_INSTANTIATE_SurfelSmoothing(PointT,PointNT) template class PCL_EXPORTS pcl::SurfelSmoothing<PointT, PointNT>;
PointCloud represents the base class in PCL for storing collections of 3D points.
typename pcl::PointCloud< PointNT >::Ptr NormalCloudPtr
void extractSalientFeaturesBetweenScales(PointCloudInPtr &cloud2, NormalCloudPtr &cloud2_normals, pcl::IndicesPtr &output_features)
float smoothCloudIteration(PointCloudInPtr &output_positions, NormalCloudPtr &output_normals)
void smoothPoint(std::size_t &point_index, PointT &output_point, PointNT &output_normal)
void computeSmoothedCloud(PointCloudInPtr &output_positions, NormalCloudPtr &output_normals)
typename pcl::PointCloud< PointT >::Ptr PointCloudInPtr
Define standard C methods to do distance calculations.
@ radius_search
The search method will mainly be used for radiusSearch.
float squaredEuclideanDistance(const PointType1 &p1, const PointType2 &p2)
Calculate the squared euclidean distance between the two given points.
IndicesAllocator<> Indices
Type used for indices in PCL.
shared_ptr< Indices > IndicesPtr
A point structure representing Euclidean xyz coordinates, and the RGB color.