Substituted grid with kdtree in the ballpivoting...

This commit is contained in:
Paolo Cignoni 2012-11-14 12:48:35 +00:00
parent f2be4a4d95
commit 41e7a19fd1
2 changed files with 40 additions and 54 deletions

View File

@ -6,7 +6,6 @@
#include <wrap/callback.h> #include <wrap/callback.h>
#include <vcg/complex/algorithms/update/topology.h> #include <vcg/complex/algorithms/update/topology.h>
#include <vcg/complex/algorithms/update/flag.h> #include <vcg/complex/algorithms/update/flag.h>
#include <map>
namespace vcg { namespace vcg {
namespace tri { namespace tri {

View File

@ -2,7 +2,8 @@
#define BALL_PIVOTING_H #define BALL_PIVOTING_H
#include "advancing_front.h" #include "advancing_front.h"
#include <vcg/space/index/grid_static_ptr.h> #include <vcg/space/index/kdtree/kdtree.h>
#include <vcg/complex/algorithms/closest.h> #include <vcg/complex/algorithms/closest.h>
/* Ball pivoting algorithm: /* Ball pivoting algorithm:
@ -22,7 +23,6 @@ template <class MESH> class BallPivoting: public AdvancingFront<MESH> {
typedef typename MESH::ScalarType ScalarType; typedef typename MESH::ScalarType ScalarType;
typedef typename MESH::VertexIterator VertexIterator; typedef typename MESH::VertexIterator VertexIterator;
typedef typename MESH::VertexType::CoordType Point3x; typedef typename MESH::VertexType::CoordType Point3x;
typedef GridStaticPtr<typename MESH::VertexType, typename MESH::ScalarType > StaticGrid;
float radius; //radius of the ball float radius; //radius of the ball
float min_edge; //min length of an edge float min_edge; //min length of an edge
@ -30,7 +30,6 @@ template <class MESH> class BallPivoting: public AdvancingFront<MESH> {
float max_angle; //max angle between 2 faces (cos(angle) actually) float max_angle; //max angle between 2 faces (cos(angle) actually)
public: public:
ScalarType radi() { return radius; }
// if radius ==0 an autoguess for the ball pivoting radius is attempted // if radius ==0 an autoguess for the ball pivoting radius is attempted
// otherwise the passed value (in absolute mesh units) is used. // otherwise the passed value (in absolute mesh units) is used.
@ -58,57 +57,49 @@ template <class MESH> class BallPivoting: public AdvancingFront<MESH> {
min_edge *= radius; min_edge *= radius;
max_edge *= radius; max_edge *= radius;
//enlarging the bbox for out-of-box queries VertexConstDataWrapper<MESH> ww(this->mesh);
Box3<ScalarType> BPbbox=this->mesh.bbox; tree = new KdTree<float>(ww);
BPbbox.Offset(4*radius); tree->setMaxNofNeighbors(16);
grid.Set(this->mesh.vert.begin(), this->mesh.vert.end(), BPbbox);
//mark visited points
std::vector<VertexType *> targets;
std::vector<Point3x> points;
std::vector<ScalarType> dists;
usedBit = VertexType::NewBitFlag(); usedBit = VertexType::NewBitFlag();
for(int i = 0; i < (int)this->mesh.vert.size(); i++) UpdateFlags<MESH>::VertexClear(this->mesh,usedBit);
this->mesh.vert[i].ClearUserBit(usedBit);
UpdateFlags<MESH>::VertexClearV(this->mesh); UpdateFlags<MESH>::VertexClearV(this->mesh);
for(int i = 0; i < (int)this->mesh.face.size(); i++) { for(int i = 0; i < (int)this->mesh.face.size(); i++) {
FaceType &f = this->mesh.face[i]; FaceType &f = this->mesh.face[i];
if(f.IsD()) continue; if(f.IsD()) continue;
for(int k = 0; k < 3; k++) { for(int k = 0; k < 3; k++) {
f.V(k)->SetV(); Mark(f.V(k));
int n = tri::GetInSphereVertex(this->mesh, grid, f.V(k)->P(), min_edge, targets, dists, points);
for(int t = 0; t < n; t++) {
targets[t]->SetUserBit(usedBit);
assert(targets[t]->IsUserBit(usedBit));
}
assert(f.V(k)->IsUserBit(usedBit));
} }
} }
} }
~BallPivoting() { ~BallPivoting() {
VertexType::DeleteBitFlag(usedBit); VertexType::DeleteBitFlag(usedBit);
delete tree;
} }
bool Seed(int &v0, int &v1, int &v2) { bool Seed(int &v0, int &v1, int &v2) {
//get a sphere of neighbours //get a sphere of neighbours
std::vector<VertexType *> targets; std::vector<VertexType *> targets;
std::vector<Point3x> points;
std::vector<ScalarType> dists;
while(++last_seed < (int)(this->mesh.vert.size())) { while(++last_seed < (int)(this->mesh.vert.size())) {
VertexType &seed = this->mesh.vert[last_seed]; VertexType &seed = this->mesh.vert[last_seed];
if(seed.IsD() || seed.IsUserBit(usedBit)) continue; if(seed.IsD() || seed.IsUserBit(usedBit)) continue;
seed.SetUserBit(usedBit); seed.SetUserBit(usedBit);
int n = tri::GetInSphereVertex(this->mesh, grid, seed.P(), 2*radius, targets, dists, points); tree->doQueryK(seed.P());
if(n < 3) { int nn = tree->getNofFoundNeighbors();
continue; for(int i=0;i<nn;++i)
{
VertexType *vp = &this->mesh.vert[tree->getNeighborId(i)];
if(Distance(seed.P(),vp->cP()) > 2*radius) continue;
targets.push_back(vp);
} }
int n = targets.size();
if(n<3) continue;
bool success = true; bool success = true;
//find the closest visited or boundary //find the closest visited or boundary
for(int i = 0; i < n; i++) { for(int i = 0; i < n; i++) {
@ -230,25 +221,21 @@ template <class MESH> class BallPivoting: public AdvancingFront<MESH> {
// r is the radius of the thorus of all possible spheres passing throug v0 and v1 // r is the radius of the thorus of all possible spheres passing throug v0 and v1
ScalarType r = sqrt(radius*radius - axis_len/4); ScalarType r = sqrt(radius*radius - axis_len/4);
std::vector<VertexType *> targets; // The vector of
std::vector<ScalarType> dists; // never used.
std::vector<Point3x> points; // never used.
tri::GetInSphereVertex(this->mesh, grid, middle, r + radius, targets, dists, points); tree->doQueryK(middle);
int nn = tree->getNofFoundNeighbors();
if(targets.size() == 0) { if(nn==0) return -1;
return -1; //this really would be strange but one never knows.
}
VertexType *candidate = NULL; VertexType *candidate = NULL;
ScalarType min_angle = M_PI; ScalarType min_angle = M_PI;
// //
// Loop over all the nearest vertexes and choose the best one according the ball pivoting strategy. // Loop over all the nearest vertexes and choose the best one according the ball pivoting strategy.
// //
for(size_t i = 0; i < targets.size(); i++) { for (int i = 0; i < nn; i++) {
VertexType *v = targets[i]; int vInd = tree->getNeighborId(i);
if(v->IsD()) continue; VertexType *v = &this->mesh.vert[vInd];
int vInd = tri::Index(this->mesh,v); if(Distance(middle,v->cP()) > r + radius) continue;
// this should always be true IsB => IsV , IsV => IsU // this should always be true IsB => IsV , IsV => IsU
if(v->IsB()) assert(v->IsV()); if(v->IsB()) assert(v->IsV());
if(v->IsV()) assert(v->IsUserBit(usedBit)); if(v->IsV()) assert(v->IsUserBit(usedBit));
@ -335,8 +322,7 @@ template <class MESH> class BallPivoting: public AdvancingFront<MESH> {
int last_seed; //used for new seeds when front is empty int last_seed; //used for new seeds when front is empty
int usedBit; //use to detect if a vertex has been already processed. int usedBit; //use to detect if a vertex has been already processed.
Point3x baricenter;//used for the first seed. Point3x baricenter;//used for the first seed.
KdTree<float> *tree;
StaticGrid grid; //lookup grid for points
/* returns the sphere touching p0, p1, p2 of radius r such that /* returns the sphere touching p0, p1, p2 of radius r such that
@ -404,12 +390,13 @@ template <class MESH> class BallPivoting: public AdvancingFront<MESH> {
} }
void Mark(VertexType *v) { void Mark(VertexType *v) {
std::vector<VertexType *> targets; tree->doQueryK(v->cP());
std::vector<Point3x> points; int n = tree->getNofFoundNeighbors();
std::vector<ScalarType> dists; for (int i = 0; i < n; i++) {
int n = tri::GetInSphereVertex(this->mesh, grid, v->P(), min_edge, targets, dists, points); VertexType *vp = &this->mesh.vert[tree->getNeighborId(i)];
for(int t = 0; t < n; t++) if(Distance(v->cP(),vp->cP())<min_edge)
targets[t]->SetUserBit(usedBit); vp->SetUserBit(usedBit);
}
v->SetV(); v->SetV();
} }
}; };