Jump to content
  • Advertisement
Sign in to follow this  
belfegor

Bone parent indices to linked list

This topic is 1537 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have an array of bone parent indices (where i marked root bone as -1). At some point i need to traverse thru all children of some arbitrary bone but this seems tough to do with this setup. I cannot come up with algorithm to create linked list like hierarchy from this , one like in DX9 animation system:

typedef struct D3DXFRAME {
...
D3DXFRAME* pFrameSibling;
D3DXFRAME* pFrameFirstChild;
};

 

where i can simply use recursion to achieve this.

 

Any pointers?

Share this post


Link to post
Share on other sites
Advertisement
A hierarchy is a tree, not a list. If you want to reconstruct a tree you first need some method like AddChild, then you use a simple map of indices to tree nodes (actually a array/vector could work just fine). Map and add children as you go. Pseudocode:
foreach(node: nodes)
{
    map[node.index] = node;
    if(node.parent >= 0)
        map[node.parent].AddChild(node);
}
This works if no child comes before its anchestor. Otherwise you need two passes: Create nodes first, then assign to parents.
I don't think you can use a (sensible) recursive algorithm here. A recursion needs a hierarchical relation, which you actually want to figure out wink.png

Alternatively, construct a list of indices of the children for each bone, then you can work with the initial array. Assuming children have consecutive indices, then pair of (startindex, endindex) suffices. The latter is the case if e.g. the indices were created using a level-order tree traversal.

Share this post


Link to post
Share on other sites


I don't think you can use a (sensible) recursive algorithm here. A recursion needs a hierarchical relation, which you actually want to figure out wink.png

 

I have explain it badly.

Before, with dx9 animation system i was able to traverse thru only children of some bone (and childrens children... and their children) using recursion because that was with tree structure, and now i cannot do that easely with this array. I want to imitate "old" way by building this "tree" which i dont see how yet, still working on it. Thanks anyway.

Share this post


Link to post
Share on other sites

I don't think it is necessary to store any explicit hierarchy of the bones - In my animation system I just have a list of bones, and each bone has an associated parent bone index (in the list). This stores the hierarchy implicitly. Then, when computing the final matrices for each bone, I just iterate through the list and lazily compute the concatenated object-space matrices of each bone (and any parent bones, if they have not yet been computed). There doesn't seem to be any reason to store the skeleton hierarchy in a traditional tree structure.

Share this post


Link to post
Share on other sites

I have no problem with animation, i was thinking about IK . Then for one character i need to go thru whole structure couple of times in one frame (head facing certain target, legs position on uneven terrain, arms reaching for items...) , with "old" way i could update only what is needed.

Share this post


Link to post
Share on other sites

If you have only a list of frames (bones/nodes) with parent indices to start with, and you want to build a DX9-like hierarchy, you can do something like:

 

1. create a D3DXFRAME* (or equivalent) for the root frame, setting its children and siblings to NULL.

Then:

2. go through your list, looking for parent index = root index.

3. when a child of root is found, create a D3DXFRAME* for the child, setting newFrame->pFrameFirstChild and newFrame->pFrameSibling to NULL.

4. if root frame's pFrameFirstChild is NULL, set pFrameFirstChild to the child frame pointer.

5. if root frame's pFrameFirstChild isn't NULL, check pFrameFirstChild->pFrameSibling.

6. if pFrameSibling is NULL, set pFrameSibling to the child frame pointer.

7. if pFrameSibling isn't NULL, check pFrameSibling->pFrameSibling. (goto step 6)

 

