Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 #ifndef __VCGLIB_LOCALOPTIMIZATION
00093 #define __VCGLIB_LOCALOPTIMIZATION
00094 #include<vector>
00095 #include<algorithm>
00096 #include<time.h>
00097 #include<math.h>
00098 #include<vcg/complex/complex.h>
00099
00100 namespace vcg{
00101
00102
00103 class BaseParameterClass { };
00104
00105 template<class MeshType>
00106 class LocalOptimization;
00107
00108 enum ModifierType{ TetraEdgeCollapseOp, TriEdgeSwapOp, TriVertexSplitOp,
00109 TriEdgeCollapseOp,TetraEdgeSpliOpt,TetraEdgeSwapOp, TriEdgeFlipOp,
00110 QuadDiagCollapseOp, QuadEdgeCollapseOp};
00113
00114 template <class MeshType>
00115 class LocalModification
00116 {
00117 public:
00118 typedef typename LocalOptimization<MeshType>::HeapType HeapType;
00119 typedef typename MeshType::ScalarType ScalarType;
00120
00121
00122 inline LocalModification(){}
00123 virtual ~LocalModification(){}
00124
00126 virtual ModifierType IsOfType() = 0 ;
00127
00129 virtual bool IsUpToDate() const = 0 ;
00130
00132 virtual bool IsFeasible(BaseParameterClass *pp) = 0;
00133
00135 virtual ScalarType ComputePriority(BaseParameterClass *pp)=0;
00136
00138 virtual ScalarType Priority() const =0;
00139
00141 virtual void Execute(MeshType &m, BaseParameterClass *pp)=0;
00142
00144 static void Init(MeshType &m, HeapType&, BaseParameterClass *pp);
00145
00151 static float HeapSimplexRatio(BaseParameterClass *) {return 6.0f;}
00152
00153 virtual const char *Info(MeshType &) {return 0;}
00155 virtual void UpdateHeap(HeapType&, BaseParameterClass *pp)=0;
00156 };
00157
00158
00165
00166 template<class MeshType>
00167 class LocalOptimization
00168 {
00169 public:
00170 LocalOptimization(MeshType &mm, BaseParameterClass *_pp): m(mm){ ClearTermination();e=0.0;HeapSimplexRatio=5; pp=_pp;}
00171
00172 struct HeapElem;
00173
00174 typedef typename MeshType::ScalarType ScalarType;
00175
00176 typedef typename std::vector<HeapElem> HeapType;
00177
00178 typedef LocalModification <MeshType> LocModType;
00179
00180 typedef LocalModification <MeshType> * LocModPtrType;
00181
00182
00183
00185 enum LOTermination {
00186 LOnSimplices = 0x01,
00187 LOnVertices = 0x02,
00188 LOnOps = 0x04,
00189 LOMetric = 0x08,
00190 LOTime = 0x10
00191 } ;
00192
00193 int tf;
00194
00195 int nPerfmormedOps,
00196 nTargetOps,
00197 nTargetSimplices,
00198 nTargetVertices;
00199
00200 float timeBudget;
00201 clock_t start;
00202 ScalarType currMetric;
00203 ScalarType targetMetric;
00204 BaseParameterClass *pp;
00205
00206
00207
00208
00209 float HeapSimplexRatio;
00210
00211 void SetTerminationFlag (int v){tf |= v;}
00212 void ClearTerminationFlag (int v){tf &= ~v;}
00213 bool IsTerminationFlag (int v){return ((tf & v)!=0);}
00214
00215 void SetTargetSimplices (int ts ){nTargetSimplices = ts; SetTerminationFlag(LOnSimplices); }
00216 void SetTargetVertices (int tv ){nTargetVertices = tv; SetTerminationFlag(LOnVertices); }
00217 void SetTargetOperations(int to ){nTargetOps = to; SetTerminationFlag(LOnOps); }
00218
00219 void SetTargetMetric (ScalarType tm ){targetMetric = tm; SetTerminationFlag(LOMetric); }
00220 void SetTimeBudget (float tb ){timeBudget = tb; SetTerminationFlag(LOTime); }
00221
00222 void ClearTermination()
00223 {
00224 tf=0;
00225 nTargetSimplices=0;
00226 nTargetOps=0;
00227 targetMetric=0;
00228 timeBudget=0;
00229 nTargetVertices=0;
00230 }
00232 MeshType & m;
00233
00234
00235
00237 HeapType h;
00238
00240
00241
00242
00243
00244 struct HeapElem
00245 {
00246 inline HeapElem(){locModPtr = NULL;}
00247 ~HeapElem(){}
00248
00250 LocModPtrType locModPtr;
00251 float pri;
00252
00253
00254 inline HeapElem( LocModPtrType _locModPtr)
00255 {
00256 locModPtr = _locModPtr;
00257 pri=float(locModPtr->Priority());
00258 };
00259
00262 inline bool operator <(const HeapElem & h) const
00263 {
00264 return (pri > h.pri);
00265
00266 }
00267
00268 bool IsUpToDate() const
00269 {
00270 return locModPtr->IsUpToDate();
00271 }
00272 };
00273
00274
00275
00277 ~LocalOptimization(){
00278 typename HeapType::iterator i;
00279 for(i = h.begin(); i != h.end(); i++)
00280 delete (*i).locModPtr;
00281 };
00282
00283 double e;
00284
00286 bool DoOptimization()
00287 {
00288 start=clock();
00289 nPerfmormedOps =0;
00290 while( !GoalReached() && !h.empty())
00291 {
00292 if(h.size()> m.SimplexNumber()*HeapSimplexRatio ) ClearHeap();
00293 std::pop_heap(h.begin(),h.end());
00294 LocModPtrType locMod = h.back().locModPtr;
00295 currMetric=h.back().pri;
00296 h.pop_back();
00297
00298 if( locMod->IsUpToDate() )
00299 {
00300
00301
00302 if (locMod->IsFeasible(this->pp))
00303 {
00304 nPerfmormedOps++;
00305 locMod->Execute(m,this->pp);
00306 locMod->UpdateHeap(h,this->pp);
00307 }
00308 }
00309
00310 delete locMod;
00311 }
00312 return !(h.empty());
00313 }
00314
00315
00316
00317
00318 void ClearHeap()
00319 {
00320 typename HeapType::iterator hi;
00321
00322 for(hi=h.begin();hi!=h.end();)
00323 {
00324 if(!(*hi).locModPtr->IsUpToDate())
00325 {
00326 delete (*hi).locModPtr;
00327 *hi=h.back();
00328 if(&*hi==&h.back())
00329 {
00330 hi=h.end();
00331 h.pop_back();
00332 break;
00333 }
00334 h.pop_back();
00335 continue;
00336 }
00337 ++hi;
00338 }
00339
00340 make_heap(h.begin(),h.end());
00341 }
00342
00346 template <class LocalModificationType> void Init()
00347 {
00348 vcg::tri::InitVertexIMark(m);
00349
00350
00351 HeapSimplexRatio = LocalModificationType::HeapSimplexRatio(pp);
00352
00353 LocalModificationType::Init(m,h,pp);
00354 std::make_heap(h.begin(),h.end());
00355 if(!h.empty()) currMetric=h.front().pri;
00356 }
00357
00358
00359 template <class LocalModificationType> void Finalize()
00360 {
00361 LocalModificationType::Finalize(m,h,pp);
00362 }
00363
00364
00367 bool GoalReached(){
00368 assert ( ( ( tf & LOnSimplices )==0) || ( nTargetSimplices!= -1));
00369 assert ( ( ( tf & LOnVertices )==0) || ( nTargetVertices != -1));
00370 assert ( ( ( tf & LOnOps )==0) || ( nTargetOps != -1));
00371 assert ( ( ( tf & LOMetric )==0) || ( targetMetric != -1));
00372 assert ( ( ( tf & LOTime )==0) || ( timeBudget != -1));
00373
00374 if ( IsTerminationFlag(LOnSimplices) && ( m.SimplexNumber()<= nTargetSimplices)) return true;
00375 if ( IsTerminationFlag(LOnVertices) && ( m.VertexNumber() <= nTargetVertices)) return true;
00376 if ( IsTerminationFlag(LOnOps) && (nPerfmormedOps == nTargetOps)) return true;
00377 if ( IsTerminationFlag(LOMetric) && ( currMetric > targetMetric)) return true;
00378 if ( IsTerminationFlag(LOTime) )
00379 {
00380 clock_t cur = clock();
00381 if(cur<start)
00382 return true;
00383 else
00384 if ( (cur - start)/(double)CLOCKS_PER_SEC > timeBudget) return true;
00385 }
00386 return false;
00387 }
00388
00389
00390
00392 void ClearHeapOld()
00393 {
00394 typename HeapType::iterator hi;
00395 for(hi=h.begin();hi!=h.end();++hi)
00396 if(!(*hi).locModPtr->IsUpToDate())
00397 {
00398 *hi=h.back();
00399 h.pop_back();
00400 if(hi==h.end()) break;
00401 }
00402
00403 make_heap(h.begin(),h.end());
00404 }
00405
00406 };
00407
00408 }
00409 #endif