这里函数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] = Bou
 【责编:admin】
|
 |
|
 |
|
多数的Windows程序都需要Windows.h和Windowsx.h这两个头文件,要确保使用它们。当然,你还需要其它......
|
|
|
|
|
为什么要研究攻击行为在人类有记载的5600年的历史中,共计发生了14,400次战争;今天,平均一天要发生............
|
|
|
|
|
|
|
|
|
|
|
|
|