- 大家好, 我正在尝试为场景图实现“深度优先搜索”。 这就是我到目前为止所做的 - 但我很难想象如何跟踪当前元素在图形中的深度。假设我可以将order.size()计为第一个分支的深度 - 但是当代码再次跳转到下一个分支时,如何弹出元素?任何暗示都将非常感激。非常感谢。
//======================================================================
//depth first search
//======================================================================
// clean start - init visited flags in joints
for (int i = 0 ; i < m_joints.size(); i++){m_joints[i]->visited = false;}
// joint indices
vector<int> stack;
vector<int> order;
for(int i = 0; i < m_joints.size(); i++)
{
if(!m_joints[i]->visited)
{
stack.push_back(i);
while(!stack.empty())
{
int top = stack.back();
stack.pop_back();
if(m_joints[top]->visited)
{
continue;
}
m_joints[top]->visited = true;
order.push_back(top);
// need to know how deep I am inside of the scene graph here
// update transformation matrix here
// draw joint here
for(int j = 0 ; j < m_joints[top]->children.size();j++)//all neighbours of top
{
if(!m_joints[top]->children[j]->visited)
{
stack.push_back(m_joints[top]->children[j]->listPosition);
}
}
}
}
}
答案 0 :(得分:2)
如果我理解你的问题,你可以为每个元素添加一个整数变量“depth”,并在每次元素的深度变化时更新它。此外,您始终可以询问您的元素当前的深度