Optimize BoundingSphere::FromBox

This commit is contained in:
Wojtek Figat
2021-06-30 13:42:22 +02:00
parent c4fc7b7e5d
commit 419f8db538
2 changed files with 14 additions and 17 deletions

View File

@@ -122,15 +122,13 @@ void BoundingSphere::FromPoints(const Vector3* points, int32 pointsCount, Boundi
void BoundingSphere::FromBox(const BoundingBox& box, BoundingSphere& result)
{
ASSERT(!box.Minimum.IsNanOrInfinity() && !box.Maximum.IsNanOrInfinity());
Vector3::Lerp(box.Minimum, box.Maximum, 0.5f, result.Center);
const float x = box.Minimum.X - box.Maximum.X;
const float y = box.Minimum.Y - box.Maximum.Y;
const float z = box.Minimum.Z - box.Maximum.Z;
const float distance = Math::Sqrt(x * x + y * y + z * z);
result.Radius = distance * 0.5f;
const float x = box.Maximum.X - box.Minimum.X;
const float y = box.Maximum.Y - box.Minimum.Y;
const float z = box.Maximum.Z - box.Minimum.Z;
result.Center.X = box.Minimum.X + x * 0.5f;
result.Center.Y = box.Minimum.Y + y * 0.5f;
result.Center.Z = box.Minimum.Z + z * 0.5f;
result.Radius = Math::Sqrt(x * x + y * y + z * z) * 0.5f;
}
void BoundingSphere::Merge(const BoundingSphere& value1, const BoundingSphere& value2, BoundingSphere& result)

View File

@@ -286,14 +286,13 @@ namespace FlaxEngine
/// <param name="result">When the method completes, the newly constructed bounding sphere.</param>
public static void FromBox(ref BoundingBox box, out BoundingSphere result)
{
Vector3.Lerp(ref box.Minimum, ref box.Maximum, 0.5f, out result.Center);
float x = box.Minimum.X - box.Maximum.X;
float y = box.Minimum.Y - box.Maximum.Y;
float z = box.Minimum.Z - box.Maximum.Z;
var distance = (float)Math.Sqrt(x * x + y * y + z * z);
result.Radius = distance * 0.5f;
float x = box.Maximum.X - box.Minimum.X;
float y = box.Maximum.Y - box.Minimum.Y;
float z = box.Maximum.Z - box.Minimum.Z;
result.Center.X = box.Minimum.X + x * 0.5f;
result.Center.Y = box.Minimum.Y + y * 0.5f;
result.Center.Z = box.Minimum.Z + z * 0.5f;
result.Radius = (float)Math.Sqrt(x * x + y * y + z * z) * 0.5f;
}
/// <summary>