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