Browse Source

De-inlined MeshTools::CompressIndices.

The class doesn't export anything templated in its API, so it's a waste
to have everything in the header.
vectorfields
Vladimír Vondruš 14 years ago
parent
commit
b7d6a7e67d
  1. 1
      src/MeshTools/CMakeLists.txt
  2. 61
      src/MeshTools/CompressIndices.cpp
  3. 45
      src/MeshTools/CompressIndices.h
  4. 2
      src/MeshTools/Test/CMakeLists.txt

1
src/MeshTools/CMakeLists.txt

@ -1,5 +1,6 @@
# Files shared between main library and unit test library
set(MagnumMeshTools_SRCS
CompressIndices.cpp
Tipsify.cpp)
set(MagnumMeshTools_HEADERS
Clean.h

61
src/MeshTools/CompressIndices.cpp

@ -0,0 +1,61 @@
/*
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
This file is part of Magnum.
Magnum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
only, as published by the Free Software Foundation.
Magnum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License version 3 for more details.
*/
#include "CompressIndices.h"
#include <cstring>
#include <vector>
#include <algorithm>
#include "IndexedMesh.h"
#include "SizeTraits.h"
namespace Magnum { namespace MeshTools {
#ifndef DOXYGEN_GENERATING_OUTPUT
namespace Implementation {
std::tuple<size_t, Type, char* > CompressIndices::operator()() const {
return SizeBasedCall<Compressor>(*std::max_element(indices.begin(), indices.end()))(indices);
}
void CompressIndices::operator()(IndexedMesh* mesh, Buffer::Usage usage) const {
size_t indexCount;
Type indexType;
char* data;
std::tie(indexCount, indexType, data) = operator()();
mesh->setIndexType(indexType);
mesh->setIndexCount(indices.size());
mesh->indexBuffer()->setData(indexCount*TypeInfo::sizeOf(indexType), data, usage);
delete[] data;
}
template<class IndexType> std::tuple<size_t, Type, char*> CompressIndices::Compressor::run(const std::vector<unsigned int>& indices) {
/* Create smallest possible version of index buffer */
char* buffer = new char[indices.size()*sizeof(IndexType)];
for(size_t i = 0; i != indices.size(); ++i) {
IndexType index = indices[i];
memcpy(buffer+i*sizeof(IndexType), reinterpret_cast<const char*>(&index), sizeof(IndexType));
}
return std::make_tuple(indices.size(), TypeTraits<IndexType>::indexType(), buffer);
}
}
#endif
}}

45
src/MeshTools/CompressIndices.h

@ -19,52 +19,33 @@
* @brief Function Magnum::MeshTools::compressIndices()
*/
#include <cstring>
#include <vector>
#include <algorithm>
#include <tuple>
#include "Buffer.h"
#include "TypeTraits.h"
#include "SizeTraits.h"
#include "IndexedMesh.h"
namespace Magnum { namespace MeshTools {
#include "magnumMeshToolsVisibility.h"
namespace Magnum {
class IndexedMesh;
namespace MeshTools {
#ifndef DOXYGEN_GENERATING_OUTPUT
namespace Implementation {
class CompressIndices {
class MESHTOOLS_EXPORT CompressIndices {
public:
CompressIndices(const std::vector<unsigned int>& indices): indices(indices) {}
inline std::tuple<size_t, Type, char*> operator()() const {
return SizeBasedCall<Compressor>(*std::max_element(indices.begin(), indices.end()))(indices);
}
void operator()(IndexedMesh* mesh, Buffer::Usage usage) const {
size_t indexCount;
Type indexType;
char* data;
std::tie(indexCount, indexType, data) = operator()();
mesh->setIndexType(indexType);
mesh->setIndexCount(indices.size());
mesh->indexBuffer()->setData(indexCount*TypeInfo::sizeOf(indexType), data, usage);
std::tuple<size_t, Type, char*> operator()() const;
delete[] data;
}
void operator()(IndexedMesh* mesh, Buffer::Usage usage) const;
private:
struct Compressor {
template<class IndexType> static std::tuple<size_t, Type, char*> run(const std::vector<unsigned int>& indices) {
/* Create smallest possible version of index buffer */
char* buffer = new char[indices.size()*sizeof(IndexType)];
for(size_t i = 0; i != indices.size(); ++i) {
IndexType index = indices[i];
memcpy(buffer+i*sizeof(IndexType), reinterpret_cast<const char*>(&index), sizeof(IndexType));
}
return std::make_tuple(indices.size(), TypeTraits<IndexType>::indexType(), buffer);
}
template<class IndexType> static std::tuple<size_t, Type, char*> run(const std::vector<unsigned int>& indices);
};
const std::vector<unsigned int>& indices;

2
src/MeshTools/Test/CMakeLists.txt

@ -1,6 +1,6 @@
corrade_add_test2(MeshToolsCleanTest CleanTest.cpp)
corrade_add_test2(MeshToolsCombineIndexedArraysTest CombineIndexedArraysTest.cpp)
corrade_add_test2(MeshToolsCompressIndicesTest CompressIndicesTest.cpp LIBRARIES Magnum)
corrade_add_test2(MeshToolsCompressIndicesTest CompressIndicesTest.cpp LIBRARIES MagnumMeshTools)
corrade_add_test2(MeshToolsFlipNormalsTest FlipNormalsTest.cpp LIBRARIES MagnumMeshToolsTestLib)
corrade_add_test2(MeshToolsGenerateFlatNormalsTest GenerateFlatNormalsTest.cpp LIBRARIES MagnumMeshToolsTestLib)
corrade_add_test2(MeshToolsInterleaveTest InterleaveTest.cpp)

Loading…
Cancel
Save