From ca47599bd543903e0093fda042832d5ce10e3e71 Mon Sep 17 00:00:00 2001 From: cignoni Date: Thu, 29 May 2008 06:17:09 +0000 Subject: [PATCH] Added weighted area sampling --- vcg/complex/trimesh/point_sampling.h | 38 ++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/vcg/complex/trimesh/point_sampling.h b/vcg/complex/trimesh/point_sampling.h index e7ec8487..62129ee1 100644 --- a/vcg/complex/trimesh/point_sampling.h +++ b/vcg/complex/trimesh/point_sampling.h @@ -180,6 +180,44 @@ static void Montecarlo(MetroMesh & m, VertexSampler &ps,int sampleNum) } } +static ScalarType WeightedArea(FaceType f) +{ + ScalarType averageQ = ( f.V(0)->Q() + f.V(1)->Q() + f.V(2)->Q() ) /3.0; + return DoubleArea(f)*averageQ/2.0; +} + +/// Compute a sampling of the surface that is weighted by the quality +/// the area of each face is multiplied by the average of the quality of the vertices. +/// So the a face with a zero quality on all its vertices is never sampled and a face with average quality 2 get twice the samples of a face with the same area but with an average quality of 1; +static void WeightedMontecarlo(MetroMesh & m, VertexSampler &ps, int sampleNum) +{ + assert(tri::HasPerVertexQuality(m)); + + ScalarType weightedArea = 0; + FaceIterator fi; + for(fi = m.face.begin(); fi != m.face.end(); ++fi) + if(!(*fi).IsD()) + weightedArea += WeightedArea(*fi); + + ScalarType samplePerAreaUnit = sampleNum/weightedArea; + qDebug("samplePerAreaUnit %f",samplePerAreaUnit); + // Montecarlo sampling. + double floatSampleNum = 0.0; + for(fi=m.face.begin(); fi != m.face.end(); fi++) + if(!(*fi).IsD()) + { + // compute # samples in the current face (taking into account of the remainders) + floatSampleNum += WeightedArea(*fi) * samplePerAreaUnit; + int faceSampleNum = (int) floatSampleNum; + + // for every sample p_i in T... + for(int i=0; i < faceSampleNum; i++) + ps.AddFace(*fi,RandomBaricentric()); + + floatSampleNum -= (double) faceSampleNum; + } +} + }; } // end namespace tri