From ee208ddc73f92410ca7c6987c46d54d5191af4aa Mon Sep 17 00:00:00 2001 From: Joseph Lenox Date: Thu, 12 Jul 2018 23:13:44 -0500 Subject: [PATCH] Added templated iterator-based constructor for TriangleMesh and tests for it. --- src/test/libslic3r/test_trianglemesh.cpp | 48 ++++++++++++++++++++++++ xs/src/libslic3r/TriangleMesh.hpp | 4 ++ 2 files changed, 52 insertions(+) diff --git a/src/test/libslic3r/test_trianglemesh.cpp b/src/test/libslic3r/test_trianglemesh.cpp index 8f10616b8..b8139da38 100644 --- a/src/test/libslic3r/test_trianglemesh.cpp +++ b/src/test/libslic3r/test_trianglemesh.cpp @@ -9,6 +9,54 @@ using namespace Slic3r; SCENARIO( "TriangleMesh: Basic mesh statistics") { + GIVEN( "A 20mm cube, built from constexpr std::array" ) { + constexpr std::array vertices { Pointf3(20,20,0), Pointf3(20,0,0), Pointf3(0,0,0), Pointf3(0,20,0), Pointf3(20,20,20), Pointf3(0,20,20), Pointf3(0,0,20), Pointf3(20,0,20) }; + constexpr std::array facets { Point3(0,1,2), Point3(0,2,3), Point3(4,5,6), Point3(4,6,7), Point3(0,4,7), Point3(0,7,1), Point3(1,7,6), Point3(1,6,2), Point3(2,6,5), Point3(2,5,3), Point3(4,0,3), Point3(4,3,5) }; + auto cube {TriangleMesh(vertices.cbegin(), vertices.cend(), facets.cbegin(), facets.cend())}; + cube.repair(); + + THEN( "Volume is appropriate for 20mm square cube.") { + REQUIRE(abs(cube.volume() - 20.0*20.0*20.0) < 1e-2); + } + + THEN( "Vertices array matches input.") { + for (auto i = 0U; i < cube.vertices().size(); i++) { + REQUIRE(cube.vertices().at(i) == vertices.at(i)); + } + for (auto i = 0U; i < vertices.size(); i++) { + REQUIRE(vertices.at(i) == cube.vertices().at(i)); + } + } + THEN( "Vertex count matches vertex array size.") { + REQUIRE(cube.facets_count() == facets.size()); + } + + THEN( "Facet array matches input.") { + for (auto i = 0U; i < cube.facets().size(); i++) { + REQUIRE(cube.facets().at(i) == facets.at(i)); + } + + for (auto i = 0U; i < facets.size(); i++) { + REQUIRE(facets.at(i) == cube.facets().at(i)); + } + } + THEN( "Facet count matches facet array size.") { + REQUIRE(cube.facets_count() == facets.size()); + } + + THEN( "Number of normals is equal to the number of facets.") { + REQUIRE(cube.normals().size() == facets.size()); + } + + THEN( "center() returns the center of the object.") { + REQUIRE(cube.center() == Pointf3(10.0,10.0,10.0)); + } + + THEN( "Size of cube is (20,20,20)") { + REQUIRE(cube.size() == Pointf3(20,20,20)); + } + + } GIVEN( "A 20mm cube with one corner on the origin") { const Pointf3s vertices { Pointf3(20,20,0), Pointf3(20,0,0), Pointf3(0,0,0), Pointf3(0,20,0), Pointf3(20,20,20), Pointf3(0,20,20), Pointf3(0,0,20), Pointf3(20,0,20) }; const Point3s facets { Point3(0,1,2), Point3(0,2,3), Point3(4,5,6), Point3(4,6,7), Point3(0,4,7), Point3(0,7,1), Point3(1,7,6), Point3(1,6,2), Point3(2,6,5), Point3(2,5,3), Point3(4,0,3), Point3(4,3,5) }; diff --git a/xs/src/libslic3r/TriangleMesh.hpp b/xs/src/libslic3r/TriangleMesh.hpp index cc4ab6d80..87d842d00 100644 --- a/xs/src/libslic3r/TriangleMesh.hpp +++ b/xs/src/libslic3r/TriangleMesh.hpp @@ -37,6 +37,10 @@ class TriangleMesh public: TriangleMesh(); TriangleMesh(const Pointf3s &points, const std::vector &facets); + + /// Templated constructor that takes a pair of iterators to build a TriangleMesh. + template + TriangleMesh(Iter_vert p_begin, Iter_vert p_end, Iter_fac f_begin, Iter_fac f_end) : TriangleMesh(Pointf3s(p_begin, p_end), Point3s(f_begin, f_end)) {} TriangleMesh(const TriangleMesh &other); TriangleMesh& operator= (TriangleMesh other); void swap(TriangleMesh &other);