Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Opcode::AABBTree Class Reference

#include <Opcode.h>

Inheritance diagram for Opcode::AABBTree:
Opcode::AABBTreeNode

Public Member Functions

 AABBTree ()
 
 ~AABBTree ()
 
bool Build (AABBTreeBuilder *builder)
 
void Release ()
 
inline_ const udwordGetIndices () const
 Catch the indices. More...
 
inline_ udword GetNbNodes () const
 Catch the number of nodes. More...
 
bool IsComplete () const
 
udword ComputeDepth () const
 
udword GetUsedBytes () const
 
udword Walk (WalkingCallback callback, void *user_data) const
 
bool Refit (AABBTreeBuilder *builder)
 
bool Refit2 (AABBTreeBuilder *builder)
 
- Public Member Functions inherited from Opcode::AABBTreeNode
inline_ const udwordGetPrimitives () const
 
inline_ udword GetNbPrimitives () const
 

Additional Inherited Members

- Protected Member Functions inherited from Opcode::AABBTreeNode
udword Split (udword axis, AABBTreeBuilder *builder)
 
bool Subdivide (AABBTreeBuilder *builder)
 
void _BuildHierarchy (AABBTreeBuilder *builder)
 
void _Refit (AABBTreeBuilder *builder)
 
- Protected Attributes inherited from Opcode::AABBTreeNode
udwordmNodePrimitives
 Node-related primitives (shortcut to a position in mIndices below) More...
 
udword mNbPrimitives
 Number of primitives for this node. More...
 

Detailed Description

Definition at line 109 of file Opcode.h.

Constructor & Destructor Documentation

AABBTree::AABBTree ( )

Constructor.

Definition at line 374 of file OPC_AABBTree.cpp.

374  : mIndices(null), mPool(null), mTotalNbNodes(0)
375 {
376 }
AABBTree::~AABBTree ( )

Destructor.

Definition at line 383 of file OPC_AABBTree.cpp.

384 {
385  Release();
386 }

Member Function Documentation

bool AABBTree::Build ( AABBTreeBuilder builder)

Builds a generic AABB tree from a tree builder.

Parameters
builder[in] the tree builder
Returns
true if success

Definition at line 406 of file OPC_AABBTree.cpp.

407 {
408  // Checkings
409  if(!builder || !builder->mNbPrimitives) return false;
410 
411  // Release previous tree
412  Release();
413 
414  // Init stats
415  builder->SetCount(1);
416  builder->SetNbInvalidSplits(0);
417 
418  // Initialize indices. This list will be modified during build.
419  mIndices = new udword[builder->mNbPrimitives];
420  CHECKALLOC(mIndices);
421  // Identity permutation
422  for(udword i=0;i<builder->mNbPrimitives;i++) mIndices[i] = i;
423 
424  // Setup initial node. Here we have a complete permutation of the app's primitives.
425  mNodePrimitives = mIndices;
426  mNbPrimitives = builder->mNbPrimitives;
427 
428  // Use a linear array for complete trees (since we can predict the final number of nodes) [Opcode 1.3]
429 // if(builder->mRules&SPLIT_COMPLETE)
430  if(builder->mSettings.mLimit==1)
431  {
432  // Allocate a pool of nodes
433  mPool = new AABBTreeNode[builder->mNbPrimitives*2 - 1];
434 
435  builder->mNodeBase = mPool; // ### ugly !
436  }
437 
438  // Build the hierarchy
439  _BuildHierarchy(builder);
440 
441  // Get back total number of nodes
442  mTotalNbNodes = builder->GetCount();
443 
444  // For complete trees, check the correct number of nodes has been created [Opcode 1.3]
445  if(mPool) OPASSERT(mTotalNbNodes==builder->mNbPrimitives*2 - 1);
446 
447  return true;
448 }
udword AABBTree::ComputeDepth ( ) const

Computes the depth of the tree. A well-balanced tree should have a log(n) depth. A degenerate tree O(n) depth.

Returns
depth of the tree

Definition at line 457 of file OPC_AABBTree.cpp.

458 {
459  return Walk(null, null); // Use the walking code without callback
460 }
inline_ const udword* Opcode::AABBTree::GetIndices ( ) const
inline

