BSP 分割空間實(shí)現(xiàn)_第1頁
BSP 分割空間實(shí)現(xiàn)_第2頁
BSP 分割空間實(shí)現(xiàn)_第3頁
BSP 分割空間實(shí)現(xiàn)_第4頁
BSP 分割空間實(shí)現(xiàn)_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、BSP:二叉分割樹,是一種分割場(chǎng)景的方法,下面代碼是BSP樹一種實(shí)現(xiàn)可運(yùn)行:運(yùn)行例子中,將定義的16個(gè)空間面,分割為一個(gè)深度是3的BSP樹,上圖顯示的是運(yùn)行結(jié)果:#include "stdafx.h"#include <map>#include <vector>#include <iostream>using namespace std;/定義空間的點(diǎn)結(jié)構(gòu)struct pointfloat x,y,z;point():x(0.0f),y(0.0f),z(0.0f);point(float a,float b,float c):x(a),y

2、(b),z(c)void operator += (int n)x += n;y += n;z += n;void operator = (point& p)memcpy(this,(void*)&p,sizeof(*this);/定義空間的面結(jié)構(gòu)struct facepoint P3;void operator +=(int n)P0 += n;P1 += n;P2 += n;/定義包圍盒結(jié)構(gòu)struct BBoxpoint Min;point Max;BBox():Min(),Max();enum EAxis/沿的軸枚舉Axis_X,Axis_Y,Axis_Z,;/樹節(jié)點(diǎn)的

3、定義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:vector<int> vFaceId;int Axis;BBox box;float Split;/存儲(chǔ)空間的面std:map<int,face> m_mFace;/通過面ID獲取面的地址face* GetFaceByID(int nID

4、)std:map<int,face>:iterator itr = m_mFace.find(nID);if (itr != m_mFace.end() )return &(m_mFacenID);return NULL;/BSP類的定義實(shí)現(xiàn)class BspTreepublic:BspTree():m_pRoot(NULL);BspTree()if (m_pRoot)DeleteNode(m_pRoot);/初始化樹根void InitTreeRoot(TreeNode *pNode);/釋放整個(gè)樹的資源void DeleteNode(TreeNode * pNode);

5、/生成AABB包圍盒void BuildAABB(TreeNode * pNode);/切分整個(gè)空間void SplitSpace(TreeNode* pRoot,int nAxis,int ndepth);/切分面void SplitFace( int nFaceId, float fSplit, int nAxis, int* pLeftNum, int* pRightNum, int* pBothNum );/遍歷整個(gè)樹void ErgodicTree(TreeNode * pNode);protected:private:TreeNode *m_pRoot;void BspTree:I

6、nitTreeRoot(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;/遍歷整個(gè)樹void BspTree:ErgodicTree(TreeNode * pNode)if (pNode = NULL)return;ErgodicTree(pNode->

7、pLChild);cout<<"節(jié)點(diǎn)深度: "<<pNode->nDepth<<"含有多少個(gè)面: "<<pNode->vFaceId.size();switch (pNode->Axis)case Axis_X:cout<<" 沿X軸分割"<<"分割點(diǎn)是: x ="<<pNode->Split<<endl;break;case Axis_Y:cout<<" 沿Y軸分割&quo

8、t;<<"分割點(diǎn)是: y ="<<pNode->Split<<endl;break;case Axis_Z:cout<<" 沿Z軸分割"<<"分割點(diǎn)是: z ="<<pNode->Split<<endl;break;ErgodicTree(pNode->pRChild);void BspTree:BuildAABB(TreeNode * pNode)if(!pNode)return;point Min(1000000,1000000,

9、1000000);point Max(-1000000,-1000000,-1000000);for(int n = 0; n < pNode->vFaceId.size(); +n)face *pFa = GetFaceByID(n);if (pFa = NULL)continue;for(int m = 0; m < 3; +m)if (pFa->Pm.x > Max.x) Max.x = pFa->Pm.x;if (pFa->Pm.y > Max.y)Max.y = pFa->Pm.y;if (pFa->Pm.z > Ma

10、x.z)Max.z = pFa->Pm.z;if (pFa->Pm.x < Min.x)Min.x = pFa->Pm.x;if (pFa->Pm.y < Min.y)Min.y = pFa->Pm.y;if (pFa->Pm.z < Min.z)Min.z = pFa->Pm.z;pNode->box.Max = Max;pNode->box.Min = Min;int CompareFloat(float a,float b,float fOff)if (abs(a - b) < fOff)return 0;if

11、 ( a > b)return 1;elsereturn -1;void BspTree:SplitFace( int nFaceId, float fSplit, int nAxis, int* pLeftNum, int* pRightNum, int* pBothNum )face* pFace = GetFaceByID(nFaceId);int nLeftCount = 0;int nRightCount = 0;int nBothCount = 0;float t3;switch( nAxis )case Axis_X:t0 = pFace->P0.x;t1 = pFa

12、ce->P1.x;t2 = pFace->P2.x;break;case Axis_Y:t0 = pFace->P0.y;t1 = pFace->P1.y;t2 = pFace->P2.y;break;case Axis_Z:t0 = pFace->P0.z;t1 = pFace->P1.z;t2 = pFace->P2.z;break;for( int i = 0; i < 3; i+ )int c = CompareFloat( ti, fSplit ,0.001f);if( c < 0 )/ 左邊nLeftCount+;else

13、 if( c > 0 )/ 右邊nRightCount+;else/ 正中間nBothCount+;*pLeftNum = nLeftCount;*pRightNum = nRightCount;*pBothNum = nBothCount;void BspTree:SplitSpace(TreeNode* pRoot,int nAxis,int ndepth)if(!pRoot)return;pRoot->nDepth = ndepth;pRoot->Axis = nAxis;if (pRoot->vFaceId.size() < 3 | ndepth >

14、 2)pRoot->pLChild = NULL;pRoot->pRChild = NULL;return;pRoot->pLChild = new TreeNode;pRoot->pRChild = new TreeNode;pRoot->pLChild->box.Max = pRoot->box.Max;pRoot->pLChild->box.Min = pRoot->box.Min;pRoot->pRChild->box.Max = pRoot->box.Max;pRoot->pRChild->bo

15、x.Min = pRoot->box.Min;nAxis = (int)Axis_X;float XLength = pRoot->box.Max.x - pRoot->box.Min.x;float YLength = pRoot->box.Max.y - pRoot->box.Min.y;float ZLength = pRoot->box.Max.z - pRoot->box.Min.z;if (YLength > XLength)nAxis = Axis_Y;XLength = YLength;if (ZLength > XLeng

16、th)nAxis = Axis_Z;float fslit = 0.0f;switch (nAxis)case Axis_X:fslit = (pRoot->box.Max.x + pRoot->box.Min.x)/2.0;pRoot->pLChild->box.Max.x = fslit;pRoot->pRChild->box.Min.x = fslit;break;case Axis_Y:fslit = (pRoot->box.Max.y + pRoot->box.Min.y)/2.0;pRoot->pLChild->box.M

17、ax.y = fslit;pRoot->pRChild->box.Min.y = fslit;break;case Axis_Z:fslit = (pRoot->box.Max.z + pRoot->box.Min.z)/2.0;pRoot->pLChild->box.Max.z = fslit;pRoot->pRChild->box.Min.z = fslit;break;pRoot->Split = fslit;int nSize = pRoot->vFaceId.size();int nLeftCount,nRightCount

18、,nBothCount;for (int n = 0; n < nSize; +n)SplitFace(pRoot->vFaceId.at(n),fslit,nAxis,&nLeftCount,&nRightCount,&nBothCount);/ 如果左邊有if( nLeftCount > 0 | nBothCount > 0 )pRoot->pLChild->vFaceId.push_back( pRoot->vFaceId.at(n) );if( nRightCount > 0 | nBothCount > 0 )pRoot->pRChild->vFaceId.push_back

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論