Point Cloud Library (PCL)  1.3.1
octree_iterator.h
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2010-2011, Willow Garage, Inc.
00006  *
00007  *  All rights reserved.
00008  *
00009  *  Redistribution and use in source and binary forms, with or without
00010  *  modification, are permitted provided that the following conditions
00011  *  are met:
00012  *
00013  *   * Redistributions of source code must retain the above copyright
00014  *     notice, this list of conditions and the following disclaimer.
00015  *   * Redistributions in binary form must reproduce the above
00016  *     copyright notice, this list of conditions and the following
00017  *     disclaimer in the documentation and/or other materials provided
00018  *     with the distribution.
00019  *   * Neither the name of Willow Garage, Inc. nor the names of its
00020  *     contributors may be used to endorse or promote products derived
00021  *     from this software without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  * $Id: octree_iterator.h 3018 2011-11-01 03:24:12Z svn $
00037  */
00038 
00039 #ifndef OCTREE_ITERATOR_H
00040 #define OCTREE_ITERATOR_H
00041 
00042 #include <cstddef>
00043 #include <vector>
00044 
00045 #include "octree_nodes.h"
00046 
00047 #include <iterator>
00048 
00049 namespace pcl
00050 {
00051   namespace octree
00052   {
00054 
00059 
00060 
00061     template<typename DataT, typename LeafT, typename OctreeT>
00062       class OctreeNodeIterator : public std::iterator<std::forward_iterator_tag, const OctreeNode, void,
00063           const OctreeNode*, const OctreeNode&>
00064       {
00065 
00066         // public typedefs
00067         typedef typename OctreeT::OctreeBranch OctreeBranch;
00068         typedef typename OctreeT::OctreeKey OctreeKey;
00069 
00070       public:
00071 
00075         explicit
00076         OctreeNodeIterator (const OctreeT& octree_arg);
00077 
00079         virtual
00080         ~OctreeNodeIterator ();
00081 
00084         inline void
00085         reset ();
00086 
00090         inline const OctreeKey&
00091         getCurrentOctreeKey () const
00092         {
00093           return currentOctreeKey_;
00094         }
00095 
00099         inline
00100         unsigned int
00101         getCurrentOctreeDepth () const
00102         {
00103           return currentOctreeDepth_;
00104         }
00105 
00109         inline const OctreeNode*
00110         getCurrentOctreeNode () const
00111         {
00112           return currentNode_;
00113         }
00114 
00118         inline const OctreeNode*
00119         operator* () const
00120         { // return designated object
00121           return this->getCurrentOctreeNode ();
00122         }
00123 
00127         inline
00128         bool
00129         operator== (const OctreeNodeIterator& right_arg) const
00130         { // test for iterator equality
00131           return ((octree_ == right_arg.octree_) && (currentNode_ == right_arg.currentNode_));
00132         }
00133 
00137         inline
00138         bool
00139         operator!= (const OctreeNodeIterator& right_arg) const
00140         { // test for iterator inequality
00141           return (!(octree_ != right_arg.octree_) || !(currentNode_ != right_arg.currentNode_));
00142         }
00143 
00146         void
00147         skipChildVoxels ();
00148 
00152         OctreeNodeIterator&
00153         operator++ ();
00154 
00158         inline OctreeNodeIterator
00159         operator++ (int)
00160         {
00161           OctreeNodeIterator _Tmp = *this;
00162           ++*this;
00163           return (_Tmp);
00164         }
00165 
00166       protected:
00167 
00168         // reference to octree class
00169         const OctreeT& octree_;
00170 
00171         // pointer to current octree node
00172         const OctreeNode* currentNode_;
00173 
00174         // child index at current octree node
00175         unsigned char currentChildIdx_;
00176 
00177         // depth level in the octree structure
00178         unsigned int currentOctreeDepth_;
00179 
00180         // octree key for current octree node
00181         OctreeKey currentOctreeKey_;
00182 
00183         // stack structure
00184         std::vector<std::pair<OctreeNode const*, unsigned char> > stack_;
00185       };
00186 
00187 
00189 
00194 
00195     template<typename DataT, typename LeafT, typename OctreeT>
00196       class OctreeLeafNodeIterator : public OctreeNodeIterator<DataT, LeafT, OctreeT>
00197       {
00198 
00199       public:
00200 
00204         explicit
00205         OctreeLeafNodeIterator (const OctreeT& octree_arg) :
00206           OctreeNodeIterator<DataT, LeafT, OctreeT> (octree_arg)
00207         {
00208         }
00209 
00211         virtual
00212         ~OctreeLeafNodeIterator ()
00213         {
00214         }
00215 
00219         inline OctreeLeafNodeIterator&
00220         operator++ ()
00221         {
00222           do
00223           {
00224             OctreeNodeIterator<DataT, LeafT, OctreeT>::operator++ ();
00225           } while ((this->currentNode_) && (this->currentNode_->getNodeType () != LEAF_NODE));
00226 
00227           return (*this);
00228         }
00229 
00233         inline OctreeLeafNodeIterator
00234         operator++ (int)
00235         {
00236           OctreeLeafNodeIterator _Tmp = *this;
00237           ++*this;
00238           return (_Tmp);
00239         }
00240 
00244         const LeafT*
00245         operator* () const
00246         { // return designated object
00247           const LeafT* ret = NULL;
00248 
00249           if (this->currentNode_ && (this->currentNode_->getNodeType () == LEAF_NODE))
00250           {
00251             ret = (const LeafT*)this->currentNode_;
00252           }
00253           return ret;
00254         }
00255 
00259         virtual void
00260         getData (const DataT*& data_arg) const
00261         {
00262           const DataT* result = 0;
00263 
00264           if (this->currentNode_ && (this->currentNode_->getNodeType () == LEAF_NODE))
00265           {
00266             const LeafT* leafNode = (const LeafT*)this->currentNode_;
00267             leafNode->getData (result);
00268           }
00269           data_arg = result;
00270         }
00271 
00275         virtual void
00276         getData (std::vector<DataT>& dataVector_arg) const
00277         {
00278           if (this->currentNode_ && (this->currentNode_->getNodeType () == LEAF_NODE))
00279           {
00280             const LeafT* leafNode = (const LeafT*)this->currentNode_;
00281             leafNode->getData (dataVector_arg);
00282           }
00283         }
00284 
00285       };
00286 
00287   }
00288 }
00289 
00290 #endif
00291 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines