41 #ifndef PCL_FEATURES_IMPL_ESF_H_
42 #define PCL_FEATURES_IMPL_ESF_H_
44 #include <pcl/features/esf.h>
46 #include <pcl/common/transforms.h>
51 template <
typename Po
intInT,
typename Po
intOutT>
void
55 const int binsize = 64;
56 const std::size_t sample_size = 20000;
58 srand (
static_cast<unsigned int> (time (
nullptr)));
59 const auto maxindex = pc.
size ();
61 std::vector<float> d2v, d1v, d3v, wt_d3;
62 std::vector<int> wt_d2;
63 d1v.reserve (sample_size);
64 d2v.reserve (sample_size * 3);
65 d3v.reserve (sample_size);
66 wt_d2.reserve (sample_size * 3);
67 wt_d3.reserve (sample_size);
69 float h_in[binsize] = {0.0f};
70 float h_out[binsize] = {0.0f};
71 float h_mix[binsize] = {0.0f};
72 float h_mix_ratio[binsize] = {0.0f};
74 float h_a3_in[binsize] = {0.0f};
75 float h_a3_out[binsize] = {0.0f};
76 float h_a3_mix[binsize] = {0.0f};
78 float h_d3_in[binsize] = {0.0f};
79 float h_d3_out[binsize] = {0.0f};
80 float h_d3_mix[binsize] = {0.0f};
83 float pih =
static_cast<float>(
M_PI) / 2.0f;
87 int pcnt1,pcnt2,pcnt3;
88 for (std::size_t nn_idx = 0; nn_idx < sample_size; ++nn_idx)
91 int index1 = rand()%maxindex;
92 int index2 = rand()%maxindex;
93 int index3 = rand()%maxindex;
95 if (index1==index2 || index1 == index3 || index2 == index3)
101 Eigen::Vector4f p1 = pc[index1].getVector4fMap ();
102 Eigen::Vector4f p2 = pc[index2].getVector4fMap ();
103 Eigen::Vector4f p3 = pc[index3].getVector4fMap ();
106 Eigen::Vector4f v21 (p2 - p1);
107 Eigen::Vector4f v31 (p3 - p1);
108 Eigen::Vector4f v23 (p2 - p3);
109 a = v21.norm (); b = v31.norm (); c = v23.norm (); s = (a+b+c) * 0.5f;
110 if (s * (s-a) * (s-b) * (s-c) <= 0.001f)
121 th1 =
static_cast<int> (
pcl_round (std::acos (std::abs (v21.dot (v31))) / pih * (binsize-1)));
122 th2 =
static_cast<int> (
pcl_round (std::acos (std::abs (v23.dot (v31))) / pih * (binsize-1)));
123 th3 =
static_cast<int> (
pcl_round (std::acos (std::abs (v23.dot (v21))) / pih * (binsize-1)));
124 if (th1 < 0 || th1 >= binsize)
129 if (th2 < 0 || th2 >= binsize)
134 if (th3 < 0 || th3 >= binsize)
149 const int xs = p1[0] < 0.0?
static_cast<int>(std::floor(p1[0])+GRIDSIZE_H):
static_cast<int>(std::ceil(p1[0])+GRIDSIZE_H-1);
150 const int ys = p1[1] < 0.0?
static_cast<int>(std::floor(p1[1])+GRIDSIZE_H):
static_cast<int>(std::ceil(p1[1])+GRIDSIZE_H-1);
151 const int zs = p1[2] < 0.0?
static_cast<int>(std::floor(p1[2])+GRIDSIZE_H):
static_cast<int>(std::ceil(p1[2])+GRIDSIZE_H-1);
152 const int xt = p2[0] < 0.0?
static_cast<int>(std::floor(p2[0])+GRIDSIZE_H):
static_cast<int>(std::ceil(p2[0])+GRIDSIZE_H-1);
153 const int yt = p2[1] < 0.0?
static_cast<int>(std::floor(p2[1])+GRIDSIZE_H):
static_cast<int>(std::ceil(p2[1])+GRIDSIZE_H-1);
154 const int zt = p2[2] < 0.0?
static_cast<int>(std::floor(p2[2])+GRIDSIZE_H):
static_cast<int>(std::ceil(p2[2])+GRIDSIZE_H-1);
155 wt_d2.push_back (this->lci (xs, ys, zs, xt, yt, zt, ratio, vxlcnt, pcnt1));
156 if (wt_d2.back () == 2)
157 h_mix_ratio[
static_cast<int> (
pcl_round (ratio * (binsize-1)))]++;
158 vxlcnt_sum += vxlcnt;
163 const int xs = p1[0] < 0.0?
static_cast<int>(std::floor(p1[0])+GRIDSIZE_H):
static_cast<int>(std::ceil(p1[0])+GRIDSIZE_H-1);
164 const int ys = p1[1] < 0.0?
static_cast<int>(std::floor(p1[1])+GRIDSIZE_H):
static_cast<int>(std::ceil(p1[1])+GRIDSIZE_H-1);
165 const int zs = p1[2] < 0.0?
static_cast<int>(std::floor(p1[2])+GRIDSIZE_H):
static_cast<int>(std::ceil(p1[2])+GRIDSIZE_H-1);
166 const int xt = p3[0] < 0.0?
static_cast<int>(std::floor(p3[0])+GRIDSIZE_H):
static_cast<int>(std::ceil(p3[0])+GRIDSIZE_H-1);
167 const int yt = p3[1] < 0.0?
static_cast<int>(std::floor(p3[1])+GRIDSIZE_H):
static_cast<int>(std::ceil(p3[1])+GRIDSIZE_H-1);
168 const int zt = p3[2] < 0.0?
static_cast<int>(std::floor(p3[2])+GRIDSIZE_H):
static_cast<int>(std::ceil(p3[2])+GRIDSIZE_H-1);
169 wt_d2.push_back (this->lci (xs, ys, zs, xt, yt, zt, ratio, vxlcnt, pcnt2));
170 if (wt_d2.back () == 2)
171 h_mix_ratio[
static_cast<int>(
pcl_round (ratio * (binsize-1)))]++;
172 vxlcnt_sum += vxlcnt;
177 const int xs = p2[0] < 0.0?
static_cast<int>(std::floor(p2[0])+GRIDSIZE_H):
static_cast<int>(std::ceil(p2[0])+GRIDSIZE_H-1);
178 const int ys = p2[1] < 0.0?
static_cast<int>(std::floor(p2[1])+GRIDSIZE_H):
static_cast<int>(std::ceil(p2[1])+GRIDSIZE_H-1);
179 const int zs = p2[2] < 0.0?
static_cast<int>(std::floor(p2[2])+GRIDSIZE_H):
static_cast<int>(std::ceil(p2[2])+GRIDSIZE_H-1);
180 const int xt = p3[0] < 0.0?
static_cast<int>(std::floor(p3[0])+GRIDSIZE_H):
static_cast<int>(std::ceil(p3[0])+GRIDSIZE_H-1);
181 const int yt = p3[1] < 0.0?
static_cast<int>(std::floor(p3[1])+GRIDSIZE_H):
static_cast<int>(std::ceil(p3[1])+GRIDSIZE_H-1);
182 const int zt = p3[2] < 0.0?
static_cast<int>(std::floor(p3[2])+GRIDSIZE_H):
static_cast<int>(std::ceil(p3[2])+GRIDSIZE_H-1);
183 wt_d2.push_back (this->lci (xs,ys,zs,xt,yt,zt,ratio,vxlcnt,pcnt3));
184 if (wt_d2.back () == 2)
185 h_mix_ratio[
static_cast<int>(
pcl_round(ratio * (binsize-1)))]++;
186 vxlcnt_sum += vxlcnt;
191 d3v.push_back (std::sqrt (std::sqrt (s * (s-a) * (s-b) * (s-c))));
192 if (vxlcnt_sum <= 21)
195 h_a3_out[th1] +=
static_cast<float> (pcnt3) / 32.0f;
196 h_a3_out[th2] +=
static_cast<float> (pcnt1) / 32.0f;
197 h_a3_out[th3] +=
static_cast<float> (pcnt2) / 32.0f;
200 if (p_cnt - vxlcnt_sum < 4)
202 h_a3_in[th1] +=
static_cast<float> (pcnt3) / 32.0f;
203 h_a3_in[th2] +=
static_cast<float> (pcnt1) / 32.0f;
204 h_a3_in[th3] +=
static_cast<float> (pcnt2) / 32.0f;
209 h_a3_mix[th1] +=
static_cast<float> (pcnt3) / 32.0f;
210 h_a3_mix[th2] +=
static_cast<float> (pcnt1) / 32.0f;
211 h_a3_mix[th3] +=
static_cast<float> (pcnt2) / 32.0f;
212 wt_d3.push_back (
static_cast<float> (vxlcnt_sum) /
static_cast<float> (p_cnt));
219 for (std::size_t nn_idx = 0; nn_idx < sample_size; ++nn_idx)
222 if (d2v[nn_idx] > maxd2)
224 if (d2v[sample_size + nn_idx] > maxd2)
225 maxd2 = d2v[sample_size + nn_idx];
226 if (d2v[sample_size*2 +nn_idx] > maxd2)
227 maxd2 = d2v[sample_size*2 +nn_idx];
228 if (d3v[nn_idx] > maxd3)
234 for (std::size_t nn_idx = 0; nn_idx < sample_size; ++nn_idx)
236 if (wt_d3[nn_idx] >= 0.999)
238 index =
static_cast<int>(
pcl_round (d3v[nn_idx] / maxd3 * (binsize-1)));
239 if (index >= 0 && index < binsize)
244 if (wt_d3[nn_idx] <= 0.001)
246 index =
static_cast<int>(
pcl_round (d3v[nn_idx] / maxd3 * (binsize-1)));
247 if (index >= 0 && index < binsize)
252 index =
static_cast<int>(
pcl_round (d3v[nn_idx] / maxd3 * (binsize-1)));
253 if (index >= 0 && index < binsize)
259 for (std::size_t nn_idx = 0; nn_idx < d2v.size(); ++nn_idx )
261 if (wt_d2[nn_idx] == 0)
262 h_in[
static_cast<int>(
pcl_round (d2v[nn_idx] / maxd2 * (binsize-1)))]++ ;
263 if (wt_d2[nn_idx] == 1)
264 h_out[
static_cast<int>(
pcl_round (d2v[nn_idx] / maxd2 * (binsize-1)))]++;
265 if (wt_d2[nn_idx] == 2)
266 h_mix[
static_cast<int>(
pcl_round (d2v[nn_idx] / maxd2 * (binsize-1)))]++ ;
270 float weights[10] = {0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f};
272 hist.reserve (
static_cast<std::size_t
>(binsize) * 10);
273 for (
const float &i : h_a3_in)
274 hist.push_back (i * weights[0]);
275 for (
const float &i : h_a3_out)
276 hist.push_back (i * weights[1]);
277 for (
const float &i : h_a3_mix)
278 hist.push_back (i * weights[2]);
280 for (
const float &i : h_d3_in)
281 hist.push_back (i * weights[3]);
282 for (
const float &i : h_d3_out)
283 hist.push_back (i * weights[4]);
284 for (
const float &i : h_d3_mix)
285 hist.push_back (i * weights[5]);
287 for (
const float &i : h_in)
288 hist.push_back (i*0.5f * weights[6]);
289 for (
const float &i : h_out)
290 hist.push_back (i * weights[7]);
291 for (
const float &i : h_mix)
292 hist.push_back (i * weights[8]);
293 for (
const float &i : h_mix_ratio)
294 hist.push_back (i*0.5f * weights[9]);
297 for (
const float &i : hist)
300 for (
float &i : hist)
305 template <
typename Po
intInT,
typename Po
intOutT>
int
307 const int x1,
const int y1,
const int z1,
308 const int x2,
const int y2,
const int z2,
309 float &ratio,
int &incnt,
int &pointcount)
317 int x_inc, y_inc, z_inc;
325 int l = std::abs (dx);
330 int m = std::abs (dy);
335 int n = std::abs (dz);
339 if ((l >= m) & (l >= n))
343 for (
int i = 1; i<l; i++)
346 voxel_in +=
static_cast<int>(lut_[act_voxel[0]][act_voxel[1]][act_voxel[2]] == 1);
349 act_voxel[1] += y_inc;
354 act_voxel[2] += z_inc;
359 act_voxel[0] += x_inc;
362 else if ((m >= l) & (m >= n))
366 for (
int i=1; i<m; i++)
369 voxel_in +=
static_cast<int>(lut_[act_voxel[0]][act_voxel[1]][act_voxel[2]] == 1);
372 act_voxel[0] += x_inc;
377 act_voxel[2] += z_inc;
382 act_voxel[1] += y_inc;
389 for (
int i=1; i<n; i++)
392 voxel_in +=
static_cast<int>(lut_[act_voxel[0]][act_voxel[1]][act_voxel[2]] == 1);
395 act_voxel[1] += y_inc;
400 act_voxel[0] += x_inc;
405 act_voxel[2] += z_inc;
409 voxel_in +=
static_cast<int>(lut_[act_voxel[0]][act_voxel[1]][act_voxel[2]] == 1);
411 pointcount = voxelcount;
413 if (voxel_in >= voxelcount-1)
419 ratio =
static_cast<float>(voxel_in) /
static_cast<float>(voxelcount);
424 template <
typename Po
intInT,
typename Po
intOutT>
void
427 for (
const auto& point: cluster)
429 int xx = point.x<0.0?
static_cast<int>(std::floor(point.x)+GRIDSIZE_H) :
static_cast<int>(std::ceil(point.x)+GRIDSIZE_H-1);
430 int yy = point.y<0.0?
static_cast<int>(std::floor(point.y)+GRIDSIZE_H) :
static_cast<int>(std::ceil(point.y)+GRIDSIZE_H-1);
431 int zz = point.z<0.0?
static_cast<int>(std::floor(point.z)+GRIDSIZE_H) :
static_cast<int>(std::ceil(point.z)+GRIDSIZE_H-1);
433 for (
int x = -1; x < 2; x++)
434 for (
int y = -1; y < 2; y++)
435 for (
int z = -1; z < 2; z++)
441 if (yi >= GRIDSIZE || xi >= GRIDSIZE || zi>=GRIDSIZE || yi < 0 || xi < 0 || zi < 0)
446 this->lut_[xi][yi][zi] = 1;
452 template <
typename Po
intInT,
typename Po
intOutT>
void
455 for (
const auto& point: cluster)
457 int xx = point.x<0.0?
static_cast<int>(std::floor(point.x)+GRIDSIZE_H) :
static_cast<int>(std::ceil(point.x)+GRIDSIZE_H-1);
458 int yy = point.y<0.0?
static_cast<int>(std::floor(point.y)+GRIDSIZE_H) :
static_cast<int>(std::ceil(point.y)+GRIDSIZE_H-1);
459 int zz = point.z<0.0?
static_cast<int>(std::floor(point.z)+GRIDSIZE_H) :
static_cast<int>(std::ceil(point.z)+GRIDSIZE_H-1);
461 for (
int x = -1; x < 2; x++)
462 for (
int y = -1; y < 2; y++)
463 for (
int z = -1; z < 2; z++)
469 if (yi >= GRIDSIZE || xi >= GRIDSIZE || zi>=GRIDSIZE || yi < 0 || xi < 0 || zi < 0)
474 this->lut_[xi][yi][zi] = 0;
480 template <
typename Po
intInT,
typename Po
intOutT>
void
487 float max_distance = 0;
490 for (
const auto& point: local_cloud_)
493 if (d > max_distance)
497 float scale_factor = 1.0f / max_distance * scalefactor;
499 Eigen::Affine3f matrix = Eigen::Affine3f::Identity();
500 matrix.scale (scale_factor);
505 template<
typename Po
intInT,
typename Po
intOutT>
void
510 output.width = output.height = 0;
515 output.header = input_->header;
521 output.width = output.height = 1;
522 output.is_dense = input_->is_dense;
526 computeFeature (output);
533 template <
typename Po
intInT,
typename Po
intOutT>
void
536 Eigen::Vector4f xyz_centroid;
537 std::vector<float> hist;
538 scale_points_unit_sphere (*surface_,
static_cast<float>(GRIDSIZE_H), xyz_centroid);
539 this->voxelize9 (local_cloud_);
540 this->computeESF (local_cloud_, hist);
541 this->cleanup9 (local_cloud_);
548 for (std::size_t d = 0; d < hist.size (); ++d)
549 output[0].histogram[d] = hist[d];
552 #define PCL_INSTANTIATE_ESFEstimation(T,OutT) template class PCL_EXPORTS pcl::ESFEstimation<T,OutT>;
void compute(PointCloudOut &output)
Overloaded computed method from pcl::Feature.
void computeESF(PointCloudIn &pc, std::vector< float > &hist)
...
typename Feature< PointInT, PointOutT >::PointCloudOut PointCloudOut
void voxelize9(PointCloudIn &cluster)
...
void scale_points_unit_sphere(const pcl::PointCloud< PointInT > &pc, float scalefactor, Eigen::Vector4f ¢roid)
...
int lci(const int x1, const int y1, const int z1, const int x2, const int y2, const int z2, float &ratio, int &incnt, int &pointcount)
...
void cleanup9(PointCloudIn &cluster)
...
void computeFeature(PointCloudOut &output) override
Estimate the Ensebmel of Shape Function (ESF) descriptors at a set of points given by <setInputCloud ...
Feature represents the base feature class.
Define standard C methods to do distance calculations.
void demeanPointCloud(ConstCloudIterator< PointT > &cloud_iterator, const Eigen::Matrix< Scalar, 4, 1 > ¢roid, pcl::PointCloud< PointT > &cloud_out, int npts=0)
Subtract a centroid from a point cloud and return the de-meaned representation.
void transformPointCloud(const pcl::PointCloud< PointT > &cloud_in, pcl::PointCloud< PointT > &cloud_out, const Eigen::Matrix< Scalar, 4, 4 > &transform, bool copy_all_fields)
Apply a rigid transform defined by a 4x4 matrix.
unsigned int compute3DCentroid(ConstCloudIterator< PointT > &cloud_iterator, Eigen::Matrix< Scalar, 4, 1 > ¢roid)
Compute the 3D (X-Y-Z) centroid of a set of points and return it as a 3D vector.
float euclideanDistance(const PointType1 &p1, const PointType2 &p2)
Calculate the euclidean distance between the two given points.
__inline double pcl_round(double number)
Win32 doesn't seem to have rounding functions.
A point structure representing Euclidean xyz coordinates.