mirror of https://github.com/mosra/magnum.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
4.2 KiB
119 lines
4.2 KiB
|
15 years ago
|
#ifndef Magnum_MeshTools_Subdivide_h
|
||
|
|
#define Magnum_MeshTools_Subdivide_h
|
||
|
|
/*
|
||
|
|
This file is part of Magnum.
|
||
|
|
|
||
|
13 years ago
|
Copyright © 2010, 2011, 2012, 2013 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.
|
||
|
15 years ago
|
|
||
|
13 years ago
|
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.
|
||
|
15 years ago
|
*/
|
||
|
|
|
||
|
|
/** @file
|
||
|
14 years ago
|
* @brief Function Magnum::MeshTools::subdivide()
|
||
|
15 years ago
|
*/
|
||
|
|
|
||
|
14 years ago
|
#include <vector>
|
||
|
14 years ago
|
#include <Utility/Debug.h>
|
||
|
15 years ago
|
|
||
|
|
namespace Magnum { namespace MeshTools {
|
||
|
|
|
||
|
14 years ago
|
namespace Implementation {
|
||
|
15 years ago
|
|
||
|
14 years ago
|
template<class Vertex, class Interpolator> class Subdivide {
|
||
|
15 years ago
|
public:
|
||
|
13 years ago
|
Subdivide(std::vector<UnsignedInt>& indices, std::vector<Vertex>& vertices): indices(indices), vertices(vertices) {}
|
||
|
|
|
||
|
|
void operator()(Interpolator interpolator);
|
||
|
14 years ago
|
|
||
|
|
private:
|
||
|
13 years ago
|
std::vector<UnsignedInt>& indices;
|
||
|
14 years ago
|
std::vector<Vertex>& vertices;
|
||
|
|
|
||
|
13 years ago
|
UnsignedInt addVertex(const Vertex& v) {
|
||
|
14 years ago
|
vertices.push_back(v);
|
||
|
|
return vertices.size()-1;
|
||
|
|
}
|
||
|
|
|
||
|
13 years ago
|
void addFace(UnsignedInt first, UnsignedInt second, UnsignedInt third) {
|
||
|
14 years ago
|
indices.push_back(first);
|
||
|
|
indices.push_back(second);
|
||
|
|
indices.push_back(third);
|
||
|
|
}
|
||
|
15 years ago
|
};
|
||
|
|
|
||
|
14 years ago
|
}
|
||
|
|
|
||
|
14 years ago
|
/**
|
||
|
15 years ago
|
@brief %Subdivide the mesh
|
||
|
14 years ago
|
@tparam Vertex Vertex data type
|
||
|
14 years ago
|
@tparam Interpolator See `interpolator` function parameter
|
||
|
14 years ago
|
@param[in,out] indices Index array to operate on
|
||
|
|
@param[in,out] vertices Vertex array to operate on
|
||
|
15 years ago
|
@param interpolator Functor or function pointer which interpolates
|
||
|
14 years ago
|
two adjacent vertices: `Vertex interpolator(Vertex a, Vertex b)`
|
||
|
15 years ago
|
|
||
|
13 years ago
|
Goes through all triangle faces and subdivides them into four new. Removing
|
||
|
15 years ago
|
duplicate vertices in the mesh is up to user.
|
||
|
|
*/
|
||
|
13 years ago
|
template<class Vertex, class Interpolator> inline void subdivide(std::vector<UnsignedInt>& indices, std::vector<Vertex>& vertices, Interpolator interpolator) {
|
||
|
14 years ago
|
Implementation::Subdivide<Vertex, Interpolator>(indices, vertices)(interpolator);
|
||
|
15 years ago
|
}
|
||
|
|
|
||
|
13 years ago
|
namespace Implementation {
|
||
|
|
|
||
|
|
template<class Vertex, class Interpolator> void Subdivide<Vertex, Interpolator>::operator()(Interpolator interpolator) {
|
||
|
|
CORRADE_ASSERT(!(indices.size()%3), "MeshTools::subdivide(): index count is not divisible by 3!", );
|
||
|
|
|
||
|
|
std::size_t indexCount = indices.size();
|
||
|
|
indices.reserve(indices.size()*4);
|
||
|
|
|
||
|
|
/* Subdivide each face to four new */
|
||
|
|
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]]));
|
||
|
|
|
||
|
|
/*
|
||
|
|
* Add three new faces (0, 1, 3) and update original (2)
|
||
|
|
*
|
||
|
|
* orig 0
|
||
|
|
* / \
|
||
|
|
* / 0 \
|
||
|
|
* / \
|
||
|
|
* new 0 ----- new 2
|
||
|
|
* / \ / \
|
||
|
|
* / 1 \ 2 / 3 \
|
||
|
|
* / \ / \
|
||
|
|
* 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]);
|
||
|
|
for(std::size_t j = 0; j != 3; ++j)
|
||
|
|
indices[i+j] = newVertices[j];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
15 years ago
|
}}
|
||
|
|
|
||
|
|
#endif
|