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