Point Cloud Library (PCL)  1.14.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  : 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, must be less than 8
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, must be less than 8
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  * \param index: index to child node, must be less than 8
242  * \return OctreeNode pointer
243  * */
244  inline void
245  setChildPtr(OctreeNode* child, unsigned char index)
246  {
247  assert(index < 8);
248  child_node_array_[index] = child;
249  }
250 
251  /** \brief Check if branch is pointing to a particular child node
252  * \param child_idx_arg: index to child node, must be less than 8
253  * \return "true" if pointer to child node exists; "false" otherwise
254  * */
255  inline bool
256  hasChild(unsigned char child_idx_arg) const
257  {
258  return (child_node_array_[child_idx_arg] != nullptr);
259  }
260 
261  /** \brief Check if branch can be pruned
262  * \note if all children are leaf nodes AND contain identical containers, branch can
263  * be pruned
264  * \return "true" if branch can be pruned; "false" otherwise
265  **/
266  /* inline bool isPrunable () const
267  {
268  const OctreeNode* firstChild = child_node_array_[0];
269  if (!firstChild || firstChild->getNodeType()==BRANCH_NODE)
270  return false;
271 
272  bool prunable = true;
273  for (unsigned char i = 1; i < 8 && prunable; ++i)
274  {
275  const OctreeNode* child = child_node_array_[i];
276  if ( (!child) ||
277  (child->getNodeType()==BRANCH_NODE) ||
278  ((*static_cast<const OctreeContainerBase*>(child)) == (*static_cast<const
279  OctreeContainerBase*>(child)) ) ) prunable = false;
280  }
281 
282  return prunable;
283  }*/
284 
285  /** \brief Get the type of octree node. Returns LEAVE_NODE type */
287  getNodeType() const override
288  {
289  return BRANCH_NODE;
290  }
291 
292  // reset node
293  void
295  {
296  child_node_array_ = {};
297  container_.reset();
298  }
299 
300  /** \brief Get const pointer to container */
301  const ContainerT*
302  operator->() const
303  {
304  return &container_;
305  }
306 
307  /** \brief Get pointer to container */
308  ContainerT*
310  {
311  return &container_;
312  }
313 
314  /** \brief Get const reference to container */
315  const ContainerT&
316  operator*() const
317  {
318  return container_;
319  }
320 
321  /** \brief Get reference to container */
322  ContainerT&
324  {
325  return container_;
326  }
327 
328  /** \brief Get const reference to container */
329  const ContainerT&
330  getContainer() const
331  {
332  return container_;
333  }
334 
335  /** \brief Get reference to container */
336  ContainerT&
338  {
339  return container_;
340  }
341 
342  /** \brief Get const pointer to container */
343  const ContainerT*
345  {
346  return &container_;
347  }
348 
349  /** \brief Get pointer to container */
350  ContainerT*
352  {
353  return &container_;
354  }
355 
356 protected:
357  std::array<OctreeNode*, 8> child_node_array_{};
358 
359  ContainerT container_;
360 };
361 } // namespace octree
362 } // 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:256
const ContainerT & getContainer() const
Get const reference to container.
Definition: octree_nodes.h:330
const ContainerT * getContainerPtr() const
Get const pointer to container.
Definition: octree_nodes.h:344
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:323
ContainerT * getContainerPtr()
Get pointer to container.
Definition: octree_nodes.h:351
ContainerT & getContainer()
Get reference to container.
Definition: octree_nodes.h:337
const ContainerT & operator*() const
Get const reference to container.
Definition: octree_nodes.h:316
const ContainerT * operator->() const
Get const pointer to container.
Definition: octree_nodes.h:302
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:309
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:287
void setChildPtr(OctreeNode *child, unsigned char index)
Get pointer to child.
Definition: octree_nodes.h:245
std::array< OctreeNode *, 8 > child_node_array_
Definition: octree_nodes.h:357
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