首页>
技术资讯>
详情

详细介绍四叉树 Quadtrees(下)

2016-05-29 来源:CloudBest 阅读量: 460
关键词: 程序设计


  这里函数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)];
热门推荐 查看更多