版權(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年繁華商圈店鋪?zhàn)赓U合同3篇
- 2024年跨國(guó)保險(xiǎn)業(yè)務(wù)分銷合同
- 2024年版:項(xiàng)目合作風(fēng)險(xiǎn)共擔(dān)協(xié)議
- 2024黃山旅游紀(jì)念品設(shè)計(jì)合同
- 2025年度大理石石材進(jìn)出口貿(mào)易承包合同規(guī)范3篇
- 2024藝術(shù)品代理銷售與藝術(shù)品展覽策劃合同3篇
- 2024蔬菜產(chǎn)地直供與電商平臺(tái)合作意向協(xié)議書3篇
- 2025年度物業(yè)費(fèi)收取與調(diào)整協(xié)議3篇
- 2024甲乙雙方共建智慧城市戰(zhàn)略合作合同
- 西南大學(xué)《特殊兒童運(yùn)動(dòng)康復(fù)》2023-2024學(xué)年第一學(xué)期期末試卷
- 眼藥水項(xiàng)目創(chuàng)業(yè)計(jì)劃書
- 2024年全國(guó)《國(guó)防和兵役》理論知識(shí)競(jìng)賽試題庫與答案
- 家居保潔課件
- 換電站(充電樁)安全風(fēng)險(xiǎn)告知
- 經(jīng)營(yíng)性房屋租賃項(xiàng)目投標(biāo)方案(技術(shù)標(biāo))
- 入戶調(diào)查合同范本
- 七年級(jí)道法上冊(cè)第一學(xué)期期末綜合測(cè)試卷(人教版 2024年秋)
- 標(biāo)桿地產(chǎn)五星級(jí)酒店精裝修標(biāo)準(zhǔn)
- DZ∕T 0153-2014 物化探工程測(cè)量規(guī)范(正式版)
- 商業(yè)空間設(shè)計(jì)(高職環(huán)境藝術(shù)設(shè)計(jì)專業(yè)和室內(nèi)設(shè)計(jì)專業(yè))全套教學(xué)課件
- 廣東省廣州市名校聯(lián)盟重點(diǎn)名校2024屆中考化學(xué)全真模擬試卷含解析
評(píng)論
0/150
提交評(píng)論