Frist working release whit a few bugs.

It almost fills the hole ...
This commit is contained in:
Paolo Cignoni 2006-09-27 09:29:53 +00:00
parent 8ecd4dc8f2
commit b6c4ae457c
1 changed files with 160 additions and 99 deletions

View File

@ -24,6 +24,9 @@
History History
$Log: not supported by cvs2svn $ $Log: not supported by cvs2svn $
Revision 1.1 2006/09/25 09:17:44 cignoni
First Non working Version
****************************************************************************/ ****************************************************************************/
#ifndef __VCG_TRI_UPDATE_HOLE #ifndef __VCG_TRI_UPDATE_HOLE
#define __VCG_TRI_UPDATE_HOLE #define __VCG_TRI_UPDATE_HOLE
@ -39,20 +42,45 @@ template<class MESH>
class SimpleEdge class SimpleEdge
{ {
public: public:
MESH::VertexPointer v[2]; typename MESH::VertexType v[2];
SimpleEdge(MESH::VertexPointer v0, MESH::VertexPointer v1) SimpleEdge()
{}
SimpleEdge(typename MESH::VertexType v0, typename MESH::VertexType v1)
{ {
if(v0>v1) {v[0]=v1; v[1]=v0;} if(v0.P().X() != v1.P().X() &&
v0.P().Y() != v1.P().Y() &&
v0.P().Z() != v1.P().Z())
{v[0]=v1; v[1]=v0;}
else {v[0]=v0; v[1]=v1;} else {v[0]=v0; v[1]=v1;}
} }
SimpleEdge(MESH::hedgepos_type &ep) { SimpleEdge(face::Pos<typename MESH::FaceType> &ep) {
*this=SimpleEdge(ep.VFlip(), ep.v); //*this=SimpleEdge(*ep.VFlip(), *ep.v);
MESH::VertexType v0 ,v1;
v0 = *ep.VFlip();
v1 = *ep.v;
if(v0.P().X() != v1.P().X() &&
v0.P().Y() != v1.P().Y() &&
v0.P().Z() != v1.P().Z())
{v[0]=v1; v[1]=v0;}
else {v[0]=v0; v[1]=v1;}
} }
bool operator < (const SimpleEdge & e) const
{ return (v[0]!=e.v[0])?(v[0]<e.v[0]):(v[1]<e.v[1]); }
bool operator < (const SimpleEdge & e) const
{ v[0] = e.v[0]; v[1]=e.v[1];
}
bool operator != (const SimpleEdge & e)
{
if(v[0].P().X() != e.v[0].P().X() &&
v[0].P().Y() != e.v[0].P().Y() &&
v[0].P().Z() != e.v[0].P().Z())
return true;
else return false;
}
}; };
template<class MESH> template<class MESH>
@ -60,15 +88,15 @@ class HoleInfo
{ {
public: public:
HoleInfo(){} HoleInfo(){}
HoleInfo(MESH::hedgepos_type const &pHole, int const pHoleSize, Box3<MESH::scalar_type> &pHoleBB) HoleInfo(face::Pos<typename MESH::FaceType> const &pHole, int const pHoleSize, vcg::Box3<typename MESH::ScalarType> &pHoleBB)
{ {
p=pHole; p=pHole;
size=pHoleSize; size=pHoleSize;
bb=pHoleBB; bb=pHoleBB;
} }
MESH::hedgepos_type p; typename face::Pos<typename MESH::FaceType> p;
int size; int size;
Box3<MESH::scalar_type> bb; vcg::Box3<typename MESH::ScalarType> bb;
bool operator < (const HoleInfo & hh) const {return size < hh.size;} bool operator < (const HoleInfo & hh) const {return size < hh.size;}
bool operator > (const HoleInfo & hh) const {return size > hh.size;} bool operator > (const HoleInfo & hh) const {return size > hh.size;}
@ -77,10 +105,10 @@ public:
bool operator >= (const HoleInfo & hh) const {return size >= hh.size;} bool operator >= (const HoleInfo & hh) const {return size >= hh.size;}
bool operator <= (const HoleInfo & hh) const {return size <= hh.size;} bool operator <= (const HoleInfo & hh) const {return size <= hh.size;}
MESH::scalar_type Perimeter() typename MESH::ScalarType Perimeter()
{ {
MESH::scalar_type sum=0; MESH::ScalarType sum=0;
MESH::hedgepos_type ip = p; face::Pos<typename MESH::FaceType> ip = p;
do do
{ {
sum+=Distance(ip.v->cP(),ip.VFlip()->cP()); sum+=Distance(ip.v->cP(),ip.VFlip()->cP());
@ -91,13 +119,13 @@ public:
} }
int CollectEdges(set< SimpleEdge<MESH> > &EV) int CollectEdges(std::vector< SimpleEdge<MESH> > &EV)
{ {
assert(p.IsBorder()); assert(p.IsBorder());
EV.clear(); EV.clear();
int tsz=0; int tsz=0;
MESH::hedgepos_type ip=p; face::Pos<typename MESH::FaceType> ip=p;
MESH::hedgepos_type tp; face::Pos<typename MESH::FaceType> tp;
do do
{ {
@ -105,16 +133,16 @@ public:
do do
{ {
ip.NextE(); ip.NextE();
EV.insert(SimpleEdge<MESH>(ip)); // l'edge che sto scorrendo EV.push_back(SimpleEdge<MESH>(ip)); // l'edge che sto scorrendo
tp=ip; tp=ip;
tp.FlipV();tp.FlipE(); tp.FlipV();tp.FlipE();
EV.insert(SimpleEdge<MESH>(tp)); // l'edge della faccia su cui sono e opposto al vertice su cui ruoto EV.push_back(SimpleEdge<MESH>(tp)); // l'edge della faccia su cui sono e opposto al vertice su cui ruoto
tp.FlipF(); tp.FlipE(); tp.FlipF(); tp.FlipE();
EV.insert(SimpleEdge<MESH>(tp)); // gli altri due edge della faccia opposta a questa EV.push_back(SimpleEdge<MESH>(tp)); // gli altri due edge della faccia opposta a questa
tp.FlipE(); tp.FlipE();
EV.insert(SimpleEdge<MESH>(tp)); EV.push_back(SimpleEdge<MESH>(tp));
} }
while(!ip.f->IsBorder(ip.z)); while(!ip.f->IsB(ip.z));
ip.FlipV(); ip.FlipV();
++tsz; ++tsz;
} }
@ -126,15 +154,15 @@ public:
}; };
template<class MESH> template<class MESH>
void FindHole(MESH &m, MESH::hedgepos_type ep, HoleInfo<MESH> &h) void FindHole(MESH &m, face::Pos<typename MESH::FaceType> ep, HoleInfo<MESH> &h)
{ {
if(!ep.IsBorder()) return; if(!ep.IsBorder()) return;
int holesize = 0; int holesize = 0;
Box3<MESH::scalar_type> hbox; Box3<MESH::ScalarType> hbox;
if(ep.v->IsR()) hbox.Add(ep.v->cP()); if(ep.v->IsR()) hbox.Add(ep.v->cP());
MESH::hedgepos_type init; face::Pos<typename MESH::FaceType> init;
init = ep; init = ep;
do do
{ {
@ -150,32 +178,32 @@ void FindHole(MESH &m, MESH::hedgepos_type ep, HoleInfo<MESH> &h)
template<class MESH,class STL_CONTAINER_HOLES> template<class MESH,class STL_CONTAINER_HOLES>
void FindHole(MESH &m, STL_CONTAINER_HOLES & H) void FindHole(MESH &m, STL_CONTAINER_HOLES & H)
{ {
MESH::face_iterator pf; MESH::FaceIterator pf;
int holesize; int holesize;
for (pf=m.face.begin(); pf!=m.face.end(); ++pf) for (pf=m.face.begin(); pf!=m.face.end(); ++pf)
if( !(*pf).IsD() && (*pf).IsW() ) if( !(*pf).IsD() && (*pf).IsW() )
(*pf).ClearV(); (*pf).ClearS();
MESH::hedgepos_type ep; face::Pos<typename MESH::FaceType> ep;
for (pf=m.face.begin(); pf!=m.face.end(); ++pf) for (pf=m.face.begin(); pf!=m.face.end(); ++pf)
{ {
if( !(*pf).IsDeleted() && !(*pf).IsV() && (*pf).IsR() ) if( !(*pf).IsD() && !(*pf).IsS() && (*pf).IsR() )
{ {
for(int j=0; j<3; ++j) for(int j=0; j<3; ++j)
if( (*pf).IsBorder(j) && !(*pf).IsV() && (*pf).IsR() ) if( (*pf).IsB(j) && !(*pf).IsS() && (*pf).IsR() )
{ {
(*pf).SetV(); (*pf).SetS();
ep.Set(&*pf, j, (*pf).V(j)); ep.Set(&*pf, j, (*pf).V(j));
holesize = 0; holesize = 0;
Box3<MESH::scalar_type> hbox; Box3<MESH::ScalarType> hbox;
if(ep.v->IsR()) hbox.Add(ep.v->cP()); if(ep.v->IsR()) hbox.Add(ep.v->cP());
MESH::hedgepos_type init; face::Pos<typename MESH::FaceType> init;
init = ep; init = ep;
do do
{ {
ep.NextB(); ep.NextB();
ep.f->SetV(); ep.f->SetS();
if(ep.v->IsR()) hbox.Add(ep.v->cP()); if(ep.v->IsR()) hbox.Add(ep.v->cP());
++holesize; ++holesize;
} }
@ -216,11 +244,11 @@ XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX
template<class MSH_TYPE> class TrivialEar template<class MSH_TYPE> class TrivialEar
{ {
public: public:
MSH_TYPE::hedgepos_type e0; // face::Pos<typename MSH_TYPE::FaceType> e0; //
MSH_TYPE::hedgepos_type e1; // face::Pos<typename MSH_TYPE::FaceType> e1; //
MSH_TYPE::scalar_type quality; typename MSH_TYPE::ScalarType quality;
TrivialEar(){} TrivialEar(){}
TrivialEar(const MSH_TYPE::hedgepos_type & ep) TrivialEar(const face::Pos<typename MSH_TYPE::FaceType> & ep)
{ {
e0=ep; e0=ep;
assert(e0.IsBorder()); assert(e0.IsBorder());
@ -244,8 +272,8 @@ template<class MSH_TYPE> class TrivialEar
bool Degen() bool Degen()
{ {
MSH_TYPE::hedgepos_type ep=e0; ep.FlipV(); ep.NextB(); ep.FlipV(); // he precedente a e0 face::Pos<typename MSH_TYPE::FaceType> ep=e0; ep.FlipV(); ep.NextB(); ep.FlipV(); // he precedente a e0
MSH_TYPE::hedgepos_type en=e1; en.NextB(); // he successivo a e1 face::Pos<typename MSH_TYPE::FaceType> en=e1; en.NextB(); // he successivo a e1
// caso ear degenere per buco triangolare // caso ear degenere per buco triangolare
if(ep==en) return true; if(ep==en) return true;
@ -257,69 +285,73 @@ template<class MSH_TYPE> class TrivialEar
return false; return false;
} }
bool Close(TrivialEar &ne0, TrivialEar &ne1, MSH_TYPE::face_type* f) bool Close(TrivialEar &ne0, TrivialEar &ne1, typename MSH_TYPE::FaceType * f)
{ {
// simple topological check // simple topological check
if(e0.f==e1.f) { if(e0.f==e1.f) {
TRACE("Avoided bad ear"); //TRACE("Avoided bad ear");
printf("Avoided bad ear");
return false; return false;
} }
//usato per generare una delle due nuove orecchie. //usato per generare una delle due nuove orecchie.
MSH_TYPE::hedgepos_type ep=e0; ep.FlipV(); ep.NextB(); ep.FlipV(); // he precedente a e0 face::Pos<typename MSH_TYPE::FaceType> ep=e0; ep.FlipV(); ep.NextB(); ep.FlipV(); // he precedente a e0
MSH_TYPE::hedgepos_type en=e1; en.NextB(); // he successivo a e1 face::Pos<typename MSH_TYPE::FaceType> en=e1; en.NextB(); // he successivo a e1
(*f).V(0) = e0.VFlip(); (*f).V(0) = e0.VFlip();
(*f).V(1) = e0.v; (*f).V(1) = e0.v;
(*f).V(2) = e1.v; (*f).V(2) = e1.v;
(*f).F(0) = e0.f; (*f).FFp(0) = e0.f;
(*f).Z(0) = e0.z; (*f).FFi(0) = e0.z;
(*f).F(1) = e1.f; (*f).FFp(1) = e1.f;
(*f).Z(1) = e1.z; (*f).FFi(1) = e1.z;
(*f).F(2) = f; (*f).FFp(2) = f;
(*f).Z(2) = 2; (*f).FFi(2) = 2;
e0.f->F(e0.z)=f; e0.f->FFp(e0.z)=f;
e0.f->Z(e0.z)=0; e0.f->FFi(e0.z)=0;
e1.f->F(e1.z)=f; e1.f->FFp(e1.z)=f;
e1.f->Z(e1.z)=1; e1.f->FFi(e1.z)=1;
// caso ear degenere per buco triangolare // caso ear degenere per buco triangolare
if(ep==en) if(ep==en)
{ {
TRACE("Closing the last triangle"); //TRACE("Closing the last triangle");
f->F(2)=en.f; printf("Closing the last triangle");
f->Z(2)=en.z; f->FFp(2)=en.f;
en.f->F(en.z)=f; f->FFi(2)=en.z;
en.f->Z(en.z)=2; en.f->FFp(en.z)=f;
en.f->FFi(en.z)=2;
ne0.SetNull(); ne0.SetNull();
ne1.SetNull(); ne1.SetNull();
} }
// Caso ear non manifold a // Caso ear non manifold a
else if(ep.v==en.v) else if(ep.v==en.v)
{ {
TRACE("Ear Non manif A\n"); //TRACE("Ear Non manif A\n");
MSH_TYPE::hedgepos_type enold=en; printf("Ear Non manif A\n");
face::Pos<typename MSH_TYPE::FaceType> enold=en;
en.NextB(); en.NextB();
f->F(2)=enold.f; f->FFp(2)=enold.f;
f->Z(2)=enold.z; f->FFi(2)=enold.z;
enold.f->F(enold.z)=f; enold.f->FFp(enold.z)=f;
enold.f->Z(enold.z)=2; enold.f->FFi(enold.z)=2;
ne0=TrivialEar(ep); ne0=TrivialEar(ep);
ne1=TrivialEar(en); ne1=TrivialEar(en);
} }
// Caso ear non manifold b // Caso ear non manifold b
else if(ep.VFlip()==e1.v) else if(ep.VFlip()==e1.v)
{ {
TRACE("Ear Non manif B\n"); //TRACE("Ear Non manif B\n");
MSH_TYPE::hedgepos_type epold=ep; printf("Ear Non manif B\n");
face::Pos<typename MSH_TYPE::FaceType> epold=ep;
ep.FlipV(); ep.NextB(); ep.FlipV(); ep.FlipV(); ep.NextB(); ep.FlipV();
f->F(2)=epold.f; f->FFp(2)=epold.f;
f->Z(2)=epold.z; f->FFi(2)=epold.z;
epold.f->F(epold.z)=f; epold.f->FFp(epold.z)=f;
epold.f->Z(epold.z)=2; epold.f->FFi(epold.z)=2;
ne0=TrivialEar(ep); ne0=TrivialEar(ep);
ne1=TrivialEar(en); ne1=TrivialEar(en);
} }
@ -327,7 +359,7 @@ template<class MSH_TYPE> class TrivialEar
// Now compute the new ears; // Now compute the new ears;
{ {
ne0=TrivialEar(ep); ne0=TrivialEar(ep);
ne1=TrivialEar(MSH_TYPE::hedgepos_type(f,2,e1.v)); ne1=TrivialEar(face::Pos<typename MSH_TYPE::FaceType>(f,2,e1.v));
} }
return true; return true;
@ -346,41 +378,69 @@ template<class MSH_TYPE> class TrivialEar
// Attenzione: se per riaggiungere facce deve riallocare il vettore non funge!!!! // Attenzione: se per riaggiungere facce deve riallocare il vettore non funge!!!!
// //
template<class MESH, class EAR> template<class MESH, class EAR>
MESH::face_iterator CloseHole(MESH &m, HoleInfo<MESH> &h) typename MESH::FaceIterator CloseHole(MESH &m, HoleInfo <MESH> &h)
{ {
set<SimpleEdge<MESH> > ES; // vettore con tutti gli edge adiacenti al buco. std::vector<SimpleEdge<MESH> > ES;
//set<SimpleEdge<MESH> > ES; // vettore con tutti gli edge adiacenti al buco.
h.CollectEdges(ES); h.CollectEdges(ES);
vector<EAR> H; // Heap delle ear da chiudere vector<EAR> H; // Heap delle ear da chiudere
H.reserve(h.size); H.reserve(h.size);
std::vector<MESH::FacePointer *> app;
app.push_back( &h.p.f );
MESH::FaceIterator f = tri::Allocator<MESH>::AddFaces(m, h.size-2, app);
h.CollectEdges(ES);
assert(h.p.IsBorder()); assert(h.p.IsBorder());
MESH::hedgepos_type ep=h.p;
face::Pos<typename MESH::FaceType> ep=h.p;
do { do {
H.push_back(EAR(ep)); H.push_back(EAR(ep));
ep.f->SetS();
ep.NextB(); ep.NextB();
assert(ep.IsBorder());
} while(ep!=h.p); } while(ep!=h.p);
make_heap(H.begin(),H.end()); make_heap(H.begin(),H.end());
int cnt=h.size; int cnt=h.size;
EAR en0,en1; EAR en0,en1;
MESH::face_iterator f=m.AddFaces(h.size-2);
MESH::face_iterator firstf=f; MESH::FaceIterator firstf = f;
SimpleEdge<MESH> se(0,0);
while(cnt>2 && !H.empty()) //SimpleEdge<MESH> se();
while(cnt > 2 && !H.empty())
{ {
pop_heap(H.begin(),H.end()); //pop_heap(H.begin(),H.end());
se=SimpleEdge<MESH>(H.back().e0.VFlip(), H.back().e1.v);
SimpleEdge<MESH> se( *(H.back().e0.VFlip()) , *(H.back().e1.v));
// se.v = p.v;
// se.v[1] = p.v[1];
//Sostituito la funzione find con la ricerca manuale
std::vector<SimpleEdge<MESH> >::iterator it;
it = ES.begin();
while( it != ES.end() &&
se != ((SimpleEdge<MESH> )(*it)) )
{it++; }
//per far funzionare il test sottostante.
if(H.back().IsUpToDate()) if(H.back().IsUpToDate())
{ {
if(!H.back().Degen() && ES.find(se)!=ES.end()){
if(/*!*/H.back().Degen() && it != ES.end()){ /*Test sbagliato*/
// Nota che nel caso di ear degeneri si DEVE permettere la creazione di un edge che gia'esiste // Nota che nel caso di ear degeneri si DEVE permettere la creazione di un edge che gia'esiste
TRACE("Evitata orecchia brutta!"); //TRACE("Evitata orecchia brutta!");
printf("\n -> Evitata orecchia brutta!");
} }
else if(H.back().Close(en0,en1,&*f)) else
if(H.back().Close(en0,en1,&*f))
{ {
ES.insert(se); //ES.insert(se);
ES.push_back(se);
if(!en0.IsNull()){ if(!en0.IsNull()){
H.push_back(en0); H.push_back(en0);
push_heap( H.begin(), H.end()); push_heap( H.begin(), H.end());
@ -414,7 +474,7 @@ template<class MSH_TYPE> class TrivialEarN : public TrivialEar<MSH_TYPE>
public: public:
TrivialEarN(){} TrivialEarN(){}
TrivialEarN(const MSH_TYPE::hedgepos_type & ep) TrivialEarN(const face::Pos<typename MSH_TYPE::FaceType> & ep)
{ {
e0=ep; e0=ep;
assert(e0.IsBorder()); assert(e0.IsBorder());
@ -424,9 +484,9 @@ template<class MSH_TYPE> class TrivialEarN : public TrivialEar<MSH_TYPE>
} }
static MSH_TYPE::vectorial_type &PreferredNormal() static typename MSH_TYPE::VertexType &PreferredNormal()
{ {
static MSH_TYPE::vectorial_type nn; static MSH_TYPE::VertexType nn;
return nn; return nn;
} }
@ -451,7 +511,7 @@ static double Area(const vector<Point2d> &contour)
double A=0.0f; double A=0.0f;
for(int p=n-1,q=0; q<n; p=q++) { for(int p=n-1,q=0; q<n; p=q++) {
A+= contour[p].x()*contour[q].y() - contour[q].x()*contour[p].y(); A+= contour[p].X()*contour[q].Y() - contour[q].X()*contour[p].Y();
} }
return A*0.5f; return A*0.5f;
} }
@ -489,22 +549,22 @@ static bool Snip(const vector<Point2d> &contour,int u,int v,int w,int n,int *V)
double Ax, Ay, Bx, By, Cx, Cy, Px, Py; double Ax, Ay, Bx, By, Cx, Cy, Px, Py;
const double epsilon =1e-2; const double epsilon =1e-2;
Ax = contour[V[u]].x(); Ax = contour[V[u]].X();
Ay = contour[V[u]].y(); Ay = contour[V[u]].Y();
Bx = contour[V[v]].x(); Bx = contour[V[v]].X();
By = contour[V[v]].y(); By = contour[V[v]].Y();
Cx = contour[V[w]].x(); Cx = contour[V[w]].X();
Cy = contour[V[w]].y(); Cy = contour[V[w]].Y();
if ( epsilon> (((Bx-Ax)*(Cy-Ay)) - ((By-Ay)*(Cx-Ax))) ) return false; if ( epsilon> (((Bx-Ax)*(Cy-Ay)) - ((By-Ay)*(Cx-Ax))) ) return false;
for (p=0;p<n;p++) for (p=0;p<n;p++)
{ {
if( (p == u) || (p == v) || (p == w) ) continue; if( (p == u) || (p == v) || (p == w) ) continue;
Px = contour[V[p]].x(); Px = contour[V[p]].X();
Py = contour[V[p]].y(); Py = contour[V[p]].Y();
if (InsideTriangle(Ax,Ay,Bx,By,Cx,Cy,Px,Py)) return false; if (InsideTriangle(Ax,Ay,Bx,By,Cx,Cy,Px,Py)) return false;
} }
@ -535,7 +595,7 @@ static bool Process(const vector<Point2d> &contour,vector<int> &result)
/* remove nv-2 Vertices, creating 1 triangle every time */ /* remove nv-2 Vertices, creating 1 triangle every time */
int count = 2*nv; /* error detection */ int count = 2*nv; /* error detection */
double CurrBest=Sqrt(area)/1000; double CurrBest= sqrt(area)/1000;
for(int m=0, v=nv-1; nv>2; ) for(int m=0, v=nv-1; nv>2; )
{ {
@ -546,7 +606,7 @@ static bool Process(const vector<Point2d> &contour,vector<int> &result)
CurrBest*=1.3; CurrBest*=1.3;
count = 2*nv; count = 2*nv;
if(CurrBest>Sqrt(area)*2) if(CurrBest > sqrt(area)*2)
return false; return false;
} }
@ -586,4 +646,5 @@ static bool Process(const vector<Point2d> &contour,vector<int> &result)
}; };
} // end namespace } // end namespace
}
#endif #endif