Point Cloud Library (PCL)  1.8.1
octree_pointcloud_voxelcentroid.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id: octree_pointcloud_voxelcentroid.hpp 6459 2012-07-18 07:50:37Z dpb $
38  */
39 
40 #ifndef PCL_OCTREE_VOXELCENTROID_HPP
41 #define PCL_OCTREE_VOXELCENTROID_HPP
42 
43 /*
44  * OctreePointCloudVoxelcontroid is not precompiled, since it's used in other
45  * parts of PCL with custom LeafContainers. So if PCL_NO_PRECOMPILE is NOT
46  * used, octree_pointcloud_voxelcentroid.h includes this file but octree_pointcloud.h
47  * would not include the implementation because it's precompiled. So we need to
48  * include it here "manually".
49  */
50 #include <pcl/octree/impl/octree_pointcloud.hpp>
51 
52 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
53 template<typename PointT, typename LeafContainerT, typename BranchContainerT> bool
55  const PointT& point_arg, PointT& voxel_centroid_arg) const
56 {
57  OctreeKey key;
58  LeafContainerT* leaf = NULL;
59 
60  // generate key
61  genOctreeKeyforPoint (point_arg, key);
62 
63  leaf = this->findLeaf (key);
64  if (leaf)
65  leaf->getCentroid (voxel_centroid_arg);
66 
67  return (leaf != NULL);
68 }
69 
70 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
71 template<typename PointT, typename LeafContainerT, typename BranchContainerT> size_t
74 {
75  OctreeKey new_key;
76 
77  // reset output vector
78  voxel_centroid_list_arg.clear ();
79  voxel_centroid_list_arg.reserve (this->leaf_count_);
80 
81  getVoxelCentroidsRecursive (this->root_node_, new_key, voxel_centroid_list_arg );
82 
83  // return size of centroid vector
84  return (voxel_centroid_list_arg.size ());
85 }
86 
87 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
88 template<typename PointT, typename LeafContainerT, typename BranchContainerT> void
90  const BranchNode* branch_arg, OctreeKey& key_arg,
92 {
93  // child iterator
94  unsigned char child_idx;
95 
96  // iterate over all children
97  for (child_idx = 0; child_idx < 8; child_idx++)
98  {
99  // if child exist
100  if (branch_arg->hasChild (child_idx))
101  {
102  // add current branch voxel to key
103  key_arg.pushBranch (child_idx);
104 
105  OctreeNode *child_node = branch_arg->getChildPtr (child_idx);
106 
107  switch (child_node->getNodeType ())
108  {
109  case BRANCH_NODE:
110  {
111  // recursively proceed with indexed child branch
112  getVoxelCentroidsRecursive (static_cast<const BranchNode*> (child_node), key_arg, voxel_centroid_list_arg);
113  break;
114  }
115  case LEAF_NODE:
116  {
117  PointT new_centroid;
118 
119  LeafNode* container = static_cast<LeafNode*> (child_node);
120 
121  container->getContainer().getCentroid (new_centroid);
122 
123  voxel_centroid_list_arg.push_back (new_centroid);
124  break;
125  }
126  default:
127  break;
128  }
129 
130  // pop current branch voxel from key
131  key_arg.popBranch ();
132  }
133  }
134 }
135 
136 
137 #define PCL_INSTANTIATE_OctreePointCloudVoxelCentroid(T) template class PCL_EXPORTS pcl::octree::OctreePointCloudVoxelCentroid<T>;
138 
139 #endif
140 
bool hasChild(unsigned char child_idx_arg) const
Check if branch is pointing to a particular child node.
Definition: octree_nodes.h:292
Octree pointcloud class
bool getVoxelCentroidAtPoint(const PointT &point_arg, PointT &voxel_centroid_arg) const
Get centroid for a single voxel addressed by a PointT point.
virtual node_type_t getNodeType() const =0
Pure virtual method for receiving the type of octree node (branch or leaf)
const ContainerT & getContainer() const
Get const reference to container.
Definition: octree_nodes.h:164
OctreeNode * getChildPtr(unsigned char child_idx_arg) const
Get pointer to child.
Definition: octree_nodes.h:272
void getVoxelCentroidsRecursive(const BranchNode *branch_arg, OctreeKey &key_arg, typename OctreePointCloud< PointT, LeafContainerT, BranchContainerT >::AlignedPointTVector &voxel_centroid_list_arg) const
Recursively explore the octree and output a PointT vector of centroids for all occupied voxels.
void popBranch()
pop child node from octree key
Definition: octree_key.h:114
size_t getVoxelCentroids(typename OctreePointCloud< PointT, LeafContainerT, BranchContainerT >::AlignedPointTVector &voxel_centroid_list_arg) const
Get PointT vector of centroids for all occupied voxels.
Octree key class
Definition: octree_key.h:51
Abstract octree leaf class
Definition: octree_nodes.h:97
A point structure representing Euclidean xyz coordinates, and the RGB color.
Abstract octree branch class
Definition: octree_nodes.h:204
void pushBranch(unsigned char childIndex)
push a child node to the octree key
Definition: octree_key.h:104
Abstract octree node class
Definition: octree_nodes.h:68