Added DihedralAngleRad that computes the signed dihedral angle between the normals of two adjacent faces

This commit is contained in:
Paolo Cignoni 2013-06-24 07:55:54 +00:00
parent 49d759af2a
commit 9ad68bc573
1 changed files with 135 additions and 85 deletions

View File

@ -35,7 +35,7 @@ namespace face {
/*@{*/ /*@{*/
/** Return a boolean that indicate if the face is complex. /** Return a boolean that indicate if the face is complex.
@param j Index of the edge @param j Index of the edge
@return true se la faccia e' manifold, false altrimenti @return true se la faccia e' manifold, false altrimenti
*/ */
template <class FaceType> template <class FaceType>
@ -43,7 +43,7 @@ inline bool IsManifold( FaceType const & f, const int j )
{ {
assert(f.cFFp(j) != 0); // never try to use this on uncomputed topology assert(f.cFFp(j) != 0); // never try to use this on uncomputed topology
if(FaceType::HasFFAdjacency()) if(FaceType::HasFFAdjacency())
return ( f.cFFp(j) == &f || &f == f.cFFp(j)->cFFp(f.cFFi(j)) ); return ( f.cFFp(j) == &f || &f == f.cFFp(j)->cFFp(f.cFFi(j)) );
else else
return true; return true;
} }
@ -56,13 +56,63 @@ template <class FaceType>
inline bool IsBorder(FaceType const & f, const int j ) inline bool IsBorder(FaceType const & f, const int j )
{ {
if(FaceType::HasFFAdjacency()) if(FaceType::HasFFAdjacency())
return f.cFFp(j)==&f; return f.cFFp(j)==&f;
//return f.IsBorder(j); //return f.IsBorder(j);
assert(0); assert(0);
return true; return true;
} }
/*! \brief Compute the signed dihedral angle between the normals of two adjacent faces
*
* The angle between the normal is signed according to the concavity/convexity of the
* dihedral angle: negative if the edge shared between the two faces is concave, positive otherwise.
* The surface it is assumend to be oriented.
* It simply use the projection of the opposite vertex onto the plane of the other one.
* It does not assume anything on face normals.
*
* v0 ___________ vf1
* |\ |
* | \i1 f1 |
* | \ |
* |f0 i0\ |
* | \ |
* |__________\|
* vf0 v1
*/
template <class FaceType>
inline typename FaceType::ScalarType DihedralAngleRad(FaceType & f, const int i )
{
typedef typename FaceType::ScalarType ScalarType;
typedef typename FaceType::CoordType CoordType;
typedef typename FaceType::VertexType VertexType;
FaceType *f0 = &f;
FaceType *f1 = f.FFp(i);
int i0=i;
int i1=f.FFi(i);
VertexType *vf0 = f0->V2(i0);
VertexType *vf1 = f1->V2(i1);
CoordType n0 = NormalizedNormal(*f0);
CoordType n1 = NormalizedNormal(*f1);
ScalarType off0 = n0*vf0->P();
ScalarType off1 = n1*vf1->P();
ScalarType dist01 = off0 - n0*vf1->P();
ScalarType dist10 = off1 - n1*vf0->P();
// just to be sure use the sign of the largest in absolute value;
ScalarType sign;
if(fabs(dist01) > fabs(dist10)) sign = dist01;
else sign=dist10;
ScalarType angleRad=Angle(f0->N(),f1->N());
if(sign > 0 ) return angleRad;
else return -angleRad;
}
/// Count border edges of the face /// Count border edges of the face
template <class FaceType> template <class FaceType>
@ -71,12 +121,12 @@ inline int BorderCount(FaceType const & f)
if(FaceType::HasFFAdjacency()) if(FaceType::HasFFAdjacency())
{ {
int t = 0; int t = 0;
if( IsBorder(f,0) ) ++t; if( IsBorder(f,0) ) ++t;
if( IsBorder(f,1) ) ++t; if( IsBorder(f,1) ) ++t;
if( IsBorder(f,2) ) ++t; if( IsBorder(f,2) ) ++t;
return t; return t;
} }
else return 3; else return 3;
} }
@ -94,22 +144,22 @@ inline int ComplexSize(FaceType & f, const int e)
int cnt=0; int cnt=0;
do do
{ {
fpos.NextF(); fpos.NextF();
assert(!fpos.IsBorder()); assert(!fpos.IsBorder());
assert(!fpos.IsManifold()); assert(!fpos.IsManifold());
++cnt; ++cnt;
} }
while(fpos.f!=&f); while(fpos.f!=&f);
assert (cnt>2); assert (cnt>2);
return cnt; return cnt;
} }
assert(0); assert(0);
return 2; return 2;
} }
/** This function check the FF topology correctness for an edge of a face. /** This function check the FF topology correctness for an edge of a face.
It's possible to use it also in non-two manifold situation. It's possible to use it also in non-two manifold situation.
The function cannot be applicated if the adjacencies among faces aren't defined. The function cannot be applicated if the adjacencies among faces aren't defined.
@param f the face to be checked @param f the face to be checked
@param e Index of the edge to be checked @param e Index of the edge to be checked
@ -135,15 +185,15 @@ bool FFCorrectness(FaceType & f, const int e)
// all the faces must be connected in a loop. // all the faces must be connected in a loop.
Pos< FaceType > curFace(&f,e); // Build the half edge Pos< FaceType > curFace(&f,e); // Build the half edge
int cnt=0; int cnt=0;
do do
{ {
if(curFace.IsManifold()) return false; if(curFace.IsManifold()) return false;
if(curFace.IsBorder()) return false; if(curFace.IsBorder()) return false;
curFace.NextF(); curFace.NextF();
cnt++; cnt++;
assert(cnt<100); assert(cnt<100);
} }
while ( curFace.f != &f); while ( curFace.f != &f);
return true; return true;
} }
@ -162,7 +212,7 @@ void FFDetachManifold(FaceType & f, const int e)
assert(!IsBorder<FaceType>(f,e)); // Never try to detach a border edge! assert(!IsBorder<FaceType>(f,e)); // Never try to detach a border edge!
FaceType *ffp = f.FFp(e); FaceType *ffp = f.FFp(e);
//int ffi=f.FFp(e); //int ffi=f.FFp(e);
int ffi=f.FFi(e); int ffi=f.FFi(e);
f.FFp(e)=&f; f.FFp(e)=&f;
f.FFi(e)=e; f.FFi(e)=e;
@ -179,7 +229,7 @@ void FFDetachManifold(FaceType & f, const int e)
} }
/** This function detach the face from the adjacent face via the edge e. /** This function detach the face from the adjacent face via the edge e.
It's possible to use it also in non-two manifold situation. It's possible to use it also in non-two manifold situation.
The function cannot be applicated if the adjacencies among faces aren't defined. The function cannot be applicated if the adjacencies among faces aren't defined.
@param f the face to be detached @param f the face to be detached
@param e Index of the edge to be detached @param e Index of the edge to be detached
@ -193,10 +243,10 @@ void FFDetach(FaceType & f, const int e)
int complexity; int complexity;
assert(complexity=ComplexSize(f,e)); assert(complexity=ComplexSize(f,e));
Pos< FaceType > FirstFace(&f,e); // Build the half edge Pos< FaceType > FirstFace(&f,e); // Build the half edge
Pos< FaceType > LastFace(&f,e); // Build the half edge Pos< FaceType > LastFace(&f,e); // Build the half edge
FirstFace.NextF(); FirstFace.NextF();
LastFace.NextF(); LastFace.NextF();
int cnt=0; int cnt=0;
// then in case of non manifold face continue to advance LastFace // then in case of non manifold face continue to advance LastFace
@ -205,25 +255,25 @@ void FFDetach(FaceType & f, const int e)
while ( LastFace.f->FFp(LastFace.z) != &f) while ( LastFace.f->FFp(LastFace.z) != &f)
{ {
assert(ComplexSize(*LastFace.f,LastFace.z)==complexity); assert(ComplexSize(*LastFace.f,LastFace.z)==complexity);
assert(!LastFace.IsManifold()); // We enter in this loop only if we are on a non manifold edge assert(!LastFace.IsManifold()); // We enter in this loop only if we are on a non manifold edge
assert(!LastFace.IsBorder()); assert(!LastFace.IsBorder());
LastFace.NextF(); LastFace.NextF();
cnt++; cnt++;
assert(cnt<100); assert(cnt<100);
} }
assert(LastFace.f->FFp(LastFace.z)==&f); assert(LastFace.f->FFp(LastFace.z)==&f);
assert(f.FFp(e)== FirstFace.f); assert(f.FFp(e)== FirstFace.f);
// Now we link the last one to the first one, skipping the face to be detached; // Now we link the last one to the first one, skipping the face to be detached;
LastFace.f->FFp(LastFace.z) = FirstFace.f; LastFace.f->FFp(LastFace.z) = FirstFace.f;
LastFace.f->FFi(LastFace.z) = FirstFace.z; LastFace.f->FFi(LastFace.z) = FirstFace.z;
assert(ComplexSize(*LastFace.f,LastFace.z)==complexity-1); assert(ComplexSize(*LastFace.f,LastFace.z)==complexity-1);
// At the end selfconnect the chosen edge to make a border. // At the end selfconnect the chosen edge to make a border.
f.FFp(e) = &f; f.FFp(e) = &f;
f.FFi(e) = e; f.FFi(e) = e;
assert(ComplexSize(f,e)==1); assert(ComplexSize(f,e)==1);
assert(FFCorrectness<FaceType>(*LastFace.f,LastFace.z)); assert(FFCorrectness<FaceType>(*LastFace.f,LastFace.z));
@ -253,12 +303,12 @@ void FFAttach(FaceType * &f, int z1, FaceType *&f2, int z2)
//Salvo i dati di f1 prima di sovrascrivere //Salvo i dati di f1 prima di sovrascrivere
FaceType *f1prec = f->FFp(z1); FaceType *f1prec = f->FFp(z1);
int z1prec = f->FFi(z1); int z1prec = f->FFi(z1);
//Aggiorno f1 //Aggiorno f1
f->FFp(z1) = TEPB.f->FFp(TEPB.z); f->FFp(z1) = TEPB.f->FFp(TEPB.z);
f->FFi(z1) = TEPB.f->FFi(TEPB.z); f->FFi(z1) = TEPB.f->FFi(TEPB.z);
//Aggiorno la faccia che precede f2 //Aggiorno la faccia che precede f2
TEPB.f->FFp(TEPB.z) = f1prec; TEPB.f->FFp(TEPB.z) = f1prec;
TEPB.f->FFi(TEPB.z) = z1prec; TEPB.f->FFi(TEPB.z) = z1prec;
} }
/** This function attach the face (via the edge z1) to another face (via the edge z2). /** This function attach the face (via the edge z1) to another face (via the edge z2).
@ -436,21 +486,21 @@ bool CheckFlipEdge(FaceType &f, int z)
if (z<0 || z>2) return false; if (z<0 || z>2) return false;
// boundary edges cannot be flipped // boundary edges cannot be flipped
if (face::IsBorder(f, z)) return false; if (face::IsBorder(f, z)) return false;
FaceType *g = f.FFp(z); FaceType *g = f.FFp(z);
int w = f.FFi(z); int w = f.FFi(z);
// check if the vertices of the edge are the same // check if the vertices of the edge are the same
// e.g. the mesh has to be well oriented // e.g. the mesh has to be well oriented
if (g->V(w)!=f.V1(z) || g->V1(w)!=f.V(z) ) if (g->V(w)!=f.V1(z) || g->V1(w)!=f.V(z) )
return false; return false;
// check if the flipped edge is already present in the mesh // check if the flipped edge is already present in the mesh
// f_v2 and g_v2 are the vertices of the new edge // f_v2 and g_v2 are the vertices of the new edge
VertexType *f_v2 = f.V2(z); VertexType *f_v2 = f.V2(z);
VertexType *g_v2 = g->V2(w); VertexType *g_v2 = g->V2(w);
// just a sanity check. If this happens the mesh is not manifold. // just a sanity check. If this happens the mesh is not manifold.
if (f_v2 == g_v2) return false; if (f_v2 == g_v2) return false;
@ -460,15 +510,15 @@ bool CheckFlipEdge(FaceType &f, int z)
PosType pos(&f, (z+2)%3, f_v2); PosType pos(&f, (z+2)%3, f_v2);
PosType startPos=pos; PosType startPos=pos;
do do
{ {
pos.NextE(); pos.NextE();
if (g_v2 == pos.VFlip()) if (g_v2 == pos.VFlip())
return false; return false;
} }
while (pos != startPos); while (pos != startPos);
return true; return true;
} }
/*! /*!
@ -488,7 +538,7 @@ void FlipEdge(FaceType &f, const int z)
assert( !IsBorder(f,z) ); assert( !IsBorder(f,z) );
assert( face::IsManifold<FaceType>(f, z)); assert( face::IsManifold<FaceType>(f, z));
FaceType *g = f.FFp(z); FaceType *g = f.FFp(z);
int w = f.FFi(z); int w = f.FFi(z);
assert( g->V(w) == f.V1(z) ); assert( g->V(w) == f.V1(z) );
@ -500,13 +550,13 @@ void FlipEdge(FaceType &f, const int z)
f.V1(z) = g->V2(w); f.V1(z) = g->V2(w);
g->V1(w) = f.V2(z); g->V1(w) = f.V2(z);
f.FFp(z) = g->FFp((w+1)%3); f.FFp(z) = g->FFp((w+1)%3);
f.FFi(z) = g->FFi((w+1)%3); f.FFi(z) = g->FFi((w+1)%3);
g->FFp(w) = f.FFp((z+1)%3); g->FFp(w) = f.FFp((z+1)%3);
g->FFi(w) = f.FFi((z+1)%3); g->FFi(w) = f.FFi((z+1)%3);
f.FFp((z+1)%3) = g; f.FFp((z+1)%3) = g;
f.FFi((z+1)%3) = (w+1)%3; f.FFi((z+1)%3) = (w+1)%3;
g->FFp((w+1)%3) = &f; g->FFp((w+1)%3) = &f;
g->FFi((w+1)%3) = (z+1)%3; g->FFi((w+1)%3) = (z+1)%3;
if(f.FFp(z)==g) if(f.FFp(z)==g)
@ -553,8 +603,8 @@ void VFDetach(FaceType & f, int z)
} }
else // scan the list of faces in order to finde the current face f to be detached else // scan the list of faces in order to finde the current face f to be detached
{ {
VFIterator<FaceType> x(f.V(z)->VFp(),f.V(z)->VFi()); VFIterator<FaceType> x(f.V(z)->VFp(),f.V(z)->VFi());
VFIterator<FaceType> y; VFIterator<FaceType> y;
for(;;) for(;;)
{ {