00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00037 #ifndef FCL_HIERARCHY_TREE_H
00038 #define FCL_HIERARCHY_TREE_H
00039
00040 #include <vector>
00041 #include <map>
00042 #include "fcl/BV/AABB.h"
00043 #include "fcl/broadphase/morton.h"
00044 #include <boost/bind.hpp>
00045 #include <boost/iterator/zip_iterator.hpp>
00046
00047 namespace fcl
00048 {
00049
00051 template<typename BV>
00052 struct NodeBase
00053 {
00055 BV bv;
00056
00058 NodeBase<BV>* parent;
00059
00061 bool isLeaf() const { return (children[1] == NULL); }
00062
00064 bool isInternal() const { return !isLeaf(); }
00065
00066 union
00067 {
00069 NodeBase<BV>* children[2];
00070 void* data;
00071 };
00072
00074 FCL_UINT32 code;
00075
00076 NodeBase()
00077 {
00078 parent = NULL;
00079 children[0] = NULL;
00080 children[1] = NULL;
00081 }
00082 };
00083
00084
00086 template<typename BV>
00087 bool nodeBaseLess(NodeBase<BV>* a, NodeBase<BV>* b, int d)
00088 {
00089 if(a->bv.center()[d] < b->bv.center()[d]) return true;
00090 return false;
00091 }
00092
00094 template<typename BV>
00095 size_t select(const NodeBase<BV>& query, const NodeBase<BV>& node1, const NodeBase<BV>& node2)
00096 {
00097 return 0;
00098 }
00099
00100 template<>
00101 size_t select(const NodeBase<AABB>& node, const NodeBase<AABB>& node1, const NodeBase<AABB>& node2);
00102
00104 template<typename BV>
00105 size_t select(const BV& query, const NodeBase<BV>& node1, const NodeBase<BV>& node2)
00106 {
00107 return 0;
00108 }
00109
00110 template<>
00111 size_t select(const AABB& query, const NodeBase<AABB>& node1, const NodeBase<AABB>& node2);
00112
00113
00115 template<typename BV>
00116 class HierarchyTree
00117 {
00118 typedef NodeBase<BV> NodeType;
00119 typedef typename std::vector<NodeBase<BV>* >::iterator NodeVecIterator;
00120 typedef typename std::vector<NodeBase<BV>* >::const_iterator NodeVecConstIterator;
00121
00122 struct SortByMorton
00123 {
00124 bool operator() (const NodeType* a, const NodeType* b) const
00125 {
00126 return a->code < b->code;
00127 }
00128 };
00129
00130 public:
00131
00136 HierarchyTree(int bu_threshold_ = 16, int topdown_level_ = 0);
00137
00138 ~HierarchyTree();
00139
00141 void init(std::vector<NodeType*>& leaves, int level = 0);
00142
00144 NodeType* insert(const BV& bv, void* data);
00145
00147 void remove(NodeType* leaf);
00148
00150 void clear();
00151
00153 bool empty() const;
00154
00156 void update(NodeType* leaf, int lookahead_level = -1);
00157
00159 bool update(NodeType* leaf, const BV& bv);
00160
00162 bool update(NodeType* leaf, const BV& bv, const Vec3f& vel, FCL_REAL margin);
00163
00165 bool update(NodeType* leaf, const BV& bv, const Vec3f& vel);
00166
00168 size_t getMaxHeight() const;
00169
00171 size_t getMaxDepth() const;
00172
00174 void balanceBottomup();
00175
00177 void balanceTopdown();
00178
00180 void balanceIncremental(int iterations);
00181
00183 void refit();
00184
00186 void extractLeaves(const NodeType* root, std::vector<NodeType*>& leaves) const;
00187
00189 size_t size() const;
00190
00192 NodeType* getRoot() const;
00193
00194 NodeType*& getRoot();
00195
00197 void print(NodeType* root, int depth);
00198
00199 private:
00200
00202 void bottomup(const NodeVecIterator lbeg, const NodeVecIterator lend);
00203
00205 NodeType* topdown(const NodeVecIterator lbeg, const NodeVecIterator lend);
00206
00208 size_t getMaxHeight(NodeType* node) const;
00209
00211 void getMaxDepth(NodeType* node, size_t depth, size_t& max_depth) const;
00212
00216 NodeType* topdown_0(const NodeVecIterator lbeg, const NodeVecIterator lend);
00217
00222 NodeType* topdown_1(const NodeVecIterator lbeg, const NodeVecIterator lend);
00223
00225 void init_0(std::vector<NodeType*>& leaves);
00226
00229 void init_1(std::vector<NodeType*>& leaves);
00230
00233 void init_2(std::vector<NodeType*>& leaves);
00234
00236 void init_3(std::vector<NodeType*>& leaves);
00237
00238 NodeType* mortonRecurse_0(const NodeVecIterator lbeg, const NodeVecIterator lend, const FCL_UINT32& split, int bits);
00239
00240 NodeType* mortonRecurse_1(const NodeVecIterator lbeg, const NodeVecIterator lend, const FCL_UINT32& split, int bits);
00241
00242 NodeType* mortonRecurse_2(const NodeVecIterator lbeg, const NodeVecIterator lend);
00243
00245 void update_(NodeType* leaf, const BV& bv);
00246
00248 NodeType* sort(NodeType* n, NodeType*& r);
00249
00251 void insertLeaf(NodeType* root, NodeType* leaf);
00252
00255 NodeType* removeLeaf(NodeType* leaf);
00256
00258 void fetchLeaves(NodeType* root, std::vector<NodeType*>& leaves, int depth = -1);
00259
00260 static size_t indexOf(NodeType* node);
00261
00263 NodeType* createNode(NodeType* parent,
00264 const BV& bv,
00265 void* data);
00266
00267 NodeType* createNode(NodeType* parent,
00268 const BV& bv1,
00269 const BV& bv2,
00270 void* data);
00271
00272 NodeType* createNode(NodeType* parent,
00273 void* data);
00274
00275 void deleteNode(NodeType* node);
00276
00277 void recurseDeleteNode(NodeType* node);
00278
00279 void recurseRefit(NodeType* node);
00280
00281 static BV bounds(const std::vector<NodeType*>& leaves);
00282
00283 static BV bounds(const NodeVecIterator lbeg, const NodeVecIterator lend);
00284
00285 protected:
00286 NodeType* root_node;
00287
00288 size_t n_leaves;
00289
00290 unsigned int opath;
00291
00293 NodeType* free_node;
00294
00295 int max_lookahead_level;
00296
00297 public:
00299 int topdown_level;
00300
00302 int bu_threshold;
00303 };
00304
00305
00306 template<>
00307 bool HierarchyTree<AABB>::update(NodeBase<AABB>* leaf, const AABB& bv_, const Vec3f& vel, FCL_REAL margin);
00308
00309 template<>
00310 bool HierarchyTree<AABB>::update(NodeBase<AABB>* leaf, const AABB& bv_, const Vec3f& vel);
00311
00312
00313 namespace implementation_array
00314 {
00315
00316 template<typename BV>
00317 struct NodeBase
00318 {
00319 BV bv;
00320
00321 union
00322 {
00323 size_t parent;
00324 size_t next;
00325 };
00326
00327 union
00328 {
00329 size_t children[2];
00330 void* data;
00331 };
00332
00333 FCL_UINT32 code;
00334
00335 bool isLeaf() const { return (children[1] == (size_t)(-1)); }
00336 bool isInternal() const { return !isLeaf(); }
00337 };
00338
00339
00340
00342 template<typename BV>
00343 struct nodeBaseLess
00344 {
00345 nodeBaseLess(const NodeBase<BV>* nodes_, size_t d_) : nodes(nodes_),
00346 d(d_)
00347 {}
00348
00349 bool operator() (size_t i, size_t j) const
00350 {
00351 if(nodes[i].bv.center()[d] < nodes[j].bv.center()[d])
00352 return true;
00353 return false;
00354 }
00355
00356 private:
00357
00359 const NodeBase<BV>* nodes;
00360
00362 size_t d;
00363 };
00364
00365
00367 template<typename BV>
00368 size_t select(size_t query, size_t node1, size_t node2, NodeBase<BV>* nodes)
00369 {
00370 return 0;
00371 }
00372
00373 template<>
00374 size_t select(size_t query, size_t node1, size_t node2, NodeBase<AABB>* nodes);
00375
00377 template<typename BV>
00378 size_t select(const BV& query, size_t node1, size_t node2, NodeBase<BV>* nodes)
00379 {
00380 return 0;
00381 }
00382
00383 template<>
00384 size_t select(const AABB& query, size_t node1, size_t node2, NodeBase<AABB>* nodes);
00385
00387 template<typename BV>
00388 class HierarchyTree
00389 {
00390 typedef NodeBase<BV> NodeType;
00391
00392 struct SortByMorton
00393 {
00394 bool operator() (size_t a, size_t b) const
00395 {
00396 if((a != NULL_NODE) && (b != NULL_NODE))
00397 return nodes[a].code < nodes[b].code;
00398 else if(a == NULL_NODE)
00399 return split < nodes[b].code;
00400 else if(b == NULL_NODE)
00401 return nodes[a].code < split;
00402
00403 return false;
00404 }
00405
00406 NodeType* nodes;
00407 FCL_UINT32 split;
00408 };
00409
00410 public:
00415 HierarchyTree(int bu_threshold_ = 16, int topdown_level_ = 0);
00416
00417 ~HierarchyTree();
00418
00420 void init(NodeType* leaves, int n_leaves_, int level = 0);
00421
00423 size_t insert(const BV& bv, void* data);
00424
00426 void remove(size_t leaf);
00427
00429 void clear();
00430
00432 bool empty() const;
00433
00435 void update(size_t leaf, int lookahead_level = -1);
00436
00438 bool update(size_t leaf, const BV& bv);
00439
00441 bool update(size_t leaf, const BV& bv, const Vec3f& vel, FCL_REAL margin);
00442
00444 bool update(size_t leaf, const BV& bv, const Vec3f& vel);
00445
00447 size_t getMaxHeight() const;
00448
00450 size_t getMaxDepth() const;
00451
00453 void balanceBottomup();
00454
00456 void balanceTopdown();
00457
00459 void balanceIncremental(int iterations);
00460
00462 void refit();
00463
00465 void extractLeaves(size_t root, NodeType*& leaves) const;
00466
00468 size_t size() const;
00469
00471 size_t getRoot() const;
00472
00474 NodeType* getNodes() const;
00475
00477 void print(size_t root, int depth);
00478
00479 private:
00480
00482 void bottomup(size_t* lbeg, size_t* lend);
00483
00485 size_t topdown(size_t* lbeg, size_t* lend);
00486
00488 size_t getMaxHeight(size_t node) const;
00489
00491 void getMaxDepth(size_t node, size_t depth, size_t& max_depth) const;
00492
00496 size_t topdown_0(size_t* lbeg, size_t* lend);
00497
00502 size_t topdown_1(size_t* lbeg, size_t* lend);
00503
00505 void init_0(NodeType* leaves, int n_leaves_);
00506
00509 void init_1(NodeType* leaves, int n_leaves_);
00510
00513 void init_2(NodeType* leaves, int n_leaves_);
00514
00516 void init_3(NodeType* leaves, int n_leaves_);
00517
00518 size_t mortonRecurse_0(size_t* lbeg, size_t* lend, const FCL_UINT32& split, int bits);
00519
00520 size_t mortonRecurse_1(size_t* lbeg, size_t* lend, const FCL_UINT32& split, int bits);
00521
00522 size_t mortonRecurse_2(size_t* lbeg, size_t* lend);
00523
00525 void update_(size_t leaf, const BV& bv);
00526
00528 void insertLeaf(size_t root, size_t leaf);
00529
00532 size_t removeLeaf(size_t leaf);
00533
00535 void fetchLeaves(size_t root, NodeType*& leaves, int depth = -1);
00536
00537 size_t indexOf(size_t node);
00538
00539 size_t allocateNode();
00540
00542 size_t createNode(size_t parent,
00543 const BV& bv1,
00544 const BV& bv2,
00545 void* data);
00546
00547 size_t createNode(size_t parent,
00548 const BV& bv,
00549 void* data);
00550
00551 size_t createNode(size_t parent,
00552 void* data);
00553
00554 void deleteNode(size_t node);
00555
00556 void recurseRefit(size_t node);
00557
00558 protected:
00559 size_t root_node;
00560 NodeType* nodes;
00561 size_t n_nodes;
00562 size_t n_nodes_alloc;
00563
00564 size_t n_leaves;
00565 size_t freelist;
00566 unsigned int opath;
00567
00568 int max_lookahead_level;
00569
00570 public:
00572 int topdown_level;
00573
00575 int bu_threshold;
00576
00577 public:
00578 static const size_t NULL_NODE = -1;
00579 };
00580
00581 template<typename BV>
00582 const size_t HierarchyTree<BV>::NULL_NODE;
00583
00584 }
00585
00586 }
00587
00588
00589 #include "fcl/broadphase/hierarchy_tree.hxx"
00590
00591
00592 #endif