Catch the indices.

Definition at line 120 of file Opcode.h.

inline_ udword Opcode::AABBTree::GetNbNodes ( ) const
inline

Catch the number of nodes.

Definition at line 121 of file Opcode.h.

udword AABBTree::GetUsedBytes ( ) const

Computes the number of bytes used by the tree.

Returns
number of bytes used

Definition at line 557 of file OPC_AABBTree.cpp.

558 {
559  udword TotalSize = mTotalNbNodes*GetNodeSize();
560  if(mIndices) TotalSize+=mNbPrimitives*sizeof(udword);
561  return TotalSize;
562 }
bool AABBTree::IsComplete ( ) const

Checks the tree is a complete tree or not. A complete tree is made of 2*N-1 nodes, where N is the number of primitives in the tree.

Returns
true for complete trees

Definition at line 571 of file OPC_AABBTree.cpp.

572 {
573  return (GetNbNodes()==GetNbPrimitives()*2-1);
574 }
bool AABBTree::Refit ( AABBTreeBuilder builder)

Refits the tree in a top-down way.

Parameters
builder[in] the tree builder

Definition at line 502 of file OPC_AABBTree.cpp.

503 {
504  if(!builder) return false;
505  _Refit(builder);
506  return true;
507 }
bool AABBTree::Refit2 ( AABBTreeBuilder builder)

Refits the tree in a bottom-up way.

Parameters
builder[in] the tree builder

Definition at line 515 of file OPC_AABBTree.cpp.

516 {
517  // Checkings
518  if(!builder) return false;
519 
520  OPASSERT(mPool);
521 
522  // Bottom-up update
523  Point Min,Max;
524  Point Min_,Max_;
525  udword Index = mTotalNbNodes;
526  while(Index--)
527  {
528  AABBTreeNode& Current = mPool[Index];
529 
530  if(Current.IsLeaf())
531  {
532  builder->ComputeGlobalBox(Current.GetPrimitives(), Current.GetNbPrimitives(), *(AABB*)Current.GetAABB());
533  }
534  else
535  {
536  Current.GetPos()->GetAABB()->GetMin(Min);
537  Current.GetPos()->GetAABB()->GetMax(Max);
538 
539  Current.GetNeg()->GetAABB()->GetMin(Min_);
540  Current.GetNeg()->GetAABB()->GetMax(Max_);
541 
542  Min.Min(Min_);
543  Max.Max(Max_);
544 
545  ((AABB*)Current.GetAABB())->SetMinMax(Min, Max);
546  }
547  }
548  return true;
549 }
void AABBTree::Release ( )

Releases the tree.

Definition at line 393 of file OPC_AABBTree.cpp.

394 {
395  DELETEARRAY(mPool);
396  DELETEARRAY(mIndices);
397 }
udword AABBTree::Walk ( WalkingCallback  callback,
void *  user_data 
) const

Walks the tree, calling the user back for each node.

Definition at line 467 of file OPC_AABBTree.cpp.

468 {
469  // Call it without callback to compute max depth
470  udword MaxDepth = 0;
471  udword CurrentDepth = 0;
472 
473  struct Local
474  {
475  static void _Walk(const AABBTreeNode* current_node, udword& max_depth, udword& current_depth, WalkingCallback callback, void* user_data)
476  {
477  // Checkings
478  if(!current_node) return;
479  // Entering a new node => increase depth
480  current_depth++;
481  // Keep track of max depth
482  if(current_depth>max_depth) max_depth = current_depth;
483 
484  // Callback
485  if(callback && !(callback)(current_node, current_depth, user_data)) return;
486 
487  // Recurse
488  if(current_node->GetPos()) { _Walk(current_node->GetPos(), max_depth, current_depth, callback, user_data); current_depth--; }
489  if(current_node->GetNeg()) { _Walk(current_node->GetNeg(), max_depth, current_depth, callback, user_data); current_depth--; }
490  }
491  };
492  Local::_Walk(this, MaxDepth, CurrentDepth, callback, user_data);
493  return MaxDepth;
494 }

The documentation for this class was generated from the following files: