#pragma once
XMGLOBALCONST XMVECTORF32 g_BoxOffset[8] =
{
{ { { -1.0f, -1.0f, 1.0f, 0.0f } } },
{ { { 1.0f, -1.0f, 1.0f, 0.0f } } },
{ { { 1.0f, 1.0f, 1.0f, 0.0f } } },
{ { { -1.0f, 1.0f, 1.0f, 0.0f } } },
{ { { -1.0f, -1.0f, -1.0f, 0.0f } } },
{ { { 1.0f, -1.0f, -1.0f, 0.0f } } },
{ { { 1.0f, 1.0f, -1.0f, 0.0f } } },
{ { { -1.0f, 1.0f, -1.0f, 0.0f } } },
};
XMGLOBALCONST XMVECTORF32 g_RayEpsilon = { { { 1e-20f, 1e-20f, 1e-20f, 1e-20f } } };
XMGLOBALCONST XMVECTORF32 g_RayNegEpsilon = { { { -1e-20f, -1e-20f, -1e-20f, -1e-20f } } };
XMGLOBALCONST XMVECTORF32 g_FltMin = { { { -FLT_MAX, -FLT_MAX, -FLT_MAX, -FLT_MAX } } };
XMGLOBALCONST XMVECTORF32 g_FltMax = { { { FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX } } };
namespace MathInternal
{
inline bool XMVector3AnyTrue(_In_ FXMVECTOR V) noexcept
{
XMVECTOR C = XMVectorSwizzle<XM_SWIZZLE_X, XM_SWIZZLE_Y, XM_SWIZZLE_Z, XM_SWIZZLE_X>(V);
return XMComparisonAnyTrue(XMVector4EqualIntR(C, XMVectorTrueInt()));
}
inline bool XMVector3AllTrue(_In_ FXMVECTOR V) noexcept
{
XMVECTOR C = XMVectorSwizzle<XM_SWIZZLE_X, XM_SWIZZLE_Y, XM_SWIZZLE_Z, XM_SWIZZLE_X>(V);
return XMComparisonAllTrue(XMVector4EqualIntR(C, XMVectorTrueInt()));
}
#if defined(_PREFAST_) || !defined(NDEBUG)
XMGLOBALCONST XMVECTORF32 g_UnitVectorEpsilon = { { { 1.0e-4f, 1.0e-4f, 1.0e-4f, 1.0e-4f } } };
XMGLOBALCONST XMVECTORF32 g_UnitQuaternionEpsilon = { { { 1.0e-4f, 1.0e-4f, 1.0e-4f, 1.0e-4f } } };
XMGLOBALCONST XMVECTORF32 g_UnitPlaneEpsilon = { { { 1.0e-4f, 1.0e-4f, 1.0e-4f, 1.0e-4f } } };
inline bool XMVector3IsUnit(_In_ FXMVECTOR V) noexcept
{
XMVECTOR Difference = XMVectorSubtract(XMVector3Length(V), XMVectorSplatOne());
return XMVector4Less(XMVectorAbs(Difference), g_UnitVectorEpsilon);
}
inline bool XMQuaternionIsUnit(_In_ FXMVECTOR Q) noexcept
{
XMVECTOR Difference = XMVectorSubtract(XMVector4Length(Q), XMVectorSplatOne());
return XMVector4Less(XMVectorAbs(Difference), g_UnitQuaternionEpsilon);
}
inline bool XMPlaneIsUnit(_In_ FXMVECTOR Plane) noexcept
{
XMVECTOR Difference = XMVectorSubtract(XMVector3Length(Plane), XMVectorSplatOne());
return XMVector4Less(XMVectorAbs(Difference), g_UnitPlaneEpsilon);
}
#endif
inline XMVECTOR XMPlaneTransform(_In_ FXMVECTOR Plane, _In_ FXMVECTOR Rotation, _In_ FXMVECTOR Translation) noexcept
{
XMVECTOR vNormal = XMVector3Rotate(Plane, Rotation);
XMVECTOR vD = XMVectorSubtract(XMVectorSplatW(Plane), XMVector3Dot(vNormal, Translation));
return XMVectorInsert<0, 0, 0, 0, 1>(vNormal, vD);
}
inline XMVECTOR PointOnLineSegmentNearestPoint(_In_ FXMVECTOR S1, _In_ FXMVECTOR S2, _In_ FXMVECTOR P) noexcept
{
XMVECTOR Dir = XMVectorSubtract(S2, S1);
XMVECTOR Projection = XMVectorSubtract(XMVector3Dot(P, Dir), XMVector3Dot(S1, Dir));
XMVECTOR LengthSq = XMVector3Dot(Dir, Dir);
XMVECTOR t = XMVectorMultiply(Projection, XMVectorReciprocal(LengthSq));
XMVECTOR Point = XMVectorMultiplyAdd(t, Dir, S1);
XMVECTOR SelectS1 = XMVectorLess(Projection, XMVectorZero());
Point = XMVectorSelect(Point, S1, SelectS1);
XMVECTOR SelectS2 = XMVectorGreater(Projection, LengthSq);
Point = XMVectorSelect(Point, S2, SelectS2);
return Point;
}
inline XMVECTOR XM_CALLCONV PointOnPlaneInsideTriangle(_In_ FXMVECTOR P, _In_ FXMVECTOR V0, _In_ FXMVECTOR V1, _In_ GXMVECTOR V2) noexcept
{
XMVECTOR N = XMVector3Cross(XMVectorSubtract(V2, V0), XMVectorSubtract(V1, V0));
XMVECTOR C0 = XMVector3Cross(XMVectorSubtract(P, V0), XMVectorSubtract(V1, V0));
XMVECTOR C1 = XMVector3Cross(XMVectorSubtract(P, V1), XMVectorSubtract(V2, V1));
XMVECTOR C2 = XMVector3Cross(XMVectorSubtract(P, V2), XMVectorSubtract(V0, V2));
XMVECTOR Zero = XMVectorZero();
XMVECTOR Inside0 = XMVectorGreaterOrEqual(XMVector3Dot(C0, N), Zero);
XMVECTOR Inside1 = XMVectorGreaterOrEqual(XMVector3Dot(C1, N), Zero);
XMVECTOR Inside2 = XMVectorGreaterOrEqual(XMVector3Dot(C2, N), Zero);
return XMVectorAndInt(XMVectorAndInt(Inside0, Inside1), Inside2);
}
inline bool SolveCubic(_In_ float e, _In_ float f, _In_ float g, _Out_ float* t, _Out_ float* u, _Out_ float* v) noexcept
{
float p, q, h, rc, d, theta, costh3, sinth3;
p = f - e * e / 3.0f;
q = g - e * f / 3.0f + e * e * e * 2.0f / 27.0f;
h = q * q / 4.0f + p * p * p / 27.0f;
if (h > 0)
{
*t = *u = *v = 0.f;
return false; }
if ((h == 0) && (q == 0)) {
*t = -e / 3;
*u = -e / 3;
*v = -e / 3;
return true;
}
d = sqrtf(q * q / 4.0f - h);
if (d < 0)
rc = -powf(-d, 1.0f / 3.0f);
else
rc = powf(d, 1.0f / 3.0f);
theta = XMScalarACos(-q / (2.0f * d));
costh3 = XMScalarCos(theta / 3.0f);
sinth3 = sqrtf(3.0f) * XMScalarSin(theta / 3.0f);
*t = 2.0f * rc * costh3 - e / 3.0f;
*u = -rc * (costh3 + sinth3) - e / 3.0f;
*v = -rc * (costh3 - sinth3) - e / 3.0f;
return true;
}
inline XMVECTOR CalculateEigenVector(_In_ float m11, _In_ float m12, _In_ float m13,
_In_ float m22, _In_ float m23, _In_ float m33, _In_ float e) noexcept
{
float fTmp[3];
fTmp[0] = m12 * m23 - m13 * (m22 - e);
fTmp[1] = m13 * m12 - m23 * (m11 - e);
fTmp[2] = (m11 - e) * (m22 - e) - m12 * m12;
XMVECTOR vTmp = XMLoadFloat3(reinterpret_cast<const XMFLOAT3*>(fTmp));
if (XMVector3Equal(vTmp, XMVectorZero())) {
float f1, f2, f3;
if ((m11 - e != 0) || (m12 != 0) || (m13 != 0))
{
f1 = m11 - e; f2 = m12; f3 = m13;
}
else if ((m12 != 0) || (m22 - e != 0) || (m23 != 0))
{
f1 = m12; f2 = m22 - e; f3 = m23;
}
else if ((m13 != 0) || (m23 != 0) || (m33 - e != 0))
{
f1 = m13; f2 = m23; f3 = m33 - e;
}
else
{
f1 = 1.0f; f2 = 0.0f; f3 = 0.0f;
}
if (f1 == 0)
vTmp = XMVectorSetX(vTmp, 0.0f);
else
vTmp = XMVectorSetX(vTmp, 1.0f);
if (f2 == 0)
vTmp = XMVectorSetY(vTmp, 0.0f);
else
vTmp = XMVectorSetY(vTmp, 1.0f);
if (f3 == 0)
{
vTmp = XMVectorSetZ(vTmp, 0.0f);
if (m12 != 0)
vTmp = XMVectorSetY(vTmp, -f1 / f2);
}
else
{
vTmp = XMVectorSetZ(vTmp, (f2 - f1) / f3);
}
}
if (XMVectorGetX(XMVector3LengthSq(vTmp)) > 1e-5f)
{
return XMVector3Normalize(vTmp);
}
else
{
vTmp = XMVectorScale(vTmp, 1e5f);
return XMVector3Normalize(vTmp);
}
}
inline bool CalculateEigenVectors(_In_ float m11, _In_ float m12, _In_ float m13,
_In_ float m22, _In_ float m23, _In_ float m33,
_In_ float e1, _In_ float e2, _In_ float e3,
_Out_ XMVECTOR* pV1, _Out_ XMVECTOR* pV2, _Out_ XMVECTOR* pV3) noexcept
{
*pV1 = DirectX::MathInternal::CalculateEigenVector(m11, m12, m13, m22, m23, m33, e1);
*pV2 = DirectX::MathInternal::CalculateEigenVector(m11, m12, m13, m22, m23, m33, e2);
*pV3 = DirectX::MathInternal::CalculateEigenVector(m11, m12, m13, m22, m23, m33, e3);
bool v1z = false;
bool v2z = false;
bool v3z = false;
XMVECTOR Zero = XMVectorZero();
if (XMVector3Equal(*pV1, Zero))
v1z = true;
if (XMVector3Equal(*pV2, Zero))
v2z = true;
if (XMVector3Equal(*pV3, Zero))
v3z = true;
bool e12 = (fabsf(XMVectorGetX(XMVector3Dot(*pV1, *pV2))) > 0.1f); bool e13 = (fabsf(XMVectorGetX(XMVector3Dot(*pV1, *pV3))) > 0.1f);
bool e23 = (fabsf(XMVectorGetX(XMVector3Dot(*pV2, *pV3))) > 0.1f);
if ((v1z && v2z && v3z) || (e12 && e13 && e23) ||
(e12 && v3z) || (e13 && v2z) || (e23 && v1z)) {
*pV1 = g_XMIdentityR0.v;
*pV2 = g_XMIdentityR1.v;
*pV3 = g_XMIdentityR2.v;
return true;
}
if (v1z && v2z)
{
XMVECTOR vTmp = XMVector3Cross(g_XMIdentityR1, *pV3);
if (XMVectorGetX(XMVector3LengthSq(vTmp)) < 1e-5f)
{
vTmp = XMVector3Cross(g_XMIdentityR0, *pV3);
}
*pV1 = XMVector3Normalize(vTmp);
*pV2 = XMVector3Cross(*pV3, *pV1);
return true;
}
if (v3z && v1z)
{
XMVECTOR vTmp = XMVector3Cross(g_XMIdentityR1, *pV2);
if (XMVectorGetX(XMVector3LengthSq(vTmp)) < 1e-5f)
{
vTmp = XMVector3Cross(g_XMIdentityR0, *pV2);
}
*pV3 = XMVector3Normalize(vTmp);
*pV1 = XMVector3Cross(*pV2, *pV3);
return true;
}
if (v2z && v3z)
{
XMVECTOR vTmp = XMVector3Cross(g_XMIdentityR1, *pV1);
if (XMVectorGetX(XMVector3LengthSq(vTmp)) < 1e-5f)
{
vTmp = XMVector3Cross(g_XMIdentityR0, *pV1);
}
*pV2 = XMVector3Normalize(vTmp);
*pV3 = XMVector3Cross(*pV1, *pV2);
return true;
}
if ((v1z) || e12)
{
*pV1 = XMVector3Cross(*pV2, *pV3);
return true;
}
if ((v2z) || e23)
{
*pV2 = XMVector3Cross(*pV3, *pV1);
return true;
}
if ((v3z) || e13)
{
*pV3 = XMVector3Cross(*pV1, *pV2);
return true;
}
return true;
}
inline bool CalculateEigenVectorsFromCovarianceMatrix(_In_ float Cxx, _In_ float Cyy, _In_ float Czz,
_In_ float Cxy, _In_ float Cxz, _In_ float Cyz,
_Out_ XMVECTOR* pV1, _Out_ XMVECTOR* pV2, _Out_ XMVECTOR* pV3) noexcept
{
float e = -(Cxx + Cyy + Czz);
float f = Cxx * Cyy + Cyy * Czz + Czz * Cxx - Cxy * Cxy - Cxz * Cxz - Cyz * Cyz;
float g = Cxy * Cxy * Czz + Cxz * Cxz * Cyy + Cyz * Cyz * Cxx - Cxy * Cyz * Cxz * 2.0f - Cxx * Cyy * Czz;
float ev1, ev2, ev3;
if (!DirectX::MathInternal::SolveCubic(e, f, g, &ev1, &ev2, &ev3))
{
*pV1 = g_XMIdentityR0.v;
*pV2 = g_XMIdentityR1.v;
*pV3 = g_XMIdentityR2.v;
return false;
}
return DirectX::MathInternal::CalculateEigenVectors(Cxx, Cxy, Cxz, Cyy, Cyz, Czz, ev1, ev2, ev3, pV1, pV2, pV3);
}
inline void XM_CALLCONV FastIntersectTrianglePlane(
FXMVECTOR V0, FXMVECTOR V1, FXMVECTOR V2,
GXMVECTOR Plane,
XMVECTOR& Outside, XMVECTOR& Inside) noexcept
{
XMVECTOR Dist0 = XMVector4Dot(V0, Plane);
XMVECTOR Dist1 = XMVector4Dot(V1, Plane);
XMVECTOR Dist2 = XMVector4Dot(V2, Plane);
XMVECTOR MinDist = XMVectorMin(Dist0, Dist1);
MinDist = XMVectorMin(MinDist, Dist2);
XMVECTOR MaxDist = XMVectorMax(Dist0, Dist1);
MaxDist = XMVectorMax(MaxDist, Dist2);
XMVECTOR Zero = XMVectorZero();
Outside = XMVectorGreater(MinDist, Zero);
Inside = XMVectorLess(MaxDist, Zero);
}
inline void FastIntersectSpherePlane(_In_ FXMVECTOR Center, _In_ FXMVECTOR Radius, _In_ FXMVECTOR Plane,
_Out_ XMVECTOR& Outside, _Out_ XMVECTOR& Inside) noexcept
{
XMVECTOR Dist = XMVector4Dot(Center, Plane);
Outside = XMVectorGreater(Dist, Radius);
Inside = XMVectorLess(Dist, XMVectorNegate(Radius));
}
inline void FastIntersectAxisAlignedBoxPlane(_In_ FXMVECTOR Center, _In_ FXMVECTOR Extents, _In_ FXMVECTOR Plane,
_Out_ XMVECTOR& Outside, _Out_ XMVECTOR& Inside) noexcept
{
XMVECTOR Dist = XMVector4Dot(Center, Plane);
XMVECTOR Radius = XMVector3Dot(Extents, XMVectorAbs(Plane));
Outside = XMVectorGreater(Dist, Radius);
Inside = XMVectorLess(Dist, XMVectorNegate(Radius));
}
inline void XM_CALLCONV FastIntersectOrientedBoxPlane(
_In_ FXMVECTOR Center, _In_ FXMVECTOR Extents, _In_ FXMVECTOR Axis0,
_In_ GXMVECTOR Axis1,
_In_ HXMVECTOR Axis2, _In_ HXMVECTOR Plane,
_Out_ XMVECTOR& Outside, _Out_ XMVECTOR& Inside) noexcept
{
XMVECTOR Dist = XMVector4Dot(Center, Plane);
XMVECTOR Radius = XMVector3Dot(Plane, Axis0);
Radius = XMVectorInsert<0, 0, 1, 0, 0>(Radius, XMVector3Dot(Plane, Axis1));
Radius = XMVectorInsert<0, 0, 0, 1, 0>(Radius, XMVector3Dot(Plane, Axis2));
Radius = XMVector3Dot(Extents, XMVectorAbs(Radius));
Outside = XMVectorGreater(Dist, Radius);
Inside = XMVectorLess(Dist, XMVectorNegate(Radius));
}
inline void XM_CALLCONV FastIntersectFrustumPlane(
_In_ FXMVECTOR Point0, _In_ FXMVECTOR Point1, _In_ FXMVECTOR Point2,
_In_ GXMVECTOR Point3,
_In_ HXMVECTOR Point4, _In_ HXMVECTOR Point5,
_In_ CXMVECTOR Point6, _In_ CXMVECTOR Point7, _In_ CXMVECTOR Plane,
_Out_ XMVECTOR& Outside, _Out_ XMVECTOR& Inside) noexcept
{
XMVECTOR Min, Max, Dist;
Min = Max = XMVector3Dot(Plane, Point0);
Dist = XMVector3Dot(Plane, Point1);
Min = XMVectorMin(Min, Dist);
Max = XMVectorMax(Max, Dist);
Dist = XMVector3Dot(Plane, Point2);
Min = XMVectorMin(Min, Dist);
Max = XMVectorMax(Max, Dist);
Dist = XMVector3Dot(Plane, Point3);
Min = XMVectorMin(Min, Dist);
Max = XMVectorMax(Max, Dist);
Dist = XMVector3Dot(Plane, Point4);
Min = XMVectorMin(Min, Dist);
Max = XMVectorMax(Max, Dist);
Dist = XMVector3Dot(Plane, Point5);
Min = XMVectorMin(Min, Dist);
Max = XMVectorMax(Max, Dist);
Dist = XMVector3Dot(Plane, Point6);
Min = XMVectorMin(Min, Dist);
Max = XMVectorMax(Max, Dist);
Dist = XMVector3Dot(Plane, Point7);
Min = XMVectorMin(Min, Dist);
Max = XMVectorMax(Max, Dist);
XMVECTOR PlaneDist = XMVectorNegate(XMVectorSplatW(Plane));
Outside = XMVectorGreater(Min, PlaneDist);
Inside = XMVectorLess(Max, PlaneDist);
}
}
_Use_decl_annotations_
inline void XM_CALLCONV BoundingSphere::Transform(BoundingSphere& Out, FXMMATRIX M) const noexcept
{
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR C = XMVector3Transform(vCenter, M);
XMVECTOR dX = XMVector3Dot(M.r[0], M.r[0]);
XMVECTOR dY = XMVector3Dot(M.r[1], M.r[1]);
XMVECTOR dZ = XMVector3Dot(M.r[2], M.r[2]);
XMVECTOR d = XMVectorMax(dX, XMVectorMax(dY, dZ));
XMStoreFloat3(&Out.Center, C);
float Scale = sqrtf(XMVectorGetX(d));
Out.Radius = Radius * Scale;
}
_Use_decl_annotations_
inline void XM_CALLCONV BoundingSphere::Transform(BoundingSphere& Out, float Scale, FXMVECTOR Rotation, FXMVECTOR Translation) const noexcept
{
XMVECTOR vCenter = XMLoadFloat3(&Center);
vCenter = XMVectorAdd(XMVector3Rotate(XMVectorScale(vCenter, Scale), Rotation), Translation);
XMStoreFloat3(&Out.Center, vCenter);
Out.Radius = Radius * Scale;
}
_Use_decl_annotations_
inline ContainmentType XM_CALLCONV BoundingSphere::Contains(FXMVECTOR Point) const noexcept
{
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vRadius = XMVectorReplicatePtr(&Radius);
XMVECTOR DistanceSquared = XMVector3LengthSq(XMVectorSubtract(Point, vCenter));
XMVECTOR RadiusSquared = XMVectorMultiply(vRadius, vRadius);
return XMVector3LessOrEqual(DistanceSquared, RadiusSquared) ? CONTAINS : DISJOINT;
}
_Use_decl_annotations_
inline ContainmentType XM_CALLCONV BoundingSphere::Contains(FXMVECTOR V0, FXMVECTOR V1, FXMVECTOR V2) const noexcept
{
if (!Intersects(V0, V1, V2))
return DISJOINT;
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vRadius = XMVectorReplicatePtr(&Radius);
XMVECTOR RadiusSquared = XMVectorMultiply(vRadius, vRadius);
XMVECTOR DistanceSquared = XMVector3LengthSq(XMVectorSubtract(V0, vCenter));
XMVECTOR Inside = XMVectorLessOrEqual(DistanceSquared, RadiusSquared);
DistanceSquared = XMVector3LengthSq(XMVectorSubtract(V1, vCenter));
Inside = XMVectorAndInt(Inside, XMVectorLessOrEqual(DistanceSquared, RadiusSquared));
DistanceSquared = XMVector3LengthSq(XMVectorSubtract(V2, vCenter));
Inside = XMVectorAndInt(Inside, XMVectorLessOrEqual(DistanceSquared, RadiusSquared));
return (XMVector3EqualInt(Inside, XMVectorTrueInt())) ? CONTAINS : INTERSECTS;
}
_Use_decl_annotations_
inline ContainmentType BoundingSphere::Contains(const BoundingSphere& sh) const noexcept
{
XMVECTOR Center1 = XMLoadFloat3(&Center);
float r1 = Radius;
XMVECTOR Center2 = XMLoadFloat3(&sh.Center);
float r2 = sh.Radius;
XMVECTOR V = XMVectorSubtract(Center2, Center1);
XMVECTOR Dist = XMVector3Length(V);
float d = XMVectorGetX(Dist);
return (r1 + r2 >= d) ? ((r1 - r2 >= d) ? CONTAINS : INTERSECTS) : DISJOINT;
}
_Use_decl_annotations_
inline ContainmentType BoundingSphere::Contains(const BoundingBox& box) const noexcept
{
if (!box.Intersects(*this))
return DISJOINT;
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vRadius = XMVectorReplicatePtr(&Radius);
XMVECTOR RadiusSq = XMVectorMultiply(vRadius, vRadius);
XMVECTOR boxCenter = XMLoadFloat3(&box.Center);
XMVECTOR boxExtents = XMLoadFloat3(&box.Extents);
XMVECTOR InsideAll = XMVectorTrueInt();
XMVECTOR offset = XMVectorSubtract(boxCenter, vCenter);
for (size_t i = 0; i < BoundingBox::CORNER_COUNT; ++i)
{
XMVECTOR C = XMVectorMultiplyAdd(boxExtents, g_BoxOffset[i], offset);
XMVECTOR d = XMVector3LengthSq(C);
InsideAll = XMVectorAndInt(InsideAll, XMVectorLessOrEqual(d, RadiusSq));
}
return (XMVector3EqualInt(InsideAll, XMVectorTrueInt())) ? CONTAINS : INTERSECTS;
}
_Use_decl_annotations_
inline ContainmentType BoundingSphere::Contains(const BoundingOrientedBox& box) const noexcept
{
if (!box.Intersects(*this))
return DISJOINT;
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vRadius = XMVectorReplicatePtr(&Radius);
XMVECTOR RadiusSq = XMVectorMultiply(vRadius, vRadius);
XMVECTOR boxCenter = XMLoadFloat3(&box.Center);
XMVECTOR boxExtents = XMLoadFloat3(&box.Extents);
XMVECTOR boxOrientation = XMLoadFloat4(&box.Orientation);
assert(DirectX::MathInternal::XMQuaternionIsUnit(boxOrientation));
XMVECTOR InsideAll = XMVectorTrueInt();
for (size_t i = 0; i < BoundingOrientedBox::CORNER_COUNT; ++i)
{
XMVECTOR C = XMVectorAdd(XMVector3Rotate(XMVectorMultiply(boxExtents, g_BoxOffset[i]), boxOrientation), boxCenter);
XMVECTOR d = XMVector3LengthSq(XMVectorSubtract(vCenter, C));
InsideAll = XMVectorAndInt(InsideAll, XMVectorLessOrEqual(d, RadiusSq));
}
return (XMVector3EqualInt(InsideAll, XMVectorTrueInt())) ? CONTAINS : INTERSECTS;
}
_Use_decl_annotations_
inline ContainmentType BoundingSphere::Contains(const BoundingFrustum& fr) const noexcept
{
if (!fr.Intersects(*this))
return DISJOINT;
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vRadius = XMVectorReplicatePtr(&Radius);
XMVECTOR RadiusSq = XMVectorMultiply(vRadius, vRadius);
XMVECTOR vOrigin = XMLoadFloat3(&fr.Origin);
XMVECTOR vOrientation = XMLoadFloat4(&fr.Orientation);
assert(DirectX::MathInternal::XMQuaternionIsUnit(vOrientation));
XMVECTOR vRightTop = XMVectorSet(fr.RightSlope, fr.TopSlope, 1.0f, 0.0f);
XMVECTOR vRightBottom = XMVectorSet(fr.RightSlope, fr.BottomSlope, 1.0f, 0.0f);
XMVECTOR vLeftTop = XMVectorSet(fr.LeftSlope, fr.TopSlope, 1.0f, 0.0f);
XMVECTOR vLeftBottom = XMVectorSet(fr.LeftSlope, fr.BottomSlope, 1.0f, 0.0f);
XMVECTOR vNear = XMVectorReplicatePtr(&fr.Near);
XMVECTOR vFar = XMVectorReplicatePtr(&fr.Far);
XMVECTOR Corners[BoundingFrustum::CORNER_COUNT];
Corners[0] = XMVectorMultiply(vRightTop, vNear);
Corners[1] = XMVectorMultiply(vRightBottom, vNear);
Corners[2] = XMVectorMultiply(vLeftTop, vNear);
Corners[3] = XMVectorMultiply(vLeftBottom, vNear);
Corners[4] = XMVectorMultiply(vRightTop, vFar);
Corners[5] = XMVectorMultiply(vRightBottom, vFar);
Corners[6] = XMVectorMultiply(vLeftTop, vFar);
Corners[7] = XMVectorMultiply(vLeftBottom, vFar);
XMVECTOR InsideAll = XMVectorTrueInt();
for (size_t i = 0; i < BoundingFrustum::CORNER_COUNT; ++i)
{
XMVECTOR C = XMVectorAdd(XMVector3Rotate(Corners[i], vOrientation), vOrigin);
XMVECTOR d = XMVector3LengthSq(XMVectorSubtract(vCenter, C));
InsideAll = XMVectorAndInt(InsideAll, XMVectorLessOrEqual(d, RadiusSq));
}
return (XMVector3EqualInt(InsideAll, XMVectorTrueInt())) ? CONTAINS : INTERSECTS;
}
_Use_decl_annotations_
inline bool BoundingSphere::Intersects(const BoundingSphere& sh) const noexcept
{
XMVECTOR vCenterA = XMLoadFloat3(&Center);
XMVECTOR vRadiusA = XMVectorReplicatePtr(&Radius);
XMVECTOR vCenterB = XMLoadFloat3(&sh.Center);
XMVECTOR vRadiusB = XMVectorReplicatePtr(&sh.Radius);
XMVECTOR Delta = XMVectorSubtract(vCenterB, vCenterA);
XMVECTOR DistanceSquared = XMVector3LengthSq(Delta);
XMVECTOR RadiusSquared = XMVectorAdd(vRadiusA, vRadiusB);
RadiusSquared = XMVectorMultiply(RadiusSquared, RadiusSquared);
return XMVector3LessOrEqual(DistanceSquared, RadiusSquared);
}
_Use_decl_annotations_
inline bool BoundingSphere::Intersects(const BoundingBox& box) const noexcept
{
return box.Intersects(*this);
}
_Use_decl_annotations_
inline bool BoundingSphere::Intersects(const BoundingOrientedBox& box) const noexcept
{
return box.Intersects(*this);
}
_Use_decl_annotations_
inline bool BoundingSphere::Intersects(const BoundingFrustum& fr) const noexcept
{
return fr.Intersects(*this);
}
_Use_decl_annotations_
inline bool XM_CALLCONV BoundingSphere::Intersects(FXMVECTOR V0, FXMVECTOR V1, FXMVECTOR V2) const noexcept
{
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vRadius = XMVectorReplicatePtr(&Radius);
XMVECTOR N = XMVector3Normalize(XMVector3Cross(XMVectorSubtract(V1, V0), XMVectorSubtract(V2, V0)));
assert(!XMVector3Equal(N, XMVectorZero()));
XMVECTOR Dist = XMVector3Dot(XMVectorSubtract(vCenter, V0), N);
XMVECTOR NoIntersection = XMVectorLess(Dist, XMVectorNegate(vRadius));
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorGreater(Dist, vRadius));
XMVECTOR Point = XMVectorNegativeMultiplySubtract(N, Dist, vCenter);
XMVECTOR Intersection = DirectX::MathInternal::PointOnPlaneInsideTriangle(Point, V0, V1, V2);
XMVECTOR RadiusSq = XMVectorMultiply(vRadius, vRadius);
Point = DirectX::MathInternal::PointOnLineSegmentNearestPoint(V0, V1, vCenter);
Intersection = XMVectorOrInt(Intersection, XMVectorLessOrEqual(XMVector3LengthSq(XMVectorSubtract(vCenter, Point)), RadiusSq));
Point = DirectX::MathInternal::PointOnLineSegmentNearestPoint(V1, V2, vCenter);
Intersection = XMVectorOrInt(Intersection, XMVectorLessOrEqual(XMVector3LengthSq(XMVectorSubtract(vCenter, Point)), RadiusSq));
Point = DirectX::MathInternal::PointOnLineSegmentNearestPoint(V2, V0, vCenter);
Intersection = XMVectorOrInt(Intersection, XMVectorLessOrEqual(XMVector3LengthSq(XMVectorSubtract(vCenter, Point)), RadiusSq));
return XMVector4EqualInt(XMVectorAndCInt(Intersection, NoIntersection), XMVectorTrueInt());
}
_Use_decl_annotations_
inline PlaneIntersectionType XM_CALLCONV BoundingSphere::Intersects(FXMVECTOR Plane) const noexcept
{
assert(DirectX::MathInternal::XMPlaneIsUnit(Plane));
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vRadius = XMVectorReplicatePtr(&Radius);
vCenter = XMVectorInsert<0, 0, 0, 0, 1>(vCenter, XMVectorSplatOne());
XMVECTOR Outside, Inside;
DirectX::MathInternal::FastIntersectSpherePlane(vCenter, vRadius, Plane, Outside, Inside);
if (XMVector4EqualInt(Outside, XMVectorTrueInt()))
return FRONT;
if (XMVector4EqualInt(Inside, XMVectorTrueInt()))
return BACK;
return INTERSECTING;
}
_Use_decl_annotations_
inline bool XM_CALLCONV BoundingSphere::Intersects(FXMVECTOR Origin, FXMVECTOR Direction, float& Dist) const noexcept
{
assert(DirectX::MathInternal::XMVector3IsUnit(Direction));
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vRadius = XMVectorReplicatePtr(&Radius);
XMVECTOR l = XMVectorSubtract(vCenter, Origin);
XMVECTOR s = XMVector3Dot(l, Direction);
XMVECTOR l2 = XMVector3Dot(l, l);
XMVECTOR r2 = XMVectorMultiply(vRadius, vRadius);
XMVECTOR m2 = XMVectorNegativeMultiplySubtract(s, s, l2);
XMVECTOR NoIntersection;
NoIntersection = XMVectorAndInt(XMVectorLess(s, XMVectorZero()), XMVectorGreater(l2, r2));
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorGreater(m2, r2));
XMVECTOR q = XMVectorSqrt(XMVectorSubtract(r2, m2));
XMVECTOR t1 = XMVectorSubtract(s, q);
XMVECTOR t2 = XMVectorAdd(s, q);
XMVECTOR OriginInside = XMVectorLessOrEqual(l2, r2);
XMVECTOR t = XMVectorSelect(t1, t2, OriginInside);
if (XMVector4NotEqualInt(NoIntersection, XMVectorTrueInt()))
{
XMStoreFloat(&Dist, t);
return true;
}
Dist = 0.f;
return false;
}
_Use_decl_annotations_
inline ContainmentType XM_CALLCONV BoundingSphere::ContainedBy(
FXMVECTOR Plane0, FXMVECTOR Plane1, FXMVECTOR Plane2,
GXMVECTOR Plane3,
HXMVECTOR Plane4, HXMVECTOR Plane5) const noexcept
{
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vRadius = XMVectorReplicatePtr(&Radius);
vCenter = XMVectorInsert<0, 0, 0, 0, 1>(vCenter, XMVectorSplatOne());
XMVECTOR Outside, Inside;
DirectX::MathInternal::FastIntersectSpherePlane(vCenter, vRadius, Plane0, Outside, Inside);
XMVECTOR AnyOutside = Outside;
XMVECTOR AllInside = Inside;
DirectX::MathInternal::FastIntersectSpherePlane(vCenter, vRadius, Plane1, Outside, Inside);
AnyOutside = XMVectorOrInt(AnyOutside, Outside);
AllInside = XMVectorAndInt(AllInside, Inside);
DirectX::MathInternal::FastIntersectSpherePlane(vCenter, vRadius, Plane2, Outside, Inside);
AnyOutside = XMVectorOrInt(AnyOutside, Outside);
AllInside = XMVectorAndInt(AllInside, Inside);
DirectX::MathInternal::FastIntersectSpherePlane(vCenter, vRadius, Plane3, Outside, Inside);
AnyOutside = XMVectorOrInt(AnyOutside, Outside);
AllInside = XMVectorAndInt(AllInside, Inside);
DirectX::MathInternal::FastIntersectSpherePlane(vCenter, vRadius, Plane4, Outside, Inside);
AnyOutside = XMVectorOrInt(AnyOutside, Outside);
AllInside = XMVectorAndInt(AllInside, Inside);
DirectX::MathInternal::FastIntersectSpherePlane(vCenter, vRadius, Plane5, Outside, Inside);
AnyOutside = XMVectorOrInt(AnyOutside, Outside);
AllInside = XMVectorAndInt(AllInside, Inside);
if (XMVector4EqualInt(AnyOutside, XMVectorTrueInt()))
return DISJOINT;
if (XMVector4EqualInt(AllInside, XMVectorTrueInt()))
return CONTAINS;
return INTERSECTS;
}
_Use_decl_annotations_
inline void BoundingSphere::CreateMerged(BoundingSphere& Out, const BoundingSphere& S1, const BoundingSphere& S2) noexcept
{
XMVECTOR Center1 = XMLoadFloat3(&S1.Center);
float r1 = S1.Radius;
XMVECTOR Center2 = XMLoadFloat3(&S2.Center);
float r2 = S2.Radius;
XMVECTOR V = XMVectorSubtract(Center2, Center1);
XMVECTOR Dist = XMVector3Length(V);
float d = XMVectorGetX(Dist);
if (r1 + r2 >= d)
{
if (r1 - r2 >= d)
{
Out = S1;
return;
}
else if (r2 - r1 >= d)
{
Out = S2;
return;
}
}
XMVECTOR N = XMVectorDivide(V, Dist);
float t1 = XMMin(-r1, d - r2);
float t2 = XMMax(r1, d + r2);
float t_5 = (t2 - t1) * 0.5f;
XMVECTOR NCenter = XMVectorAdd(Center1, XMVectorMultiply(N, XMVectorReplicate(t_5 + t1)));
XMStoreFloat3(&Out.Center, NCenter);
Out.Radius = t_5;
}
_Use_decl_annotations_
inline void BoundingSphere::CreateFromBoundingBox(BoundingSphere& Out, const BoundingBox& box) noexcept
{
Out.Center = box.Center;
XMVECTOR vExtents = XMLoadFloat3(&box.Extents);
Out.Radius = XMVectorGetX(XMVector3Length(vExtents));
}
_Use_decl_annotations_
inline void BoundingSphere::CreateFromBoundingBox(BoundingSphere& Out, const BoundingOrientedBox& box) noexcept
{
Out.Center = box.Center;
XMVECTOR vExtents = XMLoadFloat3(&box.Extents);
Out.Radius = XMVectorGetX(XMVector3Length(vExtents));
}
_Use_decl_annotations_
inline void BoundingSphere::CreateFromPoints(BoundingSphere& Out, size_t Count, const XMFLOAT3* pPoints, size_t Stride) noexcept
{
assert(Count > 0);
assert(pPoints);
XMVECTOR MinX, MaxX, MinY, MaxY, MinZ, MaxZ;
MinX = MaxX = MinY = MaxY = MinZ = MaxZ = XMLoadFloat3(pPoints);
for (size_t i = 1; i < Count; ++i)
{
XMVECTOR Point = XMLoadFloat3(reinterpret_cast<const XMFLOAT3*>(reinterpret_cast<const uint8_t*>(pPoints) + i * Stride));
float px = XMVectorGetX(Point);
float py = XMVectorGetY(Point);
float pz = XMVectorGetZ(Point);
if (px < XMVectorGetX(MinX))
MinX = Point;
if (px > XMVectorGetX(MaxX))
MaxX = Point;
if (py < XMVectorGetY(MinY))
MinY = Point;
if (py > XMVectorGetY(MaxY))
MaxY = Point;
if (pz < XMVectorGetZ(MinZ))
MinZ = Point;
if (pz > XMVectorGetZ(MaxZ))
MaxZ = Point;
}
XMVECTOR DeltaX = XMVectorSubtract(MaxX, MinX);
XMVECTOR DistX = XMVector3Length(DeltaX);
XMVECTOR DeltaY = XMVectorSubtract(MaxY, MinY);
XMVECTOR DistY = XMVector3Length(DeltaY);
XMVECTOR DeltaZ = XMVectorSubtract(MaxZ, MinZ);
XMVECTOR DistZ = XMVector3Length(DeltaZ);
XMVECTOR vCenter;
XMVECTOR vRadius;
if (XMVector3Greater(DistX, DistY))
{
if (XMVector3Greater(DistX, DistZ))
{
vCenter = XMVectorLerp(MaxX, MinX, 0.5f);
vRadius = XMVectorScale(DistX, 0.5f);
}
else
{
vCenter = XMVectorLerp(MaxZ, MinZ, 0.5f);
vRadius = XMVectorScale(DistZ, 0.5f);
}
}
else {
if (XMVector3Greater(DistY, DistZ))
{
vCenter = XMVectorLerp(MaxY, MinY, 0.5f);
vRadius = XMVectorScale(DistY, 0.5f);
}
else
{
vCenter = XMVectorLerp(MaxZ, MinZ, 0.5f);
vRadius = XMVectorScale(DistZ, 0.5f);
}
}
for (size_t i = 0; i < Count; ++i)
{
XMVECTOR Point = XMLoadFloat3(reinterpret_cast<const XMFLOAT3*>(reinterpret_cast<const uint8_t*>(pPoints) + i * Stride));
XMVECTOR Delta = XMVectorSubtract(Point, vCenter);
XMVECTOR Dist = XMVector3Length(Delta);
if (XMVector3Greater(Dist, vRadius))
{
vRadius = XMVectorScale(XMVectorAdd(vRadius, Dist), 0.5f);
vCenter = XMVectorAdd(vCenter, XMVectorMultiply(XMVectorSubtract(XMVectorReplicate(1.0f), XMVectorDivide(vRadius, Dist)), Delta));
}
}
XMStoreFloat3(&Out.Center, vCenter);
XMStoreFloat(&Out.Radius, vRadius);
}
_Use_decl_annotations_
inline void BoundingSphere::CreateFromFrustum(BoundingSphere& Out, const BoundingFrustum& fr) noexcept
{
XMFLOAT3 Corners[BoundingFrustum::CORNER_COUNT];
fr.GetCorners(Corners);
CreateFromPoints(Out, BoundingFrustum::CORNER_COUNT, Corners, sizeof(XMFLOAT3));
}
_Use_decl_annotations_
inline void XM_CALLCONV BoundingBox::Transform(BoundingBox& Out, FXMMATRIX M) const noexcept
{
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vExtents = XMLoadFloat3(&Extents);
XMVECTOR Corner = XMVectorMultiplyAdd(vExtents, g_BoxOffset[0], vCenter);
Corner = XMVector3Transform(Corner, M);
XMVECTOR Min, Max;
Min = Max = Corner;
for (size_t i = 1; i < CORNER_COUNT; ++i)
{
Corner = XMVectorMultiplyAdd(vExtents, g_BoxOffset[i], vCenter);
Corner = XMVector3Transform(Corner, M);
Min = XMVectorMin(Min, Corner);
Max = XMVectorMax(Max, Corner);
}
XMStoreFloat3(&Out.Center, XMVectorScale(XMVectorAdd(Min, Max), 0.5f));
XMStoreFloat3(&Out.Extents, XMVectorScale(XMVectorSubtract(Max, Min), 0.5f));
}
_Use_decl_annotations_
inline void XM_CALLCONV BoundingBox::Transform(BoundingBox& Out, float Scale, FXMVECTOR Rotation, FXMVECTOR Translation) const noexcept
{
assert(DirectX::MathInternal::XMQuaternionIsUnit(Rotation));
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vExtents = XMLoadFloat3(&Extents);
XMVECTOR VectorScale = XMVectorReplicate(Scale);
XMVECTOR Corner = XMVectorMultiplyAdd(vExtents, g_BoxOffset[0], vCenter);
Corner = XMVectorAdd(XMVector3Rotate(XMVectorMultiply(Corner, VectorScale), Rotation), Translation);
XMVECTOR Min, Max;
Min = Max = Corner;
for (size_t i = 1; i < CORNER_COUNT; ++i)
{
Corner = XMVectorMultiplyAdd(vExtents, g_BoxOffset[i], vCenter);
Corner = XMVectorAdd(XMVector3Rotate(XMVectorMultiply(Corner, VectorScale), Rotation), Translation);
Min = XMVectorMin(Min, Corner);
Max = XMVectorMax(Max, Corner);
}
XMStoreFloat3(&Out.Center, XMVectorScale(XMVectorAdd(Min, Max), 0.5f));
XMStoreFloat3(&Out.Extents, XMVectorScale(XMVectorSubtract(Max, Min), 0.5f));
}
_Use_decl_annotations_
inline void BoundingBox::GetCorners(XMFLOAT3* Corners) const noexcept
{
assert(Corners != nullptr);
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vExtents = XMLoadFloat3(&Extents);
for (size_t i = 0; i < CORNER_COUNT; ++i)
{
XMVECTOR C = XMVectorMultiplyAdd(vExtents, g_BoxOffset[i], vCenter);
XMStoreFloat3(&Corners[i], C);
}
}
_Use_decl_annotations_
inline ContainmentType XM_CALLCONV BoundingBox::Contains(FXMVECTOR Point) const noexcept
{
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vExtents = XMLoadFloat3(&Extents);
return XMVector3InBounds(XMVectorSubtract(Point, vCenter), vExtents) ? CONTAINS : DISJOINT;
}
_Use_decl_annotations_
inline ContainmentType XM_CALLCONV BoundingBox::Contains(FXMVECTOR V0, FXMVECTOR V1, FXMVECTOR V2) const noexcept
{
if (!Intersects(V0, V1, V2))
return DISJOINT;
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vExtents = XMLoadFloat3(&Extents);
XMVECTOR d = XMVectorAbs(XMVectorSubtract(V0, vCenter));
XMVECTOR Inside = XMVectorLessOrEqual(d, vExtents);
d = XMVectorAbs(XMVectorSubtract(V1, vCenter));
Inside = XMVectorAndInt(Inside, XMVectorLessOrEqual(d, vExtents));
d = XMVectorAbs(XMVectorSubtract(V2, vCenter));
Inside = XMVectorAndInt(Inside, XMVectorLessOrEqual(d, vExtents));
return (XMVector3EqualInt(Inside, XMVectorTrueInt())) ? CONTAINS : INTERSECTS;
}
_Use_decl_annotations_
inline ContainmentType BoundingBox::Contains(const BoundingSphere& sh) const noexcept
{
XMVECTOR SphereCenter = XMLoadFloat3(&sh.Center);
XMVECTOR SphereRadius = XMVectorReplicatePtr(&sh.Radius);
XMVECTOR BoxCenter = XMLoadFloat3(&Center);
XMVECTOR BoxExtents = XMLoadFloat3(&Extents);
XMVECTOR BoxMin = XMVectorSubtract(BoxCenter, BoxExtents);
XMVECTOR BoxMax = XMVectorAdd(BoxCenter, BoxExtents);
XMVECTOR d = XMVectorZero();
XMVECTOR LessThanMin = XMVectorLess(SphereCenter, BoxMin);
XMVECTOR GreaterThanMax = XMVectorGreater(SphereCenter, BoxMax);
XMVECTOR MinDelta = XMVectorSubtract(SphereCenter, BoxMin);
XMVECTOR MaxDelta = XMVectorSubtract(SphereCenter, BoxMax);
d = XMVectorSelect(d, MinDelta, LessThanMin);
d = XMVectorSelect(d, MaxDelta, GreaterThanMax);
XMVECTOR d2 = XMVector3Dot(d, d);
if (XMVector3Greater(d2, XMVectorMultiply(SphereRadius, SphereRadius)))
return DISJOINT;
XMVECTOR InsideAll = XMVectorLessOrEqual(XMVectorAdd(BoxMin, SphereRadius), SphereCenter);
InsideAll = XMVectorAndInt(InsideAll, XMVectorLessOrEqual(SphereCenter, XMVectorSubtract(BoxMax, SphereRadius)));
InsideAll = XMVectorAndInt(InsideAll, XMVectorGreater(XMVectorSubtract(BoxMax, BoxMin), SphereRadius));
return (XMVector3EqualInt(InsideAll, XMVectorTrueInt())) ? CONTAINS : INTERSECTS;
}
_Use_decl_annotations_
inline ContainmentType BoundingBox::Contains(const BoundingBox& box) const noexcept
{
XMVECTOR CenterA = XMLoadFloat3(&Center);
XMVECTOR ExtentsA = XMLoadFloat3(&Extents);
XMVECTOR CenterB = XMLoadFloat3(&box.Center);
XMVECTOR ExtentsB = XMLoadFloat3(&box.Extents);
XMVECTOR MinA = XMVectorSubtract(CenterA, ExtentsA);
XMVECTOR MaxA = XMVectorAdd(CenterA, ExtentsA);
XMVECTOR MinB = XMVectorSubtract(CenterB, ExtentsB);
XMVECTOR MaxB = XMVectorAdd(CenterB, ExtentsB);
XMVECTOR Disjoint = XMVectorOrInt(XMVectorGreater(MinA, MaxB), XMVectorGreater(MinB, MaxA));
if (DirectX::MathInternal::XMVector3AnyTrue(Disjoint))
return DISJOINT;
XMVECTOR Inside = XMVectorAndInt(XMVectorLessOrEqual(MinA, MinB), XMVectorLessOrEqual(MaxB, MaxA));
return DirectX::MathInternal::XMVector3AllTrue(Inside) ? CONTAINS : INTERSECTS;
}
_Use_decl_annotations_
inline ContainmentType BoundingBox::Contains(const BoundingOrientedBox& box) const noexcept
{
if (!box.Intersects(*this))
return DISJOINT;
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vExtents = XMLoadFloat3(&Extents);
XMVECTOR oCenter = XMVectorSubtract(XMLoadFloat3(&box.Center), vCenter);
XMVECTOR oExtents = XMLoadFloat3(&box.Extents);
XMVECTOR oOrientation = XMLoadFloat4(&box.Orientation);
assert(DirectX::MathInternal::XMQuaternionIsUnit(oOrientation));
XMVECTOR Inside = XMVectorTrueInt();
for (size_t i = 0; i < BoundingOrientedBox::CORNER_COUNT; ++i)
{
XMVECTOR C = XMVectorAdd(XMVector3Rotate(XMVectorMultiply(oExtents, g_BoxOffset[i]), oOrientation), oCenter);
XMVECTOR d = XMVectorAbs(C);
Inside = XMVectorAndInt(Inside, XMVectorLessOrEqual(d, vExtents));
}
return (XMVector3EqualInt(Inside, XMVectorTrueInt())) ? CONTAINS : INTERSECTS;
}
_Use_decl_annotations_
inline ContainmentType BoundingBox::Contains(const BoundingFrustum& fr) const noexcept
{
if (!fr.Intersects(*this))
return DISJOINT;
XMFLOAT3 Corners[BoundingFrustum::CORNER_COUNT];
fr.GetCorners(Corners);
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vExtents = XMLoadFloat3(&Extents);
XMVECTOR Inside = XMVectorTrueInt();
for (size_t i = 0; i < BoundingFrustum::CORNER_COUNT; ++i)
{
XMVECTOR Point = XMLoadFloat3(&Corners[i]);
XMVECTOR d = XMVectorAbs(XMVectorSubtract(Point, vCenter));
Inside = XMVectorAndInt(Inside, XMVectorLessOrEqual(d, vExtents));
}
return (XMVector3EqualInt(Inside, XMVectorTrueInt())) ? CONTAINS : INTERSECTS;
}
_Use_decl_annotations_
inline bool BoundingBox::Intersects(const BoundingSphere& sh) const noexcept
{
XMVECTOR SphereCenter = XMLoadFloat3(&sh.Center);
XMVECTOR SphereRadius = XMVectorReplicatePtr(&sh.Radius);
XMVECTOR BoxCenter = XMLoadFloat3(&Center);
XMVECTOR BoxExtents = XMLoadFloat3(&Extents);
XMVECTOR BoxMin = XMVectorSubtract(BoxCenter, BoxExtents);
XMVECTOR BoxMax = XMVectorAdd(BoxCenter, BoxExtents);
XMVECTOR d = XMVectorZero();
XMVECTOR LessThanMin = XMVectorLess(SphereCenter, BoxMin);
XMVECTOR GreaterThanMax = XMVectorGreater(SphereCenter, BoxMax);
XMVECTOR MinDelta = XMVectorSubtract(SphereCenter, BoxMin);
XMVECTOR MaxDelta = XMVectorSubtract(SphereCenter, BoxMax);
d = XMVectorSelect(d, MinDelta, LessThanMin);
d = XMVectorSelect(d, MaxDelta, GreaterThanMax);
XMVECTOR d2 = XMVector3Dot(d, d);
return XMVector3LessOrEqual(d2, XMVectorMultiply(SphereRadius, SphereRadius));
}
_Use_decl_annotations_
inline bool BoundingBox::Intersects(const BoundingBox& box) const noexcept
{
XMVECTOR CenterA = XMLoadFloat3(&Center);
XMVECTOR ExtentsA = XMLoadFloat3(&Extents);
XMVECTOR CenterB = XMLoadFloat3(&box.Center);
XMVECTOR ExtentsB = XMLoadFloat3(&box.Extents);
XMVECTOR MinA = XMVectorSubtract(CenterA, ExtentsA);
XMVECTOR MaxA = XMVectorAdd(CenterA, ExtentsA);
XMVECTOR MinB = XMVectorSubtract(CenterB, ExtentsB);
XMVECTOR MaxB = XMVectorAdd(CenterB, ExtentsB);
XMVECTOR Disjoint = XMVectorOrInt(XMVectorGreater(MinA, MaxB), XMVectorGreater(MinB, MaxA));
return !DirectX::MathInternal::XMVector3AnyTrue(Disjoint);
}
_Use_decl_annotations_
inline bool BoundingBox::Intersects(const BoundingOrientedBox& box) const noexcept
{
return box.Intersects(*this);
}
_Use_decl_annotations_
inline bool BoundingBox::Intersects(const BoundingFrustum& fr) const noexcept
{
return fr.Intersects(*this);
}
_Use_decl_annotations_
inline bool XM_CALLCONV BoundingBox::Intersects(FXMVECTOR V0, FXMVECTOR V1, FXMVECTOR V2) const noexcept
{
XMVECTOR Zero = XMVectorZero();
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vExtents = XMLoadFloat3(&Extents);
XMVECTOR BoxMin = XMVectorSubtract(vCenter, vExtents);
XMVECTOR BoxMax = XMVectorAdd(vCenter, vExtents);
XMVECTOR TriMin = XMVectorMin(XMVectorMin(V0, V1), V2);
XMVECTOR TriMax = XMVectorMax(XMVectorMax(V0, V1), V2);
XMVECTOR Disjoint = XMVectorOrInt(XMVectorGreater(TriMin, BoxMax), XMVectorGreater(BoxMin, TriMax));
if (DirectX::MathInternal::XMVector3AnyTrue(Disjoint))
return false;
XMVECTOR Normal = XMVector3Cross(XMVectorSubtract(V1, V0), XMVectorSubtract(V2, V0));
XMVECTOR Dist = XMVector3Dot(Normal, V0);
assert(!XMVector3Equal(Normal, Zero));
XMVECTOR NormalSelect = XMVectorGreater(Normal, Zero);
XMVECTOR V_Min = XMVectorSelect(BoxMax, BoxMin, NormalSelect);
XMVECTOR V_Max = XMVectorSelect(BoxMin, BoxMax, NormalSelect);
XMVECTOR MinDist = XMVector3Dot(V_Min, Normal);
XMVECTOR MaxDist = XMVector3Dot(V_Max, Normal);
XMVECTOR NoIntersection = XMVectorGreater(MinDist, Dist);
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorLess(MaxDist, Dist));
XMVECTOR TV0 = XMVectorSubtract(V0, vCenter);
XMVECTOR TV1 = XMVectorSubtract(V1, vCenter);
XMVECTOR TV2 = XMVectorSubtract(V2, vCenter);
XMVECTOR e0 = XMVectorSubtract(TV1, TV0);
XMVECTOR e1 = XMVectorSubtract(TV2, TV1);
XMVECTOR e2 = XMVectorSubtract(TV0, TV2);
e0 = XMVectorInsert<0, 0, 0, 0, 1>(e0, Zero);
e1 = XMVectorInsert<0, 0, 0, 0, 1>(e1, Zero);
e2 = XMVectorInsert<0, 0, 0, 0, 1>(e2, Zero);
XMVECTOR Axis;
XMVECTOR p0, p1, p2;
XMVECTOR Min, Max;
XMVECTOR Radius;
Axis = XMVectorPermute<XM_PERMUTE_0W, XM_PERMUTE_1Z, XM_PERMUTE_0Y, XM_PERMUTE_0X>(e0, XMVectorNegate(e0));
p0 = XMVector3Dot(TV0, Axis);
p2 = XMVector3Dot(TV2, Axis);
Min = XMVectorMin(p0, p2);
Max = XMVectorMax(p0, p2);
Radius = XMVector3Dot(vExtents, XMVectorAbs(Axis));
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorGreater(Min, Radius));
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorLess(Max, XMVectorNegate(Radius)));
Axis = XMVectorPermute<XM_PERMUTE_0W, XM_PERMUTE_1Z, XM_PERMUTE_0Y, XM_PERMUTE_0X>(e1, XMVectorNegate(e1));
p0 = XMVector3Dot(TV0, Axis);
p1 = XMVector3Dot(TV1, Axis);
Min = XMVectorMin(p0, p1);
Max = XMVectorMax(p0, p1);
Radius = XMVector3Dot(vExtents, XMVectorAbs(Axis));
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorGreater(Min, Radius));
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorLess(Max, XMVectorNegate(Radius)));
Axis = XMVectorPermute<XM_PERMUTE_0W, XM_PERMUTE_1Z, XM_PERMUTE_0Y, XM_PERMUTE_0X>(e2, XMVectorNegate(e2));
p0 = XMVector3Dot(TV0, Axis);
p1 = XMVector3Dot(TV1, Axis);
Min = XMVectorMin(p0, p1);
Max = XMVectorMax(p0, p1);
Radius = XMVector3Dot(vExtents, XMVectorAbs(Axis));
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorGreater(Min, Radius));
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorLess(Max, XMVectorNegate(Radius)));
Axis = XMVectorPermute<XM_PERMUTE_0Z, XM_PERMUTE_0W, XM_PERMUTE_1X, XM_PERMUTE_0Y>(e0, XMVectorNegate(e0));
p0 = XMVector3Dot(TV0, Axis);
p2 = XMVector3Dot(TV2, Axis);
Min = XMVectorMin(p0, p2);
Max = XMVectorMax(p0, p2);
Radius = XMVector3Dot(vExtents, XMVectorAbs(Axis));
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorGreater(Min, Radius));
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorLess(Max, XMVectorNegate(Radius)));
Axis = XMVectorPermute<XM_PERMUTE_0Z, XM_PERMUTE_0W, XM_PERMUTE_1X, XM_PERMUTE_0Y>(e1, XMVectorNegate(e1));
p0 = XMVector3Dot(TV0, Axis);
p1 = XMVector3Dot(TV1, Axis);
Min = XMVectorMin(p0, p1);
Max = XMVectorMax(p0, p1);
Radius = XMVector3Dot(vExtents, XMVectorAbs(Axis));
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorGreater(Min, Radius));
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorLess(Max, XMVectorNegate(Radius)));
Axis = XMVectorPermute<XM_PERMUTE_0Z, XM_PERMUTE_0W, XM_PERMUTE_1X, XM_PERMUTE_0Y>(e2, XMVectorNegate(e2));
p0 = XMVector3Dot(TV0, Axis);
p1 = XMVector3Dot(TV1, Axis);
Min = XMVectorMin(p0, p1);
Max = XMVectorMax(p0, p1);
Radius = XMVector3Dot(vExtents, XMVectorAbs(Axis));
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorGreater(Min, Radius));
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorLess(Max, XMVectorNegate(Radius)));
Axis = XMVectorPermute<XM_PERMUTE_1Y, XM_PERMUTE_0X, XM_PERMUTE_0W, XM_PERMUTE_0Z>(e0, XMVectorNegate(e0));
p0 = XMVector3Dot(TV0, Axis);
p2 = XMVector3Dot(TV2, Axis);
Min = XMVectorMin(p0, p2);
Max = XMVectorMax(p0, p2);
Radius = XMVector3Dot(vExtents, XMVectorAbs(Axis));
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorGreater(Min, Radius));
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorLess(Max, XMVectorNegate(Radius)));
Axis = XMVectorPermute<XM_PERMUTE_1Y, XM_PERMUTE_0X, XM_PERMUTE_0W, XM_PERMUTE_0Z>(e1, XMVectorNegate(e1));
p0 = XMVector3Dot(TV0, Axis);
p1 = XMVector3Dot(TV1, Axis);
Min = XMVectorMin(p0, p1);
Max = XMVectorMax(p0, p1);
Radius = XMVector3Dot(vExtents, XMVectorAbs(Axis));
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorGreater(Min, Radius));
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorLess(Max, XMVectorNegate(Radius)));
Axis = XMVectorPermute<XM_PERMUTE_1Y, XM_PERMUTE_0X, XM_PERMUTE_0W, XM_PERMUTE_0Z>(e2, XMVectorNegate(e2));
p0 = XMVector3Dot(TV0, Axis);
p1 = XMVector3Dot(TV1, Axis);
Min = XMVectorMin(p0, p1);
Max = XMVectorMax(p0, p1);
Radius = XMVector3Dot(vExtents, XMVectorAbs(Axis));
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorGreater(Min, Radius));
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorLess(Max, XMVectorNegate(Radius)));
return XMVector4NotEqualInt(NoIntersection, XMVectorTrueInt());
}
_Use_decl_annotations_
inline PlaneIntersectionType XM_CALLCONV BoundingBox::Intersects(FXMVECTOR Plane) const noexcept
{
assert(DirectX::MathInternal::XMPlaneIsUnit(Plane));
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vExtents = XMLoadFloat3(&Extents);
vCenter = XMVectorInsert<0, 0, 0, 0, 1>(vCenter, XMVectorSplatOne());
XMVECTOR Outside, Inside;
DirectX::MathInternal::FastIntersectAxisAlignedBoxPlane(vCenter, vExtents, Plane, Outside, Inside);
if (XMVector4EqualInt(Outside, XMVectorTrueInt()))
return FRONT;
if (XMVector4EqualInt(Inside, XMVectorTrueInt()))
return BACK;
return INTERSECTING;
}
_Use_decl_annotations_
inline bool XM_CALLCONV BoundingBox::Intersects(FXMVECTOR Origin, FXMVECTOR Direction, float& Dist) const noexcept
{
assert(DirectX::MathInternal::XMVector3IsUnit(Direction));
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vExtents = XMLoadFloat3(&Extents);
XMVECTOR TOrigin = XMVectorSubtract(vCenter, Origin);
XMVECTOR AxisDotOrigin = TOrigin;
XMVECTOR AxisDotDirection = Direction;
XMVECTOR IsParallel = XMVectorLessOrEqual(XMVectorAbs(AxisDotDirection), g_RayEpsilon);
XMVECTOR InverseAxisDotDirection = XMVectorReciprocal(AxisDotDirection);
XMVECTOR t1 = XMVectorMultiply(XMVectorSubtract(AxisDotOrigin, vExtents), InverseAxisDotDirection);
XMVECTOR t2 = XMVectorMultiply(XMVectorAdd(AxisDotOrigin, vExtents), InverseAxisDotDirection);
XMVECTOR t_min = XMVectorSelect(XMVectorMin(t1, t2), g_FltMin, IsParallel);
XMVECTOR t_max = XMVectorSelect(XMVectorMax(t1, t2), g_FltMax, IsParallel);
t_min = XMVectorMax(t_min, XMVectorSplatY(t_min)); t_min = XMVectorMax(t_min, XMVectorSplatZ(t_min)); t_max = XMVectorMin(t_max, XMVectorSplatY(t_max)); t_max = XMVectorMin(t_max, XMVectorSplatZ(t_max));
XMVECTOR NoIntersection = XMVectorGreater(XMVectorSplatX(t_min), XMVectorSplatX(t_max));
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorLess(XMVectorSplatX(t_max), XMVectorZero()));
XMVECTOR ParallelOverlap = XMVectorInBounds(AxisDotOrigin, vExtents);
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorAndCInt(IsParallel, ParallelOverlap));
if (!DirectX::MathInternal::XMVector3AnyTrue(NoIntersection))
{
XMStoreFloat(&Dist, t_min);
return true;
}
Dist = 0.f;
return false;
}
_Use_decl_annotations_
inline ContainmentType XM_CALLCONV BoundingBox::ContainedBy(
FXMVECTOR Plane0, FXMVECTOR Plane1, FXMVECTOR Plane2,
GXMVECTOR Plane3,
HXMVECTOR Plane4, HXMVECTOR Plane5) const noexcept
{
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vExtents = XMLoadFloat3(&Extents);
vCenter = XMVectorInsert<0, 0, 0, 0, 1>(vCenter, XMVectorSplatOne());
XMVECTOR Outside, Inside;
DirectX::MathInternal::FastIntersectAxisAlignedBoxPlane(vCenter, vExtents, Plane0, Outside, Inside);
XMVECTOR AnyOutside = Outside;
XMVECTOR AllInside = Inside;
DirectX::MathInternal::FastIntersectAxisAlignedBoxPlane(vCenter, vExtents, Plane1, Outside, Inside);
AnyOutside = XMVectorOrInt(AnyOutside, Outside);
AllInside = XMVectorAndInt(AllInside, Inside);
DirectX::MathInternal::FastIntersectAxisAlignedBoxPlane(vCenter, vExtents, Plane2, Outside, Inside);
AnyOutside = XMVectorOrInt(AnyOutside, Outside);
AllInside = XMVectorAndInt(AllInside, Inside);
DirectX::MathInternal::FastIntersectAxisAlignedBoxPlane(vCenter, vExtents, Plane3, Outside, Inside);
AnyOutside = XMVectorOrInt(AnyOutside, Outside);
AllInside = XMVectorAndInt(AllInside, Inside);
DirectX::MathInternal::FastIntersectAxisAlignedBoxPlane(vCenter, vExtents, Plane4, Outside, Inside);
AnyOutside = XMVectorOrInt(AnyOutside, Outside);
AllInside = XMVectorAndInt(AllInside, Inside);
DirectX::MathInternal::FastIntersectAxisAlignedBoxPlane(vCenter, vExtents, Plane5, Outside, Inside);
AnyOutside = XMVectorOrInt(AnyOutside, Outside);
AllInside = XMVectorAndInt(AllInside, Inside);
if (XMVector4EqualInt(AnyOutside, XMVectorTrueInt()))
return DISJOINT;
if (XMVector4EqualInt(AllInside, XMVectorTrueInt()))
return CONTAINS;
return INTERSECTS;
}
_Use_decl_annotations_
inline void BoundingBox::CreateMerged(BoundingBox& Out, const BoundingBox& b1, const BoundingBox& b2) noexcept
{
XMVECTOR b1Center = XMLoadFloat3(&b1.Center);
XMVECTOR b1Extents = XMLoadFloat3(&b1.Extents);
XMVECTOR b2Center = XMLoadFloat3(&b2.Center);
XMVECTOR b2Extents = XMLoadFloat3(&b2.Extents);
XMVECTOR Min = XMVectorSubtract(b1Center, b1Extents);
Min = XMVectorMin(Min, XMVectorSubtract(b2Center, b2Extents));
XMVECTOR Max = XMVectorAdd(b1Center, b1Extents);
Max = XMVectorMax(Max, XMVectorAdd(b2Center, b2Extents));
assert(XMVector3LessOrEqual(Min, Max));
XMStoreFloat3(&Out.Center, XMVectorScale(XMVectorAdd(Min, Max), 0.5f));
XMStoreFloat3(&Out.Extents, XMVectorScale(XMVectorSubtract(Max, Min), 0.5f));
}
_Use_decl_annotations_
inline void BoundingBox::CreateFromSphere(BoundingBox& Out, const BoundingSphere& sh) noexcept
{
XMVECTOR spCenter = XMLoadFloat3(&sh.Center);
XMVECTOR shRadius = XMVectorReplicatePtr(&sh.Radius);
XMVECTOR Min = XMVectorSubtract(spCenter, shRadius);
XMVECTOR Max = XMVectorAdd(spCenter, shRadius);
assert(XMVector3LessOrEqual(Min, Max));
XMStoreFloat3(&Out.Center, XMVectorScale(XMVectorAdd(Min, Max), 0.5f));
XMStoreFloat3(&Out.Extents, XMVectorScale(XMVectorSubtract(Max, Min), 0.5f));
}
_Use_decl_annotations_
inline void XM_CALLCONV BoundingBox::CreateFromPoints(BoundingBox& Out, FXMVECTOR pt1, FXMVECTOR pt2) noexcept
{
XMVECTOR Min = XMVectorMin(pt1, pt2);
XMVECTOR Max = XMVectorMax(pt1, pt2);
XMStoreFloat3(&Out.Center, XMVectorScale(XMVectorAdd(Min, Max), 0.5f));
XMStoreFloat3(&Out.Extents, XMVectorScale(XMVectorSubtract(Max, Min), 0.5f));
}
_Use_decl_annotations_
inline void BoundingBox::CreateFromPoints(BoundingBox& Out, size_t Count, const XMFLOAT3* pPoints, size_t Stride) noexcept
{
assert(Count > 0);
assert(pPoints);
XMVECTOR vMin, vMax;
vMin = vMax = XMLoadFloat3(pPoints);
for (size_t i = 1; i < Count; ++i)
{
XMVECTOR Point = XMLoadFloat3(reinterpret_cast<const XMFLOAT3*>(reinterpret_cast<const uint8_t*>(pPoints) + i * Stride));
vMin = XMVectorMin(vMin, Point);
vMax = XMVectorMax(vMax, Point);
}
XMStoreFloat3(&Out.Center, XMVectorScale(XMVectorAdd(vMin, vMax), 0.5f));
XMStoreFloat3(&Out.Extents, XMVectorScale(XMVectorSubtract(vMax, vMin), 0.5f));
}
_Use_decl_annotations_
inline void XM_CALLCONV BoundingOrientedBox::Transform(BoundingOrientedBox& Out, FXMMATRIX M) const noexcept
{
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vExtents = XMLoadFloat3(&Extents);
XMVECTOR vOrientation = XMLoadFloat4(&Orientation);
assert(DirectX::MathInternal::XMQuaternionIsUnit(vOrientation));
XMMATRIX nM;
nM.r[0] = XMVector3Normalize(M.r[0]);
nM.r[1] = XMVector3Normalize(M.r[1]);
nM.r[2] = XMVector3Normalize(M.r[2]);
nM.r[3] = g_XMIdentityR3;
XMVECTOR Rotation = XMQuaternionRotationMatrix(nM);
vOrientation = XMQuaternionMultiply(vOrientation, Rotation);
vCenter = XMVector3Transform(vCenter, M);
XMVECTOR dX = XMVector3Length(M.r[0]);
XMVECTOR dY = XMVector3Length(M.r[1]);
XMVECTOR dZ = XMVector3Length(M.r[2]);
XMVECTOR VectorScale = XMVectorSelect(dY, dX, g_XMSelect1000);
VectorScale = XMVectorSelect(dZ, VectorScale, g_XMSelect1100);
vExtents = XMVectorMultiply(vExtents, VectorScale);
XMStoreFloat3(&Out.Center, vCenter);
XMStoreFloat3(&Out.Extents, vExtents);
XMStoreFloat4(&Out.Orientation, vOrientation);
}
_Use_decl_annotations_
inline void XM_CALLCONV BoundingOrientedBox::Transform(BoundingOrientedBox& Out, float Scale, FXMVECTOR Rotation, FXMVECTOR Translation) const noexcept
{
assert(DirectX::MathInternal::XMQuaternionIsUnit(Rotation));
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vExtents = XMLoadFloat3(&Extents);
XMVECTOR vOrientation = XMLoadFloat4(&Orientation);
assert(DirectX::MathInternal::XMQuaternionIsUnit(vOrientation));
vOrientation = XMQuaternionMultiply(vOrientation, Rotation);
XMVECTOR VectorScale = XMVectorReplicate(Scale);
vCenter = XMVectorAdd(XMVector3Rotate(XMVectorMultiply(vCenter, VectorScale), Rotation), Translation);
vExtents = XMVectorMultiply(vExtents, VectorScale);
XMStoreFloat3(&Out.Center, vCenter);
XMStoreFloat3(&Out.Extents, vExtents);
XMStoreFloat4(&Out.Orientation, vOrientation);
}
_Use_decl_annotations_
inline void BoundingOrientedBox::GetCorners(XMFLOAT3* Corners) const noexcept
{
assert(Corners != nullptr);
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vExtents = XMLoadFloat3(&Extents);
XMVECTOR vOrientation = XMLoadFloat4(&Orientation);
assert(DirectX::MathInternal::XMQuaternionIsUnit(vOrientation));
for (size_t i = 0; i < CORNER_COUNT; ++i)
{
XMVECTOR C = XMVectorAdd(XMVector3Rotate(XMVectorMultiply(vExtents, g_BoxOffset[i]), vOrientation), vCenter);
XMStoreFloat3(&Corners[i], C);
}
}
_Use_decl_annotations_
inline ContainmentType XM_CALLCONV BoundingOrientedBox::Contains(FXMVECTOR Point) const noexcept
{
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vExtents = XMLoadFloat3(&Extents);
XMVECTOR vOrientation = XMLoadFloat4(&Orientation);
XMVECTOR TPoint = XMVector3InverseRotate(XMVectorSubtract(Point, vCenter), vOrientation);
return XMVector3InBounds(TPoint, vExtents) ? CONTAINS : DISJOINT;
}
_Use_decl_annotations_
inline ContainmentType XM_CALLCONV BoundingOrientedBox::Contains(FXMVECTOR V0, FXMVECTOR V1, FXMVECTOR V2) const noexcept
{
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vOrientation = XMLoadFloat4(&Orientation);
XMVECTOR TV0 = XMVector3InverseRotate(XMVectorSubtract(V0, vCenter), vOrientation);
XMVECTOR TV1 = XMVector3InverseRotate(XMVectorSubtract(V1, vCenter), vOrientation);
XMVECTOR TV2 = XMVector3InverseRotate(XMVectorSubtract(V2, vCenter), vOrientation);
BoundingBox box;
box.Center = XMFLOAT3(0.0f, 0.0f, 0.0f);
box.Extents = Extents;
return box.Contains(TV0, TV1, TV2);
}
_Use_decl_annotations_
inline ContainmentType BoundingOrientedBox::Contains(const BoundingSphere& sh) const noexcept
{
XMVECTOR SphereCenter = XMLoadFloat3(&sh.Center);
XMVECTOR SphereRadius = XMVectorReplicatePtr(&sh.Radius);
XMVECTOR BoxCenter = XMLoadFloat3(&Center);
XMVECTOR BoxExtents = XMLoadFloat3(&Extents);
XMVECTOR BoxOrientation = XMLoadFloat4(&Orientation);
assert(DirectX::MathInternal::XMQuaternionIsUnit(BoxOrientation));
SphereCenter = XMVector3InverseRotate(XMVectorSubtract(SphereCenter, BoxCenter), BoxOrientation);
XMVECTOR d = XMVectorZero();
XMVECTOR LessThanMin = XMVectorLess(SphereCenter, XMVectorNegate(BoxExtents));
XMVECTOR GreaterThanMax = XMVectorGreater(SphereCenter, BoxExtents);
XMVECTOR MinDelta = XMVectorAdd(SphereCenter, BoxExtents);
XMVECTOR MaxDelta = XMVectorSubtract(SphereCenter, BoxExtents);
d = XMVectorSelect(d, MinDelta, LessThanMin);
d = XMVectorSelect(d, MaxDelta, GreaterThanMax);
XMVECTOR d2 = XMVector3Dot(d, d);
XMVECTOR SphereRadiusSq = XMVectorMultiply(SphereRadius, SphereRadius);
if (XMVector4Greater(d2, SphereRadiusSq))
return DISJOINT;
XMVECTOR SMin = XMVectorSubtract(SphereCenter, SphereRadius);
XMVECTOR SMax = XMVectorAdd(SphereCenter, SphereRadius);
return (XMVector3InBounds(SMin, BoxExtents) && XMVector3InBounds(SMax, BoxExtents)) ? CONTAINS : INTERSECTS;
}
_Use_decl_annotations_
inline ContainmentType BoundingOrientedBox::Contains(const BoundingBox& box) const noexcept
{
BoundingOrientedBox obox(box.Center, box.Extents, XMFLOAT4(0.f, 0.f, 0.f, 1.f));
return Contains(obox);
}
_Use_decl_annotations_
inline ContainmentType BoundingOrientedBox::Contains(const BoundingOrientedBox& box) const noexcept
{
if (!Intersects(box))
return DISJOINT;
XMVECTOR aCenter = XMLoadFloat3(&Center);
XMVECTOR aExtents = XMLoadFloat3(&Extents);
XMVECTOR aOrientation = XMLoadFloat4(&Orientation);
assert(DirectX::MathInternal::XMQuaternionIsUnit(aOrientation));
XMVECTOR bCenter = XMLoadFloat3(&box.Center);
XMVECTOR bExtents = XMLoadFloat3(&box.Extents);
XMVECTOR bOrientation = XMLoadFloat4(&box.Orientation);
assert(DirectX::MathInternal::XMQuaternionIsUnit(bOrientation));
XMVECTOR offset = XMVectorSubtract(bCenter, aCenter);
for (size_t i = 0; i < CORNER_COUNT; ++i)
{
XMVECTOR C = XMVectorAdd(XMVector3Rotate(XMVectorMultiply(bExtents, g_BoxOffset[i]), bOrientation), offset);
C = XMVector3InverseRotate(C, aOrientation);
if (!XMVector3InBounds(C, aExtents))
return INTERSECTS;
}
return CONTAINS;
}
_Use_decl_annotations_
inline ContainmentType BoundingOrientedBox::Contains(const BoundingFrustum& fr) const noexcept
{
if (!fr.Intersects(*this))
return DISJOINT;
XMFLOAT3 Corners[BoundingFrustum::CORNER_COUNT];
fr.GetCorners(Corners);
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vExtents = XMLoadFloat3(&Extents);
XMVECTOR vOrientation = XMLoadFloat4(&Orientation);
assert(DirectX::MathInternal::XMQuaternionIsUnit(vOrientation));
for (size_t i = 0; i < BoundingFrustum::CORNER_COUNT; ++i)
{
XMVECTOR C = XMVector3InverseRotate(XMVectorSubtract(XMLoadFloat3(&Corners[i]), vCenter), vOrientation);
if (!XMVector3InBounds(C, vExtents))
return INTERSECTS;
}
return CONTAINS;
}
_Use_decl_annotations_
inline bool BoundingOrientedBox::Intersects(const BoundingSphere& sh) const noexcept
{
XMVECTOR SphereCenter = XMLoadFloat3(&sh.Center);
XMVECTOR SphereRadius = XMVectorReplicatePtr(&sh.Radius);
XMVECTOR BoxCenter = XMLoadFloat3(&Center);
XMVECTOR BoxExtents = XMLoadFloat3(&Extents);
XMVECTOR BoxOrientation = XMLoadFloat4(&Orientation);
assert(DirectX::MathInternal::XMQuaternionIsUnit(BoxOrientation));
SphereCenter = XMVector3InverseRotate(XMVectorSubtract(SphereCenter, BoxCenter), BoxOrientation);
XMVECTOR d = XMVectorZero();
XMVECTOR LessThanMin = XMVectorLess(SphereCenter, XMVectorNegate(BoxExtents));
XMVECTOR GreaterThanMax = XMVectorGreater(SphereCenter, BoxExtents);
XMVECTOR MinDelta = XMVectorAdd(SphereCenter, BoxExtents);
XMVECTOR MaxDelta = XMVectorSubtract(SphereCenter, BoxExtents);
d = XMVectorSelect(d, MinDelta, LessThanMin);
d = XMVectorSelect(d, MaxDelta, GreaterThanMax);
XMVECTOR d2 = XMVector3Dot(d, d);
return XMVector4LessOrEqual(d2, XMVectorMultiply(SphereRadius, SphereRadius)) ? true : false;
}
_Use_decl_annotations_
inline bool BoundingOrientedBox::Intersects(const BoundingBox& box) const noexcept
{
BoundingOrientedBox obox(box.Center, box.Extents, XMFLOAT4(0.f, 0.f, 0.f, 1.f));
return Intersects(obox);
}
_Use_decl_annotations_
inline bool BoundingOrientedBox::Intersects(const BoundingOrientedBox& box) const noexcept
{
XMVECTOR A_quat = XMLoadFloat4(&Orientation);
XMVECTOR B_quat = XMLoadFloat4(&box.Orientation);
assert(DirectX::MathInternal::XMQuaternionIsUnit(A_quat));
assert(DirectX::MathInternal::XMQuaternionIsUnit(B_quat));
XMVECTOR Q = XMQuaternionMultiply(A_quat, XMQuaternionConjugate(B_quat));
XMMATRIX R = XMMatrixRotationQuaternion(Q);
XMVECTOR A_cent = XMLoadFloat3(&Center);
XMVECTOR B_cent = XMLoadFloat3(&box.Center);
XMVECTOR t = XMVector3InverseRotate(XMVectorSubtract(B_cent, A_cent), A_quat);
XMVECTOR h_A = XMLoadFloat3(&Extents);
XMVECTOR h_B = XMLoadFloat3(&box.Extents);
XMVECTOR R0X = R.r[0];
XMVECTOR R1X = R.r[1];
XMVECTOR R2X = R.r[2];
R = XMMatrixTranspose(R);
XMVECTOR RX0 = R.r[0];
XMVECTOR RX1 = R.r[1];
XMVECTOR RX2 = R.r[2];
XMVECTOR AR0X = XMVectorAbs(R0X);
XMVECTOR AR1X = XMVectorAbs(R1X);
XMVECTOR AR2X = XMVectorAbs(R2X);
XMVECTOR ARX0 = XMVectorAbs(RX0);
XMVECTOR ARX1 = XMVectorAbs(RX1);
XMVECTOR ARX2 = XMVectorAbs(RX2);
XMVECTOR d, d_A, d_B;
d = XMVectorSplatX(t);
d_A = XMVectorSplatX(h_A);
d_B = XMVector3Dot(h_B, AR0X);
XMVECTOR NoIntersection = XMVectorGreater(XMVectorAbs(d), XMVectorAdd(d_A, d_B));
d = XMVectorSplatY(t);
d_A = XMVectorSplatY(h_A);
d_B = XMVector3Dot(h_B, AR1X);
NoIntersection = XMVectorOrInt(NoIntersection,
XMVectorGreater(XMVectorAbs(d), XMVectorAdd(d_A, d_B)));
d = XMVectorSplatZ(t);
d_A = XMVectorSplatZ(h_A);
d_B = XMVector3Dot(h_B, AR2X);
NoIntersection = XMVectorOrInt(NoIntersection,
XMVectorGreater(XMVectorAbs(d), XMVectorAdd(d_A, d_B)));
d = XMVector3Dot(t, RX0);
d_A = XMVector3Dot(h_A, ARX0);
d_B = XMVectorSplatX(h_B);
NoIntersection = XMVectorOrInt(NoIntersection,
XMVectorGreater(XMVectorAbs(d), XMVectorAdd(d_A, d_B)));
d = XMVector3Dot(t, RX1);
d_A = XMVector3Dot(h_A, ARX1);
d_B = XMVectorSplatY(h_B);
NoIntersection = XMVectorOrInt(NoIntersection,
XMVectorGreater(XMVectorAbs(d), XMVectorAdd(d_A, d_B)));
d = XMVector3Dot(t, RX2);
d_A = XMVector3Dot(h_A, ARX2);
d_B = XMVectorSplatZ(h_B);
NoIntersection = XMVectorOrInt(NoIntersection,
XMVectorGreater(XMVectorAbs(d), XMVectorAdd(d_A, d_B)));
d = XMVector3Dot(t, XMVectorPermute<XM_PERMUTE_0W, XM_PERMUTE_1Z, XM_PERMUTE_0Y, XM_PERMUTE_0X>(RX0, XMVectorNegate(RX0)));
d_A = XMVector3Dot(h_A, XMVectorSwizzle<XM_SWIZZLE_W, XM_SWIZZLE_Z, XM_SWIZZLE_Y, XM_SWIZZLE_X>(ARX0));
d_B = XMVector3Dot(h_B, XMVectorSwizzle<XM_SWIZZLE_W, XM_SWIZZLE_Z, XM_SWIZZLE_Y, XM_SWIZZLE_X>(AR0X));
NoIntersection = XMVectorOrInt(NoIntersection,
XMVectorGreater(XMVectorAbs(d), XMVectorAdd(d_A, d_B)));
d = XMVector3Dot(t, XMVectorPermute<XM_PERMUTE_0W, XM_PERMUTE_1Z, XM_PERMUTE_0Y, XM_PERMUTE_0X>(RX1, XMVectorNegate(RX1)));
d_A = XMVector3Dot(h_A, XMVectorSwizzle<XM_SWIZZLE_W, XM_SWIZZLE_Z, XM_SWIZZLE_Y, XM_SWIZZLE_X>(ARX1));
d_B = XMVector3Dot(h_B, XMVectorSwizzle<XM_SWIZZLE_Z, XM_SWIZZLE_W, XM_SWIZZLE_X, XM_SWIZZLE_Y>(AR0X));
NoIntersection = XMVectorOrInt(NoIntersection,
XMVectorGreater(XMVectorAbs(d), XMVectorAdd(d_A, d_B)));
d = XMVector3Dot(t, XMVectorPermute<XM_PERMUTE_0W, XM_PERMUTE_1Z, XM_PERMUTE_0Y, XM_PERMUTE_0X>(RX2, XMVectorNegate(RX2)));
d_A = XMVector3Dot(h_A, XMVectorSwizzle<XM_SWIZZLE_W, XM_SWIZZLE_Z, XM_SWIZZLE_Y, XM_SWIZZLE_X>(ARX2));
d_B = XMVector3Dot(h_B, XMVectorSwizzle<XM_SWIZZLE_Y, XM_SWIZZLE_X, XM_SWIZZLE_W, XM_SWIZZLE_Z>(AR0X));
NoIntersection = XMVectorOrInt(NoIntersection,
XMVectorGreater(XMVectorAbs(d), XMVectorAdd(d_A, d_B)));
d = XMVector3Dot(t, XMVectorPermute<XM_PERMUTE_0Z, XM_PERMUTE_0W, XM_PERMUTE_1X, XM_PERMUTE_0Y>(RX0, XMVectorNegate(RX0)));
d_A = XMVector3Dot(h_A, XMVectorSwizzle<XM_SWIZZLE_Z, XM_SWIZZLE_W, XM_SWIZZLE_X, XM_SWIZZLE_Y>(ARX0));
d_B = XMVector3Dot(h_B, XMVectorSwizzle<XM_SWIZZLE_W, XM_SWIZZLE_Z, XM_SWIZZLE_Y, XM_SWIZZLE_X>(AR1X));
NoIntersection = XMVectorOrInt(NoIntersection,
XMVectorGreater(XMVectorAbs(d), XMVectorAdd(d_A, d_B)));
d = XMVector3Dot(t, XMVectorPermute<XM_PERMUTE_0Z, XM_PERMUTE_0W, XM_PERMUTE_1X, XM_PERMUTE_0Y>(RX1, XMVectorNegate(RX1)));
d_A = XMVector3Dot(h_A, XMVectorSwizzle<XM_SWIZZLE_Z, XM_SWIZZLE_W, XM_SWIZZLE_X, XM_SWIZZLE_Y>(ARX1));
d_B = XMVector3Dot(h_B, XMVectorSwizzle<XM_SWIZZLE_Z, XM_SWIZZLE_W, XM_SWIZZLE_X, XM_SWIZZLE_Y>(AR1X));
NoIntersection = XMVectorOrInt(NoIntersection,
XMVectorGreater(XMVectorAbs(d), XMVectorAdd(d_A, d_B)));
d = XMVector3Dot(t, XMVectorPermute<XM_PERMUTE_0Z, XM_PERMUTE_0W, XM_PERMUTE_1X, XM_PERMUTE_0Y>(RX2, XMVectorNegate(RX2)));
d_A = XMVector3Dot(h_A, XMVectorSwizzle<XM_SWIZZLE_Z, XM_SWIZZLE_W, XM_SWIZZLE_X, XM_SWIZZLE_Y>(ARX2));
d_B = XMVector3Dot(h_B, XMVectorSwizzle<XM_SWIZZLE_Y, XM_SWIZZLE_X, XM_SWIZZLE_W, XM_SWIZZLE_Z>(AR1X));
NoIntersection = XMVectorOrInt(NoIntersection,
XMVectorGreater(XMVectorAbs(d), XMVectorAdd(d_A, d_B)));
d = XMVector3Dot(t, XMVectorPermute<XM_PERMUTE_1Y, XM_PERMUTE_0X, XM_PERMUTE_0W, XM_PERMUTE_0Z>(RX0, XMVectorNegate(RX0)));
d_A = XMVector3Dot(h_A, XMVectorSwizzle<XM_SWIZZLE_Y, XM_SWIZZLE_X, XM_SWIZZLE_W, XM_SWIZZLE_Z>(ARX0));
d_B = XMVector3Dot(h_B, XMVectorSwizzle<XM_SWIZZLE_W, XM_SWIZZLE_Z, XM_SWIZZLE_Y, XM_SWIZZLE_X>(AR2X));
NoIntersection = XMVectorOrInt(NoIntersection,
XMVectorGreater(XMVectorAbs(d), XMVectorAdd(d_A, d_B)));
d = XMVector3Dot(t, XMVectorPermute<XM_PERMUTE_1Y, XM_PERMUTE_0X, XM_PERMUTE_0W, XM_PERMUTE_0Z>(RX1, XMVectorNegate(RX1)));
d_A = XMVector3Dot(h_A, XMVectorSwizzle<XM_SWIZZLE_Y, XM_SWIZZLE_X, XM_SWIZZLE_W, XM_SWIZZLE_Z>(ARX1));
d_B = XMVector3Dot(h_B, XMVectorSwizzle<XM_SWIZZLE_Z, XM_SWIZZLE_W, XM_SWIZZLE_X, XM_SWIZZLE_Y>(AR2X));
NoIntersection = XMVectorOrInt(NoIntersection,
XMVectorGreater(XMVectorAbs(d), XMVectorAdd(d_A, d_B)));
d = XMVector3Dot(t, XMVectorPermute<XM_PERMUTE_1Y, XM_PERMUTE_0X, XM_PERMUTE_0W, XM_PERMUTE_0Z>(RX2, XMVectorNegate(RX2)));
d_A = XMVector3Dot(h_A, XMVectorSwizzle<XM_SWIZZLE_Y, XM_SWIZZLE_X, XM_SWIZZLE_W, XM_SWIZZLE_Z>(ARX2));
d_B = XMVector3Dot(h_B, XMVectorSwizzle<XM_SWIZZLE_Y, XM_SWIZZLE_X, XM_SWIZZLE_W, XM_SWIZZLE_Z>(AR2X));
NoIntersection = XMVectorOrInt(NoIntersection,
XMVectorGreater(XMVectorAbs(d), XMVectorAdd(d_A, d_B)));
return XMVector4NotEqualInt(NoIntersection, XMVectorTrueInt()) ? true : false;
}
_Use_decl_annotations_
inline bool BoundingOrientedBox::Intersects(const BoundingFrustum& fr) const noexcept
{
return fr.Intersects(*this);
}
_Use_decl_annotations_
inline bool XM_CALLCONV BoundingOrientedBox::Intersects(FXMVECTOR V0, FXMVECTOR V1, FXMVECTOR V2) const noexcept
{
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vOrientation = XMLoadFloat4(&Orientation);
XMVECTOR TV0 = XMVector3InverseRotate(XMVectorSubtract(V0, vCenter), vOrientation);
XMVECTOR TV1 = XMVector3InverseRotate(XMVectorSubtract(V1, vCenter), vOrientation);
XMVECTOR TV2 = XMVector3InverseRotate(XMVectorSubtract(V2, vCenter), vOrientation);
BoundingBox box;
box.Center = XMFLOAT3(0.0f, 0.0f, 0.0f);
box.Extents = Extents;
return box.Intersects(TV0, TV1, TV2);
}
_Use_decl_annotations_
inline PlaneIntersectionType XM_CALLCONV BoundingOrientedBox::Intersects(FXMVECTOR Plane) const noexcept
{
assert(DirectX::MathInternal::XMPlaneIsUnit(Plane));
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vExtents = XMLoadFloat3(&Extents);
XMVECTOR BoxOrientation = XMLoadFloat4(&Orientation);
assert(DirectX::MathInternal::XMQuaternionIsUnit(BoxOrientation));
vCenter = XMVectorInsert<0, 0, 0, 0, 1>(vCenter, XMVectorSplatOne());
XMMATRIX R = XMMatrixRotationQuaternion(BoxOrientation);
XMVECTOR Outside, Inside;
DirectX::MathInternal::FastIntersectOrientedBoxPlane(vCenter, vExtents, R.r[0], R.r[1], R.r[2], Plane, Outside, Inside);
if (XMVector4EqualInt(Outside, XMVectorTrueInt()))
return FRONT;
if (XMVector4EqualInt(Inside, XMVectorTrueInt()))
return BACK;
return INTERSECTING;
}
_Use_decl_annotations_
inline bool XM_CALLCONV BoundingOrientedBox::Intersects(FXMVECTOR Origin, FXMVECTOR Direction, float& Dist) const noexcept
{
assert(DirectX::MathInternal::XMVector3IsUnit(Direction));
static const XMVECTORU32 SelectY = { { { XM_SELECT_0, XM_SELECT_1, XM_SELECT_0, XM_SELECT_0 } } };
static const XMVECTORU32 SelectZ = { { { XM_SELECT_0, XM_SELECT_0, XM_SELECT_1, XM_SELECT_0 } } };
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vExtents = XMLoadFloat3(&Extents);
XMVECTOR vOrientation = XMLoadFloat4(&Orientation);
assert(DirectX::MathInternal::XMQuaternionIsUnit(vOrientation));
XMMATRIX R = XMMatrixRotationQuaternion(vOrientation);
XMVECTOR TOrigin = XMVectorSubtract(vCenter, Origin);
XMVECTOR AxisDotOrigin = XMVector3Dot(R.r[0], TOrigin);
AxisDotOrigin = XMVectorSelect(AxisDotOrigin, XMVector3Dot(R.r[1], TOrigin), SelectY);
AxisDotOrigin = XMVectorSelect(AxisDotOrigin, XMVector3Dot(R.r[2], TOrigin), SelectZ);
XMVECTOR AxisDotDirection = XMVector3Dot(R.r[0], Direction);
AxisDotDirection = XMVectorSelect(AxisDotDirection, XMVector3Dot(R.r[1], Direction), SelectY);
AxisDotDirection = XMVectorSelect(AxisDotDirection, XMVector3Dot(R.r[2], Direction), SelectZ);
XMVECTOR IsParallel = XMVectorLessOrEqual(XMVectorAbs(AxisDotDirection), g_RayEpsilon);
XMVECTOR InverseAxisDotDirection = XMVectorReciprocal(AxisDotDirection);
XMVECTOR t1 = XMVectorMultiply(XMVectorSubtract(AxisDotOrigin, vExtents), InverseAxisDotDirection);
XMVECTOR t2 = XMVectorMultiply(XMVectorAdd(AxisDotOrigin, vExtents), InverseAxisDotDirection);
XMVECTOR t_min = XMVectorSelect(XMVectorMin(t1, t2), g_FltMin, IsParallel);
XMVECTOR t_max = XMVectorSelect(XMVectorMax(t1, t2), g_FltMax, IsParallel);
t_min = XMVectorMax(t_min, XMVectorSplatY(t_min)); t_min = XMVectorMax(t_min, XMVectorSplatZ(t_min)); t_max = XMVectorMin(t_max, XMVectorSplatY(t_max)); t_max = XMVectorMin(t_max, XMVectorSplatZ(t_max));
XMVECTOR NoIntersection = XMVectorGreater(XMVectorSplatX(t_min), XMVectorSplatX(t_max));
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorLess(XMVectorSplatX(t_max), XMVectorZero()));
XMVECTOR ParallelOverlap = XMVectorInBounds(AxisDotOrigin, vExtents);
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorAndCInt(IsParallel, ParallelOverlap));
if (!DirectX::MathInternal::XMVector3AnyTrue(NoIntersection))
{
XMStoreFloat(&Dist, t_min);
return true;
}
Dist = 0.f;
return false;
}
_Use_decl_annotations_
inline ContainmentType XM_CALLCONV BoundingOrientedBox::ContainedBy(
FXMVECTOR Plane0, FXMVECTOR Plane1, FXMVECTOR Plane2,
GXMVECTOR Plane3,
HXMVECTOR Plane4, HXMVECTOR Plane5) const noexcept
{
XMVECTOR vCenter = XMLoadFloat3(&Center);
XMVECTOR vExtents = XMLoadFloat3(&Extents);
XMVECTOR BoxOrientation = XMLoadFloat4(&Orientation);
assert(DirectX::MathInternal::XMQuaternionIsUnit(BoxOrientation));
vCenter = XMVectorInsert<0, 0, 0, 0, 1>(vCenter, XMVectorSplatOne());
XMMATRIX R = XMMatrixRotationQuaternion(BoxOrientation);
XMVECTOR Outside, Inside;
DirectX::MathInternal::FastIntersectOrientedBoxPlane(vCenter, vExtents, R.r[0], R.r[1], R.r[2], Plane0, Outside, Inside);
XMVECTOR AnyOutside = Outside;
XMVECTOR AllInside = Inside;
DirectX::MathInternal::FastIntersectOrientedBoxPlane(vCenter, vExtents, R.r[0], R.r[1], R.r[2], Plane1, Outside, Inside);
AnyOutside = XMVectorOrInt(AnyOutside, Outside);
AllInside = XMVectorAndInt(AllInside, Inside);
DirectX::MathInternal::FastIntersectOrientedBoxPlane(vCenter, vExtents, R.r[0], R.r[1], R.r[2], Plane2, Outside, Inside);
AnyOutside = XMVectorOrInt(AnyOutside, Outside);
AllInside = XMVectorAndInt(AllInside, Inside);
DirectX::MathInternal::FastIntersectOrientedBoxPlane(vCenter, vExtents, R.r[0], R.r[1], R.r[2], Plane3, Outside, Inside);
AnyOutside = XMVectorOrInt(AnyOutside, Outside);
AllInside = XMVectorAndInt(AllInside, Inside);
DirectX::MathInternal::FastIntersectOrientedBoxPlane(vCenter, vExtents, R.r[0], R.r[1], R.r[2], Plane4, Outside, Inside);
AnyOutside = XMVectorOrInt(AnyOutside, Outside);
AllInside = XMVectorAndInt(AllInside, Inside);
DirectX::MathInternal::FastIntersectOrientedBoxPlane(vCenter, vExtents, R.r[0], R.r[1], R.r[2], Plane5, Outside, Inside);
AnyOutside = XMVectorOrInt(AnyOutside, Outside);
AllInside = XMVectorAndInt(AllInside, Inside);
if (XMVector4EqualInt(AnyOutside, XMVectorTrueInt()))
return DISJOINT;
if (XMVector4EqualInt(AllInside, XMVectorTrueInt()))
return CONTAINS;
return INTERSECTS;
}
_Use_decl_annotations_
inline void BoundingOrientedBox::CreateFromBoundingBox(BoundingOrientedBox& Out, const BoundingBox& box) noexcept
{
Out.Center = box.Center;
Out.Extents = box.Extents;
Out.Orientation = XMFLOAT4(0.f, 0.f, 0.f, 1.f);
}
_Use_decl_annotations_
inline void BoundingOrientedBox::CreateFromPoints(BoundingOrientedBox& Out, size_t Count, const XMFLOAT3* pPoints, size_t Stride) noexcept
{
assert(Count > 0);
assert(pPoints != nullptr);
XMVECTOR CenterOfMass = XMVectorZero();
for (size_t i = 0; i < Count; ++i)
{
XMVECTOR Point = XMLoadFloat3(reinterpret_cast<const XMFLOAT3*>(reinterpret_cast<const uint8_t*>(pPoints) + i * Stride));
CenterOfMass = XMVectorAdd(CenterOfMass, Point);
}
CenterOfMass = XMVectorMultiply(CenterOfMass, XMVectorReciprocal(XMVectorReplicate(float(Count))));
XMVECTOR XX_YY_ZZ = XMVectorZero();
XMVECTOR XY_XZ_YZ = XMVectorZero();
for (size_t i = 0; i < Count; ++i)
{
XMVECTOR Point = XMVectorSubtract(XMLoadFloat3(reinterpret_cast<const XMFLOAT3*>(reinterpret_cast<const uint8_t*>(pPoints) + i * Stride)), CenterOfMass);
XX_YY_ZZ = XMVectorAdd(XX_YY_ZZ, XMVectorMultiply(Point, Point));
XMVECTOR XXY = XMVectorSwizzle<XM_SWIZZLE_X, XM_SWIZZLE_X, XM_SWIZZLE_Y, XM_SWIZZLE_W>(Point);
XMVECTOR YZZ = XMVectorSwizzle<XM_SWIZZLE_Y, XM_SWIZZLE_Z, XM_SWIZZLE_Z, XM_SWIZZLE_W>(Point);
XY_XZ_YZ = XMVectorAdd(XY_XZ_YZ, XMVectorMultiply(XXY, YZZ));
}
XMVECTOR v1, v2, v3;
DirectX::MathInternal::CalculateEigenVectorsFromCovarianceMatrix(XMVectorGetX(XX_YY_ZZ), XMVectorGetY(XX_YY_ZZ),
XMVectorGetZ(XX_YY_ZZ),
XMVectorGetX(XY_XZ_YZ), XMVectorGetY(XY_XZ_YZ),
XMVectorGetZ(XY_XZ_YZ),
&v1, &v2, &v3);
XMMATRIX R;
R.r[0] = XMVectorSetW(v1, 0.f);
R.r[1] = XMVectorSetW(v2, 0.f);
R.r[2] = XMVectorSetW(v3, 0.f);
R.r[3] = g_XMIdentityR3.v;
XMVECTOR Det = XMMatrixDeterminant(R);
if (XMVector4Less(Det, XMVectorZero()))
{
R.r[0] = XMVectorMultiply(R.r[0], g_XMNegativeOne.v);
R.r[1] = XMVectorMultiply(R.r[1], g_XMNegativeOne.v);
R.r[2] = XMVectorMultiply(R.r[2], g_XMNegativeOne.v);
}
XMVECTOR vOrientation = XMQuaternionRotationMatrix(R);
vOrientation = XMQuaternionNormalize(vOrientation);
R = XMMatrixRotationQuaternion(vOrientation);
XMMATRIX InverseR = XMMatrixTranspose(R);
XMVECTOR vMin, vMax;
vMin = vMax = XMVector3TransformNormal(XMLoadFloat3(pPoints), InverseR);
for (size_t i = 1; i < Count; ++i)
{
XMVECTOR Point = XMVector3TransformNormal(XMLoadFloat3(reinterpret_cast<const XMFLOAT3*>(reinterpret_cast<const uint8_t*>(pPoints) + i * Stride)),
InverseR);
vMin = XMVectorMin(vMin, Point);
vMax = XMVectorMax(vMax, Point);
}
XMVECTOR vCenter = XMVectorScale(XMVectorAdd(vMin, vMax), 0.5f);
vCenter = XMVector3TransformNormal(vCenter, R);
XMStoreFloat3(&Out.Center, vCenter);
XMStoreFloat3(&Out.Extents, XMVectorScale(XMVectorSubtract(vMax, vMin), 0.5f));
XMStoreFloat4(&Out.Orientation, vOrientation);
}
_Use_decl_annotations_
inline BoundingFrustum::BoundingFrustum(CXMMATRIX Projection, bool rhcoords) noexcept
{
CreateFromMatrix(*this, Projection, rhcoords);
}
_Use_decl_annotations_
inline void XM_CALLCONV BoundingFrustum::Transform(BoundingFrustum& Out, FXMMATRIX M) const noexcept
{
XMVECTOR vOrigin = XMLoadFloat3(&Origin);
XMVECTOR vOrientation = XMLoadFloat4(&Orientation);
assert(DirectX::MathInternal::XMQuaternionIsUnit(vOrientation));
XMMATRIX nM;
nM.r[0] = XMVector3Normalize(M.r[0]);
nM.r[1] = XMVector3Normalize(M.r[1]);
nM.r[2] = XMVector3Normalize(M.r[2]);
nM.r[3] = g_XMIdentityR3;
XMVECTOR Rotation = XMQuaternionRotationMatrix(nM);
vOrientation = XMQuaternionMultiply(vOrientation, Rotation);
vOrigin = XMVector3Transform(vOrigin, M);
XMStoreFloat3(&Out.Origin, vOrigin);
XMStoreFloat4(&Out.Orientation, vOrientation);
XMVECTOR dX = XMVector3Dot(M.r[0], M.r[0]);
XMVECTOR dY = XMVector3Dot(M.r[1], M.r[1]);
XMVECTOR dZ = XMVector3Dot(M.r[2], M.r[2]);
XMVECTOR d = XMVectorMax(dX, XMVectorMax(dY, dZ));
float Scale = sqrtf(XMVectorGetX(d));
Out.Near = Near * Scale;
Out.Far = Far * Scale;
Out.RightSlope = RightSlope;
Out.LeftSlope = LeftSlope;
Out.TopSlope = TopSlope;
Out.BottomSlope = BottomSlope;
}
_Use_decl_annotations_
inline void XM_CALLCONV BoundingFrustum::Transform(BoundingFrustum& Out, float Scale, FXMVECTOR Rotation, FXMVECTOR Translation) const noexcept
{
assert(DirectX::MathInternal::XMQuaternionIsUnit(Rotation));
XMVECTOR vOrigin = XMLoadFloat3(&Origin);
XMVECTOR vOrientation = XMLoadFloat4(&Orientation);
assert(DirectX::MathInternal::XMQuaternionIsUnit(vOrientation));
vOrientation = XMQuaternionMultiply(vOrientation, Rotation);
vOrigin = XMVectorAdd(XMVector3Rotate(XMVectorScale(vOrigin, Scale), Rotation), Translation);
XMStoreFloat3(&Out.Origin, vOrigin);
XMStoreFloat4(&Out.Orientation, vOrientation);
Out.Near = Near * Scale;
Out.Far = Far * Scale;
Out.RightSlope = RightSlope;
Out.LeftSlope = LeftSlope;
Out.TopSlope = TopSlope;
Out.BottomSlope = BottomSlope;
}
_Use_decl_annotations_
inline void BoundingFrustum::GetCorners(XMFLOAT3* Corners) const noexcept
{
assert(Corners != nullptr);
XMVECTOR vOrigin = XMLoadFloat3(&Origin);
XMVECTOR vOrientation = XMLoadFloat4(&Orientation);
assert(DirectX::MathInternal::XMQuaternionIsUnit(vOrientation));
XMVECTOR vRightTop = XMVectorSet(RightSlope, TopSlope, 1.0f, 0.0f);
XMVECTOR vRightBottom = XMVectorSet(RightSlope, BottomSlope, 1.0f, 0.0f);
XMVECTOR vLeftTop = XMVectorSet(LeftSlope, TopSlope, 1.0f, 0.0f);
XMVECTOR vLeftBottom = XMVectorSet(LeftSlope, BottomSlope, 1.0f, 0.0f);
XMVECTOR vNear = XMVectorReplicatePtr(&Near);
XMVECTOR vFar = XMVectorReplicatePtr(&Far);
XMVECTOR vCorners[CORNER_COUNT];
vCorners[0] = XMVectorMultiply(vLeftTop, vNear);
vCorners[1] = XMVectorMultiply(vRightTop, vNear);
vCorners[2] = XMVectorMultiply(vRightBottom, vNear);
vCorners[3] = XMVectorMultiply(vLeftBottom, vNear);
vCorners[4] = XMVectorMultiply(vLeftTop, vFar);
vCorners[5] = XMVectorMultiply(vRightTop, vFar);
vCorners[6] = XMVectorMultiply(vRightBottom, vFar);
vCorners[7] = XMVectorMultiply(vLeftBottom, vFar);
for (size_t i = 0; i < CORNER_COUNT; ++i)
{
XMVECTOR C = XMVectorAdd(XMVector3Rotate(vCorners[i], vOrientation), vOrigin);
XMStoreFloat3(&Corners[i], C);
}
}
_Use_decl_annotations_
inline ContainmentType XM_CALLCONV BoundingFrustum::Contains(FXMVECTOR Point) const noexcept
{
XMVECTOR Planes[6];
Planes[0] = XMVectorSet(0.0f, 0.0f, -1.0f, Near);
Planes[1] = XMVectorSet(0.0f, 0.0f, 1.0f, -Far);
Planes[2] = XMVectorSet(1.0f, 0.0f, -RightSlope, 0.0f);
Planes[3] = XMVectorSet(-1.0f, 0.0f, LeftSlope, 0.0f);
Planes[4] = XMVectorSet(0.0f, 1.0f, -TopSlope, 0.0f);
Planes[5] = XMVectorSet(0.0f, -1.0f, BottomSlope, 0.0f);
XMVECTOR vOrigin = XMLoadFloat3(&Origin);
XMVECTOR vOrientation = XMLoadFloat4(&Orientation);
assert(DirectX::MathInternal::XMQuaternionIsUnit(vOrientation));
XMVECTOR TPoint = XMVector3InverseRotate(XMVectorSubtract(Point, vOrigin), vOrientation);
TPoint = XMVectorInsert<0, 0, 0, 0, 1>(TPoint, XMVectorSplatOne());
XMVECTOR Zero = XMVectorZero();
XMVECTOR Outside = Zero;
for (size_t i = 0; i < 6; ++i)
{
XMVECTOR Dot = XMVector4Dot(TPoint, Planes[i]);
Outside = XMVectorOrInt(Outside, XMVectorGreater(Dot, Zero));
}
return XMVector4NotEqualInt(Outside, XMVectorTrueInt()) ? CONTAINS : DISJOINT;
}
_Use_decl_annotations_
inline ContainmentType XM_CALLCONV BoundingFrustum::Contains(FXMVECTOR V0, FXMVECTOR V1, FXMVECTOR V2) const noexcept
{
XMVECTOR vOrigin = XMLoadFloat3(&Origin);
XMVECTOR vOrientation = XMLoadFloat4(&Orientation);
XMVECTOR NearPlane = XMVectorSet(0.0f, 0.0f, -1.0f, Near);
NearPlane = DirectX::MathInternal::XMPlaneTransform(NearPlane, vOrientation, vOrigin);
NearPlane = XMPlaneNormalize(NearPlane);
XMVECTOR FarPlane = XMVectorSet(0.0f, 0.0f, 1.0f, -Far);
FarPlane = DirectX::MathInternal::XMPlaneTransform(FarPlane, vOrientation, vOrigin);
FarPlane = XMPlaneNormalize(FarPlane);
XMVECTOR RightPlane = XMVectorSet(1.0f, 0.0f, -RightSlope, 0.0f);
RightPlane = DirectX::MathInternal::XMPlaneTransform(RightPlane, vOrientation, vOrigin);
RightPlane = XMPlaneNormalize(RightPlane);
XMVECTOR LeftPlane = XMVectorSet(-1.0f, 0.0f, LeftSlope, 0.0f);
LeftPlane = DirectX::MathInternal::XMPlaneTransform(LeftPlane, vOrientation, vOrigin);
LeftPlane = XMPlaneNormalize(LeftPlane);
XMVECTOR TopPlane = XMVectorSet(0.0f, 1.0f, -TopSlope, 0.0f);
TopPlane = DirectX::MathInternal::XMPlaneTransform(TopPlane, vOrientation, vOrigin);
TopPlane = XMPlaneNormalize(TopPlane);
XMVECTOR BottomPlane = XMVectorSet(0.0f, -1.0f, BottomSlope, 0.0f);
BottomPlane = DirectX::MathInternal::XMPlaneTransform(BottomPlane, vOrientation, vOrigin);
BottomPlane = XMPlaneNormalize(BottomPlane);
return TriangleTests::ContainedBy(V0, V1, V2, NearPlane, FarPlane, RightPlane, LeftPlane, TopPlane, BottomPlane);
}
_Use_decl_annotations_
inline ContainmentType BoundingFrustum::Contains(const BoundingSphere& sh) const noexcept
{
XMVECTOR vOrigin = XMLoadFloat3(&Origin);
XMVECTOR vOrientation = XMLoadFloat4(&Orientation);
XMVECTOR NearPlane = XMVectorSet(0.0f, 0.0f, -1.0f, Near);
NearPlane = DirectX::MathInternal::XMPlaneTransform(NearPlane, vOrientation, vOrigin);
NearPlane = XMPlaneNormalize(NearPlane);
XMVECTOR FarPlane = XMVectorSet(0.0f, 0.0f, 1.0f, -Far);
FarPlane = DirectX::MathInternal::XMPlaneTransform(FarPlane, vOrientation, vOrigin);
FarPlane = XMPlaneNormalize(FarPlane);
XMVECTOR RightPlane = XMVectorSet(1.0f, 0.0f, -RightSlope, 0.0f);
RightPlane = DirectX::MathInternal::XMPlaneTransform(RightPlane, vOrientation, vOrigin);
RightPlane = XMPlaneNormalize(RightPlane);
XMVECTOR LeftPlane = XMVectorSet(-1.0f, 0.0f, LeftSlope, 0.0f);
LeftPlane = DirectX::MathInternal::XMPlaneTransform(LeftPlane, vOrientation, vOrigin);
LeftPlane = XMPlaneNormalize(LeftPlane);
XMVECTOR TopPlane = XMVectorSet(0.0f, 1.0f, -TopSlope, 0.0f);
TopPlane = DirectX::MathInternal::XMPlaneTransform(TopPlane, vOrientation, vOrigin);
TopPlane = XMPlaneNormalize(TopPlane);
XMVECTOR BottomPlane = XMVectorSet(0.0f, -1.0f, BottomSlope, 0.0f);
BottomPlane = DirectX::MathInternal::XMPlaneTransform(BottomPlane, vOrientation, vOrigin);
BottomPlane = XMPlaneNormalize(BottomPlane);
return sh.ContainedBy(NearPlane, FarPlane, RightPlane, LeftPlane, TopPlane, BottomPlane);
}
_Use_decl_annotations_
inline ContainmentType BoundingFrustum::Contains(const BoundingBox& box) const noexcept
{
XMVECTOR vOrigin = XMLoadFloat3(&Origin);
XMVECTOR vOrientation = XMLoadFloat4(&Orientation);
XMVECTOR NearPlane = XMVectorSet(0.0f, 0.0f, -1.0f, Near);
NearPlane = DirectX::MathInternal::XMPlaneTransform(NearPlane, vOrientation, vOrigin);
NearPlane = XMPlaneNormalize(NearPlane);
XMVECTOR FarPlane = XMVectorSet(0.0f, 0.0f, 1.0f, -Far);
FarPlane = DirectX::MathInternal::XMPlaneTransform(FarPlane, vOrientation, vOrigin);
FarPlane = XMPlaneNormalize(FarPlane);
XMVECTOR RightPlane = XMVectorSet(1.0f, 0.0f, -RightSlope, 0.0f);
RightPlane = DirectX::MathInternal::XMPlaneTransform(RightPlane, vOrientation, vOrigin);
RightPlane = XMPlaneNormalize(RightPlane);
XMVECTOR LeftPlane = XMVectorSet(-1.0f, 0.0f, LeftSlope, 0.0f);
LeftPlane = DirectX::MathInternal::XMPlaneTransform(LeftPlane, vOrientation, vOrigin);
LeftPlane = XMPlaneNormalize(LeftPlane);
XMVECTOR TopPlane = XMVectorSet(0.0f, 1.0f, -TopSlope, 0.0f);
TopPlane = DirectX::MathInternal::XMPlaneTransform(TopPlane, vOrientation, vOrigin);
TopPlane = XMPlaneNormalize(TopPlane);
XMVECTOR BottomPlane = XMVectorSet(0.0f, -1.0f, BottomSlope, 0.0f);
BottomPlane = DirectX::MathInternal::XMPlaneTransform(BottomPlane, vOrientation, vOrigin);
BottomPlane = XMPlaneNormalize(BottomPlane);
return box.ContainedBy(NearPlane, FarPlane, RightPlane, LeftPlane, TopPlane, BottomPlane);
}
_Use_decl_annotations_
inline ContainmentType BoundingFrustum::Contains(const BoundingOrientedBox& box) const noexcept
{
XMVECTOR vOrigin = XMLoadFloat3(&Origin);
XMVECTOR vOrientation = XMLoadFloat4(&Orientation);
XMVECTOR NearPlane = XMVectorSet(0.0f, 0.0f, -1.0f, Near);
NearPlane = DirectX::MathInternal::XMPlaneTransform(NearPlane, vOrientation, vOrigin);
NearPlane = XMPlaneNormalize(NearPlane);
XMVECTOR FarPlane = XMVectorSet(0.0f, 0.0f, 1.0f, -Far);
FarPlane = DirectX::MathInternal::XMPlaneTransform(FarPlane, vOrientation, vOrigin);
FarPlane = XMPlaneNormalize(FarPlane);
XMVECTOR RightPlane = XMVectorSet(1.0f, 0.0f, -RightSlope, 0.0f);
RightPlane = DirectX::MathInternal::XMPlaneTransform(RightPlane, vOrientation, vOrigin);
RightPlane = XMPlaneNormalize(RightPlane);
XMVECTOR LeftPlane = XMVectorSet(-1.0f, 0.0f, LeftSlope, 0.0f);
LeftPlane = DirectX::MathInternal::XMPlaneTransform(LeftPlane, vOrientation, vOrigin);
LeftPlane = XMPlaneNormalize(LeftPlane);
XMVECTOR TopPlane = XMVectorSet(0.0f, 1.0f, -TopSlope, 0.0f);
TopPlane = DirectX::MathInternal::XMPlaneTransform(TopPlane, vOrientation, vOrigin);
TopPlane = XMPlaneNormalize(TopPlane);
XMVECTOR BottomPlane = XMVectorSet(0.0f, -1.0f, BottomSlope, 0.0f);
BottomPlane = DirectX::MathInternal::XMPlaneTransform(BottomPlane, vOrientation, vOrigin);
BottomPlane = XMPlaneNormalize(BottomPlane);
return box.ContainedBy(NearPlane, FarPlane, RightPlane, LeftPlane, TopPlane, BottomPlane);
}
_Use_decl_annotations_
inline ContainmentType BoundingFrustum::Contains(const BoundingFrustum& fr) const noexcept
{
XMVECTOR vOrigin = XMLoadFloat3(&Origin);
XMVECTOR vOrientation = XMLoadFloat4(&Orientation);
XMVECTOR NearPlane = XMVectorSet(0.0f, 0.0f, -1.0f, Near);
NearPlane = DirectX::MathInternal::XMPlaneTransform(NearPlane, vOrientation, vOrigin);
NearPlane = XMPlaneNormalize(NearPlane);
XMVECTOR FarPlane = XMVectorSet(0.0f, 0.0f, 1.0f, -Far);
FarPlane = DirectX::MathInternal::XMPlaneTransform(FarPlane, vOrientation, vOrigin);
FarPlane = XMPlaneNormalize(FarPlane);
XMVECTOR RightPlane = XMVectorSet(1.0f, 0.0f, -RightSlope, 0.0f);
RightPlane = DirectX::MathInternal::XMPlaneTransform(RightPlane, vOrientation, vOrigin);
RightPlane = XMPlaneNormalize(RightPlane);
XMVECTOR LeftPlane = XMVectorSet(-1.0f, 0.0f, LeftSlope, 0.0f);
LeftPlane = DirectX::MathInternal::XMPlaneTransform(LeftPlane, vOrientation, vOrigin);
LeftPlane = XMPlaneNormalize(LeftPlane);
XMVECTOR TopPlane = XMVectorSet(0.0f, 1.0f, -TopSlope, 0.0f);
TopPlane = DirectX::MathInternal::XMPlaneTransform(TopPlane, vOrientation, vOrigin);
TopPlane = XMPlaneNormalize(TopPlane);
XMVECTOR BottomPlane = XMVectorSet(0.0f, -1.0f, BottomSlope, 0.0f);
BottomPlane = DirectX::MathInternal::XMPlaneTransform(BottomPlane, vOrientation, vOrigin);
BottomPlane = XMPlaneNormalize(BottomPlane);
return fr.ContainedBy(NearPlane, FarPlane, RightPlane, LeftPlane, TopPlane, BottomPlane);
}
_Use_decl_annotations_
inline bool BoundingFrustum::Intersects(const BoundingSphere& sh) const noexcept
{
XMVECTOR Zero = XMVectorZero();
XMVECTOR Planes[6];
Planes[0] = XMVectorSet(0.0f, 0.0f, -1.0f, Near);
Planes[1] = XMVectorSet(0.0f, 0.0f, 1.0f, -Far);
Planes[2] = XMVectorSet(1.0f, 0.0f, -RightSlope, 0.0f);
Planes[3] = XMVectorSet(-1.0f, 0.0f, LeftSlope, 0.0f);
Planes[4] = XMVectorSet(0.0f, 1.0f, -TopSlope, 0.0f);
Planes[5] = XMVectorSet(0.0f, -1.0f, BottomSlope, 0.0f);
Planes[2] = XMVector3Normalize(Planes[2]);
Planes[3] = XMVector3Normalize(Planes[3]);
Planes[4] = XMVector3Normalize(Planes[4]);
Planes[5] = XMVector3Normalize(Planes[5]);
XMVECTOR vOrigin = XMLoadFloat3(&Origin);
XMVECTOR vOrientation = XMLoadFloat4(&Orientation);
assert(DirectX::MathInternal::XMQuaternionIsUnit(vOrientation));
XMVECTOR vCenter = XMLoadFloat3(&sh.Center);
XMVECTOR vRadius = XMVectorReplicatePtr(&sh.Radius);
vCenter = XMVector3InverseRotate(XMVectorSubtract(vCenter, vOrigin), vOrientation);
vCenter = XMVectorInsert<0, 0, 0, 0, 1>(vCenter, XMVectorSplatOne());
XMVECTOR Outside = XMVectorFalseInt();
XMVECTOR InsideAll = XMVectorTrueInt();
XMVECTOR CenterInsideAll = XMVectorTrueInt();
XMVECTOR Dist[6];
for (size_t i = 0; i < 6; ++i)
{
Dist[i] = XMVector4Dot(vCenter, Planes[i]);
Outside = XMVectorOrInt(Outside, XMVectorGreater(Dist[i], vRadius));
InsideAll = XMVectorAndInt(InsideAll, XMVectorLessOrEqual(Dist[i], XMVectorNegate(vRadius)));
CenterInsideAll = XMVectorAndInt(CenterInsideAll, XMVectorLessOrEqual(Dist[i], Zero));
}
if (XMVector4EqualInt(Outside, XMVectorTrueInt()))
return false;
if (XMVector4EqualInt(InsideAll, XMVectorTrueInt()))
return true;
if (XMVector4EqualInt(CenterInsideAll, XMVectorTrueInt()))
return true;
static const size_t adjacent_faces[6][4] =
{
{ 2, 3, 4, 5 }, { 2, 3, 4, 5 }, { 0, 1, 4, 5 }, { 0, 1, 4, 5 }, { 0, 1, 2, 3 }, { 0, 1, 2, 3 }
};
XMVECTOR Intersects = XMVectorFalseInt();
for (size_t i = 0; i < 6; ++i)
{
XMVECTOR Point = XMVectorNegativeMultiplySubtract(Planes[i], Dist[i], vCenter);
Point = XMVectorInsert<0, 0, 0, 0, 1>(Point, XMVectorSplatOne());
XMVECTOR InsideFace = XMVectorTrueInt();
for (size_t j = 0; j < 4; j++)
{
size_t plane_index = adjacent_faces[i][j];
InsideFace = XMVectorAndInt(InsideFace,
XMVectorLessOrEqual(XMVector4Dot(Point, Planes[plane_index]), Zero));
}
Intersects = XMVectorOrInt(Intersects,
XMVectorAndInt(XMVectorGreater(Dist[i], Zero), InsideFace));
}
if (XMVector4EqualInt(Intersects, XMVectorTrueInt()))
return true;
XMVECTOR vRightTop = XMVectorSet(RightSlope, TopSlope, 1.0f, 0.0f);
XMVECTOR vRightBottom = XMVectorSet(RightSlope, BottomSlope, 1.0f, 0.0f);
XMVECTOR vLeftTop = XMVectorSet(LeftSlope, TopSlope, 1.0f, 0.0f);
XMVECTOR vLeftBottom = XMVectorSet(LeftSlope, BottomSlope, 1.0f, 0.0f);
XMVECTOR vNear = XMVectorReplicatePtr(&Near);
XMVECTOR vFar = XMVectorReplicatePtr(&Far);
XMVECTOR Corners[CORNER_COUNT];
Corners[0] = XMVectorMultiply(vRightTop, vNear);
Corners[1] = XMVectorMultiply(vRightBottom, vNear);
Corners[2] = XMVectorMultiply(vLeftTop, vNear);
Corners[3] = XMVectorMultiply(vLeftBottom, vNear);
Corners[4] = XMVectorMultiply(vRightTop, vFar);
Corners[5] = XMVectorMultiply(vRightBottom, vFar);
Corners[6] = XMVectorMultiply(vLeftTop, vFar);
Corners[7] = XMVectorMultiply(vLeftBottom, vFar);
static const size_t edges[12][2] =
{
{ 0, 1 }, { 2, 3 }, { 0, 2 }, { 1, 3 }, { 4, 5 }, { 6, 7 }, { 4, 6 }, { 5, 7 }, { 0, 4 }, { 1, 5 }, { 2, 6 }, { 3, 7 },
};
XMVECTOR RadiusSq = XMVectorMultiply(vRadius, vRadius);
for (size_t i = 0; i < 12; ++i)
{
size_t ei0 = edges[i][0];
size_t ei1 = edges[i][1];
XMVECTOR Point = DirectX::MathInternal::PointOnLineSegmentNearestPoint(Corners[ei0], Corners[ei1], vCenter);
XMVECTOR Delta = XMVectorSubtract(vCenter, Point);
XMVECTOR DistSq = XMVector3Dot(Delta, Delta);
Intersects = XMVectorOrInt(Intersects, XMVectorLessOrEqual(DistSq, RadiusSq));
}
if (XMVector4EqualInt(Intersects, XMVectorTrueInt()))
return true;
return false;
}
_Use_decl_annotations_
inline bool BoundingFrustum::Intersects(const BoundingBox& box) const noexcept
{
BoundingOrientedBox obox(box.Center, box.Extents, XMFLOAT4(0.f, 0.f, 0.f, 1.f));
return Intersects(obox);
}
_Use_decl_annotations_
inline bool BoundingFrustum::Intersects(const BoundingOrientedBox& box) const noexcept
{
static const XMVECTORU32 SelectY = { { { XM_SELECT_0, XM_SELECT_1, XM_SELECT_0, XM_SELECT_0 } } };
static const XMVECTORU32 SelectZ = { { { XM_SELECT_0, XM_SELECT_0, XM_SELECT_1, XM_SELECT_0 } } };
XMVECTOR Zero = XMVectorZero();
XMVECTOR Planes[6];
Planes[0] = XMVectorSet(0.0f, 0.0f, -1.0f, Near);
Planes[1] = XMVectorSet(0.0f, 0.0f, 1.0f, -Far);
Planes[2] = XMVectorSet(1.0f, 0.0f, -RightSlope, 0.0f);
Planes[3] = XMVectorSet(-1.0f, 0.0f, LeftSlope, 0.0f);
Planes[4] = XMVectorSet(0.0f, 1.0f, -TopSlope, 0.0f);
Planes[5] = XMVectorSet(0.0f, -1.0f, BottomSlope, 0.0f);
XMVECTOR vOrigin = XMLoadFloat3(&Origin);
XMVECTOR FrustumOrientation = XMLoadFloat4(&Orientation);
assert(DirectX::MathInternal::XMQuaternionIsUnit(FrustumOrientation));
XMVECTOR Center = XMLoadFloat3(&box.Center);
XMVECTOR Extents = XMLoadFloat3(&box.Extents);
XMVECTOR BoxOrientation = XMLoadFloat4(&box.Orientation);
assert(DirectX::MathInternal::XMQuaternionIsUnit(BoxOrientation));
Center = XMVector3InverseRotate(XMVectorSubtract(Center, vOrigin), FrustumOrientation);
BoxOrientation = XMQuaternionMultiply(BoxOrientation, XMQuaternionConjugate(FrustumOrientation));
Center = XMVectorInsert<0, 0, 0, 0, 1>(Center, XMVectorSplatOne());
XMMATRIX R = XMMatrixRotationQuaternion(BoxOrientation);
XMVECTOR Outside = XMVectorFalseInt();
XMVECTOR InsideAll = XMVectorTrueInt();
XMVECTOR CenterInsideAll = XMVectorTrueInt();
for (size_t i = 0; i < 6; ++i)
{
XMVECTOR Dist = XMVector4Dot(Center, Planes[i]);
XMVECTOR Radius = XMVector3Dot(Planes[i], R.r[0]);
Radius = XMVectorSelect(Radius, XMVector3Dot(Planes[i], R.r[1]), SelectY);
Radius = XMVectorSelect(Radius, XMVector3Dot(Planes[i], R.r[2]), SelectZ);
Radius = XMVector3Dot(Extents, XMVectorAbs(Radius));
Outside = XMVectorOrInt(Outside, XMVectorGreater(Dist, Radius));
InsideAll = XMVectorAndInt(InsideAll, XMVectorLessOrEqual(Dist, XMVectorNegate(Radius)));
CenterInsideAll = XMVectorAndInt(CenterInsideAll, XMVectorLessOrEqual(Dist, Zero));
}
if (XMVector4EqualInt(Outside, XMVectorTrueInt()))
return false;
if (XMVector4EqualInt(InsideAll, XMVectorTrueInt()))
return true;
if (XMVector4EqualInt(CenterInsideAll, XMVectorTrueInt()))
return true;
XMVECTOR vRightTop = XMVectorSet(RightSlope, TopSlope, 1.0f, 0.0f);
XMVECTOR vRightBottom = XMVectorSet(RightSlope, BottomSlope, 1.0f, 0.0f);
XMVECTOR vLeftTop = XMVectorSet(LeftSlope, TopSlope, 1.0f, 0.0f);
XMVECTOR vLeftBottom = XMVectorSet(LeftSlope, BottomSlope, 1.0f, 0.0f);
XMVECTOR vNear = XMVectorReplicatePtr(&Near);
XMVECTOR vFar = XMVectorReplicatePtr(&Far);
XMVECTOR Corners[CORNER_COUNT];
Corners[0] = XMVectorMultiply(vRightTop, vNear);
Corners[1] = XMVectorMultiply(vRightBottom, vNear);
Corners[2] = XMVectorMultiply(vLeftTop, vNear);
Corners[3] = XMVectorMultiply(vLeftBottom, vNear);
Corners[4] = XMVectorMultiply(vRightTop, vFar);
Corners[5] = XMVectorMultiply(vRightBottom, vFar);
Corners[6] = XMVectorMultiply(vLeftTop, vFar);
Corners[7] = XMVectorMultiply(vLeftBottom, vFar);
{
XMVECTOR FrustumMin, FrustumMax;
FrustumMin = XMVector3Dot(Corners[0], R.r[0]);
FrustumMin = XMVectorSelect(FrustumMin, XMVector3Dot(Corners[0], R.r[1]), SelectY);
FrustumMin = XMVectorSelect(FrustumMin, XMVector3Dot(Corners[0], R.r[2]), SelectZ);
FrustumMax = FrustumMin;
for (size_t i = 1; i < BoundingOrientedBox::CORNER_COUNT; ++i)
{
XMVECTOR Temp = XMVector3Dot(Corners[i], R.r[0]);
Temp = XMVectorSelect(Temp, XMVector3Dot(Corners[i], R.r[1]), SelectY);
Temp = XMVectorSelect(Temp, XMVector3Dot(Corners[i], R.r[2]), SelectZ);
FrustumMin = XMVectorMin(FrustumMin, Temp);
FrustumMax = XMVectorMax(FrustumMax, Temp);
}
XMVECTOR BoxDist = XMVector3Dot(Center, R.r[0]);
BoxDist = XMVectorSelect(BoxDist, XMVector3Dot(Center, R.r[1]), SelectY);
BoxDist = XMVectorSelect(BoxDist, XMVector3Dot(Center, R.r[2]), SelectZ);
XMVECTOR Result = XMVectorOrInt(XMVectorGreater(FrustumMin, XMVectorAdd(BoxDist, Extents)),
XMVectorLess(FrustumMax, XMVectorSubtract(BoxDist, Extents)));
if (DirectX::MathInternal::XMVector3AnyTrue(Result))
return false;
}
XMVECTOR FrustumEdgeAxis[6];
FrustumEdgeAxis[0] = vRightTop;
FrustumEdgeAxis[1] = vRightBottom;
FrustumEdgeAxis[2] = vLeftTop;
FrustumEdgeAxis[3] = vLeftBottom;
FrustumEdgeAxis[4] = XMVectorSubtract(vRightTop, vLeftTop);
FrustumEdgeAxis[5] = XMVectorSubtract(vLeftBottom, vLeftTop);
for (size_t i = 0; i < 3; ++i)
{
for (size_t j = 0; j < 6; j++)
{
XMVECTOR Axis = XMVector3Cross(R.r[i], FrustumEdgeAxis[j]);
XMVECTOR FrustumMin, FrustumMax;
FrustumMin = FrustumMax = XMVector3Dot(Axis, Corners[0]);
for (size_t k = 1; k < CORNER_COUNT; k++)
{
XMVECTOR Temp = XMVector3Dot(Axis, Corners[k]);
FrustumMin = XMVectorMin(FrustumMin, Temp);
FrustumMax = XMVectorMax(FrustumMax, Temp);
}
XMVECTOR Dist = XMVector3Dot(Center, Axis);
XMVECTOR Radius = XMVector3Dot(Axis, R.r[0]);
Radius = XMVectorSelect(Radius, XMVector3Dot(Axis, R.r[1]), SelectY);
Radius = XMVectorSelect(Radius, XMVector3Dot(Axis, R.r[2]), SelectZ);
Radius = XMVector3Dot(Extents, XMVectorAbs(Radius));
Outside = XMVectorOrInt(Outside, XMVectorGreater(Dist, XMVectorAdd(FrustumMax, Radius)));
Outside = XMVectorOrInt(Outside, XMVectorLess(Dist, XMVectorSubtract(FrustumMin, Radius)));
}
}
if (XMVector4EqualInt(Outside, XMVectorTrueInt()))
return false;
return true;
}
_Use_decl_annotations_
inline bool BoundingFrustum::Intersects(const BoundingFrustum& fr) const noexcept
{
XMVECTOR OriginB = XMLoadFloat3(&Origin);
XMVECTOR OrientationB = XMLoadFloat4(&Orientation);
assert(DirectX::MathInternal::XMQuaternionIsUnit(OrientationB));
XMVECTOR AxisB[6];
AxisB[0] = XMVectorSet(0.0f, 0.0f, -1.0f, 0.0f);
AxisB[1] = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f);
AxisB[2] = XMVectorSet(1.0f, 0.0f, -RightSlope, 0.0f);
AxisB[3] = XMVectorSet(-1.0f, 0.0f, LeftSlope, 0.0f);
AxisB[4] = XMVectorSet(0.0f, 1.0f, -TopSlope, 0.0f);
AxisB[5] = XMVectorSet(0.0f, -1.0f, BottomSlope, 0.0f);
XMVECTOR PlaneDistB[6];
PlaneDistB[0] = XMVectorNegate(XMVectorReplicatePtr(&Near));
PlaneDistB[1] = XMVectorReplicatePtr(&Far);
PlaneDistB[2] = XMVectorZero();
PlaneDistB[3] = XMVectorZero();
PlaneDistB[4] = XMVectorZero();
PlaneDistB[5] = XMVectorZero();
XMVECTOR OriginA = XMLoadFloat3(&fr.Origin);
XMVECTOR OrientationA = XMLoadFloat4(&fr.Orientation);
assert(DirectX::MathInternal::XMQuaternionIsUnit(OrientationA));
OriginA = XMVector3InverseRotate(XMVectorSubtract(OriginA, OriginB), OrientationB);
OrientationA = XMQuaternionMultiply(OrientationA, XMQuaternionConjugate(OrientationB));
XMVECTOR RightTopA = XMVectorSet(fr.RightSlope, fr.TopSlope, 1.0f, 0.0f);
XMVECTOR RightBottomA = XMVectorSet(fr.RightSlope, fr.BottomSlope, 1.0f, 0.0f);
XMVECTOR LeftTopA = XMVectorSet(fr.LeftSlope, fr.TopSlope, 1.0f, 0.0f);
XMVECTOR LeftBottomA = XMVectorSet(fr.LeftSlope, fr.BottomSlope, 1.0f, 0.0f);
XMVECTOR NearA = XMVectorReplicatePtr(&fr.Near);
XMVECTOR FarA = XMVectorReplicatePtr(&fr.Far);
RightTopA = XMVector3Rotate(RightTopA, OrientationA);
RightBottomA = XMVector3Rotate(RightBottomA, OrientationA);
LeftTopA = XMVector3Rotate(LeftTopA, OrientationA);
LeftBottomA = XMVector3Rotate(LeftBottomA, OrientationA);
XMVECTOR CornersA[CORNER_COUNT];
CornersA[0] = XMVectorMultiplyAdd(RightTopA, NearA, OriginA);
CornersA[1] = XMVectorMultiplyAdd(RightBottomA, NearA, OriginA);
CornersA[2] = XMVectorMultiplyAdd(LeftTopA, NearA, OriginA);
CornersA[3] = XMVectorMultiplyAdd(LeftBottomA, NearA, OriginA);
CornersA[4] = XMVectorMultiplyAdd(RightTopA, FarA, OriginA);
CornersA[5] = XMVectorMultiplyAdd(RightBottomA, FarA, OriginA);
CornersA[6] = XMVectorMultiplyAdd(LeftTopA, FarA, OriginA);
CornersA[7] = XMVectorMultiplyAdd(LeftBottomA, FarA, OriginA);
XMVECTOR Outside = XMVectorFalseInt();
XMVECTOR InsideAll = XMVectorTrueInt();
for (size_t i = 0; i < 6; ++i)
{
XMVECTOR Min, Max;
Min = Max = XMVector3Dot(AxisB[i], CornersA[0]);
for (size_t j = 1; j < CORNER_COUNT; j++)
{
XMVECTOR Temp = XMVector3Dot(AxisB[i], CornersA[j]);
Min = XMVectorMin(Min, Temp);
Max = XMVectorMax(Max, Temp);
}
Outside = XMVectorOrInt(Outside, XMVectorGreater(Min, PlaneDistB[i]));
InsideAll = XMVectorAndInt(InsideAll, XMVectorLessOrEqual(Max, PlaneDistB[i]));
}
if (XMVector4EqualInt(Outside, XMVectorTrueInt()))
return false;
if (XMVector4EqualInt(InsideAll, XMVectorTrueInt()))
return true;
XMVECTOR RightTopB = XMVectorSet(RightSlope, TopSlope, 1.0f, 0.0f);
XMVECTOR RightBottomB = XMVectorSet(RightSlope, BottomSlope, 1.0f, 0.0f);
XMVECTOR LeftTopB = XMVectorSet(LeftSlope, TopSlope, 1.0f, 0.0f);
XMVECTOR LeftBottomB = XMVectorSet(LeftSlope, BottomSlope, 1.0f, 0.0f);
XMVECTOR NearB = XMVectorReplicatePtr(&Near);
XMVECTOR FarB = XMVectorReplicatePtr(&Far);
XMVECTOR CornersB[BoundingFrustum::CORNER_COUNT];
CornersB[0] = XMVectorMultiply(RightTopB, NearB);
CornersB[1] = XMVectorMultiply(RightBottomB, NearB);
CornersB[2] = XMVectorMultiply(LeftTopB, NearB);
CornersB[3] = XMVectorMultiply(LeftBottomB, NearB);
CornersB[4] = XMVectorMultiply(RightTopB, FarB);
CornersB[5] = XMVectorMultiply(RightBottomB, FarB);
CornersB[6] = XMVectorMultiply(LeftTopB, FarB);
CornersB[7] = XMVectorMultiply(LeftBottomB, FarB);
XMVECTOR AxisA[6];
XMVECTOR PlaneDistA[6];
AxisA[0] = XMVectorSet(0.0f, 0.0f, -1.0f, 0.0f);
AxisA[1] = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f);
AxisA[2] = XMVectorSet(1.0f, 0.0f, -fr.RightSlope, 0.0f);
AxisA[3] = XMVectorSet(-1.0f, 0.0f, fr.LeftSlope, 0.0f);
AxisA[4] = XMVectorSet(0.0f, 1.0f, -fr.TopSlope, 0.0f);
AxisA[5] = XMVectorSet(0.0f, -1.0f, fr.BottomSlope, 0.0f);
AxisA[0] = XMVector3Rotate(AxisA[0], OrientationA);
AxisA[1] = XMVectorNegate(AxisA[0]);
AxisA[2] = XMVector3Rotate(AxisA[2], OrientationA);
AxisA[3] = XMVector3Rotate(AxisA[3], OrientationA);
AxisA[4] = XMVector3Rotate(AxisA[4], OrientationA);
AxisA[5] = XMVector3Rotate(AxisA[5], OrientationA);
PlaneDistA[0] = XMVector3Dot(AxisA[0], CornersA[0]); PlaneDistA[1] = XMVector3Dot(AxisA[1], CornersA[4]); PlaneDistA[2] = XMVector3Dot(AxisA[2], OriginA);
PlaneDistA[3] = XMVector3Dot(AxisA[3], OriginA);
PlaneDistA[4] = XMVector3Dot(AxisA[4], OriginA);
PlaneDistA[5] = XMVector3Dot(AxisA[5], OriginA);
for (size_t i = 0; i < 6; ++i)
{
XMVECTOR Min;
Min = XMVector3Dot(AxisA[i], CornersB[0]);
for (size_t j = 1; j < CORNER_COUNT; j++)
{
XMVECTOR Temp = XMVector3Dot(AxisA[i], CornersB[j]);
Min = XMVectorMin(Min, Temp);
}
Outside = XMVectorOrInt(Outside, XMVectorGreater(Min, PlaneDistA[i]));
}
if (XMVector4EqualInt(Outside, XMVectorTrueInt()))
return false;
XMVECTOR FrustumEdgeAxisA[6];
FrustumEdgeAxisA[0] = RightTopA;
FrustumEdgeAxisA[1] = RightBottomA;
FrustumEdgeAxisA[2] = LeftTopA;
FrustumEdgeAxisA[3] = LeftBottomA;
FrustumEdgeAxisA[4] = XMVectorSubtract(RightTopA, LeftTopA);
FrustumEdgeAxisA[5] = XMVectorSubtract(LeftBottomA, LeftTopA);
XMVECTOR FrustumEdgeAxisB[6];
FrustumEdgeAxisB[0] = RightTopB;
FrustumEdgeAxisB[1] = RightBottomB;
FrustumEdgeAxisB[2] = LeftTopB;
FrustumEdgeAxisB[3] = LeftBottomB;
FrustumEdgeAxisB[4] = XMVectorSubtract(RightTopB, LeftTopB);
FrustumEdgeAxisB[5] = XMVectorSubtract(LeftBottomB, LeftTopB);
for (size_t i = 0; i < 6; ++i)
{
for (size_t j = 0; j < 6; j++)
{
XMVECTOR Axis = XMVector3Cross(FrustumEdgeAxisA[i], FrustumEdgeAxisB[j]);
XMVECTOR MinA, MaxA;
XMVECTOR MinB, MaxB;
MinA = MaxA = XMVector3Dot(Axis, CornersA[0]);
MinB = MaxB = XMVector3Dot(Axis, CornersB[0]);
for (size_t k = 1; k < CORNER_COUNT; k++)
{
XMVECTOR TempA = XMVector3Dot(Axis, CornersA[k]);
MinA = XMVectorMin(MinA, TempA);
MaxA = XMVectorMax(MaxA, TempA);
XMVECTOR TempB = XMVector3Dot(Axis, CornersB[k]);
MinB = XMVectorMin(MinB, TempB);
MaxB = XMVectorMax(MaxB, TempB);
}
Outside = XMVectorOrInt(Outside, XMVectorGreater(MinA, MaxB));
Outside = XMVectorOrInt(Outside, XMVectorGreater(MinB, MaxA));
}
}
if (XMVector4EqualInt(Outside, XMVectorTrueInt()))
return false;
return true;
}
_Use_decl_annotations_
inline bool XM_CALLCONV BoundingFrustum::Intersects(FXMVECTOR V0, FXMVECTOR V1, FXMVECTOR V2) const noexcept
{
XMVECTOR Planes[6];
Planes[0] = XMVectorSet(0.0f, 0.0f, -1.0f, -Near);
Planes[1] = XMVectorSet(0.0f, 0.0f, 1.0f, Far);
Planes[2] = XMVectorSet(1.0f, 0.0f, -RightSlope, 0.0f);
Planes[3] = XMVectorSet(-1.0f, 0.0f, LeftSlope, 0.0f);
Planes[4] = XMVectorSet(0.0f, 1.0f, -TopSlope, 0.0f);
Planes[5] = XMVectorSet(0.0f, -1.0f, BottomSlope, 0.0f);
XMVECTOR vOrigin = XMLoadFloat3(&Origin);
XMVECTOR vOrientation = XMLoadFloat4(&Orientation);
assert(DirectX::MathInternal::XMQuaternionIsUnit(vOrientation));
XMVECTOR TV0 = XMVector3InverseRotate(XMVectorSubtract(V0, vOrigin), vOrientation);
XMVECTOR TV1 = XMVector3InverseRotate(XMVectorSubtract(V1, vOrigin), vOrientation);
XMVECTOR TV2 = XMVector3InverseRotate(XMVectorSubtract(V2, vOrigin), vOrientation);
XMVECTOR Outside = XMVectorFalseInt();
XMVECTOR InsideAll = XMVectorTrueInt();
for (size_t i = 0; i < 6; ++i)
{
XMVECTOR Dist0 = XMVector3Dot(TV0, Planes[i]);
XMVECTOR Dist1 = XMVector3Dot(TV1, Planes[i]);
XMVECTOR Dist2 = XMVector3Dot(TV2, Planes[i]);
XMVECTOR MinDist = XMVectorMin(Dist0, Dist1);
MinDist = XMVectorMin(MinDist, Dist2);
XMVECTOR MaxDist = XMVectorMax(Dist0, Dist1);
MaxDist = XMVectorMax(MaxDist, Dist2);
XMVECTOR PlaneDist = XMVectorSplatW(Planes[i]);
Outside = XMVectorOrInt(Outside, XMVectorGreater(MinDist, PlaneDist));
InsideAll = XMVectorAndInt(InsideAll, XMVectorLessOrEqual(MaxDist, PlaneDist));
}
if (XMVector4EqualInt(Outside, XMVectorTrueInt()))
return false;
if (XMVector4EqualInt(InsideAll, XMVectorTrueInt()))
return true;
XMVECTOR vRightTop = XMVectorSet(RightSlope, TopSlope, 1.0f, 0.0f);
XMVECTOR vRightBottom = XMVectorSet(RightSlope, BottomSlope, 1.0f, 0.0f);
XMVECTOR vLeftTop = XMVectorSet(LeftSlope, TopSlope, 1.0f, 0.0f);
XMVECTOR vLeftBottom = XMVectorSet(LeftSlope, BottomSlope, 1.0f, 0.0f);
XMVECTOR vNear = XMVectorReplicatePtr(&Near);
XMVECTOR vFar = XMVectorReplicatePtr(&Far);
XMVECTOR Corners[CORNER_COUNT];
Corners[0] = XMVectorMultiply(vRightTop, vNear);
Corners[1] = XMVectorMultiply(vRightBottom, vNear);
Corners[2] = XMVectorMultiply(vLeftTop, vNear);
Corners[3] = XMVectorMultiply(vLeftBottom, vNear);
Corners[4] = XMVectorMultiply(vRightTop, vFar);
Corners[5] = XMVectorMultiply(vRightBottom, vFar);
Corners[6] = XMVectorMultiply(vLeftTop, vFar);
Corners[7] = XMVectorMultiply(vLeftBottom, vFar);
XMVECTOR Normal = XMVector3Cross(XMVectorSubtract(V1, V0), XMVectorSubtract(V2, V0));
XMVECTOR Dist = XMVector3Dot(Normal, V0);
XMVECTOR MinDist, MaxDist;
MinDist = MaxDist = XMVector3Dot(Corners[0], Normal);
for (size_t i = 1; i < CORNER_COUNT; ++i)
{
XMVECTOR Temp = XMVector3Dot(Corners[i], Normal);
MinDist = XMVectorMin(MinDist, Temp);
MaxDist = XMVectorMax(MaxDist, Temp);
}
Outside = XMVectorOrInt(XMVectorGreater(MinDist, Dist), XMVectorLess(MaxDist, Dist));
if (XMVector4EqualInt(Outside, XMVectorTrueInt()))
return false;
XMVECTOR TriangleEdgeAxis[3];
TriangleEdgeAxis[0] = XMVectorSubtract(V1, V0);
TriangleEdgeAxis[1] = XMVectorSubtract(V2, V1);
TriangleEdgeAxis[2] = XMVectorSubtract(V0, V2);
XMVECTOR FrustumEdgeAxis[6];
FrustumEdgeAxis[0] = vRightTop;
FrustumEdgeAxis[1] = vRightBottom;
FrustumEdgeAxis[2] = vLeftTop;
FrustumEdgeAxis[3] = vLeftBottom;
FrustumEdgeAxis[4] = XMVectorSubtract(vRightTop, vLeftTop);
FrustumEdgeAxis[5] = XMVectorSubtract(vLeftBottom, vLeftTop);
for (size_t i = 0; i < 3; ++i)
{
for (size_t j = 0; j < 6; j++)
{
XMVECTOR Axis = XMVector3Cross(TriangleEdgeAxis[i], FrustumEdgeAxis[j]);
XMVECTOR MinA, MaxA;
XMVECTOR Dist0 = XMVector3Dot(V0, Axis);
XMVECTOR Dist1 = XMVector3Dot(V1, Axis);
XMVECTOR Dist2 = XMVector3Dot(V2, Axis);
MinA = XMVectorMin(Dist0, Dist1);
MinA = XMVectorMin(MinA, Dist2);
MaxA = XMVectorMax(Dist0, Dist1);
MaxA = XMVectorMax(MaxA, Dist2);
XMVECTOR MinB, MaxB;
MinB = MaxB = XMVector3Dot(Axis, Corners[0]);
for (size_t k = 1; k < CORNER_COUNT; k++)
{
XMVECTOR Temp = XMVector3Dot(Axis, Corners[k]);
MinB = XMVectorMin(MinB, Temp);
MaxB = XMVectorMax(MaxB, Temp);
}
Outside = XMVectorOrInt(Outside, XMVectorGreater(MinA, MaxB));
Outside = XMVectorOrInt(Outside, XMVectorGreater(MinB, MaxA));
}
}
if (XMVector4EqualInt(Outside, XMVectorTrueInt()))
return false;
return true;
}
_Use_decl_annotations_
inline PlaneIntersectionType XM_CALLCONV BoundingFrustum::Intersects(FXMVECTOR Plane) const noexcept
{
assert(DirectX::MathInternal::XMPlaneIsUnit(Plane));
XMVECTOR vOrigin = XMLoadFloat3(&Origin);
XMVECTOR vOrientation = XMLoadFloat4(&Orientation);
assert(DirectX::MathInternal::XMQuaternionIsUnit(vOrientation));
vOrigin = XMVectorInsert<0, 0, 0, 0, 1>(vOrigin, XMVectorSplatOne());
XMVECTOR RightTop = XMVectorSet(RightSlope, TopSlope, 1.0f, 0.0f);
XMVECTOR RightBottom = XMVectorSet(RightSlope, BottomSlope, 1.0f, 0.0f);
XMVECTOR LeftTop = XMVectorSet(LeftSlope, TopSlope, 1.0f, 0.0f);
XMVECTOR LeftBottom = XMVectorSet(LeftSlope, BottomSlope, 1.0f, 0.0f);
XMVECTOR vNear = XMVectorReplicatePtr(&Near);
XMVECTOR vFar = XMVectorReplicatePtr(&Far);
RightTop = XMVector3Rotate(RightTop, vOrientation);
RightBottom = XMVector3Rotate(RightBottom, vOrientation);
LeftTop = XMVector3Rotate(LeftTop, vOrientation);
LeftBottom = XMVector3Rotate(LeftBottom, vOrientation);
XMVECTOR Corners0 = XMVectorMultiplyAdd(RightTop, vNear, vOrigin);
XMVECTOR Corners1 = XMVectorMultiplyAdd(RightBottom, vNear, vOrigin);
XMVECTOR Corners2 = XMVectorMultiplyAdd(LeftTop, vNear, vOrigin);
XMVECTOR Corners3 = XMVectorMultiplyAdd(LeftBottom, vNear, vOrigin);
XMVECTOR Corners4 = XMVectorMultiplyAdd(RightTop, vFar, vOrigin);
XMVECTOR Corners5 = XMVectorMultiplyAdd(RightBottom, vFar, vOrigin);
XMVECTOR Corners6 = XMVectorMultiplyAdd(LeftTop, vFar, vOrigin);
XMVECTOR Corners7 = XMVectorMultiplyAdd(LeftBottom, vFar, vOrigin);
XMVECTOR Outside, Inside;
DirectX::MathInternal::FastIntersectFrustumPlane(Corners0, Corners1, Corners2, Corners3,
Corners4, Corners5, Corners6, Corners7,
Plane, Outside, Inside);
if (XMVector4EqualInt(Outside, XMVectorTrueInt()))
return FRONT;
if (XMVector4EqualInt(Inside, XMVectorTrueInt()))
return BACK;
return INTERSECTING;
}
_Use_decl_annotations_
inline bool XM_CALLCONV BoundingFrustum::Intersects(FXMVECTOR rayOrigin, FXMVECTOR Direction, float& Dist) const noexcept
{
if (Contains(rayOrigin) == CONTAINS)
{
Dist = 0.0f;
return true;
}
XMVECTOR Planes[6];
Planes[0] = XMVectorSet(0.0f, 0.0f, -1.0f, Near);
Planes[1] = XMVectorSet(0.0f, 0.0f, 1.0f, -Far);
Planes[2] = XMVectorSet(1.0f, 0.0f, -RightSlope, 0.0f);
Planes[3] = XMVectorSet(-1.0f, 0.0f, LeftSlope, 0.0f);
Planes[4] = XMVectorSet(0.0f, 1.0f, -TopSlope, 0.0f);
Planes[5] = XMVectorSet(0.0f, -1.0f, BottomSlope, 0.0f);
XMVECTOR frOrigin = XMLoadFloat3(&Origin);
XMVECTOR frOrientation = XMLoadFloat4(&Orientation);
float tnear = -FLT_MAX;
float tfar = FLT_MAX;
for (size_t i = 0; i < 6; ++i)
{
XMVECTOR Plane = DirectX::MathInternal::XMPlaneTransform(Planes[i], frOrientation, frOrigin);
Plane = XMPlaneNormalize(Plane);
XMVECTOR AxisDotOrigin = XMPlaneDotCoord(Plane, rayOrigin);
XMVECTOR AxisDotDirection = XMVector3Dot(Plane, Direction);
if (XMVector3LessOrEqual(XMVectorAbs(AxisDotDirection), g_RayEpsilon))
{
if (XMVector3Greater(AxisDotOrigin, g_XMZero))
{
Dist = 0.f;
return false;
}
}
else
{
float vd = XMVectorGetX(AxisDotDirection);
float vn = XMVectorGetX(AxisDotOrigin);
float t = -vn / vd;
if (vd < 0.0f)
{
if (t > tfar)
{
Dist = 0.f;
return false;
}
if (t > tnear)
{
tnear = t;
}
}
else
{
if (t < tnear)
{
Dist = 0.f;
return false;
}
if (t < tfar)
{
tfar = t;
}
}
}
}
float distance = (tnear >= 0.0f) ? tnear : tfar;
if (distance >= 0.0f)
{
Dist = distance;
return true;
}
Dist = 0.f;
return false;
}
_Use_decl_annotations_
inline ContainmentType XM_CALLCONV BoundingFrustum::ContainedBy(
FXMVECTOR Plane0, FXMVECTOR Plane1, FXMVECTOR Plane2,
GXMVECTOR Plane3,
HXMVECTOR Plane4, HXMVECTOR Plane5) const noexcept
{
XMVECTOR vOrigin = XMLoadFloat3(&Origin);
XMVECTOR vOrientation = XMLoadFloat4(&Orientation);
assert(DirectX::MathInternal::XMQuaternionIsUnit(vOrientation));
vOrigin = XMVectorInsert<0, 0, 0, 0, 1>(vOrigin, XMVectorSplatOne());
XMVECTOR RightTop = XMVectorSet(RightSlope, TopSlope, 1.0f, 0.0f);
XMVECTOR RightBottom = XMVectorSet(RightSlope, BottomSlope, 1.0f, 0.0f);
XMVECTOR LeftTop = XMVectorSet(LeftSlope, TopSlope, 1.0f, 0.0f);
XMVECTOR LeftBottom = XMVectorSet(LeftSlope, BottomSlope, 1.0f, 0.0f);
XMVECTOR vNear = XMVectorReplicatePtr(&Near);
XMVECTOR vFar = XMVectorReplicatePtr(&Far);
RightTop = XMVector3Rotate(RightTop, vOrientation);
RightBottom = XMVector3Rotate(RightBottom, vOrientation);
LeftTop = XMVector3Rotate(LeftTop, vOrientation);
LeftBottom = XMVector3Rotate(LeftBottom, vOrientation);
XMVECTOR Corners0 = XMVectorMultiplyAdd(RightTop, vNear, vOrigin);
XMVECTOR Corners1 = XMVectorMultiplyAdd(RightBottom, vNear, vOrigin);
XMVECTOR Corners2 = XMVectorMultiplyAdd(LeftTop, vNear, vOrigin);
XMVECTOR Corners3 = XMVectorMultiplyAdd(LeftBottom, vNear, vOrigin);
XMVECTOR Corners4 = XMVectorMultiplyAdd(RightTop, vFar, vOrigin);
XMVECTOR Corners5 = XMVectorMultiplyAdd(RightBottom, vFar, vOrigin);
XMVECTOR Corners6 = XMVectorMultiplyAdd(LeftTop, vFar, vOrigin);
XMVECTOR Corners7 = XMVectorMultiplyAdd(LeftBottom, vFar, vOrigin);
XMVECTOR Outside, Inside;
DirectX::MathInternal::FastIntersectFrustumPlane(Corners0, Corners1, Corners2, Corners3,
Corners4, Corners5, Corners6, Corners7,
Plane0, Outside, Inside);
XMVECTOR AnyOutside = Outside;
XMVECTOR AllInside = Inside;
DirectX::MathInternal::FastIntersectFrustumPlane(Corners0, Corners1, Corners2, Corners3,
Corners4, Corners5, Corners6, Corners7,
Plane1, Outside, Inside);
AnyOutside = XMVectorOrInt(AnyOutside, Outside);
AllInside = XMVectorAndInt(AllInside, Inside);
DirectX::MathInternal::FastIntersectFrustumPlane(Corners0, Corners1, Corners2, Corners3,
Corners4, Corners5, Corners6, Corners7,
Plane2, Outside, Inside);
AnyOutside = XMVectorOrInt(AnyOutside, Outside);
AllInside = XMVectorAndInt(AllInside, Inside);
DirectX::MathInternal::FastIntersectFrustumPlane(Corners0, Corners1, Corners2, Corners3,
Corners4, Corners5, Corners6, Corners7,
Plane3, Outside, Inside);
AnyOutside = XMVectorOrInt(AnyOutside, Outside);
AllInside = XMVectorAndInt(AllInside, Inside);
DirectX::MathInternal::FastIntersectFrustumPlane(Corners0, Corners1, Corners2, Corners3,
Corners4, Corners5, Corners6, Corners7,
Plane4, Outside, Inside);
AnyOutside = XMVectorOrInt(AnyOutside, Outside);
AllInside = XMVectorAndInt(AllInside, Inside);
DirectX::MathInternal::FastIntersectFrustumPlane(Corners0, Corners1, Corners2, Corners3,
Corners4, Corners5, Corners6, Corners7,
Plane5, Outside, Inside);
AnyOutside = XMVectorOrInt(AnyOutside, Outside);
AllInside = XMVectorAndInt(AllInside, Inside);
if (XMVector4EqualInt(AnyOutside, XMVectorTrueInt()))
return DISJOINT;
if (XMVector4EqualInt(AllInside, XMVectorTrueInt()))
return CONTAINS;
return INTERSECTS;
}
_Use_decl_annotations_
inline void BoundingFrustum::GetPlanes(XMVECTOR* NearPlane, XMVECTOR* FarPlane, XMVECTOR* RightPlane,
XMVECTOR* LeftPlane, XMVECTOR* TopPlane, XMVECTOR* BottomPlane) const noexcept
{
XMVECTOR vOrigin = XMLoadFloat3(&Origin);
XMVECTOR vOrientation = XMLoadFloat4(&Orientation);
if (NearPlane)
{
XMVECTOR vNearPlane = XMVectorSet(0.0f, 0.0f, -1.0f, Near);
vNearPlane = DirectX::MathInternal::XMPlaneTransform(vNearPlane, vOrientation, vOrigin);
*NearPlane = XMPlaneNormalize(vNearPlane);
}
if (FarPlane)
{
XMVECTOR vFarPlane = XMVectorSet(0.0f, 0.0f, 1.0f, -Far);
vFarPlane = DirectX::MathInternal::XMPlaneTransform(vFarPlane, vOrientation, vOrigin);
*FarPlane = XMPlaneNormalize(vFarPlane);
}
if (RightPlane)
{
XMVECTOR vRightPlane = XMVectorSet(1.0f, 0.0f, -RightSlope, 0.0f);
vRightPlane = DirectX::MathInternal::XMPlaneTransform(vRightPlane, vOrientation, vOrigin);
*RightPlane = XMPlaneNormalize(vRightPlane);
}
if (LeftPlane)
{
XMVECTOR vLeftPlane = XMVectorSet(-1.0f, 0.0f, LeftSlope, 0.0f);
vLeftPlane = DirectX::MathInternal::XMPlaneTransform(vLeftPlane, vOrientation, vOrigin);
*LeftPlane = XMPlaneNormalize(vLeftPlane);
}
if (TopPlane)
{
XMVECTOR vTopPlane = XMVectorSet(0.0f, 1.0f, -TopSlope, 0.0f);
vTopPlane = DirectX::MathInternal::XMPlaneTransform(vTopPlane, vOrientation, vOrigin);
*TopPlane = XMPlaneNormalize(vTopPlane);
}
if (BottomPlane)
{
XMVECTOR vBottomPlane = XMVectorSet(0.0f, -1.0f, BottomSlope, 0.0f);
vBottomPlane = DirectX::MathInternal::XMPlaneTransform(vBottomPlane, vOrientation, vOrigin);
*BottomPlane = XMPlaneNormalize(vBottomPlane);
}
}
_Use_decl_annotations_
inline void XM_CALLCONV BoundingFrustum::CreateFromMatrix(BoundingFrustum& Out, FXMMATRIX Projection, bool rhcoords) noexcept
{
static XMVECTORF32 NDCPoints[6] =
{
{ { { 1.0f, 0.0f, 1.0f, 1.0f } } }, { { { -1.0f, 0.0f, 1.0f, 1.0f } } }, { { { 0.0f, 1.0f, 1.0f, 1.0f } } }, { { { 0.0f, -1.0f, 1.0f, 1.0f } } },
{ { { 0.0f, 0.0f, 0.0f, 1.0f } } }, { { { 0.0f, 0.0f, 1.0f, 1.0f } } } };
XMVECTOR Determinant;
XMMATRIX matInverse = XMMatrixInverse(&Determinant, Projection);
XMVECTOR Points[6];
for (size_t i = 0; i < 6; ++i)
{
Points[i] = XMVector4Transform(NDCPoints[i], matInverse);
}
Out.Origin = XMFLOAT3(0.0f, 0.0f, 0.0f);
Out.Orientation = XMFLOAT4(0.0f, 0.0f, 0.0f, 1.0f);
Points[0] = XMVectorMultiply(Points[0], XMVectorReciprocal(XMVectorSplatZ(Points[0])));
Points[1] = XMVectorMultiply(Points[1], XMVectorReciprocal(XMVectorSplatZ(Points[1])));
Points[2] = XMVectorMultiply(Points[2], XMVectorReciprocal(XMVectorSplatZ(Points[2])));
Points[3] = XMVectorMultiply(Points[3], XMVectorReciprocal(XMVectorSplatZ(Points[3])));
Out.RightSlope = XMVectorGetX(Points[0]);
Out.LeftSlope = XMVectorGetX(Points[1]);
Out.TopSlope = XMVectorGetY(Points[2]);
Out.BottomSlope = XMVectorGetY(Points[3]);
Points[4] = XMVectorMultiply(Points[4], XMVectorReciprocal(XMVectorSplatW(Points[4])));
Points[5] = XMVectorMultiply(Points[5], XMVectorReciprocal(XMVectorSplatW(Points[5])));
if (rhcoords)
{
Out.Near = XMVectorGetZ(Points[5]);
Out.Far = XMVectorGetZ(Points[4]);
}
else
{
Out.Near = XMVectorGetZ(Points[4]);
Out.Far = XMVectorGetZ(Points[5]);
}
}
namespace TriangleTests
{
_Use_decl_annotations_
inline bool XM_CALLCONV Intersects(
FXMVECTOR Origin, FXMVECTOR Direction, FXMVECTOR V0,
GXMVECTOR V1,
HXMVECTOR V2, float& Dist) noexcept
{
assert(DirectX::MathInternal::XMVector3IsUnit(Direction));
XMVECTOR Zero = XMVectorZero();
XMVECTOR e1 = XMVectorSubtract(V1, V0);
XMVECTOR e2 = XMVectorSubtract(V2, V0);
XMVECTOR p = XMVector3Cross(Direction, e2);
XMVECTOR det = XMVector3Dot(e1, p);
XMVECTOR u, v, t;
if (XMVector3GreaterOrEqual(det, g_RayEpsilon))
{
XMVECTOR s = XMVectorSubtract(Origin, V0);
u = XMVector3Dot(s, p);
XMVECTOR NoIntersection = XMVectorLess(u, Zero);
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorGreater(u, det));
XMVECTOR q = XMVector3Cross(s, e1);
v = XMVector3Dot(Direction, q);
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorLess(v, Zero));
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorGreater(XMVectorAdd(u, v), det));
t = XMVector3Dot(e2, q);
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorLess(t, Zero));
if (XMVector4EqualInt(NoIntersection, XMVectorTrueInt()))
{
Dist = 0.f;
return false;
}
}
else if (XMVector3LessOrEqual(det, g_RayNegEpsilon))
{
XMVECTOR s = XMVectorSubtract(Origin, V0);
u = XMVector3Dot(s, p);
XMVECTOR NoIntersection = XMVectorGreater(u, Zero);
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorLess(u, det));
XMVECTOR q = XMVector3Cross(s, e1);
v = XMVector3Dot(Direction, q);
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorGreater(v, Zero));
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorLess(XMVectorAdd(u, v), det));
t = XMVector3Dot(e2, q);
NoIntersection = XMVectorOrInt(NoIntersection, XMVectorGreater(t, Zero));
if (XMVector4EqualInt(NoIntersection, XMVectorTrueInt()))
{
Dist = 0.f;
return false;
}
}
else
{
Dist = 0.f;
return false;
}
t = XMVectorDivide(t, det);
XMStoreFloat(&Dist, t);
return true;
}
_Use_decl_annotations_
inline bool XM_CALLCONV Intersects(FXMVECTOR A0, FXMVECTOR A1, FXMVECTOR A2, GXMVECTOR B0, HXMVECTOR B1, HXMVECTOR B2) noexcept
{
static const XMVECTORU32 SelectY = { { { XM_SELECT_0, XM_SELECT_1, XM_SELECT_0, XM_SELECT_0 } } };
static const XMVECTORU32 SelectZ = { { { XM_SELECT_0, XM_SELECT_0, XM_SELECT_1, XM_SELECT_0 } } };
static const XMVECTORU32 Select0111 = { { { XM_SELECT_0, XM_SELECT_1, XM_SELECT_1, XM_SELECT_1 } } };
static const XMVECTORU32 Select1011 = { { { XM_SELECT_1, XM_SELECT_0, XM_SELECT_1, XM_SELECT_1 } } };
static const XMVECTORU32 Select1101 = { { { XM_SELECT_1, XM_SELECT_1, XM_SELECT_0, XM_SELECT_1 } } };
XMVECTOR Zero = XMVectorZero();
XMVECTOR N1 = XMVector3Cross(XMVectorSubtract(A1, A0), XMVectorSubtract(A2, A0));
assert(!XMVector3Equal(N1, Zero));
XMVECTOR BDist = XMVector3Dot(N1, XMVectorSubtract(B0, A0));
BDist = XMVectorSelect(BDist, XMVector3Dot(N1, XMVectorSubtract(B1, A0)), SelectY);
BDist = XMVectorSelect(BDist, XMVector3Dot(N1, XMVectorSubtract(B2, A0)), SelectZ);
uint32_t BDistIsZeroCR;
XMVECTOR BDistIsZero = XMVectorGreaterR(&BDistIsZeroCR, g_RayEpsilon, XMVectorAbs(BDist));
BDist = XMVectorSelect(BDist, Zero, BDistIsZero);
uint32_t BDistIsLessCR;
XMVECTOR BDistIsLess = XMVectorGreaterR(&BDistIsLessCR, Zero, BDist);
uint32_t BDistIsGreaterCR;
XMVECTOR BDistIsGreater = XMVectorGreaterR(&BDistIsGreaterCR, BDist, Zero);
if (XMComparisonAllTrue(BDistIsLessCR) || XMComparisonAllTrue(BDistIsGreaterCR))
return false;
XMVECTOR N2 = XMVector3Cross(XMVectorSubtract(B1, B0), XMVectorSubtract(B2, B0));
assert(!XMVector3Equal(N2, Zero));
XMVECTOR ADist = XMVector3Dot(N2, XMVectorSubtract(A0, B0));
ADist = XMVectorSelect(ADist, XMVector3Dot(N2, XMVectorSubtract(A1, B0)), SelectY);
ADist = XMVectorSelect(ADist, XMVector3Dot(N2, XMVectorSubtract(A2, B0)), SelectZ);
uint32_t ADistIsZeroCR;
XMVECTOR ADistIsZero = XMVectorGreaterR(&ADistIsZeroCR, g_RayEpsilon, XMVectorAbs(ADist));
ADist = XMVectorSelect(ADist, Zero, ADistIsZero);
uint32_t ADistIsLessCR;
XMVECTOR ADistIsLess = XMVectorGreaterR(&ADistIsLessCR, Zero, ADist);
uint32_t ADistIsGreaterCR;
XMVECTOR ADistIsGreater = XMVectorGreaterR(&ADistIsGreaterCR, ADist, Zero);
if (XMComparisonAllTrue(ADistIsLessCR) || XMComparisonAllTrue(ADistIsGreaterCR))
return false;
if (XMComparisonAllTrue(ADistIsZeroCR) || XMComparisonAllTrue(BDistIsZeroCR))
{
XMVECTOR Axis, Dist, MinDist;
Axis = XMVector3Cross(N1, XMVectorSubtract(A1, A0));
Dist = XMVector3Dot(Axis, A0);
MinDist = XMVector3Dot(B0, Axis);
MinDist = XMVectorMin(MinDist, XMVector3Dot(B1, Axis));
MinDist = XMVectorMin(MinDist, XMVector3Dot(B2, Axis));
if (XMVector4GreaterOrEqual(MinDist, Dist))
return false;
Axis = XMVector3Cross(N1, XMVectorSubtract(A2, A1));
Dist = XMVector3Dot(Axis, A1);
MinDist = XMVector3Dot(B0, Axis);
MinDist = XMVectorMin(MinDist, XMVector3Dot(B1, Axis));
MinDist = XMVectorMin(MinDist, XMVector3Dot(B2, Axis));
if (XMVector4GreaterOrEqual(MinDist, Dist))
return false;
Axis = XMVector3Cross(N1, XMVectorSubtract(A0, A2));
Dist = XMVector3Dot(Axis, A2);
MinDist = XMVector3Dot(B0, Axis);
MinDist = XMVectorMin(MinDist, XMVector3Dot(B1, Axis));
MinDist = XMVectorMin(MinDist, XMVector3Dot(B2, Axis));
if (XMVector4GreaterOrEqual(MinDist, Dist))
return false;
Axis = XMVector3Cross(N2, XMVectorSubtract(B1, B0));
Dist = XMVector3Dot(Axis, B0);
MinDist = XMVector3Dot(A0, Axis);
MinDist = XMVectorMin(MinDist, XMVector3Dot(A1, Axis));
MinDist = XMVectorMin(MinDist, XMVector3Dot(A2, Axis));
if (XMVector4GreaterOrEqual(MinDist, Dist))
return false;
Axis = XMVector3Cross(N2, XMVectorSubtract(B2, B1));
Dist = XMVector3Dot(Axis, B1);
MinDist = XMVector3Dot(A0, Axis);
MinDist = XMVectorMin(MinDist, XMVector3Dot(A1, Axis));
MinDist = XMVectorMin(MinDist, XMVector3Dot(A2, Axis));
if (XMVector4GreaterOrEqual(MinDist, Dist))
return false;
Axis = XMVector3Cross(N2, XMVectorSubtract(B0, B2));
Dist = XMVector3Dot(Axis, B2);
MinDist = XMVector3Dot(A0, Axis);
MinDist = XMVectorMin(MinDist, XMVector3Dot(A1, Axis));
MinDist = XMVectorMin(MinDist, XMVector3Dot(A2, Axis));
if (XMVector4GreaterOrEqual(MinDist, Dist))
return false;
return true;
}
XMVECTOR ADistIsLessEqual = XMVectorOrInt(ADistIsLess, ADistIsZero);
XMVECTOR ADistIsGreaterEqual = XMVectorOrInt(ADistIsGreater, ADistIsZero);
XMVECTOR AA0, AA1, AA2;
bool bPositiveA;
if (DirectX::MathInternal::XMVector3AllTrue(XMVectorSelect(ADistIsGreaterEqual, ADistIsLess, Select0111)) ||
DirectX::MathInternal::XMVector3AllTrue(XMVectorSelect(ADistIsGreater, ADistIsLessEqual, Select0111)))
{
AA0 = A0; AA1 = A1; AA2 = A2;
bPositiveA = true;
}
else if (DirectX::MathInternal::XMVector3AllTrue(XMVectorSelect(ADistIsLessEqual, ADistIsGreater, Select0111)) ||
DirectX::MathInternal::XMVector3AllTrue(XMVectorSelect(ADistIsLess, ADistIsGreaterEqual, Select0111)))
{
AA0 = A0; AA1 = A2; AA2 = A1;
bPositiveA = false;
}
else if (DirectX::MathInternal::XMVector3AllTrue(XMVectorSelect(ADistIsGreaterEqual, ADistIsLess, Select1011)) ||
DirectX::MathInternal::XMVector3AllTrue(XMVectorSelect(ADistIsGreater, ADistIsLessEqual, Select1011)))
{
AA0 = A1; AA1 = A2; AA2 = A0;
bPositiveA = true;
}
else if (DirectX::MathInternal::XMVector3AllTrue(XMVectorSelect(ADistIsLessEqual, ADistIsGreater, Select1011)) ||
DirectX::MathInternal::XMVector3AllTrue(XMVectorSelect(ADistIsLess, ADistIsGreaterEqual, Select1011)))
{
AA0 = A1; AA1 = A0; AA2 = A2;
bPositiveA = false;
}
else if (DirectX::MathInternal::XMVector3AllTrue(XMVectorSelect(ADistIsGreaterEqual, ADistIsLess, Select1101)) ||
DirectX::MathInternal::XMVector3AllTrue(XMVectorSelect(ADistIsGreater, ADistIsLessEqual, Select1101)))
{
AA0 = A2; AA1 = A0; AA2 = A1;
bPositiveA = true;
}
else if (DirectX::MathInternal::XMVector3AllTrue(XMVectorSelect(ADistIsLessEqual, ADistIsGreater, Select1101)) ||
DirectX::MathInternal::XMVector3AllTrue(XMVectorSelect(ADistIsLess, ADistIsGreaterEqual, Select1101)))
{
AA0 = A2; AA1 = A1; AA2 = A0;
bPositiveA = false;
}
else
{
assert(false);
return false;
}
XMVECTOR BDistIsLessEqual = XMVectorOrInt(BDistIsLess, BDistIsZero);
XMVECTOR BDistIsGreaterEqual = XMVectorOrInt(BDistIsGreater, BDistIsZero);
XMVECTOR BB0, BB1, BB2;
bool bPositiveB;
if (DirectX::MathInternal::XMVector3AllTrue(XMVectorSelect(BDistIsGreaterEqual, BDistIsLess, Select0111)) ||
DirectX::MathInternal::XMVector3AllTrue(XMVectorSelect(BDistIsGreater, BDistIsLessEqual, Select0111)))
{
BB0 = B0; BB1 = B1; BB2 = B2;
bPositiveB = true;
}
else if (DirectX::MathInternal::XMVector3AllTrue(XMVectorSelect(BDistIsLessEqual, BDistIsGreater, Select0111)) ||
DirectX::MathInternal::XMVector3AllTrue(XMVectorSelect(BDistIsLess, BDistIsGreaterEqual, Select0111)))
{
BB0 = B0; BB1 = B2; BB2 = B1;
bPositiveB = false;
}
else if (DirectX::MathInternal::XMVector3AllTrue(XMVectorSelect(BDistIsGreaterEqual, BDistIsLess, Select1011)) ||
DirectX::MathInternal::XMVector3AllTrue(XMVectorSelect(BDistIsGreater, BDistIsLessEqual, Select1011)))
{
BB0 = B1; BB1 = B2; BB2 = B0;
bPositiveB = true;
}
else if (DirectX::MathInternal::XMVector3AllTrue(XMVectorSelect(BDistIsLessEqual, BDistIsGreater, Select1011)) ||
DirectX::MathInternal::XMVector3AllTrue(XMVectorSelect(BDistIsLess, BDistIsGreaterEqual, Select1011)))
{
BB0 = B1; BB1 = B0; BB2 = B2;
bPositiveB = false;
}
else if (DirectX::MathInternal::XMVector3AllTrue(XMVectorSelect(BDistIsGreaterEqual, BDistIsLess, Select1101)) ||
DirectX::MathInternal::XMVector3AllTrue(XMVectorSelect(BDistIsGreater, BDistIsLessEqual, Select1101)))
{
BB0 = B2; BB1 = B0; BB2 = B1;
bPositiveB = true;
}
else if (DirectX::MathInternal::XMVector3AllTrue(XMVectorSelect(BDistIsLessEqual, BDistIsGreater, Select1101)) ||
DirectX::MathInternal::XMVector3AllTrue(XMVectorSelect(BDistIsLess, BDistIsGreaterEqual, Select1101)))
{
BB0 = B2; BB1 = B1; BB2 = B0;
bPositiveB = false;
}
else
{
assert(false);
return false;
}
XMVECTOR Delta0, Delta1;
if (bPositiveA ^ bPositiveB)
{
Delta0 = XMVectorSubtract(BB0, AA0);
Delta1 = XMVectorSubtract(AA0, BB0);
}
else
{
Delta0 = XMVectorSubtract(AA0, BB0);
Delta1 = XMVectorSubtract(BB0, AA0);
}
XMVECTOR Dist0 = XMVector3Dot(Delta0, XMVector3Cross(XMVectorSubtract(BB2, BB0), XMVectorSubtract(AA2, AA0)));
if (XMVector4Greater(Dist0, Zero))
return false;
XMVECTOR Dist1 = XMVector3Dot(Delta1, XMVector3Cross(XMVectorSubtract(BB1, BB0), XMVectorSubtract(AA1, AA0)));
if (XMVector4Greater(Dist1, Zero))
return false;
return true;
}
_Use_decl_annotations_
inline PlaneIntersectionType XM_CALLCONV Intersects(FXMVECTOR V0, FXMVECTOR V1, FXMVECTOR V2, GXMVECTOR Plane) noexcept
{
XMVECTOR One = XMVectorSplatOne();
assert(DirectX::MathInternal::XMPlaneIsUnit(Plane));
XMVECTOR TV0 = XMVectorInsert<0, 0, 0, 0, 1>(V0, One);
XMVECTOR TV1 = XMVectorInsert<0, 0, 0, 0, 1>(V1, One);
XMVECTOR TV2 = XMVectorInsert<0, 0, 0, 0, 1>(V2, One);
XMVECTOR Outside, Inside;
DirectX::MathInternal::FastIntersectTrianglePlane(TV0, TV1, TV2, Plane, Outside, Inside);
if (XMVector4EqualInt(Outside, XMVectorTrueInt()))
return FRONT;
if (XMVector4EqualInt(Inside, XMVectorTrueInt()))
return BACK;
return INTERSECTING;
}
_Use_decl_annotations_
inline ContainmentType XM_CALLCONV ContainedBy(
FXMVECTOR V0, FXMVECTOR V1, FXMVECTOR V2,
GXMVECTOR Plane0,
HXMVECTOR Plane1, HXMVECTOR Plane2,
CXMVECTOR Plane3, CXMVECTOR Plane4, CXMVECTOR Plane5) noexcept
{
XMVECTOR One = XMVectorSplatOne();
XMVECTOR TV0 = XMVectorInsert<0, 0, 0, 0, 1>(V0, One);
XMVECTOR TV1 = XMVectorInsert<0, 0, 0, 0, 1>(V1, One);
XMVECTOR TV2 = XMVectorInsert<0, 0, 0, 0, 1>(V2, One);
XMVECTOR Outside, Inside;
DirectX::MathInternal::FastIntersectTrianglePlane(TV0, TV1, TV2, Plane0, Outside, Inside);
XMVECTOR AnyOutside = Outside;
XMVECTOR AllInside = Inside;
DirectX::MathInternal::FastIntersectTrianglePlane(TV0, TV1, TV2, Plane1, Outside, Inside);
AnyOutside = XMVectorOrInt(AnyOutside, Outside);
AllInside = XMVectorAndInt(AllInside, Inside);
DirectX::MathInternal::FastIntersectTrianglePlane(TV0, TV1, TV2, Plane2, Outside, Inside);
AnyOutside = XMVectorOrInt(AnyOutside, Outside);
AllInside = XMVectorAndInt(AllInside, Inside);
DirectX::MathInternal::FastIntersectTrianglePlane(TV0, TV1, TV2, Plane3, Outside, Inside);
AnyOutside = XMVectorOrInt(AnyOutside, Outside);
AllInside = XMVectorAndInt(AllInside, Inside);
DirectX::MathInternal::FastIntersectTrianglePlane(TV0, TV1, TV2, Plane4, Outside, Inside);
AnyOutside = XMVectorOrInt(AnyOutside, Outside);
AllInside = XMVectorAndInt(AllInside, Inside);
DirectX::MathInternal::FastIntersectTrianglePlane(TV0, TV1, TV2, Plane5, Outside, Inside);
AnyOutside = XMVectorOrInt(AnyOutside, Outside);
AllInside = XMVectorAndInt(AllInside, Inside);
if (XMVector4EqualInt(AnyOutside, XMVectorTrueInt()))
return DISJOINT;
if (XMVector4EqualInt(AllInside, XMVectorTrueInt()))
return CONTAINS;
return INTERSECTS;
}
}