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