add observer pattern to all decimaters
closes #2366 git-svn-id: http://www.openmesh.org/svnrepo/OpenMesh/trunk@1197 fdac6126-5c0c-442c-9429-916003d36597
This commit is contained in:
@@ -67,7 +67,7 @@ namespace Decimater {
|
||||
|
||||
template<class Mesh>
|
||||
BaseDecimaterT<Mesh>::BaseDecimaterT(Mesh& _mesh) :
|
||||
mesh_(_mesh), cmodule_(NULL), initialized_(false) {
|
||||
mesh_(_mesh), cmodule_(NULL), initialized_(false), observer_(NULL) {
|
||||
// default properties
|
||||
mesh_.request_vertex_status();
|
||||
mesh_.request_edge_status();
|
||||
|
||||
@@ -71,6 +71,30 @@ namespace Decimater {
|
||||
//== CLASS DEFINITION =========================================================
|
||||
|
||||
|
||||
class Observer
|
||||
{
|
||||
public:
|
||||
Observer(size_t _step) :
|
||||
step_(_step) {
|
||||
}
|
||||
|
||||
virtual ~Observer() {
|
||||
}
|
||||
|
||||
size_t get_step() const { return step_; }
|
||||
// give the notification step size
|
||||
void set_step(size_t _step) { step_ = _step; }
|
||||
|
||||
//notifies about a finished decimater step with the given step number
|
||||
virtual void notify(size_t _step) = 0;
|
||||
//after notification, ask for abort or not, return true, if the decimater should abort
|
||||
virtual bool abort() const = 0;
|
||||
|
||||
private:
|
||||
size_t step_;
|
||||
};
|
||||
|
||||
|
||||
/** base class decimater framework
|
||||
\see BaseDecimaterT, \ref decimater_docu
|
||||
*/
|
||||
@@ -113,6 +137,17 @@ public: //------------------------------------------------------ public methods
|
||||
|
||||
public: //--------------------------------------------------- module management
|
||||
|
||||
// Observer interface
|
||||
void set_observer(Observer* _o)
|
||||
{
|
||||
observer_ = _o;
|
||||
}
|
||||
|
||||
Observer* observer()
|
||||
{
|
||||
return observer_;
|
||||
}
|
||||
|
||||
/// access mesh. used in modules.
|
||||
Mesh& mesh() { return mesh_; }
|
||||
|
||||
@@ -166,6 +201,17 @@ public: //--------------------------------------------------- module management
|
||||
|
||||
protected:
|
||||
|
||||
//returns false, if abort requested by observer
|
||||
bool notify_observer(size_t _n_collapses)
|
||||
{
|
||||
if (observer() && _n_collapses % observer()->get_step() == 0)
|
||||
{
|
||||
observer()->notify(_n_collapses);
|
||||
return !observer()->abort();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Reset the initialized flag, and clear the bmodules_ and cmodule_
|
||||
void set_uninitialized() {
|
||||
initialized_ = false;
|
||||
@@ -234,6 +280,8 @@ private: //------------------------------------------------------- private data
|
||||
// Flag if all modules were initialized
|
||||
bool initialized_;
|
||||
|
||||
// observer
|
||||
Observer* observer_;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -136,13 +136,13 @@ void DecimaterT<Mesh>::heap_vertex(VertexHandle _vh) {
|
||||
mesh_.property(priority_, _vh) = -1;
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template<class Mesh>
|
||||
size_t DecimaterT<Mesh>::decimate(size_t _n_collapses) {
|
||||
|
||||
if (!this->is_initialized())
|
||||
return 0;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template<class Mesh>
|
||||
size_t DecimaterT<Mesh>::decimate(size_t _n_collapses) {
|
||||
|
||||
if (!this->is_initialized())
|
||||
return 0;
|
||||
|
||||
typename Mesh::VertexIter v_it, v_end(mesh_.vertices_end());
|
||||
typename Mesh::VertexHandle vp;
|
||||
@@ -212,12 +212,16 @@ size_t DecimaterT<Mesh>::decimate(size_t _n_collapses) {
|
||||
|
||||
// update heap (former one ring of decimated vertex)
|
||||
for (s_it = support.begin(), s_end = support.end(); s_it != s_end; ++s_it) {
|
||||
assert(!mesh_.status(*s_it).deleted());
|
||||
heap_vertex(*s_it);
|
||||
}
|
||||
}
|
||||
|
||||
// delete heap
|
||||
assert(!mesh_.status(*s_it).deleted());
|
||||
heap_vertex(*s_it);
|
||||
}
|
||||
|
||||
// notify observer and stop if the observer requests it
|
||||
if (!this->notify_observer(n_collapses))
|
||||
return n_collapses;
|
||||
}
|
||||
|
||||
// delete heap
|
||||
heap_.reset();
|
||||
|
||||
|
||||
@@ -226,13 +230,13 @@ size_t DecimaterT<Mesh>::decimate(size_t _n_collapses) {
|
||||
return n_collapses;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class Mesh>
|
||||
size_t DecimaterT<Mesh>::decimate_to_faces(size_t _nv, size_t _nf) {
|
||||
|
||||
if (!this->is_initialized())
|
||||
return 0;
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class Mesh>
|
||||
size_t DecimaterT<Mesh>::decimate_to_faces(size_t _nv, size_t _nf) {
|
||||
|
||||
if (!this->is_initialized())
|
||||
return 0;
|
||||
|
||||
if (_nv >= mesh_.n_vertices() || _nf >= mesh_.n_faces())
|
||||
return 0;
|
||||
@@ -313,12 +317,16 @@ size_t DecimaterT<Mesh>::decimate_to_faces(size_t _nv, size_t _nf) {
|
||||
|
||||
// update heap (former one ring of decimated vertex)
|
||||
for (s_it = support.begin(), s_end = support.end(); s_it != s_end; ++s_it) {
|
||||
assert(!mesh_.status(*s_it).deleted());
|
||||
heap_vertex(*s_it);
|
||||
}
|
||||
}
|
||||
|
||||
// delete heap
|
||||
assert(!mesh_.status(*s_it).deleted());
|
||||
heap_vertex(*s_it);
|
||||
}
|
||||
|
||||
// notify observer and stop if the observer requests it
|
||||
if (!this->notify_observer(n_collapses))
|
||||
return n_collapses;
|
||||
}
|
||||
|
||||
// delete heap
|
||||
heap_.reset();
|
||||
|
||||
|
||||
|
||||
@@ -198,6 +198,10 @@ size_t McDecimaterT<Mesh>::decimate(size_t _n_collapses) {
|
||||
// post-process collapse
|
||||
this->postprocess_collapse(ci);
|
||||
|
||||
// notify observer and stop if the observer requests it
|
||||
if (!this->notify_observer(n_collapses))
|
||||
return n_collapses;
|
||||
|
||||
} else {
|
||||
if (oldCollapses == n_collapses) {
|
||||
if (collapsesUnchanged == false) {
|
||||
@@ -337,6 +341,10 @@ size_t McDecimaterT<Mesh>::decimate_to_faces(size_t _nv, size_t _nf) {
|
||||
// post-process collapse
|
||||
this->postprocess_collapse(ci);
|
||||
|
||||
// notify observer and stop if the observer requests it
|
||||
if (!this->notify_observer(n_collapses))
|
||||
return n_collapses;
|
||||
|
||||
} else {
|
||||
if (oldCollapses == n_collapses) {
|
||||
if (collapsesUnchanged == false) {
|
||||
@@ -491,6 +499,10 @@ size_t McDecimaterT<Mesh>::decimate_constraints_only(float _factor) {
|
||||
// post-process collapse
|
||||
this->postprocess_collapse(ci);
|
||||
|
||||
// notify observer and stop if the observer requests it
|
||||
if (!this->notify_observer(n_collapses))
|
||||
return n_collapses;
|
||||
|
||||
} else {
|
||||
if (oldCollapses == n_collapses) {
|
||||
if (collapsesUnchanged == false) {
|
||||
|
||||
@@ -92,6 +92,11 @@ size_t MixedDecimaterT<Mesh>::decimate(const size_t _n_collapses, const float _m
|
||||
size_t r_collapses = 0;
|
||||
if (_mc_factor > 0.0)
|
||||
r_collapses = McDecimaterT<Mesh>::decimate(n_collapses_mc);
|
||||
|
||||
// returns, if the previous steps were aborted by the observer
|
||||
if (this->observer() && this->observer()->abort())
|
||||
return r_collapses;
|
||||
|
||||
if (_mc_factor < 1.0)
|
||||
r_collapses += DecimaterT<Mesh>::decimate(n_collapses_inc);
|
||||
|
||||
@@ -147,6 +152,10 @@ size_t MixedDecimaterT<Mesh>::decimate_to_faces(const size_t _n_vertices,const
|
||||
//Update the mesh::n_vertices function, otherwise the next Decimater function will delete too much
|
||||
this->mesh().garbage_collection();
|
||||
|
||||
// returns, if the previous steps were aborted by the observer
|
||||
if (this->observer() && this->observer()->abort())
|
||||
return r_collapses;
|
||||
|
||||
//reduce the rest of the mesh
|
||||
if (_mc_factor < 1.0) {
|
||||
r_collapses += DecimaterT<Mesh>::decimate_to_faces(_n_vertices,_n_faces);
|
||||
|
||||
Reference in New Issue
Block a user