Exemplo n.º 1
0
//---------------------------------------------------------------------------
BDOctTree::BDOctNode BDOctTree::CreateSubNodes(BDOctNode node) {
  for (int i = 0; i < 6; i++)
    node.neighbour[i] = 0;

  if ((node.tris.size() <= MinTris) || ((node.x2 - node.x1) <= MinNodeSize)) {
    memset(node.children, 0, sizeof(node.children[0]) * 8);
    return node;
  }

  float midx = (node.x1 + node.x2) * 0.5,
      midy = (node.y1 + node.y2) * 0.5,
      midz = (node.z1 + node.z2) * 0.5;

  std::vector<Triangle*>::iterator i;
  BDOctNode dummy;

  for (short index = 0; index < 8; index++) {
    dummy.x1 = ((index % 2) == 0) ? node.x1 : midx;
    dummy.x2 = ((index % 2) == 0) ? midx : node.x2;
    dummy.y1 = (index < 4) ? node.y1 : midy;
    dummy.y2 = (index < 4) ? midy : node.y2;
    dummy.z1 = ((index < 2) || (index == 4) || (index == 5)) ? node.z1 : midz;     //0,1,4,5
    dummy.z2 = ((index < 2) || (index == 4) || (index == 5)) ? midz : node.z2;

    // dummy.size = node.size * 0.25;

    BDAABB box(dummy.ToBox());

    for (i = node.tris.begin(); i != node.tris.end(); i++) {
      if (box.Intersect(*(*i)))
        dummy.tris.push_back(*i);
    }

    Nodes.push_back(CreateSubNodes(dummy));
    // Put this here because more sub nodes were just created!!!
    node.children[index] = Nodes.size() - 1;
    dummy.tris.clear();
  }

  return node;
}
Exemplo n.º 2
0
void QuadTreeNode::Insert(MyObject* _Obj)
{
	if (!m_Bounds.iContains(_Obj->getRect()))
	{
		return;
	}
	if (m_Child==NULL)
	{
		CreateSubNodes();
	}
	if (m_Child!=NULL)
	{
		for (int i =0;i<4;i++)
		{
			if (m_Child[i].m_Bounds.iContains(_Obj->getRect()))
			{
				m_Child[i].Insert(_Obj);
				return;
			}
		}
	}
	this->m_Contents->push_back(_Obj);
}
Exemplo n.º 3
0
//---------------------------------------------------------------------------
void BDOctTree::CreateOctTree(Triangle *t, const int numtris) {
  tris = t;
  Nodes.push_back(BDOctNode());

  Nodes[0].x1 = Nodes[0].x2 = tris[0].a->x;
  Nodes[0].y1 = Nodes[0].y2 = tris[0].a->y;
  Nodes[0].z1 = Nodes[0].z2 = tris[0].a->z;

  Triangle *lastTri = &tris[numtris];
  for (Triangle *index = tris; index != lastTri; index++) {
    if (index->a->x < Nodes[0].x1)
      Nodes[0].x1 = index->a->x;
    if (index->b->x < Nodes[0].x1)
      Nodes[0].x1 = index->b->x;
    if (index->c->x < Nodes[0].x1)
      Nodes[0].x1 = index->c->x;

    if (index->a->x > Nodes[0].x2)
      Nodes[0].x2 = index->a->x;
    if (index->b->x > Nodes[0].x2)
      Nodes[0].x2 = index->b->x;
    if (index->c->x > Nodes[0].x2)
      Nodes[0].x2 = index->c->x;


    if (index->a->y < Nodes[0].y1)
      Nodes[0].y1 = index->a->y;
    if (index->b->y < Nodes[0].y1)
      Nodes[0].y1 = index->b->y;
    if (index->c->y < Nodes[0].y1)
      Nodes[0].y1 = index->c->y;

    if (index->a->y > Nodes[0].y2)
      Nodes[0].y2 = index->a->y;
    if (index->b->y > Nodes[0].y2)
      Nodes[0].y2 = index->b->y;
    if (index->c->y > Nodes[0].y2)
      Nodes[0].y2 = index->c->y;


    if (index->a->z < Nodes[0].z1)
      Nodes[0].z1 = index->a->z;
    if (index->b->z < Nodes[0].z1)
      Nodes[0].z1 = index->b->z;
    if (index->c->z < Nodes[0].z1)
      Nodes[0].z1 = index->c->z;

    if (index->a->z > Nodes[0].z2)
      Nodes[0].z2 = index->a->z;
    if (index->b->z > Nodes[0].z2)
      Nodes[0].z2 = index->b->z;
    if (index->c->z > Nodes[0].z2)
      Nodes[0].z2 = index->c->z;

    Nodes[0].tris.push_back(index);
  }

  float rootsize = Nodes[0].x2 - Nodes[0].x1;
  if (rootsize < Nodes[0].y2 - Nodes[0].y1)
    rootsize = Nodes[0].y2 - Nodes[0].y1;
  if (rootsize < Nodes[0].z2 - Nodes[0].z1)
    rootsize = Nodes[0].z2 - Nodes[0].z1;

  rootsize *= 0.5;
  float mid = (Nodes[0].x2 + Nodes[0].x1);
  BDOctTree::Nodes[0].x1 = mid * 0.5 - rootsize;
  BDOctTree::Nodes[0].x2 = mid * 0.5 + rootsize;
  mid = (Nodes[0].y2 + Nodes[0].y1);
  BDOctTree::Nodes[0].y1 = mid * 0.5 - rootsize;
  BDOctTree::Nodes[0].y2 = mid * 0.5 + rootsize;
  mid = (Nodes[0].z2 + Nodes[0].z1);
  BDOctTree::Nodes[0].z1 = mid * 0.5 - rootsize;
  BDOctTree::Nodes[0].z2 = mid * 0.5 + rootsize;

  // Calculate size
  // Nodes[0].size = sqrt(12*rootsize*rootsize);

  Nodes[0] = CreateSubNodes(Nodes[0]);
  DetermineNeighbours();
}