Point Cloud Library (PCL)  1.10.0-dev
octree_pointcloud_adjacency.hpp
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 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_OCTREE_POINTCLOUD_ADJACENCY_HPP_
39 #define PCL_OCTREE_POINTCLOUD_ADJACENCY_HPP_
40 
41 #include <pcl/common/geometry.h>
42 #include <pcl/console/print.h>
43 /*
44  * OctreePointCloudAdjacency 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_adjacency.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>
55  OctreePointCloudAdjacency(const double resolution_arg)
57  LeafContainerT,
58  BranchContainerT,
59  OctreeBase<LeafContainerT, BranchContainerT>>(resolution_arg)
60 {}
61 
62 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
63 template <typename PointT, typename LeafContainerT, typename BranchContainerT>
64 void
67 {
68  // double t1,t2;
69  float minX = std::numeric_limits<float>::max(),
70  minY = std::numeric_limits<float>::max(),
71  minZ = std::numeric_limits<float>::max();
72  float maxX = -std::numeric_limits<float>::max(),
73  maxY = -std::numeric_limits<float>::max(),
74  maxZ = -std::numeric_limits<float>::max();
75 
76  for (std::size_t i = 0; i < input_->size(); ++i) {
77  PointT temp(input_->points[i]);
78  if (transform_func_) // Search for point with
79  transform_func_(temp);
80  if (!pcl::isFinite(
81  temp)) // Check to make sure transform didn't make point not finite
82  continue;
83  if (temp.x < minX)
84  minX = temp.x;
85  if (temp.y < minY)
86  minY = temp.y;
87  if (temp.z < minZ)
88  minZ = temp.z;
89  if (temp.x > maxX)
90  maxX = temp.x;
91  if (temp.y > maxY)
92  maxY = temp.y;
93  if (temp.z > maxZ)
94  maxZ = temp.z;
95  }
96  this->defineBoundingBox(minX, minY, minZ, maxX, maxY, maxZ);
97 
99 
100  leaf_vector_.reserve(this->getLeafCount());
101  for (auto leaf_itr = this->leaf_depth_begin(); leaf_itr != this->leaf_depth_end();
102  ++leaf_itr) {
103  OctreeKey leaf_key = leaf_itr.getCurrentOctreeKey();
104  LeafContainerT* leaf_container = &(leaf_itr.getLeafContainer());
105 
106  // Run the leaf's compute function
107  leaf_container->computeData();
108 
109  computeNeighbors(leaf_key, leaf_container);
110 
111  leaf_vector_.push_back(leaf_container);
112  }
113  // Make sure our leaf vector is correctly sized
114  assert(leaf_vector_.size() == this->getLeafCount());
115 }
116 
117 //////////////////////////////////////////////////////////////////////////////////////////////
118 template <typename PointT, typename LeafContainerT, typename BranchContainerT>
119 void
121  genOctreeKeyforPoint(const PointT& point_arg, OctreeKey& key_arg) const
122 {
123  if (transform_func_) {
124  PointT temp(point_arg);
125  transform_func_(temp);
126  // calculate integer key for transformed point coordinates
127  if (pcl::isFinite(temp)) // Make sure transformed point is finite - if it is not, it
128  // gets default key
129  {
130  key_arg.x =
131  static_cast<unsigned int>((temp.x - this->min_x_) / this->resolution_);
132  key_arg.y =
133  static_cast<unsigned int>((temp.y - this->min_y_) / this->resolution_);
134  key_arg.z =
135  static_cast<unsigned int>((temp.z - this->min_z_) / this->resolution_);
136  }
137  else {
138  key_arg = OctreeKey();
139  }
140  }
141  else {
142  // calculate integer key for point coordinates
143  key_arg.x =
144  static_cast<unsigned int>((point_arg.x - this->min_x_) / this->resolution_);
145  key_arg.y =
146  static_cast<unsigned int>((point_arg.y - this->min_y_) / this->resolution_);
147  key_arg.z =
148  static_cast<unsigned int>((point_arg.z - this->min_z_) / this->resolution_);
149  }
150 }
151 
152 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
153 template <typename PointT, typename LeafContainerT, typename BranchContainerT>
154 void
156  addPointIdx(const int pointIdx_arg)
157 {
158  OctreeKey key;
159 
160  assert(pointIdx_arg < static_cast<int>(this->input_->points.size()));
161 
162  const PointT& point = this->input_->points[pointIdx_arg];
163  if (!pcl::isFinite(point))
164  return;
165 
166  // generate key
167  this->genOctreeKeyforPoint(point, key);
168  // add point to octree at key
169  LeafContainerT* container = this->createLeaf(key);
170  container->addPoint(point);
171 }
172 
173 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
174 template <typename PointT, typename LeafContainerT, typename BranchContainerT>
175 void
177  computeNeighbors(OctreeKey& key_arg, LeafContainerT* leaf_container)
178 {
179  // Make sure requested key is valid
180  if (key_arg.x > this->max_key_.x || key_arg.y > this->max_key_.y ||
181  key_arg.z > this->max_key_.z) {
182  PCL_ERROR("OctreePointCloudAdjacency::computeNeighbors Requested neighbors for "
183  "invalid octree key\n");
184  return;
185  }
186 
187  OctreeKey neighbor_key;
188  int dx_min = (key_arg.x > 0) ? -1 : 0;
189  int dy_min = (key_arg.y > 0) ? -1 : 0;
190  int dz_min = (key_arg.z > 0) ? -1 : 0;
191  int dx_max = (key_arg.x == this->max_key_.x) ? 0 : 1;
192  int dy_max = (key_arg.y == this->max_key_.y) ? 0 : 1;
193  int dz_max = (key_arg.z == this->max_key_.z) ? 0 : 1;
194 
195  for (int dx = dx_min; dx <= dx_max; ++dx) {
196  for (int dy = dy_min; dy <= dy_max; ++dy) {
197  for (int dz = dz_min; dz <= dz_max; ++dz) {
198  neighbor_key.x = static_cast<std::uint32_t>(key_arg.x + dx);
199  neighbor_key.y = static_cast<std::uint32_t>(key_arg.y + dy);
200  neighbor_key.z = static_cast<std::uint32_t>(key_arg.z + dz);
201  LeafContainerT* neighbor = this->findLeaf(neighbor_key);
202  if (neighbor) {
203  leaf_container->addNeighbor(neighbor);
204  }
205  }
206  }
207  }
208 }
209 
210 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
211 template <typename PointT, typename LeafContainerT, typename BranchContainerT>
212 LeafContainerT*
214  getLeafContainerAtPoint(const PointT& point_arg) const
215 {
216  OctreeKey key;
217  LeafContainerT* leaf = nullptr;
218  // generate key
219  this->genOctreeKeyforPoint(point_arg, key);
220 
221  leaf = this->findLeaf(key);
222 
223  return leaf;
224 }
225 
226 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
227 template <typename PointT, typename LeafContainerT, typename BranchContainerT>
228 void
231 {
232  // TODO Change this to use leaf centers, not centroids!
233 
234  voxel_adjacency_graph.clear();
235  // Add a vertex for each voxel, store ids in map
236  std::map<LeafContainerT*, VoxelID> leaf_vertex_id_map;
237  for (typename OctreeAdjacencyT::LeafNodeDepthFirstIterator leaf_itr =
238  this->leaf_depth_begin();
239  leaf_itr != this->leaf_depth_end();
240  ++leaf_itr) {
241  OctreeKey leaf_key = leaf_itr.getCurrentOctreeKey();
242  PointT centroid_point;
243  this->genLeafNodeCenterFromOctreeKey(leaf_key, centroid_point);
244  VoxelID node_id = add_vertex(voxel_adjacency_graph);
245 
246  voxel_adjacency_graph[node_id] = centroid_point;
247  LeafContainerT* leaf_container = &(leaf_itr.getLeafContainer());
248  leaf_vertex_id_map[leaf_container] = node_id;
249  }
250 
251  // Iterate through and add edges to adjacency graph
252  for (typename std::vector<LeafContainerT*>::iterator leaf_itr = leaf_vector_.begin();
253  leaf_itr != leaf_vector_.end();
254  ++leaf_itr) {
255  VoxelID u = (leaf_vertex_id_map.find(*leaf_itr))->second;
256  PointT p_u = voxel_adjacency_graph[u];
257  for (auto neighbor_itr = (*leaf_itr)->cbegin(), neighbor_end = (*leaf_itr)->cend();
258  neighbor_itr != neighbor_end;
259  ++neighbor_itr) {
260  LeafContainerT* neighbor_container = *neighbor_itr;
261  EdgeID edge;
262  bool edge_added;
263  VoxelID v = (leaf_vertex_id_map.find(neighbor_container))->second;
264  boost::tie(edge, edge_added) = add_edge(u, v, voxel_adjacency_graph);
265 
266  PointT p_v = voxel_adjacency_graph[v];
267  float dist = (p_v.getVector3fMap() - p_u.getVector3fMap()).norm();
268  voxel_adjacency_graph[edge] = dist;
269  }
270  }
271 }
272 
273 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
274 template <typename PointT, typename LeafContainerT, typename BranchContainerT>
275 bool
277  testForOcclusion(const PointT& point_arg, const PointXYZ& camera_pos)
278 {
279  OctreeKey key;
280  this->genOctreeKeyforPoint(point_arg, key);
281  // This code follows the method in Octree::PointCloud
282  Eigen::Vector3f sensor(camera_pos.x, camera_pos.y, camera_pos.z);
283 
284  Eigen::Vector3f leaf_centroid(
285  static_cast<float>((static_cast<double>(key.x) + 0.5f) * this->resolution_ +
286  this->min_x_),
287  static_cast<float>((static_cast<double>(key.y) + 0.5f) * this->resolution_ +
288  this->min_y_),
289  static_cast<float>((static_cast<double>(key.z) + 0.5f) * this->resolution_ +
290  this->min_z_));
291  Eigen::Vector3f direction = sensor - leaf_centroid;
292 
293  float norm = direction.norm();
294  direction.normalize();
295  float precision = 1.0f;
296  const float step_size = static_cast<const float>(resolution_) * precision;
297  const int nsteps = std::max(1, static_cast<int>(norm / step_size));
298 
299  OctreeKey prev_key = key;
300  // Walk along the line segment with small steps.
301  Eigen::Vector3f p = leaf_centroid;
302  PointT octree_p;
303  for (int i = 0; i < nsteps; ++i) {
304  // Start at the leaf voxel, and move back towards sensor.
305  p += (direction * step_size);
306 
307  octree_p.x = p.x();
308  octree_p.y = p.y();
309  octree_p.z = p.z();
310  // std::cout << octree_p<< "\n";
311  OctreeKey key;
312  this->genOctreeKeyforPoint(octree_p, key);
313 
314  // Not a new key, still the same voxel (starts at self).
315  if ((key == prev_key))
316  continue;
317 
318  prev_key = key;
319 
320  LeafContainerT* leaf = this->findLeaf(key);
321  // If the voxel is occupied, there is a possible occlusion
322  if (leaf) {
323  return true;
324  }
325  }
326 
327  // If we didn't run into a voxel on the way to this camera, it can't be occluded.
328  return false;
329 }
330 
331 #define PCL_INSTANTIATE_OctreePointCloudAdjacency(T) \
332  template class PCL_EXPORTS pcl::octree::OctreePointCloudAdjacency<T>;
333 
334 #endif
typename VoxelAdjacencyList::edge_descriptor EdgeID
void genOctreeKeyforPoint(const PointT &point_arg, OctreeKey &key_arg) const
Generates octree key for specified point (uses transform if provided).
LeafContainerT * createLeaf(unsigned int idx_x_arg, unsigned int idx_y_arg, unsigned int idx_z_arg)
Create new leaf node at (idx_x_arg, idx_y_arg, idx_z_arg).
Octree pointcloud class
bool isFinite(const PointT &pt)
Tests if the 3D components of a point are all finite param[in] pt point to be tested return true if f...
Definition: point_tests.h:55
Octree class.
Definition: octree_base.h:60
void addPointsFromInputCloud()
Add points from input point cloud to octree.
void genLeafNodeCenterFromOctreeKey(const OctreeKey &key_arg, PointT &point_arg) const
Generate a point at center of leaf node voxel.
OctreePointCloudAdjacency(const double resolution_arg)
Constructor.
typename VoxelAdjacencyList::vertex_descriptor VoxelID
const LeafNodeDepthFirstIterator leaf_depth_end()
Definition: octree_base.h:153
void addPointIdx(const int point_idx_arg) override
Add point at index from input pointcloud dataset to octree.
Defines some geometrical functions and utility functions.
boost::adjacency_list< boost::setS, boost::setS, boost::undirectedS, PointT, float > VoxelAdjacencyList
PointCloudConstPtr input_
Pointer to input point cloud dataset.
void defineBoundingBox()
Investigate dimensions of pointcloud data set and define corresponding bounding box for octree...
OctreeKey max_key_
key range
Definition: octree_base.h:94
void computeNeighbors(OctreeKey &key_arg, LeafContainerT *leaf_container)
Fills in the neighbors fields for new voxels.
A point structure representing Euclidean xyz coordinates.
LeafContainerT * findLeaf(unsigned int idx_x_arg, unsigned int idx_y_arg, unsigned int idx_z_arg)
Find leaf node at (idx_x_arg, idx_y_arg, idx_z_arg).
std::size_t getLeafCount() const
Return the amount of existing leafs in the octree.
Definition: octree_base.h:321
Octree leaf node iterator class.
LeafNodeDepthFirstIterator leaf_depth_begin(unsigned int max_depth_arg=0u)
Definition: octree_base.h:146
LeafContainerT * getLeafContainerAtPoint(const PointT &point_arg) const
Gets the leaf container for a given point.
Octree key class
Definition: octree_key.h:49
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.
void computeVoxelAdjacencyGraph(VoxelAdjacencyList &voxel_adjacency_graph)
Computes an adjacency graph of voxel relations.
A point structure representing Euclidean xyz coordinates, and the RGB color.
void addPointsFromInputCloud()
Adds points from cloud to the octree.