2012-10-03 62 views
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); 
       } 
      } 
     } 
    } 
} 

回答

2

如果我深知你的問題,你可以再補充一個整型變量「深度」每個元素和更新每次元素的深度變化。此外,你總是可以問你的元素在哪個深度,它是目前

+0

謝謝你的答案 - 你能舉一個例子,看看這可能嗎?我想我不完全明白... – timkado