diff --git a/src/Magnum/MeshTools/Subdivide.h b/src/Magnum/MeshTools/Subdivide.h index 45139b360..5cfc614f4 100644 --- a/src/Magnum/MeshTools/Subdivide.h +++ b/src/Magnum/MeshTools/Subdivide.h @@ -30,36 +30,10 @@ */ #include -#include +#include namespace Magnum { namespace MeshTools { -namespace Implementation { - -template class Subdivide { - public: - Subdivide(std::vector& indices, std::vector& vertices): indices(indices), vertices(vertices) {} - - void operator()(Interpolator interpolator); - - private: - std::vector& indices; - std::vector& vertices; - - UnsignedInt addVertex(const Vertex& v) { - vertices.push_back(v); - return vertices.size()-1; - } - - void addFace(UnsignedInt first, UnsignedInt second, UnsignedInt third) { - indices.push_back(first); - indices.push_back(second); - indices.push_back(third); - } -}; - -} - /** @brief Subdivide the mesh @tparam Vertex Vertex data type @@ -72,13 +46,7 @@ template class Subdivide { Goes through all triangle faces and subdivides them into four new. Removing duplicate vertices in the mesh is up to user. */ -template inline void subdivide(std::vector& indices, std::vector& vertices, Interpolator interpolator) { - Implementation::Subdivide(indices, vertices)(interpolator); -} - -namespace Implementation { - -template void Subdivide::operator()(Interpolator interpolator) { +template void subdivide(std::vector& indices, std::vector& vertices, Interpolator interpolator) { CORRADE_ASSERT(!(indices.size()%3), "MeshTools::subdivide(): index count is not divisible by 3!", ); std::size_t indexCount = indices.size(); @@ -88,8 +56,9 @@ template void Subdivide: for(std::size_t i = 0; i != indexCount; i += 3) { /* Interpolate each side */ UnsignedInt newVertices[3]; - for(int j = 0; j != 3; ++j) - newVertices[j] = addVertex(interpolator(vertices[indices[i+j]], vertices[indices[i+(j+1)%3]])); + for(int j = 0; j != 3; ++j) { + newVertices[j] = vertices.size(); vertices.push_back(interpolator(vertices[indices[i+j]], vertices[indices[i+(j+1)%3]])); + } /* Add three new faces (0, 1, 3) and update original (2) @@ -104,16 +73,16 @@ template void Subdivide: / \ / \ orig 1 ----- new 1 ---- orig 2 */ - addFace(indices[i], newVertices[0], newVertices[2]); - addFace(newVertices[0], indices[i+1], newVertices[1]); - addFace(newVertices[2], newVertices[1], indices[i+2]); + indices.insert(indices.end(), { + indices[i], newVertices[0], newVertices[2], + newVertices[0], indices[i+1], newVertices[1], + newVertices[2], newVertices[1], indices[i+2] + }); for(std::size_t j = 0; j != 3; ++j) indices[i+j] = newVertices[j]; } } -} - }} #endif