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