CodeWalker/CodeWalker.Core/Utils/BoundingBoxes.cs

40 lines
1.2 KiB
C#
Raw Normal View History

2018-06-08 00:42:41 +08:00
using System;
using SharpDX;
namespace CodeWalker.Core.Utils
{
public static class BoundingBoxExtensions
{
public static Vector3 Size(in this BoundingBox bounds)
2018-06-08 00:42:41 +08:00
{
return new Vector3(
Math.Abs(bounds.Maximum.X - bounds.Minimum.X),
Math.Abs(bounds.Maximum.Y - bounds.Minimum.Y),
Math.Abs(bounds.Maximum.Z - bounds.Minimum.Z));
}
public static Vector3 Center(in this BoundingBox bounds)
2018-06-08 00:42:41 +08:00
{
return (bounds.Minimum + bounds.Maximum) * 0.5F;
}
public static BoundingBox Encapsulate(ref this BoundingBox box, ref BoundingBox bounds)
2018-06-08 00:42:41 +08:00
{
Vector3.Min(ref box.Minimum, ref bounds.Minimum, out box.Minimum);
Vector3.Max(ref box.Maximum, ref bounds.Maximum, out box.Maximum);
2018-06-08 00:42:41 +08:00
return box;
}
public static float Radius(in this BoundingBox box)
2018-06-08 00:42:41 +08:00
{
var extents = (box.Maximum - box.Minimum) * 0.5F;
return extents.Length();
}
public static BoundingBox Expand(in this BoundingBox b, float amount)
{
return new BoundingBox(b.Minimum - Vector3.One * amount, b.Maximum + Vector3.One * amount);
}
2018-06-08 00:42:41 +08:00
}
}