vcglib/vcg/simplex/tetrahedron/pos.h

470 lines
10 KiB
C
Raw Normal View History

2004-05-14 18:04:51 +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. *
* *
****************************************************************************/
/****************************************************************************
History
****************************************************************************/
#ifndef __VCG_TETRA_POS
#define __VCG_TETRA_POS
namespace vcg {
2004-05-17 17:28:35 +02:00
namespace tetra {
2004-05-14 18:04:51 +02:00
/** \addtogroup tetra */
/*@{*/
/** Class VTIterator.
This is a vertex - tetrahedron iterator
@param MTTYPE (Template Parameter) Specifies the type of the tetrahedron.
*/
template < class MTTYPE>
class VTIterator
{
public:
/// The tetrahedron type
typedef MTTYPE TetraType;
2004-05-14 18:04:51 +02:00
private:
/// Pointer to a tetrahedron
TetraType *_vt;
/// Index of one vertex
int _vi;
/// Default Constructor
public:
2004-05-17 17:28:35 +02:00
VTIterator(){}
2004-05-14 18:04:51 +02:00
/// Constructor which associates the EdgePos elementet with a face and its edge
VTIterator(TetraType * const tp, int const zp)
{
_vt=tp;
_vi=zp;
2004-05-17 17:28:35 +02:00
}
2004-05-14 18:04:51 +02:00
~VTIterator(){};
2004-05-17 17:28:35 +02:00
/// Return the tetrahedron stored in the half edge
2004-05-31 18:04:19 +02:00
inline TetraType* & Vt()
2004-05-17 17:28:35 +02:00
{
return _vt;
}
2004-05-14 18:04:51 +02:00
/// Return the index of vertex as seen from the tetrahedron
2004-05-17 17:28:35 +02:00
inline int & Vi()
{
return _vi;
}
2004-05-14 18:04:51 +02:00
2004-05-17 17:28:35 +02:00
/// Return the index of vertex as seen from the tetrahedron
inline const int & Vi() const
{
2004-05-14 18:04:51 +02:00
return _vi;
2004-05-17 17:28:35 +02:00
}
2004-05-31 18:04:19 +02:00
inline bool End(){return (Vt()==NULL);}
2004-05-14 18:04:51 +02:00
/// move on the next tetrahedron that share the vertex
void operator++()
2004-05-17 17:28:35 +02:00
{
2004-05-14 18:04:51 +02:00
int vi=Vi();
TetraType * tw = Vt();
2004-05-31 18:04:19 +02:00
Vt() = tw->TVp(vi);
Vi() = tw->TVi(vi);
assert((Vt()==NULL)||((tw->V(vi))==(Vt()->V(Vi()))));
2004-05-17 17:28:35 +02:00
}
2004-05-14 18:04:51 +02:00
};
/** Templated over the class tetrahedron, it stores a \em position over a tetrahedron in a mesh.
It contain a pointer to the current tetrahedron,
the index of one face,edge and a edge's incident vertex.
*/
template < class MTTYPE>
class Pos
{
public:
/// The tetrahedron type
typedef MTTYPE TetraType;
2004-05-14 18:04:51 +02:00
/// The vertex type
2004-05-31 18:04:19 +02:00
typedef typename TetraType::VertexType VertexType;
2004-05-14 18:04:51 +02:00
/// The coordinate type
2004-05-31 18:04:19 +02:00
typedef typename TetraType::VertexType::CoordType CoordType;
2004-05-14 18:04:51 +02:00
///The HEdgePos type
typedef Pos<TetraType> BasePosType;
private:
/// Pointer to the tetrahedron of the half-edge
TetraType *_t;
/// Index of the face
char _f;
/// Index of the edge
char _e;
/// Pointer to the vertex
char _v;
public:
/// Default constructor
Pos(){SetNull();};
/// Constructor which associates the half-edge elementet with a face, its edge and its vertex
Pos(TetraType * const tp, char const fap,char const ep,
2004-07-08 10:44:27 +02:00
char const vp){_t=tp;_f=fap;_e=ep;_v=vp;}
2004-05-14 18:04:51 +02:00
~Pos(){};
/// Return the tetrahedron stored in the half edge
2004-05-31 18:04:19 +02:00
inline TetraType* & T()
2004-05-17 17:28:35 +02:00
{
2004-05-14 18:04:51 +02:00
return _t;
}
/// Return the tetrahedron stored in the half edge
2004-07-09 12:16:42 +02:00
inline TetraType* const & T() const
2004-05-14 18:04:51 +02:00
{
return _t;
}
/// Return the index of face as seen from the tetrahedron
inline char & F()
{
return _f;
}
/// Return the index of face as seen from the tetrahedron
inline const char & F() const
{
return _f;
}
/// Return the index of face as seen from the tetrahedron
inline char & E()
{
return _e;
}
/// Return the index of face as seen from the tetrahedron
inline const char & E() const
{
return _e;
}
/// Return the index of vertex as seen from the tetrahedron
inline char & V()
{
return _v;
}
/// Return the index of vertex as seen from the tetrahedron
inline const char & V() const
{
return _v;
}
/// Operator to compare two half-edge
inline bool operator == ( BasePosType const & p ) const {
return (T()==p.T() && F()==p.F() && E==p.E() && V==p.V());
}
/// Operator to compare two half-edge
inline bool operator != ( BasePosType const & p ) const {
return (!((*this)==p));
}
2004-07-08 10:44:27 +02:00
2004-05-14 18:04:51 +02:00
/// Set to null the half-edge
void SetNull(){
T()=0;
F()=-1;
E()=-1;
V()=-1;
}
/// Check if the half-edge is null
bool IsNull() const {
return ((T()==0) || (F()<0) || (E()<0) || (V()<0));
}
/// Changes edge maintaining the same face and the same vertex
void FlipE()
{
//take the absolute index of the tree edges of the faces
char e0=vcg::Tetra::EofF(fa,0);
char e1=vcg::Tetra::EofF(fa,1);
char e2=vcg::Tetra::EofF(fa,2);
//eliminate the same as himself
if (e0==E())
{
e0=e1;
e1=e2;
}
else
if (e1==E())
{
e1=e2;
}
//now choose the one that preserve the same vertex
if ((vcg::Tetra::VofE(e1,0)==V())||(vcg::Tetra::VofE(e1,1)==V()))
E()=e1;
else
E()=e0;
}
/// Changes vertex maintaining the same face and the same edge
void FlipV()
{
// in the same edge choose the one that change
char v0=vcg::Tetra::VofE(E(),0);
char v1=vcg::Tetra::VofE(E(),1);
if (v0!=V())
V()=v0;
else
V()=v1;
}
/// Changes face maintaining the same vertex and the same edge
void FlipF()
{
2004-05-31 18:04:19 +02:00
char f0=vcg::Tetra::FofE(E(),0);
char f1=vcg::Tetra::FofE(E(),1);
2004-05-14 18:04:51 +02:00
if (f0!=F())
F()=f0;
else
F()=f1;
}
/// Changes tetrahedron maintaining the same face edge and vertex'... to finish
void FlipT()
{
//save the two vertices of the old edge
2004-05-17 17:28:35 +02:00
VertexType *v0=T()->V(vcg::Tetra::VofE(E(),0));
VertexType *v1=T()->V(vcg::Tetra::VofE(E(),1));
2004-05-14 18:04:51 +02:00
2004-05-31 18:04:19 +02:00
//get the current vertex
VertexType *vcurr=T()->V(V());
2004-05-14 18:04:51 +02:00
//get new tetrahedron according to faceto face topology
TetraType *nt=T()->TTp(F());
char nfa=T()->TTi(F());
if (nfa!=-1)
{
//find the right edge
char ne0=vcg::Tetra::EofF(nfa,0);
char ne1=vcg::Tetra::EofF(nfa,1);
char ne2=vcg::Tetra::EofF(nfa,2);
2004-05-17 17:28:35 +02:00
//the vertices of new edges
VertexType *vn0=nt->V(vcg::Tetra::VofE(ne0,0));
VertexType *vn1=nt->V(vcg::Tetra::VofE(ne0,1));
2004-05-14 18:04:51 +02:00
//verify that the two vertices of tetrahedron are identical
2004-05-17 17:28:35 +02:00
if (((vn0==v0)&&(vn1==v1))||((vn1==v0)&&(vn0==v1)))
E()=ne0;
2004-05-14 18:04:51 +02:00
else
2004-05-17 17:28:35 +02:00
{
vn0=nt->V(vcg::Tetra::VofE(ne1,0));
vn1=nt->V(vcg::Tetra::VofE(ne1,1));
if (((vn0==v0)&&(vn1==v1))||((vn1==v0)&&(vn0==v1)))
E()=ne1;
else
2004-05-31 18:04:19 +02:00
{
#ifdef _DEBUG
vn0=nt->V(vcg::Tetra::VofE(ne2,0));
vn1=nt->V(vcg::Tetra::VofE(ne2,1));
assert(((vn0==v0)&&(vn1==v1))||((vn1==v0)&&(vn0==v1)));
#endif
2004-05-17 17:28:35 +02:00
E()=ne2;
2004-05-31 18:04:19 +02:00
}
2004-05-17 17:28:35 +02:00
}
2004-05-31 18:04:19 +02:00
//find the right vertex
vn0=nt->V(vcg::Tetra::VofE(E(),0));
#ifdef _DEBUG
vn1=nt->V(vcg::Tetra::VofE(E(),1));
assert((vn0==vcurr)||(vn1==vcurr));
#endif
if (vn0==vcurr)
V()=vcg::Tetra::VofE(E(),0);
else
V()=vcg::Tetra::VofE(E(),1);
2004-05-17 17:28:35 +02:00
T()=nt;
2004-05-31 18:04:19 +02:00
assert(T()->V(V())==vcurr);
2004-05-17 17:28:35 +02:00
F()=nfa;
}
}
2004-05-14 18:04:51 +02:00
2004-05-17 17:28:35 +02:00
///returns the next half edge on the same edge
void NextT( )
2004-05-14 18:04:51 +02:00
{
#ifdef _DEBUG
2004-05-31 18:04:19 +02:00
VertexType *vold=T()->V(V());
2004-05-14 18:04:51 +02:00
#endif
FlipT();
FlipF();
#ifdef _DEBUG
2004-05-31 18:04:19 +02:00
VertexType *vnew=T()->V(V());
assert(vold==vnew);
2004-05-17 17:28:35 +02:00
#endif
2004-05-14 18:04:51 +02:00
}
void Assert()
#ifdef _DEBUG
{
HETYPE ht=*this;
ht.FlipT();
ht.FlipT();
assert(ht==*this);
ht=*this;
ht.FlipF();
ht.FlipF();
assert(ht==*this);
ht=*this;
ht.FlipE();
ht.FlipE();
assert(ht==*this);
ht=*this;
ht.FlipV();
ht.FlipV();
assert(ht==*this);
}
#else
{}
#endif
};
2004-05-17 17:28:35 +02:00
///this pos structure jump on next tetrahedron if find an external face
2004-05-14 18:04:51 +02:00
template < class MTTYPE>
2004-05-17 17:28:35 +02:00
class PosJump:public Pos<MTTYPE>
2004-05-14 18:04:51 +02:00
{
2004-05-17 17:28:35 +02:00
private:
MTTYPE *_t_initial;
short int _back;
public :
PosJump(const TetraType* tp,const int fap,const int ep,
2004-05-31 18:04:19 +02:00
int vp){T()=tp;F()=fap;E()=ep;V()=vp;_t_initial=tp;_back=0;}
2004-05-17 17:28:35 +02:00
void NextT()
{
2004-05-14 18:04:51 +02:00
#ifdef _DEBUG
int cont=0;
#endif
2004-05-17 17:28:35 +02:00
MTTYPE *tpred=T();
Pos<MTTYPE>::NextT();
//external face
if (tpred==T())
2004-05-14 18:04:51 +02:00
{
2004-05-17 17:28:35 +02:00
while (T()!=_t_initial)
2004-05-14 18:04:51 +02:00
{
2004-05-17 17:28:35 +02:00
Pos<MTTYPE>::NextT();
2004-05-14 18:04:51 +02:00
#ifdef _DEBUG
cont++;
assert (cont<500);
#endif
}
2004-05-17 17:28:35 +02:00
_back++;
if (_back==1)
2004-05-14 18:04:51 +02:00
{
2004-05-17 17:28:35 +02:00
Pos<MTTYPE>::NextT();
}
}
2004-05-14 18:04:51 +02:00
}
2004-05-17 17:28:35 +02:00
};
2004-05-14 18:04:51 +02:00
2004-05-17 17:28:35 +02:00
///this pos structure jump on next tetrahedron in rotational sense if find an external face
template < class MTTYPE>
class PosLoop:public Pos<MTTYPE>
{
2004-05-31 18:04:19 +02:00
private:
MTTYPE *_t_initial;
bool _jump;
bool _loop;
2004-05-17 17:28:35 +02:00
public :
2004-05-31 18:04:19 +02:00
PosLoop(TetraType* tp,const int fap,const int ep,
int vp){T()=tp;F()=fap;E()=ep;V()=vp;_t_initial=tp;_jump=false;_loop=false;}
bool LoopEnd()
{
return (_loop);
}
bool Jump()
{
return(_jump);
}
void Reset()
{
_loop=false;
_jump=false;
}
2004-05-17 17:28:35 +02:00
void NextT()
{
2004-05-31 18:04:19 +02:00
#ifdef _DEBUG
TetraType *t_old=T();
#endif
TetraType *tpred=T();
Pos<TetraType>::NextT();
_loop=false;
_jump=false;
2004-05-17 17:28:35 +02:00
//external face
if (tpred==T())
{
tpred=T();
2004-05-31 18:04:19 +02:00
//jump next one
Pos<TetraType>::NextT();
2004-05-17 17:28:35 +02:00
//find the next external face
while (tpred!=T())
{
tpred=T();
2004-05-31 18:04:19 +02:00
Pos<TetraType>::NextT();
2004-05-17 17:28:35 +02:00
}
2004-05-31 18:04:19 +02:00
////reset right rotation sense
// Pos<TetraType>::NextT();
_jump=true;
2004-05-17 17:28:35 +02:00
}
2004-05-31 18:04:19 +02:00
if (T()==_t_initial)
_loop=true;
#ifdef _DEBUG
if (_loop==false)
assert(t_old!=T());
#endif
2004-05-17 17:28:35 +02:00
}
2004-05-31 18:04:19 +02:00
2004-05-14 18:04:51 +02:00
};
2004-10-28 02:56:44 +02:00
//@}
2004-05-17 17:28:35 +02:00
}//end namespace tetra
}//end namespace vcg
2004-05-14 18:04:51 +02:00
#endif