Point Cloud Library (PCL)  1.9.1-dev
octree_nodes.h
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$
37  */
38 
39 #pragma once
40 
41 #include <cstddef>
42 
43 #include <cassert>
44 
45 #include <Eigen/Core>
46 
47 #include <pcl/pcl_macros.h>
48 
49 #include "octree_container.h"
50 
51 namespace pcl
52 {
53  namespace octree
54  {
55 
56  // enum of node types within the octree
58  {
60  };
61 
62  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
63  /** \brief @b Abstract octree node class
64  * \note Every octree node should implement the getNodeType () method
65  * \author Julius Kammerl (julius@kammerl.de)
66  */
67  class PCL_EXPORTS OctreeNode
68  {
69  public:
70 
72  {
73  }
74 
75  virtual
77  {
78  }
79  /** \brief Pure virtual method for receiving the type of octree node (branch or leaf) */
80  virtual node_type_t
81  getNodeType () const = 0;
82 
83  /** \brief Pure virtual method to perform a deep copy of the octree */
84  virtual OctreeNode*
85  deepCopy () const = 0;
86 
87  };
88 
89  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
90  /** \brief @b Abstract octree leaf class
91  * \note Octree leafs may collect data of type DataT
92  * \author Julius Kammerl (julius@kammerl.de)
93  */
94 
95  template<typename ContainerT>
96  class OctreeLeafNode : public OctreeNode
97  {
98  public:
99 
100  /** \brief Empty constructor. */
102  OctreeNode ()
103  {
104  }
105 
106  /** \brief Copy constructor. */
107  OctreeLeafNode (const OctreeLeafNode& source) :
108  OctreeNode ()
109  {
110  container_ = source.container_;
111  }
112 
113  /** \brief Empty deconstructor. */
114 
116  {
117  }
118 
119  /** \brief Method to perform a deep copy of the octree */
121  deepCopy () const override
122  {
123  return new OctreeLeafNode<ContainerT> (*this);
124  }
125 
126  /** \brief Get the type of octree node. Returns LEAVE_NODE type */
128  getNodeType () const override
129  {
130  return LEAF_NODE;
131  }
132 
133  /** \brief Get const pointer to container */
134  const ContainerT*
135  operator->() const
136  {
137  return &container_;
138  }
139 
140  /** \brief Get pointer to container */
141  ContainerT*
142  operator-> ()
143  {
144  return &container_;
145  }
146 
147  /** \brief Get const reference to container */
148  const ContainerT&
149  operator* () const
150  {
151  return container_;
152  }
153 
154  /** \brief Get reference to container */
155  ContainerT&
156  operator* ()
157  {
158  return container_;
159  }
160 
161  /** \brief Get const reference to container */
162  const ContainerT&
163  getContainer () const
164  {
165  return container_;
166  }
167 
168  /** \brief Get reference to container */
169  ContainerT&
171  {
172  return container_;
173  }
174 
175  /** \brief Get const pointer to container */
176  const ContainerT*
178  {
179  return &container_;
180  }
181 
182  /** \brief Get pointer to container */
183  ContainerT*
185  {
186  return &container_;
187  }
188 
189  protected:
190  ContainerT container_;
191 
192  public:
193  //Type ContainerT may have fixed-size Eigen objects inside
194  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
195  };
196 
197  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
198  /** \brief @b Abstract octree branch class
199  * \note Octree branch classes may collect data of type DataT
200  * \author Julius Kammerl (julius@kammerl.de)
201  */
202  template<typename ContainerT>
204  {
205  public:
206 
207  /** \brief Empty constructor. */
209  OctreeNode()
210  {
211  // reset pointer to child node vectors
212  memset (child_node_array_, 0, sizeof(child_node_array_));
213  }
214 
215  /** \brief Empty constructor. */
217  OctreeNode()
218  {
219  memset (child_node_array_, 0, sizeof(child_node_array_));
220 
221  for (unsigned char i = 0; i < 8; ++i)
222  if (source.child_node_array_[i])
223  child_node_array_[i] = source.child_node_array_[i]->deepCopy ();
224  }
225 
226  /** \brief Copy operator. */
227  inline OctreeBranchNode&
228  operator = (const OctreeBranchNode &source)
229  {
230  memset (child_node_array_, 0, sizeof(child_node_array_));
231 
232  for (unsigned char i = 0; i < 8; ++i)
233  if (source.child_node_array_[i])
234  child_node_array_[i] = source.child_node_array_[i]->deepCopy ();
235  return (*this);
236  }
237 
238  /** \brief Octree deep copy method */
240  deepCopy () const override
241  {
242  return (new OctreeBranchNode<ContainerT> (*this));
243  }
244 
245  /** \brief Empty deconstructor. */
246 
248  {
249  }
250 
251  /** \brief Access operator.
252  * \param child_idx_arg: index to child node
253  * \return OctreeNode pointer
254  * */
255  inline OctreeNode*&
256  operator[] (unsigned char child_idx_arg)
257  {
258  assert(child_idx_arg < 8);
259  return child_node_array_[child_idx_arg];
260  }
261 
262  /** \brief Get pointer to child
263  * \param child_idx_arg: index to child node
264  * \return OctreeNode pointer
265  * */
266  inline OctreeNode*
267  getChildPtr (unsigned char child_idx_arg) const
268  {
269  assert(child_idx_arg < 8);
270  return child_node_array_[child_idx_arg];
271  }
272 
273  /** \brief Get pointer to child
274  * \return OctreeNode pointer
275  * */
276  inline void setChildPtr (OctreeNode* child, unsigned char index)
277  {
278  assert(index < 8);
279  child_node_array_[index] = child;
280  }
281 
282 
283  /** \brief Check if branch is pointing to a particular child node
284  * \param child_idx_arg: index to child node
285  * \return "true" if pointer to child node exists; "false" otherwise
286  * */
287  inline bool hasChild (unsigned char child_idx_arg) const
288  {
289  return (child_node_array_[child_idx_arg] != nullptr);
290  }
291 
292 
293  /** \brief Check if branch can be pruned
294  * \note if all children are leaf nodes AND contain identical containers, branch can be pruned
295  * \return "true" if branch can be pruned; "false" otherwise
296  * */
297  /* inline bool isPrunable () const
298  {
299  const OctreeNode* firstChild = child_node_array_[0];
300  if (!firstChild || firstChild->getNodeType()==BRANCH_NODE)
301  return false;
302 
303  bool prunable = true;
304  for (unsigned char i = 1; i < 8 && prunable; ++i)
305  {
306  const OctreeNode* child = child_node_array_[i];
307  if ( (!child) ||
308  (child->getNodeType()==BRANCH_NODE) ||
309  ((*static_cast<const OctreeContainerBase*>(child)) == (*static_cast<const OctreeContainerBase*>(child)) ) )
310  prunable = false;
311  }
312 
313  return prunable;
314  }*/
315 
316  /** \brief Get the type of octree node. Returns LEAVE_NODE type */
318  getNodeType () const override
319  {
320  return BRANCH_NODE;
321  }
322 
323  // reset node
324  void reset()
325  {
326  memset(child_node_array_, 0, sizeof(child_node_array_));
327  container_.reset();
328  }
329 
330 
331  /** \brief Get const pointer to container */
332  const ContainerT*
333  operator->() const
334  {
335  return &container_;
336  }
337 
338  /** \brief Get pointer to container */
339  ContainerT*
340  operator-> ()
341  {
342  return &container_;
343  }
344 
345  /** \brief Get const reference to container */
346  const ContainerT&
347  operator* () const
348  {
349  return container_;
350  }
351 
352  /** \brief Get reference to container */
353  ContainerT&
354  operator* ()
355  {
356  return container_;
357  }
358 
359  /** \brief Get const reference to container */
360  const ContainerT&
361  getContainer () const
362  {
363  return container_;
364  }
365 
366  /** \brief Get reference to container */
367  ContainerT&
369  {
370  return container_;
371  }
372 
373  /** \brief Get const pointer to container */
374  const ContainerT*
376  {
377  return &container_;
378  }
379 
380  /** \brief Get pointer to container */
381  ContainerT*
383  {
384  return &container_;
385  }
386 
387  protected:
388  OctreeNode* child_node_array_[8];
389 
390  ContainerT container_;
391  };
392  }
393 }
void setChildPtr(OctreeNode *child, unsigned char index)
Get pointer to child.
Definition: octree_nodes.h:276
const ContainerT * operator->() const
Get const pointer to container.
Definition: octree_nodes.h:135
const ContainerT * operator->() const
Get const pointer to container.
Definition: octree_nodes.h:333
~OctreeLeafNode()
Empty deconstructor.
Definition: octree_nodes.h:115
ContainerT * getContainerPtr()
Get pointer to container.
Definition: octree_nodes.h:382
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:44
OctreeNode * child_node_array_[8]
Definition: octree_nodes.h:388
OctreeNode * getChildPtr(unsigned char child_idx_arg) const
Get pointer to child.
Definition: octree_nodes.h:267
OctreeBranchNode * deepCopy() const override
Octree deep copy method.
Definition: octree_nodes.h:240
~OctreeBranchNode()
Empty deconstructor.
Definition: octree_nodes.h:247
ContainerT & getContainer()
Get reference to container.
Definition: octree_nodes.h:368
bool hasChild(unsigned char child_idx_arg) const
Check if branch is pointing to a particular child node.
Definition: octree_nodes.h:287
const ContainerT * getContainerPtr() const
Get const pointer to container.
Definition: octree_nodes.h:375
node_type_t getNodeType() const override
Get the type of octree node.
Definition: octree_nodes.h:128
const ContainerT * getContainerPtr() const
Get const pointer to container.
Definition: octree_nodes.h:177
ContainerT * getContainerPtr()
Get pointer to container.
Definition: octree_nodes.h:184
OctreeBranchNode(const OctreeBranchNode &source)
Empty constructor.
Definition: octree_nodes.h:216
node_type_t getNodeType() const override
Check if branch can be pruned.
Definition: octree_nodes.h:318
virtual OctreeNode * deepCopy() const =0
Pure virtual method to perform a deep copy of the octree.
Abstract octree leaf class
Definition: octree_nodes.h:96
OctreeLeafNode()
Empty constructor.
Definition: octree_nodes.h:101
const ContainerT & getContainer() const
Get const reference to container.
Definition: octree_nodes.h:361
OctreeBranchNode()
Empty constructor.
Definition: octree_nodes.h:208
OctreeLeafNode< ContainerT > * deepCopy() const override
Method to perform a deep copy of the octree.
Definition: octree_nodes.h:121
OctreeLeafNode(const OctreeLeafNode &source)
Copy constructor.
Definition: octree_nodes.h:107
Abstract octree branch class
Definition: octree_nodes.h:203
const ContainerT & getContainer() const
Get const reference to container.
Definition: octree_nodes.h:163
Abstract octree node class
Definition: octree_nodes.h:67
ContainerT & getContainer()
Get reference to container.
Definition: octree_nodes.h:170