0
- 大家好, 我想为场景图实现“深度优先搜索”。 这就是我到目前为止 - 但我坚持如何跟踪图中的当前元素的深度。比方说,我可以将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);
}
}
}
}
}
谢谢你的答案 - 你能举一个例子,看看这可能吗?我想我不完全明白... – timkado