2009-02-06 13:37:46 +00:00
|
|
|
#include <OpenMesh/Core/IO/MeshIO.hh>
|
|
|
|
|
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
|
2018-11-30 20:13:37 +01:00
|
|
|
#include <OpenMesh/Core/Utils/PropertyManager.hh>
|
2009-02-06 13:37:46 +00:00
|
|
|
|
2018-11-30 20:13:37 +01:00
|
|
|
#include <iostream>
|
|
|
|
|
#include <vector>
|
2009-02-06 13:37:46 +00:00
|
|
|
|
2018-11-30 20:13:37 +01:00
|
|
|
using MyMesh = OpenMesh::TriMesh_ArrayKernelT<>;
|
2009-02-06 13:37:46 +00:00
|
|
|
|
2018-11-30 20:13:37 +01:00
|
|
|
int main(int argc, char** argv)
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
2018-11-30 20:13:37 +01:00
|
|
|
// Read command line options
|
|
|
|
|
MyMesh mesh;
|
|
|
|
|
if (argc != 4) {
|
|
|
|
|
std::cerr << "Usage: " << argv[0] << " #iterations infile outfile" << std::endl;
|
|
|
|
|
return 1;
|
2009-02-06 13:37:46 +00:00
|
|
|
}
|
2018-11-30 20:13:37 +01:00
|
|
|
const int iterations = argv[1];
|
|
|
|
|
const std::string infile = argv[2];
|
|
|
|
|
const std::string outfile = argv[3];
|
2009-02-06 13:37:46 +00:00
|
|
|
|
2018-11-30 20:13:37 +01:00
|
|
|
// Read mesh file
|
|
|
|
|
if (!OpenMesh::IO::read_mesh(mesh, infile)) {
|
|
|
|
|
std::cerr << "Error: Cannot read mesh from " << infile << std::endl;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2018-12-03 07:52:59 +01:00
|
|
|
|
2018-11-30 20:13:37 +01:00
|
|
|
{
|
2018-12-03 07:52:59 +01:00
|
|
|
// Add a vertex property storing the computed centers of gravity
|
|
|
|
|
auto cog = OpenMesh::makeTemporaryProperty<OpenMesh::VertexHandle, MyMesh::Point>(mesh);
|
|
|
|
|
|
|
|
|
|
// Smooth the mesh several times
|
|
|
|
|
for (int i = 0; i < iterations; ++i) {
|
|
|
|
|
// Iterate over all vertices to compute centers of gravity
|
|
|
|
|
for (const auto& vh : mesh.vertices()) {
|
|
|
|
|
cog[vh] = {0,0,0};
|
|
|
|
|
int valence = 0;
|
|
|
|
|
// Iterate over all 1-ring vertices around vh
|
|
|
|
|
for (const auto& vvh : mesh.vv_range(vh)) {
|
|
|
|
|
cog[vh] += mesh.point(vvh);
|
|
|
|
|
++valence;
|
|
|
|
|
}
|
|
|
|
|
cog[vh] /= valence;
|
|
|
|
|
}
|
|
|
|
|
// Move all vertices to the previously computed positions
|
|
|
|
|
for (const auto& vh : mesh.vertices()) {
|
|
|
|
|
mesh.point(vh) = cog[vh];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} // The cog vertex property is removed from the mesh at the end of this scope
|
2018-11-30 20:13:37 +01:00
|
|
|
|
|
|
|
|
// Write mesh file
|
|
|
|
|
if (!OpenMesh::IO::read_mesh(mesh, outfile)) {
|
|
|
|
|
std::cerr << "Error: Cannot write mesh to " << outfile << std::endl;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2009-02-06 13:37:46 +00:00
|
|
|
}
|