- 大家好,我正在尝试为场景图实现“深度优先搜索”。这就是我到目前为止所拥有的——但我一直在思考如何跟踪当前元素在图表中的深度。假设我可以将 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);
}
}
}
}
}