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