OpenMesh
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
DecimaterT.hh
Go to the documentation of this file.
1 /*===========================================================================*\
2  * *
3  * OpenMesh *
4  * Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen *
5  * www.openmesh.org *
6  * *
7  *---------------------------------------------------------------------------*
8  * This file is part of OpenMesh. *
9  * *
10  * OpenMesh is free software: you can redistribute it and/or modify *
11  * it under the terms of the GNU Lesser General Public License as *
12  * published by the Free Software Foundation, either version 3 of *
13  * the License, or (at your option) any later version with the *
14  * following exceptions: *
15  * *
16  * If other files instantiate templates or use macros *
17  * or inline functions from this file, or you compile this file and *
18  * link it with other files to produce an executable, this file does *
19  * not by itself cause the resulting executable to be covered by the *
20  * GNU Lesser General Public License. This exception does not however *
21  * invalidate any other reasons why the executable file might be *
22  * covered by the GNU Lesser General Public License. *
23  * *
24  * OpenMesh is distributed in the hope that it will be useful, *
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
27  * GNU Lesser General Public License for more details. *
28  * *
29  * You should have received a copy of the GNU LesserGeneral Public *
30  * License along with OpenMesh. If not, *
31  * see <http://www.gnu.org/licenses/>. *
32  * *
33 \*===========================================================================*/
34 
35 /*===========================================================================*\
36  * *
37  * $Revision: 448 $ *
38  * $Date: 2011-11-04 13:59:37 +0100 (Fr, 04 Nov 2011) $ *
39  * *
40 \*===========================================================================*/
41 
45 //=============================================================================
46 //
47 // CLASS DecimaterT
48 //
49 //=============================================================================
50 
51 #ifndef OPENMESH_DECIMATER_DECIMATERT_HH
52 #define OPENMESH_DECIMATER_DECIMATERT_HH
53 
54 
55 //== INCLUDES =================================================================
56 
57 #include <memory>
58 
59 #include <OpenMesh/Core/Utils/Property.hh>
62 
63 
64 
65 //== NAMESPACE ================================================================
66 
67 namespace OpenMesh {
68 namespace Decimater {
69 
70 
71 //== CLASS DEFINITION =========================================================
72 
73 
77 template < typename MeshT >
79 {
80 public: //-------------------------------------------------------- public types
81 
82  typedef DecimaterT< MeshT > Self;
83  typedef MeshT Mesh;
85  typedef ModBaseT<Self> Module;
86  typedef std::vector< Module* > ModuleList;
87  typedef typename ModuleList::iterator ModuleListIterator;
88 
89 public: //------------------------------------------------------ public methods
90 
92  DecimaterT( Mesh& _mesh );
93 
95  ~DecimaterT();
96 
97 
105  bool initialize();
106 
107 
109  bool is_initialized() const { return initialized_; }
110 
111 
113  void info( std::ostream& _os );
114 
115 public: //--------------------------------------------------- module management
116 
118  Mesh& mesh() { return mesh_; }
119 
121  template < typename _Module >
123  {
124  if (_mh.is_valid())
125  return false;
126 
127  _mh.init( new _Module(*this) );
128  all_modules_.push_back( _mh.module() );
129 
130  set_uninitialized();
131 
132  return true;
133  }
134 
135 
137  template < typename _Module >
138  bool remove( ModHandleT<_Module>& _mh )
139  {
140  if (!_mh.is_valid())
141  return false;
142 
143  typename ModuleList::iterator it = std::find(all_modules_.begin(),
144  all_modules_.end(),
145  _mh.module() );
146 
147  if ( it == all_modules_.end() ) // module not found
148  return false;
149 
150  delete *it;
151  all_modules_.erase( it ); // finally remove from list
152  _mh.clear();
153 
154  set_uninitialized();
155  return true;
156  }
157 
158 
160  template < typename Module >
162  {
163  assert( _mh.is_valid() );
164  return *_mh.module();
165  }
166 
167 public:
168 
172  size_t decimate( size_t _n_collapses = 0 );
173 
175  size_t decimate_to( size_t _n_vertices )
176  {
177  return ( (_n_vertices < mesh().n_vertices()) ?
178  decimate( mesh().n_vertices() - _n_vertices ) : 0 );
179  }
180 
184  size_t decimate_to_faces( size_t _n_vertices=0, size_t _n_faces=0 );
185 
186 private:
187 
188  void update_modules(CollapseInfo& _ci)
189  {
190  typename ModuleList::iterator m_it, m_end = bmodules_.end();
191  for (m_it = bmodules_.begin(); m_it != m_end; ++m_it)
192  (*m_it)->postprocess_collapse(_ci);
193  cmodule_->postprocess_collapse(_ci);
194  }
195 
196 public:
197 
198  typedef typename Mesh::VertexHandle VertexHandle;
199  typedef typename Mesh::HalfedgeHandle HalfedgeHandle;
200 
203  {
204  public:
205 
206  HeapInterface(Mesh& _mesh,
207  VPropHandleT<float> _prio,
208  VPropHandleT<int> _pos)
209  : mesh_(_mesh), prio_(_prio), pos_(_pos)
210  { }
211 
212  inline bool
213  less( VertexHandle _vh0, VertexHandle _vh1 )
214  { return mesh_.property(prio_, _vh0) < mesh_.property(prio_, _vh1); }
215 
216  inline bool
217  greater( VertexHandle _vh0, VertexHandle _vh1 )
218  { return mesh_.property(prio_, _vh0) > mesh_.property(prio_, _vh1); }
219 
220  inline int
221  get_heap_position(VertexHandle _vh)
222  { return mesh_.property(pos_, _vh); }
223 
224  inline void
225  set_heap_position(VertexHandle _vh, int _pos)
226  { mesh_.property(pos_, _vh) = _pos; }
227 
228 
229  private:
230  Mesh& mesh_;
231  VPropHandleT<float> prio_;
232  VPropHandleT<int> pos_;
233  };
234 
236 
237 
238 private: //---------------------------------------------------- private methods
239 
241  void heap_vertex(VertexHandle _vh);
242 
247  bool is_collapse_legal(const CollapseInfo& _ci);
248 
250  float collapse_priority(const CollapseInfo& _ci);
251 
253  void preprocess_collapse(CollapseInfo& _ci);
254 
256  void postprocess_collapse(CollapseInfo& _ci);
257 
258  // Reset the initialized flag, and clear the bmodules_ and cmodule_
259  void set_uninitialized() {
260  initialized_ = false;
261  cmodule_ = 0;
262  bmodules_.clear();
263  }
264 
265 
266 private: //------------------------------------------------------- private data
267 
268 
269  // reference to mesh
270  Mesh& mesh_;
271 
272  // heap
273  std::auto_ptr<DeciHeap> heap_;
274 
275  // list of binary modules
276  ModuleList bmodules_;
277 
278  // the current priority module
279  Module* cmodule_;
280 
281  // list of all allocated modules (including cmodule_ and all of bmodules_)
282  ModuleList all_modules_;
283 
284  bool initialized_;
285 
286 
287  // vertex properties
288  VPropHandleT<HalfedgeHandle> collapse_target_;
289  VPropHandleT<float> priority_;
290  VPropHandleT<int> heap_position_;
291 
292 
293 
294 private: // Noncopyable
295 
296  DecimaterT(const Self&);
297  Self& operator = (const Self&);
298 
299 };
300 
301 //=============================================================================
302 } // END_NS_DECIMATER
303 } // END_NS_OPENMESH
304 //=============================================================================
305 #if defined(OM_INCLUDE_TEMPLATES) && !defined(OPENMESH_DECIMATER_DECIMATERT_CC)
306 #define OPENMESH_DECIMATER_TEMPLATES
307 #include "DecimaterT.cc"
308 #endif
309 //=============================================================================
310 #endif // OPENMESH_DECIMATER_DECIMATERT_HH defined
311 //=============================================================================
312 

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