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