Developer Documentation

smooth.cc
1 #include <OpenMesh/Core/IO/MeshIO.hh>
2 #include <OpenMesh/Core/Mesh/DefaultTriMesh.hh>
3 #include <OpenMesh/Core/Utils/PropertyManager.hh>
4
5 #include <iostream>
6 #include <vector>
7
8 using MyMesh = OpenMesh::TriMesh;
9
10 int main(int argc, char** argv)
11 {
12 // Read command line options
13 MyMesh mesh;
14 if (argc != 4) {
15 std::cerr << "Usage: " << argv[0] << " #iterations infile outfile" << std::endl;
16 return 1;
17 }
18 const int iterations = argv[1];
19 const std::string infile = argv[2];
20 const std::string outfile = argv[3];
21
22 // Read mesh file
23 if (!OpenMesh::IO::read_mesh(mesh, infile)) {
24 std::cerr << "Error: Cannot read mesh from " << infile << std::endl;
25 return 1;
26 }
27
28 {
29 // Add a vertex property storing the computed centers of gravity
30 auto cog = OpenMesh::VProp<MyMesh::Point>(mesh);
31
32 // Smooth the mesh several times
33 for (int i = 0; i < iterations; ++i) {
34 // Iterate over all vertices to compute centers of gravity
35 for (const auto& vh : mesh.vertices()) {
36 cog[vh] = {0,0,0};
37 int valence = 0;
38 // Iterate over all 1-ring vertices around vh
39 for (const auto& vvh : mesh.vv_range(vh)) {
40 cog[vh] += mesh.point(vvh);
41 ++valence;
42 }
43 cog[vh] /= valence;
44 }
45 // Move all vertices to the previously computed positions
46 for (const auto& vh : mesh.vertices()) {
47 mesh.point(vh) = cog[vh];
48 }
49 }
50 } // The cog vertex property is removed from the mesh at the end of this scope
51
52 // Write mesh file
53 if (!OpenMesh::IO::read_mesh(mesh, outfile)) {
54 std::cerr << "Error: Cannot write mesh to " << outfile << std::endl;
55 return 1;
56 }
57 }
OpenMesh::TriMesh_ArrayKernelT
Definition: PolyMesh_ArrayKernelT.hh:73
OpenMesh::IO::read_mesh
bool read_mesh(Mesh &_mesh, const std::string &_filename)
Read a mesh from file _filename.
Definition: MeshIO.hh:112
OpenMesh::PropertyManager
Definition: PropertyManager.hh:80