| OpenMesh

smooth_algo.hh
1 #include <algorithm>
2 #include <OpenMesh/Core/Utils/Property.hh>
3
4 #ifndef DOXY_IGNORE_THIS
5
6 template <class Mesh> class SmootherT
7 {
8 public:
9
10 typedef typename Mesh::Point cog_t;
11 typedef OpenMesh::VPropHandleT< cog_t > Property_cog;
12
13 public:
14
15 // construct with a given mesh
16 explicit SmootherT(Mesh& _mesh)
17 : mesh_(_mesh)
18 {
19 mesh_.add_property( cog_ );
20 }
21
22 ~SmootherT()
23 {
24 mesh_.remove_property( cog_ );
25 }
26
27 // smooth mesh _iterations times
28 void smooth(unsigned int _iterations)
29 {
30 for (unsigned int i=0; i < _iterations; ++i)
31 {
32 std::for_each(mesh_.vertices_begin(),
33 mesh_.vertices_end(),
34 ComputeCOG(mesh_, cog_));
35
36 std::for_each(mesh_.vertices_begin(),
37 mesh_.vertices_end(),
38 SetCOG(mesh_, cog_));
39 }
40 }
41
42
43 private:
44
45
46 //--- private classes ---
47
48 class ComputeCOG
49 {
50 public:
51 ComputeCOG(Mesh& _mesh, Property_cog& _cog)
52 : mesh_(_mesh), cog_(_cog)
53 {}
54
55 void operator()(typename Mesh::Vertex& _v)
56 {
57 typename Mesh::VertexHandle vh( mesh_.handle(_v) );
58 typename Mesh::VertexVertexIter vv_it;
59 typename Mesh::Scalar valence(0.0);
60
61 mesh_.property(cog_, vh) = typename Mesh::Point(0.0, 0.0, 0.0);
62
63 for (vv_it=mesh_.vv_iter(vh); vv_it; ++vv_it)
64 {
65 mesh_.property(cog_, vh) += mesh_.point( vv_it );
66 ++valence;
67 }
68
69 mesh_.property(cog_, mesh_.handle(_v) ) /= valence;
70 }
71
72 private:
73 Mesh& mesh_;
74 Property_cog& cog_;
75 };
76
77
78 class SetCOG
79 {
80 public:
81 SetCOG(Mesh& _mesh, Property_cog& _cog)
82 : mesh_(_mesh), cog_(_cog)
83 {}
84
85 void operator()(typename Mesh::Vertex& _v)
86 {
87 typename Mesh::VertexHandle vh(mesh_.handle(_v));
88
89 if (!mesh_.is_boundary(vh))
90 mesh_.set_point( vh, mesh_.property(cog_, vh) );
91 }
92
93 private:
94
95 Mesh& mesh_;
96 Property_cog& cog_;
97 };
98
99
100 //--- private elements ---
101
102 Mesh& mesh_;
103 Property_cog cog_;
104 };
105
106 #endif
OpenMesh::VPropHandleT
Handle representing a vertex property.
Definition: Property.hh:484