voxel_grid_graph_builder.hpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2014-, Open Perception, 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 the copyright holder(s) 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 #ifndef PCL_GRAPH_IMPL_VOXEL_GRID_GRAPH_BUILDER_CPP
39 #define PCL_GRAPH_IMPL_VOXEL_GRID_GRAPH_BUILDER_CPP
40 
41 
42 #include <boost/unordered_map.hpp>
43 
44 #include <pcl/common/io.h>
45 #include <pcl/common/common.h>
46 #include <pcl/common/centroid.h>
47 
48 #pragma GCC diagnostic push
49 #pragma GCC diagnostic ignored "-Wignored-qualifiers"
50 #pragma GCC diagnostic push
51 #pragma GCC diagnostic ignored "-Wpedantic"
52 #include <pcl/octree/octree.h>
53 #pragma GCC diagnostic pop
54 #pragma GCC diagnostic pop
55 
57 
58 /* The function below is required in order to use boost::unordered_map with
59  * pcl::octree::OctreeKey key type. It simply hashes the x, y, z array of
60  * indices, because it uniquely defines the key. */
61 
62 #pragma GCC diagnostic push
63 #pragma GCC diagnostic ignored "-Wunused-function"
64 namespace pcl
65 {
66 
67  namespace octree
68  {
69 
70  static size_t hash_value(const OctreeKey& b)
71  {
72  return boost::hash_value(b.key_);
73  }
74 
75  }
76 
77 }
78 #pragma GCC diagnostic pop
79 
80 template <typename PointT, typename GraphT> void
82 {
83  if (!initCompute())
84  {
85  graph = GraphT();
86  deinitCompute();
87  return;
88  }
89 
90  typename pcl::PointCloud<PointT>::Ptr transformed(new pcl::PointCloud<PointT>);
91  pcl::copyPointCloud(*input_, *transformed);
92  for (size_t i = 0; i < transformed->size(); ++i)
93  {
94  PointT& p = transformed->points[i];
95  p.x /= p.z;
96  p.y /= p.z;
97  p.z = std::log(p.z);
98  }
99 
100  Eigen::Vector4f min, max;
101  pcl::getMinMax3D(*transformed, *indices_, min, max);
102 
103  // Create and initialize an Octree that stores point indices
104  typedef pcl::octree::OctreePointCloud<PointT> Octree;
105  Octree octree(voxel_resolution_);
106  octree.defineBoundingBox(min(0), min(1), min(2), max(0), max(1), max(2));
107  octree.setInputCloud(transformed, indices_);
108  octree.addPointsFromInputCloud();
109 
110  graph = GraphT(octree.getLeafCount());
111 
112  typedef boost::unordered_map<pcl::octree::OctreeKey, VertexId> KeyVertexMap;
113  KeyVertexMap key_to_vertex_map;
114 
115  point_to_vertex_map_.clear();
116  point_to_vertex_map_.resize(transformed->size(), std::numeric_limits<VertexId>::max());
117 
118  typename Octree::LeafNodeIterator leaf_itr = octree.leaf_depth_begin();
119  for (VertexId v = 0; leaf_itr != octree.leaf_depth_end(); ++leaf_itr, ++v)
120  {
121  // Step 1: compute leaf centroid and fill in corresponding elements of the
122  // point to vertex map.
123  pcl::CentroidPoint<PointInT> centroid;
124  std::vector<int>& indices = leaf_itr.getLeafContainer().getPointIndicesVector();
125  for (int & index : indices)
126  {
127  centroid.add(input_->operator[](index));
128  point_to_vertex_map_[index] = v;
129  }
130  centroid.get(graph[v]);
131 
132  // Step 2: fill in octree key to vertex map.
133  octree::OctreeKey key = leaf_itr.getCurrentOctreeKey();
134  key_to_vertex_map[key] = v;
135 
136  // Step 3: find neighbors and insert edges.
137  octree::OctreeKey neighbor_key;
138  for (int dx = (key.x > 0) ? -1 : 0; dx <= 1; ++dx)
139  {
140  neighbor_key.x = static_cast<std::uint32_t>(key.x + dx);
141  for (int dy = (key.y > 0) ? -1 : 0; dy <= 1; ++dy)
142  {
143  neighbor_key.y = static_cast<std::uint32_t>(key.y + dy);
144  for (int dz = (key.z > 0) ? -1 : 0; dz <= 1; ++dz)
145  {
146  neighbor_key.z = static_cast<std::uint32_t>(key.z + dz);
147  typename KeyVertexMap::iterator f = key_to_vertex_map.find(neighbor_key);
148  if (f != key_to_vertex_map.end() && v != f->second)
149  {
150  boost::add_edge(v, f->second, graph);
151  }
152  }
153  }
154  }
155  }
156  graph.m_graph.m_point_cloud->header = input_->header;
157 }
158 
159 #endif /* PCL_GRAPH_IMPL_VOXEL_GRID_GRAPH_BUILDER_CPP */
pcl
Definition: pcl_point_operators.cpp:4
index
uint8_t index
Definition: EtherCATFrame.h:59
pcl::graph::GraphBuilder::VertexId
boost::graph_traits< GraphT >::vertex_descriptor VertexId
Definition: graph_builder.h:81
voxel_grid_graph_builder.h
pcl::graph::indices
pcl::PointIndices::Ptr indices(const PCG &g)
Retrieve the indices of the points of the point cloud stored in a point cloud graph that actually bel...
Definition: point_cloud_graph.h:737
pcl::graph::VoxelGridGraphBuilder::compute
virtual void compute(GraphT &graph)
Build a graph based on the provided input data.
Definition: voxel_grid_graph_builder.hpp:81
armarx::PointT
pcl::PointXYZRGBL PointT
Definition: Common.h:28
max
T max(T t1, T t2)
Definition: gdiam.h:48
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
min
T min(T t1, T t2)
Definition: gdiam.h:42