00001 /************************************************************************ 00002 * BVH_Node.h 00003 ************************************************************************ 00004 * Author : Duksu Kim (bluekds@tclab.kaist.ac.kr) 00005 * Affiliation : SGLAB(http://sglab.kaist.ac.kr), Dept. of Computer Science(http://cs.kaist.ac.kr) , KAIST(http://www.kaist.ac.kr) 00006 * Version : 0.5 00007 * Create : 2009/02/23 00008 * Last update : 2009/03/10 00009 ************************************************************************/ 00010 #pragma once 00011 00012 #include <vector> 00013 #include "typeDef.h" 00014 #include "Geometry/box.h" 00015 #include "CCD_output.h" 00016 00017 class CCD_Object ; 00018 00019 #define BVH_NODE_TYPE_OBJECT -1 00020 #define BVH_NODE_TYPE_ROOT 0 00021 #define BVH_NODE_TYPE_ROOT_STATIC 1 00022 #define BVH_NODE_TYPE_INTER 2 00023 #define BVH_NODE_TYPE_LEAF 3 00024 00025 using namespace std ; 00026 00027 class BVH_Node { 00028 public: 00029 00030 /************************************************************************/ 00031 /* Friends */ 00032 /************************************************************************/ 00033 friend class BVH ; 00034 friend class BVH_Node ; 00035 friend class CCD ; 00036 00037 private: 00038 00039 /************************************************************************/ 00040 /* Attributes */ 00041 /************************************************************************/ 00042 int _nodeType ; // 4 byte 00043 BOX _box ; // 24 byte 00044 00045 // for lazy evaluation 00046 union { // 8 byte 00047 struct { // low level node 00048 UINT* _triList ; 00049 UINT _numTris ; 00050 }; 00051 struct { // high level node 00052 UINT* _objList ; 00053 UINT _numObj ; 00054 }; 00055 }; 00056 00057 // 8 byte 00058 union { 00059 struct { // not leaf node 00060 BVH_Node* _leftChild ; 00061 BVH_Node* _rightChild ; 00062 }; 00063 struct { // leaf node 00064 CCD_Object* _pObject ; 00065 UINT _triID ; 00066 } ; 00067 }; 00068 00069 public: 00070 00071 BVH_Node(void) ; 00072 BVH_Node( int type ) ; 00073 ~BVH_Node(void) ; 00074 00075 void semi_init ( int type = BVH_NODE_TYPE_INTER ) ; 00076 00077 /************************************************************************/ 00078 /* Node type */ 00079 /************************************************************************/ 00080 FORCEINLINE void setType ( int type ) { 00081 _nodeType = type ; 00082 } 00083 FORCEINLINE int getType ( void ) { 00084 return _nodeType ; 00085 } 00086 00087 /************************************************************************/ 00088 /* Box */ 00089 /************************************************************************/ 00090 FORCEINLINE void setBox ( BOX box ) { 00091 _box = box ; 00092 } 00093 FORCEINLINE BOX getBox ( void ) { 00094 return _box ; 00095 } 00096 00097 /************************************************************************/ 00098 /* Set child */ 00099 /************************************************************************/ 00100 FORCEINLINE void setLeftChild( BVH_Node* child ) { 00101 _leftChild = child ; 00102 } 00103 FORCEINLINE void setRightChild( BVH_Node* child ) { 00104 _rightChild = child ; 00105 } 00106 FORCEINLINE void setChilds( BVH_Node* lChild, BVH_Node* rChild ) { 00107 _leftChild = lChild ; _rightChild = rChild ; 00108 } 00109 00110 /************************************************************************/ 00111 /* Get child */ 00112 /************************************************************************/ 00113 FORCEINLINE BVH_Node* getLeftChild (void){ 00114 return _leftChild ; 00115 } 00116 FORCEINLINE BVH_Node* getRightChild (void){ 00117 return _rightChild ; 00118 } 00119 00120 /************************************************************************/ 00121 /* Leaf */ 00122 /************************************************************************/ 00123 FORCEINLINE bool isLeaf ( void ) { 00124 return ( _nodeType == BVH_NODE_TYPE_LEAF ? true : false ) ; 00125 } 00126 FORCEINLINE void setObject( CCD_Object* object ) { 00127 _pObject = object ; 00128 } 00129 FORCEINLINE void setTriID ( UINT id ) { 00130 _triID = id ; 00131 } 00132 FORCEINLINE UINT getTriID ( void ) { 00133 return _triID ; 00134 } 00135 00136 /************************************************************************/ 00137 /* Structure */ 00138 /************************************************************************/ 00139 void refit ( void ) ; 00140 BVH_Node* rebuild ( CCD_Object* object, UINT* triList, UINT numTris ) ; 00141 void clear (void ) ; 00142 bool clearObjNode ( void ) ; 00143 00144 /************************************************************************/ 00145 /* CCD */ 00146 /************************************************************************/ 00147 void collide ( BVH_Node* target ) ; 00148 bool leafCollide ( BVH_Node* target ) ; 00149 void selfCollide ( void ) ; 00150 00151 /************************************************************************/ 00152 /* Utilities */ 00153 /************************************************************************/ 00154 void visualize ( int level ) ; 00155 } ;