• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Bone parent indices to linked list

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

10 replies to this topic

### #1belfegor  Members

Posted 06 July 2014 - 12:14 AM

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?

### #2unbird  Members

Posted 06 July 2014 - 07:24 AM

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)
}

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

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.

### #3belfegor  Members

Posted 06 July 2014 - 11:56 AM

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

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.

### #4Aressera  Members

Posted 06 July 2014 - 11:58 AM

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.

### #5belfegor  Members

Posted 06 July 2014 - 12:21 PM

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.

### #6Buckeye  GDNet+

Posted 06 July 2014 - 03:43 PM

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, 06 July 2014 - 03:48 PM.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

### #7belfegor  Members

Posted 07 July 2014 - 04:08 AM

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.

### #8Buckeye  GDNet+

Posted 07 July 2014 - 06:08 AM

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, 07 July 2014 - 06:11 AM.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

### #9belfegor  Members

Posted 07 July 2014 - 07:06 AM

#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)
{
}
}
}
}
}
}

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, 07 July 2014 - 07:08 AM.

### #10Buckeye  GDNet+

Posted 07 July 2014 - 10:50 AM

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
CreateHierarchy( child, newChild ); // add any children of this frame
}
}
}

{
// 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, 07 July 2014 - 10:51 AM.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

### #11belfegor  Members

Posted 07 July 2014 - 01:32 PM

Thank you very much on your time and patience Buckeye.

I think i love you (tho no homo).

I think that this works. Printed some indices and got expected results, replaced all pointers to shared_ptr so i don't have to worry about freeing memory.

Hopefully i didn't make any mistakes.

#include <iostream>
#include <vector>
#include <memory>

struct Bone;

typedef std::shared_ptr<Bone> sp_Bone;

struct Bone
{
int myInx;
sp_Bone sibling;
sp_Bone child;

Bone( int index ) : myInx(index), sibling(nullptr), child(nullptr) {}
~Bone() { }
};

std::vector<int>  vParentIndices;

{
// find an empty pointer
if( nullptr == parFrame->child )
{
parFrame->child = childFrame;
return;
}
// childNum is a sibling of pFirstChild
sp_Bone tmpFrame = parFrame->child;
while( tmpFrame->sibling )
tmpFrame = tmpFrame->sibling;
tmpFrame->sibling = childFrame;
}

int CreateHierarchy(int parIdx, sp_Bone parFrame)
{
int count = 1;
for( int i = 0; i < (int)vParentIndices.size(); ++i)
{
// find any child with parent == parIdx
if( vParentIndices[i] == parIdx )
{
sp_Bone newChild = std::make_shared<Bone>(i);
count += CreateHierarchy( i, newChild ); // add any children of this frame
}
}
return count;
}

void printTree(sp_Bone tree)
{
if(tree)
{
std::cout << tree->myInx << std::endl;

printTree(tree->sibling);
printTree(tree->child);
}
}

void printChildren(sp_Bone bone)
{
if(bone->child)
{
std::cout << bone->child->myInx << std::endl;
printChildren(bone->child);
}
}

void printSiblings(sp_Bone bone)
{
if(bone->sibling)
{
std::cout << bone->sibling->myInx << std::endl;
printSiblings(bone->sibling);
}
}

sp_Bone findBone(sp_Bone root, int inx)
{
if(root)
{
if(root->myInx == inx)
return root;
auto sb = findBone(root->sibling, inx);
if(sb)
return sb;
auto cb = findBone(root->child, inx);
if(cb)
return cb;
}
return nullptr;
}

int main()
{
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);

sp_Bone root = std::make_shared<Bone>(0);
int cnt = CreateHierarchy(0, root);
std::cout << "bone count: " << cnt << std::endl;

//printTree( root );

auto bone = findBone(root, 16);
if(bone)
{
//printChildren(bone);
printSiblings(bone);
}

return 0;
}


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.