【问题标题】:How to sum up the child values that starts from child to root in a tree structure?如何总结树结构中从子节点到根节点的子值?
【发布时间】:2015-05-15 06:45:54
【问题描述】:

我想得到每个节点的绝对值。绝对值表示到根的距离。

如果我有骨架模型。

根子节点是

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

所以如果我想从根部得到脚关节的位置,应该是3。对吗?

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

所以这些是我正在使用的子例程..

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

输出会是这样的

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

我怎样才能达到这个结果?如何将树的子值添加从子到根?

最终目标是通过计算关节的绝对变换得到最终的位姿。

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

谢谢。

【问题讨论】:

  • 有一些严重的缺陷。首先,从哪里可以获得边缘成本?它总是单位吗?那么 Kane@ 的解决方案是正确的。请指定如何获取 b 和 b->child 之间的边缘成本。

标签: c++ algorithm visual-c++ tree skeleton-code


【解决方案1】:

您的递归似乎有问题。试试

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

*edit:如果要反向遍历树,

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

【讨论】:

  • 没有凯恩,它只是计算孩子的数量。我不想数一棵树上的孩子。我想将所有父母的价值观加到特定的孩子身上。例如,foot 有膝盖父级,膝盖有 hip 父级,所以 footchild->value +=kneeparent->value+hipparent->value。
  • 根据您的评论,将 int result = 1 替换为 int result = b->value
  • 是的,我已经在使用这个函数 int ComputeAbsSum(node *b)... 你可以看到。此函数将 from root 添加到 child,这意味着它将所有 child 的值添加并存储到 parent。例如臀部+=膝盖+脚,然后膝盖+=脚,但我想要相反的方式,脚+=膝盖+臀部,膝盖+=臀部......就像那样。我想要的是一样的 std::reverse(vector.begin(), vector.end());但问题是这种你有父母和孩子关系的列表,我怎样才能从孩子到父母?父母对孩子很容易,但孩子对父母?
  • 在节点类中,包含一个指向父节点的指针。然后在我的回答中,将 b->child 更改为 b->parent
猜你喜欢
  • 2010-12-12
  • 1970-01-01
  • 2011-09-10
  • 1970-01-01
  • 1970-01-01
  • 2012-01-02
  • 1970-01-01
  • 2010-10-05
  • 1970-01-01
相关资源
最近更新 更多