00001 /* $Id:$ */ 00002 00003 #ifndef __OCT_TREE_H 00004 #define __OCT_TREE_H 00005 00006 #include <esg/spacesorting/SDS.h> 00007 00008 /* 00009 * TO DO: 00010 * - delay of tree build 00011 * - generic insertion of objects 00012 */ 00013 00014 namespace esg { 00015 00016 class OctTree : public SDS { 00017 protected: 00018 struct Node { 00019 Node * children; // array of eight children or NULL 00020 Node * pParent; 00021 List<SceneGraphObject> objectList; 00022 00023 Node() : children(NULL), pParent(NULL) {} 00024 ~Node() { objectList.dropAll(); } 00025 00026 bool isLeaf () const { return ((children) ? false : true ); } 00027 bool isInner () const { return ((children) ? true : false); } 00028 bool isRoot () const { return ((pParent) ? false : true ); } 00029 bool isEmpty () const { return objectList.empty(); } 00030 } _root; 00031 00032 protected: 00033 void _append (SceneGraphObject*, Node*); 00034 SceneGraphObject* _detach (SceneGraphObject::OID, Node); 00035 void _build_tree (Node*); 00036 void _rebuild_tree (Node*); 00037 void _destroy_tree (Node*); 00038 00039 virtual void _duplicate_attributes (const SDS&) = 0; 00040 00041 protected: 00042 unsigned _sideLength; // maximal extent of scene 00043 00044 public: 00045 OctTree(bool /* delay build */, unsigned /* side length */); 00046 00047 virtual ~OctTree() { _destroy_tree(&_root); } 00048 00049 virtual SDS* clone () const; 00050 virtual Iterator* createIterator (); 00051 virtual InspectorSDS* createInspector (unsigned); 00052 00053 // Operations changing inner structure must be fixed in SDS 00054 // interface. Operations which traverse inner structure can be 00055 // moved to Iterator. 00056 // 00057 // All operation are applied locally (not recursivelly to sub-nodes) 00058 virtual int append (SceneGraphObject*); 00059 virtual SceneGraphObject* detach (SceneGraphObject::OID); 00060 00061 // Update inner structure. Typically when some stored object 00062 // has been moved. 00063 virtual bool update (void); 00064 00065 // Build inner structure. Some space sub-division structures 00066 // may preffer its real creation not until they know most of storing 00067 // objects. 00068 // This method must be called after all objects are appended when 00069 // delayBuild is set to true in constructor 00070 virtual bool build (void); 00071 00072 // Dump the tree structure into (opened) file 00073 virtual void dump (const char* /* intent */, const char* /* tab */) {} 00074 virtual void dump (const char* /* file name */) 00075 { 00076 dump("", " "); 00077 /* to do */ 00078 } 00079 }; 00080 00081 }; // namespace 00082 00083 #endif // __OCT_TREE_H