AI_SDB_SpatialAABBTree.h

Go to the documentation of this file.
00001 #ifndef AI_SDB_SPATIALAABBTREE_H
00002 #define AI_SDB_SPATIALAABBTREE_H
00003 
00004 #include "AI_SDB_SpatialElements.h"
00005 
00006 class AI_SDB_SpatialAABBTreeNode;
00007 
00008 
00031 class AI_SDB_SpatialAABBTree : public AI_SDB_SpatialHierarchyElement
00032 {
00033 public:
00034     AI_SDB_SpatialAABBTree();
00035     ~AI_SDB_SpatialAABBTree();
00036 
00038     void ClearNodes();
00039 
00041     void AddElement(AI_SDB_SpatialElement *appendme);
00043     void RemoveElement(AI_SDB_SpatialElement *removeme);
00045     void MoveElement(AI_SDB_SpatialElement *moveme, const AI_BBox &newbox);
00046 
00048     virtual void Accept(AI_SDB_VisibilityVisitor &visitor, int recursiondepth, AI_SDB_VisitorFlags flags);
00050     virtual void Accept(AI_SDB_SpatialVisitor &visitor, int recursiondepth, AI_SDB_VisitorFlags flags);
00052     virtual void Accept(AI_SDB_OcclusionVisitor &visitor, int recursiondepth, AI_SDB_VisitorFlags flags);
00053 
00054     // node management functions--should break this out into a separate class or something...
00055     // allocate a tree node
00056     AI_SDB_SpatialAABBTreeNode *alloc_node();
00057 
00058     // free a tree node
00059     void free_node(AI_SDB_SpatialAABBTreeNode *freeme);
00060 
00061 protected:
00062     // the tree root
00063     AI_SDB_SpatialAABBTreeNode *m_treenodes;
00064 
00065     // other tree operations
00066     AI_SDB_SpatialAABBTreeNode *FindSuitableNode(const AI_Vector3 &v_center);
00067     void collapse_node(AI_SDB_SpatialAABBTreeNode *collapseme);
00068     void split_node(AI_SDB_SpatialAABBTreeNode *splitme);
00069     void balance_tree(AI_SDB_SpatialAABBTreeNode *treeroot);
00070 };
00071 
00072 
00073 class AI_SDB_SpatialAABBTreeNode {
00074 public:
00075     AI_SDB_SpatialAABBTreeNode() : m_elementsinnode(0), m_totalelements(0), m_parent(NULL) 
00076     { m_children[0] = m_children[1] = NULL; }
00077 
00078     // destructor does nothing really
00079     ~AI_SDB_SpatialAABBTreeNode() { ai_assert(!HasChildren()); }
00080 
00081     // returns true if this node haschild nodes
00082     bool HasChildren() const { return (m_children[0] != NULL); }
00083 
00084     // adds element to this node
00085     void AddElement(AI_SDB_SpatialElement *addme);
00086 
00087     // finds the element in this node or child nodes; returns the containing tree node
00088     AI_SDB_SpatialAABBTreeNode *FindElement(AI_SDB_SpatialElement *findme);
00089 
00090     // removes the element from this node only; if it's not in this node, the call fails.
00091     // for proper use, find the containing treenode using FindElement() and call RemoveElement()
00092     // on that one
00093     void RemoveElement(AI_SDB_SpatialElement *removeme);
00094 
00095     // this function will divvy up the elements and stuff them into the child nodes
00096     void Subdivide(AI_SDB_SpatialAABBTree *nodemanager);
00097 
00098     // recompute the total element count
00099     void RecomputeElementCount();
00100 
00101     // recompute the bounding box
00102     void RecomputeBoundingBox();
00103 
00104     void ClearNodes(AI_SDB_SpatialAABBTree *nodemanager);
00105 
00106     // visitor processing
00107     void Accept(AI_SDB_VisibilityVisitor &visitor, int recursiondepth, AI_SDB_VisitorFlags flags);
00108     void Accept(AI_SDB_SpatialVisitor &visitor, int recursiondepth, AI_SDB_VisitorFlags flags);
00109     void Accept(AI_SDB_OcclusionVisitor &visitor, int recursiondepth, AI_SDB_VisitorFlags flags);
00110 
00111     // bbox for this node
00112     AI_BBox m_bbox;
00113 
00114     // split axis and position
00115     int m_splitaxis;  // 0=x,1=y,2=z
00116     float m_splitposition;
00117 
00118     enum { MAX_ELEMENTSPERNODE = 10 };
00119 
00120     // spatialelements in this node
00121     AI_SDB_SpatialElement *m_elements[MAX_ELEMENTSPERNODE];
00122     int m_elementsinnode;
00123 
00124     // total elements in this node and all of its children
00125     int m_totalelements;
00126 
00127     // pointers to other nodes
00128     AI_SDB_SpatialAABBTreeNode *m_children[2];
00129     AI_SDB_SpatialAABBTreeNode *m_parent;
00130 };
00131 
00132 #endif