次の再帰コードがありますが、期待どおりに動作しません (以下の詳細を参照)。
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 つ持つroot
typeの 1 つ)。テストのために、それぞれが正確に 1 つの/と交差します。node
shape
children
shape
ray
node
shape
最初の の GDB でコードを実行すると、最初ray
に がコードを介して渡されますが、root
これには がありません。最初の子の交差を正しく計算し、変数も正しく設定します。これは再帰の最高レベルに返され、ここで設定されますshape
children
closest_inter
child_inter
closest_inter
child_inter.hit = 1;
次に、2 番目の子が処理されます。ComputeIntersectionShape(...)
2 番目の子との交差を返しません ( shape_inter.hit == 0
;) - これは予期された動作です。ただし、関数が最上位の再帰に戻ると、何らかの理由で child_inter.hit が 1 に設定されます (ただし、0 に設定する必要があります)。
助言がありますか?
前もって感謝します。