51#define OPENMESH_MULTIPLE_CHOICE_DECIMATER_DECIMATERT_CC
58#if defined(OM_CC_MIPS)
65# include <OpenMesh/Core/Utils/RandomNumberGenerator.hh>
78 mesh_(_mesh), randomSamples_(10) {
81 mesh_.request_vertex_status();
82 mesh_.request_halfedge_status();
83 mesh_.request_edge_status();
84 mesh_.request_face_status();
93 mesh_.release_vertex_status();
94 mesh_.release_edge_status();
95 mesh_.release_halfedge_status();
96 mesh_.release_face_status();
104 if (!this->is_initialized())
107 unsigned int n_collapses(0);
109 bool collapsesUnchanged =
false;
111 unsigned int oldCollapses = 0;
114 unsigned int noCollapses = 0;
120 const bool update_normals = mesh_.has_face_normals();
122 while ( n_collapses < _n_collapses) {
124 if (noCollapses > 20) {
125 omlog() <<
"[McDecimater] : no collapses performed in over 20 iterations in a row\n";
132 double bestEnergy = FLT_MAX;
133 double energy = FLT_MAX;
136 for (
int i = 0; i < (int)randomSamples_; ++i) {
142 tmpHandle =
typename Mesh::HalfedgeHandle( (
double(rand()) /
double(RAND_MAX) ) *
double(mesh_.n_halfedges()-1) );
146 if ( ! mesh_.status(tmpHandle).deleted() && (!_only_selected || mesh_.status(tmpHandle).selected() ) ) {
151 if (this->is_collapse_legal(ci)) {
152 energy = this->collapse_priority(ci);
156 if ( energy < bestEnergy ) {
158 bestHandle = tmpHandle;
169 if ( bestEnergy != FLT_MAX ) {
175 if (!this->is_collapse_legal(ci))
179 this->preprocess_collapse(ci);
182 mesh_.collapse(bestHandle);
186 oldCollapses = n_collapses;
188 collapsesUnchanged =
false;
194 for (; vf_it.is_valid(); ++vf_it)
195 if (!mesh_.status(*vf_it).deleted())
196 mesh_.set_normal(*vf_it, mesh_.calc_face_normal(*vf_it));
200 this->postprocess_collapse(ci);
203 if (!this->notify_observer(n_collapses))
207 if (oldCollapses == n_collapses) {
208 if (collapsesUnchanged ==
false) {
210 collapsesUnchanged =
true;
228 if (!this->is_initialized())
232 if ( (_nv == 0) && (_nf == 1) )
233 return decimate_constraints_only(1.0);
235 size_t nv = mesh_.n_vertices();
236 size_t nf = mesh_.n_faces();
237 unsigned int n_collapses(0);
240 bool collapsesUnchanged =
false;
242 unsigned int oldCollapses = 0;
245 unsigned int noCollapses = 0;
251 const bool update_normals = mesh_.has_face_normals();
253 while ((_nv < nv) && (_nf < nf)) {
255 if (noCollapses > 20) {
256 omlog() <<
"[McDecimater] : no collapses performed in over 20 iterations in a row\n";
263 double bestEnergy = FLT_MAX;
264 double energy = FLT_MAX;
267 for (
int i = 0; i < (int) randomSamples_; ++i) {
273 tmpHandle =
typename Mesh::HalfedgeHandle( (
double(rand()) /
double(RAND_MAX) ) *
double(mesh_.n_halfedges() - 1));
277 if ( ! mesh_.status(tmpHandle).deleted() && (!_only_selected || mesh_.status(tmpHandle).selected() ) ) {
282 if (this->is_collapse_legal(ci)) {
283 energy = this->collapse_priority(ci);
287 if (energy < bestEnergy) {
289 bestHandle = tmpHandle;
300 if ( bestEnergy != FLT_MAX ) {
306 if (!this->is_collapse_legal(ci))
316 if (mesh_.is_boundary(ci.
v0v1) || mesh_.is_boundary(ci.
v1v0))
322 this->preprocess_collapse(ci);
325 mesh_.collapse(bestHandle);
329 oldCollapses = n_collapses;
331 collapsesUnchanged =
false;
337 for (; vf_it.is_valid(); ++vf_it)
338 if (!mesh_.status(*vf_it).deleted())
339 mesh_.set_normal(*vf_it, mesh_.calc_face_normal(*vf_it));
343 this->postprocess_collapse(ci);
346 if (!this->notify_observer(n_collapses))
350 if (oldCollapses == n_collapses) {
351 if (collapsesUnchanged ==
false) {
353 collapsesUnchanged =
true;
371 if (!this->is_initialized())
375 this->set_error_tolerance_factor(_factor);
377 unsigned int n_collapses(0);
378 size_t nv = mesh_.n_vertices();
379 size_t nf = mesh_.n_faces();
381 bool lastCollapseIllegal =
false;
383 unsigned int illegalCollapses = 0;
385 bool collapsesUnchanged =
false;
387 unsigned int oldCollapses = 0;
390 unsigned int noCollapses = 0;
392 double energy = FLT_MAX;
393 double bestEnergy = FLT_MAX;
399 while ((noCollapses <= 50) && (illegalCollapses <= 50) && (nv > 0) && (nf > 1)) {
404 bestEnergy = FLT_MAX;
408 const double randomNormalizer = (1.0 / RAND_MAX) * (mesh_.n_halfedges() - 1);
412 for (
int i = 0; i < (int) randomSamples_; ++i) {
422 if (!mesh_.status(mesh_.edge_handle(tmpHandle)).deleted() &&
423 (!_only_selected || ( mesh_.status(mesh_.to_vertex_handle(tmpHandle)).selected() && mesh_.status(mesh_.from_vertex_handle(tmpHandle)).selected() ) ) ) {
428 if (this->is_collapse_legal(ci)) {
430 energy = this->collapse_priority(ci);
433 if (lastCollapseIllegal) {
436 illegalCollapses = 1;
437 lastCollapseIllegal =
true;
441 illegalCollapses = 0;
442 lastCollapseIllegal =
false;
445 if (energy < bestEnergy) {
447 bestHandle = tmpHandle;
461 if ( bestEnergy != FLT_MAX ) {
467 if (!this->is_collapse_legal(ci))
477 if (mesh_.is_boundary(ci.
v0v1) || mesh_.is_boundary(ci.
v1v0))
483 this->preprocess_collapse(ci);
486 mesh_.collapse(bestHandle);
490 oldCollapses = n_collapses;
492 collapsesUnchanged =
false;
497 for (; vf_it.is_valid(); ++vf_it)
498 if (!mesh_.status(*vf_it).deleted())
499 mesh_.set_normal(*vf_it, mesh_.calc_face_normal(*vf_it));
502 this->postprocess_collapse(ci);
505 if (!this->notify_observer(n_collapses))
509 if (oldCollapses == n_collapses) {
510 if (collapsesUnchanged ==
false) {
512 collapsesUnchanged =
true;
522 this->set_error_tolerance_factor(1.0);
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:59
Kernel::VertexFaceIter VertexFaceIter
Circulator.
Definition: PolyMeshT.hh:166
Kernel::HalfedgeHandle HalfedgeHandle
Scalar type.
Definition: PolyMeshT.hh:137
Generate a random number between 0.0 and 1.0 with a guaranteed resolution ( Number of possible values...
Definition: RandomNumberGenerator.hh:78
double getRand() const
returns a random double between 0.0 and 1.0 with a guaranteed resolution
Definition: RandomNumberGenerator.cc:82
Definition: BaseDecimaterT.hh:86
Stores information about a halfedge collapse.
Definition: CollapseInfoT.hh:74
Mesh::HalfedgeHandle v0v1
Halfedge to be collapsed.
Definition: CollapseInfoT.hh:80
Mesh::HalfedgeHandle v1v0
Reverse halfedge.
Definition: CollapseInfoT.hh:81
Mesh::VertexHandle v1
Remaining vertex.
Definition: CollapseInfoT.hh:83
size_t decimate_constraints_only(float _factor, bool _only_selected=false)
Decimate only with constraints, while _factor gives the percentage of the constraints that should be ...
Definition: McDecimaterT_impl.hh:369
~McDecimaterT()
Destructor.
Definition: McDecimaterT_impl.hh:91
size_t decimate_to_faces(size_t _n_vertices=0, size_t _n_faces=0, bool _only_selected=false)
Attempts to decimate the mesh until a desired vertex or face complexity is achieved.
Definition: McDecimaterT_impl.hh:226
size_t decimate(size_t _n_collapses, bool _only_selected=false)
Decimate (perform _n_collapses collapses).
Definition: McDecimaterT_impl.hh:102
McDecimaterT(Mesh &_mesh)
Constructor.
Definition: McDecimaterT_impl.hh:76
Base class for all decimation modules.
Definition: ModBaseT.hh:193