Geodesic: Added possibility of saving also the implict tree of the shortest path. For each vertex you can give an attribute where the function will save the 'parent' e.g. the previous vertex in the shortest path to the closest source.
This commit is contained in:
parent
1c5f2c2264
commit
ffdc2f2b28
vcg/complex/algorithms
|
@ -73,37 +73,37 @@ namespace vcg{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* Auxiliary class for keeping the heap of vertices to visit and their estimated distance
|
/* Auxiliary class for keeping the heap of vertices to visit and their estimated distance */
|
||||||
*/
|
|
||||||
struct VertDist{
|
struct VertDist{
|
||||||
VertDist(){}
|
VertDist(){}
|
||||||
VertDist(VertexPointer _v, ScalarType _d):v(_v),d(_d){}
|
VertDist(VertexPointer _v, ScalarType _d):v(_v),d(_d){}
|
||||||
|
|
||||||
VertexPointer v;
|
VertexPointer v;
|
||||||
ScalarType d;
|
ScalarType d;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/* Temporary data to associate to all the vertices: estimated distance and boolean flag
|
/* Temporary data to associate to all the vertices: estimated distance and boolean flag */
|
||||||
*/
|
|
||||||
struct TempData{
|
struct TempData{
|
||||||
TempData(){}
|
TempData(){}
|
||||||
TempData(const ScalarType & d_){d=d_;source = NULL;}
|
TempData(const ScalarType & _d):d(_d),source(0),parent(0){}
|
||||||
|
|
||||||
ScalarType d;
|
ScalarType d;
|
||||||
VertexPointer source;//closest source
|
VertexPointer source;//closest source
|
||||||
|
VertexPointer parent;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef SimpleTempData<std::vector<VertexType>, TempData > TempDataType;
|
typedef SimpleTempData<std::vector<VertexType>, TempData > TempDataType;
|
||||||
//TempDataType * TD;
|
|
||||||
|
|
||||||
|
|
||||||
struct pred: public std::binary_function<VertDist,VertDist,bool>{
|
struct pred: public std::binary_function<VertDist,VertDist,bool>{
|
||||||
pred(){};
|
pred(){}
|
||||||
bool operator()(const VertDist& v0, const VertDist& v1) const
|
bool operator()(const VertDist& v0, const VertDist& v1) const
|
||||||
{return (v0.d > v1.d);}
|
{return (v0.d > v1.d);}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct pred_addr: public std::binary_function<VertDist,VertDist,bool>{
|
struct pred_addr: public std::binary_function<VertDist,VertDist,bool>{
|
||||||
pred_addr(){};
|
pred_addr(){}
|
||||||
bool operator()(const VertDist& v0, const VertDist& v1) const
|
bool operator()(const VertDist& v0, const VertDist& v1) const
|
||||||
{return (v0.v > v1.v);}
|
{return (v0.v > v1.v);}
|
||||||
};
|
};
|
||||||
|
@ -163,62 +163,56 @@ namespace vcg{
|
||||||
*/
|
*/
|
||||||
static VertexPointer Visit(
|
static VertexPointer Visit(
|
||||||
MeshType & m,
|
MeshType & m,
|
||||||
std::vector<VertDist> & seedVec,
|
std::vector<VertDist> & seedVec, // the set of seed to start from
|
||||||
bool farthestOnBorder = false,
|
bool farthestOnBorder = false,
|
||||||
ScalarType distance_threshold = std::numeric_limits<ScalarType>::max(),
|
ScalarType distance_threshold = std::numeric_limits<ScalarType>::max(), // cut off distance (do no compute anything farther than this value)
|
||||||
typename MeshType::template PerVertexAttributeHandle<VertexPointer> * vertSource = NULL,
|
typename MeshType::template PerVertexAttributeHandle<VertexPointer> * vertSource = NULL, // if present we put in this attribute the closest source for each vertex
|
||||||
|
typename MeshType::template PerVertexAttributeHandle<VertexPointer> * vertParent = NULL, // if present we put in this attribute the parent in the path that goes from the vertex to the closest source
|
||||||
std::vector<VertexPointer> *InInterval=NULL)
|
std::vector<VertexPointer> *InInterval=NULL)
|
||||||
{
|
{
|
||||||
bool isLeaf;
|
|
||||||
std::vector<VertDist> frontier;
|
std::vector<VertDist> frontier;
|
||||||
VertexPointer curr,farthest=0,pw1;
|
VertexPointer farthest=0,pw,pw1;
|
||||||
ScalarType unreached = std::numeric_limits<ScalarType>::max();
|
|
||||||
|
|
||||||
VertexPointer pw;
|
|
||||||
|
|
||||||
//Requirements
|
//Requirements
|
||||||
assert(HasPerVertexVFAdjacency(m) && HasPerFaceVFAdjacency(m));
|
assert(HasPerVertexVFAdjacency(m) && HasPerFaceVFAdjacency(m));
|
||||||
assert(!seedVec.empty());
|
assert(!seedVec.empty());
|
||||||
|
|
||||||
TempDataType TD(m.vert,unreached);
|
TempDataType TD(m.vert, std::numeric_limits<ScalarType>::max());
|
||||||
|
|
||||||
typename std::vector <VertDist >::iterator ifr;
|
typename std::vector <VertDist >::iterator ifr;
|
||||||
for(ifr = seedVec.begin(); ifr != seedVec.end(); ++ifr){
|
for(ifr = seedVec.begin(); ifr != seedVec.end(); ++ifr){
|
||||||
TD[(*ifr).v].d = 0.0;
|
|
||||||
(*ifr).d = 0.0;
|
(*ifr).d = 0.0;
|
||||||
|
TD[(*ifr).v].d = 0.0;
|
||||||
TD[(*ifr).v].source = (*ifr).v;
|
TD[(*ifr).v].source = (*ifr).v;
|
||||||
|
TD[(*ifr).v].parent = (*ifr).v;
|
||||||
frontier.push_back(VertDist((*ifr).v,0.0));
|
frontier.push_back(VertDist((*ifr).v,0.0));
|
||||||
}
|
}
|
||||||
// initialize Heap
|
// initialize Heap
|
||||||
make_heap(frontier.begin(),frontier.end(),pred());
|
make_heap(frontier.begin(),frontier.end(),pred());
|
||||||
|
|
||||||
ScalarType curr_d,d_curr = 0.0,d_heap;
|
ScalarType curr_d,d_curr = 0.0,d_heap;
|
||||||
VertexPointer curr_s = NULL;
|
|
||||||
ScalarType max_distance=0.0;
|
ScalarType max_distance=0.0;
|
||||||
typename std::vector<VertDist >:: iterator iv;
|
|
||||||
|
|
||||||
while(!frontier.empty() && max_distance < distance_threshold)
|
while(!frontier.empty() && max_distance < distance_threshold)
|
||||||
{
|
{
|
||||||
pop_heap(frontier.begin(),frontier.end(),pred());
|
pop_heap(frontier.begin(),frontier.end(),pred());
|
||||||
curr = (frontier.back()).v;
|
VertexPointer curr = (frontier.back()).v;
|
||||||
if (InInterval!=NULL)
|
if (InInterval!=NULL) InInterval->push_back(curr);
|
||||||
InInterval->push_back(curr);
|
|
||||||
|
if(vertSource!=NULL) (*vertSource)[curr] = TD[curr].source;
|
||||||
|
if(vertParent!=NULL) (*vertParent)[curr] = TD[curr].parent;
|
||||||
|
|
||||||
curr_s = TD[curr].source;
|
|
||||||
if(vertSource!=NULL)
|
|
||||||
(*vertSource)[curr] = curr_s;
|
|
||||||
d_heap = (frontier.back()).d;
|
d_heap = (frontier.back()).d;
|
||||||
frontier.pop_back();
|
frontier.pop_back();
|
||||||
|
|
||||||
assert(TD[curr].d <= d_heap);
|
assert(TD[curr].d <= d_heap);
|
||||||
assert(curr_s != NULL);
|
|
||||||
if(TD[curr].d < d_heap )// a vertex whose distance has been improved after it was inserted in the queue
|
if(TD[curr].d < d_heap )// a vertex whose distance has been improved after it was inserted in the queue
|
||||||
continue;
|
continue;
|
||||||
assert(TD[curr].d == d_heap);
|
assert(TD[curr].d == d_heap);
|
||||||
|
|
||||||
d_curr = TD[curr].d;
|
d_curr = TD[curr].d;
|
||||||
|
|
||||||
isLeaf = (!farthestOnBorder || curr->IsB());
|
bool isLeaf = (!farthestOnBorder || curr->IsB());
|
||||||
|
|
||||||
face::VFIterator<FaceType> x;int k;
|
face::VFIterator<FaceType> x;int k;
|
||||||
|
|
||||||
|
@ -249,9 +243,10 @@ namespace vcg{
|
||||||
curr_d = Distance(pw,pw1,curr,d_pw1,d_curr);
|
curr_d = Distance(pw,pw1,curr,d_pw1,d_curr);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(TD[(pw)].d > curr_d){
|
if(TD[pw].d > curr_d){
|
||||||
TD[(pw)].d = curr_d;
|
TD[pw].d = curr_d;
|
||||||
TD[pw].source = curr_s;
|
TD[pw].source = TD[curr].source;
|
||||||
|
TD[pw].parent = curr;
|
||||||
frontier.push_back(VertDist(pw,curr_d));
|
frontier.push_back(VertDist(pw,curr_d));
|
||||||
push_heap(frontier.begin(),frontier.end(),pred());
|
push_heap(frontier.begin(),frontier.end(),pred());
|
||||||
}
|
}
|
||||||
|
@ -298,31 +293,25 @@ namespace vcg{
|
||||||
VertexPointer & farthest_vert,
|
VertexPointer & farthest_vert,
|
||||||
ScalarType distance_thr = std::numeric_limits<ScalarType>::max(),
|
ScalarType distance_thr = std::numeric_limits<ScalarType>::max(),
|
||||||
typename MeshType::template PerVertexAttributeHandle<VertexPointer> * sourceSeed = NULL,
|
typename MeshType::template PerVertexAttributeHandle<VertexPointer> * sourceSeed = NULL,
|
||||||
|
typename MeshType::template PerVertexAttributeHandle<VertexPointer> * parentSeed = NULL,
|
||||||
std::vector<VertexPointer> *InInterval=NULL)
|
std::vector<VertexPointer> *InInterval=NULL)
|
||||||
{
|
{
|
||||||
typename std::vector<VertexPointer>::iterator fi;
|
typename std::vector<VertexPointer>::iterator fi;
|
||||||
std::vector<VertDist> vdSeedVec;
|
std::vector<VertDist> vdSeedVec;
|
||||||
if(seedVec.empty()) return false;
|
if(seedVec.empty()) return false;
|
||||||
for( fi = seedVec.begin(); fi != seedVec.end() ; ++fi)
|
for( fi = seedVec.begin(); fi != seedVec.end() ; ++fi)
|
||||||
{
|
|
||||||
vdSeedVec.push_back(VertDist(*fi,0.0));
|
vdSeedVec.push_back(VertDist(*fi,0.0));
|
||||||
/* if (InInterval==NULL)continue;
|
farthest_vert = Visit(m, vdSeedVec, false, distance_thr, sourceSeed, parentSeed, InInterval);
|
||||||
InInterval.push_back();*/
|
|
||||||
}
|
|
||||||
farthest_vert = Visit(m, vdSeedVec, false, distance_thr, sourceSeed, InInterval);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
Given a mesh and a pointers to a vertex-source (source), assigns the approximated geodesic
|
Given a mesh and a pointers to a vertex-source (source), assigns the approximated geodesic
|
||||||
distance from the vertex-source to all the mesh vertices and returns the pointer to the farthest
|
distance from the vertex-source to all the mesh vertices and returns the pointer to the farthest
|
||||||
Note: update the field Q() of the vertices
|
Note: it updates the field Q() of the vertices
|
||||||
*/
|
*/
|
||||||
static bool FarthestVertex( MeshType & m,
|
static bool FarthestVertex( MeshType & m, VertexPointer seed, ScalarType distance_thr = std::numeric_limits<ScalarType>::max())
|
||||||
VertexPointer seed,
|
|
||||||
ScalarType distance_thr = std::numeric_limits<ScalarType>::max())
|
|
||||||
{
|
{
|
||||||
std::vector<VertexPointer> seedVec;
|
std::vector<VertexPointer> seedVec(1,seed);
|
||||||
seedVec.push_back( seed );
|
|
||||||
VertexPointer v0;
|
VertexPointer v0;
|
||||||
return FarthestVertex(m,seedVec,v0,distance_thr);
|
return FarthestVertex(m,seedVec,v0,distance_thr);
|
||||||
}
|
}
|
||||||
|
@ -337,13 +326,11 @@ namespace vcg{
|
||||||
VertexPointer & farthest,
|
VertexPointer & farthest,
|
||||||
ScalarType & distance,
|
ScalarType & distance,
|
||||||
typename MeshType::template PerVertexAttributeHandle<VertexPointer> * sources = NULL
|
typename MeshType::template PerVertexAttributeHandle<VertexPointer> * sources = NULL
|
||||||
){
|
)
|
||||||
|
{
|
||||||
typename std::vector<VertexPointer>::iterator fi;
|
|
||||||
std::vector<VertDist>fr;
|
std::vector<VertDist>fr;
|
||||||
|
for(typename std::vector<VertexPointer>::iterator fi = seedVec.begin(); fi != seedVec.end() ; ++fi)
|
||||||
for( fi = seedVec.begin(); fi != seedVec.end() ; ++fi)
|
fr.push_back(VertDist(*fi,0));
|
||||||
fr.push_back(VertDist(*fi,-1));
|
|
||||||
farthest = Visit(m,fr,distance,true,sources);
|
farthest = Visit(m,fr,distance,true,sources);
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
|
@ -354,9 +341,9 @@ namespace vcg{
|
||||||
VertexPointer seed,
|
VertexPointer seed,
|
||||||
VertexPointer & farthest,
|
VertexPointer & farthest,
|
||||||
ScalarType & distance,
|
ScalarType & distance,
|
||||||
typename MeshType::template PerVertexAttributeHandle<VertexPointer> * sources = NULL){
|
typename MeshType::template PerVertexAttributeHandle<VertexPointer> * sources = NULL)
|
||||||
std::vector<VertexPointer> fro;
|
{
|
||||||
fro.push_back( seed );
|
std::vector<VertexPointer> fro(1,seed);
|
||||||
VertexPointer v0;
|
VertexPointer v0;
|
||||||
FarthestBVertex(m,fro,v0,distance,sources);
|
FarthestBVertex(m,fro,v0,distance,sources);
|
||||||
farthest = v0;
|
farthest = v0;
|
||||||
|
@ -365,9 +352,9 @@ namespace vcg{
|
||||||
/*
|
/*
|
||||||
Assigns to each vertex of the mesh its distance to the closest vertex on the border
|
Assigns to each vertex of the mesh its distance to the closest vertex on the border
|
||||||
Note: update the field Q() of the vertices
|
Note: update the field Q() of the vertices
|
||||||
|
Note: it needs the border bit set.
|
||||||
*/
|
*/
|
||||||
static bool DistanceFromBorder( MeshType & m,
|
static bool DistanceFromBorder( MeshType & m, typename MeshType::template PerVertexAttributeHandle<VertexPointer> * sources = NULL
|
||||||
typename MeshType::template PerVertexAttributeHandle<VertexPointer> * sources = NULL
|
|
||||||
){
|
){
|
||||||
std::vector<VertexPointer> fro;
|
std::vector<VertexPointer> fro;
|
||||||
VertexIterator vi;
|
VertexIterator vi;
|
||||||
|
@ -382,7 +369,7 @@ namespace vcg{
|
||||||
return FarthestVertex(m,fro,farthest,std::numeric_limits<ScalarType>::max(),sources);
|
return FarthestVertex(m,fro,farthest,std::numeric_limits<ScalarType>::max(),sources);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};// end class
|
||||||
};// end namespace tri
|
}// end namespace tri
|
||||||
};// end namespace vcg
|
}// end namespace vcg
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue