Point Cloud Library (PCL)  1.13.1-dev
poisson.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id$
37  *
38  */
39 
40 #ifndef PCL_SURFACE_IMPL_POISSON_H_
41 #define PCL_SURFACE_IMPL_POISSON_H_
42 
43 #include <pcl/surface/poisson.h>
44 #include <pcl/common/common.h>
45 #include <pcl/common/vector_average.h>
46 #include <pcl/Vertices.h>
47 
48 #include <pcl/surface/3rdparty/poisson4/octree_poisson.h>
49 #include <pcl/surface/3rdparty/poisson4/sparse_matrix.h>
50 #include <pcl/surface/3rdparty/poisson4/function_data.h>
51 #include <pcl/surface/3rdparty/poisson4/ppolynomial.h>
52 #include <pcl/surface/3rdparty/poisson4/multi_grid_octree_data.h>
53 #include <pcl/surface/3rdparty/poisson4/geometry.h>
54 
55 #define MEMORY_ALLOCATOR_BLOCK_SIZE 1<<12
56 
57 #include <cstdarg>
58 
59 using namespace pcl;
60 
61 //////////////////////////////////////////////////////////////////////////////////////////////
62 template <typename PointNT>
64  : depth_ (8)
65  , min_depth_ (5)
66  , point_weight_ (4)
67  , scale_ (1.1f)
68  , solver_divide_ (8)
69  , iso_divide_ (8)
70  , samples_per_node_ (1.0)
71  , confidence_ (false)
72  , output_polygons_ (false)
73  , no_reset_samples_ (false)
74  , no_clip_tree_ (false)
75  , manifold_ (true)
76  , refine_ (3)
77  , kernel_depth_ (8)
78  , degree_ (2)
79  , non_adaptive_weights_ (false)
80  , show_residual_ (false)
81  , min_iterations_ (8)
82  , solver_accuracy_ (1e-3f)
83  , threads_(1)
84 {
85 }
86 
87 //////////////////////////////////////////////////////////////////////////////////////////////
88 template <typename PointNT>
90 
91 //////////////////////////////////////////////////////////////////////////////////////////////
92 template <typename PointNT> void
94 {
95  if (threads == 0)
96 #ifdef _OPENMP
97  threads_ = omp_get_num_procs();
98 #else
99  threads_ = 1;
100 #endif
101  else
102  threads_ = threads;
103 }
104 
105 //////////////////////////////////////////////////////////////////////////////////////////////
106 template <typename PointNT> template <int Degree> void
108  poisson::Point3D<float> &center,
109  float &scale)
110 {
111  pcl::poisson::Real iso_value = 0;
114 
115 
116  tree.threads = threads_;
117  center.coords[0] = center.coords[1] = center.coords[2] = 0;
118 
119 
120  if (solver_divide_ < min_depth_)
121  {
122  PCL_WARN ("[pcl::Poisson] solver_divide_ must be at least as large as min_depth_: %d >= %d\n", solver_divide_, min_depth_);
123  solver_divide_ = min_depth_;
124  }
125  if (iso_divide_< min_depth_)
126  {
127  PCL_WARN ("[pcl::Poisson] iso_divide_ must be at least as large as min_depth_: %d >= %d\n", iso_divide_, min_depth_);
128  iso_divide_ = min_depth_;
129  }
130 
131  pcl::poisson::TreeOctNode::SetAllocator (MEMORY_ALLOCATOR_BLOCK_SIZE);
132 
133  kernel_depth_ = depth_ - 2;
134 
135  tree.setBSplineData (depth_, static_cast<pcl::poisson::Real>(1.0 / (1 << depth_)), true);
136 
137  tree.maxMemoryUsage = 0;
138 
139 
140  int point_count = tree.template setTree<PointNT> (input_, depth_, min_depth_, kernel_depth_, samples_per_node_,
141  scale_, center, scale, confidence_, point_weight_, !non_adaptive_weights_);
142 
143  tree.ClipTree ();
144  tree.finalize ();
145  tree.RefineBoundary (iso_divide_);
146 
147  PCL_DEBUG ("Input Points: %d\n" , point_count );
148  PCL_DEBUG ("Leaves/Nodes: %d/%d\n" , tree.tree.leaves() , tree.tree.nodes() );
149 
150  tree.maxMemoryUsage = 0;
151  tree.SetLaplacianConstraints ();
152 
153  tree.maxMemoryUsage = 0;
154  tree.LaplacianMatrixIteration (solver_divide_, show_residual_, min_iterations_, solver_accuracy_);
155 
156  iso_value = tree.GetIsoValue ();
157 
158  tree.GetMCIsoTriangles (iso_value, iso_divide_, &mesh, 0, 1, manifold_, output_polygons_);
159 }
160 
161 
162 //////////////////////////////////////////////////////////////////////////////////////////////
163 template <typename PointNT> void
165 {
168  float scale = 1.0f;
169 
170  switch (degree_)
171  {
172  case 1:
173  {
174  execute<1> (mesh, center, scale);
175  break;
176  }
177  case 2:
178  {
179  execute<2> (mesh, center, scale);
180  break;
181  }
182  case 3:
183  {
184  execute<3> (mesh, center, scale);
185  break;
186  }
187  case 4:
188  {
189  execute<4> (mesh, center, scale);
190  break;
191  }
192  case 5:
193  {
194  execute<5> (mesh, center, scale);
195  break;
196  }
197  default:
198  {
199  PCL_ERROR (stderr, "Degree %d not supported\n", degree_);
200  }
201  }
202 
203  // Write output PolygonMesh
205  cloud.resize (static_cast<int>(mesh.outOfCorePointCount () + mesh.inCorePoints.size ()));
207  for (int i = 0; i < static_cast<int>(mesh.inCorePoints.size ()); i++)
208  {
209  p = mesh.inCorePoints[i];
210  cloud[i].x = p.coords[0]*scale+center.coords[0];
211  cloud[i].y = p.coords[1]*scale+center.coords[1];
212  cloud[i].z = p.coords[2]*scale+center.coords[2];
213  }
214  for (int i = static_cast<int>(mesh.inCorePoints.size ()); i < static_cast<int>(mesh.outOfCorePointCount () + mesh.inCorePoints.size ()); i++)
215  {
216  mesh.nextOutOfCorePoint (p);
217  cloud[i].x = p.coords[0]*scale+center.coords[0];
218  cloud[i].y = p.coords[1]*scale+center.coords[1];
219  cloud[i].z = p.coords[2]*scale+center.coords[2];
220  }
221  pcl::toPCLPointCloud2 (cloud, output.cloud);
222  output.polygons.resize (mesh.polygonCount ());
223 
224  // Write faces
225  std::vector<poisson::CoredVertexIndex> polygon;
226  for (int p_i = 0; p_i < mesh.polygonCount (); p_i++)
227  {
228  pcl::Vertices v;
229  mesh.nextPolygon (polygon);
230  v.vertices.resize (polygon.size ());
231 
232  for (int i = 0; i < static_cast<int> (polygon.size ()); ++i)
233  if (polygon[i].inCore )
234  v.vertices[i] = polygon[i].idx;
235  else
236  v.vertices[i] = polygon[i].idx + static_cast<int>(mesh.inCorePoints.size ());
237 
238  output.polygons[p_i] = v;
239  }
240 }
241 
242 //////////////////////////////////////////////////////////////////////////////////////////////
243 template <typename PointNT> void
245  std::vector<pcl::Vertices> &polygons)
246 {
249  float scale = 1.0f;
250 
251  switch (degree_)
252  {
253  case 1:
254  {
255  execute<1> (mesh, center, scale);
256  break;
257  }
258  case 2:
259  {
260  execute<2> (mesh, center, scale);
261  break;
262  }
263  case 3:
264  {
265  execute<3> (mesh, center, scale);
266  break;
267  }
268  case 4:
269  {
270  execute<4> (mesh, center, scale);
271  break;
272  }
273  case 5:
274  {
275  execute<5> (mesh, center, scale);
276  break;
277  }
278  default:
279  {
280  PCL_ERROR (stderr, "Degree %d not supported\n", degree_);
281  }
282  }
283 
284  // Write output PolygonMesh
285  // Write vertices
286  points.resize (static_cast<int>(mesh.outOfCorePointCount () + mesh.inCorePoints.size ()));
288  for (int i = 0; i < static_cast<int>(mesh.inCorePoints.size ()); i++)
289  {
290  p = mesh.inCorePoints[i];
291  points[i].x = p.coords[0]*scale+center.coords[0];
292  points[i].y = p.coords[1]*scale+center.coords[1];
293  points[i].z = p.coords[2]*scale+center.coords[2];
294  }
295  for (int i = static_cast<int>(mesh.inCorePoints.size()); i < static_cast<int>(mesh.outOfCorePointCount() + mesh.inCorePoints.size ()); i++)
296  {
297  mesh.nextOutOfCorePoint (p);
298  points[i].x = p.coords[0]*scale+center.coords[0];
299  points[i].y = p.coords[1]*scale+center.coords[1];
300  points[i].z = p.coords[2]*scale+center.coords[2];
301  }
302 
303  polygons.resize (mesh.polygonCount ());
304 
305  // Write faces
306  std::vector<poisson::CoredVertexIndex> polygon;
307  for (int p_i = 0; p_i < mesh.polygonCount (); p_i++)
308  {
309  pcl::Vertices v;
310  mesh.nextPolygon (polygon);
311  v.vertices.resize (polygon.size ());
312 
313  for (int i = 0; i < static_cast<int> (polygon.size ()); ++i)
314  if (polygon[i].inCore )
315  v.vertices[i] = polygon[i].idx;
316  else
317  v.vertices[i] = polygon[i].idx + static_cast<int>(mesh.inCorePoints.size ());
318 
319  polygons[p_i] = v;
320  }
321 }
322 
323 
324 #define PCL_INSTANTIATE_Poisson(T) template class PCL_EXPORTS pcl::Poisson<T>;
325 
326 #endif // PCL_SURFACE_IMPL_POISSON_H_
327 
void resize(std::size_t count)
Resizes the container to contain count elements.
Definition: point_cloud.h:462
The Poisson surface reconstruction algorithm.
Definition: poisson.h:62
void setThreads(int threads)
Set the number of threads to use.
Definition: poisson.hpp:93
Poisson()
Constructor that sets all the parameters to working default values.
Definition: poisson.hpp:63
void performReconstruction(pcl::PolygonMesh &output) override
Create the surface.
Definition: poisson.hpp:164
~Poisson() override
Destructor.
std::vector< Point3D< float > > inCorePoints
Definition: geometry.h:202
int nextPolygon(std::vector< CoredVertexIndex > &vertices)
int nextOutOfCorePoint(Point3D< float > &p)
int leaves(void) const
void RefineBoundary(int subdivisionDepth)
void setBSplineData(int maxDepth, Real normalSmooth=-1, bool reflectBoundary=false)
int LaplacianMatrixIteration(int subdivideDepth, bool showResidual, int minIters, double accuracy)
Define standard C methods and C++ classes that are common to all methods.
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:264
std::vector< ::pcl::Vertices > polygons
Definition: PolygonMesh.h:22
::pcl::PCLPointCloud2 cloud
Definition: PolygonMesh.h:20
Describes a set of vertices in a polygon mesh, by basically storing an array of indices.
Definition: Vertices.h:15
Indices vertices
Definition: Vertices.h:18