Developer Documentation
MixedDecimaterT.cc
Go to the documentation of this file.
1 /* ========================================================================= *
2  * *
3  * OpenMesh *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openmesh.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenMesh. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40  * ========================================================================= */
41 
42 /*===========================================================================*\
43 * *
44 * $Revision$ *
45 * $Date$ *
46 * *
47 \*===========================================================================*/
48 
52 //=============================================================================
53 //
54 // CLASS MixedDecimaterT - IMPLEMENTATION
55 //
56 //=============================================================================
57 #define OPENMESH_MIXED_DECIMATER_DECIMATERT_CC
58 
59 //== INCLUDES =================================================================
60 
61 #include <OpenMesh/Tools/Decimater/MixedDecimaterT.hh>
62 
63 #include <vector>
64 #if defined(OM_CC_MIPS)
65 # include <float.h>
66 #else
67 # include <cfloat>
68 #endif
69 
70 //== NAMESPACE ===============================================================
71 namespace OpenMesh {
72 namespace Decimater {
73 
74 //== IMPLEMENTATION ==========================================================
75 
76 template<class Mesh>
78  BaseDecimaterT<Mesh>(_mesh),McDecimaterT<Mesh>(_mesh), DecimaterT<Mesh>(_mesh) {
79 
80 }
81 
82 //-----------------------------------------------------------------------------
83 
84 template<class Mesh>
86 
87 }
88 
89 //-----------------------------------------------------------------------------
90 template<class Mesh>
91 size_t MixedDecimaterT<Mesh>::decimate(const size_t _n_collapses, const float _mc_factor) {
92 
93  if (_mc_factor > 1.0)
94  return 0;
95 
96  size_t n_collapses_mc = static_cast<size_t>(_mc_factor*_n_collapses);
97  size_t n_collapses_inc = static_cast<size_t>(_n_collapses - n_collapses_mc);
98 
99  size_t r_collapses = 0;
100  if (_mc_factor > 0.0)
101  r_collapses = McDecimaterT<Mesh>::decimate(n_collapses_mc);
102 
103  // returns, if the previous steps were aborted by the observer
104  if (this->observer() && this->observer()->abort())
105  return r_collapses;
106 
107  if (_mc_factor < 1.0)
108  r_collapses += DecimaterT<Mesh>::decimate(n_collapses_inc);
109 
110  return r_collapses;
111 
112 }
113 
114 template<class Mesh>
115 size_t MixedDecimaterT<Mesh>::decimate_to_faces(const size_t _n_vertices,const size_t _n_faces, const float _mc_factor ){
116 
117  if (_mc_factor > 1.0)
118  return 0;
119 
120  std::size_t r_collapses = 0;
121  if (_mc_factor > 0.0)
122  {
123  bool constraintsOnly = (_n_vertices == 0) && (_n_faces == 1);
124  if (!constraintsOnly) {
125  size_t mesh_faces = this->mesh().n_faces();
126  size_t mesh_vertices = this->mesh().n_vertices();
127  //reduce the mesh only for _mc_factor
128  size_t n_vertices_mc = static_cast<size_t>(mesh_vertices - _mc_factor * (mesh_vertices - _n_vertices));
129  size_t n_faces_mc = static_cast<size_t>(mesh_faces - _mc_factor * (mesh_faces - _n_faces));
130 
131  r_collapses = McDecimaterT<Mesh>::decimate_to_faces(n_vertices_mc, n_faces_mc);
132  } else {
133 
134  const size_t samples = this->samples();
135 
136  // MinimalSample count for the McDecimater
137  const size_t min = 2;
138 
139  // Maximal number of samples for the McDecimater
140  const size_t max = samples;
141 
142  // Number of incremental steps
143  const size_t steps = 7;
144 
145  for ( size_t i = 0; i < steps; ++i ) {
146 
147  // Compute number of samples to be used
148  size_t samples = int (double( min) + double(i)/(double(steps)-1.0) * (max-2) ) ;
149 
150  // We won't allow 1 here, as this is the last step in the incremental part
151  float decimaterLevel = (float(i + 1)) * _mc_factor / (float(steps) );
152 
153  this->set_samples(samples);
154  r_collapses += McDecimaterT<Mesh>::decimate_constraints_only(decimaterLevel);
155  }
156  }
157  }
158 
159  //Update the mesh::n_vertices function, otherwise the next Decimater function will delete too much
160  this->mesh().garbage_collection();
161 
162  // returns, if the previous steps were aborted by the observer
163  if (this->observer() && this->observer()->abort())
164  return r_collapses;
165 
166  //reduce the rest of the mesh
167  if (_mc_factor < 1.0) {
168  r_collapses += DecimaterT<Mesh>::decimate_to_faces(_n_vertices,_n_faces);
169  }
170 
171 
172  return r_collapses;
173 }
174 
175 //=============================================================================
176 }// END_NS_MC_DECIMATER
177 } // END_NS_OPENMESH
178 //=============================================================================
size_t decimate_to_faces(size_t _n_vertices=0, size_t _n_faces=0)
size_t decimate_to_faces(const size_t _n_vertices=0, const size_t _n_faces=0, const float _mc_factor=0.8)
size_t decimate(size_t _n_collapses=0)
Perform a number of collapses on the mesh.
Definition: DecimaterT.cc:156
Mesh & mesh()
access mesh. used in modules.
size_t decimate(size_t _n_collapses)
size_t decimate_constraints_only(float _factor)
size_t decimate_to_faces(size_t _n_vertices=0, size_t _n_faces=0)
Attempts to decimate the mesh until a desired vertex or face complexity is achieved.
Definition: DecimaterT.cc:260
size_t decimate(const size_t _n_collapses, const float _mc_factor)
Observer * observer()
Get current observer of a decimater.
MixedDecimaterT(Mesh &_mesh)
Constructor.