Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
octree_pointcloud.h
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) 2010-2011, Willow Garage, 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 Willow Garage, Inc. 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  * $Id: octree_pointcloud.h 6119 2012-07-03 18:50:04Z aichim $
37  */
38 
39 #ifndef OCTREE_POINTCLOUD_H
40 #define OCTREE_POINTCLOUD_H
41 
42 #include "octree_base.h"
43 #include "octree2buf_base.h"
44 
45 #include <pcl/point_cloud.h>
46 #include <pcl/point_types.h>
47 
48 #include "octree_nodes.h"
49 #include "octree_iterator.h"
50 
51 #include <queue>
52 #include <vector>
53 #include <algorithm>
54 #include <iostream>
55 
56 namespace pcl
57 {
58  namespace octree
59  {
60 
62 
75  template<typename PointT, typename LeafT = OctreeContainerDataTVector<int>,
77  typename BranchT = OctreeContainerEmpty<int>,
78  typename OctreeT = OctreeBase<int, LeafT, BranchT> >
79 
80  class OctreePointCloud : public OctreeT
81  {
82  // iterators are friends
83  friend class OctreeIteratorBase<int, OctreeT> ;
84  friend class OctreeDepthFirstIterator<int, OctreeT> ;
85  friend class OctreeBreadthFirstIterator<int, OctreeT> ;
86  friend class OctreeLeafNodeIterator<int, OctreeT> ;
87 
88  public:
89  typedef OctreeT Base;
90 
91  typedef typename OctreeT::LeafNode LeafNode;
92  typedef typename OctreeT::BranchNode BranchNode;
93 
94  // Octree iterators
97 
100 
105 
109  OctreePointCloud (const double resolution_arg);
110 
112  virtual
114 
115  // public typedefs
116  typedef boost::shared_ptr<std::vector<int> > IndicesPtr;
117  typedef boost::shared_ptr<const std::vector<int> > IndicesConstPtr;
118 
120  typedef boost::shared_ptr<PointCloud> PointCloudPtr;
121  typedef boost::shared_ptr<const PointCloud> PointCloudConstPtr;
122 
123  // public typedefs for single/double buffering
126 
127  // Boost shared pointers
128  typedef boost::shared_ptr<OctreePointCloud<PointT, LeafT, OctreeT> > Ptr;
129  typedef boost::shared_ptr<const OctreePointCloud<PointT, LeafT, OctreeT> > ConstPtr;
130 
131  // Eigen aligned allocator
132  typedef std::vector<PointT, Eigen::aligned_allocator<PointT> > AlignedPointTVector;
133 
138  inline void setInputCloud (const PointCloudConstPtr &cloud_arg,
139  const IndicesConstPtr &indices_arg = IndicesConstPtr ())
140  {
141  assert(this->leafCount_==0);
142 
143  input_ = cloud_arg;
144  indices_ = indices_arg;
145  }
146 
150  inline IndicesConstPtr const getIndices () const
151  {
152  return (indices_);
153  }
154 
159  {
160  return (input_);
161  }
162 
166  inline void setEpsilon (double eps)
167  {
168  epsilon_ = eps;
169  }
170 
172  inline double getEpsilon () const
173  {
174  return (epsilon_);
175  }
176 
180  inline void setResolution (double resolution_arg)
181  {
182  // octree needs to be empty to change its resolution
183  assert( this->leafCount_ == 0);
184 
185  resolution_ = resolution_arg;
186 
187  getKeyBitSize ();
188  }
189 
193  inline double getResolution () const
194  {
195  return (resolution_);
196  }
197 
201  inline unsigned int getTreeDepth () const
202  {
203  return this->octreeDepth_;
204  }
205 
207  void
209 
214  void
215  addPointFromCloud (const int pointIdx_arg, IndicesPtr indices_arg);
216 
221  void
222  addPointToCloud (const PointT& point_arg, PointCloudPtr cloud_arg);
223 
229  void
230  addPointToCloud (const PointT& point_arg, PointCloudPtr cloud_arg,
231  IndicesPtr indices_arg);
232 
237  bool
238  isVoxelOccupiedAtPoint (const PointT& point_arg) const;
239 
243  void deleteTree (bool freeMemory_arg = false)
244  {
245  // reset bounding box
246  minX_ = minY_ = maxY_ = minZ_ = maxZ_ = 0;
247  this->boundingBoxDefined_ = false;
248 
249  OctreeT::deleteTree (freeMemory_arg);
250  }
251 
258  bool
259  isVoxelOccupiedAtPoint (const double pointX_arg,
260  const double pointY_arg, const double pointZ_arg) const;
261 
266  bool
267  isVoxelOccupiedAtPoint (const int& pointIdx_arg) const;
268 
273  int
275  AlignedPointTVector &voxelCenterList_arg) const;
276 
287  int
289  const Eigen::Vector3f& origin, const Eigen::Vector3f& end,
290  AlignedPointTVector &voxel_center_list, float precision = 0.2);
291 
295  void
296  deleteVoxelAtPoint (const PointT& point_arg);
297 
301  void
302  deleteVoxelAtPoint (const int& pointIdx_arg);
303 
305  // Bounding box methods
307 
309  void
311 
321  void
322  defineBoundingBox (const double minX_arg, const double minY_arg,
323  const double minZ_arg, const double maxX_arg, const double maxY_arg,
324  const double maxZ_arg);
325 
333  void
334  defineBoundingBox (const double maxX_arg, const double maxY_arg,
335  const double maxZ_arg);
336 
342  void
343  defineBoundingBox (const double cubeLen_arg);
344 
354  void
355  getBoundingBox (double& minX_arg, double& minY_arg, double& minZ_arg,
356  double& maxX_arg, double& maxY_arg, double& maxZ_arg) const;
357 
362  double
363  getVoxelSquaredDiameter (unsigned int treeDepth_arg) const;
364 
368  inline double getVoxelSquaredDiameter () const
369  {
370  return getVoxelSquaredDiameter (this->octreeDepth_);
371  }
372 
377  double
378  getVoxelSquaredSideLen (unsigned int treeDepth_arg) const;
379 
383  inline double getVoxelSquaredSideLen () const
384  {
385  return getVoxelSquaredSideLen (this->octreeDepth_);
386  }
387 
394  Eigen::Vector3f &min_pt, Eigen::Vector3f &max_pt)
395  {
396  this->genVoxelBoundsFromOctreeKey (iterator.getCurrentOctreeKey (),
397  iterator.getCurrentOctreeDepth (), min_pt, max_pt);
398  }
399 
400  protected:
401 
405  void
406  addPointIdx (const int pointIdx_arg);
407 
412  const PointT&
413  getPointByIndex (const unsigned int index_arg) const;
414 
419  LeafT*
420  findLeafAtPoint (const PointT& point_arg) const;
421 
423  // Protected octree methods based on octree keys
425 
427  void
428  getKeyBitSize ();
429 
433  void
434  adoptBoundingBoxToPoint (const PointT& pointIdx_arg);
435 
440  inline bool isPointWithinBoundingBox (const PointT& pointIdx_arg) const
441  {
442  return (! ( (pointIdx_arg.x < minX_) || (pointIdx_arg.y < minY_)
443  || (pointIdx_arg.z < minZ_) || (pointIdx_arg.x >= maxX_)
444  || (pointIdx_arg.y >= maxY_) || (pointIdx_arg.z >= maxZ_)));
445  }
446 
451  void
452  genOctreeKeyforPoint (const PointT & point_arg,
453  OctreeKey &key_arg) const;
454 
461  void
462  genOctreeKeyforPoint (const double pointX_arg, const double pointY_arg,
463  const double pointZ_arg, OctreeKey & key_arg) const;
464 
471  virtual bool
472  genOctreeKeyForDataT (const int& data_arg, OctreeKey & key_arg) const;
473 
478  void
479  genLeafNodeCenterFromOctreeKey (const OctreeKey & key_arg,
480  PointT& point_arg) const;
481 
487  void
488  genVoxelCenterFromOctreeKey (const OctreeKey & key_arg,
489  unsigned int treeDepth_arg, PointT& point_arg) const;
490 
497  void
498  genVoxelBoundsFromOctreeKey (const OctreeKey & key_arg,
499  unsigned int treeDepth_arg, Eigen::Vector3f &min_pt,
500  Eigen::Vector3f &max_pt) const;
501 
508  int
509  getOccupiedVoxelCentersRecursive (const BranchNode* node_arg,
510  const OctreeKey& key_arg,
511  AlignedPointTVector &voxelCenterList_arg) const;
512 
514  // Globals
516 
517  PointCloudConstPtr input_;
518 
520  IndicesConstPtr indices_;
521 
523  double epsilon_;
524 
526  double resolution_;
527 
528  // Octree bounding box coordinates
529  double minX_;
530  double maxX_;
531 
532  double minY_;
533  double maxY_;
534 
535  double minZ_;
536  double maxZ_;
537 
539  bool boundingBoxDefined_;
540  };
541  }
542 }
543 
544 #endif
545