0

次の再帰コードがありますが、期待どおりに動作しません (以下の詳細を参照)。

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(...)は、再帰呼び出しに加えて、プログラム内の複数の光線に対しても呼び出されています。この関数をテストするために、 4raysと 4に対して実行しますnodes(より正確には、 を持たず4 を持ち、それぞれに を 1 つ持つroottypeの 1 つ)。テストのために、それぞれが正確に 1 つの/と交差します。nodeshapechildrenshaperaynodeshape

最初の の GDB でコードを実行すると、最初rayに がコードを介して渡されますが、rootこれには がありません。最初の子の交差を正しく計算し、変数も正しく設定します。これは再帰の最高レベルに返され、ここで設定されますshapechildrenclosest_interchild_interclosest_interchild_inter.hit = 1;

次に、2 番目の子が処理されます。ComputeIntersectionShape(...)2 番目の子との交差を返しません ( shape_inter.hit == 0;) - これは予期された動作です。ただし、関数が最上位の再帰に戻ると、何らかの理由で child_inter.hit が 1 に設定されます (ただし、0 に設定する必要があります)。

助言がありますか?

前もって感謝します。

4

1 に答える 1

1

あなたの問題は、デフォルトで初期化されたを返しているという事実によって引き起こされていると思います(つまり、交差しないときにR3Intersection返さない)。shape_interデフォルトのコンストラクターによっては、表示されるものが得られる場合があります。

于 2010-03-31T20:56:23.760 に答える