FCL  0.6.0
Flexible Collision Library
hierarchy_tree_array.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011-2014, Willow Garage, Inc.
5  * Copyright (c) 2014-2016, Open Source Robotics Foundation
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of Open Source Robotics Foundation nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
38 #ifndef FCL_HIERARCHY_TREE_ARRAY_H
39 #define FCL_HIERARCHY_TREE_ARRAY_H
40 
41 #include <vector>
42 #include <map>
43 #include <functional>
44 #include <iostream>
45 #include "fcl/common/warning.h"
46 #include "fcl/math/bv/AABB.h"
47 #include "fcl/broadphase/detail/morton.h"
48 #include "fcl/broadphase/detail/node_base_array.h"
49 
50 namespace fcl
51 {
52 
53 namespace detail
54 {
55 
56 namespace implementation_array
57 {
58 
60 template<typename BV>
62 {
63  using S = typename BV::S;
64  typedef NodeBase<BV> NodeType;
65 
66  struct SortByMorton
67  {
68  bool operator() (size_t a, size_t b) const
69  {
70  if((a != NULL_NODE) && (b != NULL_NODE))
71  return nodes[a].code < nodes[b].code;
72  else if(a == NULL_NODE)
73  return split < nodes[b].code;
74  else if(b == NULL_NODE)
75  return nodes[a].code < split;
76 
77  return false;
78  }
79 
80  NodeType* nodes;
81  uint32 split;
82  };
83 
84 public:
89  HierarchyTree(int bu_threshold_ = 16, int topdown_level_ = 0);
90 
91  ~HierarchyTree();
92 
94  void init(NodeType* leaves, int n_leaves_, int level = 0);
95 
97  size_t insert(const BV& bv, void* data);
98 
100  void remove(size_t leaf);
101 
103  void clear();
104 
106  bool empty() const;
107 
109  void update(size_t leaf, int lookahead_level = -1);
110 
112  bool update(size_t leaf, const BV& bv);
113 
115  bool update(size_t leaf, const BV& bv, const Vector3<S>& vel, S margin);
116 
118  bool update(size_t leaf, const BV& bv, const Vector3<S>& vel);
119 
121  size_t getMaxHeight() const;
122 
124  size_t getMaxDepth() const;
125 
127  void balanceBottomup();
128 
130  void balanceTopdown();
131 
133  void balanceIncremental(int iterations);
134 
136  void refit();
137 
139  void extractLeaves(size_t root, NodeType*& leaves) const;
140 
142  size_t size() const;
143 
145  size_t getRoot() const;
146 
148  NodeType* getNodes() const;
149 
151  void print(size_t root, int depth);
152 
153 private:
154 
156  void bottomup(size_t* lbeg, size_t* lend);
157 
159  size_t topdown(size_t* lbeg, size_t* lend);
160 
162  size_t getMaxHeight(size_t node) const;
163 
165  void getMaxDepth(size_t node, size_t depth, size_t& max_depth) const;
166 
170  size_t topdown_0(size_t* lbeg, size_t* lend);
171 
176  size_t topdown_1(size_t* lbeg, size_t* lend);
177 
179  void init_0(NodeType* leaves, int n_leaves_);
180 
183  void init_1(NodeType* leaves, int n_leaves_);
184 
187  void init_2(NodeType* leaves, int n_leaves_);
188 
190  void init_3(NodeType* leaves, int n_leaves_);
191 
192  size_t mortonRecurse_0(size_t* lbeg, size_t* lend, const uint32& split, int bits);
193 
194  size_t mortonRecurse_1(size_t* lbeg, size_t* lend, const uint32& split, int bits);
195 
196  size_t mortonRecurse_2(size_t* lbeg, size_t* lend);
197 
199  void update_(size_t leaf, const BV& bv);
200 
202  void insertLeaf(size_t root, size_t leaf);
203 
206  size_t removeLeaf(size_t leaf);
207 
209  void fetchLeaves(size_t root, NodeType*& leaves, int depth = -1);
210 
211  size_t indexOf(size_t node);
212 
213  size_t allocateNode();
214 
216  size_t createNode(size_t parent,
217  const BV& bv1,
218  const BV& bv2,
219  void* data);
220 
221  size_t createNode(size_t parent,
222  const BV& bv,
223  void* data);
224 
225  size_t createNode(size_t parent,
226  void* data);
227 
228  void deleteNode(size_t node);
229 
230  void recurseRefit(size_t node);
231 
232 protected:
233  size_t root_node;
234  NodeType* nodes;
235  size_t n_nodes;
236  size_t n_nodes_alloc;
237 
238  size_t n_leaves;
239  size_t freelist;
240  unsigned int opath;
241 
242  int max_lookahead_level;
243 
244 public:
247 
250 
251 public:
252  static const size_t NULL_NODE = -1;
253 };
254 
255 template<typename BV>
256 const size_t HierarchyTree<BV>::NULL_NODE;
257 
258 
260 template<typename BV>
262 {
263  nodeBaseLess(const NodeBase<BV>* nodes_, size_t d_);
264 
265  bool operator() (size_t i, size_t j) const;
266 
267 private:
268 
270  const NodeBase<BV>* nodes;
271 
273  size_t d;
274 };
275 
278 template<typename BV>
279 size_t select(size_t query, size_t node1, size_t node2, NodeBase<BV>* nodes);
280 
283 template<typename BV>
284 size_t select(const BV& query, size_t node1, size_t node2, NodeBase<BV>* nodes);
285 
286 } // namespace implementation_array
287 } // namespace detail
288 } // namespace fcl
289 
290 #include "fcl/broadphase/detail/hierarchy_tree_array-inl.h"
291 
292 #endif
Class for hierarchy tree structure.
Definition: hierarchy_tree_array.h:61
bool empty() const
Whether the tree is empty.
Definition: hierarchy_tree_array-inl.h:309
Main namespace.
Definition: broadphase_bruteforce-inl.h:45
void clear()
Clear the tree.
Definition: hierarchy_tree_array-inl.h:291
void print(size_t root, int depth)
print the tree in a recursive way
Definition: hierarchy_tree_array-inl.h:503
int topdown_level
decide which topdown algorithm to use
Definition: hierarchy_tree_array.h:246
HierarchyTree(int bu_threshold_=16, int topdown_level_=0)
Create hierarchy tree with suitable setting. bu_threshold decides the height of tree node to start bo...
Definition: hierarchy_tree_array-inl.h:54
void refit()
refit the tree, i.e., when the leaf nodes&#39; bounding volumes change, update the entire tree in a botto...
Definition: hierarchy_tree_array-inl.h:458
size_t getMaxDepth() const
get the max depth of the tree
Definition: hierarchy_tree_array-inl.h:370
size_t getRoot() const
get the root of the tree
Definition: hierarchy_tree_array-inl.h:489
size_t insert(const BV &bv, void *data)
Initialize the tree by a set of leaves using algorithm with a given level.
Definition: hierarchy_tree_array-inl.h:272
void balanceBottomup()
balance the tree from bottom
Definition: hierarchy_tree_array-inl.h:381
NodeType * getNodes() const
get the pointer to the nodes array
Definition: hierarchy_tree_array-inl.h:496
Definition: node_base_array.h:53
int bu_threshold
decide the depth to use expensive bottom-up algorithm
Definition: hierarchy_tree_array.h:249
void balanceTopdown()
balance the tree from top
Definition: hierarchy_tree_array-inl.h:410
void init(NodeType *leaves, int n_leaves_, int level=0)
Initialize the tree by a set of leaves using algorithm with a given level.
Definition: hierarchy_tree_array-inl.h:80
size_t getMaxHeight() const
get the max height of the tree
Definition: hierarchy_tree_array-inl.h:361
void update(size_t leaf, int lookahead_level=-1)
update one leaf node
Definition: hierarchy_tree_array-inl.h:316
Functor comparing two nodes.
Definition: hierarchy_tree_array.h:261
void balanceIncremental(int iterations)
balance the tree in an incremental way
Definition: hierarchy_tree_array-inl.h:436
size_t size() const
number of leaves in the tree
Definition: hierarchy_tree_array-inl.h:482
void extractLeaves(size_t root, NodeType *&leaves) const
extract all the leaves of the tree
Definition: hierarchy_tree_array-inl.h:466