From 47760294fc8c104489dde1e280f8f00c4cf4dc39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 6 Jan 2020 19:43:28 +0100 Subject: [PATCH] MeshTools: retroactively fire the 2010 edition of me. Reason: a serious case of overengineerism. What's the point of those useless "functor" classes, anyway? --- src/Magnum/MeshTools/CMakeLists.txt | 6 +- src/Magnum/MeshTools/Implementation/Tipsify.h | 67 +++++++++++++++++++ src/Magnum/MeshTools/Test/TipsifyTest.cpp | 3 +- src/Magnum/MeshTools/Tipsify.cpp | 39 ++--------- src/Magnum/MeshTools/Tipsify.h | 28 +------- 5 files changed, 81 insertions(+), 62 deletions(-) create mode 100644 src/Magnum/MeshTools/Implementation/Tipsify.h diff --git a/src/Magnum/MeshTools/CMakeLists.txt b/src/Magnum/MeshTools/CMakeLists.txt index a6d6b4d28..7c1c65d7d 100644 --- a/src/Magnum/MeshTools/CMakeLists.txt +++ b/src/Magnum/MeshTools/CMakeLists.txt @@ -48,6 +48,9 @@ set(MagnumMeshTools_HEADERS visibility.h) +set(MagnumMeshTools_INTERNAL_HEADERS + Implementation/Tipsify.h) + if(BUILD_DEPRECATED) list(APPEND MagnumMeshTools_HEADERS GenerateFlatNormals.h) endif() @@ -65,7 +68,8 @@ endif() # Objects shared between main and test library add_library(MagnumMeshToolsObjects OBJECT ${MagnumMeshTools_SRCS} - ${MagnumMeshTools_HEADERS}) + ${MagnumMeshTools_HEADERS} + ${MagnumMeshTools_INTERNAL_HEADERS}) target_include_directories(MagnumMeshToolsObjects PUBLIC $) if(NOT BUILD_STATIC) target_compile_definitions(MagnumMeshToolsObjects PRIVATE "MagnumMeshToolsObjects_EXPORTS") diff --git a/src/Magnum/MeshTools/Implementation/Tipsify.h b/src/Magnum/MeshTools/Implementation/Tipsify.h new file mode 100644 index 000000000..63def0dcd --- /dev/null +++ b/src/Magnum/MeshTools/Implementation/Tipsify.h @@ -0,0 +1,67 @@ +#ifndef Magnum_MeshTools_Implementation_Tipsify_h +#define Magnum_MeshTools_Implementation_Tipsify_h +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 + Vladimír Vondruš + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. +*/ + +#include + +#include "Magnum/Magnum.h" + +namespace Magnum { namespace MeshTools { namespace Implementation { namespace { + +/* Vertex-triangle adjacency. Computes count and indices of adjacent triangles + for each vertex (used internally by tipsify()) */ +void buildAdjacency(const std::vector& indices, const UnsignedInt vertexCount, std::vector& liveTriangleCount, std::vector& neighborOffset, std::vector& neighbors) { + /* How many times is each vertex referenced == count of neighboring + triangles for each vertex */ + liveTriangleCount.clear(); + liveTriangleCount.resize(vertexCount); + for(std::size_t i = 0; i != indices.size(); ++i) + ++liveTriangleCount[indices[i]]; + + /* Building offset array from counts. Neighbors for i-th vertex will at + the end be in interval neighbors[neighborOffset[i]] ; + neighbors[neighborOffset[i+1]]. Currently the values are shifted to + right, because the next loop will shift them back left. */ + neighborOffset.clear(); + neighborOffset.reserve(vertexCount+1); + neighborOffset.push_back(0); + UnsignedInt sum = 0; + for(std::size_t i = 0; i != vertexCount; ++i) { + neighborOffset.push_back(sum); + sum += liveTriangleCount[i]; + } + + /* Array of neighbors, using (and changing) neighborOffset array for + positioning */ + neighbors.clear(); + neighbors.resize(sum); + for(std::size_t i = 0; i != indices.size(); ++i) + neighbors[neighborOffset[indices[i]+1]++] = i/3; +} + +}}}} + +#endif diff --git a/src/Magnum/MeshTools/Test/TipsifyTest.cpp b/src/Magnum/MeshTools/Test/TipsifyTest.cpp index 2bb86f09c..92ca9411d 100644 --- a/src/Magnum/MeshTools/Test/TipsifyTest.cpp +++ b/src/Magnum/MeshTools/Test/TipsifyTest.cpp @@ -27,6 +27,7 @@ #include "Magnum/Magnum.h" #include "Magnum/MeshTools/Tipsify.h" +#include "Magnum/MeshTools/Implementation/Tipsify.h" namespace Magnum { namespace MeshTools { namespace Test { namespace { @@ -87,7 +88,7 @@ TipsifyTest::TipsifyTest() { void TipsifyTest::buildAdjacency() { std::vector indices = Indices; std::vector liveTriangleCount, neighborOffset, neighbors; - Implementation::Tipsify(indices, VertexCount).buildAdjacency(liveTriangleCount, neighborOffset, neighbors); + Implementation::buildAdjacency(indices, VertexCount, liveTriangleCount, neighborOffset, neighbors); CORRADE_COMPARE(liveTriangleCount, (std::vector{ 1, 3, 3, 2, diff --git a/src/Magnum/MeshTools/Tipsify.cpp b/src/Magnum/MeshTools/Tipsify.cpp index 415c712c9..84086bbe3 100644 --- a/src/Magnum/MeshTools/Tipsify.cpp +++ b/src/Magnum/MeshTools/Tipsify.cpp @@ -27,12 +27,14 @@ #include -namespace Magnum { namespace MeshTools { namespace Implementation { +#include "Magnum/MeshTools/Implementation/Tipsify.h" -void Tipsify::operator()(std::size_t cacheSize) { +namespace Magnum { namespace MeshTools { + +void tipsify(std::vector& indices, const UnsignedInt vertexCount, const std::size_t cacheSize) { /* Neighboring triangles for each vertex, per-vertex live triangle count */ std::vector liveTriangleCount, neighborPosition, neighbors; - buildAdjacency(liveTriangleCount, neighborPosition, neighbors); + Implementation::buildAdjacency(indices, vertexCount, liveTriangleCount, neighborPosition, neighbors); /* Global time, per-vertex caching timestamps, per-triangle emmited flag */ UnsignedInt time = cacheSize+1; @@ -129,33 +131,4 @@ void Tipsify::operator()(std::size_t cacheSize) { swap(indices, outputIndices); } -void Tipsify::buildAdjacency(std::vector& liveTriangleCount, std::vector& neighborOffset, std::vector& neighbors) const { - /* How many times is each vertex referenced == count of neighboring - triangles for each vertex */ - liveTriangleCount.clear(); - liveTriangleCount.resize(vertexCount); - for(std::size_t i = 0; i != indices.size(); ++i) - ++liveTriangleCount[indices[i]]; - - /* Building offset array from counts. Neighbors for i-th vertex will at - the end be in interval neighbors[neighborOffset[i]] ; - neighbors[neighborOffset[i+1]]. Currently the values are shifted to - right, because the next loop will shift them back left. */ - neighborOffset.clear(); - neighborOffset.reserve(vertexCount+1); - neighborOffset.push_back(0); - UnsignedInt sum = 0; - for(std::size_t i = 0; i != vertexCount; ++i) { - neighborOffset.push_back(sum); - sum += liveTriangleCount[i]; - } - - /* Array of neighbors, using (and changing) neighborOffset array for - positioning */ - neighbors.clear(); - neighbors.resize(sum); - for(std::size_t i = 0; i != indices.size(); ++i) - neighbors[neighborOffset[indices[i]+1]++] = i/3; -} - -}}} +}} diff --git a/src/Magnum/MeshTools/Tipsify.h b/src/Magnum/MeshTools/Tipsify.h index 4a6f19691..4855d175f 100644 --- a/src/Magnum/MeshTools/Tipsify.h +++ b/src/Magnum/MeshTools/Tipsify.h @@ -36,30 +36,6 @@ namespace Magnum { namespace MeshTools { -namespace Implementation { - -class MAGNUM_MESHTOOLS_EXPORT Tipsify { - public: - Tipsify(std::vector& indices, UnsignedInt vertexCount): indices(indices), vertexCount(vertexCount) {} - - void operator()(std::size_t cacheSize); - - /** - * @brief Build vertex-triangle adjacency - * - * Computes count and indices of adjacent triangles for each vertex - * (used internally). - * @todo Export only for unit test, hide otherwise - */ - void buildAdjacency(std::vector& liveTriangleCount, std::vector& neighborOffset, std::vector& neighbors) const; - - private: - std::vector& indices; - const UnsignedInt vertexCount; -}; - -} - /** @brief Tipsify the mesh @param[in,out] indices Indices array to operate on @@ -73,9 +49,7 @@ for Vertex Locality and Reduced Overdraw, SIGGRAPH 2007, http://gfx.cs.princeton.edu/pubs/Sander_2007_%3ETR/index.php*. @todo Ability to compute vertex count automatically */ -inline void tipsify(std::vector& indices, UnsignedInt vertexCount, std::size_t cacheSize) { - Implementation::Tipsify(indices, vertexCount)(cacheSize); -} +MAGNUM_MESHTOOLS_EXPORT void tipsify(std::vector& indices, UnsignedInt vertexCount, std::size_t cacheSize); }}