From 3ba2dbed57a201f50152591a09fc24084afd8fa0 Mon Sep 17 00:00:00 2001 From: Razakhel Date: Sat, 2 Sep 2023 15:34:30 +0200 Subject: [PATCH] [Tests/Data/Mesh] Added a basic Mesh unit test - This checks the Mesh's basic behavior - Computing tangents on an empty mesh is now a no-op --- include/RaZ/Data/Mesh.hpp | 2 +- src/RaZ/Data/Mesh.cpp | 3 +++ tests/src/RaZ/Data/Mesh.cpp | 30 ++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/include/RaZ/Data/Mesh.hpp b/include/RaZ/Data/Mesh.hpp index 806ed9c2..be04fda7 100644 --- a/include/RaZ/Data/Mesh.hpp +++ b/include/RaZ/Data/Mesh.hpp @@ -72,7 +72,7 @@ class Mesh : public Component { void createIcosphere(const Sphere& sphere, uint32_t subdivCount); std::vector m_submeshes {}; - AABB m_boundingBox = AABB(Vec3f(), Vec3f()); + AABB m_boundingBox = AABB(Vec3f(0.f), Vec3f(0.f)); }; } // namespace Raz diff --git a/src/RaZ/Data/Mesh.cpp b/src/RaZ/Data/Mesh.cpp index c23d8bb0..b417846f 100644 --- a/src/RaZ/Data/Mesh.cpp +++ b/src/RaZ/Data/Mesh.cpp @@ -250,6 +250,9 @@ const AABB& Mesh::computeBoundingBox() { } void Mesh::computeTangents() { + if (m_submeshes.empty()) + return; + Threading::parallelize(m_submeshes, [] (const auto& range) noexcept { for (Submesh& submesh : range) { for (std::size_t i = 0; i < submesh.getTriangleIndexCount(); i += 3) { diff --git a/tests/src/RaZ/Data/Mesh.cpp b/tests/src/RaZ/Data/Mesh.cpp index 0002af7b..f3729ca7 100644 --- a/tests/src/RaZ/Data/Mesh.cpp +++ b/tests/src/RaZ/Data/Mesh.cpp @@ -2,6 +2,36 @@ #include "RaZ/Data/Mesh.hpp" +TEST_CASE("Mesh basic") { + Raz::Mesh mesh; + CHECK(mesh.getSubmeshes().empty()); + CHECK(mesh.getBoundingBox() == Raz::AABB(Raz::Vec3f(0.f), Raz::Vec3f(0.f))); + CHECK(mesh.recoverVertexCount() == 0); + CHECK(mesh.recoverTriangleCount() == 0); + + CHECK_NOTHROW(mesh.computeTangents()); // Attempting to compute tangents for an empty mesh does nothing + + const Raz::AABB& boundingBox = mesh.computeBoundingBox(); + CHECK(boundingBox == Raz::AABB(Raz::Vec3f(std::numeric_limits::max()), Raz::Vec3f(std::numeric_limits::lowest()))); + CHECK(boundingBox == mesh.getBoundingBox()); + + Raz::Submesh& submesh = mesh.addSubmesh(); + CHECK(mesh.getSubmeshes().size() == 1); + + submesh.getVertices() = { + Raz::Vertex{ Raz::Vec3f(-1.f) }, + Raz::Vertex{ Raz::Vec3f(0.f) }, + Raz::Vertex{ Raz::Vec3f(1.f) } + }; + submesh.getTriangleIndices() = { 0, 1, 2 }; + CHECK(mesh.recoverVertexCount() == 3); + CHECK(mesh.recoverTriangleCount() == 1); + + mesh.computeBoundingBox(); + CHECK(boundingBox == Raz::AABB(Raz::Vec3f(-1.f), Raz::Vec3f(1.f))); + CHECK(boundingBox == mesh.getBoundingBox()); +} + TEST_CASE("Mesh plane") { const Raz::Plane plane(1.5f, Raz::Axis::Y);