further cleaning up and commenting

This commit is contained in:
Paolo Cignoni 2013-02-28 15:51:38 +00:00
parent 391e376be7
commit 0f34456c92
1 changed files with 115 additions and 119 deletions

View File

@ -64,13 +64,13 @@ public:
typedef vcg::GridStaticPtr<typename PMesh::VertexType, ScalarType > GridType;
/* class for Parameters */
struct Parameters
struct Param
{
ScalarType delta;
ScalarType delta; // Approximation Level
int feetsize; // how many points in the neighborhood of each of the 4 points
ScalarType f; // overlap estimation
int scoreFeet, // how many of the feetsize points must match (max feetsize*4) to try an early interrupt
scoreAln; // how good must be the alignement to end the process successfully
ScalarType f; // overlap estimation as a percentage
int scoreFeet; // how many of the feetsize points must match (max feetsize*4) to try an early interrupt
int scoreAln; // how good must be the alignement to end the process successfully
void Default(){
delta = 0.5;
@ -81,11 +81,11 @@ public:
}
};
Parameters prs; /// parameters
Param par; /// parameters
public:
void Init(MeshType &_P,MeshType &_Q);
bool Align( int L, vcg::Matrix44f & result, AACb * cb = NULL ); // main function
bool Align( int L, vcg::Matrix44f & result, vcg::CallBackPos * cb = NULL ); // main function
private:
struct Couple: public std::pair<int,int>
@ -97,8 +97,6 @@ private:
int & operator[](const int &i){return (i==0)? first : second;}
};
/* returns the closest point between to segments x1-x2 and x3-x4. */
void IntersectionLineLine(const CoordType & x1,const CoordType & x2,const CoordType & x3,const CoordType & x4, CoordType&x)
{
@ -109,27 +107,28 @@ private:
struct CandiType{
CandiType(){}
CandiType(FourPoints _p,vcg::Matrix44<ScalarType>_T):p(_p),T(_T){}
struct Candidate
{
Candidate(){}
Candidate(FourPoints _p,vcg::Matrix44<ScalarType>_T):p(_p),T(_T){}
FourPoints p;
vcg::Matrix44<ScalarType> T;
ScalarType err;
int score;
int base; // debug: for which base
inline bool operator <(const CandiType & o) const {return score > o.score;}
inline bool operator <(const Candidate & o) const {return score > o.score;}
};
MeshType *P, // mesh from which the coplanar base is selected
*Q; // mesh where to find the correspondences
MeshType *P; // mesh from which the coplanar base is selected
MeshType *Q; // mesh where to find the correspondences
std::vector<int> mapsub; // subset of index to the vertices in Q
PMesh Invr; // invariants
std::vector< CandiType > U;
CandiType winner;
std::vector< Candidate > U;
Candidate winner;
int iwinner; // winner == U[iwinner]
FourPoints B; // coplanar base
@ -160,9 +159,9 @@ private:
void ComputeR1R2(ScalarType d1,ScalarType d2);
bool IsTransfCongruent(FourPoints fp,vcg::Matrix44<ScalarType> & mat, float & trerr);
int EvaluateSample(CandiType & fp, CoordType & tp, CoordType & np, const float & angle);
void EvaluateAlignment(CandiType & fp);
void TestAlignment(CandiType & fp);
int EvaluateSample(Candidate & fp, CoordType & tp, CoordType & np, const float & angle);
void EvaluateAlignment(Candidate & fp);
void TestAlignment(Candidate & fp);
/* debug tools */
public:
@ -197,9 +196,8 @@ public:
};
template <class MeshType>
void
FourPCS<MeshType>:: Init(MeshType &_P,MeshType &_Q){
void FourPCS<MeshType>:: Init(MeshType &_P,MeshType &_Q)
{
P = &_P;Q=&_Q;
ugridQ.Set(Q->vert.begin(),Q->vert.end());
ugridP.Set(P->vert.begin(),P->vert.end());
@ -232,8 +230,8 @@ FourPCS<MeshType>:: Init(MeshType &_P,MeshType &_Q){
avD /=100; // average vertex-vertex distance
avD /= sqrt(ratio); // take into account the ratio
prs.delta = avD * prs.delta;
side = P->bbox.Dim()[P->bbox.MaxDim()]*prs.f; //rough implementation
par.delta = avD * par.delta;
side = P->bbox.Dim()[P->bbox.MaxDim()]*par.f; //rough implementation
}
@ -322,7 +320,7 @@ FourPCS<MeshType>::SelectCoplanarBase(){
r1 = (x - B[0]).dot(B[1]-B[0]) / (B[1]-B[0]).SquaredNorm();
r2 = (x - B[2]).dot(B[3]-B[2]) / (B[3]-B[2]).SquaredNorm();
if( ((B[0]+(B[1]-B[0])*r1)-(B[2]+(B[3]-B[2])*r2)).Norm() > prs.delta )
if( ((B[0]+(B[1]-B[0])*r1)-(B[2]+(B[3]-B[2])*r2)).Norm() > par.delta )
return false;
radius =side*0.5;
@ -335,7 +333,7 @@ FourPCS<MeshType>::SelectCoplanarBase(){
vcg::GridStaticPtr<typename MeshType::VertexType, ScalarType >,
std::vector<VertexType*>,
std::vector<ScalarType>,
std::vector< CoordType > >(*P,ugridP, prs.feetsize ,B[i],radius, ExtB[i],dists, samples);
std::vector< CoordType > >(*P,ugridP, par.feetsize ,B[i],radius, ExtB[i],dists, samples);
}
//for(int i = 0 ; i< 4; ++i)
@ -368,7 +366,7 @@ bool FourPCS<MeshType>::IsTransfCongruent(FourPoints fp, vcg::Matrix44<ScalarTyp
for(int i = 0; i < 4; ++i) err+= (mat * mov[i] - fix[i]).SquaredNorm();
trerr = vcg::math::Sqrt(err);
return err < prs.delta* prs.delta*4.0;
return err < par.delta* par.delta*4.0;
}
template <class MeshType>
@ -400,8 +398,7 @@ FourPCS<MeshType>::ComputeR1R2(ScalarType d1,ScalarType d2){
}
template <class MeshType>
bool
FourPCS<MeshType>::FindCongruent() { // of base B, on Q, with approximation delta
bool FourPCS<MeshType>::FindCongruent() { // of base B, on Q, with approximation delta
bool done = false;
std::vector<EPoint> R2inv;
int n_closests = 0, n_congr = 0;
@ -415,13 +412,13 @@ FourPCS<MeshType>::FindCongruent() { // of base B, on Q, with approximation delt
typename PMesh::VertexIterator vii;
typename std::vector<Couple>::iterator bR1,eR1,bR2,eR2,ite,cite;
bR1 = std::lower_bound<typename std::vector<Couple>::iterator,Couple>(R1.begin(),R1.end(),Couple(d1-prs.delta*2.0));
eR1 = std::lower_bound<typename std::vector<Couple>::iterator,Couple>(R1.begin(),R1.end(),Couple(d1+prs.delta*2.0));
bR2 = std::lower_bound<typename std::vector<Couple>::iterator,Couple>(R1.begin(),R1.end(),Couple(d2-prs.delta*2.0));
eR2 = std::lower_bound<typename std::vector<Couple>::iterator,Couple>(R1.begin(),R1.end(),Couple(d2+prs.delta*2.0));
bR1 = std::lower_bound<typename std::vector<Couple>::iterator,Couple>(R1.begin(),R1.end(),Couple(d1-par.delta*2.0));
eR1 = std::lower_bound<typename std::vector<Couple>::iterator,Couple>(R1.begin(),R1.end(),Couple(d1+par.delta*2.0));
bR2 = std::lower_bound<typename std::vector<Couple>::iterator,Couple>(R1.begin(),R1.end(),Couple(d2-par.delta*2.0));
eR2 = std::lower_bound<typename std::vector<Couple>::iterator,Couple>(R1.begin(),R1.end(),Couple(d2+par.delta*2.0));
// in [bR1,eR1) there are all the pairs ad a distance d1 +- prs.delta
// in [bR1,eR1) there are all the pairs ad a distance d2 +- prs.delta
// in [bR1,eR1) there are all the pairs ad a distance d1 +- par.delta
// in [bR1,eR1) there are all the pairs ad a distance d2 +- par.delta
if(bR1 == R1.end()) return false;// if there are no such pairs return
if(bR2 == R1.end()) return false; // if there are no such pairs return
@ -455,18 +452,16 @@ FourPCS<MeshType>::FindCongruent() { // of base B, on Q, with approximation delt
}
n_closests = 0; n_congr = 0; ac =0 ; acf = 0; tr = 0; trf = 0;
// fprintf(db,"R2Inv.size = %d \n",R2inv.size());
printf("R2Inv.size = %d \n",R2inv.size());
for(uint i = 0 ; i < R2inv.size() ; ++i){
std::vector<typename PMesh::VertexType*> closests;
std::vector<ScalarType> distances;
std::vector<CoordType> points;
// for each point in R2inv get all the points in R1 closer than prs.delta
// for each point in R2inv get all the points in R1 closer than par.delta
vcg::Matrix44<ScalarType> mat;
vcg::Box3f bb;
bb.Add(R2inv[i].pos+vcg::Point3f(prs.delta * 0.1,prs.delta * 0.1 , prs.delta * 0.1 ));
bb.Add(R2inv[i].pos-vcg::Point3f(prs.delta * 0.1,prs.delta* 0.1 , prs.delta* 0.1));
bb.Add(R2inv[i].pos+vcg::Point3f(par.delta * 0.1,par.delta * 0.1 , par.delta * 0.1 ));
bb.Add(R2inv[i].pos-vcg::Point3f(par.delta * 0.1,par.delta* 0.1 , par.delta* 0.1));
vcg::tri::GetInBoxVertex<PMesh,GridType,std::vector<typename PMesh::VertexType*> >
(Invr,*ugrid,bb,closests);
@ -491,13 +486,13 @@ FourPCS<MeshType>::FindCongruent() { // of base B, on Q, with approximation delt
else{
tr++;
n_congr++;
U.push_back(CandiType(p,mat));
U.push_back(Candidate(p,mat));
EvaluateAlignment(U.back());
U.back().base = bases.size()-1;
if( U.back().score > prs.scoreFeet){
if( U.back().score > par.scoreFeet){
TestAlignment(U.back());
if(U.back().score > prs.scoreAln)
if(U.back().score > par.scoreAln)
{
done = true; break;
}
@ -522,10 +517,11 @@ FourPCS<MeshType>::FindCongruent() { // of base B, on Q, with approximation delt
template <class MeshType>
int FourPCS<MeshType>::EvaluateSample(CandiType & fp, CoordType & tp, CoordType & np, const float & angle){
int FourPCS<MeshType>::EvaluateSample(Candidate & fp, CoordType & tp, CoordType & np, const float & cosAngle)
{
VertexType* v;
ScalarType dist ;
radius = prs.delta;
radius = par.delta;
tp = fp.T * tp;
vcg::Point4<ScalarType> np4;
@ -546,14 +542,14 @@ int FourPCS<MeshType>::EvaluateSample(CandiType & fp, CoordType & tp, CoordType
>(*Q,ugridQ,vq,radius, dist );
if(v!=0)
if( v->N().dot(np) -angle >0) return 1; else return -1;
if( v->N().dot(np) - cosAngle >0) return 1; else return -1;
}
template <class MeshType>
void
FourPCS<MeshType>::EvaluateAlignment(CandiType & fp){
FourPCS<MeshType>::EvaluateAlignment(Candidate & fp){
int n_delta_close = 0;
for(int i = 0 ; i< 4; ++i) {
for(uint j = 0; j < ExtB[i].size();++j){
@ -567,8 +563,8 @@ FourPCS<MeshType>::EvaluateAlignment(CandiType & fp){
template <class MeshType>
void
FourPCS<MeshType>::TestAlignment(CandiType & fp){
radius = prs.delta;
FourPCS<MeshType>::TestAlignment(Candidate & fp){
radius = par.delta;
int n_delta_close = 0;
for(uint j = 0; j < subsetP.size();++j){
CoordType np = subsetP[j]->N();
@ -581,7 +577,7 @@ FourPCS<MeshType>::TestAlignment(CandiType & fp){
template <class MeshType>
bool
FourPCS<MeshType>:: Align( int L, vcg::Matrix44f & result, AACb * cb ){ // main loop
FourPCS<MeshType>:: Align( int L, vcg::Matrix44f & result, vcg::CallBackPos * cb ){ // main loop
int bestv = 0;
bool found;
@ -590,7 +586,7 @@ FourPCS<MeshType>:: Align( int L, vcg::Matrix44f & result, AACb * cb ){ // mai
if(L==0)
{
L = (log(1.0-0.9999) / log(1.0-pow((float)prs.f,3.f)))+1;
L = (log(1.0-0.9999) / log(1.0-pow((float)par.f,3.f)))+1;
printf("using %d bases\n",L);
}
@ -605,13 +601,13 @@ FourPCS<MeshType>:: Align( int L, vcg::Matrix44f & result, AACb * cb ){ // mai
}
while(!found && (n_tries <50));
if(!found) {
prs.f*=0.98;
side = P->bbox.Dim()[P->bbox.MaxDim()]*prs.f; //rough implementation
par.f*=0.98;
side = P->bbox.Dim()[P->bbox.MaxDim()]*par.f; //rough implementation
ComputeR1R2(side*1.4,side*1.4);
}
} while (!found && (prs.f >0.1));
} while (!found && (par.f >0.1));
if(prs.f <0.1) {
if(par.f <0.1) {
printf("FAILED");
return false;
}