我正在尝试为我的barnes-hut算法实现实现四叉树。我不确定到目前为止我写的代码是不是很好的实现 - 它很脏,而且往往很慢。当然我也知道结果可能是正确的,但如果有人可以看一下并给我一些提示,我会很高兴。因为整个代码非常大并且还包括OpenGL可视化,所以我只在这里放置重要的部分。我希望没问题。
Quadtree.h
Quadtree* trees[2][2][2];
glm::vec3 vBoundriesBox[8];
bool leaf;
float combined_weight = 0;
std::vector<Element*> objects;
Quadtree.cpp
#define MAX_LEVELS 5
void Quadtree::AddObject(Element* object)
{
this->objects.push_back(object);
}
void Quadtree::Update()
{
if(this->objects.size()<=1 || level > MAX_LEVELS)
{
for(Element* Element:this->objects)
{
Element->parent_group = this;
this->combined_weight += Element->weight;
}
return;
}
if(leaf)
{
glm::vec3 newBoundries[8];
// 0,0,0
newBoundries[0] = vBoundriesBox[0];
newBoundries[1] = glm::vec3(vBoundriesBox[0].x + (std::abs(vBoundriesBox[1].x - vBoundriesBox[0].x)/2.0f),vBoundriesBox[0].y,vBoundriesBox[0].z );
newBoundries[2] = glm::vec3(vBoundriesBox[0].x + (std::abs(vBoundriesBox[1].x - vBoundriesBox[0].x)/2.0f),vBoundriesBox[0].y,vBoundriesBox[0].z - (std::abs(vBoundriesBox[3].z - vBoundriesBox[0].z)/2.0f));
newBoundries[3] = glm::vec3(vBoundriesBox[0].x,vBoundriesBox[0].y,vBoundriesBox[0].z - (std::abs(vBoundriesBox[3].z - vBoundriesBox[0].z)/2.0f));
newBoundries[4] = glm::vec3(vBoundriesBox[0].x,vBoundriesBox[0].y - (std::abs(vBoundriesBox[4].y - vBoundriesBox[0].y)/2.0f), vBoundriesBox[0].z);
newBoundries[5] = glm::vec3(vBoundriesBox[0].x + (std::abs(vBoundriesBox[1].x - vBoundriesBox[0].x)/2.0f),vBoundriesBox[0].y - (std::abs(vBoundriesBox[4].y - vBoundriesBox[0].y)/2.0f),vBoundriesBox[0].z);
newBoundries[6] = glm::vec3(vBoundriesBox[0].x + (std::abs(vBoundriesBox[1].x - vBoundriesBox[0].x)/2.0f),vBoundriesBox[0].y - (std::abs(vBoundriesBox[4].y - vBoundriesBox[0].y)/2.0f),vBoundriesBox[0].z - (std::abs(vBoundriesBox[3].z - vBoundriesBox[0].z)/2.0f));
newBoundries[7] = glm::vec3(vBoundriesBox[0].x,vBoundriesBox[0].y - (std::abs(vBoundriesBox[4].y - vBoundriesBox[0].y)/2.0f),vBoundriesBox[0].z - (std::abs(vBoundriesBox[3].z - vBoundriesBox[0].z)/2.0f));
trees[0][0][0] = new Quadtree(this, newBoundries, level+1);
// 0,1,0
newBoundries[0] = glm::vec3(vBoundriesBox[1].x - (std::abs(vBoundriesBox[1].x - vBoundriesBox[0].x)/2.0f),vBoundriesBox[0].y,vBoundriesBox[1][2]);
newBoundries[1] = vBoundriesBox[1];
newBoundries[2] = glm::vec3(vBoundriesBox[1].x,vBoundriesBox[1].y,vBoundriesBox[1].z - (std::abs(vBoundriesBox[2][2] - vBoundriesBox[1][2])/2.0f));
newBoundries[3] = glm::vec3(vBoundriesBox[1].x - (std::abs(vBoundriesBox[1].x - vBoundriesBox[0].x)/2.0f),vBoundriesBox[1][1],vBoundriesBox[1][2] - (std::abs(vBoundriesBox[2].z - vBoundriesBox[1].z)/2.0f));
newBoundries[4] = glm::vec3(vBoundriesBox[1].x - (std::abs(vBoundriesBox[1].x - vBoundriesBox[0].x)/2.0f),vBoundriesBox[1].y - (std::abs(vBoundriesBox[5].y - vBoundriesBox[1].y)/2.0f),vBoundriesBox[1].z);
newBoundries[5] = glm::vec3(vBoundriesBox[1].x,vBoundriesBox[1].y - (std::abs(vBoundriesBox[5].y - vBoundriesBox[1].y)/2.0f), vBoundriesBox[1].z);
newBoundries[6] = glm::vec3(vBoundriesBox[1].x,vBoundriesBox[1].y - (std::abs(vBoundriesBox[5].y - vBoundriesBox[1].y)/2.0f),vBoundriesBox[1].z - (std::abs(vBoundriesBox[2].z - vBoundriesBox[1].z)/2.0f));
newBoundries[7] = glm::vec3(vBoundriesBox[1].x - (std::abs(vBoundriesBox[1].x - vBoundriesBox[0].x)/2.0f),vBoundriesBox[1].y - (std::abs(vBoundriesBox[5].y - vBoundriesBox[1].y)/2.0f),vBoundriesBox[1].z - (std::abs(vBoundriesBox[2].z - vBoundriesBox[1].z)/2.0f));
trees[0][1][0] = new Quadtree(this, newBoundries, level+1);
// 0,0,1
newBoundries[0] = glm::vec3(vBoundriesBox[3].x,vBoundriesBox[3].y,vBoundriesBox[3].z + (std::abs(vBoundriesBox[3].z - vBoundriesBox[0].z)/2.0f));
newBoundries[1] = glm::vec3(vBoundriesBox[3].x + (std::abs(vBoundriesBox[2].x - vBoundriesBox[3].x)/2.0f),vBoundriesBox[3].y,vBoundriesBox[3].z + (std::abs(vBoundriesBox[3].z - vBoundriesBox[0].z)/2.0f));
newBoundries[2] = glm::vec3(vBoundriesBox[3].x + (std::abs(vBoundriesBox[2].x - vBoundriesBox[3].x)/2.0f),vBoundriesBox[3].y,vBoundriesBox[3].z );
newBoundries[3] = vBoundriesBox[3];
newBoundries[4] = glm::vec3(vBoundriesBox[3].x,vBoundriesBox[3].y - (std::abs(vBoundriesBox[7].y - vBoundriesBox[3].y)/2.0f),vBoundriesBox[3].z + (std::abs(vBoundriesBox[3].z - vBoundriesBox[0].z)/2.0f));
newBoundries[5] = glm::vec3(vBoundriesBox[3].x + (std::abs(vBoundriesBox[2].x - vBoundriesBox[3].x)/2.0f),vBoundriesBox[3].y - (std::abs(vBoundriesBox[7].y - vBoundriesBox[3].y)/2.0f),vBoundriesBox[3].z + (std::abs(vBoundriesBox[3].z - vBoundriesBox[0].z)/2.0f));
newBoundries[6] = glm::vec3(vBoundriesBox[3].x + (std::abs(vBoundriesBox[2].x - vBoundriesBox[3].x)/2.0f),vBoundriesBox[3].y - (std::abs(vBoundriesBox[7].y - vBoundriesBox[3].y)/2.0f),vBoundriesBox[3].z );
newBoundries[7] = glm::vec3(vBoundriesBox[3].x,vBoundriesBox[3].y - (std::abs(vBoundriesBox[7].y - vBoundriesBox[3].y)/2.0f),vBoundriesBox[3].z);
trees[0][0][1] = new Quadtree(this, newBoundries, level+1);
// 0,1,1
newBoundries[0] = glm::vec3(vBoundriesBox[2].x - (std::abs(vBoundriesBox[2].x - vBoundriesBox[3].x)/2.0f),vBoundriesBox[2].y,vBoundriesBox[2].z + (std::abs(vBoundriesBox[2][2] - vBoundriesBox[1][2])/2.0f));
newBoundries[1] = glm::vec3(vBoundriesBox[2].x,vBoundriesBox[2].y,vBoundriesBox[2].z + (std::abs(vBoundriesBox[2].z - vBoundriesBox[1].z)/2.0f));
newBoundries[2] = vBoundriesBox[2];
newBoundries[3] = glm::vec3(vBoundriesBox[2].x - (std::abs(vBoundriesBox[2].x - vBoundriesBox[3].x)/2.0f),vBoundriesBox[2].y,vBoundriesBox[2].z );
newBoundries[4] = glm::vec3(vBoundriesBox[2].x - (std::abs(vBoundriesBox[2].x - vBoundriesBox[3].x)/2.0f),vBoundriesBox[2].y - (std::abs(vBoundriesBox[6].y - vBoundriesBox[2].y)/2.0f),vBoundriesBox[2].z + (std::abs(vBoundriesBox[2].z - vBoundriesBox[1].z)/2.0f));
newBoundries[5] = glm::vec3(vBoundriesBox[2].x,vBoundriesBox[2].y - (std::abs(vBoundriesBox[6].y - vBoundriesBox[2].y)/2.0f),vBoundriesBox[2].z + (std::abs(vBoundriesBox[2].z - vBoundriesBox[1].z)/2.0f));
newBoundries[6] = glm::vec3(vBoundriesBox[2].x,vBoundriesBox[2].y - (std::abs(vBoundriesBox[6].y - vBoundriesBox[2].y)/2.0f),vBoundriesBox[2].z);
newBoundries[7] = glm::vec3(vBoundriesBox[2].x - (std::abs(vBoundriesBox[2].x - vBoundriesBox[3].x)/2.0f),vBoundriesBox[2].y - (std::abs(vBoundriesBox[6].y - vBoundriesBox[2].y)/2.0f),vBoundriesBox[2].z);
trees[0][1][1] = new Quadtree(this, newBoundries, level+1);
// 1,0,0
newBoundries[0] = glm::vec3(vBoundriesBox[4].x,vBoundriesBox[4].y + (std::abs(vBoundriesBox[4].y - vBoundriesBox[0].y)/2.0f), vBoundriesBox[4].z);
newBoundries[1] = glm::vec3(vBoundriesBox[4].x + (std::abs(vBoundriesBox[5].x - vBoundriesBox[4].x)/2.0f),vBoundriesBox[4].y + (std::abs(vBoundriesBox[4].y - vBoundriesBox[0].y)/2.0f),vBoundriesBox[4].z);
newBoundries[2] = glm::vec3(vBoundriesBox[4].x + (std::abs(vBoundriesBox[5].x - vBoundriesBox[4].x)/2.0f),vBoundriesBox[4].y + (std::abs(vBoundriesBox[4].y - vBoundriesBox[0].y)/2.0f),vBoundriesBox[4].z - (std::abs(vBoundriesBox[7].z - vBoundriesBox[4].z)/2.0f));
newBoundries[3] = glm::vec3(vBoundriesBox[4].x,vBoundriesBox[4].y + (std::abs(vBoundriesBox[4].y - vBoundriesBox[0].y)/2.0f),vBoundriesBox[4].z - (std::abs(vBoundriesBox[7].z - vBoundriesBox[4].z)/2.0f));
newBoundries[4] = vBoundriesBox[4];
newBoundries[5] = glm::vec3(vBoundriesBox[4].x + (std::abs(vBoundriesBox[5].x - vBoundriesBox[4].x)/2.0f),vBoundriesBox[4].y,vBoundriesBox[4].z );
newBoundries[6] = glm::vec3(vBoundriesBox[4].x + (std::abs(vBoundriesBox[5].x - vBoundriesBox[4].x)/2.0f),vBoundriesBox[4].y,vBoundriesBox[4].z - (std::abs(vBoundriesBox[7].z - vBoundriesBox[4].z)/2.0f));
newBoundries[7] = glm::vec3(vBoundriesBox[4].x,vBoundriesBox[4].y,vBoundriesBox[4].z - (std::abs(vBoundriesBox[7].z - vBoundriesBox[4].z)/2.0f));
trees[1][0][0] = new Quadtree(this, newBoundries, level+1);
// 1,1,0
newBoundries[0] = glm::vec3(vBoundriesBox[5].x - (std::abs(vBoundriesBox[4].x - vBoundriesBox[5].x)/2.0f),vBoundriesBox[5].y + (std::abs(vBoundriesBox[5].y - vBoundriesBox[1].y)/2.0f),vBoundriesBox[5].z);
newBoundries[1] = glm::vec3(vBoundriesBox[5].x,vBoundriesBox[5].y + (std::abs(vBoundriesBox[5].y - vBoundriesBox[1].y)/2.0f),vBoundriesBox[5].z);
newBoundries[2] = glm::vec3(vBoundriesBox[5].x,vBoundriesBox[5].y + (std::abs(vBoundriesBox[5].y - vBoundriesBox[1].y)/2.0f),vBoundriesBox[5].z - (std::abs(vBoundriesBox[5].z - vBoundriesBox[6].z)/2.0f));
newBoundries[3] = glm::vec3(vBoundriesBox[5].x - (std::abs(vBoundriesBox[4].x - vBoundriesBox[5].x)/2.0f),vBoundriesBox[5].y + (std::abs(vBoundriesBox[5].y - vBoundriesBox[1].y)/2.0f),vBoundriesBox[5].z - (std::abs(vBoundriesBox[5].z - vBoundriesBox[6].z)/2.0f));
newBoundries[4] = glm::vec3(vBoundriesBox[5].x - (std::abs(vBoundriesBox[4].x - vBoundriesBox[5].x)/2.0f),vBoundriesBox[5].y,vBoundriesBox[5].z);
newBoundries[5] = vBoundriesBox[5];
newBoundries[6] = glm::vec3(vBoundriesBox[5].x,vBoundriesBox[5].y,vBoundriesBox[5].z - (std::abs(vBoundriesBox[6].z - vBoundriesBox[5].z)/2.0f));
newBoundries[7] = glm::vec3(vBoundriesBox[5].x - (std::abs(vBoundriesBox[4].x - vBoundriesBox[5].x)/2.0f),vBoundriesBox[5].y,vBoundriesBox[5].z - (std::abs(vBoundriesBox[5].z - vBoundriesBox[6].z)/2.0f));
trees[1][1][0] = new Quadtree(this, newBoundries, level+1);
// 1,0,1
newBoundries[0] = glm::vec3(vBoundriesBox[7].x,vBoundriesBox[7].y + (std::abs(vBoundriesBox[7].y - vBoundriesBox[3].y)/2.0f),vBoundriesBox[7].z + (std::abs(vBoundriesBox[7].z - vBoundriesBox[4].z)/2.0f));
newBoundries[1] = glm::vec3(vBoundriesBox[7].x + (std::abs(vBoundriesBox[7].x - vBoundriesBox[6].x)/2.0f),vBoundriesBox[7].y + (std::abs(vBoundriesBox[7].y - vBoundriesBox[3].y)/2.0f),vBoundriesBox[7].z + (std::abs(vBoundriesBox[4].z - vBoundriesBox[7].z)/2.0f));
newBoundries[2] = glm::vec3(vBoundriesBox[7].x + (std::abs(vBoundriesBox[7].x - vBoundriesBox[6].x)/2.0f),vBoundriesBox[7].y + (std::abs(vBoundriesBox[7].y - vBoundriesBox[3].y)/2.0f),vBoundriesBox[7].z );
newBoundries[3] = glm::vec3(vBoundriesBox[7].x,vBoundriesBox[7].y + (std::abs(vBoundriesBox[7].y - vBoundriesBox[3].y)/2.0f),vBoundriesBox[7].z );
newBoundries[4] = glm::vec3(vBoundriesBox[7].x,vBoundriesBox[7].y,vBoundriesBox[7][2] + (std::abs(vBoundriesBox[7].z - vBoundriesBox[4].z)/2.0f));
newBoundries[5] = glm::vec3(vBoundriesBox[7].x + (std::abs(vBoundriesBox[7].x - vBoundriesBox[6].x)/2.0f),vBoundriesBox[7].y,vBoundriesBox[7].z + (std::abs(vBoundriesBox[7].z - vBoundriesBox[4].z)/2.0f));
newBoundries[6] = glm::vec3(vBoundriesBox[7].x + (std::abs(vBoundriesBox[7].x - vBoundriesBox[6].x)/2.0f),vBoundriesBox[7].y,vBoundriesBox[7].z );
newBoundries[7] = vBoundriesBox[7];
trees[1][0][1] = new Quadtree(this, newBoundries, level+1);
// 1,1,1
newBoundries[0] = glm::vec3(vBoundriesBox[6].x - (std::abs(vBoundriesBox[6].x - vBoundriesBox[7].x)/2.0f),vBoundriesBox[6].y + (std::abs(vBoundriesBox[6].y - vBoundriesBox[2].y)/2.0f),vBoundriesBox[6].z + (std::abs(vBoundriesBox[6].z - vBoundriesBox[5].z)/2.0f));
newBoundries[1] = glm::vec3(vBoundriesBox[6].x,vBoundriesBox[6].y + (std::abs(vBoundriesBox[6].y - vBoundriesBox[2].y)/2.0f),vBoundriesBox[6].z + (std::abs(vBoundriesBox[6].z - vBoundriesBox[5].z)/2.0f));
newBoundries[2] = glm::vec3(vBoundriesBox[6].x,vBoundriesBox[6].y + (std::abs(vBoundriesBox[6].y - vBoundriesBox[2].y)/2.0f),vBoundriesBox[6].z);
newBoundries[3] = glm::vec3(vBoundriesBox[6].x - (std::abs(vBoundriesBox[6].x - vBoundriesBox[7].x)/2.0f),vBoundriesBox[6].y + (std::abs(vBoundriesBox[6].y - vBoundriesBox[2].y)/2.0f),vBoundriesBox[6].z);
newBoundries[4] = glm::vec3(vBoundriesBox[6].x - (std::abs(vBoundriesBox[6].x - vBoundriesBox[7].x)/2.0f),vBoundriesBox[6].y,vBoundriesBox[6].z + (std::abs(vBoundriesBox[6].z - vBoundriesBox[5].z)/2.0f));
newBoundries[5] = glm::vec3(vBoundriesBox[6].x,vBoundriesBox[6].y,vBoundriesBox[6].z + (std::abs(vBoundriesBox[6].z - vBoundriesBox[5].z)/2.0f));
newBoundries[6] = vBoundriesBox[6];
newBoundries[7] = glm::vec3(vBoundriesBox[6].x - (std::abs(vBoundriesBox[6].x - vBoundriesBox[7].x)/2.0f),vBoundriesBox[6].y,vBoundriesBox[6].z );
trees[1][1][1] = new Quadtree(this, newBoundries, level+1);
leaf = false;
}
while (!this->objects.empty())
{
Element* obj = this->objects.back();
this->objects.pop_back();
if(contains(trees[0][0][0],obj))
{
trees[0][0][0]->AddObject(obj);
trees[0][0][0]->combined_weight += obj->weight;
} else if(contains(trees[0][0][1],obj))
{
trees[0][0][1]->AddObject(obj);
trees[0][0][1]->combined_weight += obj->weight;
} else if(contains(trees[0][1][0],obj))
{
trees[0][1][0]->AddObject(obj);
trees[0][1][0]->combined_weight += obj->weight;
} else if(contains(trees[0][1][1],obj))
{
trees[0][1][1]->AddObject(obj);
trees[0][1][1]->combined_weight += obj->weight;
} else if(contains(trees[1][0][0],obj))
{
trees[1][0][0]->AddObject(obj);
trees[1][0][0]->combined_weight += obj->weight;
} else if(contains(trees[1][0][1],obj))
{
trees[1][0][1]->AddObject(obj);
trees[1][0][1]->combined_weight += obj->weight;
} else if(contains(trees[1][1][0],obj))
{
trees[1][1][0]->AddObject(obj);
trees[1][1][0]->combined_weight += obj->weight;
} else if(contains(trees[1][1][1],obj))
{
trees[1][1][1]->AddObject(obj);
trees[1][1][1]->combined_weight += obj->weight;
}
}
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
for(int k=0;k<2;k++)
{
trees[i][j][k]->Update();
}
}
}
}
bool Quadtree::contains(Quadtree* child, Element* object)
{
if(object->pos[0] >= child->vBoundriesBox[0][0] && object->pos[0] <= child->vBoundriesBox[1][0] &&
object->pos[1] >= child->vBoundriesBox[4][1] && object->pos[1] <= child->vBoundriesBox[0][1] &&
object->pos[2] >= child->vBoundriesBox[3][2] && object->pos[2] <= child->vBoundriesBox[0][2])
return true;
return false;
}
在我的主文件中,我将元素添加到树中:
void AddTreeElements(Quadtree* tree, glm::vec3* boundries, Graph& graph)
{
for(auto& node:graph.NodeVector())
{
node.parent_group = nullptr;
if(node.pos[0] < boundries[1][0] && node.pos[0] > boundries[0][0] &&
node.pos[1] > boundries[4][1] && node.pos[1] < boundries[1][1] &&
node.pos[2] < boundries[0][2] && node.pos[2] > boundries[3][2])
{
tree->AddObject(&node.second);
continue;
}
if(node.pos[0] < boundries[0][0])
{
boundries[0][0] = node.pos[0]-1.0f;
boundries[3][0] = node.pos[0]-1.0f;
boundries[4][0] = node.pos[0]-1.0f;
boundries[7][0] = node.pos[0]-1.0f;
}
else if(node.pos[0] > boundries[1][0])
{
boundries[1][0] = node.pos[0]+1.0f;
boundries[2][0] = node.pos[0]+1.0f;
boundries[5][0] = node.pos[0]+1.0f;
boundries[6][0] = node.pos[0]+1.0f;
}
if(node.pos[1] < boundries[4][1])
{
boundries[4][1] = node.pos[1]-1.0f;
boundries[5][1] = node.pos[1]-1.0f;
boundries[6][1] = node.pos[1]-1.0f;
boundries[7][1] = node.pos[1]-1.0f;
}
else if(node.pos[1] > boundries[0][1])
{
boundries[0][1] = node.pos[1]+1.0f;
boundries[1][1] = node.pos[1]+1.0f;
boundries[2][1] = node.pos[1]+1.0f;
boundries[3][1] = node.pos[1]+1.0f;
}
if(node.pos[2] < boundries[3][2])
{
boundries[2][2] = node.pos[2]-1.0f;
boundries[3][2] = node.pos[2]-1.0f;
boundries[6][2] = node.pos[2]-1.0f;
boundries[7][2] = node.pos[2]-1.0f;
}
else if(node.pos[2] > boundries[0][2])
{
boundries[0][2] = node.pos[2]+1.0f;
boundries[1][2] = node.pos[2]+1.0f;
boundries[4][2] = node.pos[2]+1.0f;
boundries[5][2] = node.pos[2]+1.0f;
}
}
}
因为它是八叉树我使用框来可视化它并测试它是否正常工作。此外,boundries
数组代表此框,这就是我编号角的方式:
至于速度,我通过随机放置新节点进行样本测试,这就是时间/元素图的样子:
当我更改MAX_LEVELS
值时,这是正确的行为吗?