Browse Source

MeshTools: some cleanup.

pull/278/head
Vladimír Vondruš 13 years ago
parent
commit
915d49cc7d
  1. 35
      src/MeshTools/Clean.h
  2. 16
      src/MeshTools/Test/CleanTest.cpp
  3. 21
      src/MeshTools/Test/SubdivideTest.cpp

35
src/MeshTools/Clean.h

@ -32,7 +32,7 @@
#include <limits>
#include <Utility/MurmurHash2.h>
#include "Math/Vector.h"
#include "Math/Functions.h"
#include "Magnum.h"
namespace Magnum { namespace MeshTools {
@ -92,32 +92,23 @@ template<class Vertex, std::size_t vertexSize> void Clean<Vertex, vertexSize>::o
if(indices.empty()) return;
/* Get mesh bounds */
Vertex min, max;
for(std::size_t i = 0; i != Vertex::Size; ++i) {
min[i] = std::numeric_limits<typename Vertex::Type>::max();
max[i] = std::numeric_limits<typename Vertex::Type>::min();
Vertex min = vertices[0], max = vertices[0];
for(const auto& v: vertices) {
min = Math::min(v, min);
max = Math::max(v, max);
}
for(auto it = vertices.cbegin(); it != vertices.cend(); ++it)
for(std::size_t i = 0; i != vertexSize; ++i)
if((*it)[i] < min[i])
min[i] = (*it)[i];
else if((*it)[i] > max[i])
max[i] = (*it)[i];
/* Make epsilon so large that std::size_t can index all vertices
inside mesh bounds. */
Vertex size = max-min;
for(std::size_t i = 0; i != Vertex::Size; ++i)
if(static_cast<typename Vertex::Type>(size[i]/std::numeric_limits<std::size_t>::max()) > epsilon)
epsilon = static_cast<typename Vertex::Type>(size[i]/std::numeric_limits<std::size_t>::max());
/* Make epsilon so large that std::size_t can index all vertices inside
mesh bounds. */
epsilon = Math::max(epsilon, static_cast<typename Vertex::Type>((max-min).max()/std::numeric_limits<std::size_t>::max()));
/* First go with original vertex coordinates, then move them by
epsilon/2 in each direction. */
epsilon/2 in each direction. */
Vertex moved;
for(std::size_t moving = 0; moving <= vertexSize; ++moving) {
/* Under each index is pointer to face which contains given vertex
and index of vertex in the face. */
and index of vertex in the face. */
std::unordered_map<Math::Vector<vertexSize, std::size_t>, HashedVertex, IndexHash> table;
/* Reserve space for all vertices */
@ -131,8 +122,8 @@ template<class Vertex, std::size_t vertexSize> void Clean<Vertex, vertexSize>::o
index[ii] = (vertices[*it][ii]+moved[ii]-min[ii])/epsilon;
/* Try inserting the vertex into table, if it already
exists, change vertex pointer of the face to already
existing vertex */
exists, change vertex pointer of the face to already
existing vertex */
HashedVertex v(*it, table.size());
auto result = table.insert(std::pair<Math::Vector<vertexSize, std::size_t>, HashedVertex>(Math::Vector<vertexSize, std::size_t>::from(index), v));
*it = result.first->second.newIndex;

16
src/MeshTools/Test/CleanTest.cpp

@ -35,21 +35,7 @@ class CleanTest: public TestSuite::Tester {
void cleanMesh();
};
class Vector1 {
public:
static const std::size_t Size = 1;
typedef Int Type;
Vector1(): data(0) {}
Vector1(Type i): data(i) {}
Type operator[](std::size_t) const { return data; }
Type& operator[](std::size_t) { return data; }
bool operator==(Vector1 i) const { return i.data == data; }
Vector1 operator-(Vector1 i) const { return data-i.data; }
private:
Type data;
};
typedef Math::Vector<1, int> Vector1;
CleanTest::CleanTest() {
addTests({&CleanTest::cleanMesh});

21
src/MeshTools/Test/SubdivideTest.cpp

@ -36,26 +36,15 @@ class SubdivideTest: public TestSuite::Tester {
void wrongIndexCount();
void subdivide();
};
private:
class Vector1 {
public:
static const std::size_t Size = 1;
typedef Int Type;
namespace {
Vector1(): data(0) {}
Vector1(Type i): data(i) {}
Type operator[](std::size_t) const { return data; }
Type& operator[](std::size_t) { return data; }
bool operator==(Vector1 i) const { return i.data == data; }
Vector1 operator-(Vector1 i) const { return data-i.data; }
typedef Math::Vector<1, Int> Vector1;
private:
Type data;
};
inline Vector1 interpolator(Vector1 a, Vector1 b) { return (a[0]+b[0])/2; }
static Vector1 interpolator(Vector1 a, Vector1 b) { return (a[0]+b[0])/2; }
};
}
SubdivideTest::SubdivideTest() {
addTests({&SubdivideTest::wrongIndexCount,

Loading…
Cancel
Save