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();
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";
130 typename Mesh::HalfedgeHandle bestHandle(-1);
131 typename Mesh::HalfedgeHandle tmpHandle(-1);
132 double bestEnergy = FLT_MAX;
133 double energy = FLT_MAX;
136 for (
int i = 0; i < (int)randomSamples_; ++i) {
140 tmpHandle =
typename Mesh::HalfedgeHandle(
int(randGen.getRand() * double(mesh_.n_halfedges() - 1.0)) );
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() ) ) {
156 if ( energy < bestEnergy ) {
158 bestHandle = tmpHandle;
169 if ( bestEnergy != FLT_MAX ) {
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));
207 if (oldCollapses == n_collapses) {
208 if (collapsesUnchanged ==
false) {
210 collapsesUnchanged =
true;
232 if ( (_nv == 0) && (_nf == 1) )
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";
261 typename Mesh::HalfedgeHandle bestHandle(-1);
262 typename Mesh::HalfedgeHandle tmpHandle(-1);
263 double bestEnergy = FLT_MAX;
264 double energy = FLT_MAX;
267 for (
int i = 0; i < (int) randomSamples_; ++i) {
271 tmpHandle =
typename Mesh::HalfedgeHandle(
int(randGen.getRand() * double(mesh_.n_halfedges() - 1.0)) );
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() ) ) {
287 if (energy < bestEnergy) {
289 bestHandle = tmpHandle;
300 if ( bestEnergy != FLT_MAX ) {
316 if (mesh_.is_boundary(ci.
v0v1) || mesh_.is_boundary(ci.
v1v0))
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));
350 if (oldCollapses == n_collapses) {
351 if (collapsesUnchanged ==
false) {
353 collapsesUnchanged =
true;
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)) {
402 typename Mesh::HalfedgeHandle bestHandle(-1);
403 typename Mesh::HalfedgeHandle tmpHandle(-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) {
416 tmpHandle =
typename Mesh::HalfedgeHandle(
int(randGen.getRand() * double(mesh_.n_halfedges() - 1.0)) );
418 tmpHandle =
typename Mesh::HalfedgeHandle(
int(rand() * randomNormalizer ) );
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() ) ) ) {
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 ) {
477 if (mesh_.is_boundary(ci.
v0v1) || mesh_.is_boundary(ci.
v1v0))
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));
509 if (oldCollapses == n_collapses) {
510 if (collapsesUnchanged ==
false) {
512 collapsesUnchanged =
true;
~McDecimaterT()
Destructor.
bool is_initialized() const
Returns whether decimater has been successfully initialized.
Mesh::HalfedgeHandle v0v1
Halfedge to be collapsed.
void preprocess_collapse(CollapseInfo &_ci)
Pre-process a collapse.
Kernel::VertexFaceIter VertexFaceIter
Circulator.
bool is_collapse_legal(const CollapseInfo &_ci)
void postprocess_collapse(CollapseInfo &_ci)
Post-process a collapse.
Mesh::HalfedgeHandle v1v0
Reverse halfedge.
float collapse_priority(const CollapseInfo &_ci)
Calculate priority of an halfedge collapse (using the modules)
size_t decimate(size_t _n_collapses, bool _only_selected=false)
Decimate (perform _n_collapses collapses). Return number of performed collapses. If _n_collapses is n...
size_t decimate_constraints_only(float _factor, bool _only_selected=false)
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.
bool notify_observer(size_t _n_collapses)
returns false, if abort requested by observer
void set_error_tolerance_factor(double _factor)
Mesh::VertexHandle v1
Remaining vertex.
McDecimaterT(Mesh &_mesh)
Constructor.