详细介绍四叉树 Quadtrees(下)
2010-03-19 15:10
369 查看
编码
Before we go any further, I advise those who are unsure about Indexed Lists to read through my tutorial here.
我们需要:
一个保存我们QUADTREE数据的结构
一个建立树的函数
一个保存三角形数据的结构
typedef struct node
{
int bType;
VERTEX vBoundingCoordinates[4];
unsigned int uiBranches[4];
unsigned int uiVertexStrip1[8];
unsigned int uiVertexStrip2[8];
unsigned int uiVertexStrip3[8];
unsigned int uiVertexStrip4[8];
unsigned int uiID;
unsigned int uiParentID;
}NODE;
变量bType告诉我们节点的类型,可以为NODE_TYPE or LEAF_TYPE,如果我们画树的话,他用 来作为一个标志告诉程序停止或画一些三角形(LEAF_TYPE),或继续向下解析树(NODE_TYPE)。 下一个变量是一个包含4个顶点的数组,他用来保存节点的包围坐标,VERTEX定义如下
typedef struct vertex
{
float x,y,z;
}VERTEX;
我们还有一个叫做uiBranches的数组,他保存了四个索引值,代表了节点的四个子节点,如果本 节点类型是LEAF_TYPE,就不使用。
由于我们说每个叶节点保存16个多边形,这里有四个数组,名为uiVertexStrip1到uiVertexStrip4, 每个数组保存四个三角形。在本向导中,他们没被使用
变量uiID保存了QUADTREE的节点ID,在我解释他以前,QUADTREE就如同一个节点的数组,这个ID就是 数组的索引
T让我们看看最后一个变量,uiParentID,它是父节点的索引,让我们用自己的方法来遍历这棵树,对 于给定的节点,我们可以从它的父节点遍历到它的子节点,对于下面给定一个树,我们如何遍历他呢,
NODE *pNodeList;
这是一个pNodeList的指针,它是一个QUADTREE,注意:我们使用数组pNodeList[0] 作为根节点。
Formula 1.
上面的公式给出了叶节点的数目,叶宽指的是每个叶的三角形数目,这里我们称叶节点为单元,也可以说每 个单元包含16个三角形,那么这里的叶宽为4个三角形,Grid Width 指的是格子的宽度,由于每个单元有4个 三角形,Grid Width 为16个单元乘以4是64,为了求出树中的节点数,使用下面的函数:
Before we go any further, I advise those who are unsure about Indexed Lists to read through my tutorial here.
我们需要:
一个保存我们QUADTREE数据的结构
一个建立树的函数
一个保存三角形数据的结构
typedef struct node
{
int bType;
VERTEX vBoundingCoordinates[4];
unsigned int uiBranches[4];
unsigned int uiVertexStrip1[8];
unsigned int uiVertexStrip2[8];
unsigned int uiVertexStrip3[8];
unsigned int uiVertexStrip4[8];
unsigned int uiID;
unsigned int uiParentID;
}NODE;
变量bType告诉我们节点的类型,可以为NODE_TYPE or LEAF_TYPE,如果我们画树的话,他用 来作为一个标志告诉程序停止或画一些三角形(LEAF_TYPE),或继续向下解析树(NODE_TYPE)。 下一个变量是一个包含4个顶点的数组,他用来保存节点的包围坐标,VERTEX定义如下
typedef struct vertex
{
float x,y,z;
}VERTEX;
我们还有一个叫做uiBranches的数组,他保存了四个索引值,代表了节点的四个子节点,如果本 节点类型是LEAF_TYPE,就不使用。
由于我们说每个叶节点保存16个多边形,这里有四个数组,名为uiVertexStrip1到uiVertexStrip4, 每个数组保存四个三角形。在本向导中,他们没被使用
变量uiID保存了QUADTREE的节点ID,在我解释他以前,QUADTREE就如同一个节点的数组,这个ID就是 数组的索引
T让我们看看最后一个变量,uiParentID,它是父节点的索引,让我们用自己的方法来遍历这棵树,对 于给定的节点,我们可以从它的父节点遍历到它的子节点,对于下面给定一个树,我们如何遍历他呢,
NODE *pNodeList;
这是一个pNodeList的指针,它是一个QUADTREE,注意:我们使用数组pNodeList[0] 作为根节点。
Formula 1.
上面的公式给出了叶节点的数目,叶宽指的是每个叶的三角形数目,这里我们称叶节点为单元,也可以说每 个单元包含16个三角形,那么这里的叶宽为4个三角形,Grid Width 指的是格子的宽度,由于每个单元有4个 三角形,Grid Width 为16个单元乘以4是64,为了求出树中的节点数,使用下面的函数:
unsigned int CalcNodeNum(unsigned int max,unsigned int min) { int ctr=0; int var = 0; while(var==0) { ctr+=max; max=max/min; if(max==1) {var=1;} } ctr++; return ctr; }
这里函数CalcNodeNum 有两个参数,叶节点的数目(MAX)和叶宽(MIN),在这里叶宽为4 个三角形,叶节点的数目包含在上面的公式中,为了更好的理解上面的函数,给出下面的代码: unsigned int Temp =(GridWidth/4)*(GridWidth/4); unsigned int Temp2 = CalcNodeNum(Temp,4); pNodeList = (NODE*)malloc(sizeof(NODE)*Temp2); 首先计算叶节点的总数,其次保存节点的总数到变量Temp2,第三行是为指针分配内存,现在 我们已经技术了节点的总数并分配了内存,接着调用QUADTREE的建立函数。 但是首先,让我们回忆一下递归的代码,如果我们想显示数目1到10,我们可以这样做:void Count(int num) { cout< } void main() { Count(0); Count(1); Count(2); Count(3); Count(4); Count(5); Count(6); Count(7); Count(8); Count(9); return; }
这样做很乏味,可以这样
for(int ctr=0;ctr<10;ctr++)
{
Count(ctr);
}
虽然上面的代码没有任何错误,但在QUADTREE中使用他简直是噩梦,在上面我们调用了10次,如 果我们想调用20次,我们不得不告诉FOR循环使用20次,而递归只需要一次。他不需要FOR或WHILE结 构,正确的代码如下:
void Count(int num) { static int ctr = 0; if(ctr>num) {return;} else { cout< ctr++; Count(num); } } void main() { Count(ctr); return; }
现在让我们看看函数CreateNode,象它的名字一样,他用来建立节点,实际他不仅可以建立一个 节点,还可以建立整个树,我们只要调用函数一次,
void CreateNode(unsigned int Bounding[4],unsigned int ParentID,unsigned int NodeID)
在一个2D数组中扩展为高度为0的X和Z的面,为发现左上坐标,使用下面的公式
右上为:
左下
右下
数学并不困难,现在准备调用:
unsigned int uiBoundingCoordinates[] =
{0,GridWidth,(GridHeight*(GridWidth+1)),((GridHeight)*(GridWidth+1))+GridWidth};
CreateNode(uiBoundingCoordinates,0,0);
父节点已经建立好了,我们可以通过CreateNode来工作了。.
void CreateNode(unsigned int Bounding[4],unsigned int ParentID,unsigned int NodeID)
{
static unsigned int TotalTreeID = 0;
unsigned int uiNodeType;
unsigned int uiWidth,uiHeight;
OK,静态变量TotalTreeID保存了当前的节点数目,我们最后使用他来将子节点与他们的ID联系起 来,uiNodeType保存节点的类型,uiWidth,uiHeight保存节点的宽和高,由于我们传送的是包围坐 标,实际上我们并不知道节点的大小,我们使用uiWidth,uiHeight来告诉节点是叶节点还是普通节点 ,现在需要从包围坐标中获得uiWidth,uiHeight:
uiWidth = fVerts[(Bounding[1]*3)] - fVerts[(Bounding[0]*3)];
uiHeight = fVerts[(Bounding[2]*3)+2] - fVerts[(Bounding[0]*3)+2];
T这里假设fVerts是一个包含顶点列表的数组,每个顶点包含3个部件,X,Y,Z,如果我们有顶点的 索引,就可以获得指向这个顶点的指针,
Figure 14
如同你看见的一样,索引0指向element[0],element[0]是顶点0的X部件,依次类推。 现在,我们说我们的叶节点是4*4的三角形,这意味着叶宽为4三角形,由于我们知道节点的宽度(存储 在uiWidth),如果我们分割宽度的结果为2,那么意味着这个宽度为4,这个节点就是一个叶节点,
if(0.5*uiWidth==2)
{
uiNodeType = LEAF_TYPE;
}
else
{
uiNodeType = NODE_TYPE;
}
接着,我们想得到一个指向我们节点的指针,pNodeList包含所有我们的节点,我们需要选择一个。
NODE *pNode = &pNodeList[NodeID];
向节点内填充内容
pNodeList[NodeID].uID = Whatever;
我们可以简单的做:
pNode->uiID = Whatever;
用我们得到的值填充pNode->uiID = NodeID; pNode->uiParentID = ParentID; pNode->vBoundingCoordinates[0].x = fVerts[(Bounding[0]*3)]; pNode->vBoundingCoordinates[0].y = fVerts[(Bounding[0]*3)+1]; pNode->vBoundingCoordinates[0].z = fVerts[(Bounding[0]*3)+2]; pNode->vBoundingCoordinates[1].x = fVerts[(Bounding[1]*3)]; pNode->vBoundingCoordinates[1].y = fVerts[(Bounding[1]*3)+1]; pNode->vBoundingCoordinates[1].z = fVerts[(Bounding[1]*3)+2]; pNode->vBoundingCoordinates[2].x = fVerts[(Bounding[2]*3)]; pNode->vBoundingCoordinates[2].y = fVerts[(Bounding[2]*3)+1]; pNode->vBoundingCoordinates[2].z = fVerts[(Bounding[2]*3)+2]; pNode->vBoundingCoordinates[3].x = fVerts[(Bounding[3]*3)]; pNode->vBoundingCoordinates[3].y = fVerts[(Bounding[3]*3)+1]; pNode->vBoundingCoordinates[3].z = fVerts[(Bounding[3]*3)+2]; pNode->bType = uiNodeType;
现在我们还没有处理叶节点,一旦我们分配了叶节点,我们将返回调用函数,在真实的世界里,你 或许希望得到一个指向数组或三角形的叶节点指针,如果你仔细看过NODE结构,你将注意变量 uiVertexStrip1...4,如果你愿意的话,可以在里面填充三角形,. if(uiNodeType == LEAF_TYPE) { return; } else { 下面,我们需要处理节点的子节点unsigned int BoundingBox[4]; TotalTreeID++; pNode->uiBranches[0] = TotalTreeID; //Top-Left i.e. b[0] BoundingBox[0] = Bounding[0]; //Between b[0] and b[1] BoundingBox[1] = Bounding[0]+((Bounding[1]-Bounding[0])/2); //[between b[0] and b[2] BoundingBox[2] = Bounding[0]+((Bounding[2]-Bounding[0])/2); //middle of node BoundingBox[3] = Bounding[0]+((Bounding[2]-Bounding[0])/2)+((Bounding[1]-Bounding[0])/2); CreateNode(BoundingBox,NodeID,TotalTreeID); //很简单,自己看吧, //****************************************************************************** TotalTreeID++; pNode->uiBranches[1] = TotalTreeID; // Between b[0] and b[1] BoundingBox[0] = Bounding[0]+((Bounding[1]-Bounding[0])/2); //Top-Right i.e. b[1] BoundingBox[1] = Bounding[1]; //middle of node BoundingBox[2] = Bounding[0]+((Bounding[2]-Bounding[0])/2)+((Bounding[1]-Bounding[0])/2); //between b[1] & b[3] BoundingBox[3] = Bounding[0]+((Bounding[2]-Bounding[0])/2)+((Bounding[1]-Bounding[0])); CreateNode(BoundingBox,NodeID,TotalTreeID); //****************************************************************************** TotalTreeID++; pNode->uiBranches[2] = TotalTreeID; //between b[0] and b[2] BoundingBox[0] = Bounding[0]+((Bounding[2]-Bounding[0])/2); //middle of node BoundingBox[1] = Bounding[0]+((Bounding[2]-Bounding[0])/2)+((Bounding[1]-Bounding[0])/2); //Bottom-Left i.e. b[2] BoundingBox[2] = Bounding[2]; //between b[2] and b[3] BoundingBox[3] = Bounding[2]+((Bounding[3]-Bounding[2])/2); CreateNode(BoundingBox,NodeID,TotalTreeID); //****************************************************************************** TotalTreeID++; pNode->uiBranches[3] = TotalTreeID; //middle of node BoundingBox[0] = Bounding[0]+((Bounding[2]-Bounding[0])/2) +((Bounding[1]-Bounding[0])/2); //between b[1] and b[3] BoundingBox[1] = Bounding[0]+((Bounding[2]-Bounding[0])/2) + uiWidth; //between b[2] and b[3] BoundingBox[2] = Bounding[2]+((Bounding[3]-Bounding[2])/2); //Bottom-Right i.e. b[3] BoundingBox[3] = Bounding[3]; CreateNode(BoundingBox,NodeID,TotalTreeID); //All we need to do is return nothing and end CreateNode's curly brackets: return; }
相关文章推荐
- 详细介绍四叉树 Quadtrees(上)
- 详细介绍四叉树 Quadtrees(上)(转载)
- 详细介绍四叉树 Quadtrees(下)(转载)
- UVA 297 Quadtrees(四叉树建树、合并与遍历)
- UVA 297 Quadtrees(四叉树建树、合并与遍历)
- 什么是Quadtrees? 四叉树
- UVA 297 Quadtrees(四叉树建树、合并与遍历)
- 297 - Quadtrees (四叉树)
- UVA 297 Quadtrees(四叉树)
- 详细介绍四叉树 Quadtrees
- RabbitMQ基础概念详细介绍
- Nginx配置中运行与启动的详细介绍
- Java开源的混淆器:Proguard详细介绍
- StringUtils详细介绍
- activiti工作流数据库表详细介绍(23张表)
- Java中的位运算符、移位运算详细介绍
- wireshark怎么抓包、wireshark抓包详细图文教程,简单介绍(及wireshark与wireshark legacy差别 )
- mysql分区功能详细介绍,以及实例
- 详细介绍JAVA和C++区别
- jquery的each()详细介绍