Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
marching_cubes_rbf.hpp
Go to the documentation of this file.
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  */
35 
36 #ifndef PCL_SURFACE_IMPL_MARCHING_CUBES_RBF_H_
37 #define PCL_SURFACE_IMPL_MARCHING_CUBES_RBF_H_
38 
40 #include <pcl/common/common.h>
42 #include <pcl/Vertices.h>
44 
46 template <typename PointNT>
48  : MarchingCubes<PointNT> (),
49  off_surface_epsilon_ (0.1f)
50 {
51 }
52 
54 template <typename PointNT>
56 {
57 }
58 
59 
61 template <typename PointNT> void
63 {
64  // Initialize data structures
65  unsigned int N = static_cast<unsigned int> (input_->size ());
66  Eigen::MatrixXd M (2*N, 2*N),
67  d (2*N, 1);
68 
69 
70  for (unsigned int row_i = 0; row_i < 2*N; ++row_i)
71  {
72  // boolean variable to determine whether we are in the off_surface domain for the rows
73  bool row_off = (row_i >= N) ? 1 : 0;
74  for (unsigned int col_i = 0; col_i < 2*N; ++col_i)
75  {
76  // boolean variable to determine whether we are in the off_surface domain for the columns
77  bool col_off = (col_i >= N) ? 1 : 0;
78  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_,
79  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_);
80  }
81 
82  d (row_i, 0) = row_off * off_surface_epsilon_;
83  }
84 
85  // Solve for the weights
86  Eigen::MatrixXd w (2*N, 1);
87 
88  // Solve_linear_system (M, d, w);
89  w = M.fullPivLu ().solve (d);
90 
91  std::vector<double> weights (2*N);
92  std::vector<Eigen::Vector3d> centers (2*N);
93  for (unsigned int i = 0; i < N; ++i)
94  {
95  centers[i] = Eigen::Vector3f (input_->points[i].getVector3fMap ()).cast<double> ();
96  centers[i + N] = Eigen::Vector3f (input_->points[i].getVector3fMap ()).cast<double> () + Eigen::Vector3f (input_->points[i].getNormalVector3fMap ()).cast<double> () * off_surface_epsilon_;
97  weights[i] = w (i, 0);
98  weights[i + N] = w (i + N, 0);
99  }
100 
101 
102 
103  for (int x = 0; x < res_x_; ++x)
104  for (int y = 0; y < res_y_; ++y)
105  for (int z = 0; z < res_z_; ++z)
106  {
107  Eigen::Vector3d point;
108  point[0] = min_p_[0] + (max_p_[0] - min_p_[0]) * x / res_x_;
109  point[1] = min_p_[1] + (max_p_[1] - min_p_[1]) * y / res_y_;
110  point[2] = min_p_[2] + (max_p_[2] - min_p_[2]) * z / res_z_;
111 
112  double f = 0.0f;
113  std::vector<double>::const_iterator w_it (weights.begin());
114  for (std::vector<Eigen::Vector3d>::const_iterator c_it = centers.begin ();
115  c_it != centers.end (); ++c_it, ++w_it)
116  f += *w_it * kernel (*c_it, point);
117 
118  grid_[x * res_y_*res_z_ + y * res_z_ + z] = f;
119  }
120 }
121 
123 template <typename PointNT> double
124 pcl::MarchingCubesRBF<PointNT>::kernel (Eigen::Vector3d c, Eigen::Vector3d x)
125 {
126  double r = (x - c).norm();
127  return r*r*r;
128 }
129 
130 
131 
132 #define PCL_INSTANTIATE_MarchingCubesRBF(T) template class PCL_EXPORTS pcl::MarchingCubesRBF<T>;
133 
134 #endif // PCL_SURFACE_IMPL_MARCHING_CUBES_HOPPE_H_
135