Point Cloud Library (PCL)  1.9.1-dev
marching_cubes_rbf.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010, Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  */
38 
39 #ifndef PCL_SURFACE_IMPL_MARCHING_CUBES_RBF_H_
40 #define PCL_SURFACE_IMPL_MARCHING_CUBES_RBF_H_
41 
42 #include <pcl/surface/marching_cubes_rbf.h>
43 #include <pcl/common/common.h>
44 #include <pcl/common/vector_average.h>
45 #include <pcl/Vertices.h>
46 
47 //////////////////////////////////////////////////////////////////////////////////////////////
48 template <typename PointNT>
50 {
51 }
52 
53 //////////////////////////////////////////////////////////////////////////////////////////////
54 template <typename PointNT> void
56 {
57  // Initialize data structures
58  const unsigned int N = static_cast<unsigned int> (input_->size ());
59  Eigen::MatrixXd M (2*N, 2*N),
60  d (2*N, 1);
61 
62  for (unsigned int row_i = 0; row_i < 2*N; ++row_i)
63  {
64  // boolean variable to determine whether we are in the off_surface domain for the rows
65  bool row_off = (row_i >= N);
66  for (unsigned int col_i = 0; col_i < 2*N; ++col_i)
67  {
68  // boolean variable to determine whether we are in the off_surface domain for the columns
69  bool col_off = (col_i >= N);
70  M (row_i, col_i) = kernel (Eigen::Vector3f (input_->points[col_i%N].getVector3fMap ()).cast<double> () + Eigen::Vector3f (input_->points[col_i%N].getNormalVector3fMap ()).cast<double> () * col_off * off_surface_epsilon_,
71  Eigen::Vector3f (input_->points[row_i%N].getVector3fMap ()).cast<double> () + Eigen::Vector3f (input_->points[row_i%N].getNormalVector3fMap ()).cast<double> () * row_off * off_surface_epsilon_);
72  }
73 
74  d (row_i, 0) = row_off * off_surface_epsilon_;
75  }
76 
77  // Solve for the weights
78  Eigen::MatrixXd w (2*N, 1);
79 
80  // Solve_linear_system (M, d, w);
81  w = M.fullPivLu ().solve (d);
82 
83  std::vector<double> weights (2*N);
84  std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> > centers (2*N);
85  for (unsigned int i = 0; i < N; ++i)
86  {
87  centers[i] = Eigen::Vector3f (input_->points[i].getVector3fMap ()).cast<double> ();
88  centers[i + N] = Eigen::Vector3f (input_->points[i].getVector3fMap ()).cast<double> () + Eigen::Vector3f (input_->points[i].getNormalVector3fMap ()).cast<double> () * off_surface_epsilon_;
89  weights[i] = w (i, 0);
90  weights[i + N] = w (i + N, 0);
91  }
92 
93  for (int x = 0; x < res_x_; ++x)
94  for (int y = 0; y < res_y_; ++y)
95  for (int z = 0; z < res_z_; ++z)
96  {
97  const Eigen::Vector3f point_f = (size_voxel_ * Eigen::Array3f (x, y, z)
98  + lower_boundary_).matrix ();
99  const Eigen::Vector3d point = point_f.cast<double> ();
100 
101  double f = 0.0;
102  std::vector<double>::const_iterator w_it (weights.begin());
103  for (std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d> >::const_iterator c_it = centers.begin ();
104  c_it != centers.end (); ++c_it, ++w_it)
105  f += *w_it * kernel (*c_it, point);
106 
107  grid_[x * res_y_*res_z_ + y * res_z_ + z] = float (f);
108  }
109 }
110 
111 //////////////////////////////////////////////////////////////////////////////////////////////
112 template <typename PointNT> double
113 pcl::MarchingCubesRBF<PointNT>::kernel (Eigen::Vector3d c, Eigen::Vector3d x)
114 {
115  double r = (x - c).norm ();
116  return (r * r * r);
117 }
118 
119 #define PCL_INSTANTIATE_MarchingCubesRBF(T) template class PCL_EXPORTS pcl::MarchingCubesRBF<T>;
120 
121 #endif // PCL_SURFACE_IMPL_MARCHING_CUBES_HOPPE_H_
122 
double kernel(Eigen::Vector3d c, Eigen::Vector3d x)
the Radial Basis Function kernel.
Define standard C methods and C++ classes that are common to all methods.
void voxelizeData() override
Convert the point cloud into voxel data.