Once you've searched the list for the root frame's index (as a parent), use the same steps to search for root->pFrameFirstChild's index as a parent, then pFrameFirstChild->pFrameSibling, etc. To ensure you've gotten everyone, you can remove a child from the list after you've created a frame for it and added that frame as a child or sibling. If you've done it all correctly, the original list should be empty (if it's a true hierarchy to begin with).

Edited by Buckeye

Share this post


Link to post
Share on other sites

I think i got it without creating tree like hierarchy.

#include <iostream>
#include <vector>

struct Bone
{
    int data;
};

std::vector<int>  vParentIndices;
std::vector<Bone> vBones;

void printChildData(int parentInx)
{
    for(std::size_t i = 0; i < vParentIndices.size(); ++i) // my children
    {
        if((vParentIndices[i] == parentInx) && (i != parentInx))
        {
            std::cout << vBones[i].data << std::endl;
            printChildData(i); // child children
        }
    }
}

int main()
{
    // copied from my character bone structure
    vParentIndices.resize(47);
    vParentIndices[0]  = -1;
    vParentIndices[1]  = 0;
    vParentIndices[2]  = 1;
    vParentIndices[3]  = 2;
    vParentIndices[4]  = 3;
    vParentIndices[5]  = 4;
    vParentIndices[6]  = 5;
    vParentIndices[7]  = 2;
    vParentIndices[8]  = 7;
    vParentIndices[9]  = 8;
    vParentIndices[10] = 9;
    vParentIndices[11] = 2;
    vParentIndices[12] = 11;
    vParentIndices[13] = 12;
    vParentIndices[14] = 13;
    vParentIndices[15] = 14;
    vParentIndices[16] = 15;
    vParentIndices[17] = 15;
    vParentIndices[18] = 15;
    vParentIndices[19] = 15;
    vParentIndices[20] = 14;
    vParentIndices[21] = 20;
    vParentIndices[22] = 21;
    vParentIndices[23] = 22;
    vParentIndices[24] = 23;
    vParentIndices[25] = 24;
    vParentIndices[26] = 25;
    vParentIndices[27] = 23;
    vParentIndices[28] = 27;
    vParentIndices[29] = 28;
    vParentIndices[30] = 23;
    vParentIndices[31] = 30;
    vParentIndices[32] = 31;
    vParentIndices[33] = 14;
    vParentIndices[34] = 33;
    vParentIndices[35] = 34;
    vParentIndices[36] = 35;
    vParentIndices[37] = 36;
    vParentIndices[38] = 37;
    vParentIndices[39] = 38;
    vParentIndices[40] = 36;
    vParentIndices[41] = 40;
    vParentIndices[42] = 41;
    vParentIndices[43] = 36;
    vParentIndices[44] = 43;
    vParentIndices[45] = 44;
    vParentIndices[46] = 2;

    vBones.resize(47);
    for(std::size_t i = 0; i < 47; ++i)
    {
        vBones[i].data = i; // data = inx
    }

    //printChildData(-1);
    printChildData(15);

    return 0;
}

 

Can someone confirm if this could fail?

 

Thanks everybody.

Share this post


Link to post
Share on other sites

Can someone confirm if this could fail?

 

Not sure what you mean by "fail." There are, of course, ways it could "fail." You could enter a wrong number. You could mistype some line of code. You could change the number of parent indices and fail to change the hard-coded "47" you have several places. Etc., etc.

 

As an experiment, as coded, assuming all the numbers are correct, to avoid setting up a hierarchy, it should work. However, just as a comment, your method is a very inefficient way to determine the children of a node. You access the entire array of possibilities each time you want to know the children of a single node.

 

In any case, to generalize a bit more, if you want to use a std::vector, it'll make your code easier to revise:

vParentIndices.clear();
vParentIndices.push_back( -1 );
vParentIndices.push_back( 0 );
vParentIndices.push_back( 1 );
...
vParentIndices.push_back( 2 );

vBones.clear();
for( size_t i = 0; i < vParentIndices.size(); ++i) vBones.push_back(i);
Edited by Buckeye

Share this post


Link to post
Share on other sites
^ I cannot follow steps from your post #6
 
 
#include <iostream>
#include <vector>

struct Bone
{
    int myInx; // tmp data to print later

    Bone* sibling;
    Bone* child;
};

std::vector<int>  vParentIndices;

void buildTree(Bone* tree)
{
    for(std::size_t i = 0; i < vParentIndices.size(); ++i)
    {
        if(tree->myInx == vParentIndices[i])
        {
            Bone* p = new Bone;
            p->myInx = i;
            p->child = nullptr;
            p->sibling = nullptr;
            if(tree->child == nullptr)
            {
                tree->child = p;
            }
            else
            {
                if(!tree->child->sibling) // step 6
                {
                    tree->child->sibling = p;
                }
                else
                {
                    if(tree->sibling->sibling)
                    {
                        // goto step 6; //HOW to jump to step 6
                    }
                }
            }
        }
    }
}

int main()
{
    //vParentIndices.resize(47);
    vParentIndices.push_back(-1);
    vParentIndices.push_back(0);
    vParentIndices.push_back(1);
    vParentIndices.push_back(2);
    vParentIndices.push_back(3);
    vParentIndices.push_back(4);
    vParentIndices.push_back(5);
    vParentIndices.push_back(2);
    vParentIndices.push_back(7);
    vParentIndices.push_back(8);
    vParentIndices.push_back(9);
    vParentIndices.push_back(2);
    vParentIndices.push_back(11);
    vParentIndices.push_back(12);
    vParentIndices.push_back(13);
    vParentIndices.push_back(14);
    vParentIndices.push_back(15);
    vParentIndices.push_back(15);
    vParentIndices.push_back(15);
    vParentIndices.push_back(15);
    vParentIndices.push_back(14);
    vParentIndices.push_back(20);
    vParentIndices.push_back(21);
    vParentIndices.push_back(22);
    vParentIndices.push_back(23);
    vParentIndices.push_back(24);
    vParentIndices.push_back(25);
    vParentIndices.push_back(23);
    vParentIndices.push_back(27);
    vParentIndices.push_back(28);
    vParentIndices.push_back(23);
    vParentIndices.push_back(30);
    vParentIndices.push_back(31);
    vParentIndices.push_back(14);
    vParentIndices.push_back(33);
    vParentIndices.push_back(34);
    vParentIndices.push_back(35);
    vParentIndices.push_back(36);
    vParentIndices.push_back(37);
    vParentIndices.push_back(38);
    vParentIndices.push_back(36);
    vParentIndices.push_back(40);
    vParentIndices.push_back(41);
    vParentIndices.push_back(36);
    vParentIndices.push_back(43);
    vParentIndices.push_back(44);
    vParentIndices.push_back(2);

    Bone* root = new Bone;
    root->myInx = 0;
    root->child = nullptr;
    root->sibling = nullptr;
    buildTree(root);

    return 0;
}
 
Can you help some more please. Thanks. Edited by belfegor

Share this post


Link to post
Share on other sites

DISCLAIMER: This is off the top of my head, so be careful - look through it, test it out and make sure it's compatible with your indices structure. It ASSUMES every frame has a parent, other than root.

D3DXFRAME *rootFrame = new D3DXFRAME;

CreateHierarchy(-1, rootFrame);
...
void CreateHierarchy(int parIdx, D3DXFRAME* parFrame)
{
    for( int child = 0; child < (int)indices.size(); ++child)
    {
        // find any child with parent == parIdx
        if( indices[i] == parIdx )
        {
            D3DXFRAME* newChild = new D3DXFRAME;
            childFrame->pFirstChild = childFrame->pSibling = NULL;
            // do other initialization of childFrame needed HERE
            AddChild( newChild, parFrame );
            CreateHierarchy( child, newChild ); // add any children of this frame
        }
    }
}

void AddChild(D3DXFRAME* childFrame, D3DXFRAME* parFrame)
{
    // find an empty pointer
    if( NULL == parFrame->pFirstChild )
    {
        parFrame->pFirstChild = childFrame;
        return;
    }
    // childNum is a sibling of pFirstChild
    D3DXFRAME *tmpFrame = parFrame->pFirstChild;
    while( tmpFrame->pSibling != NULL ) tmpFrame = tmpFrame->pSibling;
    tmpFrame->pSibling = childFrame;
}

Edited by Buckeye

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!