Point Cloud Library (PCL)  1.9.1-dev
standalone_marching_cubes.h
1  /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2011, 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  */
37 
38 #pragma once
39 
40 //General includes and I/O
41 
42 #include <iostream>
43 #include <pcl/io/pcd_io.h>
44 #include <pcl/io/ply_io.h>
45 #include <pcl/io/vtk_io.h>
46 #include <pcl/point_types.h>
47 #include <cstdio>
48 #include <cstdarg>
49 #include <pcl/pcl_macros.h>
50 
51 //Marching cubes includes
52 #include <pcl/gpu/kinfu_large_scale/marching_cubes.h>
53 #include <pcl/PolygonMesh.h>
54 
55 #include <pcl/gpu/containers/device_array.h>
56 
57 //TSDF volume includes
58 #include <pcl/gpu/kinfu_large_scale/tsdf_volume.h>
59 
60 //Eigen Geometry and Transforms
61 #include <pcl/common/transforms.h>
62 #include <Eigen/Geometry>
63 //#include <boost/graph/buffer_concepts.hpp>
64 
65 namespace pcl
66 {
67  namespace gpu
68  {
69  namespace kinfuLS
70  {
71  /** \brief The Standalone Marching Cubes Class provides encapsulated functionality for the Marching Cubes implementation originally by Anatoly Baksheev.
72  * \author Raphael Favier
73  * \author Francisco Heredia
74  */
75 
76  template <typename PointT>
78  {
79  public:
81  using PointCloudPtr = typename PointCloud::Ptr;
82  using MeshPtr = boost::shared_ptr<pcl::PolygonMesh>;
83 
84  /** \brief Constructor
85  */
86  StandaloneMarchingCubes (int voxels_x = 512, int voxels_y = 512, int voxels_z = 512, float volume_size = 3.0f);
87 
88  /** \brief Destructor
89  */
91 
92  /** \brief Run marching cubes in a TSDF cloud and returns a PolygonMesh. Input X,Y,Z coordinates must be in indices of the TSDF volume grid, output is in meters.
93  * \param[in] cloud TSDF cloud with indices between [0 ... VOXELS_X][0 ... VOXELS_Y][0 ... VOXELS_Z]. Intensity value corresponds to the TSDF value in that coordinate.
94  * \return pointer to a PolygonMesh in meters generated by marching cubes.
95  */
96  MeshPtr
97  getMeshFromTSDFCloud (const PointCloud &cloud);
98 
99  /** \brief Runs marching cubes on every pointcloud in the vector. Returns a vector containing the PolygonMeshes.
100  * \param[in] tsdf_clouds Vector of TSDF Clouds
101  * \param[in] tsdf_offsets Vector of the offsets for every pointcloud in TsdfClouds. This offset (in indices) indicates the position of the cloud with respect to the absolute origin of the world model
102  */
103  void
104  getMeshesFromTSDFVector (const std::vector<PointCloudPtr> &tsdf_clouds, const std::vector<Eigen::Vector3f, Eigen::aligned_allocator<Eigen::Vector3f> > &tsdf_offsets);
105 
106  /** \brief Returns the associated Tsdf Volume buffer in GPU
107  * \return pointer to the Tsdf Volume buffer in GPU
108  */
110  tsdfVolumeGPU ();
111 
112  /** \brief Returns the associated Tsdf Volume buffer in CPU
113  * \return the Tsdf Volume buffer in CPU returned returned by reference
114  */
115  std::vector<int>&
116  tsdfVolumeCPU ();
117 
118  protected:
119 
120  /** \brief Loads a TSDF Cloud to the TSDF Volume in GPU
121  * \param[in] cloud TSDF cloud that will be loaded. X,Y,Z of the cloud will only be loaded if their range is between [0 ... VOXELS_X][0 ... VOXELS_Y][0 ... VOXELS_Z]
122  */
123  void
124  loadTsdfCloudToGPU (const PointCloud &cloud);
125 
126  /** \brief Read the data in the point cloud. Performs a conversion to a suitable format for the TSDF Volume. Loads the converted data to the output vector.
127  * \param[in] cloud point cloud to be converted
128  * \param[out] output the vector of converted values, ready to be loaded to the GPU.
129  */
130  void
131  convertTsdfVectors (const PointCloud &cloud, std::vector<int> &output);
132 
133  /** \brief Converts the triangles buffer device to a PolygonMesh.
134  * \param[in] triangles the triangles buffer containing the points of the mesh
135  * \return pointer to the PolygonMesh egnerated by marchign cubes
136  */
137  MeshPtr
139 
140  /** \brief Runs marching cubes on the data that is contained in the TSDF Volume in GPU.
141  * \return param[return] pointer to a PolygonMesh in meters generated by marching cubes.
142  */
143  MeshPtr
144  runMarchingCubes ();
145 
146  private:
147 
148  /** The TSDF volume in GPU*/
149  TsdfVolume::Ptr tsdf_volume_gpu_;
150 
151  /** The TSDF volume in CPU */
152  std::vector<int> tsdf_volume_cpu_;
153 
154  /** Number of voxels in the grid for each axis */
155  int voxels_x_;
156  int voxels_y_;
157  int voxels_z_;
158 
159  /** Tsdf volume size in meters. Should match the ones in internal.h */
160  float volume_size_;
161 
162  /** Mesh counter used to name the output meshes */
163  int mesh_counter_;
164 
165  };
166  }
167  }
168 }
169 
170 #define PCL_INSTANTIATE_StandaloneMarchingCubes(PointT) template class PCL_EXPORTS pcl::gpu::kinfuLS::StandaloneMarchingCubes<PointT>;
void convertTsdfVectors(const PointCloud &cloud, std::vector< int > &output)
Read the data in the point cloud.
boost::shared_ptr< TsdfVolume > Ptr
Definition: tsdf_volume.h:64
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
std::vector< int > & tsdfVolumeCPU()
Returns the associated Tsdf Volume buffer in CPU.
void getMeshesFromTSDFVector(const std::vector< PointCloudPtr > &tsdf_clouds, const std::vector< Eigen::Vector3f, Eigen::aligned_allocator< Eigen::Vector3f > > &tsdf_offsets)
Runs marching cubes on every pointcloud in the vector.
boost::shared_ptr< pcl::PolygonMesh > MeshPtr
The Standalone Marching Cubes Class provides encapsulated functionality for the Marching Cubes implem...
Defines all the PCL implemented PointT point type structures.
boost::shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:444
PointCloud represents the base class in PCL for storing collections of 3D points. ...
MeshPtr getMeshFromTSDFCloud(const PointCloud &cloud)
Run marching cubes in a TSDF cloud and returns a PolygonMesh.
MeshPtr convertTrianglesToMesh(const pcl::gpu::DeviceArray< pcl::PointXYZ > &triangles)
Converts the triangles buffer device to a PolygonMesh.
TsdfVolume::Ptr tsdfVolumeGPU()
Returns the associated Tsdf Volume buffer in GPU.
MeshPtr runMarchingCubes()
Runs marching cubes on the data that is contained in the TSDF Volume in GPU.
void loadTsdfCloudToGPU(const PointCloud &cloud)
Loads a TSDF Cloud to the TSDF Volume in GPU.
Defines all the PCL and non-PCL macros used.
StandaloneMarchingCubes(int voxels_x=512, int voxels_y=512, int voxels_z=512, float volume_size=3.0f)
Constructor.