Point Cloud Library (PCL)  1.12.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 #pragma once
39 
40 #include <pcl/common/point_tests.h> // for pcl::isFinite
41 #include <pcl/console/print.h>
42 
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_)[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 = static_cast<uindex_t>((temp.x - this->min_x_) / this->resolution_);
131  key_arg.y = static_cast<uindex_t>((temp.y - this->min_y_) / this->resolution_);
132  key_arg.z = static_cast<uindex_t>((temp.z - this->min_z_) / this->resolution_);
133  }
134  else {
135  key_arg = OctreeKey();
136  }
137  }
138  else {
139  // calculate integer key for point coordinates
140  key_arg.x = static_cast<uindex_t>((point_arg.x - this->min_x_) / this->resolution_);
141  key_arg.y = static_cast<uindex_t>((point_arg.y - this->min_y_) / this->resolution_);
142  key_arg.z = static_cast<uindex_t>((point_arg.z - this->min_z_) / this->resolution_);
143  }
144 }
145 
146 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
147 template <typename PointT, typename LeafContainerT, typename BranchContainerT>
148 void
150  addPointIdx(const uindex_t pointIdx_arg)
151 {
152  OctreeKey key;
153 
154  assert(pointIdx_arg < this->input_->size());
155 
156  const PointT& point = (*this->input_)[pointIdx_arg];
157  if (!pcl::isFinite(point))
158  return;
159 
160  // generate key
161  this->genOctreeKeyforPoint(point, key);
162  // add point to octree at key
163  LeafContainerT* container = this->createLeaf(key);
164  container->addPoint(point);
165 }
166 
167 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
168 template <typename PointT, typename LeafContainerT, typename BranchContainerT>
169 void
171  computeNeighbors(OctreeKey& key_arg, LeafContainerT* leaf_container)
172 {
173  // Make sure requested key is valid
174  if (key_arg.x > this->max_key_.x || key_arg.y > this->max_key_.y ||
175  key_arg.z > this->max_key_.z) {
176  PCL_ERROR("OctreePointCloudAdjacency::computeNeighbors Requested neighbors for "
177  "invalid octree key\n");
178  return;
179  }
180 
181  OctreeKey neighbor_key;
182  int dx_min = (key_arg.x > 0) ? -1 : 0;
183  int dy_min = (key_arg.y > 0) ? -1 : 0;
184  int dz_min = (key_arg.z > 0) ? -1 : 0;
185  int dx_max = (key_arg.x == this->max_key_.x) ? 0 : 1;
186  int dy_max = (key_arg.y == this->max_key_.y) ? 0 : 1;
187  int dz_max = (key_arg.z == this->max_key_.z) ? 0 : 1;
188 
189  for (int dx = dx_min; dx <= dx_max; ++dx) {
190  for (int dy = dy_min; dy <= dy_max; ++dy) {
191  for (int dz = dz_min; dz <= dz_max; ++dz) {
192  neighbor_key.x = static_cast<std::uint32_t>(key_arg.x + dx);
193  neighbor_key.y = static_cast<std::uint32_t>(key_arg.y + dy);
194  neighbor_key.z = static_cast<std::uint32_t>(key_arg.z + dz);
195  LeafContainerT* neighbor = this->findLeaf(neighbor_key);
196  if (neighbor) {
197  leaf_container->addNeighbor(neighbor);
198  }
199  }
200  }
201  }
202 }
203 
204 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
205 template <typename PointT, typename LeafContainerT, typename BranchContainerT>
206 LeafContainerT*
208  getLeafContainerAtPoint(const PointT& point_arg) const
209 {
210  OctreeKey key;
211  LeafContainerT* leaf = nullptr;
212  // generate key
213  this->genOctreeKeyforPoint(point_arg, key);
214 
215  leaf = this->findLeaf(key);
216 
217  return leaf;
218 }
219 
220 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
221 template <typename PointT, typename LeafContainerT, typename BranchContainerT>
222 void
225 {
226  // TODO Change this to use leaf centers, not centroids!
227 
228  voxel_adjacency_graph.clear();
229  // Add a vertex for each voxel, store ids in map
230  std::map<LeafContainerT*, VoxelID> leaf_vertex_id_map;
231  for (typename OctreeAdjacencyT::LeafNodeDepthFirstIterator leaf_itr =
232  this->leaf_depth_begin();
233  leaf_itr != this->leaf_depth_end();
234  ++leaf_itr) {
235  OctreeKey leaf_key = leaf_itr.getCurrentOctreeKey();
236  PointT centroid_point;
237  this->genLeafNodeCenterFromOctreeKey(leaf_key, centroid_point);
238  VoxelID node_id = add_vertex(voxel_adjacency_graph);
239 
240  voxel_adjacency_graph[node_id] = centroid_point;
241  LeafContainerT* leaf_container = &(leaf_itr.getLeafContainer());
242  leaf_vertex_id_map[leaf_container] = node_id;
243  }
244 
245  // Iterate through and add edges to adjacency graph
246  for (typename std::vector<LeafContainerT*>::iterator leaf_itr = leaf_vector_.begin();
247  leaf_itr != leaf_vector_.end();
248  ++leaf_itr) {
249  VoxelID u = (leaf_vertex_id_map.find(*leaf_itr))->second;
250  PointT p_u = voxel_adjacency_graph[u];
251  for (auto neighbor_itr = (*leaf_itr)->cbegin(), neighbor_end = (*leaf_itr)->cend();
252  neighbor_itr != neighbor_end;
253  ++neighbor_itr) {
254  LeafContainerT* neighbor_container = *neighbor_itr;
255  EdgeID edge;
256  bool edge_added;
257  VoxelID v = (leaf_vertex_id_map.find(neighbor_container))->second;
258  boost::tie(edge, edge_added) = add_edge(u, v, voxel_adjacency_graph);
259 
260  PointT p_v = voxel_adjacency_graph[v];
261  float dist = (p_v.getVector3fMap() - p_u.getVector3fMap()).norm();
262  voxel_adjacency_graph[edge] = dist;
263  }
264  }
265 }
266 
267 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
268 template <typename PointT, typename LeafContainerT, typename BranchContainerT>
269 bool
271  testForOcclusion(const PointT& point_arg, const PointXYZ& camera_pos)
272 {
273  OctreeKey key;
274  this->genOctreeKeyforPoint(point_arg, key);
275  // This code follows the method in Octree::PointCloud
276  Eigen::Vector3f sensor(camera_pos.x, camera_pos.y, camera_pos.z);
277 
278  Eigen::Vector3f leaf_centroid(
279  static_cast<float>((static_cast<double>(key.x) + 0.5f) * this->resolution_ +
280  this->min_x_),
281  static_cast<float>((static_cast<double>(key.y) + 0.5f) * this->resolution_ +
282  this->min_y_),
283  static_cast<float>((static_cast<double>(key.z) + 0.5f) * this->resolution_ +
284  this->min_z_));
285  Eigen::Vector3f direction = sensor - leaf_centroid;
286 
287  float norm = direction.norm();
288  direction.normalize();
289  float precision = 1.0f;
290  const float step_size = static_cast<const float>(resolution_) * precision;
291  const auto nsteps = std::max<std::size_t>(1, norm / step_size);
292 
293  OctreeKey prev_key = key;
294  // Walk along the line segment with small steps.
295  Eigen::Vector3f p = leaf_centroid;
296  PointT octree_p;
297  for (std::size_t i = 0; i < nsteps; ++i) {
298  // Start at the leaf voxel, and move back towards sensor.
299  p += (direction * step_size);
300 
301  octree_p.x = p.x();
302  octree_p.y = p.y();
303  octree_p.z = p.z();
304  // std::cout << octree_p<< "\n";
305  OctreeKey key;
306  this->genOctreeKeyforPoint(octree_p, key);
307 
308  // Not a new key, still the same voxel (starts at self).
309  if ((key == prev_key))
310  continue;
311 
312  prev_key = key;
313 
314  LeafContainerT* leaf = this->findLeaf(key);
315  // If the voxel is occupied, there is a possible occlusion
316  if (leaf) {
317  return true;
318  }
319  }
320 
321  // If we didn't run into a voxel on the way to this camera, it can't be occluded.
322  return false;
323 }
324 
325 #define PCL_INSTANTIATE_OctreePointCloudAdjacency(T) \
326  template class PCL_EXPORTS pcl::octree::OctreePointCloudAdjacency<T>;
pcl::isFinite
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
pcl::octree::OctreeKey::z
uindex_t z
Definition: octree_key.h:149
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:171
pcl::octree::OctreePointCloudAdjacency::VoxelID
typename VoxelAdjacencyList::vertex_descriptor VoxelID
Definition: octree_pointcloud_adjacency.h:101
pcl::octree::OctreePointCloud
Octree pointcloud class
Definition: octree_pointcloud.h:72
pcl::octree::OctreeKey::y
uindex_t y
Definition: octree_key.h:148
pcl::octree::OctreePointCloud::addPointsFromInputCloud
void addPointsFromInputCloud()
Add points from input point cloud to octree.
Definition: octree_pointcloud.hpp:79
pcl::octree::OctreeKey
Octree key class
Definition: octree_key.h:52
pcl::PointXYZRGB
A point structure representing Euclidean xyz coordinates, and the RGB color.
Definition: point_types.hpp:674
pcl::octree::OctreePointCloudAdjacency::EdgeID
typename VoxelAdjacencyList::edge_descriptor EdgeID
Definition: octree_pointcloud_adjacency.h:102
pcl::octree::OctreePointCloudAdjacency::addPointsFromInputCloud
void addPointsFromInputCloud()
Adds points from cloud to the octree.
Definition: octree_pointcloud_adjacency.hpp:66
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:121
pcl::octree::OctreeBase
Octree class.
Definition: octree_base.h:62
pcl::PointXYZ
A point structure representing Euclidean xyz coordinates.
Definition: point_types.hpp:346
pcl::octree::OctreeLeafNodeDepthFirstIterator
Octree leaf node iterator class.
Definition: octree_iterator.h:660
pcl::octree::OctreePointCloudAdjacency::OctreePointCloudAdjacency
OctreePointCloudAdjacency(const double resolution_arg)
Constructor.
Definition: octree_pointcloud_adjacency.hpp:55
pcl::octree::OctreeKey::x
uindex_t x
Definition: octree_key.h:147
pcl::octree::OctreePointCloudAdjacency::addPointIdx
void addPointIdx(uindex_t point_idx_arg) override
Add point at index from input pointcloud dataset to octree.
Definition: octree_pointcloud_adjacency.hpp:150
pcl::octree::OctreePointCloudAdjacency::getLeafContainerAtPoint
LeafContainerT * getLeafContainerAtPoint(const PointT &point_arg) const
Gets the leaf container for a given point.
Definition: octree_pointcloud_adjacency.hpp:208
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:271
pcl::octree::OctreePointCloudAdjacency::computeVoxelAdjacencyGraph
void computeVoxelAdjacencyGraph(VoxelAdjacencyList &voxel_adjacency_graph)
Computes an adjacency graph of voxel relations.
Definition: octree_pointcloud_adjacency.hpp:224
pcl::octree::OctreePointCloudAdjacency::VoxelAdjacencyList
boost::adjacency_list< boost::setS, boost::setS, boost::undirectedS, PointT, float > VoxelAdjacencyList
Definition: octree_pointcloud_adjacency.h:100
pcl::uindex_t
detail::int_type_t< detail::index_type_size, false > uindex_t
Type used for an unsigned index in PCL.
Definition: types.h:120