Browse Source

MeshTools: retroactively fire the 2010 edition of me.

Reason: a serious case of overengineerism.

What's the point of those useless "functor" classes, anyway?
pull/415/head
Vladimír Vondruš 6 years ago
parent
commit
47760294fc
  1. 6
      src/Magnum/MeshTools/CMakeLists.txt
  2. 67
      src/Magnum/MeshTools/Implementation/Tipsify.h
  3. 3
      src/Magnum/MeshTools/Test/TipsifyTest.cpp
  4. 39
      src/Magnum/MeshTools/Tipsify.cpp
  5. 28
      src/Magnum/MeshTools/Tipsify.h

6
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 $<TARGET_PROPERTY:Magnum,INTERFACE_INCLUDE_DIRECTORIES>)
if(NOT BUILD_STATIC)
target_compile_definitions(MagnumMeshToolsObjects PRIVATE "MagnumMeshToolsObjects_EXPORTS")

67
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š <mosra@centrum.cz>
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 <vector>
#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<UnsignedInt>& indices, const UnsignedInt vertexCount, std::vector<UnsignedInt>& liveTriangleCount, std::vector<UnsignedInt>& neighborOffset, std::vector<UnsignedInt>& 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

3
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<UnsignedInt> indices = Indices;
std::vector<UnsignedInt> liveTriangleCount, neighborOffset, neighbors;
Implementation::Tipsify(indices, VertexCount).buildAdjacency(liveTriangleCount, neighborOffset, neighbors);
Implementation::buildAdjacency(indices, VertexCount, liveTriangleCount, neighborOffset, neighbors);
CORRADE_COMPARE(liveTriangleCount, (std::vector<UnsignedInt>{
1, 3, 3, 2,

39
src/Magnum/MeshTools/Tipsify.cpp

@ -27,12 +27,14 @@
#include <stack>
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<UnsignedInt>& indices, const UnsignedInt vertexCount, const std::size_t cacheSize) {
/* Neighboring triangles for each vertex, per-vertex live triangle count */
std::vector<UnsignedInt> 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<UnsignedInt>& liveTriangleCount, std::vector<UnsignedInt>& neighborOffset, std::vector<UnsignedInt>& 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;
}
}}}
}}

28
src/Magnum/MeshTools/Tipsify.h

@ -36,30 +36,6 @@
namespace Magnum { namespace MeshTools {
namespace Implementation {
class MAGNUM_MESHTOOLS_EXPORT Tipsify {
public:
Tipsify(std::vector<UnsignedInt>& 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<UnsignedInt>& liveTriangleCount, std::vector<UnsignedInt>& neighborOffset, std::vector<UnsignedInt>& neighbors) const;
private:
std::vector<UnsignedInt>& 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<UnsignedInt>& indices, UnsignedInt vertexCount, std::size_t cacheSize) {
Implementation::Tipsify(indices, vertexCount)(cacheSize);
}
MAGNUM_MESHTOOLS_EXPORT void tipsify(std::vector<UnsignedInt>& indices, UnsignedInt vertexCount, std::size_t cacheSize);
}}

Loading…
Cancel
Save