2004-07-08 10:25:15 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* VCGLib o o *
|
|
|
|
* Visual and Computer Graphics Library o o *
|
|
|
|
* _ O _ *
|
|
|
|
* Copyright(C) 2004 \/)\/ *
|
|
|
|
* Visual Computing Lab /\/| *
|
|
|
|
* ISTI - Italian National Research Council | *
|
|
|
|
* \ *
|
|
|
|
* All rights reserved. *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
|
|
|
|
* for more details. *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
|
|
$Log: not supported by cvs2svn $
|
2005-11-10 16:38:46 +01:00
|
|
|
Revision 1.15 2005/10/02 23:23:52 cignoni
|
|
|
|
Changed the sense of the < operator for heap: it is reversed according to the stl where highest score elements must float in the heap
|
|
|
|
Completed TimeBudget Termination condition.
|
|
|
|
Parametrized the ClearHeap procedure now there is a HeapSimplexRatio param. Removed dirty printf.
|
|
|
|
|
2005-10-03 01:23:52 +02:00
|
|
|
Revision 1.14 2005/04/14 11:34:33 ponchio
|
|
|
|
*** empty log message ***
|
|
|
|
|
2005-04-14 13:35:09 +02:00
|
|
|
Revision 1.13 2005/01/19 10:33:50 cignoni
|
|
|
|
Improved ClearHeap management
|
|
|
|
|
2005-01-19 11:33:50 +01:00
|
|
|
Revision 1.12 2004/12/10 01:02:48 cignoni
|
|
|
|
added an inline and removed loggng
|
|
|
|
|
2004-12-10 02:03:15 +01:00
|
|
|
Revision 1.11 2004/12/03 21:14:39 ponchio
|
|
|
|
Fixed memory leak...
|
|
|
|
|
2004-12-03 22:14:39 +01:00
|
|
|
Revision 1.10 2004/11/23 10:37:17 cignoni
|
|
|
|
Added a member with a cached copy of the floating Priority() value inside the HeapElem to optimize operator< in heap updating operator
|
|
|
|
|
2004-11-23 11:37:17 +01:00
|
|
|
Revision 1.9 2004/11/05 10:03:47 fiorin
|
|
|
|
Added ModifierType::TriEdgeFlipOp
|
|
|
|
|
2004-11-05 11:03:47 +01:00
|
|
|
Revision 1.8 2004/10/25 07:02:56 ganovelli
|
|
|
|
some inline function, logs on file (precompiler directive)
|
|
|
|
|
2004-10-25 09:02:56 +02:00
|
|
|
Revision 1.7 2004/09/29 17:08:39 ganovelli
|
|
|
|
changed > to < in heapelem comparison
|
|
|
|
|
2004-09-29 19:08:39 +02:00
|
|
|
Revision 1.6 2004/09/28 09:57:08 cignoni
|
|
|
|
Better Doxygen docs
|
|
|
|
|
2004-09-28 11:58:47 +02:00
|
|
|
Revision 1.5 2004/09/15 10:40:20 ponchio
|
|
|
|
typedef LocalOptimization HeapType -> public:
|
|
|
|
|
2004-09-15 12:40:20 +02:00
|
|
|
Revision 1.4 2004/09/08 15:10:59 ganovelli
|
|
|
|
*** empty log message ***
|
|
|
|
|
2004-09-08 17:13:03 +02:00
|
|
|
Revision 1.3 2004/07/27 09:46:15 cignoni
|
|
|
|
First working version of the LocalOptimization/Simplification Framework
|
|
|
|
|
2004-07-27 11:46:15 +02:00
|
|
|
Revision 1.1 2004/07/15 12:04:14 ganovelli
|
|
|
|
minor changes
|
|
|
|
|
|
|
|
Revision 1.2 2004/07/09 10:22:56 ganovelli
|
|
|
|
working draft
|
|
|
|
|
2004-07-09 12:22:56 +02:00
|
|
|
Revision 1.1 2004/07/08 08:25:15 ganovelli
|
|
|
|
first draft
|
|
|
|
|
2004-07-08 10:25:15 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __VCGLIB_LOCALOPTIMIZATION
|
|
|
|
#define __VCGLIB_LOCALOPTIMIZATION
|
|
|
|
#include<vector>
|
|
|
|
#include<algorithm>
|
|
|
|
#include<time.h>
|
|
|
|
#include<math.h>
|
|
|
|
|
|
|
|
namespace vcg{
|
|
|
|
|
2004-07-27 11:46:15 +02:00
|
|
|
template<class MeshType>
|
|
|
|
class LocalOptimization;
|
|
|
|
|
|
|
|
enum ModifierType{ TetraEdgeCollapseOp, TriEdgeSwapOp, TriVertexSplitOp,
|
2004-11-05 11:03:47 +01:00
|
|
|
TriEdgeCollapseOp,TetraEdgeSpliOpt,TetraEdgeSwapOp, TriEdgeFlipOp};
|
2004-07-27 11:46:15 +02:00
|
|
|
/** \addtogroup tetramesh */
|
2004-07-08 10:25:15 +02:00
|
|
|
/*@{*/
|
2004-07-27 11:46:15 +02:00
|
|
|
/// This abstract class define which functions a local modification to be used in the LocalOptimization.
|
|
|
|
template <class MeshType>
|
2004-07-08 10:25:15 +02:00
|
|
|
class LocalModification
|
|
|
|
{
|
|
|
|
public:
|
2004-09-15 12:40:20 +02:00
|
|
|
typedef typename LocalOptimization<MeshType>::HeapType HeapType;
|
|
|
|
typedef typename MeshType::ScalarType ScalarType;
|
|
|
|
|
|
|
|
|
2004-10-25 09:02:56 +02:00
|
|
|
inline LocalModification(){};
|
2004-09-08 17:13:03 +02:00
|
|
|
virtual ~LocalModification(){};
|
2004-07-08 10:25:15 +02:00
|
|
|
|
|
|
|
/// return the type of operation
|
|
|
|
virtual ModifierType IsOfType() = 0 ;
|
|
|
|
|
|
|
|
/// return true if the data have not changed since it was created
|
|
|
|
virtual bool IsUpToDate() = 0 ;
|
|
|
|
|
|
|
|
/// return true if no constraint disallow this operation to be performed (ex: change of topology in edge collapses)
|
|
|
|
virtual bool IsFeasible() = 0;
|
|
|
|
|
|
|
|
/// Compute the priority to be used in the heap
|
|
|
|
virtual ScalarType ComputePriority()=0;
|
|
|
|
|
2004-07-09 12:22:56 +02:00
|
|
|
/// Return the priority to be used in the heap (implement static priority)
|
2004-07-27 11:46:15 +02:00
|
|
|
virtual ScalarType Priority() const =0;
|
2004-07-09 12:22:56 +02:00
|
|
|
|
|
|
|
/// Perform the operation and return the variation in the number of simplicies (>0 is refinement, <0 is simplification)
|
2004-07-27 11:46:15 +02:00
|
|
|
virtual void Execute(MeshType &m)=0;
|
2004-07-09 12:22:56 +02:00
|
|
|
|
|
|
|
/// perform initialization
|
2004-07-27 11:46:15 +02:00
|
|
|
static void Init(MeshType &m, HeapType&);
|
2004-07-09 12:22:56 +02:00
|
|
|
|
2004-07-27 11:46:15 +02:00
|
|
|
virtual const char *Info(MeshType &) {return 0;}
|
2004-07-08 10:25:15 +02:00
|
|
|
/// Update the heap as a consequence of this operation
|
|
|
|
virtual void UpdateHeap(HeapType&)=0;
|
|
|
|
}; //end class local modification
|
|
|
|
|
|
|
|
|
2004-07-27 11:46:15 +02:00
|
|
|
/// LocalOptimization:
|
|
|
|
/// This class implements the algorihms running on 0-1-2-3-simplicial complex that are based on local modification
|
2004-09-28 11:58:47 +02:00
|
|
|
/// The local modification can be and edge_collpase, or an edge_swap, a vertex plit...as far as they implement
|
2004-07-27 11:46:15 +02:00
|
|
|
/// the interface defined in LocalModification.
|
2004-09-28 11:58:47 +02:00
|
|
|
/// Implementation note: in order to keep the local modification itself indepented by its use in this class, they are not
|
|
|
|
/// really derived by LocalModification. Instead, a wrapper is done to this purpose (see vcg/complex/tetramesh/decimation/collapse.h)
|
2004-07-08 10:25:15 +02:00
|
|
|
|
|
|
|
template<class MeshType>
|
|
|
|
class LocalOptimization
|
|
|
|
{
|
|
|
|
public:
|
2005-10-03 01:23:52 +02:00
|
|
|
LocalOptimization(MeshType &mm): m(mm){ ClearTermination();e=0.0;HeapSimplexRatio=5;}
|
2004-07-08 10:25:15 +02:00
|
|
|
|
|
|
|
struct HeapElem;
|
|
|
|
// scalar type
|
|
|
|
typedef typename MeshType::ScalarType ScalarType;
|
|
|
|
// type of the heap
|
|
|
|
typedef typename std::vector<HeapElem> HeapType;
|
|
|
|
// modification type
|
2004-07-27 11:46:15 +02:00
|
|
|
typedef LocalModification <MeshType> LocModType;
|
2004-07-08 10:25:15 +02:00
|
|
|
// modification Pointer type
|
2004-07-27 11:46:15 +02:00
|
|
|
typedef LocalModification <MeshType> * LocModPtrType;
|
2004-07-08 10:25:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// termination conditions
|
2004-07-27 11:46:15 +02:00
|
|
|
enum LOTermination {
|
2004-09-29 19:08:39 +02:00
|
|
|
LOnSimplices = 0x01, // test number of simplicies
|
|
|
|
LOnVertices = 0x02, // test number of verticies
|
|
|
|
LOnOps = 0x04, // test number of operations
|
|
|
|
LOMetric = 0x08, // test Metric (error, quality...instance dependent)
|
|
|
|
LOTime = 0x10 // test how much time is passed since the start
|
2004-07-09 12:22:56 +02:00
|
|
|
} ;
|
|
|
|
|
2004-07-27 11:46:15 +02:00
|
|
|
int tf;
|
|
|
|
|
|
|
|
int nPerfmormedOps,
|
2004-07-08 10:25:15 +02:00
|
|
|
nTargetOps,
|
|
|
|
nTargetSimplices,
|
|
|
|
nTargetVertices;
|
|
|
|
|
2005-10-03 01:23:52 +02:00
|
|
|
float timeBudget;
|
|
|
|
int start;
|
2005-11-10 16:38:46 +01:00
|
|
|
ScalarType currMetric;
|
|
|
|
ScalarType targetMetric;
|
2004-07-08 10:25:15 +02:00
|
|
|
|
2005-10-03 01:23:52 +02:00
|
|
|
// The ratio between Heap size and the number of simplices in the current mesh
|
|
|
|
// When this value is exceeded a ClearHeap Start;
|
|
|
|
|
|
|
|
float HeapSimplexRatio;
|
|
|
|
|
2004-07-08 10:25:15 +02:00
|
|
|
void SetTerminationFlag (int v){tf |= v;}
|
|
|
|
void ClearTerminationFlag (int v){tf &= ~v;}
|
2004-10-25 09:02:56 +02:00
|
|
|
bool IsTerminationFlag (int v){return ((tf & v)!=0);}
|
2004-07-08 10:25:15 +02:00
|
|
|
|
|
|
|
void SetTargetSimplices (int ts ){nTargetSimplices = ts; SetTerminationFlag(LOnSimplices); }
|
2004-07-27 11:46:15 +02:00
|
|
|
void SetTargetVertices (int tv ){nTargetVertices = tv; SetTerminationFlag(LOnVertices); }
|
2004-07-08 10:25:15 +02:00
|
|
|
void SetTargetOperations(int to ){nTargetOps = to; SetTerminationFlag(LOnOps); }
|
2004-07-27 11:46:15 +02:00
|
|
|
|
2004-07-08 10:25:15 +02:00
|
|
|
void SetTargetMetric (ScalarType tm ){targetMetric = tm; SetTerminationFlag(LOMetric); }
|
|
|
|
void SetTimeBudget (float tb ){timeBudget = tb; SetTerminationFlag(LOTime); }
|
|
|
|
|
2004-07-27 11:46:15 +02:00
|
|
|
void ClearTermination()
|
|
|
|
{
|
|
|
|
tf=0;
|
|
|
|
nTargetSimplices=0;
|
|
|
|
nTargetOps=0;
|
|
|
|
targetMetric=0;
|
|
|
|
timeBudget=0;
|
|
|
|
nTargetVertices=0;
|
|
|
|
}
|
2004-07-08 10:25:15 +02:00
|
|
|
/// the mesh to optimize
|
2004-07-27 11:46:15 +02:00
|
|
|
MeshType & m;
|
2004-07-08 10:25:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///the heap of operations
|
|
|
|
HeapType h;
|
|
|
|
|
|
|
|
///the element of the heap
|
2004-07-27 11:46:15 +02:00
|
|
|
// it is just a wrapper of the pointer to the localMod.
|
|
|
|
// std heap does not work for
|
|
|
|
// pointers and we want pointers to have heterogenous heaps.
|
|
|
|
|
2004-07-08 10:25:15 +02:00
|
|
|
struct HeapElem
|
|
|
|
{
|
2004-10-25 09:02:56 +02:00
|
|
|
inline HeapElem(){locModPtr = NULL;}
|
2004-07-09 12:22:56 +02:00
|
|
|
~HeapElem(){}
|
2004-07-08 10:25:15 +02:00
|
|
|
|
|
|
|
///pointer to instance of local modifier
|
|
|
|
LocModPtrType locModPtr;
|
2004-11-23 11:37:17 +01:00
|
|
|
float pri;
|
2004-07-08 10:25:15 +02:00
|
|
|
|
|
|
|
|
2004-10-25 09:02:56 +02:00
|
|
|
inline HeapElem( LocModPtrType _locModPtr)
|
2004-07-08 10:25:15 +02:00
|
|
|
{
|
2004-11-23 11:37:17 +01:00
|
|
|
locModPtr = _locModPtr;
|
|
|
|
pri=float(locModPtr->Priority());
|
2004-07-08 10:25:15 +02:00
|
|
|
};
|
|
|
|
|
2004-07-27 11:46:15 +02:00
|
|
|
/// STL heap has the largest element as the first one.
|
|
|
|
/// usually we mean priority as an error so we should invert the comparison
|
2004-12-10 02:03:15 +01:00
|
|
|
inline const bool operator <(const HeapElem & h) const
|
2004-07-08 10:25:15 +02:00
|
|
|
{
|
2005-10-03 01:23:52 +02:00
|
|
|
return (pri > h.pri);
|
2004-11-23 11:37:17 +01:00
|
|
|
//return (locModPtr->Priority() < h.locModPtr->Priority());
|
2004-07-27 11:46:15 +02:00
|
|
|
}
|
2004-07-08 10:25:15 +02:00
|
|
|
|
|
|
|
bool IsUpToDate()
|
|
|
|
{
|
2004-07-09 12:22:56 +02:00
|
|
|
return locModPtr->IsUpToDate();
|
|
|
|
}
|
2004-07-08 10:25:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Default distructor
|
2004-12-03 22:14:39 +01:00
|
|
|
~LocalOptimization(){
|
|
|
|
typename HeapType::iterator i;
|
|
|
|
for(i = h.begin(); i != h.end(); i++)
|
|
|
|
delete (*i).locModPtr;
|
|
|
|
};
|
2004-09-29 19:08:39 +02:00
|
|
|
|
|
|
|
double e;
|
2004-07-08 10:25:15 +02:00
|
|
|
|
|
|
|
/// main cycle of optimization
|
2004-07-27 11:46:15 +02:00
|
|
|
bool DoOptimization()
|
2004-07-08 10:25:15 +02:00
|
|
|
{
|
2004-07-27 11:46:15 +02:00
|
|
|
start=clock();
|
2004-07-09 12:22:56 +02:00
|
|
|
nPerfmormedOps =0;
|
2004-07-27 11:46:15 +02:00
|
|
|
while( !GoalReached() && !h.empty())
|
2004-09-08 17:13:03 +02:00
|
|
|
{
|
2005-10-03 01:23:52 +02:00
|
|
|
if(h.size()> m.SimplexNumber()*HeapSimplexRatio ) ClearHeap();
|
2004-07-27 11:46:15 +02:00
|
|
|
std::pop_heap(h.begin(),h.end());
|
|
|
|
LocModPtrType locMod = h.back().locModPtr;
|
2005-10-03 01:23:52 +02:00
|
|
|
currMetric=h.back().pri;
|
|
|
|
h.pop_back();
|
2004-07-27 11:46:15 +02:00
|
|
|
|
|
|
|
if( locMod->IsUpToDate() )
|
|
|
|
{
|
|
|
|
//printf("popped out: %s\n",locMod->Info(m));
|
|
|
|
// check if it is feasible
|
|
|
|
if (locMod->IsFeasible())
|
|
|
|
{
|
|
|
|
nPerfmormedOps++;
|
|
|
|
locMod->Execute(m);
|
|
|
|
locMod->UpdateHeap(h);
|
|
|
|
}
|
2004-07-09 12:22:56 +02:00
|
|
|
}
|
2004-07-27 11:46:15 +02:00
|
|
|
//else printf("popped out unfeasible\n");
|
|
|
|
delete locMod;
|
2004-07-09 12:22:56 +02:00
|
|
|
}
|
2004-07-27 11:46:15 +02:00
|
|
|
return !(h.empty());
|
2004-07-08 10:25:15 +02:00
|
|
|
}
|
|
|
|
|
2005-01-19 11:33:50 +01:00
|
|
|
// Toglie dallo heap tutti i collassi non up to date
|
|
|
|
// Chiamata dalla do decimate ogni tanto quando lo heap diventa troppo grande (>fn*3)
|
|
|
|
void ClearHeap()
|
|
|
|
{
|
2005-04-14 13:35:09 +02:00
|
|
|
typename HeapType::iterator hi;
|
2005-11-10 16:38:46 +01:00
|
|
|
//int sz=h.size();
|
2005-01-19 11:33:50 +01:00
|
|
|
for(hi=h.begin();hi!=h.end();++hi)
|
|
|
|
{
|
|
|
|
if(!(*hi).locModPtr->IsUpToDate())
|
|
|
|
{
|
|
|
|
delete (*hi).locModPtr;
|
|
|
|
*hi=h.back();
|
|
|
|
h.pop_back();
|
|
|
|
if(hi==h.end()) break;
|
|
|
|
--hi;
|
|
|
|
}
|
|
|
|
}
|
2005-10-03 01:23:52 +02:00
|
|
|
//printf("\nReduced heap from %7i to %7i (fn %7i) ",sz,h.size(),m.fn);
|
2005-01-19 11:33:50 +01:00
|
|
|
make_heap(h.begin(),h.end());
|
|
|
|
}
|
|
|
|
|
2004-07-08 10:25:15 +02:00
|
|
|
///initialize for all vertex the temporary mark must call only at the start of decimation
|
2004-07-09 12:22:56 +02:00
|
|
|
///by default it takes the first element in the heap and calls Init (static funcion) of that type
|
|
|
|
///of local modification.
|
2004-07-27 11:46:15 +02:00
|
|
|
template <class LocalModificationType> void Init()
|
2004-07-08 10:25:15 +02:00
|
|
|
{
|
2004-07-27 11:46:15 +02:00
|
|
|
m.InitVertexIMark();
|
|
|
|
LocalModificationType::Init(m,h);
|
|
|
|
std::make_heap(h.begin(),h.end());
|
2004-07-08 10:25:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// say if the process is to end or not: the process ends when any of the termination conditions is verified
|
|
|
|
/// override this function to implemetn other tests
|
|
|
|
bool GoalReached(){
|
2004-07-09 12:22:56 +02:00
|
|
|
assert ( ( ( tf & LOnSimplices )==0) || ( nTargetSimplices!= -1));
|
|
|
|
assert ( ( ( tf & LOnVertices )==0) || ( nTargetVertices != -1));
|
|
|
|
assert ( ( ( tf & LOnOps )==0) || ( nTargetOps != -1));
|
|
|
|
assert ( ( ( tf & LOMetric )==0) || ( targetMetric != -1));
|
|
|
|
assert ( ( ( tf & LOTime )==0) || ( timeBudget != -1));
|
|
|
|
|
2004-10-25 09:02:56 +02:00
|
|
|
if ( IsTerminationFlag(LOnSimplices) && ( m.SimplexNumber()<= nTargetSimplices)) return true;
|
|
|
|
if ( IsTerminationFlag(LOnVertices) && ( m.VertexNumber() <= nTargetVertices)) return true;
|
2004-07-27 11:46:15 +02:00
|
|
|
if ( IsTerminationFlag(LOnOps) && (nPerfmormedOps == nTargetOps)) return true;
|
|
|
|
if ( IsTerminationFlag(LOMetric) && ( currMetric > targetMetric)) return true;
|
|
|
|
if ( IsTerminationFlag(LOTime) && ( (clock()-start)/(float)CLOCKS_PER_SEC > timeBudget)) return true;
|
2004-07-09 12:22:56 +02:00
|
|
|
return false;
|
2004-07-08 10:25:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///erase from the heap the operations that are out of date
|
2005-01-19 11:33:50 +01:00
|
|
|
void ClearHeapOld()
|
2004-07-08 10:25:15 +02:00
|
|
|
{
|
|
|
|
typename HeapType::iterator hi;
|
|
|
|
for(hi=h.begin();hi!=h.end();++hi)
|
2004-07-09 12:22:56 +02:00
|
|
|
if(!(*hi).locModPtr->IsUpToDate())
|
2004-07-08 10:25:15 +02:00
|
|
|
{
|
|
|
|
*hi=h.back();
|
|
|
|
h.pop_back();
|
|
|
|
if(hi==h.end()) break;
|
|
|
|
}
|
|
|
|
//printf("\nReduced heap from %i to %i",sz,h.size());
|
|
|
|
make_heap(h.begin(),h.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
};//end class decimation
|
|
|
|
|
|
|
|
}//end namespace
|
|
|
|
#endif
|