Point Cloud Library (PCL)  1.9.1-dev
grid_projection.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2010, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $Id$
35  *
36  */
37 
38 #ifndef PCL_SURFACE_IMPL_GRID_PROJECTION_H_
39 #define PCL_SURFACE_IMPL_GRID_PROJECTION_H_
40 
41 #include <pcl/surface/grid_projection.h>
42 #include <pcl/common/common.h>
43 #include <pcl/common/centroid.h>
44 #include <pcl/common/vector_average.h>
45 #include <pcl/Vertices.h>
46 
47 //////////////////////////////////////////////////////////////////////////////////////////////
48 template <typename PointNT>
50  cell_hash_map_ (), leaf_size_ (0.001), gaussian_scale_ (),
51  data_size_ (0), max_binary_search_level_ (10), k_ (50), padding_size_ (3), data_ ()
52 {}
53 
54 //////////////////////////////////////////////////////////////////////////////////////////////
55 template <typename PointNT>
57  cell_hash_map_ (), leaf_size_ (resolution), gaussian_scale_ (),
58  data_size_ (0), max_binary_search_level_ (10), k_ (50), padding_size_ (3), data_ ()
59 {}
60 
61 //////////////////////////////////////////////////////////////////////////////////////////////
62 template <typename PointNT>
64 {
65  vector_at_data_point_.clear ();
66  surface_.clear ();
67  cell_hash_map_.clear ();
68  occupied_cell_list_.clear ();
69  data_.reset ();
70 }
71 
72 //////////////////////////////////////////////////////////////////////////////////////////////
73 template <typename PointNT> void
75 {
76  for (size_t i = 0; i < data_->points.size(); ++i)
77  {
78  data_->points[i].x /= static_cast<float> (scale_factor);
79  data_->points[i].y /= static_cast<float> (scale_factor);
80  data_->points[i].z /= static_cast<float> (scale_factor);
81  }
82  max_p_ /= static_cast<float> (scale_factor);
83  min_p_ /= static_cast<float> (scale_factor);
84 }
85 
86 //////////////////////////////////////////////////////////////////////////////////////////////
87 template <typename PointNT> void
89 {
90  pcl::getMinMax3D (*data_, min_p_, max_p_);
91 
92  Eigen::Vector4f bounding_box_size = max_p_ - min_p_;
93  double scale_factor = (std::max)((std::max)(bounding_box_size.x (),
94  bounding_box_size.y ()),
95  bounding_box_size.z ());
96  if (scale_factor > 1)
97  scaleInputDataPoint (scale_factor);
98 
99  // Round the max_p_, min_p_ so that they are aligned with the cells vertices
100  int upper_right_index[3];
101  int lower_left_index[3];
102  for (size_t i = 0; i < 3; ++i)
103  {
104  upper_right_index[i] = static_cast<int> (max_p_(i) / leaf_size_ + 5);
105  lower_left_index[i] = static_cast<int> (min_p_(i) / leaf_size_ - 5);
106  max_p_(i) = static_cast<float> (upper_right_index[i] * leaf_size_);
107  min_p_(i) = static_cast<float> (lower_left_index[i] * leaf_size_);
108  }
109  bounding_box_size = max_p_ - min_p_;
110  PCL_DEBUG ("[pcl::GridProjection::getBoundingBox] Size of Bounding Box is [%f, %f, %f]\n",
111  bounding_box_size.x (), bounding_box_size.y (), bounding_box_size.z ());
112  double max_size =
113  (std::max) ((std::max)(bounding_box_size.x (), bounding_box_size.y ()),
114  bounding_box_size.z ());
115 
116  data_size_ = static_cast<int> (max_size / leaf_size_);
117  PCL_DEBUG ("[pcl::GridProjection::getBoundingBox] Lower left point is [%f, %f, %f]\n",
118  min_p_.x (), min_p_.y (), min_p_.z ());
119  PCL_DEBUG ("[pcl::GridProjection::getBoundingBox] Upper left point is [%f, %f, %f]\n",
120  max_p_.x (), max_p_.y (), max_p_.z ());
121  PCL_DEBUG ("[pcl::GridProjection::getBoundingBox] Padding size: %d\n", padding_size_);
122  PCL_DEBUG ("[pcl::GridProjection::getBoundingBox] Leaf size: %f\n", leaf_size_);
123  occupied_cell_list_.resize (data_size_ * data_size_ * data_size_);
124  gaussian_scale_ = pow ((padding_size_+1) * leaf_size_ / 2.0, 2.0);
125 }
126 
127 //////////////////////////////////////////////////////////////////////////////////////////////
128 template <typename PointNT> void
130  const Eigen::Vector4f &cell_center,
131  std::vector<Eigen::Vector4f, Eigen::aligned_allocator<Eigen::Vector4f> > &pts) const
132 {
133  assert (pts.size () == 8);
134 
135  float sz = static_cast<float> (leaf_size_) / 2.0f;
136 
137  pts[0] = cell_center + Eigen::Vector4f (-sz, sz, -sz, 0);
138  pts[1] = cell_center + Eigen::Vector4f (-sz, -sz, -sz, 0);
139  pts[2] = cell_center + Eigen::Vector4f (sz, -sz, -sz, 0);
140  pts[3] = cell_center + Eigen::Vector4f (sz, sz, -sz, 0);
141  pts[4] = cell_center + Eigen::Vector4f (-sz, sz, sz, 0);
142  pts[5] = cell_center + Eigen::Vector4f (-sz, -sz, sz, 0);
143  pts[6] = cell_center + Eigen::Vector4f (sz, -sz, sz, 0);
144  pts[7] = cell_center + Eigen::Vector4f (sz, sz, sz, 0);
145 }
146 
147 //////////////////////////////////////////////////////////////////////////////////////////////
148 template <typename PointNT> void
149 pcl::GridProjection<PointNT>::getDataPtsUnion (const Eigen::Vector3i &index,
150  std::vector <int> &pt_union_indices)
151 {
152  for (int i = index[0] - padding_size_; i <= index[0] + padding_size_; ++i)
153  {
154  for (int j = index[1] - padding_size_; j <= index[1] + padding_size_; ++j)
155  {
156  for (int k = index[2] - padding_size_; k <= index[2] + padding_size_; ++k)
157  {
158  Eigen::Vector3i cell_index_3d (i, j, k);
159  int cell_index_1d = getIndexIn1D (cell_index_3d);
160  if (cell_hash_map_.find (cell_index_1d) != cell_hash_map_.end ())
161  {
162  // Get the indices of the input points within the cell(i,j,k), which
163  // is stored in the hash map
164  pt_union_indices.insert (pt_union_indices.end (),
165  cell_hash_map_.at (cell_index_1d).data_indices.begin (),
166  cell_hash_map_.at (cell_index_1d).data_indices.end ());
167  }
168  }
169  }
170  }
171 }
172 
173 //////////////////////////////////////////////////////////////////////////////////////////////
174 template <typename PointNT> void
176  std::vector <int> &pt_union_indices)
177 {
178  // 8 vertices of the cell
179  std::vector<Eigen::Vector4f, Eigen::aligned_allocator<Eigen::Vector4f> > vertices (8);
180 
181  // 4 end points that shared by 3 edges connecting the upper left front points
182  Eigen::Vector4f pts[4];
183  Eigen::Vector3f vector_at_pts[4];
184 
185  // Given the index of cell, caluate the coordinates of the eight vertices of the cell
186  // index the index of the cell in (x,y,z) 3d format
187  Eigen::Vector4f cell_center = Eigen::Vector4f::Zero ();
188  getCellCenterFromIndex (index, cell_center);
189  getVertexFromCellCenter (cell_center, vertices);
190 
191  // Get the indices of the cells which stores the 4 end points.
192  Eigen::Vector3i indices[4];
193  indices[0] = Eigen::Vector3i (index[0], index[1], index[2] - 1);
194  indices[1] = Eigen::Vector3i (index[0], index[1], index[2]);
195  indices[2] = Eigen::Vector3i (index[0], index[1] - 1, index[2]);
196  indices[3] = Eigen::Vector3i (index[0] + 1, index[1], index[2]);
197 
198  // Get the coordinate of the 4 end points, and the corresponding vectors
199  for (size_t i = 0; i < 4; ++i)
200  {
201  pts[i] = vertices[I_SHIFT_PT[i]];
202  unsigned int index_1d = getIndexIn1D (indices[i]);
203  if (cell_hash_map_.find (index_1d) == cell_hash_map_.end () ||
204  occupied_cell_list_[index_1d] == 0)
205  return;
206  else
207  vector_at_pts[i] = cell_hash_map_.at (index_1d).vect_at_grid_pt;
208  }
209 
210  // Go through the 3 edges, test whether they are intersected by the surface
211  for (size_t i = 0; i < 3; ++i)
212  {
213  std::vector<Eigen::Vector4f, Eigen::aligned_allocator<Eigen::Vector4f> > end_pts (2);
214  std::vector<Eigen::Vector3f, Eigen::aligned_allocator<Eigen::Vector3f> > vect_at_end_pts (2);
215  for (size_t j = 0; j < 2; ++j)
216  {
217  end_pts[j] = pts[I_SHIFT_EDGE[i][j]];
218  vect_at_end_pts[j] = vector_at_pts[I_SHIFT_EDGE[i][j]];
219  }
220 
221  if (isIntersected (end_pts, vect_at_end_pts, pt_union_indices))
222  {
223  // Indices of cells that contains points which will be connected to
224  // create a polygon
225  Eigen::Vector3i polygon[4];
226  Eigen::Vector4f polygon_pts[4];
227  int polygon_indices_1d[4];
228  bool is_all_in_hash_map = true;
229  switch (i)
230  {
231  case 0:
232  polygon[0] = Eigen::Vector3i (index[0] - 1, index[1] + 1, index[2]);
233  polygon[1] = Eigen::Vector3i (index[0] - 1, index[1], index[2]);
234  polygon[2] = Eigen::Vector3i (index[0], index[1], index[2]);
235  polygon[3] = Eigen::Vector3i (index[0], index[1] + 1, index[2]);
236  break;
237  case 1:
238  polygon[0] = Eigen::Vector3i (index[0], index[1] + 1, index[2] + 1);
239  polygon[1] = Eigen::Vector3i (index[0], index[1] + 1, index[2]);
240  polygon[2] = Eigen::Vector3i (index[0], index[1], index[2]);
241  polygon[3] = Eigen::Vector3i (index[0], index[1], index[2] + 1);
242  break;
243  case 2:
244  polygon[0] = Eigen::Vector3i (index[0] - 1, index[1], index[2] + 1);
245  polygon[1] = Eigen::Vector3i (index[0] - 1, index[1], index[2]);
246  polygon[2] = Eigen::Vector3i (index[0], index[1], index[2]);
247  polygon[3] = Eigen::Vector3i (index[0], index[1], index[2] + 1);
248  break;
249  default:
250  break;
251  }
252  for (size_t k = 0; k < 4; k++)
253  {
254  polygon_indices_1d[k] = getIndexIn1D (polygon[k]);
255  if (!occupied_cell_list_[polygon_indices_1d[k]])
256  {
257  is_all_in_hash_map = false;
258  break;
259  }
260  }
261  if (is_all_in_hash_map)
262  {
263  for (size_t k = 0; k < 4; k++)
264  {
265  polygon_pts[k] = cell_hash_map_.at (polygon_indices_1d[k]).pt_on_surface;
266  surface_.push_back (polygon_pts[k]);
267  }
268  }
269  }
270  }
271 }
272 
273 //////////////////////////////////////////////////////////////////////////////////////////////
274 template <typename PointNT> void
276  std::vector <int> &pt_union_indices, Eigen::Vector4f &projection)
277 {
278  const double projection_distance = leaf_size_ * 3;
279  std::vector<Eigen::Vector4f, Eigen::aligned_allocator<Eigen::Vector4f> > end_pt (2);
280  std::vector<Eigen::Vector3f, Eigen::aligned_allocator<Eigen::Vector3f> > end_pt_vect (2);
281  end_pt[0] = p;
282  getVectorAtPoint (end_pt[0], pt_union_indices, end_pt_vect[0]);
283  end_pt_vect[0].normalize();
284 
285  double dSecond = getD2AtPoint (end_pt[0], end_pt_vect[0], pt_union_indices);
286 
287  // Find another point which is projection_distance away from the p, do a
288  // binary search between these two points, to find the projected point on the
289  // surface
290  if (dSecond > 0)
291  end_pt[1] = end_pt[0] + Eigen::Vector4f (
292  end_pt_vect[0][0] * static_cast<float> (projection_distance),
293  end_pt_vect[0][1] * static_cast<float> (projection_distance),
294  end_pt_vect[0][2] * static_cast<float> (projection_distance),
295  0.0f);
296  else
297  end_pt[1] = end_pt[0] - Eigen::Vector4f (
298  end_pt_vect[0][0] * static_cast<float> (projection_distance),
299  end_pt_vect[0][1] * static_cast<float> (projection_distance),
300  end_pt_vect[0][2] * static_cast<float> (projection_distance),
301  0.0f);
302  getVectorAtPoint (end_pt[1], pt_union_indices, end_pt_vect[1]);
303  if (end_pt_vect[1].dot (end_pt_vect[0]) < 0)
304  {
305  Eigen::Vector4f mid_pt = end_pt[0] + (end_pt[1] - end_pt[0]) * 0.5;
306  findIntersection (0, end_pt, end_pt_vect, mid_pt, pt_union_indices, projection);
307  }
308  else
309  projection = p;
310 }
311 
312 //////////////////////////////////////////////////////////////////////////////////////////////
313 template <typename PointNT> void
315  std::vector<int> (&pt_union_indices),
316  Eigen::Vector4f &projection)
317 {
318  // Compute the plane coefficients
319  Eigen::Vector4f model_coefficients;
320  /// @remark iterative weighted least squares or sac might give better results
321  Eigen::Matrix3f covariance_matrix;
322  Eigen::Vector4f xyz_centroid;
323 
324  computeMeanAndCovarianceMatrix (*data_, pt_union_indices, covariance_matrix, xyz_centroid);
325 
326  // Get the plane normal
327  EIGEN_ALIGN16 Eigen::Vector3f::Scalar eigen_value;
328  EIGEN_ALIGN16 Eigen::Vector3f eigen_vector;
329  pcl::eigen33 (covariance_matrix, eigen_value, eigen_vector);
330 
331  // The normalization is not necessary, since the eigenvectors from libeigen are already normalized
332  model_coefficients[0] = eigen_vector [0];
333  model_coefficients[1] = eigen_vector [1];
334  model_coefficients[2] = eigen_vector [2];
335  model_coefficients[3] = 0;
336  // Hessian form (D = nc . p_plane (centroid here) + p)
337  model_coefficients[3] = -1 * model_coefficients.dot (xyz_centroid);
338 
339  // Projected point
340  Eigen::Vector3f point (p.x (), p.y (), p.z ()); //= Eigen::Vector3f::MapAligned (&output.points[cp].x, 3);
341  float distance = point.dot (model_coefficients.head <3> ()) + model_coefficients[3];
342  point -= distance * model_coefficients.head < 3 > ();
343 
344  projection = Eigen::Vector4f (point[0], point[1], point[2], 0);
345 }
346 
347 //////////////////////////////////////////////////////////////////////////////////////////////
348 template <typename PointNT> void
350  std::vector <int> &pt_union_indices,
351  Eigen::Vector3f &vo)
352 {
353  std::vector <double> pt_union_dist (pt_union_indices.size ());
354  std::vector <double> pt_union_weight (pt_union_indices.size ());
355  Eigen::Vector3f out_vector (0, 0, 0);
356  double sum = 0.0;
357  double mag = 0.0;
358 
359  for (size_t i = 0; i < pt_union_indices.size (); ++i)
360  {
361  Eigen::Vector4f pp (data_->points[pt_union_indices[i]].x, data_->points[pt_union_indices[i]].y, data_->points[pt_union_indices[i]].z, 0);
362  pt_union_dist[i] = (pp - p).squaredNorm ();
363  pt_union_weight[i] = pow (M_E, -pow (pt_union_dist[i], 2.0) / gaussian_scale_);
364  mag += pow (M_E, -pow (sqrt (pt_union_dist[i]), 2.0) / gaussian_scale_);
365  sum += pt_union_weight[i];
366  }
367 
368  pcl::VectorAverage3f vector_average;
369 
370  Eigen::Vector3f v (
371  data_->points[pt_union_indices[0]].normal[0],
372  data_->points[pt_union_indices[0]].normal[1],
373  data_->points[pt_union_indices[0]].normal[2]);
374 
375  for (size_t i = 0; i < pt_union_weight.size (); ++i)
376  {
377  pt_union_weight[i] /= sum;
378  Eigen::Vector3f vec (data_->points[pt_union_indices[i]].normal[0],
379  data_->points[pt_union_indices[i]].normal[1],
380  data_->points[pt_union_indices[i]].normal[2]);
381  if (vec.dot (v) < 0)
382  vec = -vec;
383  vector_average.add (vec, static_cast<float> (pt_union_weight[i]));
384  }
385  out_vector = vector_average.getMean ();
386  // vector_average.getEigenVector1(out_vector);
387 
388  out_vector.normalize ();
389  double d1 = getD1AtPoint (p, out_vector, pt_union_indices);
390  out_vector *= static_cast<float> (sum);
391  vo = ((d1 > 0) ? -1 : 1) * out_vector;
392 }
393 
394 //////////////////////////////////////////////////////////////////////////////////////////////
395 template <typename PointNT> void
397  std::vector <int> &k_indices,
398  std::vector <float> &k_squared_distances,
399  Eigen::Vector3f &vo)
400 {
401  Eigen::Vector3f out_vector (0, 0, 0);
402  std::vector <float> k_weight;
403  k_weight.resize (k_);
404  float sum = 0.0;
405  for (int i = 0; i < k_; i++)
406  {
407  //k_weight[i] = pow (M_E, -pow (k_squared_distances[i], 2.0) / gaussian_scale_);
408  k_weight[i] = std::pow (static_cast<float>(M_E), static_cast<float>(-std::pow (static_cast<float>(k_squared_distances[i]), 2.0f) / gaussian_scale_));
409  sum += k_weight[i];
410  }
411  pcl::VectorAverage3f vector_average;
412  for (int i = 0; i < k_; i++)
413  {
414  k_weight[i] /= sum;
415  Eigen::Vector3f vec (data_->points[k_indices[i]].normal[0],
416  data_->points[k_indices[i]].normal[1],
417  data_->points[k_indices[i]].normal[2]);
418  vector_average.add (vec, k_weight[i]);
419  }
420  vector_average.getEigenVector1 (out_vector);
421  out_vector.normalize ();
422  double d1 = getD1AtPoint (p, out_vector, k_indices);
423  out_vector *= sum;
424  vo = ((d1 > 0) ? -1 : 1) * out_vector;
425 
426 }
427 
428 //////////////////////////////////////////////////////////////////////////////////////////////
429 template <typename PointNT> double
431  const std::vector <int> &pt_union_indices)
432 {
433  std::vector <double> pt_union_dist (pt_union_indices.size ());
434  std::vector <double> pt_union_weight (pt_union_indices.size ());
435  double sum = 0.0;
436  for (size_t i = 0; i < pt_union_indices.size (); ++i)
437  {
438  Eigen::Vector4f pp (data_->points[pt_union_indices[i]].x, data_->points[pt_union_indices[i]].y, data_->points[pt_union_indices[i]].z, 0);
439  pt_union_dist[i] = (pp - p).norm ();
440  sum += pow (M_E, -pow (pt_union_dist[i], 2.0) / gaussian_scale_);
441  }
442  return (sum);
443 }
444 
445 //////////////////////////////////////////////////////////////////////////////////////////////
446 template <typename PointNT> double
447 pcl::GridProjection<PointNT>::getD1AtPoint (const Eigen::Vector4f &p, const Eigen::Vector3f &vec,
448  const std::vector <int> &pt_union_indices)
449 {
450  double sz = 0.01 * leaf_size_;
451  Eigen::Vector3f v = vec * static_cast<float> (sz);
452 
453  double forward = getMagAtPoint (p + Eigen::Vector4f (v[0], v[1], v[2], 0), pt_union_indices);
454  double backward = getMagAtPoint (p - Eigen::Vector4f (v[0], v[1], v[2], 0), pt_union_indices);
455  return ((forward - backward) / (0.02 * leaf_size_));
456 }
457 
458 //////////////////////////////////////////////////////////////////////////////////////////////
459 template <typename PointNT> double
460 pcl::GridProjection<PointNT>::getD2AtPoint (const Eigen::Vector4f &p, const Eigen::Vector3f &vec,
461  const std::vector <int> &pt_union_indices)
462 {
463  double sz = 0.01 * leaf_size_;
464  Eigen::Vector3f v = vec * static_cast<float> (sz);
465 
466  double forward = getD1AtPoint (p + Eigen::Vector4f (v[0], v[1], v[2], 0), vec, pt_union_indices);
467  double backward = getD1AtPoint (p - Eigen::Vector4f (v[0], v[1], v[2], 0), vec, pt_union_indices);
468  return ((forward - backward) / (0.02 * leaf_size_));
469 }
470 
471 //////////////////////////////////////////////////////////////////////////////////////////////
472 template <typename PointNT> bool
473 pcl::GridProjection<PointNT>::isIntersected (const std::vector<Eigen::Vector4f, Eigen::aligned_allocator<Eigen::Vector4f> > &end_pts,
474  std::vector<Eigen::Vector3f, Eigen::aligned_allocator<Eigen::Vector3f> > &vect_at_end_pts,
475  std::vector <int> &pt_union_indices)
476 {
477  assert (end_pts.size () == 2);
478  assert (vect_at_end_pts.size () == 2);
479 
480  double length[2];
481  for (size_t i = 0; i < 2; ++i)
482  {
483  length[i] = vect_at_end_pts[i].norm ();
484  vect_at_end_pts[i].normalize ();
485  }
486  double dot_prod = vect_at_end_pts[0].dot (vect_at_end_pts[1]);
487  if (dot_prod < 0)
488  {
489  double ratio = length[0] / (length[0] + length[1]);
490  Eigen::Vector4f start_pt =
491  end_pts[0] + (end_pts[1] - end_pts[0]) * static_cast<float> (ratio);
492  Eigen::Vector4f intersection_pt = Eigen::Vector4f::Zero ();
493  findIntersection (0, end_pts, vect_at_end_pts, start_pt, pt_union_indices, intersection_pt);
494 
495  Eigen::Vector3f vec;
496  getVectorAtPoint (intersection_pt, pt_union_indices, vec);
497  vec.normalize ();
498 
499  double d2 = getD2AtPoint (intersection_pt, vec, pt_union_indices);
500  if (d2 < 0)
501  return (true);
502  }
503  return (false);
504 }
505 
506 //////////////////////////////////////////////////////////////////////////////////////////////
507 template <typename PointNT> void
509  const std::vector<Eigen::Vector4f, Eigen::aligned_allocator<Eigen::Vector4f> > &end_pts,
510  const std::vector<Eigen::Vector3f, Eigen::aligned_allocator<Eigen::Vector3f> > &vect_at_end_pts,
511  const Eigen::Vector4f &start_pt,
512  std::vector <int> &pt_union_indices,
513  Eigen::Vector4f &intersection)
514 {
515  assert (end_pts.size () == 2);
516  assert (vect_at_end_pts.size () == 2);
517 
518  Eigen::Vector3f vec;
519  getVectorAtPoint (start_pt, pt_union_indices, vec);
520  double d1 = getD1AtPoint (start_pt, vec, pt_union_indices);
521  std::vector<Eigen::Vector4f, Eigen::aligned_allocator<Eigen::Vector4f> > new_end_pts (2);
522  std::vector<Eigen::Vector3f, Eigen::aligned_allocator<Eigen::Vector3f> > new_vect_at_end_pts (2);
523  if ((fabs (d1) < 10e-3) || (level == max_binary_search_level_))
524  {
525  intersection = start_pt;
526  return;
527  }
528  else
529  {
530  vec.normalize ();
531  if (vec.dot (vect_at_end_pts[0]) < 0)
532  {
533  Eigen::Vector4f new_start_pt = end_pts[0] + (start_pt - end_pts[0]) * 0.5;
534  new_end_pts[0] = end_pts[0];
535  new_end_pts[1] = start_pt;
536  new_vect_at_end_pts[0] = vect_at_end_pts[0];
537  new_vect_at_end_pts[1] = vec;
538  findIntersection (level + 1, new_end_pts, new_vect_at_end_pts, new_start_pt, pt_union_indices, intersection);
539  return;
540  }
541  if (vec.dot (vect_at_end_pts[1]) < 0)
542  {
543  Eigen::Vector4f new_start_pt = start_pt + (end_pts[1] - start_pt) * 0.5;
544  new_end_pts[0] = start_pt;
545  new_end_pts[1] = end_pts[1];
546  new_vect_at_end_pts[0] = vec;
547  new_vect_at_end_pts[1] = vect_at_end_pts[1];
548  findIntersection (level + 1, new_end_pts, new_vect_at_end_pts, new_start_pt, pt_union_indices, intersection);
549  return;
550  }
551  intersection = start_pt;
552  return;
553  }
554 }
555 
556 
557 //////////////////////////////////////////////////////////////////////////////////////////////
558 template <typename PointNT> void
559 pcl::GridProjection<PointNT>::fillPad (const Eigen::Vector3i &index)
560 {
561  for (int i = index[0] - padding_size_; i < index[0] + padding_size_; ++i)
562  {
563  for (int j = index[1] - padding_size_; j < index[1] + padding_size_; ++j)
564  {
565  for (int k = index[2] - padding_size_; k < index[2] + padding_size_; ++k)
566  {
567  Eigen::Vector3i cell_index_3d (i, j, k);
568  unsigned int cell_index_1d = getIndexIn1D (cell_index_3d);
569  if (cell_hash_map_.find (cell_index_1d) == cell_hash_map_.end ())
570  {
571  cell_hash_map_[cell_index_1d].data_indices.resize (0);
572  getCellCenterFromIndex (cell_index_3d, cell_hash_map_[cell_index_1d].pt_on_surface);
573  }
574  }
575  }
576  }
577 }
578 
579 
580 //////////////////////////////////////////////////////////////////////////////////////////////
581 template <typename PointNT> void
583  const Eigen::Vector3i &,
584  std::vector <int> &pt_union_indices,
585  const Leaf &cell_data)
586 {
587  // Get point on grid
588  Eigen::Vector4f grid_pt (
589  cell_data.pt_on_surface.x () - static_cast<float> (leaf_size_) / 2.0f,
590  cell_data.pt_on_surface.y () + static_cast<float> (leaf_size_) / 2.0f,
591  cell_data.pt_on_surface.z () + static_cast<float> (leaf_size_) / 2.0f, 0.0f);
592 
593  // Save the vector and the point on the surface
594  getVectorAtPoint (grid_pt, pt_union_indices, cell_hash_map_[index_1d].vect_at_grid_pt);
595  getProjection (cell_data.pt_on_surface, pt_union_indices, cell_hash_map_[index_1d].pt_on_surface);
596 }
597 
598 //////////////////////////////////////////////////////////////////////////////////////////////
599 template <typename PointNT> void
600 pcl::GridProjection<PointNT>::storeVectAndSurfacePointKNN (int index_1d, const Eigen::Vector3i &,
601  const Leaf &cell_data)
602 {
603  Eigen::Vector4f cell_center = cell_data.pt_on_surface;
604  Eigen::Vector4f grid_pt (
605  cell_center.x () - static_cast<float> (leaf_size_) / 2.0f,
606  cell_center.y () + static_cast<float> (leaf_size_) / 2.0f,
607  cell_center.z () + static_cast<float> (leaf_size_) / 2.0f, 0.0f);
608 
609  std::vector <int> k_indices;
610  k_indices.resize (k_);
611  std::vector <float> k_squared_distances;
612  k_squared_distances.resize (k_);
613 
614  PointNT pt; pt.x = grid_pt.x (); pt.y = grid_pt.y (); pt.z = grid_pt.z ();
615  tree_->nearestKSearch (pt, k_, k_indices, k_squared_distances);
616 
617  getVectorAtPointKNN (grid_pt, k_indices, k_squared_distances, cell_hash_map_[index_1d].vect_at_grid_pt);
618  getProjectionWithPlaneFit (cell_center, k_indices, cell_hash_map_[index_1d].pt_on_surface);
619 }
620 
621 //////////////////////////////////////////////////////////////////////////////////////////////
622 template <typename PointNT> bool
623 pcl::GridProjection<PointNT>::reconstructPolygons (std::vector<pcl::Vertices> &polygons)
624 {
625  data_.reset (new pcl::PointCloud<PointNT> (*input_));
626  getBoundingBox ();
627 
628  // Store the point cloud data into the voxel grid, and at the same time
629  // create a hash map to store the information for each cell
630  cell_hash_map_.max_load_factor (2.0);
631  cell_hash_map_.rehash (data_->points.size () / static_cast<long unsigned int> (cell_hash_map_.max_load_factor ()));
632 
633  // Go over all points and insert them into the right leaf
634  for (int cp = 0; cp < static_cast<int> (data_->points.size ()); ++cp)
635  {
636  // Check if the point is invalid
637  if (!std::isfinite (data_->points[cp].x) ||
638  !std::isfinite (data_->points[cp].y) ||
639  !std::isfinite (data_->points[cp].z))
640  continue;
641 
642  Eigen::Vector3i index_3d;
643  getCellIndex (data_->points[cp].getVector4fMap (), index_3d);
644  int index_1d = getIndexIn1D (index_3d);
645  if (cell_hash_map_.find (index_1d) == cell_hash_map_.end ())
646  {
647  Leaf cell_data;
648  cell_data.data_indices.push_back (cp);
649  getCellCenterFromIndex (index_3d, cell_data.pt_on_surface);
650  cell_hash_map_[index_1d] = cell_data;
651  occupied_cell_list_[index_1d] = 1;
652  }
653  else
654  {
655  Leaf cell_data = cell_hash_map_.at (index_1d);
656  cell_data.data_indices.push_back (cp);
657  cell_hash_map_[index_1d] = cell_data;
658  }
659  }
660 
661  Eigen::Vector3i index;
662  int numOfFilledPad = 0;
663 
664  for (int i = 0; i < data_size_; ++i)
665  {
666  for (int j = 0; j < data_size_; ++j)
667  {
668  for (int k = 0; k < data_size_; ++k)
669  {
670  index[0] = i;
671  index[1] = j;
672  index[2] = k;
673  if (occupied_cell_list_[getIndexIn1D (index)])
674  {
675  fillPad (index);
676  numOfFilledPad++;
677  }
678  }
679  }
680  }
681 
682  // Update the hashtable and store the vector and point
683  BOOST_FOREACH (typename HashMap::value_type entry, cell_hash_map_)
684  {
685  getIndexIn3D (entry.first, index);
686  std::vector <int> pt_union_indices;
687  getDataPtsUnion (index, pt_union_indices);
688 
689  // Needs at least 10 points (?)
690  // NOTE: set as parameter later
691  if (pt_union_indices.size () > 10)
692  {
693  storeVectAndSurfacePoint (entry.first, index, pt_union_indices, entry.second);
694  //storeVectAndSurfacePointKNN(entry.first, index, entry.second);
695  occupied_cell_list_[entry.first] = 1;
696  }
697  }
698 
699  // Go through the hash table another time to extract surface
700  BOOST_FOREACH (typename HashMap::value_type entry, cell_hash_map_)
701  {
702  getIndexIn3D (entry.first, index);
703  std::vector <int> pt_union_indices;
704  getDataPtsUnion (index, pt_union_indices);
705 
706  // Needs at least 10 points (?)
707  // NOTE: set as parameter later
708  if (pt_union_indices.size () > 10)
709  createSurfaceForCell (index, pt_union_indices);
710  }
711 
712  polygons.resize (surface_.size () / 4);
713  // Copy the data from surface_ to polygons
714  for (int i = 0; i < static_cast<int> (polygons.size ()); ++i)
715  {
716  pcl::Vertices v;
717  v.vertices.resize (4);
718  for (int j = 0; j < 4; ++j)
719  v.vertices[j] = i*4+j;
720  polygons[i] = v;
721  }
722  return (true);
723 }
724 
725 //////////////////////////////////////////////////////////////////////////////////////////////
726 template <typename PointNT> void
728 {
729  if (!reconstructPolygons (output.polygons))
730  return;
731 
732  // The mesh surface is held in surface_. Copy it to the output format
733  output.header = input_->header;
734 
736  cloud.width = static_cast<uint32_t> (surface_.size ());
737  cloud.height = 1;
738  cloud.is_dense = true;
739 
740  cloud.points.resize (surface_.size ());
741  // Copy the data from surface_ to cloud
742  for (size_t i = 0; i < cloud.points.size (); ++i)
743  {
744  cloud.points[i].x = surface_[i].x ();
745  cloud.points[i].y = surface_[i].y ();
746  cloud.points[i].z = surface_[i].z ();
747  }
748  pcl::toPCLPointCloud2 (cloud, output.cloud);
749 }
750 
751 //////////////////////////////////////////////////////////////////////////////////////////////
752 template <typename PointNT> void
754  std::vector<pcl::Vertices> &polygons)
755 {
756  if (!reconstructPolygons (polygons))
757  return;
758 
759  // The mesh surface is held in surface_. Copy it to the output format
760  points.header = input_->header;
761  points.width = static_cast<uint32_t> (surface_.size ());
762  points.height = 1;
763  points.is_dense = true;
764 
765  points.resize (surface_.size ());
766  // Copy the data from surface_ to cloud
767  for (size_t i = 0; i < points.size (); ++i)
768  {
769  points[i].x = surface_[i].x ();
770  points[i].y = surface_[i].y ();
771  points[i].z = surface_[i].z ();
772  }
773 }
774 
775 #define PCL_INSTANTIATE_GridProjection(T) template class PCL_EXPORTS pcl::GridProjection<T>;
776 
777 #endif // PCL_SURFACE_IMPL_GRID_PROJECTION_H_
778 
~GridProjection()
Destructor.
void getCellIndex(const Eigen::Vector4f &p, Eigen::Vector3i &index) const
Get the 3d index (x,y,z) of the cell based on the location of the cell.
size_t size() const
Definition: point_cloud.h:447
bool isIntersected(const std::vector< Eigen::Vector4f, Eigen::aligned_allocator< Eigen::Vector4f > > &end_pts, std::vector< Eigen::Vector3f, Eigen::aligned_allocator< Eigen::Vector3f > > &vect_at_end_pts, std::vector< int > &pt_union_indices)
Test whether the edge is intersected by the surface by doing the dot product of the vector at two end...
double getD1AtPoint(const Eigen::Vector4f &p, const Eigen::Vector3f &vec, const std::vector< int > &pt_union_indices)
Get the 1st derivative.
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:409
struct pcl::PointXYZIEdge EIGEN_ALIGN16
void getProjection(const Eigen::Vector4f &p, std::vector< int > &pt_union_indices, Eigen::Vector4f &projection)
Given the coordinates of one point, project it onto the surface, return the projected point...
unsigned int computeMeanAndCovarianceMatrix(const pcl::PointCloud< PointT > &cloud, Eigen::Matrix< Scalar, 3, 3 > &covariance_matrix, Eigen::Matrix< Scalar, 4, 1 > &centroid)
Compute the normalized 3x3 covariance matrix and the centroid of a given set of points in a single lo...
Definition: centroid.hpp:489
std::vector< uint32_t > vertices
Definition: Vertices.h:19
void scaleInputDataPoint(double scale_factor)
When the input data points don&#39;t fill into the 1*1*1 box, scale them so that they can be filled in th...
const Eigen::Matrix< real, dimension, 1 > & getMean() const
Get the mean of the added vectors.
const int I_SHIFT_PT[4]
void add(const Eigen::Matrix< real, dimension, 1 > &sample, real weight=1.0)
Add a new sample.
uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:414
Describes a set of vertices in a polygon mesh, by basically storing an array of indices.
Definition: Vertices.h:14
uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:412
void getIndexIn3D(int index_1d, Eigen::Vector3i &index_3d) const
Given an index in 1d, translate it into the index (x, y, z) in 3d.
::pcl::PCLHeader header
Definition: PolygonMesh.h:20
Eigen::Vector4f pt_on_surface
void storeVectAndSurfacePointKNN(int index_1d, const Eigen::Vector3i &index_3d, const Leaf &cell_data)
Go through all the entries in the hash table and update the cellData.
std::vector< int > data_indices
void getBoundingBox()
Get the bounding box for the input data points, also calculating the cell size, and the gaussian scal...
void getMinMax3D(const pcl::PointCloud< PointT > &cloud, PointT &min_pt, PointT &max_pt)
Get the minimum and maximum values on each of the 3 (x-y-z) dimensions in a given pointcloud...
Definition: common.hpp:242
void createSurfaceForCell(const Eigen::Vector3i &index, std::vector< int > &pt_union_indices)
Given the index of a cell, exam it&#39;s up, left, front edges, and add the vectices to m_surface list...
void fillPad(const Eigen::Vector3i &index)
For a given 3d index of a cell, test whether the cells within its padding area exist in the hash tabl...
void getCellCenterFromIndex(const Eigen::Vector3i &index, Eigen::Vector4f &center) const
Given the 3d index (x, y, z) of the cell, get the coordinates of the cell center. ...
std::vector< ::pcl::Vertices > polygons
Definition: PolygonMesh.h:24
pcl::PCLHeader header
The point cloud header.
Definition: point_cloud.h:406
void eigen33(const Matrix &mat, typename Matrix::Scalar &eigenvalue, Vector &eigenvector)
determines the eigenvector and eigenvalue of the smallest eigenvalue of the symmetric positive semi d...
Definition: eigen.hpp:251
void getEigenVector1(Eigen::Matrix< real, dimension, 1 > &eigen_vector1) const
Get the eigenvector corresponding to the smallest eigenvalue.
void getDataPtsUnion(const Eigen::Vector3i &index, std::vector< int > &pt_union_indices)
Obtain the index of a cell and the pad size.
double getMagAtPoint(const Eigen::Vector4f &p, const std::vector< int > &pt_union_indices)
Get the magnitude of the vector by summing up the distance.
void storeVectAndSurfacePoint(int index_1d, const Eigen::Vector3i &index_3d, std::vector< int > &pt_union_indices, const Leaf &cell_data)
Go through all the entries in the hash table and update the cellData.
::pcl::PCLPointCloud2 cloud
Definition: PolygonMesh.h:22
void toPCLPointCloud2(const pcl::PointCloud< PointT > &cloud, pcl::PCLPointCloud2 &msg)
Convert a pcl::PointCloud<T> object to a PCLPointCloud2 binary data blob.
Definition: conversions.h:241
void performReconstruction(pcl::PolygonMesh &output) override
Create the surface.
double getD2AtPoint(const Eigen::Vector4f &p, const Eigen::Vector3f &vec, const std::vector< int > &pt_union_indices)
Get the 2nd derivative.
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields)...
Definition: point_cloud.h:417
void getVectorAtPointKNN(const Eigen::Vector4f &p, std::vector< int > &k_indices, std::vector< float > &k_squared_distances, Eigen::Vector3f &vo)
Given the location of a point, get it&#39;s vector.
PointCloudConstPtr input_
The input point cloud dataset.
Definition: pcl_base.h:150
void resize(size_t n)
Resize the cloud.
Definition: point_cloud.h:454
void findIntersection(int level, const std::vector< Eigen::Vector4f, Eigen::aligned_allocator< Eigen::Vector4f > > &end_pts, const std::vector< Eigen::Vector3f, Eigen::aligned_allocator< Eigen::Vector3f > > &vect_at_end_pts, const Eigen::Vector4f &start_pt, std::vector< int > &pt_union_indices, Eigen::Vector4f &intersection)
Find point where the edge intersects the surface.
void getVertexFromCellCenter(const Eigen::Vector4f &cell_center, std::vector< Eigen::Vector4f, Eigen::aligned_allocator< Eigen::Vector4f > > &pts) const
Given cell center, caluate the coordinates of the eight vertices of the cell.
void getProjectionWithPlaneFit(const Eigen::Vector4f &p, std::vector< int > &pt_union_indices, Eigen::Vector4f &projection)
Given the coordinates of one point, project it onto the surface, return the projected point...
int getIndexIn1D(const Eigen::Vector3i &index) const
Given an index (x, y, z) in 3d, translate it into the index in 1d.
const int I_SHIFT_EDGE[3][2]
bool reconstructPolygons(std::vector< pcl::Vertices > &polygons)
The actual surface reconstruction method.
Calculates the weighted average and the covariance matrix.
KdTreePtr tree_
A pointer to the spatial search object.
void getVectorAtPoint(const Eigen::Vector4f &p, std::vector< int > &pt_union_indices, Eigen::Vector3f &vo)
Given the location of a point, get it&#39;s vector.
GridProjection()
Constructor.