Developer Documentation
smooth.cc
1 
2 #include <OpenMesh/Core/IO/MeshIO.hh>
3 #include <OpenMesh/Core/Mesh/DefaultTriMesh.hh>
4 #include <OpenMesh/Core/Utils/PropertyManager.hh>
5 
6 #include <iostream>
7 #include <vector>
8 
10 
11 int main(int argc, char** argv)
12 {
13  // Read command line options
14  MyMesh mesh;
15  if (argc != 4) {
16  std::cerr << "Usage: " << argv[0] << " #iterations infile outfile" << std::endl;
17  return 1;
18  }
19  const int iterations = argv[1];
20  const std::string infile = argv[2];
21  const std::string outfile = argv[3];
22 
23  // Read mesh file
24  if (!OpenMesh::IO::read_mesh(mesh, infile)) {
25  std::cerr << "Error: Cannot read mesh from " << infile << std::endl;
26  return 1;
27  }
28 
29  {
30  // Add a vertex property storing the laplace vector
31  auto laplace = OpenMesh::VProp<MyMesh::Point>(mesh);
32 
33  // Add a vertex property storing the laplace of the laplace
34  auto bi_laplace = OpenMesh::VProp<MyMesh::Point>(mesh);
35 
36  // Get a propertymanager of the points property of the mesh to use as functor
37  auto points = OpenMesh::getPointsProperty(mesh);
38 
39  // Smooth the mesh several times
40  for (int i = 0; i < iterations; ++i) {
41  // Iterate over all vertices to compute laplace vector
42  for (const auto& vh : mesh.vertices())
43  laplace(vh) = vh.vertices().avg(points) - points(vh);
44 
45  // Iterate over all vertices to compte update vectors as the negative of the laplace of the laplace damped by 0.5
46  for (const auto& vh : mesh.vertices())
47  bi_laplace(vh) = (vh.vertices().avg(laplace) - laplace(vh));
48 
49  // update points
50  for (const auto& vh : mesh.vertices())
51  points(vh) += -0.5 * bi_laplace(vh);
52  }
53  } // The laplace and update properties are removed is removed from the mesh at the end of this scope.
54 
55 
56  // Write mesh file
57  if (!OpenMesh::IO::read_mesh(mesh, outfile)) {
58  std::cerr << "Error: Cannot write mesh to " << outfile << std::endl;
59  return 1;
60  }
61 }
62 
bool read_mesh(Mesh &_mesh, const std::string &_filename)
Read a mesh from file _filename.
Definition: MeshIO.hh:112