// todo: improve precision public static Sphere BoundingSphere(IReadOnlyList <Sphere> spheres) { var boundingBox = AaBox.BoundingBox(spheres); const float normalizedStep = 0.01f; var scale = Math.Max(Math.Max(boundingBox.HalfSize.Width, boundingBox.HalfSize.Height), boundingBox.HalfSize.Depth); var step = scale * normalizedStep; var center = boundingBox.Center; for (int i = 0; i < 0.25f / normalizedStep; i++) { var mostDistantSphere = spheres.Maximal(x => (center - x.Center).Length() + x.Radius); var towards = mostDistantSphere.Center - center; var towardsLength = towards.Length(); if (towardsLength > MathHelper.Eps5) { center += step * towards / towardsLength; } } var finalMostDistantSphere = spheres.Maximal(x => (center - x.Center).Length() + x.Radius); return(new Sphere(center, (center - finalMostDistantSphere.Center).Length() + finalMostDistantSphere.Radius)); }
public static Sphere BoundingSphere <T>(IReadOnlyList <T> vertices, Func <T, Vector3> getPosition) { var boundingBox = AaBox.BoundingBox(vertices.Select(getPosition)); const float normalizedStep = 0.01f; var scale = Math.Max(Math.Max(boundingBox.HalfSize.Width, boundingBox.HalfSize.Height), boundingBox.HalfSize.Depth); var step = scale * normalizedStep; var center = boundingBox.Center; for (int i = 0; i < 0.25f / normalizedStep; i++) { var mostDistantPoint = vertices.Select(getPosition).Maximal(x => (center - x).LengthSquared()); var towards = mostDistantPoint - center; var towardsLength = towards.Length(); if (towardsLength > MathHelper.Eps5) { center += step * towards / towardsLength; } } var finalMostDistantPoint = vertices.Select(getPosition).Maximal(x => (center - x).LengthSquared()); return(new Sphere(center, (center - finalMostDistantPoint).Length())); }
public static Sphere BoundingSphere(AaBox box) { return(new Sphere(box.Center, box.HalfSize.ToVector().Length())); }