BSP 分割空间实现.docVIP

  • 4
  • 0
  • 约6.44千字
  • 约 8页
  • 2017-01-09 发布于贵州
  • 举报
BSP: 二叉分割树,是一种分割场景的方法,下面代码是BSP树一种实现可运行: 运行例子中,将定义的16个空间面,分割为一个深度是3的BSP树,上图显示的是运行结果: #include stdafx.h #include map #include vector #include iostream using namespace std; //定义空间的点结构 struct point { float x,y,z; point():x(0.0f),y(0.0f),z(0.0f){}; point(float a,float b,float c):x(a),y(b),z(c){} void operator += (int n) { x += n; y += n; z += n; } void operator = (point p) { memcpy(this,(void*)p,sizeof(*this)); } }; //定义空间的面结构 struct face { point P[3]; void operator +=(int n) { P[0] += n; P[1] += n; P[2] += n; } }; //定义包围盒结构 struct BBox { point Min; point Max; BBox():Min(),Max(){} }; enum EAxis {//沿的轴枚举 Axis_X, Axis_Y, Axis_Z, }; //树节点的定义 struct TreeNode { TreeNode():box(),nDepth(0),pLChild(NULL),pRChild(NULL),Axis(Axis_X),Split(0.0f){vFaceId.reserve(16);} int nDepth; TreeNode* pLChild; TreeNode* pRChild; std::vectorint vFaceId; int Axis; BBox box; float Split; }; //存储空间的面 std::mapint,face m_mFace; //通过面ID获取面的地址 face* GetFaceByID(int nID) { std::mapint,face::iterator itr = m_mFace.find(nID); if (itr != m_mFace.end() ) { return (m_mFace[nID]); } return NULL; } //BSP类的定义实现 class BspTree { public: BspTree():m_pRoot(NULL){}; ~BspTree() { if (m_pRoot) { DeleteNode(m_pRoot); } } //初始化树根 void InitTreeRoot(TreeNode *pNode); //释放整个树的资源 void DeleteNode(TreeNode * pNode); //生成AABB包围盒 void BuildAABB(TreeNode * pNode); //切分整个空间 void SplitSpace(TreeNode* pRoot,int nAxis,int ndepth); //切分面 void SplitFace( int nFaceId, float fSplit, int nAxis, int* pLeftNum, int* pRightNum, int* pBothNum ); //遍历整个树 void ErgodicTree(TreeNode * pNode); protected: private: TreeNode *m_pRoot; }; void BspTree::InitTreeRoot(TreeNode *pNode) { if (pNode == NULL) return; m_pRoot = pNode; } void BspTree::DeleteNode(TreeNode * pNode) { if (pNode == NULL) return; DeleteNode(pNode-pLChild); DeleteNode(pNode-pRChild); delete pNode; } //遍历整个树 void BspTree::ErgodicTree(TreeNode * pNode) { if (pNode == NULL) return; ErgodicTree(

文档评论(0)

1亿VIP精品文档

相关文档