/// <summary> /// Finds the smallest potential bounding sphere for two objects in this list. Removes both objects from the list, adds them to a /// new bounding sphere/BVH node and then adds that to the list. Also returns the BVH node. /// </summary> /// <param name="spheres">A list of spheres</param> /// <returns>The BVH node with the smallest volume</returns> private BVHNode SmallestBoundingSphere(List <Sphere> spheres) { float minBsRadius = float.MaxValue; int sphere1 = -1, sphere2 = -1; for (int i = 0; i < spheres.Count - 1; i++) { for (int j = i + 1; j < spheres.Count; j++) { float bsRadius = ((spheres[j].Position - spheres[i].Position).Length() + spheres[i].R + spheres[j].R) / 2; if (bsRadius < minBsRadius) { minBsRadius = bsRadius; sphere1 = i; sphere2 = j; } } } BVHNode node = new BVHNode(spheres[sphere1], spheres[sphere2]); spheres.Remove(spheres[sphere2]); spheres.Remove(spheres[sphere1]); spheres.Add(node); return(node); }
/// <summary> /// Builds the bounding volume hierarchy based on <see cref="spheres"/>. /// </summary> private void CreateBVH() { List <Sphere> spheresTemp = new List <Sphere>(spheres); while (spheresTemp.Count > 1) { Root = SmallestBoundingSphere(spheresTemp); } }