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