private HitPoint GetHitPointR(Vector3 d, Vector3 OP, BVHNode node)
        {
            if (node.Left != null && node.Right != null)
            {
                float lambdaLeft  = Helper.VectorSphereHitPoint(d, OP, _spheres[node.Left.SphereIndex]);
                float lambdaRight = Helper.VectorSphereHitPoint(d, OP, _spheres[node.Right.SphereIndex]);

                if (lambdaLeft == HitPoint.NoHitPoint.Lambda && lambdaRight == HitPoint.NoHitPoint.Lambda)
                {
                    return(HitPoint.NoHitPoint);
                }
                else if (lambdaLeft != HitPoint.NoHitPoint.Lambda && lambdaRight != HitPoint.NoHitPoint.Lambda)
                {
                    HitPoint hpLeft  = GetHitPointR(d, OP, node.Left);
                    HitPoint hpRight = GetHitPointR(d, OP, node.Right);

                    return((hpLeft.Lambda < hpRight.Lambda) ? hpLeft : hpRight);
                }
                else if (lambdaLeft != HitPoint.NoHitPoint.Lambda)
                {
                    return(GetHitPointR(d, OP, node.Left));
                }
                else // lambdaRight != HitPoint.NoHitPoint.Lambda
                {
                    return(GetHitPointR(d, OP, node.Right));
                }
            }
            else
            {
                Debug.Assert(node.SphereIndex < _naturalSpheresCount);
                float lambda = Helper.VectorSphereHitPoint(d, OP, _spheres[node.SphereIndex]);

                if (lambda == HitPoint.NoHitPoint.Lambda)
                {
                    return(HitPoint.NoHitPoint);
                }
                else
                {
                    return(new HitPoint(OP, lambda, d, node.SphereIndex));
                }
            }
        }
 public BVHAccelerationStructure(List <Sphere> spheres)
 {
     _spheres             = new Sphere[spheres.Count * 2];
     _naturalSpheresCount = spheres.Count;
     _root = Populate(spheres);
 }
 public BVHNode(BVHNode l, BVHNode r, int sphere)
 {
     Left        = l;
     Right       = r;
     SphereIndex = sphere;
 }