Optimized version of poisson sampling

This commit is contained in:
Paolo Cignoni 2009-11-30 15:53:23 +00:00
parent 27c319a79c
commit dc82ece647
1 changed files with 52 additions and 57 deletions

View File

@ -743,11 +743,11 @@ static CoordType RandomBox(vcg::Box3<ScalarType> box)
// generate Poisson-disk sample using a set of pre-generated samples (with the Montecarlo algorithm) // generate Poisson-disk sample using a set of pre-generated samples (with the Montecarlo algorithm)
// It always return a point. // It always return a point.
static VertexPointer getPrecomputedMontecarloSample(Point3i *cell, MontecarloSHT & samplepool) static VertexPointer getPrecomputedMontecarloSample(Point3i &cell, MontecarloSHT & samplepool)
{ {
MontecarloSHTIterator cellBegin; MontecarloSHTIterator cellBegin;
MontecarloSHTIterator cellEnd; MontecarloSHTIterator cellEnd;
samplepool.Grid(*cell, cellBegin, cellEnd); samplepool.Grid(cell, cellBegin, cellEnd);
return *cellBegin; return *cellBegin;
} }
@ -827,25 +827,31 @@ static void ComputePoissonSampleRadii(MetroMesh &sampleMesh, ScalarType diskRadi
} }
} }
static void removeUselessSample(MontecarloSHT &montecarloSHT, VertexType *vp, ScalarType radius) static int removeUselessSample(MontecarloSHT &montecarloSHT, VertexType *vp, ScalarType radius)
{
return montecarloSHT.RemoveInSphere(vp->cP(),radius);
}
static int oldremoveUselessSample(MontecarloSHT &montecarloSHT, VertexType *vp, ScalarType radius)
{ {
// get the samples closest to the given one // get the samples closest to the given one
std::vector<VertexType*> closests; std::vector<VertexType*> closests;
typedef VertTmark<MetroMesh> MarkerVert; typedef VertTmark<MetroMesh> MarkerVert;
MarkerVert mv; MarkerVert mv;
int cnt=0;
Box3f bb(vp->cP()-Point3f(radius,radius,radius),vp->cP()+Point3f(radius,radius,radius)); Box3f bb(vp->cP()-Point3f(radius,radius,radius),vp->cP()+Point3f(radius,radius,radius));
int nsamples = GridGetInBox(montecarloSHT, mv, bb, closests); int nsamples = GridGetInBox(montecarloSHT, mv, bb, closests);
ScalarType r2 = radius*radius; ScalarType r2 = radius*radius;
for(int i=0; i<closests.size(); ++i) for(int i=0; i<closests.size(); ++i)
{ {
if(SquaredDistance(vp->cP(),closests[i]->cP()) <= r2) if(SquaredDistance(vp->cP(),closests[i]->cP()) <= r2)
{ {
montecarloSHT.Remove(closests[i]); montecarloSHT.Remove(closests[i]);
cnt++;
} }
} }
return cnt;
} }
/** Compute a Poisson-disk sampling of the surface. /** Compute a Poisson-disk sampling of the surface.
@ -887,7 +893,7 @@ static void Poissondisk(MetroMesh &origMesh, VertexSampler &ps, MetroMesh &monte
// initialize spatial hash table for searching // initialize spatial hash table for searching
// radius is the radius of empty disk centered over the samples (e.g. twice of the empty space disk) // radius is the radius of empty disk centered over the samples (e.g. twice of the empty space disk)
// This radius implies that when we pick a sample in a cell all that cell will not be touched again. // This radius implies that when we pick a sample in a cell all that cell will not be touched again.
ScalarType cellsize = diskRadius / sqrt(3.0); ScalarType cellsize = 2.0f* diskRadius / sqrt(3.0);
// inflating // inflating
origMesh.bbox.Offset(cellsize); origMesh.bbox.Offset(cellsize);
@ -898,19 +904,19 @@ static void Poissondisk(MetroMesh &origMesh, VertexSampler &ps, MetroMesh &monte
Point3i gridsize(sizeX, sizeY, sizeZ); Point3i gridsize(sizeX, sizeY, sizeZ);
#ifdef QT_VERSION #ifdef QT_VERSION
qDebug("PDS: radius %f Grid:(%i %i %i) ",diskRadius,sizeX,sizeY,sizeZ); qDebug("PDS: radius %f Grid:(%i %i %i) ",diskRadius,sizeX,sizeY,sizeZ);
int t0=clock(); QTime tt; tt.start();
#endif #endif
// initialize spatial hash to index pre-generated samples // initialize spatial hash to index pre-generated samples
VertexIterator vi;
montecarloSHT.InitEmpty(origMesh.bbox, gridsize); montecarloSHT.InitEmpty(origMesh.bbox, gridsize);
for (vi = montecarloMesh.vert.begin(); vi != montecarloMesh.vert.end(); vi++) for (VertexIterator vi = montecarloMesh.vert.begin(); vi != montecarloMesh.vert.end(); vi++)
{ {
montecarloSHT.Add(&(*vi)); montecarloSHT.Add(&(*vi));
verticescounter[0]++; verticescounter[0]++;
} }
#ifdef QT_VERSION #ifdef QT_VERSION
int t1=clock(); int t1=clock();
qDebug("PDS: Completed montercarloSHT, inserted %i vertex in %i cells (%i)", montecarloMesh.vn, montecarloSHT.AllocatedCells.size(), t1-t0); qDebug("PDS: Completed montercarloSHT, inserted %i vertex in %i cells (%i)", montecarloMesh.vn, montecarloSHT.AllocatedCells.size(), tt.restart());
#endif #endif
// initialize spatial hash table for check poisson-disk radius constrain // initialize spatial hash table for check poisson-disk radius constrain
checkSHT.InitEmpty(origMesh.bbox, gridsize); checkSHT.InitEmpty(origMesh.bbox, gridsize);
@ -925,11 +931,6 @@ static void Poissondisk(MetroMesh &origMesh, VertexSampler &ps, MetroMesh &monte
// - if the sample violates the radius constrain discard it, and add the cell to the cells-to-subdivide list // - if the sample violates the radius constrain discard it, and add the cell to the cells-to-subdivide list
// - iterate until the active cell list is empty or a pre-defined number of subdivisions is reached // - iterate until the active cell list is empty or a pre-defined number of subdivisions is reached
// //
std::vector<Point3i *> activeCells;
std::vector<VertexType *> nextPoints;
typename std::vector<VertexType *>::iterator nextPointsIt;
typename std::vector<Point3i>::iterator it; typename std::vector<Point3i>::iterator it;
vcg::Box3<ScalarType> currentBox; vcg::Box3<ScalarType> currentBox;
@ -944,43 +945,36 @@ static void Poissondisk(MetroMesh &origMesh, VertexSampler &ps, MetroMesh &monte
// extract a cell (C) from the active cell list (with probability proportional to cell's volume) // extract a cell (C) from the active cell list (with probability proportional to cell's volume)
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
supportMesh.vert.reserve(montecarloMesh.vn); //supportMesh.vert.reserve(montecarloMesh.vn);
// create active cell list // create active cell list
for (it = montecarloSHT.AllocatedCells.begin(); it != montecarloSHT.AllocatedCells.end(); it++) montecarloSHT.UpdateAllocatedCells();
{
activeCells.push_back(&(*it));
}
int ncell = static_cast<int>(activeCells.size()); int ncell = static_cast<int>(montecarloSHT.AllocatedCells.size());
cellusedcounter[level] = ncell; cellusedcounter[level] = ncell;
// shuffle active cells // shuffle active cells
unsigned int (*p_myrandom)(unsigned int) = RandomInt; unsigned int (*p_myrandom)(unsigned int) = RandomInt;
std::random_shuffle(activeCells.begin(),activeCells.end(), p_myrandom); std::random_shuffle(montecarloSHT.AllocatedCells.begin(),montecarloSHT.AllocatedCells.end(), p_myrandom);
#ifdef QT_VERSION #ifdef QT_VERSION
int t2=clock(); qDebug("PDS: Completed creation of activeCells, %i cells (%i msec)", montecarloSHT.AllocatedCells.size(), tt.restart());
qDebug("PDS: Completed creation of activeCells, %i cells (%i)", montecarloSHT.AllocatedCells.size(), t2-t1);
#endif #endif
// generate a sample inside C by choosing one of the contained pre-generated samples // generate a sample inside C by choosing one of the contained pre-generated samples
////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////
int removedCnt=0;
for (int i = 0; i < ncell; i++) for (int i = 0; i < ncell; i++)
{ {
Point3i *currentCell = activeCells[i]; if( montecarloSHT.EmptyCell(montecarloSHT.AllocatedCells[i]) ) continue;
//vcg::Point3<ScalarType > s; // current sample
if(montecarloSHT.CellSize(*currentCell) == 0 ) continue;
// generate a sample chosen from the pre-generated one // generate a sample chosen from the pre-generated one
VertexPointer sp = getPrecomputedMontecarloSample(currentCell, montecarloSHT); VertexPointer sp = getPrecomputedMontecarloSample(montecarloSHT.AllocatedCells[i], montecarloSHT);
samplesgenerated[level]++; samplesgenerated[level]++;
// vr spans between 3.0*r and r / 4.0 according to vertex quality // vr spans between 3.0*r and r / 4.0 according to vertex quality
ScalarType sampleRadius = diskRadius; ScalarType sampleRadius = diskRadius;
if(pp.adaptiveRadiusFlag) sampleRadius = sp->Q(); if(pp.adaptiveRadiusFlag) sampleRadius = sp->Q();
//if (checkPoissonDisk(*ps.m, checkSHT, sp->cP(), sampleRadius)) //if (checkPoissonDisk(*ps.m, checkSHT, sp->cP(), sampleRadius))
{ {
// add sample // add sample
@ -990,10 +984,10 @@ static void Poissondisk(MetroMesh &origMesh, VertexSampler &ps, MetroMesh &monte
//ps.AddVert(supportMesh.vert.back()); Small change, we should call the sampler class with the input mesh. //ps.AddVert(supportMesh.vert.back()); Small change, we should call the sampler class with the input mesh.
ps.AddVert(*sp); ps.AddVert(*sp);
removeUselessSample(montecarloSHT,sp,sampleRadius); removedCnt += removeUselessSample(montecarloSHT,sp,sampleRadius);
// add to control spatial index // add to control spatial index
checkSHT.Add(&supportMesh.vert.back()); //checkSHT.Add(&supportMesh.vert.back());
samplesaccepted[level]++; samplesaccepted[level]++;
} }
@ -1014,30 +1008,31 @@ static void Poissondisk(MetroMesh &origMesh, VertexSampler &ps, MetroMesh &monte
// } // }
} }
activeCells.clear(); #ifdef QT_VERSION
qDebug("Removed %i samples",removedCnt);
#endif
montecarloSHT.UpdateAllocatedCells();
// proceed to the next level of subdivision // proceed to the next level of subdivision
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// cleaning spatial index data structures // cleaning spatial index data structures
montecarloSHT.Clear(); //montecarloSHT.Clear();
// increase grid resolution // increase grid resolution
gridsize[0] *= 2; //gridsize *= 2;
gridsize[1] *= 2;
gridsize[2] *= 2;
montecarloSHT.InitEmpty(origMesh.bbox, gridsize); //montecarloSHT.InitEmpty(origMesh.bbox, gridsize);
for (nextPointsIt = nextPoints.begin(); nextPointsIt != nextPoints.end(); nextPointsIt++) // for (nextPointsIt = nextPoints.begin(); nextPointsIt != nextPoints.end(); nextPointsIt++)
{ // {
montecarloSHT.Add(*nextPointsIt); // montecarloSHT.Add(*nextPointsIt);
verticescounter[level+1]++; // verticescounter[level+1]++;
} // }
//
nextPoints.clear(); // nextPoints.clear();
#ifdef QT_VERSION #ifdef QT_VERSION
qDebug("PDS: Completed Level %i, added %i samples",level,samplesaccepted[level]); qDebug("PDS: Completed Level %i, added %i samples (%i msec)",level,samplesaccepted[level],tt.elapsed());
#endif #endif
level++; level++;