FCL  0.6.0
Flexible Collision Library
hierarchy_tree.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_H
39 #define FCL_HIERARCHY_TREE_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.h"
49 
50 namespace fcl
51 {
52 
53 namespace detail
54 {
55 
57 template<typename BV>
59 {
60 public:
61 
62  using S = typename BV::S;
63 
64  typedef NodeBase<BV> NodeType;
65 
70  HierarchyTree(int bu_threshold_ = 16, int topdown_level_ = 0);
71 
72  ~HierarchyTree();
73 
75  void init(std::vector<NodeType*>& leaves, int level = 0);
76 
78  NodeType* insert(const BV& bv, void* data);
79 
81  void remove(NodeType* leaf);
82 
84  void clear();
85 
87  bool empty() const;
88 
90  void update(NodeType* leaf, int lookahead_level = -1);
91 
93  bool update(NodeType* leaf, const BV& bv);
94 
96  bool update(NodeType* leaf, const BV& bv, const Vector3<S>& vel, S margin);
97 
99  bool update(NodeType* leaf, const BV& bv, const Vector3<S>& vel);
100 
102  size_t getMaxHeight() const;
103 
105  size_t getMaxDepth() const;
106 
108  void balanceBottomup();
109 
111  void balanceTopdown();
112 
114  void balanceIncremental(int iterations);
115 
117  void refit();
118 
120  void extractLeaves(const NodeType* root, std::vector<NodeType*>& leaves) const;
121 
123  size_t size() const;
124 
126  NodeType* getRoot() const;
127 
128  NodeType*& getRoot();
129 
131  void print(NodeType* root, int depth);
132 
133 private:
134 
135  typedef typename std::vector<NodeBase<BV>* >::iterator NodeVecIterator;
136  typedef typename std::vector<NodeBase<BV>* >::const_iterator NodeVecConstIterator;
137 
138  struct SortByMorton
139  {
140  bool operator() (const NodeType* a, const NodeType* b) const
141  {
142  return a->code < b->code;
143  }
144  };
145 
147  void bottomup(const NodeVecIterator lbeg, const NodeVecIterator lend);
148 
150  NodeType* topdown(const NodeVecIterator lbeg, const NodeVecIterator lend);
151 
153  size_t getMaxHeight(NodeType* node) const;
154 
156  void getMaxDepth(NodeType* node, size_t depth, size_t& max_depth) const;
157 
161  NodeType* topdown_0(const NodeVecIterator lbeg, const NodeVecIterator lend);
162 
167  NodeType* topdown_1(const NodeVecIterator lbeg, const NodeVecIterator lend);
168 
170  void init_0(std::vector<NodeType*>& leaves);
171 
174  void init_1(std::vector<NodeType*>& leaves);
175 
178  void init_2(std::vector<NodeType*>& leaves);
179 
181  void init_3(std::vector<NodeType*>& leaves);
182 
183  NodeType* mortonRecurse_0(const NodeVecIterator lbeg, const NodeVecIterator lend, const uint32& split, int bits);
184 
185  NodeType* mortonRecurse_1(const NodeVecIterator lbeg, const NodeVecIterator lend, const uint32& split, int bits);
186 
187  NodeType* mortonRecurse_2(const NodeVecIterator lbeg, const NodeVecIterator lend);
188 
190  void update_(NodeType* leaf, const BV& bv);
191 
193  NodeType* sort(NodeType* n, NodeType*& r);
194 
196  void insertLeaf(NodeType* root, NodeType* leaf);
197 
200  NodeType* removeLeaf(NodeType* leaf);
201 
203  void fetchLeaves(NodeType* root, std::vector<NodeType*>& leaves, int depth = -1);
204 
205  static size_t indexOf(NodeType* node);
206 
208  NodeType* createNode(NodeType* parent,
209  const BV& bv,
210  void* data);
211 
212  NodeType* createNode(NodeType* parent,
213  const BV& bv1,
214  const BV& bv2,
215  void* data);
216 
217  NodeType* createNode(NodeType* parent,
218  void* data);
219 
220  void deleteNode(NodeType* node);
221 
222  void recurseDeleteNode(NodeType* node);
223 
224  void recurseRefit(NodeType* node);
225 
226  static BV bounds(const std::vector<NodeType*>& leaves);
227 
228  static BV bounds(const NodeVecIterator lbeg, const NodeVecIterator lend);
229 
230 protected:
231  NodeType* root_node;
232 
233  size_t n_leaves;
234 
235  unsigned int opath;
236 
238  NodeType* free_node;
239 
240  int max_lookahead_level;
241 
242 public:
245 
248 };
249 
251 template<typename BV>
252 bool nodeBaseLess(NodeBase<BV>* a, NodeBase<BV>* b, int d);
253 
256 template<typename BV>
257 size_t select(
258  const NodeBase<BV>& query,
259  const NodeBase<BV>& node1,
260  const NodeBase<BV>& node2);
261 
264 template<typename BV>
265 size_t select(
266  const BV& query, const NodeBase<BV>& node1, const NodeBase<BV>& node2);
267 
268 } // namespace detail
269 } // namespace fcl
270 
271 #include "fcl/broadphase/detail/hierarchy_tree-inl.h"
272 
273 #endif
dynamic AABB<S> tree node
Definition: node_base.h:51
void clear()
Clear the tree.
Definition: hierarchy_tree-inl.h:113
void extractLeaves(const NodeType *root, std::vector< NodeType * > &leaves) const
extract all the leaves of the tree
Definition: hierarchy_tree-inl.h:279
Main namespace.
Definition: broadphase_bruteforce-inl.h:45
void balanceBottomup()
balance the tree from bottom
Definition: hierarchy_tree-inl.h:222
size_t getMaxDepth() const
get the max depth of the tree
Definition: hierarchy_tree-inl.h:211
void update(NodeType *leaf, int lookahead_level=-1)
update one leaf node
Definition: hierarchy_tree-inl.h:133
size_t getMaxHeight() const
get the max height of the tree
Definition: hierarchy_tree-inl.h:202
void balanceIncremental(int iterations)
balance the tree in an incremental way
Definition: hierarchy_tree-inl.h:249
NodeType * getRoot() const
get the root of the tree
Definition: hierarchy_tree-inl.h:299
uint32 code
morton code for current BV
Definition: node_base.h:73
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-inl.h:271
bool empty() const
Whether the tree is empty.
Definition: hierarchy_tree-inl.h:126
Class for hierarchy tree structure.
Definition: hierarchy_tree.h:58
void init(std::vector< NodeType * > &leaves, int level=0)
Initialize the tree by a set of leaves using algorithm with a given level.
Definition: hierarchy_tree-inl.h:71
size_t size() const
number of leaves in the tree
Definition: hierarchy_tree-inl.h:292
int bu_threshold
decide the depth to use expensive bottom-up algorithm
Definition: hierarchy_tree.h:247
void balanceTopdown()
balance the tree from top
Definition: hierarchy_tree-inl.h:236
NodeType * insert(const BV &bv, void *data)
Insest a node.
Definition: hierarchy_tree-inl.h:94
NodeType * free_node
This is a one NodeType cache, the reason is that we need to remove a node and then add it again frequ...
Definition: hierarchy_tree.h:238
void print(NodeType *root, int depth)
print the tree in a recursive way
Definition: hierarchy_tree-inl.h:313
int topdown_level
decide which topdown algorithm to use
Definition: hierarchy_tree.h:244
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-inl.h:51