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

DecimaterT.hh

Go to the documentation of this file.
00001 //=============================================================================
00002 //                                                                            
00003 //                               OpenMesh                                     
00004 //        Copyright (C) 2003 by Computer Graphics Group, RWTH Aachen          
00005 //                           www.openmesh.org                                 
00006 //                                                                            
00007 //-----------------------------------------------------------------------------
00008 //                                                                            
00009 //                                License                                     
00010 //                                                                            
00011 //   This library is free software; you can redistribute it and/or modify it 
00012 //   under the terms of the GNU Library General Public License as published  
00013 //   by the Free Software Foundation, version 2.                             
00014 //                                                                             
00015 //   This library is distributed in the hope that it will be useful, but       
00016 //   WITHOUT ANY WARRANTY; without even the implied warranty of                
00017 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         
00018 //   Library General Public License for more details.                          
00019 //                                                                            
00020 //   You should have received a copy of the GNU Library General Public         
00021 //   License along with this library; if not, write to the Free Software       
00022 //   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 
00023 //                                                                            
00024 //-----------------------------------------------------------------------------
00025 //                                                                            
00026 //   $Revision: 1.23 $
00027 //   $Date: 2004/01/12 17:36:30 $
00028 //                                                                            
00029 //=============================================================================
00030 
00034 //=============================================================================
00035 //
00036 //  CLASS DecimaterT
00037 //
00038 //=============================================================================
00039 
00040 #ifndef OPENMESH_DECIMATER_DECIMATERT_HH
00041 #define OPENMESH_DECIMATER_DECIMATERT_HH
00042 
00043 
00044 //== INCLUDES =================================================================
00045 
00046 #include <memory>
00047 
00048 #include <OpenMesh/Core/Utils/Property.hh>
00049 #include <OpenMesh/Tools/Utils/HeapT.hh>
00050 #include <OpenMesh/Tools/Decimater/ModBaseT.hh>
00051 
00052 
00053 
00054 //== NAMESPACE ================================================================
00055 
00056 namespace OpenMesh  { 
00057 namespace Decimater {
00058 
00059 
00060 //== CLASS DEFINITION =========================================================
00061               
00062 
00066 template < typename MeshT >
00067 class DecimaterT
00068 {
00069 public: //-------------------------------------------------------- public types
00070    
00071   typedef DecimaterT< MeshT >        Self;
00072   typedef MeshT                      Mesh;
00073   typedef CollapseInfoT<MeshT>       CollapseInfo;
00074   typedef ModBaseT<Self>             Module;
00075   typedef std::vector< Module* >     ModuleList;
00076 
00077 public: //------------------------------------------------------ public methods
00078 
00080   DecimaterT( Mesh& _mesh ); 
00081    
00083   ~DecimaterT();
00084 
00085 
00093   bool initialize();
00094 
00095 
00097   bool is_initialized() const { return initialized_; }
00098 
00099 
00101   void info( std::ostream& _os );
00102 
00103 
00107   size_t decimate( size_t _n_collapses );
00108 
00109 
00111   size_t decimate_to( size_t  _n_vertices )
00112   { 
00113     assert( _n_vertices < mesh().n_vertices() );
00114     return decimate( mesh().n_vertices() - _n_vertices ); 
00115   }
00116 
00117 
00119   Mesh& mesh() { return mesh_; }
00120 
00121 
00122 
00123 
00124 
00125 public: //--------------------------------------------------- module management
00126 
00128   template < typename Module >
00129   bool add( ModHandleT<Module>& _mh )
00130   {
00131     if (_mh.is_valid())
00132       return false;
00133 
00134     _mh.init( new Module(*this) );
00135     bmodules_.push_back( _mh.module() );
00136 
00137     return !(initialized_=false);
00138   }
00139 
00140 
00142   template < typename Module >
00143   bool remove( ModHandleT<Module>& _mh )
00144   {
00145     if (!_mh.is_valid())
00146       return false;
00147 
00148     typename ModuleList::iterator it = std::find(bmodules_.begin(),
00149                                                  bmodules_.end(),
00150                                                  _mh.module() );
00151 
00152     if ( it == bmodules_.end() ) // module not found
00153       return false;
00154 
00155     delete *it;
00156     bmodules_.erase( it ); // finally remove from list
00157     _mh.clear();
00158 
00159     initialized_ = false; // reset initialized state
00160     return true;
00161   }
00162 
00163 
00165   template < typename Module >
00166   Module& module( ModHandleT<Module>& _mh )
00167   {
00168     assert( _mh.is_valid() );
00169     return *_mh.module();
00170   }
00171 
00172 
00173 
00174 
00175 private: //------------------------------------------------------ private types
00176 
00177   typedef typename Mesh::VertexHandle    VertexHandle;
00178   typedef typename Mesh::HalfedgeHandle  HalfedgeHandle;
00179 
00181   class HeapInterface
00182   {
00183   public:
00184 
00185     HeapInterface(Mesh&               _mesh,
00186                   VPropHandleT<float> _prio, 
00187                   VPropHandleT<int>   _pos)
00188       : mesh_(_mesh), prio_(_prio), pos_(_pos)
00189     { }
00190 
00191     inline bool
00192     less( VertexHandle _vh0, VertexHandle _vh1 )
00193     { return mesh_.property(prio_, _vh0) < mesh_.property(prio_, _vh1); }
00194      
00195     inline bool
00196     greater( VertexHandle _vh0, VertexHandle _vh1 )
00197     { return mesh_.property(prio_, _vh0) > mesh_.property(prio_, _vh1); }
00198      
00199     inline int
00200     get_heap_position(VertexHandle _vh)
00201     { return mesh_.property(pos_, _vh); }
00202      
00203     inline void
00204     set_heap_position(VertexHandle _vh, int _pos)
00205     { mesh_.property(pos_, _vh) = _pos; }
00206 
00207 
00208   private:
00209     Mesh&                mesh_;
00210     VPropHandleT<float>  prio_;
00211     VPropHandleT<int>    pos_;
00212   };
00213 
00214   typedef Utils::HeapT<VertexHandle, HeapInterface>  DeciHeap;
00215 
00216 
00217 private: //---------------------------------------------------- private methods
00218   
00220   void heap_vertex(VertexHandle _vh);
00221   
00226   bool is_collapse_legal(const CollapseInfo& _ci);
00227   
00229   float collapse_priority(const CollapseInfo& _ci);
00230 
00232   void postprocess_collapse(CollapseInfo& _ci);
00233   
00234 
00235 
00236 
00237 private: //------------------------------------------------------- private data
00238    
00239    
00240   // reference to mesh
00241   Mesh&      mesh_;
00242 
00243   // heap
00244   std::auto_ptr<DeciHeap> heap_;
00245 
00246   // list of modules
00247   ModuleList bmodules_;
00248   Module*    cmodule_;
00249 
00250   bool       initialized_;
00251 
00252 
00253   // vertex properties
00254   VPropHandleT<HalfedgeHandle>  collapse_target_;
00255   VPropHandleT<float>           priority_;
00256   VPropHandleT<int>             heap_position_;
00257 
00258 
00259 
00260 private: // Noncopyable
00261 
00262   DecimaterT(const Self&);
00263   Self& operator = (const Self&);
00264 
00265 };
00266 
00267 //=============================================================================
00268 } // END_NS_DECIMATER
00269 } // END_NS_OPENMESH
00270 //=============================================================================
00271 #if defined(OM_INCLUDE_TEMPLATES) && !defined(OPENMESH_DECIMATER_DECIMATERT_CC)
00272 #define OPENMESH_DECIMATER_TEMPLATES
00273 #include "DecimaterT.cc"
00274 #endif
00275 //=============================================================================
00276 #endif // OPENMESH_DECIMATER_DECIMATERT_HH defined
00277 //=============================================================================
00278 

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