Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

smooth_algo.hh

00001 #include <algorithm>
00002 #include <OpenMesh/Core/Utils/Property.hh>
00003 
00004 #ifndef DOXY_IGNORE_THIS
00005 
00006 template <class Mesh> class SmootherT
00007 {
00008 public:
00009 
00010   typedef typename Mesh::Point            cog_t;
00011   typedef OpenMesh::VPropHandleT< cog_t > Property_cog;
00012 
00013 public:
00014 
00015   // construct with a given mesh
00016   SmootherT(Mesh& _mesh) 
00017     : mesh_(_mesh)
00018   { 
00019     mesh_.add_property( cog_ );
00020   }
00021 
00022   ~SmootherT()
00023   {
00024     mesh_.remove_property( cog_ );
00025   }
00026 
00027   // smooth mesh _iterations times
00028   void smooth(unsigned int _iterations)
00029   {
00030     for (unsigned int i=0; i < _iterations; ++i)
00031     {
00032       std::for_each(mesh_.vertices_begin(), 
00033                     mesh_.vertices_end(), 
00034                     ComputeCOG(mesh_, cog_));
00035 
00036       std::for_each(mesh_.vertices_begin(), 
00037                     mesh_.vertices_end(), 
00038                     SetCOG(mesh_, cog_));
00039     }
00040   }
00041 
00042 
00043 private:
00044 
00045 
00046   //--- private classes ---
00047 
00048   class ComputeCOG
00049   {
00050   public:
00051     ComputeCOG(Mesh& _mesh, Property_cog& _cog) 
00052       : mesh_(_mesh), cog_(_cog)
00053     {}
00054 
00055     void operator()(typename Mesh::Vertex& _v)
00056     {
00057       typename Mesh::VertexHandle      vh( mesh_.handle(_v) );
00058       typename Mesh::VertexVertexIter  vv_it;
00059       typename Mesh::Scalar            valence(0.0);
00060     
00061       mesh_.property(cog_, vh) = typename Mesh::Point(0.0, 0.0, 0.0);
00062 
00063       for (vv_it=mesh_.vv_iter(vh); vv_it; ++vv_it)
00064       {
00065         mesh_.property(cog_, vh) += mesh_.point( vv_it );
00066         ++valence;
00067       }
00068 
00069       mesh_.property(cog_, mesh_.handle(_v) ) /= valence;
00070     }
00071 
00072   private:
00073     Mesh&         mesh_;
00074     Property_cog& cog_;
00075   };
00076 
00077 
00078   class SetCOG
00079   {
00080   public:
00081     SetCOG(Mesh& _mesh, Property_cog& _cog) 
00082       : mesh_(_mesh), cog_(_cog)
00083     {}
00084 
00085     void operator()(typename Mesh::Vertex& _v)
00086     {
00087       typename Mesh::VertexHandle vh(mesh_.handle(_v));
00088 
00089       if (!mesh_.is_boundary(vh))
00090         mesh_.set_point( vh, mesh_.property(cog_, vh) );
00091     }
00092 
00093   private:
00094 
00095     Mesh&         mesh_;
00096     Property_cog& cog_;
00097   };
00098 
00099 
00100   //--- private elements ---
00101 
00102   Mesh&        mesh_;
00103   Property_cog cog_;
00104 };
00105 
00106 #endif

acg pic Project OpenMesh, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .