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 // --------------------
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 
24  // read mesh from stdin
25  if ( ! OpenMesh::IO::read_mesh(mesh, argv[2]) )
26  {
27  std::cerr << "Error: Cannot read mesh from " << argv[2] << std::endl;
28  return 1;
29  }
30 
31 
32 
33  // this vertex property stores the computed centers of gravity
35  mesh.add_property(cogs);
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  for (v_it=mesh.vertices_begin(); v_it!=v_end; ++v_it)
48  {
49  mesh.property(cogs,*v_it).vectorize(0.0f);
50  valence = 0.0;
51 
52  for (vv_it=mesh.vv_iter( *v_it ); vv_it; ++vv_it)
53  {
54  mesh.property(cogs,*v_it) += mesh.point( *vv_it );
55  ++valence;
56  }
57  mesh.property(cogs,*v_it) /= valence;
58  }
59 
60  for (v_it=mesh.vertices_begin(); v_it!=v_end; ++v_it)
61  if ( !mesh.is_boundary( *v_it ) )
62  mesh.set_point( *v_it, mesh.property(cogs,*v_it) );
63  }
64 
65 
66  // write mesh to stdout
67  if ( ! OpenMesh::IO::write_mesh(mesh, argv[3]) )
68  {
69  std::cerr << "Error: cannot write mesh to " << argv[3] << std::endl;
70  return 1;
71  }
72 
73  return 0;
74 }
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