FLAXengine代码剖析

2021SC@SDUSC

结构体Struct BoundingSphere

构造函数BoundingSphere(Vector3, Single)

Fields:

Center三维空间中球体的中心。 类型是Vector3

Empty:表示空白空间的 BoundingSphere。类型是BoundingSphere

Radius:球体的半径。类型是System.Single

Methods:
Contains(ref BoundingBox)
确定当前对象是否包含 BoundingBox。
返回值是一个ContainmentType

枚举类型Enum ContainmentType

Fields:
Contains:一个包围盒完全包含另一个包围盒。
Disjoint:这两个包围盒完全不相交。
Intersects:这两个包围体是重叠的。

Contains(ref Vector3)
同上

Contains(ref Vector3, ref Vector3, ref Vector3)
同上

然后是重写的等于方法。

FromBox(BoundingBox)
从给定的框构造一个 BoundingSphere。

FromBox(ref BoundingBox, out BoundingSphere)
同上

FromPoints(Vector3[])
构造一个完全包含给定点的 BoundingSphere。

FromPoints(Vector3[], out BoundingSphere)
同上

FromPoints(Vector3[], Int32, Int32, out BoundingSphere)
同上

Intersects(BoundingBox)
确定当前对象和 BoundingBox 之间是否有交集。

下面是大量的不同参数的Intersects方法。

Merge(BoundingSphere, BoundingSphere)
构造一个与两个指定球体的总合面积一样大的 BoundingSphere。

然后是重写运算符。

我们着重看一下这个从点构建Sphere的方法。
void BoundingSphere::FromPoints(const Vector3* points, int32 pointsCount, BoundingSphere& result)
{

断言。
ASSERT(points && pointsCount > 0);

Vector3 center = Vector3::Zero;
for (int32 i = 0; i < pointsCount; i++)
{
Vector3::Add(points[i], center, center);
}
找到这些点的中心。

center /= static_cast(pointsCount);

计算半径
float radius = 0.0f;
for (int32 i = 0; i < pointsCount; i++)
{
const float distance = Vector3::DistanceSquared(center, points[i]);

if (distance > radius)
    radius = distance;

}
构造球体

result.Center = center;
result.Radius = radius;

从Box中构建Sphere。
void BoundingSphere::FromBox(const BoundingBox& box, BoundingSphere& result)
{
ASSERT(!box.Minimum.IsNanOrInfinity() && !box.Maximum.IsNanOrInfinity());
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;
}

典型的Merge。
void BoundingSphere::Merge(const BoundingSphere& value1, const BoundingSphere& value2, BoundingSphere& result)
{

输入合法性检定
if (value1 == Empty)
{
result = value2;
return;
}
if (value2 == Empty)
{
result = value1;
return;
}

计算中心之差和半径。

const Vector3 difference = value2.Center - value1.Center;

const float length = difference.Length();
const float radius = value1.Radius;
const float radius2 = value2.Radius;

判定不需要额外构建,直接返回的情况。

if (radius + radius2 >= length)
{
if (radius - radius2 >= length)
{
result = value1;
return;
}

if (radius2 - radius >= length)
{
    result = value2;
    return;
}

}

构建新Sphere然后返回。

const Vector3 vector = difference * (1.0f / length);
const float min = Math::Min(-radius, length - radius2);
const float max = (Math::Max(radius, length + radius2) - min) * 0.5f;

result.Center = value1.Center + vector * (max + min);
result.Radius = max;

转换函数Transform
void BoundingSphere::Transform(const BoundingSphere& sphere, const Matrix& matrix, BoundingSphere& result)
{
Vector3::Transform(sphere.Center, matrix, result.Center);
result.Radius = sphere.Radius * matrix.GetScaleVector().GetAbsolute().MaxValue();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值