OpenMesh: Doc/Tutorial/04-stl_algorithms/smooth_algo.hh Source File

| 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()(const typename Mesh::VertexHandle& _vh)
56 {
57 typename Mesh::VertexVertexIter vv_it;
58 typename Mesh::Scalar valence(0.0);
59
60 mesh_.property(cog_, _vh) = typename Mesh::Point(0.0, 0.0, 0.0);
61
62 for (vv_it=mesh_.vv_iter(_vh); vv_it.is_valid(); ++vv_it)
63 {
64 mesh_.property(cog_, _vh) += mesh_.point( *vv_it );
65 ++valence;
66 }
67
68 mesh_.property(cog_, _vh ) /= valence;
69 }
70
71 private:
72 Mesh& mesh_;
73 Property_cog& cog_;
74 };
75
76
77 class SetCOG
78 {
79 public:
80 SetCOG(Mesh& _mesh, Property_cog& _cog)
81 : mesh_(_mesh), cog_(_cog)
82 {}
83
84 void operator()(const typename Mesh::VertexHandle& _vh)
85 {
86
87 if (!mesh_.is_boundary(_vh))
88 mesh_.set_point( _vh, mesh_.property(cog_, _vh) );
89 }
90
91 private:
92
93 Mesh& mesh_;
94 Property_cog& cog_;
95 };
96
97
98 //--- private elements ---
99
100 Mesh& mesh_;
101 Property_cog cog_;
102 };
103
104 #endif
OpenMesh::VPropHandleT
Handle representing a vertex property.
Definition: Property.hh:487