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