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 |
|
|
|
36 |
|
|
/** @author Jia Pan */ |
37 |
|
|
|
38 |
|
|
#ifndef HPP_FCL_HIERARCHY_TREE_H |
39 |
|
|
#define HPP_FCL_HIERARCHY_TREE_H |
40 |
|
|
|
41 |
|
|
#include <vector> |
42 |
|
|
#include <map> |
43 |
|
|
#include <functional> |
44 |
|
|
#include <iostream> |
45 |
|
|
#include "hpp/fcl/warning.hh" |
46 |
|
|
#include "hpp/fcl/BV/AABB.h" |
47 |
|
|
#include "hpp/fcl/broadphase/detail/morton.h" |
48 |
|
|
#include "hpp/fcl/broadphase/detail/node_base.h" |
49 |
|
|
|
50 |
|
|
namespace hpp { |
51 |
|
|
namespace fcl { |
52 |
|
|
|
53 |
|
|
namespace detail { |
54 |
|
|
|
55 |
|
|
/// @brief Class for hierarchy tree structure |
56 |
|
|
template <typename BV> |
57 |
|
|
class HierarchyTree { |
58 |
|
|
public: |
59 |
|
|
typedef NodeBase<BV> Node; |
60 |
|
|
|
61 |
|
|
/// @brief Create hierarchy tree with suitable setting. |
62 |
|
|
/// bu_threshold decides the height of tree node to start bottom-up |
63 |
|
|
/// construction / optimization; topdown_level decides different methods to |
64 |
|
|
/// construct tree in topdown manner. lower level method constructs tree with |
65 |
|
|
/// better quality but is slower. |
66 |
|
|
HierarchyTree(int bu_threshold_ = 16, int topdown_level_ = 0); |
67 |
|
|
|
68 |
|
|
~HierarchyTree(); |
69 |
|
|
|
70 |
|
|
/// @brief Initialize the tree by a set of leaves using algorithm with a given |
71 |
|
|
/// level. |
72 |
|
|
void init(std::vector<Node*>& leaves, int level = 0); |
73 |
|
|
|
74 |
|
|
/// @brief Insest a node |
75 |
|
|
Node* insert(const BV& bv, void* data); |
76 |
|
|
|
77 |
|
|
/// @brief Remove a leaf node |
78 |
|
|
void remove(Node* leaf); |
79 |
|
|
|
80 |
|
|
/// @brief Clear the tree |
81 |
|
|
void clear(); |
82 |
|
|
|
83 |
|
|
/// @brief Whether the tree is empty |
84 |
|
|
bool empty() const; |
85 |
|
|
|
86 |
|
|
/// @brief Updates a `leaf` node. A use case is when the bounding volume |
87 |
|
|
/// of an object changes. Ensure every parent node has its bounding volume |
88 |
|
|
/// expand or shrink to fit the bounding volumes of its children. |
89 |
|
|
/// @note Strangely the structure of the tree may change even if the |
90 |
|
|
/// bounding volume of the `leaf` node does not change. It is just |
91 |
|
|
/// another valid tree of the same set of objects. This is because |
92 |
|
|
/// update() works by first removing `leaf` and then inserting `leaf` |
93 |
|
|
/// back. The structural change could be as simple as switching the |
94 |
|
|
/// order of two leaves if the sibling of the `leaf` is also a leaf. |
95 |
|
|
/// Or it could be more complicated if the sibling is an internal |
96 |
|
|
/// node. |
97 |
|
|
void update(Node* leaf, int lookahead_level = -1); |
98 |
|
|
|
99 |
|
|
/// @brief update the tree when the bounding volume of a given leaf has |
100 |
|
|
/// changed |
101 |
|
|
bool update(Node* leaf, const BV& bv); |
102 |
|
|
|
103 |
|
|
/// @brief update one leaf's bounding volume, with prediction |
104 |
|
|
bool update(Node* leaf, const BV& bv, const Vec3f& vel, FCL_REAL margin); |
105 |
|
|
|
106 |
|
|
/// @brief update one leaf's bounding volume, with prediction |
107 |
|
|
bool update(Node* leaf, const BV& bv, const Vec3f& vel); |
108 |
|
|
|
109 |
|
|
/// @brief get the max height of the tree |
110 |
|
|
size_t getMaxHeight() const; |
111 |
|
|
|
112 |
|
|
/// @brief get the max depth of the tree |
113 |
|
|
size_t getMaxDepth() const; |
114 |
|
|
|
115 |
|
|
/// @brief balance the tree from bottom |
116 |
|
|
void balanceBottomup(); |
117 |
|
|
|
118 |
|
|
/// @brief balance the tree from top |
119 |
|
|
void balanceTopdown(); |
120 |
|
|
|
121 |
|
|
/// @brief balance the tree in an incremental way |
122 |
|
|
void balanceIncremental(int iterations); |
123 |
|
|
|
124 |
|
|
/// @brief refit the tree, i.e., when the leaf nodes' bounding volumes change, |
125 |
|
|
/// update the entire tree in a bottom-up manner |
126 |
|
|
void refit(); |
127 |
|
|
|
128 |
|
|
/// @brief extract all the leaves of the tree |
129 |
|
|
void extractLeaves(const Node* root, std::vector<Node*>& leaves) const; |
130 |
|
|
|
131 |
|
|
/// @brief number of leaves in the tree |
132 |
|
|
size_t size() const; |
133 |
|
|
|
134 |
|
|
/// @brief get the root of the tree |
135 |
|
|
Node* getRoot() const; |
136 |
|
|
|
137 |
|
|
Node*& getRoot(); |
138 |
|
|
|
139 |
|
|
/// @brief print the tree in a recursive way |
140 |
|
|
void print(Node* root, int depth); |
141 |
|
|
|
142 |
|
|
private: |
143 |
|
|
typedef typename std::vector<NodeBase<BV>*>::iterator NodeVecIterator; |
144 |
|
|
typedef |
145 |
|
|
typename std::vector<NodeBase<BV>*>::const_iterator NodeVecConstIterator; |
146 |
|
|
|
147 |
|
|
struct SortByMorton { |
148 |
|
209604 |
bool operator()(const Node* a, const Node* b) const { |
149 |
|
209604 |
return a->code < b->code; |
150 |
|
|
} |
151 |
|
|
}; |
152 |
|
|
|
153 |
|
|
/// @brief construct a tree for a set of leaves from bottom -- very heavy way |
154 |
|
|
void bottomup(const NodeVecIterator lbeg, const NodeVecIterator lend); |
155 |
|
|
|
156 |
|
|
/// @brief construct a tree for a set of leaves from top |
157 |
|
|
Node* topdown(const NodeVecIterator lbeg, const NodeVecIterator lend); |
158 |
|
|
|
159 |
|
|
/// @brief compute the maximum height of a subtree rooted from a given node |
160 |
|
|
size_t getMaxHeight(Node* node) const; |
161 |
|
|
|
162 |
|
|
/// @brief compute the maximum depth of a subtree rooted from a given node |
163 |
|
|
void getMaxDepth(Node* node, size_t depth, size_t& max_depth) const; |
164 |
|
|
|
165 |
|
|
/// @brief construct a tree from a list of nodes stored in [lbeg, lend) in a |
166 |
|
|
/// topdown manner. During construction, first compute the best split axis as |
167 |
|
|
/// the axis along with the longest AABB edge. Then compute the median of all |
168 |
|
|
/// nodes' center projection onto the axis and using it as the split |
169 |
|
|
/// threshold. |
170 |
|
|
Node* topdown_0(const NodeVecIterator lbeg, const NodeVecIterator lend); |
171 |
|
|
|
172 |
|
|
/// @brief construct a tree from a list of nodes stored in [lbeg, lend) in a |
173 |
|
|
/// topdown manner. During construction, first compute the best split |
174 |
|
|
/// thresholds for different axes as the average of all nodes' center. Then |
175 |
|
|
/// choose the split axis as the axis whose threshold can divide the nodes |
176 |
|
|
/// into two parts with almost similar size. This construction is more |
177 |
|
|
/// expensive then topdown_0, but also can provide tree with better quality. |
178 |
|
|
Node* topdown_1(const NodeVecIterator lbeg, const NodeVecIterator lend); |
179 |
|
|
|
180 |
|
|
/// @brief init tree from leaves in the topdown manner (topdown_0 or |
181 |
|
|
/// topdown_1) |
182 |
|
|
void init_0(std::vector<Node*>& leaves); |
183 |
|
|
|
184 |
|
|
/// @brief init tree from leaves using morton code. It uses morton_0, i.e., |
185 |
|
|
/// for nodes which is of depth more than the maximum bits of the morton code, |
186 |
|
|
/// we use bottomup method to construct the subtree, which is slow but can |
187 |
|
|
/// construct tree with high quality. |
188 |
|
|
void init_1(std::vector<Node*>& leaves); |
189 |
|
|
|
190 |
|
|
/// @brief init tree from leaves using morton code. It uses morton_0, i.e., |
191 |
|
|
/// for nodes which is of depth more than the maximum bits of the morton code, |
192 |
|
|
/// we split the leaves into two parts with the same size simply using the |
193 |
|
|
/// node index. |
194 |
|
|
void init_2(std::vector<Node*>& leaves); |
195 |
|
|
|
196 |
|
|
/// @brief init tree from leaves using morton code. It uses morton_2, i.e., |
197 |
|
|
/// for all nodes, we simply divide the leaves into parts with the same size |
198 |
|
|
/// simply using the node index. |
199 |
|
|
void init_3(std::vector<Node*>& leaves); |
200 |
|
|
|
201 |
|
|
Node* mortonRecurse_0(const NodeVecIterator lbeg, const NodeVecIterator lend, |
202 |
|
|
const uint32_t& split, int bits); |
203 |
|
|
|
204 |
|
|
Node* mortonRecurse_1(const NodeVecIterator lbeg, const NodeVecIterator lend, |
205 |
|
|
const uint32_t& split, int bits); |
206 |
|
|
|
207 |
|
|
Node* mortonRecurse_2(const NodeVecIterator lbeg, const NodeVecIterator lend); |
208 |
|
|
|
209 |
|
|
/// @brief update one leaf node's bounding volume |
210 |
|
|
void update_(Node* leaf, const BV& bv); |
211 |
|
|
|
212 |
|
|
/// @brief sort node n and its parent according to their memory position |
213 |
|
|
Node* sort(Node* n, Node*& r); |
214 |
|
|
|
215 |
|
|
/// @brief Insert a leaf node and also update its ancestors. Maintain the |
216 |
|
|
/// tree as a full binary tree (every interior node has exactly two children). |
217 |
|
|
/// Furthermore, adjust the BV of interior nodes so that each parent's BV |
218 |
|
|
/// tightly fits its children's BVs. |
219 |
|
|
/// @param sub_root The root of the subtree into which we will insert the |
220 |
|
|
// leaf node. |
221 |
|
|
void insertLeaf(Node* const sub_root, Node* const leaf); |
222 |
|
|
|
223 |
|
|
/// @brief Remove a leaf. Maintain the tree as a full binary tree (every |
224 |
|
|
/// interior node has exactly two children). Furthermore, adjust the BV of |
225 |
|
|
/// interior nodes so that each parent's BV tightly fits its children's BVs. |
226 |
|
|
/// @note The leaf node itself is not deleted yet, but all the unnecessary |
227 |
|
|
/// internal nodes are deleted. |
228 |
|
|
/// @returns the root of the subtree containing the nodes whose BVs were |
229 |
|
|
// adjusted. |
230 |
|
|
Node* removeLeaf(Node* const leaf); |
231 |
|
|
|
232 |
|
|
/// @brief Delete all internal nodes and return all leaves nodes with given |
233 |
|
|
/// depth from root |
234 |
|
|
void fetchLeaves(Node* root, std::vector<Node*>& leaves, int depth = -1); |
235 |
|
|
|
236 |
|
|
static size_t indexOf(Node* node); |
237 |
|
|
|
238 |
|
|
/// @brief create one node (leaf or internal) |
239 |
|
|
Node* createNode(Node* parent, const BV& bv, void* data); |
240 |
|
|
|
241 |
|
|
Node* createNode(Node* parent, const BV& bv1, const BV& bv2, void* data); |
242 |
|
|
|
243 |
|
|
Node* createNode(Node* parent, void* data); |
244 |
|
|
|
245 |
|
|
void deleteNode(Node* node); |
246 |
|
|
|
247 |
|
|
void recurseDeleteNode(Node* node); |
248 |
|
|
|
249 |
|
|
void recurseRefit(Node* node); |
250 |
|
|
|
251 |
|
|
protected: |
252 |
|
|
Node* root_node; |
253 |
|
|
|
254 |
|
|
size_t n_leaves; |
255 |
|
|
|
256 |
|
|
unsigned int opath; |
257 |
|
|
|
258 |
|
|
/// This is a one Node cache, the reason is that we need to remove a node and |
259 |
|
|
/// then add it again frequently. |
260 |
|
|
Node* free_node; |
261 |
|
|
|
262 |
|
|
int max_lookahead_level; |
263 |
|
|
|
264 |
|
|
public: |
265 |
|
|
/// @brief decide which topdown algorithm to use |
266 |
|
|
int topdown_level; |
267 |
|
|
|
268 |
|
|
/// @brief decide the depth to use expensive bottom-up algorithm |
269 |
|
|
int bu_threshold; |
270 |
|
|
}; |
271 |
|
|
|
272 |
|
|
/// @brief Compare two nodes accoording to the d-th dimension of node center |
273 |
|
|
template <typename BV> |
274 |
|
|
bool nodeBaseLess(NodeBase<BV>* a, NodeBase<BV>* b, int d); |
275 |
|
|
|
276 |
|
|
/// @brief select from node1 and node2 which is close to a given query. 0 for |
277 |
|
|
/// node1 and 1 for node2 |
278 |
|
|
template <typename BV> |
279 |
|
|
size_t select(const NodeBase<BV>& query, const NodeBase<BV>& node1, |
280 |
|
|
const NodeBase<BV>& node2); |
281 |
|
|
|
282 |
|
|
/// @brief select from node1 and node2 which is close to a given query bounding |
283 |
|
|
/// volume. 0 for node1 and 1 for node2 |
284 |
|
|
template <typename BV> |
285 |
|
|
size_t select(const BV& query, const NodeBase<BV>& node1, |
286 |
|
|
const NodeBase<BV>& node2); |
287 |
|
|
|
288 |
|
|
} // namespace detail |
289 |
|
|
} // namespace fcl |
290 |
|
|
} // namespace hpp |
291 |
|
|
|
292 |
|
|
#include "hpp/fcl/broadphase/detail/hierarchy_tree-inl.h" |
293 |
|
|
|
294 |
|
|
#endif |