fixed uniform resampling of edge mesh

This commit is contained in:
Luigi Malomo 2015-11-05 23:35:44 +00:00
parent 0f05ee423d
commit 62b7955f22
1 changed files with 76 additions and 55 deletions

View File

@ -645,63 +645,84 @@ static void EdgeMeshUniform(MeshType &m, VertexSampler &ps, float radius)
{ {
tri::RequireEEAdjacency(m); tri::RequireEEAdjacency(m);
tri::RequireCompactness(m); tri::RequireCompactness(m);
tri::RequirePerEdgeFlags(m);
tri::RequirePerVertexFlags(m);
tri::UpdateTopology<MeshType>::EdgeEdge(m); tri::UpdateTopology<MeshType>::EdgeEdge(m);
tri::UpdateFlags<MeshType>::EdgeClearV(m); tri::UpdateFlags<MeshType>::EdgeClearV(m);
for(EdgeIterator ei = m.edge.begin();ei!=m.edge.end();++ei) for (EdgeIterator ei = m.edge.begin(); ei != m.edge.end(); ++ei)
{ {
if(!ei->IsV()) if (!ei->IsV())
{ {
edge::Pos<EdgeType> ep(&*ei,0); edge::Pos<EdgeType> ep(&*ei,0);
edge::Pos<EdgeType> startep =ep; edge::Pos<EdgeType> startep = ep;
VertexPointer startVertex=0; VertexPointer startVertex = 0;
do // first loop to search a boundary component. do // first loop to search a boundary component.
{ {
ep.NextE(); ep.NextE();
if(ep.IsBorder()) break; if (ep.IsBorder())
} while(startep!=ep); break;
if(!ep.IsBorder()) } while (startep != ep);
startVertex=ep.V(); if (!ep.IsBorder())
{
// it's a loop
startVertex = ep.V();
}
else
{
// to keep the uniform resampling order-independent
// start from the border with 'lowest' point
edge::Pos<EdgeType> altEp = ep;
do {
altEp.NextE();
} while (!altEp.IsBorder());
ScalarType totalLen=0; if (altEp.V()->cP() < ep.V()->cP())
{
ep = altEp;
}
}
ScalarType totalLen = 0;
ep.FlipV(); ep.FlipV();
do // second loop to compute len of the chain. // second loop to compute length of the chain.
do
{ {
ep.E()->SetV(); ep.E()->SetV();
totalLen+=Distance(ep.V()->P(),ep.VFlip()->P()); totalLen += Distance(ep.V()->cP(), ep.VFlip()->cP());
ep.NextE(); ep.NextE();
} while(!ep.IsBorder() && ep.V()!=startVertex); } while (!ep.IsBorder() && ep.V() != startVertex);
ep.E()->SetV(); ep.E()->SetV();
totalLen+=Distance(ep.V()->P(),ep.VFlip()->P()); totalLen += Distance(ep.V()->cP(), ep.VFlip()->cP());
// Third loop actually perform the sampling. // Third loop actually perform the sampling.
int sampleNum = floor(totalLen / radius); int sampleNum = floor(totalLen / radius);
ScalarType sampleDist = totalLen/sampleNum; ScalarType sampleDist = totalLen / sampleNum;
// printf("Found a chain of %f with %i samples every %f (%f)\n",totalLen,sampleNum,sampleDist,radius); // printf("Found a chain of %f with %i samples every %f (%f)\n", totalLen, sampleNum, sampleDist, radius);
ScalarType curLen=0; ScalarType curLen = 0;
int sampleCnt=1; int sampleCnt = 1;
ps.AddEdge(*(ep.E()),ep.VInd()==0?0.0:1.0); ps.AddEdge(*(ep.E()), ep.VInd() == 0 ? 0.0 : 1.0);
ep.FlipV();
do do
{ {
ScalarType edgeLen = Distance(ep.V()->P(),ep.VFlip()->P()); ep.NextE();
assert(ep.E()->IsV());
ScalarType edgeLen = Distance(ep.V()->cP(), ep.VFlip()->cP());
ScalarType d0 = curLen; ScalarType d0 = curLen;
ScalarType d1 = d0+edgeLen; ScalarType d1 = d0 + edgeLen;
while(d1>sampleCnt*sampleDist) while (d1 > sampleCnt * sampleDist && sampleCnt < sampleNum)
{ {
ScalarType off = (sampleCnt*sampleDist-d0) /edgeLen; ScalarType off = (sampleCnt * sampleDist - d0) / edgeLen;
// printf("edgeLen %f off %f samplecnt %i\n",edgeLen,off,sampleCnt); // printf("edgeLen %f off %f samplecnt %i\n", edgeLen, off, sampleCnt);
ps.AddEdge(*(ep.E()),ep.VInd()==0?1.0-off:off); ps.AddEdge(*(ep.E()), ep.VInd() == 0 ? 1.0 - off : off);
sampleCnt++; sampleCnt++;
} }
curLen+=edgeLen; curLen += edgeLen;
ep.NextE(); } while(!ep.IsBorder() && ep.V() != startVertex);
} while(!ep.IsBorder() && ep.V()!=startVertex);
if(ep.V()!=startVertex) if(ep.V() != startVertex)
ps.AddEdge(*(ep.E()),ep.VInd()==0?0.0:1.0); ps.AddEdge(*(ep.E()), ep.VInd() == 0 ? 0.0 : 1.0);
} }
} }
} }