vcglib/vcg/complex/local_optimization/tri_edge_flip.h

399 lines
11 KiB
C
Raw Normal View History

2004-11-05 10:57:18 +01: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. *
* *
2007-01-15 12:41:09 +01:00
* This program is free software; you can redistribute it and/or modify *
2004-11-05 10:57:18 +01:00
* 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. *
* *
****************************************************************************/
#include <vcg/complex/local_optimization.h>
#include <vcg/simplex/face/topology.h>
//#include <vcg/space/point3.h>
#include <vcg/space/triangle3.h>
2004-11-05 10:57:18 +01:00
namespace vcg
{
namespace tri
{
/** \addtogroup trimesh */
/* @{ */
/*!
* This Class is specialization of LocalModification for the edge flip
2007-01-15 12:41:09 +01:00
* It wraps the atomic operation EdgeFlip to be used in a optimization routine.
2004-11-05 10:57:18 +01:00
* Note that it has knowledge of the heap of the class LocalOptimization because
* it is responsible of updating it after a flip has been performed
2007-01-15 12:41:09 +01:00
* This is the simplest edge flipping class.
* It flips an edge only if two adjacent faces are coplanar and the
* quality of the faces improves after the flip.
2004-11-05 10:57:18 +01:00
*/
template <class TRIMESH_TYPE, class MYTYPE>
2007-01-15 12:41:09 +01:00
class PlanarEdgeFlip : public LocalOptimization< TRIMESH_TYPE >::LocModType
2004-11-05 10:57:18 +01:00
{
protected:
typedef typename TRIMESH_TYPE::FaceType FaceType;
typedef typename TRIMESH_TYPE::FacePointer FacePointer;
typedef typename TRIMESH_TYPE::FaceIterator FaceIterator;
typedef typename TRIMESH_TYPE::VertexType VertexType;
typedef typename TRIMESH_TYPE::ScalarType ScalarType;
typedef typename TRIMESH_TYPE::VertexPointer VertexPointer;
typedef typename TRIMESH_TYPE::CoordType CoordType;
typedef vcg::face::Pos<FaceType> PosType;
2004-11-05 10:57:18 +01:00
typedef typename LocalOptimization<TRIMESH_TYPE>::HeapElem HeapElem;
typedef typename LocalOptimization<TRIMESH_TYPE>::HeapType HeapType;
/*!
* the pos of the flipping
*/
PosType _pos;
/*!
* priority in the heap
*/
ScalarType _priority;
/*!
* Mark for updating
*/
int _localMark;
/*!
* mark for up_dating
*/
static int& GlobalMark()
{
static int im = 0;
return im;
}
2004-11-05 10:57:18 +01:00
public:
/*!
* Default constructor
*/
2007-01-15 12:41:09 +01:00
inline PlanarEdgeFlip()
{}
2004-11-05 10:57:18 +01:00
/*!
* Constructor with <I>pos</I> type
*/
2007-01-15 12:41:09 +01:00
inline PlanarEdgeFlip(PosType pos, int mark)
2004-11-05 10:57:18 +01:00
{
_pos = pos;
_localMark = mark;
_priority = ComputePriority();
}
2004-11-05 10:57:18 +01:00
2007-01-15 12:41:09 +01:00
/*!
* Copy Constructor
*/
inline PlanarEdgeFlip(const PlanarEdgeFlip &par)
{
_pos = par.GetPos();
_localMark = par.GetMark();
_priority = par.Priority();
}
2007-01-15 12:41:09 +01:00
2004-11-05 10:57:18 +01:00
/*!
*/
2007-01-15 12:41:09 +01:00
~PlanarEdgeFlip()
2004-11-05 10:57:18 +01:00
{
}
2004-11-05 10:57:18 +01:00
2007-01-15 12:41:09 +01:00
/*!
* Parameter
*/
static ScalarType &CoplanarAngleThresholdDeg()
{
2007-01-15 12:41:09 +01:00
static ScalarType _CoplanarAngleThresholdDeg = 0.01f;
return _CoplanarAngleThresholdDeg;
}
inline PosType GetPos() {return _pos;}
inline int GetMark(){return _localMark;}
2004-11-05 10:57:18 +01:00
/*!
* Return the LocalOptimization type
*/
ModifierType IsOfType()
{
return TriEdgeFlipOp;
}
2004-11-05 10:57:18 +01:00
/*!
* Check if the pos is updated
*/
bool IsUpToDate()
{
int MostRecentVertexMark = _pos.F()->V(0)->IMark();
MostRecentVertexMark = vcg::math::Max<ScalarType>(MostRecentVertexMark, _pos.F()->V(1)->IMark());
MostRecentVertexMark = vcg::math::Max<ScalarType>(MostRecentVertexMark, _pos.F()->V(2)->IMark());
2004-11-05 10:57:18 +01:00
2007-01-15 12:41:09 +01:00
return ( _localMark >= MostRecentVertexMark );
}
2004-11-05 10:57:18 +01:00
/*!
2007-01-15 12:41:09 +01:00
*
Check if this flipping operation can be performed.
It is a topological and geometrical check.
2004-11-05 10:57:18 +01:00
*/
2007-01-15 12:41:09 +01:00
virtual bool IsFeasible()
2004-11-05 10:57:18 +01:00
{
2007-01-15 12:41:09 +01:00
if( math::ToDeg( Angle( _pos.FFlip()->cN() , _pos.F()->cN() ) ) > CoplanarAngleThresholdDeg() ) return false;
return vcg::face::CheckFlipEdge(*_pos.f, _pos.z);
}
2004-11-05 10:57:18 +01:00
2007-01-15 12:41:09 +01:00
2004-11-05 10:57:18 +01:00
/*!
* Compute the priority of this optimization
*/
2007-01-15 12:41:09 +01:00
/*
0
/|\
/ | \
2007-01-15 12:41:09 +01:00
1 | 3
\ | /
\|/
2
*/
virtual ScalarType ComputePriority()
2004-11-05 10:57:18 +01:00
{
2007-01-15 12:41:09 +01:00
CoordType v0,v1,v2,v3;
PosType app = _pos;
2007-01-15 12:41:09 +01:00
v0 = app.v->P();
app.FlipE(); app.FlipV();
v1 = app.v->P();
app.FlipE(); app.FlipV();
v2 = app.v->P();
app.FlipE(); app.FlipF(); app.FlipE(); app.FlipV();
v3 = app.v->P();
ScalarType Qa = Quality(v0,v1,v2);
ScalarType Qb = Quality(v0,v2,v3);
ScalarType QaAfter = Quality(v0,v1,v3);
ScalarType QbAfter = Quality(v1,v2,v3);
// higher the quality better the triangle.
// swaps that improve the worst quality more are performed before
// (e.g. they have an higher priority)
_priority = vcg::math::Max<ScalarType>(QaAfter,QbAfter) - vcg::math::Min<ScalarType>(Qa,Qb) ;
2007-01-15 12:41:09 +01:00
_priority *=-1;
2004-11-05 10:57:18 +01:00
return _priority;
}
2004-11-05 10:57:18 +01:00
/*!
* Return the priority of this optimization
*/
2007-01-15 12:41:09 +01:00
virtual ScalarType Priority() const
2004-11-05 10:57:18 +01:00
{
return _priority;
}
2004-11-05 10:57:18 +01:00
/*!
* Execute the flipping of the edge
*/
void Execute(TRIMESH_TYPE &m)
{
2007-01-15 12:41:09 +01:00
int z = _pos.z;
vcg::face::FlipEdge(*_pos.f, z);
}
2004-11-05 10:57:18 +01:00
/*!
*/
const char* Info(TRIMESH_TYPE &m)
{
static char dump[60];
sprintf(dump,"%i -> %i %g\n", _pos.F()->V(0)-&m.vert[0], _pos.F()->V(1)-&m.vert[0],-_priority);
2004-11-05 10:57:18 +01:00
return dump;
}
2004-11-05 10:57:18 +01:00
/*!
*/
2007-01-31 16:24:30 +01:00
static void Init(TRIMESH_TYPE &mesh, HeapType &heap)
2004-11-05 10:57:18 +01:00
{
heap.clear();
FaceIterator f_iter;
for (f_iter = mesh.face.begin(); f_iter!=mesh.face.end(); ++f_iter)
{
if (! (*f_iter).IsD() )
{
2007-01-31 16:24:30 +01:00
//if(!(Selected && !(*f_iter).IsS()))
if( (*f_iter).V(0)->IsW() && (*f_iter).V(1)->IsW() && (*f_iter).V(2)->IsW())
2004-11-05 10:57:18 +01:00
{
2007-01-31 16:24:30 +01:00
for (unsigned int i=0; i<3; i++)
2007-01-15 12:41:09 +01:00
{
2007-01-31 16:24:30 +01:00
if( !(*f_iter).IsB(i) && (*f_iter).FFp(i)->V2((*f_iter).FFi(i) )->IsW() )
{
VertexPointer v0 = (*f_iter).V0(i);
VertexPointer v1 = (*f_iter).V1(i);
if (v1-v0 > 0)
{
heap.push_back( HeapElem( new MYTYPE(PosType(&*f_iter, i), mesh.IMark() )) );
}
} //endif
} //endfor
}
2004-11-05 10:57:18 +01:00
} // endif
} //endfor
}
2004-11-05 10:57:18 +01:00
/*!
*/
void UpdateHeap(HeapType &heap)
{
GlobalMark()++;
PosType pos(_pos.f, _pos.z);
pos.FlipF();
_pos.F()->V(0)->IMark() = GlobalMark();
_pos.F()->V(1)->IMark() = GlobalMark();
_pos.F()->V(2)->IMark() = GlobalMark();
pos.F()->V(2)->IMark() = GlobalMark();
2004-11-05 10:57:18 +01:00
2007-01-15 12:41:09 +01:00
PosType poss(_pos.f, _pos.z);
poss.FlipE();
if(!poss.IsBorder())
{
heap.push_back( HeapElem( new MYTYPE( PosType(poss.f, poss.z), GlobalMark() ) ) );
}
2004-11-05 10:57:18 +01:00
2007-01-15 12:41:09 +01:00
poss.FlipE(); poss.FlipV(); poss.FlipE();
if(!poss.IsBorder() )
{
heap.push_back( HeapElem( new MYTYPE( PosType(poss.f, poss.z), GlobalMark() ) ) );
}
pos.FlipE();
if(!poss.IsBorder())
{
heap.push_back( HeapElem( new MYTYPE( PosType(pos.f, pos.z), GlobalMark() ) ) );
}
pos.FlipE(); pos.FlipV(); pos.FlipE();
if(!poss.IsBorder())
{
heap.push_back( HeapElem( new MYTYPE( PosType(pos.f, pos.z), GlobalMark() ) ) );
}
2004-11-05 10:57:18 +01:00
std::push_heap(heap.begin(),heap.end());
}
2007-01-15 12:41:09 +01:00
}; // end of PlanarEdgeFlip class
template <class TRIMESH_TYPE, class MYTYPE>
class TriEdgeFlip : public PlanarEdgeFlip<TRIMESH_TYPE, MYTYPE>
{
protected:
typedef typename TRIMESH_TYPE::FaceType FaceType;
typedef typename TRIMESH_TYPE::FacePointer FacePointer;
typedef typename TRIMESH_TYPE::FaceIterator FaceIterator;
typedef typename TRIMESH_TYPE::VertexType VertexType;
typedef typename TRIMESH_TYPE::VertexPointer VertexPointer;
typedef typename TRIMESH_TYPE::ScalarType ScalarType;
typedef typename TRIMESH_TYPE::CoordType CoordType;
typedef vcg::face::Pos<FaceType> PosType;
typedef typename LocalOptimization<TRIMESH_TYPE>::HeapElem HeapElem;
typedef typename LocalOptimization<TRIMESH_TYPE>::HeapType HeapType;
typedef typename vcg::Triangle3<ScalarType> TriangleType;
2007-01-15 12:41:09 +01:00
public:
/*!
* Default constructor
*/
inline TriEdgeFlip() {}
2007-01-15 12:41:09 +01:00
/*!
* Constructor with <I>pos</I> type
*/
inline TriEdgeFlip(const PosType pos, int mark)
2007-01-15 12:41:09 +01:00
{
this->_pos = pos;
this->_localMark = mark;
this->_priority = ComputePriority();
}
2007-01-15 12:41:09 +01:00
/*!
* Copy Constructor
*/
inline TriEdgeFlip(const TriEdgeFlip &par)
{
this->_pos = par.GetPos();
this->_localMark = par.GetMark();
this->_priority = par.Priority();
}
2007-01-15 12:41:09 +01:00
//only topology check
bool IsFeasible()
{
return vcg::face::CheckFlipEdge(*this->_pos.f, this->_pos.z);
}
2007-01-15 12:41:09 +01:00
ScalarType ComputePriority()
{
/*
0
/|\
/ | \
1 | 3
\ | /
\|/
2
*/
2007-01-15 12:41:09 +01:00
CoordType v0,v1,v2,v3;
PosType app = this->_pos;
2007-01-15 12:41:09 +01:00
v0 = app.v->P();
app.FlipE(); app.FlipV();
v1 = app.v->P();
app.FlipE(); app.FlipV();
v2 = app.v->P();
app.FlipE(); app.FlipF(); app.FlipE(); app.FlipV();
v3 = app.v->P();
2008-03-08 13:43:26 +01:00
CoordType CircumCenter = vcg::Circumcenter(*(app.F()));
2007-01-15 12:41:09 +01:00
ScalarType Radius= Distance(v0,CircumCenter);
ScalarType Radius1= Distance(v1,CircumCenter);
ScalarType Radius2= Distance(v2,CircumCenter);
assert( fabs(Radius-Radius1) < 0.1 );
assert( fabs(Radius-Radius2) < 0.1 );
///Return the difference of radius and the distance of v3 and the CircumCenter
this->_priority = (Radius - Distance(v3,CircumCenter));
this->_priority *=-1;
return this->_priority;
}
2007-01-15 12:41:09 +01:00
};
2004-11-05 10:57:18 +01:00
/*! @} */
}; // end of namespace tri
}; // end of namespace vcg