Make use of the new EnumMap class

This commit is contained in:
SirLynix
2023-05-30 12:32:37 +02:00
parent d914f41404
commit dfe6b2ddcf
46 changed files with 354 additions and 379 deletions

View File

@@ -15,6 +15,7 @@
#include <Nazara/Math/Plane.hpp>
#include <Nazara/Math/Sphere.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <NazaraUtils/EnumMap.hpp>
#include <array>
#include <string>
@@ -27,7 +28,7 @@ namespace Nz
{
public:
Frustum() = default;
explicit Frustum(const std::array<Plane<T>, FrustumPlaneCount>& planes);
explicit Frustum(const EnumMap<FrustumPlane, Plane<T>>& planes);
template<typename U> explicit Frustum(const Frustum<U>& frustum);
Frustum(const Frustum& frustum) = default;
~Frustum() = default;
@@ -63,7 +64,7 @@ namespace Nz
friend bool Unserialize(SerializationContext& context, Frustum<U>* frustum, TypeTag<Frustum<U>>);
private:
std::array<Plane<T>, FrustumPlaneCount> m_planes;
EnumMap<FrustumPlane, Plane<T>> m_planes;
};
using Frustumd = Frustum<double>;

View File

@@ -8,6 +8,7 @@
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Math/Algorithm.hpp>
#include <NazaraUtils/EnumMap.hpp>
#include <cstring>
#include <sstream>
#include <Nazara/Core/Debug.hpp>
@@ -30,7 +31,7 @@ namespace Nz
* \param planes Frustum of type U to convert to type T
*/
template<typename T>
Frustum<T>::Frustum(const std::array<Plane<T>, FrustumPlaneCount>& planes) :
Frustum<T>::Frustum(const EnumMap<FrustumPlane, Plane<T>>& planes) :
m_planes(planes)
{
}
@@ -44,8 +45,8 @@ namespace Nz
template<typename U>
Frustum<T>::Frustum(const Frustum<U>& frustum)
{
for (std::size_t i = 0; i < FrustumPlaneCount; ++i)
m_planes[i].Set(frustum.m_planes[i]);
for (auto&& [planeEnum, plane] : m_planes)
plane = Frustum(frustum.GetPlane(planeEnum));
}
/*!
@@ -129,9 +130,9 @@ namespace Nz
bool Frustum<T>::Contains(const Box<T>& box) const
{
// http://www.lighthouse3d.com/tutorials/view-frustum-culling/geometric-approach-testing-boxes-ii/
for (unsigned int i = 0; i < FrustumPlaneCount; i++)
for (const auto& plane : m_planes)
{
if (m_planes[i].Distance(box.GetPositiveVertex(m_planes[i].normal)) < T(0.0))
if (plane.Distance(box.GetPositiveVertex(plane.normal)) < T(0.0))
return false;
}
@@ -161,9 +162,9 @@ namespace Nz
template<typename T>
bool Frustum<T>::Contains(const Sphere<T>& sphere) const
{
for (unsigned int i = 0; i < FrustumPlaneCount; i++)
for (const auto& plane : m_planes)
{
if (m_planes[i].Distance(sphere.GetPosition()) < -sphere.radius)
if (plane.Distance(sphere.GetPosition()) < -sphere.radius)
return false;
}
@@ -180,9 +181,9 @@ namespace Nz
template<typename T>
bool Frustum<T>::Contains(const Vector3<T>& point) const
{
for (unsigned int i = 0; i < FrustumPlaneCount; ++i)
for (const auto& plane : m_planes)
{
if (m_planes[i].Distance(point) < T(0.0))
if (plane.Distance(point) < T(0.0))
return false;
}
@@ -200,12 +201,12 @@ namespace Nz
template<typename T>
bool Frustum<T>::Contains(const Vector3<T>* points, std::size_t pointCount) const
{
for (std::size_t i = 0; i < FrustumPlaneCount; ++i)
for (const auto& plane : m_planes)
{
std::size_t j;
for (j = 0; j < pointCount; j++ )
{
if (m_planes[i].Distance(points[j]) > T(0.0))
if (plane.Distance(points[j]) > T(0.0))
break;
}
@@ -228,8 +229,8 @@ namespace Nz
template<typename T>
const Plane<T>& Frustum<T>::GetPlane(FrustumPlane plane) const
{
NazaraAssert(UnderlyingCast(plane) < FrustumPlaneCount, "invalid plane");
return m_planes[UnderlyingCast(plane)];
NazaraAssert(plane <= FrustumPlane::Max, "invalid plane");
return m_planes[plane];
}
/*!
@@ -292,11 +293,11 @@ namespace Nz
// http://www.lighthouse3d.com/tutorials/view-frustum-culling/geometric-approach-testing-boxes-ii/
IntersectionSide side = IntersectionSide::Inside;
for (std::size_t i = 0; i < FrustumPlaneCount; i++)
for (const auto& plane : m_planes)
{
if (m_planes[i].Distance(box.GetPositiveVertex(m_planes[i].normal)) < T(0.0))
if (plane.Distance(box.GetPositiveVertex(plane.normal)) < T(0.0))
return IntersectionSide::Outside;
else if (m_planes[i].Distance(box.GetNegativeVertex(m_planes[i].normal)) < T(0.0))
else if (plane.Distance(box.GetNegativeVertex(plane.normal)) < T(0.0))
side = IntersectionSide::Intersecting;
}
@@ -329,9 +330,9 @@ namespace Nz
// http://www.lighthouse3d.com/tutorials/view-frustum-culling/geometric-approach-testing-points-and-spheres/
IntersectionSide side = IntersectionSide::Inside;
for (std::size_t i = 0; i < FrustumPlaneCount; i++)
for (const auto& plane : m_planes)
{
T distance = m_planes[i].Distance(sphere.GetPosition());
T distance = plane.Distance(sphere.GetPosition());
if (distance < -sphere.radius)
return IntersectionSide::Outside;
else if (distance < sphere.radius)
@@ -354,12 +355,12 @@ namespace Nz
{
std::size_t c = 0;
for (std::size_t i = 0; i < FrustumPlaneCount; ++i)
for (const auto& plane : m_planes)
{
std::size_t j;
for (j = 0; j < pointCount; j++ )
{
if (m_planes[i].Distance(points[j]) > T(0.0))
if (plane.Distance(points[j]) > T(0.0))
break;
}
@@ -418,25 +419,26 @@ namespace Nz
Vector3<T> fc = eye + f * zFar;
// Computing the frustum
std::array<Vector3<T>, BoxCornerCount> corners;
corners[UnderlyingCast(BoxCorner::FarLeftBottom)] = fc - u * farH - s * farW;
corners[UnderlyingCast(BoxCorner::FarLeftTop)] = fc + u * farH - s * farW;
corners[UnderlyingCast(BoxCorner::FarRightTop)] = fc + u * farH + s * farW;
corners[UnderlyingCast(BoxCorner::FarRightBottom)] = fc - u * farH + s * farW;
EnumMap<BoxCorner, Vector3<T>> corners;
corners[BoxCorner::FarLeftBottom] = fc - u * farH - s * farW;
corners[BoxCorner::FarLeftTop] = fc + u * farH - s * farW;
corners[BoxCorner::FarRightTop] = fc + u * farH + s * farW;
corners[BoxCorner::FarRightBottom] = fc - u * farH + s * farW;
corners[UnderlyingCast(BoxCorner::NearLeftBottom)] = nc - u * nearH - s * nearW;
corners[UnderlyingCast(BoxCorner::NearLeftTop)] = nc + u * nearH - s * nearW;
corners[UnderlyingCast(BoxCorner::NearRightTop)] = nc + u * nearH + s * nearW;
corners[UnderlyingCast(BoxCorner::NearRightBottom)] = nc - u * nearH + s * nearW;
corners[BoxCorner::NearLeftBottom] = nc - u * nearH - s * nearW;
corners[BoxCorner::NearLeftTop] = nc + u * nearH - s * nearW;
corners[BoxCorner::NearRightTop] = nc + u * nearH + s * nearW;
corners[BoxCorner::NearRightBottom] = nc - u * nearH + s * nearW;
// Construction of frustum's planes
std::array<Plane<T>, FrustumPlaneCount> planes;
planes[UnderlyingCast(FrustumPlane::Bottom)] = Plane(corners[UnderlyingCast(BoxCorner::NearLeftBottom)], corners[UnderlyingCast(BoxCorner::NearRightBottom)], corners[UnderlyingCast(BoxCorner::FarRightBottom)]);
planes[UnderlyingCast(FrustumPlane::Far)] = Plane(corners[UnderlyingCast(BoxCorner::FarRightTop)], corners[UnderlyingCast(BoxCorner::FarLeftTop)], corners[UnderlyingCast(BoxCorner::FarLeftBottom)]);
planes[UnderlyingCast(FrustumPlane::Left)] = Plane(corners[UnderlyingCast(BoxCorner::NearLeftTop)], corners[UnderlyingCast(BoxCorner::NearLeftBottom)], corners[UnderlyingCast(BoxCorner::FarLeftBottom)]);
planes[UnderlyingCast(FrustumPlane::Near)] = Plane(corners[UnderlyingCast(BoxCorner::NearLeftTop)], corners[UnderlyingCast(BoxCorner::NearRightTop)], corners[UnderlyingCast(BoxCorner::NearRightBottom)]);
planes[UnderlyingCast(FrustumPlane::Right)] = Plane(corners[UnderlyingCast(BoxCorner::NearRightBottom)], corners[UnderlyingCast(BoxCorner::NearRightTop)], corners[UnderlyingCast(BoxCorner::FarRightBottom)]);
planes[UnderlyingCast(FrustumPlane::Top)] = Plane(corners[UnderlyingCast(BoxCorner::NearRightTop)], corners[UnderlyingCast(BoxCorner::NearLeftTop)], corners[UnderlyingCast(BoxCorner::FarLeftTop)]);
EnumMap<FrustumPlane, Plane<T>> planes;
planes[FrustumPlane::Bottom] = Plane(corners[BoxCorner::NearLeftBottom], corners[BoxCorner::NearRightBottom], corners[BoxCorner::FarRightBottom]);
planes[FrustumPlane::Far] = Plane(corners[BoxCorner::FarRightTop], corners[BoxCorner::FarLeftTop], corners[BoxCorner::FarLeftBottom]);
planes[FrustumPlane::Left] = Plane(corners[BoxCorner::NearLeftTop], corners[BoxCorner::NearLeftBottom], corners[BoxCorner::FarLeftBottom]);
planes[FrustumPlane::Near] = Plane(corners[BoxCorner::NearLeftTop], corners[BoxCorner::NearRightTop], corners[BoxCorner::NearRightBottom]);
planes[FrustumPlane::Right] = Plane(corners[BoxCorner::NearRightBottom], corners[BoxCorner::NearRightTop], corners[BoxCorner::FarRightBottom]);
planes[FrustumPlane::Top] = Plane(corners[BoxCorner::NearRightTop], corners[BoxCorner::NearLeftTop], corners[BoxCorner::FarLeftTop]);
return Frustum(planes);
}
@@ -454,7 +456,7 @@ namespace Nz
T plane[4];
T invLength;
std::array<Plane<T>, FrustumPlaneCount> planes;
EnumMap<FrustumPlane, Plane<T>> planes;
// Extract the numbers for the RIGHT plane
plane[0] = viewProjMatrix[3] - viewProjMatrix[0];
@@ -469,7 +471,7 @@ namespace Nz
plane[2] *= invLength;
plane[3] *= -invLength;
planes[UnderlyingCast(FrustumPlane::Right)] = Plane<T>(plane);
planes[FrustumPlane::Right] = Plane<T>(plane);
// Extract the numbers for the LEFT plane
plane[0] = viewProjMatrix[3] + viewProjMatrix[0];
@@ -484,7 +486,7 @@ namespace Nz
plane[2] *= invLength;
plane[3] *= -invLength;
planes[UnderlyingCast(FrustumPlane::Left)] = Plane<T>(plane);
planes[FrustumPlane::Left] = Plane<T>(plane);
// Extract the BOTTOM plane
plane[0] = viewProjMatrix[3] + viewProjMatrix[1];
@@ -499,7 +501,7 @@ namespace Nz
plane[2] *= invLength;
plane[3] *= -invLength;
planes[UnderlyingCast(FrustumPlane::Bottom)] = Plane<T>(plane);
planes[FrustumPlane::Bottom] = Plane<T>(plane);
// Extract the TOP plane
plane[0] = viewProjMatrix[3] - viewProjMatrix[1];
@@ -514,7 +516,7 @@ namespace Nz
plane[2] *= invLength;
plane[3] *= -invLength;
planes[UnderlyingCast(FrustumPlane::Top)] = Plane<T>(plane);
planes[FrustumPlane::Top] = Plane<T>(plane);
// Extract the FAR plane
plane[0] = viewProjMatrix[3] - viewProjMatrix[2];
@@ -529,7 +531,7 @@ namespace Nz
plane[2] *= invLength;
plane[3] *= -invLength;
planes[UnderlyingCast(FrustumPlane::Far)] = Plane<T>(plane);
planes[FrustumPlane::Far] = Plane<T>(plane);
// Extract the NEAR plane
plane[0] = viewProjMatrix[3] + viewProjMatrix[2];
@@ -544,7 +546,7 @@ namespace Nz
plane[2] *= invLength;
plane[3] *= -invLength;
planes[UnderlyingCast(FrustumPlane::Near)] = Plane<T>(plane);
planes[FrustumPlane::Near] = Plane<T>(plane);
return Frustum(planes);
}
@@ -559,9 +561,9 @@ namespace Nz
template<typename T>
bool Serialize(SerializationContext& context, const Frustum<T>& frustum, TypeTag<Frustum<T>>)
{
for (unsigned int i = 0; i < FrustumPlaneCount; ++i)
for (const auto& plane : frustum.m_planes)
{
if (!Serialize(context, frustum.m_planes[i]))
if (!Serialize(context, plane))
return false;
}
@@ -578,9 +580,9 @@ namespace Nz
template<typename T>
bool Unserialize(SerializationContext& context, Frustum<T>* frustum, TypeTag<Frustum<T>>)
{
for (unsigned int i = 0; i < FrustumPlaneCount; ++i)
for (auto& plane : frustum->m_planes)
{
if (!Unserialize(context, &frustum->m_planes[i]))
if (!Unserialize(context, &plane))
return false;
}

View File

@@ -11,6 +11,7 @@
#include <Nazara/Math/Enums.hpp>
#include <Nazara/Math/Matrix4.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <NazaraUtils/EnumMap.hpp>
#include <string>
namespace Nz
@@ -53,7 +54,7 @@ namespace Nz
Box<T> localBox;
private:
Vector3<T> m_corners[BoxCornerCount];
EnumMap<BoxCorner, Vector3<T>> m_corners;
};
using OrientedBoxd = OrientedBox<double>;

View File

@@ -43,8 +43,8 @@ namespace Nz
OrientedBox<T>::OrientedBox(const OrientedBox<U>& orientedBox) :
localBox(orientedBox.localBox)
{
for (unsigned int i = 0; i < BoxCornerCount; ++i)
m_corners[i] = Vector3<T>(orientedBox(i));
for (auto&& [cornerEnum, corner] : m_corners.iter_kv())
corner = Vector3<T>(orientedBox.GetCorner(cornerEnum));
}
/*!
@@ -59,23 +59,14 @@ namespace Nz
template<typename T>
const Vector3<T>& OrientedBox<T>::GetCorner(BoxCorner corner) const
{
#ifdef NAZARA_DEBUG
if (UnderlyingCast(corner) > BoxCornerCount)
{
NazaraError("Corner not handled (0x" + NumberToString(UnderlyingCast(corner), 16) + ')');
static Vector3<T> dummy;
return dummy;
}
#endif
return m_corners[UnderlyingCast(corner)];
NazaraAssert(corner <= BoxCorner::Max, "invalid corner");
return m_corners[corner];
}
template<typename T>
const Vector3<T>* OrientedBox<T>::GetCorners() const
{
return &m_corners[0];
return &m_corners.front();
}
/*!
@@ -112,8 +103,8 @@ namespace Nz
template<typename T>
void OrientedBox<T>::Update(const Matrix4<T>& transformMatrix)
{
for (unsigned int i = 0; i < BoxCornerCount; ++i)
m_corners[i] = transformMatrix.Transform(localBox.GetCorner(static_cast<BoxCorner>(i)));
for (auto&& [corner, pos] : m_corners.iter_kv())
pos = transformMatrix.Transform(localBox.GetCorner(corner));
}
/*!
@@ -125,8 +116,8 @@ namespace Nz
template<typename T>
void OrientedBox<T>::Update(const Vector3<T>& translation)
{
for (unsigned int i = 0; i < BoxCornerCount; ++i)
m_corners[i] = localBox.GetCorner(static_cast<BoxCorner>(i)) + translation;
for (auto&& [corner, pos] : m_corners.iter_kv())
pos = localBox.GetCorner(corner) + translation;
}
/*!
@@ -140,18 +131,8 @@ namespace Nz
template<typename T>
Vector3<T>& OrientedBox<T>::operator()(unsigned int i)
{
#if NAZARA_MATH_SAFE
if (i >= BoxCornerCount)
{
std::ostringstream ss;
ss << "Index out of range: (" << i << " >= " << BoxCornerCount << ")";
NazaraError(ss.str());
throw std::out_of_range(ss.str());
}
#endif
return m_corners[i];
NazaraAssert(i < m_corners.size(), "corner out of range");
return m_corners[static_cast<BoxCorner>(i)];
}
/*!
@@ -165,18 +146,8 @@ namespace Nz
template<typename T>
const Vector3<T>& OrientedBox<T>::operator()(unsigned int i) const
{
#if NAZARA_MATH_SAFE
if (i >= BoxCornerCount)
{
std::ostringstream ss;
ss << "Index out of range: (" << i << " >= " << BoxCornerCount << ")";
NazaraError(ss.str());
throw std::out_of_range(ss.str());
}
#endif
return m_corners[i];
NazaraAssert(i < m_corners.size(), "corner out of range");
return m_corners[static_cast<BoxCorner>(i)];
}
/*!
@@ -282,16 +253,16 @@ namespace Nz
* \param orientedBox The orientedBox to output
*/
template<typename T>
std::ostream& operator<<(std::ostream& out, const Nz::OrientedBox<T>& orientedBox)
std::ostream& operator<<(std::ostream& out, const OrientedBox<T>& orientedBox)
{
return out << "OrientedBox(FLB: " << orientedBox.GetCorner(Nz::BoxCorner::FarLeftBottom) << ",\n"
<< " FLT: " << orientedBox.GetCorner(Nz::BoxCorner::FarLeftTop) << ",\n"
<< " FRB: " << orientedBox.GetCorner(Nz::BoxCorner::FarRightBottom) << ",\n"
<< " FRT: " << orientedBox.GetCorner(Nz::BoxCorner::FarRightTop) << ",\n"
<< " NLB: " << orientedBox.GetCorner(Nz::BoxCorner::NearLeftBottom) << ",\n"
<< " NLT: " << orientedBox.GetCorner(Nz::BoxCorner::NearLeftTop) << ",\n"
<< " NRB: " << orientedBox.GetCorner(Nz::BoxCorner::NearRightBottom) << ",\n"
<< " NRT: " << orientedBox.GetCorner(Nz::BoxCorner::NearRightTop) << ")\n";
return out << "OrientedBox(FLB: " << orientedBox.GetCorner(BoxCorner::FarLeftBottom) << ",\n"
<< " FLT: " << orientedBox.GetCorner(BoxCorner::FarLeftTop) << ",\n"
<< " FRB: " << orientedBox.GetCorner(BoxCorner::FarRightBottom) << ",\n"
<< " FRT: " << orientedBox.GetCorner(BoxCorner::FarRightTop) << ",\n"
<< " NLB: " << orientedBox.GetCorner(BoxCorner::NearLeftBottom) << ",\n"
<< " NLT: " << orientedBox.GetCorner(BoxCorner::NearLeftTop) << ",\n"
<< " NRB: " << orientedBox.GetCorner(BoxCorner::NearRightBottom) << ",\n"
<< " NRT: " << orientedBox.GetCorner(BoxCorner::NearRightTop) << ")\n";
}
}