38 #ifndef FCL_INTERVAL_TREE_INL_H 39 #define FCL_INTERVAL_TREE_INL_H 41 #include "fcl/broadphase/detail/interval_tree.h" 50 class IntervalTree<double>;
57 nil->left = nil->right = nil->parent = nil;
59 nil->key = nil->high = nil->max_high = -std::numeric_limits<double>::max();
60 nil->stored_interval =
nullptr;
63 root->parent = root->left = root->right = nil;
64 root->key = root->high = root->max_high = std::numeric_limits<double>::max();
66 root->stored_interval =
nullptr;
69 recursion_node_stack_size = 128;
71 recursion_node_stack_top = 1;
72 recursion_node_stack[0].start_node =
nullptr;
80 std::deque<IntervalTreeNode<S>*> nodes_to_free;
86 nodes_to_free.push_back(x->left);
90 nodes_to_free.push_back(x->right);
94 while( nodes_to_free.size() > 0)
96 x = nodes_to_free.back();
97 nodes_to_free.pop_back();
100 nodes_to_free.push_back(x->left);
104 nodes_to_free.push_back(x->right);
111 free(recursion_node_stack);
115 template <
typename S>
123 if(y->left != nil) y->left->parent = x;
125 y->parent = x->parent;
127 if(x == x->parent->left)
130 x->parent->right = y;
135 x->max_high = std::max(x->left->max_high, std::max(x->right->max_high, x->high));
136 y->max_high = std::max(x->max_high, std::max(y->right->max_high, y->high));
140 template <
typename S>
148 if(nil != x->right) x->right->parent = y;
150 x->parent = y->parent;
151 if(y == y->parent->left)
154 y->parent->right = x;
159 y->max_high = std::max(y->left->max_high, std::max(y->right->max_high, y->high));
160 x->max_high = std::max(x->left->max_high, std::max(y->max_high, x->high));
164 template <
typename S>
170 z->left = z->right = nil;
182 if((y == root) || (y->key > z->key))
189 template <
typename S>
194 x->max_high = std::max(x->high, std::max(x->left->max_high, x->right->max_high));
200 template <
typename S>
209 fixupMaxHigh(x->parent);
212 while(x->parent->
red)
215 if(x->parent == x->parent->parent->left)
217 y = x->parent->parent->right;
220 x->parent->
red =
true;
222 x->parent->parent->
red =
true;
223 x = x->parent->parent;
227 if(x == x->parent->right)
232 x->parent->
red =
false;
233 x->parent->parent->
red =
true;
234 rightRotate(x->parent->parent);
239 y = x->parent->parent->left;
242 x->parent->
red =
false;
244 x->parent->parent->
red =
true;
245 x = x->parent->parent;
249 if(x == x->parent->left)
254 x->parent->
red =
false;
255 x->parent->parent->
red =
true;
256 leftRotate(x->parent->parent);
260 root->left->red =
false;
265 template <
typename S>
270 if(nil != (y = x->right))
272 while(y->left != nil)
284 if(y == root)
return nil;
290 template <
typename S>
295 if(nil != (y = x->left))
297 while(y->right != nil)
306 if(y == root)
return nil;
315 template <
typename S>
320 recursivePrint(x->left);
322 recursivePrint(x->right);
327 template <
typename S>
330 recursivePrint(root->left);
334 template <
typename S>
340 while((!x->
red) && (root_left_node != x))
342 if(x == x->parent->left)
344 w = x->parent->right;
348 x->parent->
red =
true;
349 leftRotate(x->parent);
350 w = x->parent->right;
352 if((!w->right->
red) && (!w->left->
red))
361 w->left->
red =
false;
364 w = x->parent->right;
367 x->parent->
red =
false;
368 w->right->
red =
false;
369 leftRotate(x->parent);
379 x->parent->
red =
true;
380 rightRotate(x->parent);
383 if((!w->right->
red) && (!w->left->
red))
392 w->right->
red =
false;
398 x->parent->
red =
false;
399 w->left->
red =
false;
400 rightRotate(x->parent);
409 template <
typename S>
418 template <
typename S>
427 if(left != nil)
return left;
429 if(right != nil)
return right;
436 template <
typename S>
443 y= ((z->left == nil) || (z->right == nil)) ? z : getSuccessor(z);
444 x= (y->left == nil) ? y->right : y->left;
445 if(root == (x->parent = y->parent))
451 if(y == y->parent->left)
457 y->parent->right = x;
465 y->max_high = -std::numeric_limits<double>::max();
468 y->parent = z->parent;
469 z->left->parent = z->right->parent = y;
470 if(z == z->parent->left)
473 z->parent->right = y;
475 fixupMaxHigh(x->parent);
487 fixupMaxHigh(x->parent);
488 if(!(y->
red)) deleteFixup(x);
492 return node_to_delete;
497 template <
typename S>
498 bool overlap(S a1, S a2, S b1, S b2)
511 template <
typename S>
514 std::deque<SimpleInterval<S>*> result_stack;
516 bool run = (x != nil);
522 if(
overlap(low,high,x->key,x->high))
525 recursion_node_stack[current_parent].try_right_branch =
true;
527 if(x->left->max_high >= low)
529 if(recursion_node_stack_top == recursion_node_stack_size)
531 recursion_node_stack_size *= 2;
533 if(recursion_node_stack ==
nullptr)
536 recursion_node_stack[recursion_node_stack_top].start_node = x;
537 recursion_node_stack[recursion_node_stack_top].try_right_branch =
false;
538 recursion_node_stack[recursion_node_stack_top].parent_index = current_parent;
539 current_parent = recursion_node_stack_top++;
546 while((!run) && (recursion_node_stack_top > 1))
548 if(recursion_node_stack[--recursion_node_stack_top].try_right_branch)
550 x=recursion_node_stack[recursion_node_stack_top].start_node->right;
551 current_parent=recursion_node_stack[recursion_node_stack_top].parent_index;
552 recursion_node_stack[current_parent].try_right_branch =
true;
void fixupMaxHigh(IntervalTreeNode< S > *node)
Travels up to the root fixing the max_high fields after an insertion or deletion. ...
Definition: interval_tree-inl.h:190
Main namespace.
Definition: broadphase_bruteforce-inl.h:45
Class describes the information needed when we take the right branch in searching for intervals but p...
Definition: interval_tree.h:54
IntervalTree()
Definition: interval_tree-inl.h:54
bool red
red or black node: if red = false then the node is black
Definition: interval_tree_node.h:85
bool overlap(const Eigen::MatrixBase< DerivedA > &R0, const Eigen::MatrixBase< DerivedB > &T0, const kIOS< S > &b1, const kIOS< S > &b2)
Check collision between two kIOSs, b1 is in configuration (R0, T0) and b2 is in identity.
Definition: kIOS-inl.h:249
IntervalTreeNode< S > * getSuccessor(IntervalTreeNode< S > *node) const
Get the successor of a given node.
Definition: interval_tree-inl.h:266
void leftRotate(IntervalTreeNode< S > *node)
left rotation of tree node
Definition: interval_tree-inl.h:116
IntervalTreeNode< S > * insert(SimpleInterval< S > *new_interval)
Insert one node of the interval tree.
Definition: interval_tree-inl.h:201
Interval trees implemented using red-black-trees as described in the book Introduction_To_Algorithms_...
Definition: simple_interval.h:50
void recursivePrint(IntervalTreeNode< S > *node) const
recursively print a subtree
Definition: interval_tree-inl.h:316
void print() const
Print the whole interval tree.
Definition: interval_tree-inl.h:328
SimpleInterval< S > * deleteNode(IntervalTreeNode< S > *node)
Delete one node of the interval tree.
Definition: interval_tree-inl.h:437
IntervalTreeNode< S > * recursiveSearch(IntervalTreeNode< S > *node, SimpleInterval< S > *ivl) const
recursively find the node corresponding to the interval
Definition: interval_tree-inl.h:419
IntervalTreeNode< S > * getPredecessor(IntervalTreeNode< S > *node) const
get the predecessor of a given node
Definition: interval_tree-inl.h:291
void print(IntervalTreeNode *left, IntervalTreeNode *right) const
Print the interval node information: set left = nil and right = root.
Definition: interval_tree_node-inl.h:79
void rightRotate(IntervalTreeNode< S > *node)
right rotation of tree node
Definition: interval_tree-inl.h:141
std::deque< SimpleInterval< S > * > query(S low, S high)
Return result for a given query.
Definition: interval_tree-inl.h:512
void recursiveInsert(IntervalTreeNode< S > *node)
Inserts node into the tree as if it were a regular binary tree.
Definition: interval_tree-inl.h:165
The node for interval tree.
Definition: interval_tree_node.h:54
SimpleInterval< S > * stored_interval
interval stored in the node
Definition: interval_tree_node.h:76
Interval tree.
Definition: interval_tree.h:72