Point Cloud Library (PCL)  1.10.0
octree_pointcloud_adjacency.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2012, Jeremie Papon
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  * Author : jpapon@gmail.com
37  * Email : jpapon@gmail.com
38  */
39 
40 #pragma once
41 
42 #include <pcl/octree/boost.h>
43 #include <pcl/octree/octree_pointcloud.h>
44 #include <pcl/octree/octree_pointcloud_adjacency_container.h>
45 
46 #include <set>
47 #include <list>
48 
49 namespace pcl
50 {
51 
52  namespace octree
53  {
54  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
55  /** \brief @b Octree pointcloud voxel class which maintains adjacency information for its voxels.
56  *
57  * This pointcloud octree class generates an octree from a point cloud (zero-copy). The octree pointcloud is
58  * initialized with its voxel resolution. Its bounding box is automatically adjusted or can be predefined.
59  *
60  * The OctreePointCloudAdjacencyContainer class can be used to store data in leaf nodes.
61  *
62  * An optional transform function can be provided which changes how the voxel grid is computed - this can be used to,
63  * for example, make voxel bins larger as they increase in distance from the origin (camera).
64  * \note See SupervoxelClustering for an example of how to provide a transform function.
65  *
66  * If used in academic work, please cite:
67  *
68  * - J. Papon, A. Abramov, M. Schoeler, F. Woergoetter
69  * Voxel Cloud Connectivity Segmentation - Supervoxels from PointClouds
70  * In Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition (CVPR) 2013
71  *
72  * \ingroup octree
73  * \author Jeremie Papon (jpapon@gmail.com) */
74  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
75  template <typename PointT,
76  typename LeafContainerT = OctreePointCloudAdjacencyContainer<PointT>,
77  typename BranchContainerT = OctreeContainerEmpty>
78  class OctreePointCloudAdjacency : public OctreePointCloud<PointT, LeafContainerT, BranchContainerT>
79  {
80 
81  public:
82 
84 
88 
92 
94  using PointCloudPtr = typename PointCloud::Ptr;
96 
97  // BGL graph
98  using VoxelAdjacencyList = boost::adjacency_list<boost::setS, boost::setS, boost::undirectedS, PointT, float>;
99  using VoxelID = typename VoxelAdjacencyList::vertex_descriptor;
100  using EdgeID = typename VoxelAdjacencyList::edge_descriptor;
101 
102  // Leaf vector - pointers to all leaves
103  using LeafVectorT = std::vector<LeafContainerT *>;
104 
105  // Fast leaf iterators that don't require traversing tree
106  using iterator = typename LeafVectorT::iterator;
107  using const_iterator = typename LeafVectorT::const_iterator;
108 
109  inline iterator begin () { return (leaf_vector_.begin ()); }
110  inline iterator end () { return (leaf_vector_.end ()); }
111  inline LeafContainerT* at (std::size_t idx) { return leaf_vector_.at (idx); }
112 
113  // Size of neighbors
114  inline std::size_t size () const { return leaf_vector_.size (); }
115 
116  /** \brief Constructor.
117  *
118  * \param[in] resolution_arg Octree resolution at lowest octree level (voxel size) */
119  OctreePointCloudAdjacency (const double resolution_arg);
120 
121  /** \brief Adds points from cloud to the octree.
122  *
123  * \note This overrides addPointsFromInputCloud() from the OctreePointCloud class. */
124  void
126 
127  /** \brief Gets the leaf container for a given point.
128  *
129  * \param[in] point_arg Point to search for
130  *
131  * \returns Pointer to the leaf container - null if no leaf container found. */
132  LeafContainerT*
133  getLeafContainerAtPoint (const PointT& point_arg) const;
134 
135  /** \brief Computes an adjacency graph of voxel relations.
136  *
137  * \warning This slows down rapidly as cloud size increases due to the number of edges.
138  *
139  * \param[out] voxel_adjacency_graph Boost Graph Library Adjacency graph of the voxel touching relationships.
140  * Vertices are PointT, edges represent touching, and edge lengths are the distance between the points. */
141  void
142  computeVoxelAdjacencyGraph (VoxelAdjacencyList &voxel_adjacency_graph);
143 
144  /** \brief Sets a point transform (and inverse) used to transform the space of the input cloud.
145  *
146  * This is useful for changing how adjacency is calculated - such as relaxing the adjacency criterion for
147  * points further from the camera.
148  *
149  * \param[in] transform_func A boost:function pointer to the transform to be used. The transform must have one
150  * parameter (a point) which it modifies in place. */
151  void
152  setTransformFunction (std::function<void (PointT &p)> transform_func)
153  {
154  transform_func_ = transform_func;
155  }
156 
157  /** \brief Tests whether input point is occluded from specified camera point by other voxels.
158  *
159  * \param[in] point_arg Point to test for
160  * \param[in] camera_pos Position of camera, defaults to origin
161  *
162  * \returns True if path to camera is blocked by a voxel, false otherwise. */
163  bool
164  testForOcclusion (const PointT& point_arg, const PointXYZ &camera_pos = PointXYZ (0, 0, 0));
165 
166  protected:
167 
168  /** \brief Add point at index from input pointcloud dataset to octree.
169  *
170  * \param[in] point_idx_arg The index representing the point in the dataset given by setInputCloud() to be added
171  *
172  * \note This virtual implementation allows the use of a transform function to compute keys. */
173  void
174  addPointIdx (const int point_idx_arg) override;
175 
176  /** \brief Fills in the neighbors fields for new voxels.
177  *
178  * \param[in] key_arg Key of the voxel to check neighbors for
179  * \param[in] leaf_container Pointer to container of the leaf to check neighbors for */
180  void
181  computeNeighbors (OctreeKey &key_arg, LeafContainerT* leaf_container);
182 
183  /** \brief Generates octree key for specified point (uses transform if provided).
184  *
185  * \param[in] point_arg Point to generate key for
186  * \param[out] key_arg Resulting octree key */
187  void
188  genOctreeKeyforPoint (const PointT& point_arg, OctreeKey& key_arg) const;
189 
190  private:
191 
192  /** \brief Add point at given index from input point cloud to octree.
193  *
194  * Index will be also added to indices vector. This functionality is not enabled for adjacency octree. */
196 
197  /** \brief Add point simultaneously to octree and input point cloud.
198  *
199  * This functionality is not enabled for adjacency octree. */
201 
210 
211  /// Local leaf pointer vector used to make iterating through leaves fast.
212  LeafVectorT leaf_vector_;
213 
214  std::function<void (PointT &p)> transform_func_;
215 
216  };
217 
218  }
219 
220 }
221 
222 // Note: Do not precompile this octree type because it is typically used with custom leaf containers.
223 #include <pcl/octree/impl/octree_pointcloud_adjacency.hpp>
pcl
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
pcl::octree::OctreePointCloudAdjacency::end
iterator end()
Definition: octree_pointcloud_adjacency.h:110
pcl::octree::OctreePointCloudAdjacency::setTransformFunction
void setTransformFunction(std::function< void(PointT &p)> transform_func)
Sets a point transform (and inverse) used to transform the space of the input cloud.
Definition: octree_pointcloud_adjacency.h:152
pcl::octree::OctreePointCloud::resolution_
double resolution_
Octree resolution.
Definition: octree_pointcloud.h:521
pcl::octree::OctreePointCloudAdjacency::computeNeighbors
void computeNeighbors(OctreeKey &key_arg, LeafContainerT *leaf_container)
Fills in the neighbors fields for new voxels.
Definition: octree_pointcloud_adjacency.hpp:160
pcl::octree::OctreePointCloudAdjacency::VoxelID
typename VoxelAdjacencyList::vertex_descriptor VoxelID
Definition: octree_pointcloud_adjacency.h:99
pcl::octree::OctreePointCloud
Octree pointcloud class
Definition: octree_pointcloud.h:72
pcl::octree::OctreePointCloudAdjacency::Ptr
shared_ptr< OctreeAdjacencyT > Ptr
Definition: octree_pointcloud_adjacency.h:86
pcl::octree::OctreeKey
Octree key class
Definition: octree_key.h:50
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: projection_matrix.h:52
pcl::octree::OctreePointCloud::addPointToCloud
void addPointToCloud(const PointT &point_arg, PointCloudPtr cloud_arg)
Add point simultaneously to octree and input point cloud.
Definition: octree_pointcloud.hpp:98
pcl::PointXYZRGB
A point structure representing Euclidean xyz coordinates, and the RGB color.
Definition: point_types.hpp:623
pcl::octree::OctreePointCloudAdjacency::EdgeID
typename VoxelAdjacencyList::edge_descriptor EdgeID
Definition: octree_pointcloud_adjacency.h:100
pcl::octree::OctreePointCloudAdjacency::addPointsFromInputCloud
void addPointsFromInputCloud()
Adds points from cloud to the octree.
Definition: octree_pointcloud_adjacency.hpp:63
pcl::octree::OctreePointCloud::min_y_
double min_y_
Definition: octree_pointcloud.h:527
pcl::octree::OctreePointCloudAdjacency::size
std::size_t size() const
Definition: octree_pointcloud_adjacency.h:114
pcl::octree::OctreeLeafNode
Abstract octree leaf class
Definition: octree_nodes.h:96
pcl::octree::OctreePointCloudAdjacency::iterator
typename LeafVectorT::iterator iterator
Definition: octree_pointcloud_adjacency.h:106
pcl::octree::OctreePointCloudAdjacency::genOctreeKeyforPoint
void genOctreeKeyforPoint(const PointT &point_arg, OctreeKey &key_arg) const
Generates octree key for specified point (uses transform if provided).
Definition: octree_pointcloud_adjacency.hpp:112
pcl::octree::OctreePointCloudAdjacency::PointCloudConstPtr
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: octree_pointcloud_adjacency.h:95
pcl::octree::OctreeBase
Octree class.
Definition: octree_base.h:61
pcl::octree::OctreePointCloudAdjacency::PointCloudPtr
typename PointCloud::Ptr PointCloudPtr
Definition: octree_pointcloud_adjacency.h:94
pcl::PointXYZ
A point structure representing Euclidean xyz coordinates.
Definition: point_types.hpp:291
pcl::octree::OctreeBranchNode
Abstract octree branch class
Definition: octree_nodes.h:203
pcl::octree::OctreePointCloudAdjacency::LeafVectorT
std::vector< LeafContainerT * > LeafVectorT
Definition: octree_pointcloud_adjacency.h:103
pcl::octree::OctreePointCloud::min_z_
double min_z_
Definition: octree_pointcloud.h:530
pcl::octree::OctreePointCloudAdjacency::addPointIdx
void addPointIdx(const int point_idx_arg) override
Add point at index from input pointcloud dataset to octree.
Definition: octree_pointcloud_adjacency.hpp:141
pcl::octree::OctreePointCloud::max_x_
double max_x_
Definition: octree_pointcloud.h:525
pcl::octree::OctreePointCloud::BranchNode
typename OctreeT::BranchNode BranchNode
Definition: octree_pointcloud.h:78
pcl::octree::OctreePointCloudAdjacency
Octree pointcloud voxel class which maintains adjacency information for its voxels.
Definition: octree_pointcloud_adjacency.h:78
pcl::octree::OctreePointCloudAdjacency::at
LeafContainerT * at(std::size_t idx)
Definition: octree_pointcloud_adjacency.h:111
pcl::octree::OctreePointCloud::addPointFromCloud
void addPointFromCloud(const int point_idx_arg, IndicesPtr indices_arg)
Add point at given index from input point cloud to octree.
Definition: octree_pointcloud.hpp:89
pcl::octree::OctreePointCloud::max_z_
double max_z_
Definition: octree_pointcloud.h:531
pcl::octree::OctreePointCloudAdjacency::begin
iterator begin()
Definition: octree_pointcloud_adjacency.h:109
pcl::octree::OctreePointCloud::max_y_
double max_y_
Definition: octree_pointcloud.h:528
pcl::octree::OctreePointCloudAdjacency::OctreePointCloudAdjacency
OctreePointCloudAdjacency(const double resolution_arg)
Constructor.
Definition: octree_pointcloud_adjacency.hpp:54
pcl::PointCloud::Ptr
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:415
pcl::octree::OctreePointCloudAdjacency::const_iterator
typename LeafVectorT::const_iterator const_iterator
Definition: octree_pointcloud_adjacency.h:107
pcl::PointCloud::ConstPtr
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:416
pcl::octree::OctreePointCloudAdjacency::getLeafContainerAtPoint
LeafContainerT * getLeafContainerAtPoint(const PointT &point_arg) const
Gets the leaf container for a given point.
Definition: octree_pointcloud_adjacency.hpp:200
pcl::octree::OctreePointCloudAdjacency::testForOcclusion
bool testForOcclusion(const PointT &point_arg, const PointXYZ &camera_pos=PointXYZ(0, 0, 0))
Tests whether input point is occluded from specified camera point by other voxels.
Definition: octree_pointcloud_adjacency.hpp:259
pcl::octree::OctreePointCloudAdjacency::computeVoxelAdjacencyGraph
void computeVoxelAdjacencyGraph(VoxelAdjacencyList &voxel_adjacency_graph)
Computes an adjacency graph of voxel relations.
Definition: octree_pointcloud_adjacency.hpp:215
pcl::octree::OctreePointCloudAdjacency::ConstPtr
shared_ptr< const OctreeAdjacencyT > ConstPtr
Definition: octree_pointcloud_adjacency.h:87
pcl::octree::OctreePointCloud::input_
PointCloudConstPtr input_
Pointer to input point cloud dataset.
Definition: octree_pointcloud.h:512
pcl::octree::OctreePointCloudAdjacency::VoxelAdjacencyList
boost::adjacency_list< boost::setS, boost::setS, boost::undirectedS, PointT, float > VoxelAdjacencyList
Definition: octree_pointcloud_adjacency.h:98
pcl::octree::OctreePointCloud::LeafNode
typename OctreeT::LeafNode LeafNode
Definition: octree_pointcloud.h:77
pcl::shared_ptr
boost::shared_ptr< T > shared_ptr
Alias for boost::shared_ptr.
Definition: pcl_macros.h:90
pcl::octree::OctreePointCloud::min_x_
double min_x_
Definition: octree_pointcloud.h:524