Point Cloud Library (PCL)  1.7.1
octree_node_pool.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  */
37 
38 #ifndef PCL_OCTREE_NODE_POOL_H
39 #define PCL_OCTREE_NODE_POOL_H
40 
41 #include <vector>
42 
43 #include <pcl/pcl_macros.h>
44 
45 namespace pcl
46 {
47  namespace octree
48  {
49 
50  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
51  /** \brief @b Octree node pool
52  * \note Used to reduce memory allocation and class instantiation events when generating octrees at high rate
53  * \author Julius Kammerl (julius@kammerl.de)
54  */
55  template<typename NodeT>
57  {
58  public:
59  /** \brief Empty constructor. */
61  nodePool_ ()
62  {
63  }
64 
65  /** \brief Empty deconstructor. */
66  virtual
68  {
69  deletePool ();
70  }
71 
72  /** \brief Push node to pool
73  * \param node_arg: add this node to the pool
74  * */
75  inline
76  void
77  pushNode (NodeT* node_arg)
78  {
79  nodePool_.push_back (node_arg);
80  }
81 
82  /** \brief Pop node from pool - Allocates new nodes if pool is empty
83  * \return Pointer to octree node
84  * */
85  inline NodeT*
87  {
88 
89  NodeT* newLeafNode;
90 
91  if (!nodePool_.size ())
92  {
93  // leaf pool is empty
94  // we need to create a new octree leaf class
95  newLeafNode = new NodeT ();
96  }
97  else
98  {
99  // reuse leaf node from branch pool
100  newLeafNode = nodePool_.back ();
101  nodePool_.pop_back ();
102  newLeafNode->reset ();
103  }
104 
105  return newLeafNode;
106  }
107 
108 
109  /** \brief Delete all nodes in pool
110  * */
111  void
113  {
114  // delete all branch instances from branch pool
115  while (!nodePool_.empty ())
116  {
117  delete (nodePool_.back ());
118  nodePool_.pop_back ();
119  }
120  }
121 
122  protected:
123  std::vector<NodeT*> nodePool_;
124  };
125 
126  }
127 }
128 
129 #endif