Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
octree_nodes.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_nodes.h 6119 2012-07-03 18:50:04Z aichim $
37  */
38 
39 #ifndef OCTREE_NODE_H
40 #define OCTREE_NODE_H
41 
42 #include <string>
43 #include <vector>
44 #include <cstddef>
45 
46 #include <assert.h>
47 
48 #include <pcl/pcl_macros.h>
49 
50 using namespace std;
51 
52 namespace pcl
53 {
54  namespace octree
55  {
56 
57  // enum of node types within the octree
59  {
61  };
62 
64 
69  {
70  public:
71 
73  {
74  }
75 
76  virtual
78  {
79  }
81  virtual node_type_t
82  getNodeType () const = 0;
83 
85  virtual OctreeNode*
86  deepCopy () const = 0;
87 
88  };
89 
91 
96  template<typename ContainerT>
97  class OctreeLeafNode : public OctreeNode, public ContainerT
98  {
99  public:
100 
101  using ContainerT::getSize;
102  using ContainerT::getData;
103  using ContainerT::setData;
104 
107  OctreeNode (), ContainerT ()
108  {
109  }
110 
112  OctreeLeafNode (const OctreeLeafNode& source) :
113  OctreeNode (), ContainerT (source)
114  {
115  }
116 
118  virtual
120  {
121  }
122 
125  deepCopy () const
126  {
127  return new OctreeLeafNode<ContainerT> (*this);
128  }
129 
131  virtual node_type_t
132  getNodeType () const
133  {
134  return LEAF_NODE;
135  }
136 
138  inline
139  void
140  reset ()
141  {
142  ContainerT::reset ();
143  }
144 
145  };
146 
148 
152  template<typename ContainerT>
153  class OctreeBranchNode : public OctreeNode, ContainerT
154  {
155  public:
156 
157  using ContainerT::getSize;
158  using ContainerT::getData;
159  using ContainerT::setData;
160 
163  ContainerT ()
164  {
165  // reset pointer to child node vectors
166  memset (childNodeArray_, 0, sizeof(childNodeArray_));
167  }
168 
171  ContainerT (source)
172  {
173  unsigned char i;
174 
175  memset (childNodeArray_, 0, sizeof(childNodeArray_));
176 
177  for (i = 0; i < 8; ++i)
178  if (source.childNodeArray_[i])
179  childNodeArray_[i] = source.childNodeArray_[i]->deepCopy ();
180  }
181 
183  inline OctreeBranchNode&
184  operator = (const OctreeBranchNode &source)
185  {
186  unsigned char i;
187 
188  memset (childNodeArray_, 0, sizeof(childNodeArray_));
189 
190  for (i = 0; i < 8; ++i)
191  if (source.childNodeArray_[i])
192  childNodeArray_[i] = source.childNodeArray_[i]->deepCopy ();
193  return (*this);
194  }
195 
197  virtual OctreeBranchNode*
198  deepCopy () const
199  {
200  return (new OctreeBranchNode<ContainerT> (*this));
201  }
202 
204  virtual
206  {
207  }
208 
209  inline
210  void
211  reset ()
212  {
213  memset (childNodeArray_, 0, sizeof(childNodeArray_));
214  ContainerT::reset ();
215  }
216 
221  inline OctreeNode*&
222  operator[] (unsigned char childIdx_arg)
223  {
224  assert(childIdx_arg < 8);
225  return childNodeArray_[childIdx_arg];
226  }
227 
232  inline OctreeNode*
233  getChildPtr (unsigned char childIdx_arg) const
234  {
235  assert(childIdx_arg < 8);
236  return childNodeArray_[childIdx_arg];
237  }
238 
242  inline void setChildPtr (OctreeNode* child, unsigned char index)
243  {
244  assert(index < 8);
245  childNodeArray_[index] = child;
246  }
247 
248 
253  inline bool hasChild (unsigned char childIdx_arg) const
254  {
255  return (childNodeArray_[childIdx_arg] != 0);
256  }
257 
259  virtual node_type_t
260  getNodeType () const
261  {
262  return BRANCH_NODE;
263  }
264 
265  protected:
266  OctreeNode* childNodeArray_[8];
267  };
268  }
269 }
270 
271 #endif