41 #ifndef PCL_FEATURES_IMPL_ESF_H_
42 #define PCL_FEATURES_IMPL_ESF_H_
44 #include <pcl/features/esf.h>
45 #include <pcl/common/common.h>
46 #include <pcl/common/distances.h>
47 #include <pcl/common/transforms.h>
51 template <
typename Po
intInT,
typename Po
intOutT>
void
55 const int binsize = 64;
56 unsigned int sample_size = 20000;
57 srand (static_cast<unsigned int> (time (0)));
58 int maxindex =
static_cast<int> (pc.
points.size ());
60 int index1, index2, index3;
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};
70 float h_out[binsize] = {0};
71 float h_mix[binsize] = {0};
72 float h_mix_ratio[binsize] = {0};
74 float h_a3_in[binsize] = {0};
75 float h_a3_out[binsize] = {0};
76 float h_a3_mix[binsize] = {0};
78 float h_d3_in[binsize] = {0};
79 float h_d3_out[binsize] = {0};
80 float h_d3_mix[binsize] = {0};
83 float pih =
static_cast<float>(M_PI) / 2.0f;
87 int pcnt1,pcnt2,pcnt3;
88 for (
size_t nn_idx = 0; nn_idx < sample_size; ++nn_idx)
91 index1 = rand()%maxindex;
92 index2 = rand()%maxindex;
93 index3 = rand()%maxindex;
95 if (index1==index2 || index1 == index3 || index2 == index3)
101 Eigen::Vector4f p1 = pc.
points[index1].getVector4fMap ();
102 Eigen::Vector4f p2 = pc.
points[index2].getVector4fMap ();
103 Eigen::Vector4f p3 = pc.
points[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)
118 th1 =
static_cast<int> (pcl_round (acos (fabs (v21.dot (v31))) / pih * (binsize-1)));
119 th2 =
static_cast<int> (pcl_round (acos (fabs (v23.dot (v31))) / pih * (binsize-1)));
120 th3 =
static_cast<int> (pcl_round (acos (fabs (v23.dot (v21))) / pih * (binsize-1)));
121 if (th1 < 0 || th1 >= binsize)
126 if (th2 < 0 || th2 >= binsize)
131 if (th3 < 0 || th3 >= binsize)
146 const int xs = p1[0] < 0.0?
static_cast<int>(floor(p1[0])+GRIDSIZE_H): static_cast<int>(ceil(p1[0])+GRIDSIZE_H-1);
147 const int ys = p1[1] < 0.0?
static_cast<int>(floor(p1[1])+GRIDSIZE_H): static_cast<int>(ceil(p1[1])+GRIDSIZE_H-1);
148 const int zs = p1[2] < 0.0?
static_cast<int>(floor(p1[2])+GRIDSIZE_H): static_cast<int>(ceil(p1[2])+GRIDSIZE_H-1);
149 const int xt = p2[0] < 0.0?
static_cast<int>(floor(p2[0])+GRIDSIZE_H): static_cast<int>(ceil(p2[0])+GRIDSIZE_H-1);
150 const int yt = p2[1] < 0.0?
static_cast<int>(floor(p2[1])+GRIDSIZE_H): static_cast<int>(ceil(p2[1])+GRIDSIZE_H-1);
151 const int zt = p2[2] < 0.0?
static_cast<int>(floor(p2[2])+GRIDSIZE_H): static_cast<int>(ceil(p2[2])+GRIDSIZE_H-1);
152 wt_d2.push_back (this->lci (xs, ys, zs, xt, yt, zt, ratio, vxlcnt, pcnt1));
153 if (wt_d2.back () == 2)
154 h_mix_ratio[static_cast<int> (pcl_round (ratio * (binsize-1)))]++;
155 vxlcnt_sum += vxlcnt;
160 const int xs = p1[0] < 0.0?
static_cast<int>(floor(p1[0])+GRIDSIZE_H): static_cast<int>(ceil(p1[0])+GRIDSIZE_H-1);
161 const int ys = p1[1] < 0.0?
static_cast<int>(floor(p1[1])+GRIDSIZE_H): static_cast<int>(ceil(p1[1])+GRIDSIZE_H-1);
162 const int zs = p1[2] < 0.0?
static_cast<int>(floor(p1[2])+GRIDSIZE_H): static_cast<int>(ceil(p1[2])+GRIDSIZE_H-1);
163 const int xt = p3[0] < 0.0?
static_cast<int>(floor(p3[0])+GRIDSIZE_H): static_cast<int>(ceil(p3[0])+GRIDSIZE_H-1);
164 const int yt = p3[1] < 0.0?
static_cast<int>(floor(p3[1])+GRIDSIZE_H): static_cast<int>(ceil(p3[1])+GRIDSIZE_H-1);
165 const int zt = p3[2] < 0.0?
static_cast<int>(floor(p3[2])+GRIDSIZE_H): static_cast<int>(ceil(p3[2])+GRIDSIZE_H-1);
166 wt_d2.push_back (this->lci (xs, ys, zs, xt, yt, zt, ratio, vxlcnt, pcnt2));
167 if (wt_d2.back () == 2)
168 h_mix_ratio[static_cast<int>(pcl_round (ratio * (binsize-1)))]++;
169 vxlcnt_sum += vxlcnt;
174 const int xs = p2[0] < 0.0?
static_cast<int>(floor(p2[0])+GRIDSIZE_H): static_cast<int>(ceil(p2[0])+GRIDSIZE_H-1);
175 const int ys = p2[1] < 0.0?
static_cast<int>(floor(p2[1])+GRIDSIZE_H): static_cast<int>(ceil(p2[1])+GRIDSIZE_H-1);
176 const int zs = p2[2] < 0.0?
static_cast<int>(floor(p2[2])+GRIDSIZE_H): static_cast<int>(ceil(p2[2])+GRIDSIZE_H-1);
177 const int xt = p3[0] < 0.0?
static_cast<int>(floor(p3[0])+GRIDSIZE_H): static_cast<int>(ceil(p3[0])+GRIDSIZE_H-1);
178 const int yt = p3[1] < 0.0?
static_cast<int>(floor(p3[1])+GRIDSIZE_H): static_cast<int>(ceil(p3[1])+GRIDSIZE_H-1);
179 const int zt = p3[2] < 0.0?
static_cast<int>(floor(p3[2])+GRIDSIZE_H): static_cast<int>(ceil(p3[2])+GRIDSIZE_H-1);
180 wt_d2.push_back (this->lci (xs,ys,zs,xt,yt,zt,ratio,vxlcnt,pcnt3));
181 if (wt_d2.back () == 2)
182 h_mix_ratio[static_cast<int>(pcl_round(ratio * (binsize-1)))]++;
183 vxlcnt_sum += vxlcnt;
188 d3v.push_back (sqrtf (sqrtf (s * (s-a) * (s-b) * (s-c))));
189 if (vxlcnt_sum <= 21)
192 h_a3_out[th1] +=
static_cast<float> (pcnt3) / 32.0f;
193 h_a3_out[th2] +=
static_cast<float> (pcnt1) / 32.0f;
194 h_a3_out[th3] +=
static_cast<float> (pcnt2) / 32.0f;
197 if (p_cnt - vxlcnt_sum < 4)
199 h_a3_in[th1] +=
static_cast<float> (pcnt3) / 32.0f;
200 h_a3_in[th2] +=
static_cast<float> (pcnt1) / 32.0f;
201 h_a3_in[th3] +=
static_cast<float> (pcnt2) / 32.0f;
206 h_a3_mix[th1] +=
static_cast<float> (pcnt3) / 32.0f;
207 h_a3_mix[th2] +=
static_cast<float> (pcnt1) / 32.0f;
208 h_a3_mix[th3] +=
static_cast<float> (pcnt2) / 32.0f;
209 wt_d3.push_back (static_cast<float> (vxlcnt_sum) / static_cast<float> (p_cnt));
216 for (
size_t nn_idx = 0; nn_idx < sample_size; ++nn_idx)
219 if (d2v[nn_idx] > maxd2)
221 if (d2v[sample_size + nn_idx] > maxd2)
222 maxd2 = d2v[sample_size + nn_idx];
223 if (d2v[sample_size*2 +nn_idx] > maxd2)
224 maxd2 = d2v[sample_size*2 +nn_idx];
225 if (d3v[nn_idx] > maxd3)
231 for (
size_t nn_idx = 0; nn_idx < sample_size; ++nn_idx)
233 if (wt_d3[nn_idx] >= 0.999)
235 index =
static_cast<int>(pcl_round (d3v[nn_idx] / maxd3 * (binsize-1)));
236 if (index >= 0 && index < binsize)
241 if (wt_d3[nn_idx] <= 0.001)
243 index =
static_cast<int>(pcl_round (d3v[nn_idx] / maxd3 * (binsize-1)));
244 if (index >= 0 && index < binsize)
249 index =
static_cast<int>(pcl_round (d3v[nn_idx] / maxd3 * (binsize-1)));
250 if (index >= 0 && index < binsize)
256 for (
size_t nn_idx = 0; nn_idx < d2v.size(); ++nn_idx )
258 if (wt_d2[nn_idx] == 0)
259 h_in[
static_cast<int>(pcl_round (d2v[nn_idx] / maxd2 * (binsize-1)))]++ ;
260 if (wt_d2[nn_idx] == 1)
261 h_out[
static_cast<int>(pcl_round (d2v[nn_idx] / maxd2 * (binsize-1)))]++;
262 if (wt_d2[nn_idx] == 2)
263 h_mix[
static_cast<int>(pcl_round (d2v[nn_idx] / maxd2 * (binsize-1)))]++ ;
267 float weights[10] = {0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f};
269 hist.reserve (binsize * 10);
270 for (
int i = 0; i < binsize; i++)
271 hist.push_back (h_a3_in[i] * weights[0]);
272 for (
int i = 0; i < binsize; i++)
273 hist.push_back (h_a3_out[i] * weights[1]);
274 for (
int i = 0; i < binsize; i++)
275 hist.push_back (h_a3_mix[i] * weights[2]);
277 for (
int i = 0; i < binsize; i++)
278 hist.push_back (h_d3_in[i] * weights[3]);
279 for (
int i = 0; i < binsize; i++)
280 hist.push_back (h_d3_out[i] * weights[4]);
281 for (
int i = 0; i < binsize; i++)
282 hist.push_back (h_d3_mix[i] * weights[5]);
284 for (
int i = 0; i < binsize; i++)
285 hist.push_back (h_in[i]*0.5f * weights[6]);
286 for (
int i = 0; i < binsize; i++)
287 hist.push_back (h_out[i] * weights[7]);
288 for (
int i = 0; i < binsize; i++)
289 hist.push_back (h_mix[i] * weights[8]);
290 for (
int i = 0; i < binsize; i++)
291 hist.push_back (h_mix_ratio[i]*0.5f * weights[9]);
294 for (
size_t i = 0; i < hist.size (); i++)
297 for (
size_t i = 0; i < hist.size (); i++)
302 template <
typename Po
intInT,
typename Po
intOutT>
int
304 const int x1,
const int y1,
const int z1,
305 const int x2,
const int y2,
const int z2,
306 float &ratio,
int &incnt,
int &pointcount)
314 int x_inc, y_inc, z_inc;
336 if ((l >= m) & (l >= n))
340 for (
int i = 1; i<l; i++)
343 voxel_in +=
static_cast<int>(lut_[act_voxel[0]][act_voxel[1]][act_voxel[2]] == 1);
346 act_voxel[1] += y_inc;
351 act_voxel[2] += z_inc;
356 act_voxel[0] += x_inc;
359 else if ((m >= l) & (m >= n))
363 for (
int i=1; i<m; i++)
366 voxel_in +=
static_cast<int>(lut_[act_voxel[0]][act_voxel[1]][act_voxel[2]] == 1);
369 act_voxel[0] += x_inc;
374 act_voxel[2] += z_inc;
379 act_voxel[1] += y_inc;
386 for (
int i=1; i<n; i++)
389 voxel_in +=
static_cast<int>(lut_[act_voxel[0]][act_voxel[1]][act_voxel[2]] == 1);
392 act_voxel[1] += y_inc;
397 act_voxel[0] += x_inc;
402 act_voxel[2] += z_inc;
406 voxel_in +=
static_cast<int>(lut_[act_voxel[0]][act_voxel[1]][act_voxel[2]] == 1);
408 pointcount = voxelcount;
410 if (voxel_in >= voxelcount-1)
416 ratio =
static_cast<float>(voxel_in) / static_cast<float>(voxelcount);
421 template <
typename Po
intInT,
typename Po
intOutT>
void
424 int xi,yi,zi,xx,yy,zz;
425 for (
size_t i = 0; i < cluster.
points.size (); ++i)
427 xx = cluster.
points[i].x<0.0?
static_cast<int>(floor(cluster.
points[i].x)+GRIDSIZE_H) : static_cast<int>(ceil(cluster.
points[i].x)+GRIDSIZE_H-1);
428 yy = cluster.
points[i].y<0.0?
static_cast<int>(floor(cluster.
points[i].y)+GRIDSIZE_H) : static_cast<int>(ceil(cluster.
points[i].y)+GRIDSIZE_H-1);
429 zz = cluster.
points[i].z<0.0?
static_cast<int>(floor(cluster.
points[i].z)+GRIDSIZE_H) : static_cast<int>(ceil(cluster.
points[i].z)+GRIDSIZE_H-1);
431 for (
int x = -1; x < 2; x++)
432 for (
int y = -1; y < 2; y++)
433 for (
int z = -1; z < 2; z++)
439 if (yi >= GRIDSIZE || xi >= GRIDSIZE || zi>=GRIDSIZE || yi < 0 || xi < 0 || zi < 0)
444 this->lut_[xi][yi][zi] = 1;
450 template <
typename Po
intInT,
typename Po
intOutT>
void
453 int xi,yi,zi,xx,yy,zz;
454 for (
size_t i = 0; i < cluster.
points.size (); ++i)
456 xx = cluster.
points[i].x<0.0?
static_cast<int>(floor(cluster.
points[i].x)+GRIDSIZE_H) : static_cast<int>(ceil(cluster.
points[i].x)+GRIDSIZE_H-1);
457 yy = cluster.
points[i].y<0.0?
static_cast<int>(floor(cluster.
points[i].y)+GRIDSIZE_H) : static_cast<int>(ceil(cluster.
points[i].y)+GRIDSIZE_H-1);
458 zz = cluster.
points[i].z<0.0?
static_cast<int>(floor(cluster.
points[i].z)+GRIDSIZE_H) : static_cast<int>(ceil(cluster.
points[i].z)+GRIDSIZE_H-1);
460 for (
int x = -1; x < 2; x++)
461 for (
int y = -1; y < 2; y++)
462 for (
int z = -1; z < 2; z++)
468 if (yi >= GRIDSIZE || xi >= GRIDSIZE || zi>=GRIDSIZE || yi < 0 || xi < 0 || zi < 0)
473 this->lut_[xi][yi][zi] = 0;
479 template <
typename Po
intInT,
typename Po
intOutT>
void
486 float max_distance = 0, d;
489 for (
size_t i = 0; i < local_cloud_.points.size (); ++i)
492 if (d > max_distance)
496 float scale_factor = 1.0f / max_distance * scalefactor;
498 Eigen::Affine3f matrix = Eigen::Affine3f::Identity();
499 matrix.scale (scale_factor);
504 template<
typename Po
intInT,
typename Po
intOutT>
void
514 output.
header = input_->header;
525 computeFeature (output);
532 template <
typename Po
intInT,
typename Po
intOutT>
void
535 Eigen::Vector4f xyz_centroid;
536 std::vector<float> hist;
537 scale_points_unit_sphere (*surface_, static_cast<float>(GRIDSIZE_H), xyz_centroid);
538 this->voxelize9 (local_cloud_);
539 this->computeESF (local_cloud_, hist);
540 this->cleanup9 (local_cloud_);
547 for (
size_t d = 0; d < hist.size (); ++d)
548 output.
points[0].histogram[d] = hist[d];
551 #define PCL_INSTANTIATE_ESFEstimation(T,OutT) template class PCL_EXPORTS pcl::ESFEstimation<T,OutT>;
553 #endif // PCL_FEATURES_IMPL_ESF_H_
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
void voxelize9(PointCloudIn &cluster)
...
void computeFeature(PointCloudOut &output)
Estimate the Ensebmel of Shape Function (ESF) descriptors at a set of points given by
void transformPointCloud(const pcl::PointCloud< PointT > &cloud_in, pcl::PointCloud< PointT > &cloud_out, const Eigen::Transform< Scalar, 3, Eigen::Affine > &transform)
Apply an affine transform defined by an Eigen Transform.
uint32_t height
The point cloud height (if organized as an image-structure).
float euclideanDistance(const PointType1 &p1, const PointType2 &p2)
Calculate the euclidean distance between the two given points.
void cleanup9(PointCloudIn &cluster)
...
void scale_points_unit_sphere(const pcl::PointCloud< PointInT > &pc, float scalefactor, Eigen::Vector4f ¢roid)
...
uint32_t width
The point cloud width (if organized as an image-structure).
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.
A point structure representing Euclidean xyz coordinates.
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)
...
pcl::PCLHeader header
The point cloud header.
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values).
void computeESF(PointCloudIn &pc, std::vector< float > &hist)
...
Feature represents the base feature class.
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.
void compute(PointCloudOut &output)
Overloaded computed method from pcl::Feature.