Point Cloud Library (PCL)  1.9.1-dev
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  // iterate over all children
94  for (unsigned char child_idx = 0; child_idx < 8; child_idx++)
95  {
96  // if child exist
97  if (branch_arg->hasChild (child_idx))
98  {
99  // add current branch voxel to key
100  key_arg.pushBranch (child_idx);
101 
102  OctreeNode *child_node = branch_arg->getChildPtr (child_idx);
103 
104  switch (child_node->getNodeType ())
105  {
106  case BRANCH_NODE:
107  {
108  // recursively proceed with indexed child branch
109  getVoxelCentroidsRecursive (static_cast<const BranchNode*> (child_node), key_arg, voxel_centroid_list_arg);
110  break;
111  }
112  case LEAF_NODE:
113  {
114  PointT new_centroid;
115 
116  LeafNode* container = static_cast<LeafNode*> (child_node);
117 
118  container->getContainer().getCentroid (new_centroid);
119 
120  voxel_centroid_list_arg.push_back (new_centroid);
121  break;
122  }
123  default:
124  break;
125  }
126 
127  // pop current branch voxel from key
128  key_arg.popBranch ();
129  }
130  }
131 }
132 
133 
134 #define PCL_INSTANTIATE_OctreePointCloudVoxelCentroid(T) template class PCL_EXPORTS pcl::octree::OctreePointCloudVoxelCentroid<T>;
135 
136 #endif
137 
Octree pointcloud class
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...
size_t getVoxelCentroids(typename OctreePointCloud< PointT, LeafContainerT, BranchContainerT >::AlignedPointTVector &voxel_centroid_list_arg) const
Get PointT vector of centroids for all occupied voxels.
virtual node_type_t getNodeType() const =0
Pure virtual method for receiving the type of octree node (branch or leaf)
OctreeNode * getChildPtr(unsigned char child_idx_arg) const
Get pointer to child.
Definition: octree_nodes.h:267
bool getVoxelCentroidAtPoint(const PointT &point_arg, PointT &voxel_centroid_arg) const
Get centroid for a single voxel addressed by a PointT point.
void popBranch()
pop child node from octree key
Definition: octree_key.h:122
bool hasChild(unsigned char child_idx_arg) const
Check if branch is pointing to a particular child node.
Definition: octree_nodes.h:287
Octree key class
Definition: octree_key.h:50
Abstract octree leaf class
Definition: octree_nodes.h:96
A point structure representing Euclidean xyz coordinates, and the RGB color.
Abstract octree branch class
Definition: octree_nodes.h:203
void pushBranch(unsigned char childIndex)
push a child node to the octree key
Definition: octree_key.h:112
const ContainerT & getContainer() const
Get const reference to container.
Definition: octree_nodes.h:163
Abstract octree node class
Definition: octree_nodes.h:67