Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
smooth.cc
1 #include <iostream>
2 #include <vector>
3 // -------------------- OpenMesh
4 #include <OpenMesh/Core/IO/MeshIO.hh>
5 #include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
6 
8 
9 
10 int main(int argc, char **argv)
11 {
12  MyMesh mesh;
13 
14 
15  // check command line options
16  if (argc != 4)
17  {
18  std::cerr << "Usage: " << argv[0] << " #iterations infile outfile\n";
19  return 1;
20  }
21 
22 
23  // read mesh from stdin
24  if ( ! OpenMesh::IO::read_mesh(mesh, argv[2]) )
25  {
26  std::cerr << "Error: Cannot read mesh from " << argv[2] << std::endl;
27  return 1;
28  }
29 
30 
31  // this vector stores the computed centers of gravity
32  std::vector<MyMesh::Point> cogs;
33  std::vector<MyMesh::Point>::iterator cog_it;
34  cogs.reserve(mesh.n_vertices());
35 
36 
37  // smoothing mesh argv[1] times
38  MyMesh::VertexIter v_it, v_end(mesh.vertices_end());
40  MyMesh::Point cog;
41  MyMesh::Scalar valence;
42  unsigned int i, N(atoi(argv[1]));
43 
44 
45  for (i=0; i < N; ++i)
46  {
47  cogs.clear();
48  for (v_it=mesh.vertices_begin(); v_it!=v_end; ++v_it)
49  {
50  cog[0] = cog[1] = cog[2] = valence = 0.0;
51 
52  for (vv_it=mesh.vv_iter( v_it ); vv_it; ++vv_it)
53  {
54  cog += mesh.point( vv_it );
55  ++valence;
56  }
57 
58  cogs.push_back(cog / valence);
59  }
60 
61  for (v_it=mesh.vertices_begin(), cog_it=cogs.begin();
62  v_it!=v_end; ++v_it, ++cog_it)
63  if ( !mesh.is_boundary( v_it ) )
64  mesh.set_point( v_it, *cog_it );
65  }
66 
67 
68  // write mesh to stdout
69  if ( ! OpenMesh::IO::write_mesh(mesh, argv[3]) )
70  {
71  std::cerr << "Error: cannot write mesh to " << argv[3] << std::endl;
72  return 1;
73  }
74 
75  return 0;
76 }
bool read_mesh(Mesh &_mesh, const std::string &_filename)
Read a mesh from file _filename.
Definition: MeshIO.hh:104
Kernel::VertexVertexIter VertexVertexIter
Circulator.
Definition: PolyMeshT.hh:165
Kernel::Point Point
Coordinate type.
Definition: PolyMeshT.hh:115
Kernel::Scalar Scalar
Scalar type.
Definition: PolyMeshT.hh:113
bool write_mesh(const Mesh &_mesh, const std::string &_filename, Options _opt=Options::Default, std::streamsize _precision=6)
Write a mesh to the file _filename.
Definition: MeshIO.hh:199