/usr/include/crystalspace-2.0/csgeom/aabbtree.h is in libcrystalspace-dev 2.0+dfsg-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 | /*
Copyright (C) 2008 by Marten Svanfeldt
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef __CS_CSGEOM_AABBTREE_H__
#define __CS_CSGEOM_AABBTREE_H__
#include "csutil/blockallocator.h"
#include "csgeom/box.h"
#include "csutil/dirtyaccessarray.h"
namespace CS
{
namespace Geometry //@@Right?
{
template<typename ObjectType>
struct AABBTreeNodeExtraDataNone
{
void LeafAddObject (ObjectType*) {}
void LeafUpdateObjects (ObjectType**, uint) {}
void NodeUpdate (const AABBTreeNodeExtraDataNone& child1,
const AABBTreeNodeExtraDataNone& child2) {}
};
/**
*
*/
template<
typename ObjectType,
unsigned int objectsPerLeaf = 1,
typename NodeExtraData = AABBTreeNodeExtraDataNone<ObjectType>
>
class AABBTree
{
public:
///
enum
{
AABB_NODE_INNER = 0x0,
AABB_NODE_LEAF = 0x1,
AABB_NODE_TYPE_MASK = 0x1,
AABB_NODE_FLAG_SHIFT = 0x08,
AABB_NODE_FLAG_MASK = 0xFF00
};
///
class Node;
///
AABBTree ()
: rootNode (0)
{
rootNode = AllocNode ();
rootNode->SetLeaf (true);
}
///
~AABBTree ()
{
#ifdef CS_DEBUG
// Destroy only pointers "in use" to track down leaking nodes
DeleteNodeRecursive (rootNode);
#else
// Don't bother with tree traversal, just free all nodes
nodeAllocator.DeleteAll ();
#endif
}
/**
*
*/
void AddObject (ObjectType* object)
{
AddObjectRecursive (rootNode, object);
}
/**
*
*/
bool RemoveObject (const ObjectType* object)
{
return RemoveObjectRec (object, rootNode);
}
/**
*
*/
// void AddObjects (csDirtyAccessArray<ObjectType*> objects)
// {
// // Collect any existing objects into the objects array
// {
// ObjectCollectFn collect (objects);
// TraverseLeafs (collect);
// }
//
// // Build a new tree
// DeleteNodeRecursive (rootNode);
//
// // New root
// rootNode = AllocNode ();
// rootNode->SetLeaf (true);
//
// // Build
// BuildTree (rootNode, objects, 0, objects.GetSize ());
// }
/**
*
*/
bool MoveObject (ObjectType* object, const csBox3& oldBox)
{
// Traverse down the tree, recursively updating BB if we have an update
return MoveObjectRec (object, rootNode, oldBox);
}
/**
*
*/
template<typename InnerFn, typename LeafFn>
void Traverse (InnerFn& inner, LeafFn& leaf)
{
if (rootNode)
TraverseRec (inner, leaf, rootNode);
}
/**
*
*/
template<typename InnerFn, typename LeafFn>
void Traverse (InnerFn& inner, LeafFn& leaf) const
{
if (rootNode)
TraverseRec (inner, leaf, rootNode);
}
/**
*
*/
template<typename InnerFn, typename LeafFn>
void TraverseF2B (InnerFn& inner, LeafFn& leaf, const csVector3& direction)
{
if (rootNode)
TraverseRecF2B (inner, leaf, direction, rootNode);
}
/**
*
*/
template<typename InnerFn, typename LeafFn>
void TraverseF2B (InnerFn& inner, LeafFn& leaf, const csVector3& direction) const
{
if (rootNode)
TraverseRecF2B (inner, leaf, direction, rootNode);
}
/**
*
*/
template<typename InnerFn, typename LeafFn>
void TraverseOut (InnerFn& inner, LeafFn& leaf, const csVector3& point)
{
if (rootNode)
TraverseRecOut (inner, leaf, point, rootNode);
}
/**
*
*/
template<typename InnerFn, typename LeafFn>
void TraverseOut (InnerFn& inner, LeafFn& leaf, const csVector3& point) const
{
if (rootNode)
TraverseRecOut (inner, leaf, point, rootNode);
}
/**
*
*/
struct InnerNodeNoOp
{
bool operator() (const Node* n)
{
CS_ASSERT_MSG("Invalid AABB-tree", !n->IsLeaf ());
return true;
}
};
/**
*
*/
struct LeafNodeNoOp
{
bool operator() (const Node* n)
{
CS_ASSERT_MSG("Invalid AABB-tree", n->IsLeaf ());
return true;
}
};
protected:
/**
*
*/
struct ObjectTypeSortByCenter
{
ObjectTypeSortByCenter (size_t axis)
: axis (axis)
{}
bool operator() (ObjectType* o1, ObjectType* o2)
{
return o1->GetBBox ().GetCenter ()[axis] <
o2->GetBBox ().GetCenter ()[axis];
}
size_t axis;
};
/**
*
*/
struct ObjectCollectFn
{
ObjectCollectFn (csDirtyAccessArray<ObjectType*>& objects)
: objects (objects)
{}
void operator() (Node* child)
{
CS_ASSERT(child->IsLeaf ());
for (size_t i = 0; child->GetObjectCount (); ++i)
{
objects.Push (child->GetLeafData (i));
}
}
csDirtyAccessArray<ObjectType*>& objects;
};
/**
*
*/
void AddObjectRecursive (Node* node, ObjectType* object)
{
if (node->IsLeaf ())
{
if (node->IsObjectSlotFree ())
{
node->AddLeafData (object);
static_cast<NodeExtraData*> (node)->LeafAddObject (object);
}
else
{
// Need to split node in two
// Split according to longest axis
const size_t axis = node->GetBBox ().GetSize ().DominantAxis ();
// Save old info
ObjectType* oldNodeI[objectsPerLeaf+1];
oldNodeI[0] = object;
size_t oldNodeCount = node->GetObjectCount ();
for (size_t i = 0; i < oldNodeCount; ++i)
{
oldNodeI[i+1] = node->GetLeafData (i);
}
{
ObjectTypeSortByCenter sorter (axis);
std::sort (oldNodeI, oldNodeI+oldNodeCount+1, sorter);
}
Node* node1 = AllocNode ();
node1->SetLeaf (true);
Node* node2 = AllocNode ();
node2->SetLeaf (true);
// Assign first
{
size_t i = 0;
for (i = 0; i < (oldNodeCount+1)/2; ++i)
{
AddObjectRecursive (node1, oldNodeI[i]);
}
for (; i < oldNodeCount+1; ++i)
{
AddObjectRecursive (node2, oldNodeI[i]);
}
}
// Setup new
node->SetLeaf (false);
node->SetChild1 (node1);
node->SetChild2 (node2);
static_cast<NodeExtraData*> (node)->NodeUpdate (*node1, *node2);
// update bbox
node->GetBBox() += object->GetBBox();
}
}
else
{
// Select left or right depending on closeness to center (find better)
const csVector3 objBoxCenter = object->GetBBox ().GetCenter ();
const size_t axis = node->GetBBox ().GetCenter ().DominantAxis ();
if (objBoxCenter[axis] < node->GetBBox ().GetCenter ()[axis])
{
AddObjectRecursive (node->GetChild1 (), object);
node->GetBBox ().AddBoundingBox (node->GetChild1 ()->GetBBox ());
}
else
{
AddObjectRecursive (node->GetChild2 (), object);
node->GetBBox ().AddBoundingBox (node->GetChild2 ()->GetBBox ());
}
static_cast<NodeExtraData*> (node)->NodeUpdate (*node->GetChild1(), *node->GetChild2());
}
}
/**
*
*/
template<typename InnerFn, typename LeafFn>
bool TraverseRec (InnerFn& inner, LeafFn& leaf, Node* node)
{
bool ret = true;
if (!node)
return ret;
if (node->IsLeaf ())
{
ret = leaf (node);
}
else
{
if (inner (node))
{
ret = TraverseRec (inner, leaf, node->GetChild1 ());
if (ret) ret = TraverseRec (inner, leaf, node->GetChild2 ());
}
}
return ret;
}
/**
*
*/
template<typename InnerFn, typename LeafFn>
bool TraverseRec (InnerFn& inner, LeafFn& leaf, const Node* node) const
{
bool ret = true;
if (!node)
return ret;
if (node->IsLeaf ())
{
ret = leaf (node);
}
else
{
if (inner (node))
{
ret = TraverseRec (inner, leaf, node->GetChild1 ());
if (ret) ret = TraverseRec (inner, leaf, node->GetChild2 ());
}
}
return ret;
}
/**
*
*/
template<typename InnerFn, typename LeafFn>
bool TraverseRecF2B (InnerFn& inner, LeafFn& leaf, const csVector3& direction, Node* node)
{
bool ret = true;
if (!node)
return ret;
if (node->IsLeaf ())
{
ret = leaf (node);
}
else
{
if (inner (node))
{
const csVector3 centerDiff = node->GetChild2 ()->GetBBox ().GetCenter () -
node->GetChild1 ()->GetBBox ().GetCenter ();
const size_t firstIdx = (centerDiff * direction > 0) ? 0 : 1;
ret = TraverseRecF2B (inner, leaf, direction, node->GetChild (firstIdx));
ret &= TraverseRecF2B (inner, leaf, direction, node->GetChild (1-firstIdx));
}
}
return ret;
}
/**
*
*/
template<typename InnerFn, typename LeafFn>
bool TraverseRecF2B (InnerFn& inner, LeafFn& leaf, const csVector3& direction, const Node* node) const
{
bool ret = true;
if (!node)
return ret;
if (node->IsLeaf ())
{
ret = leaf (node);
}
else
{
if (inner (node))
{
const csVector3 centerDiff = node->GetChild2 ()->GetBBox ().GetCenter () -
node->GetChild1 ()->GetBBox ().GetCenter ();
const size_t firstIdx = (centerDiff * direction > 0) ? 0 : 1;
ret = TraverseRecF2B (inner, leaf, direction, node->GetChild (firstIdx));
ret &= TraverseRecF2B (inner, leaf, direction, node->GetChild (1-firstIdx));
}
}
return ret;
}
/**
*
*/
template<typename InnerFn, typename LeafFn>
bool TraverseRecOut (InnerFn& inner, LeafFn& leaf, const csVector3& point, Node* node)
{
bool ret = true;
if (!node)
return ret;
if (node->IsLeaf ())
{
ret = leaf (node);
}
else
{
if (inner (node))
{
const float ch1LenSq = (node->GetChild1 ()->GetBBox ().GetCenter () - point).SquaredNorm ();
const float ch2LenSq = (node->GetChild2 ()->GetBBox ().GetCenter () - point).SquaredNorm ();
const size_t firstIdx = (ch1LenSq > ch2LenSq) ? 0 : 1;
ret = TraverseRecOut (inner, leaf, point, node->GetChild (firstIdx));
if (ret) ret = TraverseRecOut (inner, leaf, point, node->GetChild (1-firstIdx));
}
}
return ret;
}
/**
*
*/
template<typename InnerFn, typename LeafFn>
bool TraverseRecOut (InnerFn& inner, LeafFn& leaf, const csVector3& point, const Node* node) const
{
bool ret = true;
if (!node)
return ret;
if (node->IsLeaf ())
{
ret = leaf (node);
}
else
{
if (inner (node))
{
const float ch1LenSq = (node->GetChild1 ()->GetBBox ().GetCenter () - point).SquaredNorm ();
const float ch2LenSq = (node->GetChild2 ()->GetBBox ().GetCenter () - point).SquaredNorm ();
const size_t firstIdx = (ch1LenSq > ch2LenSq) ? 0 : 1;
ret = TraverseRecOut (inner, leaf, point, node->GetChild (firstIdx));
if (ret) ret = TraverseRecOut (inner, leaf, point, node->GetChild (1-firstIdx));
}
}
return ret;
}
/**
*
*/
void BuildTree (Node* root, csDirtyAccessArray<ObjectType*>& objects,
size_t objectStart, size_t objectEnd)
{
if (objectEnd <= objectStart)
return;
const size_t numObjects = objectEnd - objectStart;
const bool fewEnough = numObjects <= objectsPerLeaf;
if (fewEnough)
{
// Assign to a leaf
root->SetLeaf (true);
for (size_t i = objectStart; i < objectEnd; ++i)
{
root->AddLeafData (objects[i]);
}
static_cast<NodeExtraData*> (root)->LeafUpdateObjects (
root->GetLeafObjects (), root->GetObjectCount());
}
else
{
// Very dumb, sort by center, split at median
const size_t axis = root->GetBBox ().GetSize ().DominantAxis ();
{
ObjectTypeSortByCenter sorter (axis);
ObjectType** arr = objects.GetArray ();
std::sort (arr + objectStart, arr + objectEnd, sorter);
const size_t median = objectStart + numObjects / 2;
Node* left = AllocNode ();
Node* right = AllocNode ();
root->SetChild1 (left);
root->SetChild2 (right);
BuildTree (left, objects, objectStart, median);
BuildTree (right, objects, median + 1, objectEnd);
static_cast<NodeExtraData*> (root)->NodeUpdate (*left, *right);
}
}
}
/**
*
*/
Node* FindObjectNodeRec (Node* node, ObjectType* object)
{
{
const csBox3& objBox = object->GetBBox ();
if (!node->GetBBox ().Overlap (objBox))
{
return 0;
}
}
if (node->IsLeaf ())
{
for (size_t i = 0; i < node->GetObjectCount (); ++i)
{
if (node->GetLeafData (i) == object)
{
return node;
}
}
}
else
{
Node* result = FindObjectNodeRec (node->GetChild1 (), object);
if (!result)
result = FindObjectNodeRec (node->GetChild2 (), object);
return result;
}
return 0;
}
/**
*
*/
bool MoveObjectRec (ObjectType* object, Node* node, const csBox3& oldBox)
{
if (node && oldBox.Overlap (node->GetBBox ()))
{
if (node->IsLeaf ())
{
for (size_t i = 0; i < node->GetObjectCount (); ++i)
{
if (node->GetLeafData (i) == object)
{
// Found node, update the node BB
csBox3 newNodeBB = node->GetLeafData (0)->GetBBox ();
for (size_t i = 1; i < node->GetObjectCount (); ++i)
{
newNodeBB += node->GetLeafData (i)->GetBBox ();
}
node->SetBBox (newNodeBB);
return true; // Found it
}
}
}
else
{
Node* left = node->GetChild1 ();
Node* right = node->GetChild2 ();
if (left && MoveObjectRec (object, left, oldBox))
{
// Tree was updated, update our bb
csBox3 newNodeBB = left->GetBBox ();
if (right)
{
newNodeBB += right->GetBBox ();
}
node->SetBBox (newNodeBB);
return true;
}
if (right && MoveObjectRec (object, right, oldBox))
{
// Tree was updated, update our bb
csBox3 newNodeBB = right->GetBBox ();
if (left)
{
newNodeBB += left->GetBBox ();
}
node->SetBBox (newNodeBB);
return true;
}
}
}
return false; // Don't overlap in this node
}
/**
*
*/
bool RemoveObjectRec (const ObjectType* object, Node* node)
{
const csBox3& objBox = object->GetBBox ();
if (node && objBox.Overlap (node->GetBBox ()))
{
if (node->IsLeaf ())
{
for (size_t i = 0; i < node->GetObjectCount (); ++i)
{
if (node->GetLeafData (i) == object)
{
// Found node, update the node BB
csBox3 newNodeBB;
for (size_t j = 0; j < node->GetObjectCount (); ++j)
{
if (i != j)
{
newNodeBB += node->GetLeafData (j)->GetBBox ();
}
}
node->SetBBox (newNodeBB);
node->RemoveLeafData (i);
static_cast<NodeExtraData*> (node)->LeafUpdateObjects (
node->GetLeafObjects(), node->GetObjectCount());
return true; // Found it
}
}
}
else
{
Node* left = node->GetChild1 ();
Node* right = node->GetChild2 ();
if (left && RemoveObjectRec (object, left))
{
csBox3 newNodeBB;
if (right)
{
newNodeBB = right->GetBBox ();
}
// Tree was updated, update our bb
if (!left->IsLeaf() || left->GetObjectCount () > 0)
{
newNodeBB += left->GetBBox ();
static_cast<NodeExtraData*> (node)->NodeUpdate (*left, *right);
}
else
{
// We have to delete the left node. We do that by moving the children
// of the right node down.
if (right)
{
node->Copy (right);
nodeAllocator.Free (left);
nodeAllocator.Free (right);
newNodeBB = node->GetBBox ();
}
else
{
node->SetChild1 (0);
node->SetLeaf (true);
static_cast<NodeExtraData*> (node)->LeafUpdateObjects (0, 0);
nodeAllocator.Free (left);
}
}
node->SetBBox (newNodeBB);
return true;
}
if (right && RemoveObjectRec (object, right))
{
csBox3 newNodeBB;
if (left)
{
newNodeBB = left->GetBBox ();
}
// Tree was updated, update our bb
if (!right->IsLeaf() || right->GetObjectCount () > 0)
{
newNodeBB += right->GetBBox ();
static_cast<NodeExtraData*> (node)->NodeUpdate (*left, *right);
}
else
{
// We have to delete the right node. We do that by moving the children
// of the left node down.
if (left)
{
node->Copy (left);
nodeAllocator.Free (left);
nodeAllocator.Free (right);
newNodeBB = node->GetBBox ();
}
else
{
node->SetChild2 (0);
node->SetLeaf (true);
static_cast<NodeExtraData*> (node)->LeafUpdateObjects (0, 0);
nodeAllocator.Free (right);
}
}
node->SetBBox (newNodeBB);
return true;
}
}
}
return false;
}
/**
*
*/
Node* AllocNode ()
{
return nodeAllocator.Alloc ();
}
/**
*
*/
void DeleteNodeRecursive (Node* node)
{
if (!node)
return;
if (!node->IsLeaf ())
{
DeleteNodeRecursive (node->GetChild1 ());
DeleteNodeRecursive (node->GetChild2 ());
}
nodeAllocator.Free (node);
}
typedef csBlockAllocator<
Node,
CS::Memory::AllocatorAlign<32>,
csBlockAllocatorDisposeLeaky<Node>,
csBlockAllocatorSizeObjectAlign<Node, 32>
> NodeAllocatorType;
///
NodeAllocatorType nodeAllocator;
///
Node* rootNode;
};
/**
*
*/
template<
typename ObjectType,
unsigned int objectsPerLeaf,
typename NodeExtraData
>
class AABBTree<ObjectType, objectsPerLeaf, NodeExtraData>::Node : public NodeExtraData
{
public:
Node ()
: typeAndFlags (AABB_NODE_INNER), leafObjCount (0)
{
children[0] = children[1] = 0;
}
// General accessors
///
bool IsLeaf () const
{
return (typeAndFlags & AABB_NODE_TYPE_MASK) == AABB_NODE_LEAF;
}
///
void SetLeaf (bool isLeaf)
{
if (isLeaf && !IsLeaf ())
{
typeAndFlags |= AABB_NODE_LEAF;
// Ensure no children are 'lost'
CS_ASSERT(children[0] == 0);
CS_ASSERT(children[1] == 0);
leafObjCount = 0;
}
else if (!isLeaf && IsLeaf ())
{
typeAndFlags &= ~AABB_NODE_LEAF;
leafObjCount = 0;
children[0] = children[1] = 0;
}
}
///
uint GetFlags () const
{
return typeAndFlags >> AABB_NODE_FLAG_SHIFT;
}
///
void SetFlags (uint newFlags)
{
typeAndFlags = (typeAndFlags & ~AABB_NODE_FLAG_MASK) |
(newFlags << AABB_NODE_FLAG_SHIFT);
}
///
uint GetObjectCount () const
{
CS_ASSERT(IsLeaf ()); // object count is only sensible for leaves
return leafObjCount;
}
///
bool IsObjectSlotFree () const
{
CS_ASSERT(IsLeaf ()); // object count is only sensible for leaves
return leafObjCount < objectsPerLeaf;
}
///
const csBox3& GetBBox () const
{
return boundingBox;
}
///
csBox3& GetBBox ()
{
return boundingBox;
}
///
void SetBBox (const csBox3& box)
{
boundingBox = box;
}
// Accessor for inner node data
///
Node* GetChild1 () const
{
CS_ASSERT(!IsLeaf ());
return children[0];
}
///
Node* GetChild2 () const
{
CS_ASSERT(!IsLeaf ());
return children[1];
}
///
Node* GetChild (size_t i) const
{
CS_ASSERT(!IsLeaf () && i < 2);
return children[i];
}
///
void SetChild1 (Node* child)
{
CS_ASSERT(!IsLeaf ());
children[0] = child;
}
///
void SetChild2 (Node* child)
{
CS_ASSERT(!IsLeaf ());
children[1] = child;
}
/// Copy the node contents to this one.
void Copy (Node* source)
{
typeAndFlags = source->typeAndFlags;
if (IsLeaf ())
{
memcpy (leafStorage, source->leafStorage, sizeof (ObjectType*) * objectsPerLeaf);
}
else
{
SetChild1 (source->GetChild1 ());
SetChild2 (source->GetChild2 ());
}
leafObjCount = source->leafObjCount;
SetBBox (source->GetBBox ());
NodeExtraData::operator= (*source);
}
// Accessor for leaf node data
///
ObjectType* GetLeafData (size_t index) const
{
CS_ASSERT(IsLeaf ());
CS_ASSERT(index < objectsPerLeaf);
return leafStorage[index];
}
ObjectType** GetLeafObjects ()
{
CS_ASSERT(IsLeaf ());
return leafStorage;
}
///
void AddLeafData (ObjectType* object)
{
CS_ASSERT(IsLeaf ());
CS_ASSERT(leafObjCount < objectsPerLeaf);
leafStorage[leafObjCount++] = object;
boundingBox.AddBoundingBox (object->GetBBox ());
}
void RemoveLeafData (size_t index)
{
CS_ASSERT(IsLeaf ());
CS_ASSERT(leafObjCount > 0);
leafStorage[index] = leafStorage[--leafObjCount];
}
private:
///
uint16 typeAndFlags;
///
uint16 leafObjCount;
///
csBox3 boundingBox;
///
union
{
ObjectType* leafStorage[objectsPerLeaf];
Node* children[2];
};
};
}
}
#endif
|