Point Cloud Library (PCL)  1.12.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 #include <string>
59 
60 using namespace pcl;
61 
62 //////////////////////////////////////////////////////////////////////////////////////////////
63 template <typename PointNT>
65  : depth_ (8)
66  , min_depth_ (5)
67  , point_weight_ (4)
68  , scale_ (1.1f)
69  , solver_divide_ (8)
70  , iso_divide_ (8)
71  , samples_per_node_ (1.0)
72  , confidence_ (false)
73  , output_polygons_ (false)
74  , no_reset_samples_ (false)
75  , no_clip_tree_ (false)
76  , manifold_ (true)
77  , refine_ (3)
78  , kernel_depth_ (8)
79  , degree_ (2)
80  , non_adaptive_weights_ (false)
81  , show_residual_ (false)
82  , min_iterations_ (8)
83  , solver_accuracy_ (1e-3f)
84  , threads_(1)
85 {
86 }
87 
88 //////////////////////////////////////////////////////////////////////////////////////////////
89 template <typename PointNT>
91 
92 //////////////////////////////////////////////////////////////////////////////////////////////
93 template <typename PointNT> void
95 {
96  if (threads == 0)
97 #ifdef _OPENMP
98  threads_ = omp_get_num_procs();
99 #else
100  threads_ = 1;
101 #endif
102  else
103  threads_ = threads;
104 }
105 
106 //////////////////////////////////////////////////////////////////////////////////////////////
107 template <typename PointNT> template <int Degree> void
109  poisson::Point3D<float> &center,
110  float &scale)
111 {
112  pcl::poisson::Real iso_value = 0;
115 
116 
117  tree.threads = threads_;
118  center.coords[0] = center.coords[1] = center.coords[2] = 0;
119 
120 
121  if (solver_divide_ < min_depth_)
122  {
123  PCL_WARN ("[pcl::Poisson] solver_divide_ must be at least as large as min_depth_: %d >= %d\n", solver_divide_, min_depth_);
124  solver_divide_ = min_depth_;
125  }
126  if (iso_divide_< min_depth_)
127  {
128  PCL_WARN ("[pcl::Poisson] iso_divide_ must be at least as large as min_depth_: %d >= %d\n", iso_divide_, min_depth_);
129  iso_divide_ = min_depth_;
130  }
131 
132  pcl::poisson::TreeOctNode::SetAllocator (MEMORY_ALLOCATOR_BLOCK_SIZE);
133 
134  kernel_depth_ = depth_ - 2;
135 
136  tree.setBSplineData (depth_, pcl::poisson::Real (1.0 / (1 << depth_)), true);
137 
138  tree.maxMemoryUsage = 0;
139 
140 
141  int point_count = tree.template setTree<PointNT> (input_, depth_, min_depth_, kernel_depth_, samples_per_node_,
142  scale_, center, scale, confidence_, point_weight_, !non_adaptive_weights_);
143 
144  tree.ClipTree ();
145  tree.finalize ();
146  tree.RefineBoundary (iso_divide_);
147 
148  PCL_DEBUG ("Input Points: %d\n" , point_count );
149  PCL_DEBUG ("Leaves/Nodes: %d/%d\n" , tree.tree.leaves() , tree.tree.nodes() );
150 
151  tree.maxMemoryUsage = 0;
152  tree.SetLaplacianConstraints ();
153 
154  tree.maxMemoryUsage = 0;
155  tree.LaplacianMatrixIteration (solver_divide_, show_residual_, min_iterations_, solver_accuracy_);
156 
157  iso_value = tree.GetIsoValue ();
158 
159  tree.GetMCIsoTriangles (iso_value, iso_divide_, &mesh, 0, 1, manifold_, output_polygons_);
160 }
161 
162 
163 //////////////////////////////////////////////////////////////////////////////////////////////
164 template <typename PointNT> void
166 {
169  float scale = 1.0f;
170 
171  switch (degree_)
172  {
173  case 1:
174  {
175  execute<1> (mesh, center, scale);
176  break;
177  }
178  case 2:
179  {
180  execute<2> (mesh, center, scale);
181  break;
182  }
183  case 3:
184  {
185  execute<3> (mesh, center, scale);
186  break;
187  }
188  case 4:
189  {
190  execute<4> (mesh, center, scale);
191  break;
192  }
193  case 5:
194  {
195  execute<5> (mesh, center, scale);
196  break;
197  }
198  default:
199  {
200  PCL_ERROR (stderr, "Degree %d not supported\n", degree_);
201  }
202  }
203 
204  // Write output PolygonMesh
206  cloud.resize (int (mesh.outOfCorePointCount () + mesh.inCorePoints.size ()));
208  for (int i = 0; i < int (mesh.inCorePoints.size ()); i++)
209  {
210  p = mesh.inCorePoints[i];
211  cloud[i].x = p.coords[0]*scale+center.coords[0];
212  cloud[i].y = p.coords[1]*scale+center.coords[1];
213  cloud[i].z = p.coords[2]*scale+center.coords[2];
214  }
215  for (int i = int (mesh.inCorePoints.size ()); i < int (mesh.outOfCorePointCount () + mesh.inCorePoints.size ()); i++)
216  {
217  mesh.nextOutOfCorePoint (p);
218  cloud[i].x = p.coords[0]*scale+center.coords[0];
219  cloud[i].y = p.coords[1]*scale+center.coords[1];
220  cloud[i].z = p.coords[2]*scale+center.coords[2];
221  }
222  pcl::toPCLPointCloud2 (cloud, output.cloud);
223  output.polygons.resize (mesh.polygonCount ());
224 
225  // Write faces
226  std::vector<poisson::CoredVertexIndex> polygon;
227  for (int p_i = 0; p_i < mesh.polygonCount (); p_i++)
228  {
229  pcl::Vertices v;
230  mesh.nextPolygon (polygon);
231  v.vertices.resize (polygon.size ());
232 
233  for (int i = 0; i < static_cast<int> (polygon.size ()); ++i)
234  if (polygon[i].inCore )
235  v.vertices[i] = polygon[i].idx;
236  else
237  v.vertices[i] = polygon[i].idx + int (mesh.inCorePoints.size ());
238 
239  output.polygons[p_i] = v;
240  }
241 }
242 
243 //////////////////////////////////////////////////////////////////////////////////////////////
244 template <typename PointNT> void
246  std::vector<pcl::Vertices> &polygons)
247 {
250  float scale = 1.0f;
251 
252  switch (degree_)
253  {
254  case 1:
255  {
256  execute<1> (mesh, center, scale);
257  break;
258  }
259  case 2:
260  {
261  execute<2> (mesh, center, scale);
262  break;
263  }
264  case 3:
265  {
266  execute<3> (mesh, center, scale);
267  break;
268  }
269  case 4:
270  {
271  execute<4> (mesh, center, scale);
272  break;
273  }
274  case 5:
275  {
276  execute<5> (mesh, center, scale);
277  break;
278  }
279  default:
280  {
281  PCL_ERROR (stderr, "Degree %d not supported\n", degree_);
282  }
283  }
284 
285  // Write output PolygonMesh
286  // Write vertices
287  points.resize (int (mesh.outOfCorePointCount () + mesh.inCorePoints.size ()));
289  for (int i = 0; i < int(mesh.inCorePoints.size ()); i++)
290  {
291  p = mesh.inCorePoints[i];
292  points[i].x = p.coords[0]*scale+center.coords[0];
293  points[i].y = p.coords[1]*scale+center.coords[1];
294  points[i].z = p.coords[2]*scale+center.coords[2];
295  }
296  for (int i = int(mesh.inCorePoints.size()); i < int (mesh.outOfCorePointCount() + mesh.inCorePoints.size ()); i++)
297  {
298  mesh.nextOutOfCorePoint (p);
299  points[i].x = p.coords[0]*scale+center.coords[0];
300  points[i].y = p.coords[1]*scale+center.coords[1];
301  points[i].z = p.coords[2]*scale+center.coords[2];
302  }
303 
304  polygons.resize (mesh.polygonCount ());
305 
306  // Write faces
307  std::vector<poisson::CoredVertexIndex> polygon;
308  for (int p_i = 0; p_i < mesh.polygonCount (); p_i++)
309  {
310  pcl::Vertices v;
311  mesh.nextPolygon (polygon);
312  v.vertices.resize (polygon.size ());
313 
314  for (int i = 0; i < static_cast<int> (polygon.size ()); ++i)
315  if (polygon[i].inCore )
316  v.vertices[i] = polygon[i].idx;
317  else
318  v.vertices[i] = polygon[i].idx + int (mesh.inCorePoints.size ());
319 
320  polygons[p_i] = v;
321  }
322 }
323 
324 
325 #define PCL_INSTANTIATE_Poisson(T) template class PCL_EXPORTS pcl::Poisson<T>;
326 
327 #endif // PCL_SURFACE_IMPL_POISSON_H_
328 
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:94
Poisson()
Constructor that sets all the parameters to working default values.
Definition: poisson.hpp:64
void performReconstruction(pcl::PolygonMesh &output) override
Create the surface.
Definition: poisson.hpp:165
~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:236
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