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