$search
00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2011, Willow Garage, Inc. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Willow Garage, Inc. nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 */ 00034 00037 #include "collision_space_ccd/interval_tree.h" 00038 #include <iostream> 00039 #include <cstdlib> 00040 00041 00042 namespace collision_space_ccd 00043 { 00044 00045 IntervalTreeNode::IntervalTreeNode(){} 00046 00047 IntervalTreeNode::IntervalTreeNode(Interval* new_interval) : 00048 stored_interval (new_interval), 00049 key(new_interval->low), 00050 high(new_interval->high), 00051 max_high(high) {} 00052 00053 IntervalTreeNode::~IntervalTreeNode(){} 00054 00055 00056 IntervalTree::IntervalTree() 00057 { 00058 nil = new IntervalTreeNode; 00059 nil->left = nil->right = nil->parent = nil; 00060 nil->red = false; 00061 nil->key = nil->high = nil->max_high = -std::numeric_limits<double>::max(); 00062 nil->stored_interval = NULL; 00063 00064 root = new IntervalTreeNode; 00065 root->parent = root->left = root->right = nil; 00066 root->key = root->high = root->max_high = std::numeric_limits<double>::max(); 00067 root->red = false; 00068 root->stored_interval = NULL; 00069 00070 /* the following are used for the query function */ 00071 recursion_node_stack_size = 128; 00072 recursion_node_stack = (it_recursion_node*)malloc(recursion_node_stack_size*sizeof(it_recursion_node)); 00073 recursion_node_stack_top = 1; 00074 recursion_node_stack[0].start_node = NULL; 00075 } 00076 00077 00078 void IntervalTree::leftRotate(IntervalTreeNode* x) 00079 { 00080 IntervalTreeNode* y; 00081 00082 y = x->right; 00083 x->right = y->left; 00084 00085 if(y->left != nil) y->left->parent = x; 00086 00087 y->parent = x->parent; 00088 00089 if(x == x->parent->left) 00090 x->parent->left = y; 00091 else 00092 x->parent->right = y; 00093 00094 y->left = x; 00095 x->parent = y; 00096 00097 x->max_high = std::max(x->left->max_high, std::max(x->right->max_high, x->high)); 00098 y->max_high = std::max(x->max_high, std::max(y->right->max_high, y->high)); 00099 } 00100 00101 00102 void IntervalTree::rightRotate(IntervalTreeNode* y) 00103 { 00104 IntervalTreeNode* x; 00105 00106 x = y->left; 00107 y->left = x->right; 00108 00109 if(nil != x->right) x->right->parent = y; 00110 00111 x->parent = y->parent; 00112 if(y == y->parent->left) 00113 y->parent->left = x; 00114 else 00115 y->parent->right = x; 00116 00117 x->right = y; 00118 y->parent = x; 00119 00120 y->max_high = std::max(y->left->max_high, std::max(y->right->max_high, y->high)); 00121 x->max_high = std::max(x->left->max_high, std::max(y->max_high, x->high)); 00122 } 00123 00124 00125 00127 void IntervalTree::recursiveInsert(IntervalTreeNode* z) 00128 { 00129 IntervalTreeNode* x; 00130 IntervalTreeNode* y; 00131 00132 z->left = z->right = nil; 00133 y = root; 00134 x = root->left; 00135 while(x != nil) 00136 { 00137 y = x; 00138 if(x->key > z->key) 00139 x = x->left; 00140 else 00141 x = x->right; 00142 } 00143 z->parent = y; 00144 if((y == root) || (y->key > z->key)) 00145 y->left = z; 00146 else 00147 y->right = z; 00148 } 00149 00150 00151 void IntervalTree::fixupMaxHigh(IntervalTreeNode* x) 00152 { 00153 while(x != root) 00154 { 00155 x->max_high = std::max(x->high, std::max(x->left->max_high, x->right->max_high)); 00156 x = x->parent; 00157 } 00158 } 00159 00160 IntervalTreeNode* IntervalTree::insert(Interval* new_interval) 00161 { 00162 IntervalTreeNode* y; 00163 IntervalTreeNode* x; 00164 IntervalTreeNode* new_node; 00165 00166 x = new IntervalTreeNode(new_interval); 00167 recursiveInsert(x); 00168 fixupMaxHigh(x->parent); 00169 new_node = x; 00170 x->red = true; 00171 while(x->parent->red) 00172 { 00173 /* use sentinel instead of checking for root */ 00174 if(x->parent == x->parent->parent->left) 00175 { 00176 y = x->parent->parent->right; 00177 if(y->red) 00178 { 00179 x->parent->red = true; 00180 y->red = true; 00181 x->parent->parent->red = true; 00182 x = x->parent->parent; 00183 } 00184 else 00185 { 00186 if(x == x->parent->right) 00187 { 00188 x = x->parent; 00189 leftRotate(x); 00190 } 00191 x->parent->red = false; 00192 x->parent->parent->red = true; 00193 rightRotate(x->parent->parent); 00194 } 00195 } 00196 else 00197 { 00198 y = x->parent->parent->left; 00199 if(y->red) 00200 { 00201 x->parent->red = false; 00202 y->red = false; 00203 x->parent->parent->red = true; 00204 x = x->parent->parent; 00205 } 00206 else 00207 { 00208 if(x == x->parent->left) 00209 { 00210 x = x->parent; 00211 rightRotate(x); 00212 } 00213 x->parent->red = false; 00214 x->parent->parent->red = true; 00215 leftRotate(x->parent->parent); 00216 } 00217 } 00218 } 00219 root->left->red = false; 00220 return new_node; 00221 } 00222 00223 IntervalTreeNode* IntervalTree::getSuccessor(IntervalTreeNode* x) const 00224 { 00225 IntervalTreeNode* y; 00226 00227 if(nil != (y = x->right)) 00228 { 00229 while(y->left != nil) 00230 y = y->left; 00231 return y; 00232 } 00233 else 00234 { 00235 y = x->parent; 00236 while(x == y->right) 00237 { 00238 x = y; 00239 y = y->parent; 00240 } 00241 if(y == root) return nil; 00242 return y; 00243 } 00244 } 00245 00246 00247 IntervalTreeNode* IntervalTree::getPredecessor(IntervalTreeNode* x) const 00248 { 00249 IntervalTreeNode* y; 00250 00251 if(nil != (y = x->left)) 00252 { 00253 while(y->right != nil) 00254 y = y->right; 00255 return y; 00256 } 00257 else 00258 { 00259 y = x->parent; 00260 while(x == y->left) 00261 { 00262 if(y == root) return nil; 00263 x = y; 00264 y = y->parent; 00265 } 00266 return y; 00267 } 00268 } 00269 00270 void IntervalTreeNode::print(IntervalTreeNode* nil, IntervalTreeNode* root) const 00271 { 00272 stored_interval->print(); 00273 std::cout << ", k = " << key << ", h = " << high << ", mH = " << max_high; 00274 std::cout << " l->key = "; 00275 if(left == nil) std::cout << "NULL"; else std::cout << left->key; 00276 std::cout << " r->key = "; 00277 if(right == nil) std::cout << "NULL"; else std::cout << right->key; 00278 std::cout << " p->key = "; 00279 if(parent == root) std::cout << "NULL"; else std::cout << parent->key; 00280 std::cout << " red = " << (int)red << std::endl; 00281 } 00282 00283 void IntervalTree::recursivePrint(IntervalTreeNode* x) const 00284 { 00285 if(x != nil) 00286 { 00287 recursivePrint(x->left); 00288 x->print(nil,root); 00289 recursivePrint(x->right); 00290 } 00291 } 00292 00293 IntervalTree::~IntervalTree() 00294 { 00295 IntervalTreeNode* x = root->left; 00296 std::deque<IntervalTreeNode*> nodes_to_free; 00297 00298 if(x != nil) 00299 { 00300 if(x->left != nil) 00301 { 00302 nodes_to_free.push_back(x->left); 00303 } 00304 if(x->right != nil) 00305 { 00306 nodes_to_free.push_back(x->right); 00307 } 00308 00309 delete x; 00310 while( nodes_to_free.size() > 0) 00311 { 00312 x = nodes_to_free.back(); 00313 nodes_to_free.pop_back(); 00314 if(x->left != nil) 00315 { 00316 nodes_to_free.push_back(x->left); 00317 } 00318 if(x->right != nil) 00319 { 00320 nodes_to_free.push_back(x->right); 00321 } 00322 delete x; 00323 } 00324 } 00325 delete nil; 00326 delete root; 00327 free(recursion_node_stack); 00328 } 00329 00330 00331 void IntervalTree::print() const 00332 { 00333 recursivePrint(root->left); 00334 } 00335 00336 void IntervalTree::deleteFixup(IntervalTreeNode* x) 00337 { 00338 IntervalTreeNode* w; 00339 IntervalTreeNode* root_left_node = root->left; 00340 00341 while((!x->red) && (root_left_node != x)) 00342 { 00343 if(x == x->parent->left) 00344 { 00345 w = x->parent->right; 00346 if(w->red) 00347 { 00348 w->red = false; 00349 x->parent->red = true; 00350 leftRotate(x->parent); 00351 w = x->parent->right; 00352 } 00353 if((!w->right->red) && (!w->left->red)) 00354 { 00355 w->red = true; 00356 x = x->parent; 00357 } 00358 else 00359 { 00360 if(!w->right->red) 00361 { 00362 w->left->red = false; 00363 w->red = true; 00364 rightRotate(w); 00365 w = x->parent->right; 00366 } 00367 w->red = x->parent->red; 00368 x->parent->red = false; 00369 w->right->red = false; 00370 leftRotate(x->parent); 00371 x = root_left_node; 00372 } 00373 } 00374 else 00375 { 00376 w = x->parent->left; 00377 if(w->red) 00378 { 00379 w->red = false; 00380 x->parent->red = true; 00381 rightRotate(x->parent); 00382 w = x->parent->left; 00383 } 00384 if((!w->right->red) && (!w->left->red)) 00385 { 00386 w->red = true; 00387 x = x->parent; 00388 } 00389 else 00390 { 00391 if(!w->left->red) 00392 { 00393 w->right->red = false; 00394 w->red = true; 00395 leftRotate(w); 00396 w = x->parent->left; 00397 } 00398 w->red = x->parent->red; 00399 x->parent->red = false; 00400 w->left->red = false; 00401 rightRotate(x->parent); 00402 x = root_left_node; 00403 } 00404 } 00405 } 00406 x->red = false; 00407 } 00408 00409 Interval* IntervalTree::deleteNode(IntervalTreeNode* z) 00410 { 00411 IntervalTreeNode* y; 00412 IntervalTreeNode* x; 00413 Interval* node_to_delete = z->stored_interval; 00414 00415 y= ((z->left == nil) || (z->right == nil)) ? z : getSuccessor(z); 00416 x= (y->left == nil) ? y->right : y->left; 00417 if(root == (x->parent = y->parent)) 00418 { 00419 root->left = x; 00420 } 00421 else 00422 { 00423 if(y == y->parent->left) 00424 { 00425 y->parent->left = x; 00426 } 00427 else 00428 { 00429 y->parent->right = x; 00430 } 00431 } 00432 00433 /* y should not be nil in this case */ 00434 /* y is the node to splice out and x is its child */ 00435 if(y != z) 00436 { 00437 y->max_high = -std::numeric_limits<double>::max(); 00438 y->left = z->left; 00439 y->right = z->right; 00440 y->parent = z->parent; 00441 z->left->parent = z->right->parent = y; 00442 if(z == z->parent->left) 00443 z->parent->left = y; 00444 else 00445 z->parent->right = y; 00446 00447 fixupMaxHigh(x->parent); 00448 if(!(y->red)) 00449 { 00450 y->red = z->red; 00451 deleteFixup(x); 00452 } 00453 else 00454 y->red = z->red; 00455 delete z; 00456 } 00457 else 00458 { 00459 fixupMaxHigh(x->parent); 00460 if(!(y->red)) deleteFixup(x); 00461 delete y; 00462 } 00463 00464 return node_to_delete; 00465 } 00466 00468 bool overlap(double a1, double a2, double b1, double b2) 00469 { 00470 if(a1 <= b1) 00471 { 00472 return (b1 <= a2); 00473 } 00474 else 00475 { 00476 return (a1 <= b2); 00477 } 00478 } 00479 00480 std::deque<Interval*> IntervalTree::query(double low, double high) 00481 { 00482 std::deque<Interval*> result_stack; 00483 IntervalTreeNode* x = root->left; 00484 bool run = (x != nil); 00485 00486 current_parent = 0; 00487 00488 while(run) 00489 { 00490 if(overlap(low,high,x->key,x->high)) 00491 { 00492 result_stack.push_back(x->stored_interval); 00493 recursion_node_stack[current_parent].try_right_branch = true; 00494 } 00495 if(x->left->max_high >= low) 00496 { 00497 if(recursion_node_stack_top == recursion_node_stack_size) 00498 { 00499 recursion_node_stack_size *= 2; 00500 recursion_node_stack = (it_recursion_node *)realloc(recursion_node_stack, recursion_node_stack_size * sizeof(it_recursion_node)); 00501 if(recursion_node_stack == NULL) 00502 exit(1); 00503 } 00504 recursion_node_stack[recursion_node_stack_top].start_node = x; 00505 recursion_node_stack[recursion_node_stack_top].try_right_branch = false; 00506 recursion_node_stack[recursion_node_stack_top].parent_index = current_parent; 00507 current_parent = recursion_node_stack_top++; 00508 x = x->left; 00509 } 00510 else 00511 x = x->right; 00512 00513 run = (x != nil); 00514 while((!run) && (recursion_node_stack_top > 1)) 00515 { 00516 if(recursion_node_stack[--recursion_node_stack_top].try_right_branch) 00517 { 00518 x=recursion_node_stack[recursion_node_stack_top].start_node->right; 00519 current_parent=recursion_node_stack[recursion_node_stack_top].parent_index; 00520 recursion_node_stack[current_parent].try_right_branch = true; 00521 run = (x != nil); 00522 } 00523 } 00524 } 00525 return result_stack; 00526 } 00527 00528 }