Bone parent indices to linked list

Started by
9 comments, last by belfegor 9 years, 9 months ago

Thank you very much on your time and patience Buckeye. smile.png

I think i love you (tho no homo). tongue.png

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;

void AddChild(sp_Bone childFrame, sp_Bone parFrame)
{
    // 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);
            AddChild( newChild, parFrame );
            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;
}

This topic is closed to new replies.

Advertisement