简体   繁体   中英

Depth First Search - Scene Graph - Whats my current depth?

-- Hi All, I am trying to implement a "Depth First Search" for a Scene Graph. This is what I have so far - but I am stuck figuring how to keep track of how deep the current element is inside the graph. Lets say I could count the order.size() as depth for the first branch - but how do I pop of elements when the code jumps up again and into the next branch? Any hint would be greatly appreciated. Thanks a lot in advance.

//======================================================================
//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); 
                }
            }
        }
    }
}

If I understood well your problem, you can just add an integer variable "depth" to each element and update it every time element's depth is changing. In addition, you can always ask your element at which depth it is currently

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM