0

I want to get the absolute values of each node. Absolute value meaning that the distance from the root.

if I have a skeleton model.

the root childs are

root
left hip - child
left knee - child
left foot - child

assume that all the bones lengths are equal to 1.
root to hip = 1
hip to knee = 1
knee to foot = 1

So if I want to get the position of foot joint from the root, it should be 3. am I right?

root to foot = root to hip + hip to knee + knee to foot = 3

so these are the subroutines I am using..

void ComputeAbs()
{
    for(unsigned int i=1; i<nNodes(); i++) 
    {
        node* b = getNode(i);
        if(b)
        {
            b->nb = ComputeAbsSum(b);
        }
    }
}

int ComputeAbsSum(node *b)
{
    int m = b->nb;
    if (b->child != NULL) 
    {
        m *= ComputeAbsSum(b->child);
    }
    return m;
}

the output would be like

root to hip = 3
root to knee = 2
root to foot = 1

But I want in a reverse way, i should get like this

root to hip = 1
root to knee = 2
root to foot = 3

How can I achieve this result? how to add tree childs values starts from child to root?

the final objective is to get the final pose by calculating the absolute transformation of a joint.

bonePoseAbsolute[i] = bonePoseAbsolute[parentIndex] * bonePoseRelative[i];

Thanks.

maxpayne
  • 1,111
  • 2
  • 21
  • 41
  • There are some serious flaws. First, from where can one get the edge cost? Is it always unit? Then the solution from Kane@ is correct. Please specify how to get the edge cost between b and b->child. – Fallen Mar 13 '15 at 00:14

1 Answers1

0

It looks like you have a problem in your recursion. Try

int ComputeAbsSum(node *b)
{
    int result = 1;
    if (b->child != NULL)
        result += ComputeAbsSum(b->child);
    return result;
}

*edit: if you want to traverse the tree in reverse,

int ComputeAbsSumReverse(node *b)
{
    int result = 1;
    if (b->parent != NULL)
        result += ComputeAbsSum(b->parent);
    return result;
}
Kane Anderson
  • 503
  • 1
  • 7
  • 14
  • No Kane, It just counts the number of childs. I do not want counting of childs in a tree. I want to add up the values of all parents to the specific child. For example, foot has knee parent and knee has hip parent, so the footchild->value += kneeparent->value+hipparent->value. – maxpayne Mar 12 '15 at 22:53
  • based on your comment, replace int result = 1 with int result = b->value – Kane Anderson Mar 13 '15 at 23:15
  • Yes I am already doing this using this function int ComputeAbsSum(node *b)... you can see that. This function adds the from root to child, meaning that it adds all the childs values and store into the parent. For example hip += knee+foot, then knee+=foot, but I want in a reverse way, foot+=knee+hip, and knee+=hip.. like that. What I want is same like this std::reverse(vector.begin(), vector.end()); but the issue is with this kind of list in which you have parent and child relationship, how can I go from child to parent? parent to child is easy but child to parent? – maxpayne Mar 14 '15 at 01:13
  • 1
    In the node class, include a pointer to the parent(s). Then in my answer, change b->child to b->parent – Kane Anderson Mar 16 '15 at 16:22