我有以下递归代码,它的行为不正确(请参见下面的详细信息):

R3Intersection ComputeIntersectionNode(R3Ray *ray, R3Node *node)
{
  R3Intersection closest_inter;
  R3Intersection child_inter;
  R3Intersection shape_inter;

  double least_t = DBL_MAX;

  // check for intersection with shape
  if(node->shape != NULL)
  {
    shape_inter = ComputeIntersectionShape(ray, node->shape);
    if(shape_inter.hit == 1)
      closest_inter = shape_inter;
  }

  // go through all the children and for each child, compute
  // the closest intersection with ray
  for(int i = 0; i < node->children.size(); i++)
  {
    // compute intersection with children[i] and ray
    child_inter = ComputeIntersectionNode(ray, node->children[i]);

    // if there's an intersection with the child node and
    // it is the closest intersection, set closest intersection
    if(child_inter.hit == 1 && fabs(child_inter.t) < fabs(least_t))
      closest_inter = child_inter;
  }

  return closest_inter;
}

这个 ComputeIntersectionNode(...), 除了递归电话外,该程序中的多个射线也被要求使用。要测试此功能,我将其运行4 rays 和4 nodes (或更确切地说,一个 root 类型 node, ,没有 shape, ,但有4个 children, ,每个都有一个 shape)。用于测试,每个 ray 完全相交 node/shape.

当我在GDB中运行第一个代码时 ray, ,它首先通过 root 通过没有一个没有一个的代码 shape, ,所以它直接进入 children. 。它正确计算第一个孩子的交叉点,并设置 closest_inter 也正确的可变,将返回到最高的递归水平,然后 child_interclosest_inter 在这里设置 child_inter.hit = 1;

然后,处理第二个孩子。 ComputeIntersectionShape(...) 与第二个孩子没有任何交叉点(shape_inter.hit == 0;) - 这是预期的行为。但是,当功能返回最高递归级时,出于某种原因,child_inter.hit设置为1(但应设置为0)。

有什么建议么?

先感谢您。

有帮助吗?

解决方案

我认为您的问题是由您返回默认限制的事实引起的 R3Intersection (即。你不返回 shape_inter 当它不相交时)。根据其默认构造函数,您可能会得到所看到的。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top