VCG Library
Loading...
Searching...
No Matches
point_sampling.h
1/****************************************************************************
2* VCGLib o o *
3* Visual and Computer Graphics Library o o *
4* _ O _ *
5* Copyright(C) 2004-2016 \/)\/ *
6* Visual Computing Lab /\/| *
7* ISTI - Italian National Research Council | *
8* \ *
9* All rights reserved. *
10* *
11* This program is free software; you can redistribute it and/or modify *
12* it under the terms of the GNU General Public License as published by *
13* the Free Software Foundation; either version 2 of the License, or *
14* (at your option) any later version. *
15* *
16* This program is distributed in the hope that it will be useful, *
17* but WITHOUT ANY WARRANTY; without even the implied warranty of *
18* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20* for more details. *
21* *
22****************************************************************************/
23/****************************************************************************
24
25The sampling Class has a set of static functions, that you can call to sample the surface of a mesh.
26Each function is templated on the mesh and on a Sampler object s.
27Each function calls many time the sample object with the sampling point as parameter.
28
29Sampler Classes and Sampling algorithms are independent.
30Sampler classes exploits the sample that are generated with various algorithms.
31For example, you can compute Hausdorff distance (that is a sampler) using various
32sampling strategies (montecarlo, stratified etc).
33
34****************************************************************************/
35#ifndef __VCGLIB_POINT_SAMPLING
36#define __VCGLIB_POINT_SAMPLING
37
38#include <random>
39
40#include <vcg/math/random_generator.h>
41#include <vcg/complex/algorithms/closest.h>
42#include <vcg/space/index/spatial_hashing.h>
43#include <vcg/complex/algorithms/hole.h>
44#include <vcg/complex/algorithms/stat.h>
45#include <vcg/complex/algorithms/create/platonic.h>
46#include <vcg/complex/algorithms/update/normal.h>
47#include <vcg/complex/algorithms/update/bounding.h>
48#include <vcg/space/segment2.h>
49#include <vcg/space/index/grid_static_ptr.h>
50
51namespace vcg
52{
53namespace tri
54{
57
70template <class MeshType>
72{
73public:
74 typedef typename MeshType::ScalarType ScalarType;
75 typedef typename MeshType::CoordType CoordType;
76 typedef typename MeshType::VertexType VertexType;
77 typedef typename MeshType::EdgeType EdgeType;
78 typedef typename MeshType::FaceType FaceType;
79
80 void reset()
81 {
82 sampleVec->clear();
83 }
84
86 {
87 sampleVec = new std::vector<CoordType>();
88 vectorOwner=true;
89 }
90
91 TrivialSampler(std::vector<CoordType> &Vec)
92 {
93 sampleVec = &Vec;
94 vectorOwner=false;
95 reset();
96 }
97
99 {
100 if(vectorOwner) delete sampleVec;
101 }
102
103private:
104 std::vector<CoordType> *sampleVec;
105 bool vectorOwner;
106public:
107
108 std::vector<CoordType> &SampleVec()
109 {
110 return *sampleVec;
111 }
112
113 void AddVert(const VertexType &p)
114 {
115 sampleVec->push_back(p.cP());
116 }
117 void AddEdge(const EdgeType& e, ScalarType u ) // u==0 -> v(0) u==1 -> v(1);
118 {
119 sampleVec->push_back(e.cV(0)->cP()*(1.0-u)+e.cV(1)->cP()*u);
120 }
121
122 void AddFace(const FaceType &f, const CoordType &p)
123 {
124 sampleVec->push_back(f.cP(0)*p[0] + f.cP(1)*p[1] +f.cP(2)*p[2] );
125 }
126
127 void AddTextureSample(const FaceType &, const CoordType &, const Point2i &, float )
128 {
129 // Retrieve the color of the sample from the face f using the barycentric coord p
130 // and write that color in a texture image at position <tp[0], texHeight-tp[1]>
131 // if edgeDist is > 0 then the corrisponding point is affecting face color even if outside the face area (in texture space)
132 }
133}; // end class TrivialSampler
134
135template <class MeshType>
137{
138public:
139 typedef typename MeshType::ScalarType ScalarType;
140 typedef typename MeshType::CoordType CoordType;
141 typedef typename MeshType::VertexType VertexType;
142 typedef typename MeshType::EdgeType EdgeType;
143 typedef typename MeshType::FaceType FaceType;
144
147
148 void reset()
149 {
150 sampleVec.clear();
151 }
152
153public:
154 std::vector<VertexType *> sampleVec;
155
156 void AddVert(VertexType &p)
157 {
158 sampleVec.push_back(&p);
159 }
160
161 void AddEdge(const EdgeType& e, ScalarType u ) // u==0 -> v(0) u==1 -> v(1);
162 {
163 if( u < 0.5 )
164 sampleVec.push_back(e.cV(0));
165 else
166 sampleVec.push_back(e.cV(1));
167 }
168
169 // This sampler should be used only for getting vertex pointers. Meaningless in other case.
170 void AddFace(const FaceType &, const CoordType &) { assert(0); }
171 void AddTextureSample(const FaceType &, const CoordType &, const Point2i &, float ) { assert(0); }
172}; // end class TrivialSampler
173
174
175template <class MeshType>
177{
178public:
179 typedef typename MeshType::VertexType VertexType;
180 typedef typename MeshType::FaceType FaceType;
181 typedef typename MeshType::EdgeType EdgeType;
182 typedef typename MeshType::CoordType CoordType;
183 typedef typename MeshType::ScalarType ScalarType;
184
185 MeshSampler(MeshType &_m):m(_m){
186 perFaceNormal = false;
187 }
188 MeshType &m;
189
190 bool perFaceNormal; // default false; if true the sample normal is the face normal, otherwise it is interpolated
191
192 void reset()
193 {
194 m.Clear();
195 }
196
197 void AddVert(const VertexType &p)
198 {
200 m.vert.back().ImportData(p);
201 }
202
203 void AddEdge(const EdgeType& e, ScalarType u ) // u==0 -> v(0) u==1 -> v(1);
204 {
206 m.vert.back().P() = e.cV(0)->cP()*(1.0-u)+e.cV(1)->cP()*u;
207 m.vert.back().N() = e.cV(0)->cN()*(1.0-u)+e.cV(1)->cN()*u;
208 }
209
210 void AddFace(const FaceType &f, CoordType p)
211 {
213 m.vert.back().P() = f.cP(0)*p[0] + f.cP(1)*p[1] +f.cP(2)*p[2];
214 if(perFaceNormal) m.vert.back().N() = f.cN();
215 else m.vert.back().N() = f.cV(0)->N()*p[0] + f.cV(1)->N()*p[1] + f.cV(2)->N()*p[2];
216 if(tri::HasPerVertexQuality(m) )
217 m.vert.back().Q() = f.cV(0)->Q()*p[0] + f.cV(1)->Q()*p[1] + f.cV(2)->Q()*p[2];
218 }
219}; // end class MeshSampler
220
221
222
223/* This sampler is used to perform compute the Hausdorff measuring.
224 * It keep internally the spatial indexing structure used to find the closest point
225 * and the partial integration results needed to compute the average and rms error values.
226 * Averaged values assume that the samples are equi-distributed (e.g. a good unbiased montecarlo sampling of the surface).
227 */
228template <class MeshType>
230{
231 typedef typename MeshType::FaceType FaceType;
232 typedef typename MeshType::VertexType VertexType;
233 typedef typename MeshType::CoordType CoordType;
234 typedef typename MeshType::ScalarType ScalarType;
235 typedef GridStaticPtr<FaceType, ScalarType > MetroMeshFaceGrid;
236 typedef GridStaticPtr<VertexType, ScalarType > MetroMeshVertexGrid;
237
238public:
239
240 HausdorffSampler(MeshType* _m, MeshType* _sampleMesh=0, MeshType* _closestMesh=0 ) :markerFunctor(_m)
241 {
242 m=_m;
243 init(_sampleMesh,_closestMesh);
244 }
245
246 MeshType *m;
247 MeshType *samplePtMesh;
248 MeshType *closestPtMesh;
249
250 MetroMeshVertexGrid unifGridVert;
251 MetroMeshFaceGrid unifGridFace;
252
253 // Parameters
254 double min_dist;
255 double max_dist;
256 double mean_dist;
257 double RMS_dist;
258 double volume;
259 double area_S1;
260 Histogramf hist;
261 // globals parameters driving the samples.
262 int n_total_samples;
263 int n_samples;
264 bool useVertexSampling;
265 ScalarType dist_upper_bound; // samples that have a distance beyond this threshold distance are not considered.
266 typedef typename tri::FaceTmark<MeshType> MarkerFace;
267 MarkerFace markerFunctor;
268
269
270 float getMeanDist() const { return mean_dist / n_total_samples; }
271 float getMinDist() const { return min_dist ; }
272 float getMaxDist() const { return max_dist ; }
273 float getRMSDist() const { return sqrt(RMS_dist / n_total_samples); }
274
275 void init(MeshType* _sampleMesh=0, MeshType* _closestMesh=0 )
276 {
277 samplePtMesh =_sampleMesh;
278 closestPtMesh = _closestMesh;
279 if(m)
280 {
282 if(m->fn==0) useVertexSampling = true;
283 else useVertexSampling = false;
284
285 if(useVertexSampling) unifGridVert.Set(m->vert.begin(),m->vert.end());
286 else unifGridFace.Set(m->face.begin(),m->face.end());
287 markerFunctor.SetMesh(m);
288 hist.SetRange(0.0, m->bbox.Diag()/100.0, 100);
289 }
290 min_dist = std::numeric_limits<double>::max();
291 max_dist = 0;
292 mean_dist =0;
293 RMS_dist = 0;
294 n_total_samples = 0;
295 }
296
297 void AddFace(const FaceType &f, CoordType interp)
298 {
299 CoordType startPt = f.cP(0)*interp[0] + f.cP(1)*interp[1] +f.cP(2)*interp[2]; // point to be sampled
300 CoordType startN = f.cV(0)->cN()*interp[0] + f.cV(1)->cN()*interp[1] +f.cV(2)->cN()*interp[2]; // Normal of the interpolated point
301 AddSample(startPt,startN); // point to be sampled);
302 }
303
304 void AddVert(VertexType &p)
305 {
306 p.Q()=AddSample(p.cP(),p.cN());
307 }
308
309
310 float AddSample(const CoordType &startPt,const CoordType &startN)
311 {
312 // the results
313 CoordType closestPt;
314 ScalarType dist = dist_upper_bound;
315
316 // compute distance between startPt and the mesh S2
317 FaceType *nearestF=0;
318 VertexType *nearestV=0;
319 vcg::face::PointDistanceBaseFunctor<ScalarType> PDistFunct;
320 dist=dist_upper_bound;
321 if(useVertexSampling)
322 nearestV = tri::GetClosestVertex<MeshType,MetroMeshVertexGrid>(*m,unifGridVert,startPt,dist_upper_bound,dist);
323 else
324 nearestF = unifGridFace.GetClosest(PDistFunct,markerFunctor,startPt,dist_upper_bound,dist,closestPt);
325
326 // update distance measures
327 if(dist == dist_upper_bound)
328 return dist;
329
330 if(dist > max_dist) max_dist = dist; // L_inf
331 if(dist < min_dist) min_dist = dist; // L_inf
332
333 mean_dist += dist; // L_1
334 RMS_dist += dist*dist; // L_2
335 n_total_samples++;
336
337 hist.Add((float)fabs(dist));
338 if(samplePtMesh)
339 {
341 samplePtMesh->vert.back().P() = startPt;
342 samplePtMesh->vert.back().Q() = dist;
343 samplePtMesh->vert.back().N() = startN;
344 }
345 if(closestPtMesh)
346 {
348 closestPtMesh->vert.back().P() = closestPt;
349 closestPtMesh->vert.back().Q() = dist;
350 closestPtMesh->vert.back().N() = startN;
351 }
352 return dist;
353 }
354}; // end class HausdorffSampler
355
356
357
358/* This sampler is used to transfer the detail of a mesh onto another one.
359 * It keep internally the spatial indexing structure used to find the closest point
360 */
361template <class MeshType>
363{
364 typedef typename MeshType::FaceType FaceType;
365 typedef typename MeshType::VertexType VertexType;
366 typedef typename MeshType::CoordType CoordType;
367 typedef typename MeshType::ScalarType ScalarType;
368 typedef GridStaticPtr<FaceType, ScalarType > MetroMeshGrid;
369 typedef GridStaticPtr<VertexType, ScalarType > VertexMeshGrid;
370
371public:
372
373 RedetailSampler():m(0) {}
374
375 MeshType *m;
376 CallBackPos *cb;
377 int sampleNum; // the expected number of samples. Used only for the callback
378 int sampleCnt;
379 MetroMeshGrid unifGridFace;
380 VertexMeshGrid unifGridVert;
381 bool useVertexSampling;
382
383 // Parameters
384 typedef tri::FaceTmark<MeshType> MarkerFace;
385 MarkerFace markerFunctor;
386
387 bool coordFlag;
388 bool colorFlag;
389 bool normalFlag;
390 bool qualityFlag;
391 bool selectionFlag;
392 bool storeDistanceAsQualityFlag;
393 float dist_upper_bound;
394 void init(MeshType *_m, CallBackPos *_cb=0, int targetSz=0)
395 {
396 coordFlag=false;
397 colorFlag=false;
398 qualityFlag=false;
399 selectionFlag=false;
400 storeDistanceAsQualityFlag=false;
401 m=_m;
403 if(m->fn==0) useVertexSampling = true;
404 else useVertexSampling = false;
405
406 if(useVertexSampling) unifGridVert.Set(m->vert.begin(),m->vert.end());
407 else unifGridFace.Set(m->face.begin(),m->face.end());
408 markerFunctor.SetMesh(m);
409 // sampleNum and sampleCnt are used only for the progress callback.
410 cb=_cb;
411 sampleNum = targetSz;
412 sampleCnt = 0;
413 }
414
415 // this function is called for each vertex of the target mesh.
416 // and retrieve the closest point on the source mesh.
417 void AddVert(VertexType &p)
418 {
419 assert(m);
420 // the results
421 CoordType closestPt, normf, bestq, ip;
422 ScalarType dist = dist_upper_bound;
423 const CoordType &startPt= p.cP();
424 // compute distance between startPt and the mesh S2
425 if(useVertexSampling)
426 {
427 VertexType *nearestV=0;
428 nearestV = tri::GetClosestVertex<MeshType,VertexMeshGrid>(*m,unifGridVert,startPt,dist_upper_bound,dist); //(PDistFunct,markerFunctor,startPt,dist_upper_bound,dist,closestPt);
429 if(cb) cb(sampleCnt++*100/sampleNum,"Resampling Vertex attributes");
430 if(storeDistanceAsQualityFlag) p.Q() = dist;
431 if(dist == dist_upper_bound) return ;
432
433 if(coordFlag) p.P()=nearestV->P();
434 if(colorFlag) p.C() = nearestV->C();
435 if(normalFlag) p.N() = nearestV->N();
436 if(qualityFlag) p.Q()= nearestV->Q();
437 if(selectionFlag) if(nearestV->IsS()) p.SetS();
438 }
439 else
440 {
441 FaceType *nearestF=0;
442 vcg::face::PointDistanceBaseFunctor<ScalarType> PDistFunct;
443 dist=dist_upper_bound;
444 if(cb) cb(sampleCnt++*100/sampleNum,"Resampling Vertex attributes");
445 nearestF = unifGridFace.GetClosest(PDistFunct,markerFunctor,startPt,dist_upper_bound,dist,closestPt);
446 if(dist == dist_upper_bound) return ;
447
448 CoordType interp;
449 InterpolationParameters(*nearestF,(*nearestF).cN(),closestPt, interp);
450 interp[2]=1.0-interp[1]-interp[0];
451
452 if(coordFlag) p.P()=closestPt;
453 if(colorFlag) p.C().lerp(nearestF->V(0)->C(),nearestF->V(1)->C(),nearestF->V(2)->C(),interp);
454 if(normalFlag) p.N() = nearestF->V(0)->N()*interp[0] + nearestF->V(1)->N()*interp[1] + nearestF->V(2)->N()*interp[2];
455 if(qualityFlag) p.Q()= nearestF->V(0)->Q()*interp[0] + nearestF->V(1)->Q()*interp[1] + nearestF->V(2)->Q()*interp[2];
456 if(selectionFlag) if(nearestF->IsS()) p.SetS();
457 }
458 }
459}; // end class RedetailSampler
460
461
462
463
474template <class MeshType, class VertexSampler = TrivialSampler< MeshType> >
476{
477 typedef typename MeshType::CoordType CoordType;
478 typedef typename MeshType::BoxType BoxType;
479 typedef typename MeshType::ScalarType ScalarType;
480 typedef typename MeshType::VertexType VertexType;
481 typedef typename MeshType::VertexPointer VertexPointer;
482 typedef typename MeshType::VertexIterator VertexIterator;
483 typedef typename MeshType::EdgeType EdgeType;
484 typedef typename MeshType::EdgeIterator EdgeIterator;
485 typedef typename MeshType::FaceType FaceType;
486 typedef typename MeshType::FacePointer FacePointer;
487 typedef typename MeshType::FaceIterator FaceIterator;
488 typedef typename MeshType::FaceContainer FaceContainer;
489
490 typedef typename vcg::SpatialHashTable<FaceType, ScalarType> MeshSHT;
491 typedef typename vcg::SpatialHashTable<FaceType, ScalarType>::CellIterator MeshSHTIterator;
492 typedef typename vcg::SpatialHashTable<VertexType, ScalarType> MontecarloSHT;
493 typedef typename vcg::SpatialHashTable<VertexType, ScalarType>::CellIterator MontecarloSHTIterator;
494 typedef typename vcg::SpatialHashTable<VertexType, ScalarType> SampleSHT;
495 typedef typename vcg::SpatialHashTable<VertexType, ScalarType>::CellIterator SampleSHTIterator;
496
497 typedef typename MeshType::template PerVertexAttributeHandle<float> PerVertexFloatAttribute;
498
499public:
500
501static math::MarsenneTwisterRNG &SamplingRandomGenerator()
502{
503 static math::MarsenneTwisterRNG rnd;
504 return rnd;
505}
506
507// Returns an integer random number in the [0,i-1] interval using the improve Marsenne-Twister method.
508// this functor is needed for passing it to the std functions.
509static unsigned int RandomInt(unsigned int i)
510{
511 return (SamplingRandomGenerator().generate(i));
512}
513
514// A UniformRandomBitGenerator over the shared sampling generator, for std::shuffle and
515// friends.
516//
517// It used to take an upper bound and return generate(bound), i.e. values in [0,bound),
518// while still advertising the full 32 bit range through min()/max() -- a broken
519// UniformRandomBitGenerator. In practice std::shuffle survived it, because every caller
520// passed the container size and the range shuffle asks for is bounded by that same size;
521// measured over 4000 permutations of a 642 vertex mesh the result was still uniform.
522// It is fixed anyway: the guarantee a generator makes about its own range should not
523// depend on a coincidence between two call sites, and a different standard library is
524// free to compose several calls into a wider value, where the missing high bits would
525// matter. The old one-argument constructor is kept, ignoring its argument, so existing
526// callers still compile.
527//
528// Note that this changes which random numbers each caller consumes, so a given seed no
529// longer selects the same subset it did before.
531{
532public:
533 typedef unsigned int result_type;
535 explicit MarsenneTwisterURBG(result_type /*unused_bound*/) {}
536 static constexpr result_type min() {return 0;}
537 static constexpr result_type max() {return std::numeric_limits<result_type>::max();}
538 result_type operator()() {return SamplingRandomGenerator().generate();}
539};
540
541// Returns a random number in the [0,1) real interval using the improved Marsenne-Twister method.
542static double RandomDouble01()
543{
544 return SamplingRandomGenerator().generate01();
545}
546
547#define FAK_LEN 1024
548static double LnFac(int n) {
549 // Tabled log factorial function. gives natural logarithm of n!
550
551 // define constants
552 static const double // coefficients in Stirling approximation
553 C0 = 0.918938533204672722, // ln(sqrt(2*pi))
554 C1 = 1./12.,
555 C3 = -1./360.;
556 // C5 = 1./1260., // use r^5 term if FAK_LEN < 50
557 // C7 = -1./1680.; // use r^7 term if FAK_LEN < 20
558 // static variables
559 static double fac_table[FAK_LEN]; // table of ln(n!):
560 static bool initialized = false; // remember if fac_table has been initialized
561
562
563 if (n < FAK_LEN) {
564 if (n <= 1) {
565 if (n < 0) assert(0);//("Parameter negative in LnFac function");
566 return 0;
567 }
568 if (!initialized) { // first time. Must initialize table
569 // make table of ln(n!)
570 double sum = fac_table[0] = 0.;
571 for (int i=1; i<FAK_LEN; i++) {
572 sum += log(double(i));
573 fac_table[i] = sum;
574 }
575 initialized = true;
576 }
577 return fac_table[n];
578 }
579 // not found in table. use Stirling approximation
580 double n1, r;
581 n1 = n; r = 1. / n1;
582 return (n1 + 0.5)*log(n1) - n1 + C0 + r*(C1 + r*r*C3);
583}
584
585static int PoissonRatioUniforms(double L) {
586 /*
587
588 This subfunction generates a integer with the poisson
589 distribution using the ratio-of-uniforms rejection method (PRUAt).
590 This approach is STABLE even for large L (e.g. it does not suffer from the overflow limit of the classical Knuth implementation)
591 Execution time does not depend on L, except that it matters whether
592 is within the range where ln(n!) is tabulated.
593
594 Reference:
595
596 E. Stadlober
597 "The ratio of uniforms approach for generating discrete random variates".
598 Journal of Computational and Applied Mathematics,
599 vol. 31, no. 1, 1990, pp. 181-189.
600
601 Partially adapted/inspired from some subfunctions of the Agner Fog stocc library ( www.agner.org/random )
602 Same licensing scheme.
603
604 */
605 // constants
606
607 const double SHAT1 = 2.943035529371538573; // 8/e
608 const double SHAT2 = 0.8989161620588987408; // 3-sqrt(12/e)
609 double u; // uniform random
610 double lf; // ln(f(x))
611 double x; // real sample
612 int k; // integer sample
613
614 double pois_a = L + 0.5; // hat center
615 int mode = (int)L; // mode
616 double pois_g = log(L);
617 double pois_f0 = mode * pois_g - LnFac(mode); // value at mode
618 double pois_h = sqrt(SHAT1 * (L+0.5)) + SHAT2; // hat width
619 double pois_bound = (int)(pois_a + 6.0 * pois_h); // safety-bound
620
621 while(1) {
622 u = RandomDouble01();
623 if (u == 0) continue; // avoid division by 0
624 x = pois_a + pois_h * (RandomDouble01() - 0.5) / u;
625 if (x < 0 || x >= pois_bound) continue; // reject if outside valid range
626 k = (int)(x);
627 lf = k * pois_g - LnFac(k) - pois_f0;
628 if (lf >= u * (4.0 - u) - 3.0) break; // quick acceptance
629 if (u * (u - lf) > 1.0) continue; // quick rejection
630 if (2.0 * log(u) <= lf) break; // final acceptance
631 }
632 return k;
633}
634
635
647static int Poisson(double lambda)
648{
649 if(lambda>50) return PoissonRatioUniforms(lambda);
650 double L = exp(-lambda);
651 int k =0;
652 double p = 1.0;
653 do
654 {
655 k = k+1;
656 p = p*RandomDouble01();
657 } while (p>L);
658
659 return k -1;
660}
661
662
663static void AllVertex(MeshType & m, VertexSampler &ps)
664{
665 AllVertex(m, ps, false);
666}
667
668static void AllVertex(MeshType & m, VertexSampler &ps, bool onlySelected)
669{
670 VertexIterator vi;
671 for(vi=m.vert.begin();vi!=m.vert.end();++vi)
672 if(!(*vi).IsD())
673 if ((!onlySelected) || ((*vi).IsS()))
674 {
675 ps.AddVert(*vi);
676 }
677}
678
686
687static void VertexWeighted(MeshType & m, VertexSampler &ps, int sampleNum)
688{
689 ScalarType qSum = 0;
690 VertexIterator vi;
691 for(vi = m.vert.begin(); vi != m.vert.end(); ++vi)
692 if(!(*vi).IsD())
693 qSum += (*vi).Q();
694
695 ScalarType samplePerUnit = sampleNum/qSum;
696 ScalarType floatSampleNum =0;
697 std::vector<VertexPointer> vertVec;
698 FillAndShuffleVertexPointerVector(m,vertVec);
699
700 std::vector<bool> vertUsed(m.vn,false);
701
702 int i=0; int cnt=0;
703 while(cnt < sampleNum)
704 {
705 if(vertUsed[i])
706 {
707 floatSampleNum += vertVec[i]->Q() * samplePerUnit;
708 int vertSampleNum = (int) floatSampleNum;
709 floatSampleNum -= (float) vertSampleNum;
710
711 // for every sample p_i in T...
712 if(vertSampleNum > 1)
713 {
714 ps.AddVert(*vertVec[i]);
715 cnt++;
716 vertUsed[i]=true;
717 }
718 }
719 i = (i+1)%m.vn;
720 }
721}
722
725static void VertexAreaUniform(MeshType & m, VertexSampler &ps, int sampleNum)
726{
727 VertexIterator vi;
728 for(vi = m.vert.begin(); vi != m.vert.end(); ++vi)
729 if(!(*vi).IsD())
730 (*vi).Q() = 0;
731
732 FaceIterator fi;
733 for(fi = m.face.begin(); fi != m.face.end(); ++fi)
734 if(!(*fi).IsD())
735 {
736 ScalarType areaThird = DoubleArea(*fi)/6.0;
737 (*fi).V(0)->Q()+=areaThird;
738 (*fi).V(1)->Q()+=areaThird;
739 (*fi).V(2)->Q()+=areaThird;
740 }
741
742 VertexWeighted(m,ps,sampleNum);
743}
744
745static void FillAndShuffleFacePointerVector(MeshType & m, std::vector<FacePointer> &faceVec)
746{
747 for(FaceIterator fi=m.face.begin();fi!=m.face.end();++fi)
748 if(!(*fi).IsD()) faceVec.push_back(&*fi);
749
750 assert((int)faceVec.size()==m.fn);
751
752 //unsigned int (*p_myrandom)(unsigned int) = RandomInt;
753 //std::random_device rd;
754 //std::mt19937 g(rd());
755 MarsenneTwisterURBG g;
756 std::shuffle(faceVec.begin(),faceVec.end(), g);
757}
758static void FillAndShuffleVertexPointerVector(MeshType & m, std::vector<VertexPointer> &vertVec)
759{
760 for(VertexIterator vi=m.vert.begin();vi!=m.vert.end();++vi)
761 if(!(*vi).IsD()) vertVec.push_back(&*vi);
762
763 assert((int)vertVec.size()==m.vn);
764
765 //unsigned int (*p_myrandom)(unsigned int) = RandomInt;
766 //std::random_device rd;
767 //std::mt19937 g(rd());
768 MarsenneTwisterURBG g;
769 std::shuffle(vertVec.begin(),vertVec.end(), g);
770}
771
773static void VertexUniform(MeshType & m, VertexSampler &ps, int sampleNum, bool onlySelected)
774{
775 if (sampleNum >= m.vn) {
776 AllVertex(m, ps, onlySelected);
777 return;
778 }
779
780 std::vector<VertexPointer> vertVec;
781 FillAndShuffleVertexPointerVector(m, vertVec);
782
783 int added = 0;
784 for (int i = 0; ((i < m.vn) && (added < sampleNum)); ++i)
785 if (!(*vertVec[i]).IsD())
786 if ((!onlySelected) || (*vertVec[i]).IsS())
787 {
788 ps.AddVert(*vertVec[i]);
789 added++;
790 }
791
792}
793
794
795static void VertexUniform(MeshType & m, VertexSampler &ps, int sampleNum)
796{
797 VertexUniform(m, ps, sampleNum, false);
798}
799
800
808{
809 Floor = 0,
810 Round,
811 Ceil,
812};
813
829
830static void EdgeMeshUniform(MeshType &m, VertexSampler &ps, float radius, EdgeSamplingRoundingStrategy strategy = Floor)
831{
832 tri::RequireEEAdjacency(m);
833 tri::RequireCompactness(m);
834 tri::RequirePerEdgeFlags(m);
835 tri::RequirePerVertexFlags(m);
838 tri::MeshAssert<MeshType>::EEOneManifold(m);
839
840 for (EdgeIterator ei = m.edge.begin(); ei != m.edge.end(); ++ei)
841 {
842 if (!ei->IsV())
843 {
844 edge::Pos<EdgeType> ep(&*ei,0);
845 edge::Pos<EdgeType> startep = ep;
846 do // first loop to search a boundary component or check if it is a ring
847 {
848 ep.NextE();
849 if (ep.IsBorder())
850 break;
851 } while (startep != ep);
852
853 if (!ep.IsBorder()) // ******************** Circular Ring ********************
854 {
855 assert(ep == startep);
856
857 // to keep the uniform resampling order-independent:
858 // 1) start from the 'lowest' point...
859 edge::Pos<EdgeType> altEp = ep;
860 altEp.NextE();
861 while (altEp != startep) {
862 if (altEp.V()->cP() < ep.V()->cP())
863 {
864 ep = altEp;
865 }
866 altEp.NextE();
867 }
868
869 // 2) ... with consistent direction
870 const auto dir0 = ep.VFlip()->cP() - ep.V()->cP();
871 ep.FlipE();
872 const auto dir1 = ep.VFlip()->cP() - ep.V()->cP();
873 if (dir0 < dir1)
874 ep.FlipE();
875 }
876 else // ******************** NOT Circular Ring ********************
877 {
878 // to keep the uniform resampling order-independent
879 // start from the border with 'lowest' point
880 edge::Pos<EdgeType> altEp = ep;
881 do {
882 altEp.NextE();
883 } while (!altEp.IsBorder());
884
885 if (altEp.V()->cP() < ep.V()->cP())
886 {
887 ep = altEp;
888 }
889 }
890
891 ScalarType totalLen = 0;
892 ep.FlipV();
893 // second loop to compute the length of the chain marking visited all the edges
894 do
895 {
896 ep.E()->SetV();
897 totalLen += Distance(ep.V()->cP(), ep.VFlip()->cP());
898 ep.NextE();
899 } while (!ep.E()->IsV() && !ep.IsBorder());
900 if (ep.IsBorder() && !ep.E()->IsV())
901 {
902 ep.E()->SetV();
903 totalLen += Distance(ep.V()->cP(), ep.VFlip()->cP());
904 }
905
906 VertexPointer startVertex = ep.V();
907
908 // Third loop: actually performs the sampling by walking on the edge chain
909 int sampleNum = -1;
910 {
911 double div = double(totalLen) / radius;
912 switch (strategy) {
913 case Round: sampleNum = int(round(div)); break;
914 case Ceil: sampleNum = int( ceil(div)); break;
915 default: sampleNum = int(floor(div)); break;
916 };
917 }
918 assert(sampleNum >= 0);
919
920 ScalarType sampleDist = totalLen / sampleNum; // the steplen we use for sampling
921
922 // printf("Found a chain with len %f: we will place %i samples every %f (original radius : %f)\n", totalLen, sampleNum, sampleDist, radius);
923
924 ScalarType curLen = 0;
925 int sampleCnt = 1;
926 ps.AddEdge(*(ep.E()), ep.VInd() == 0 ? 0.0 : 1.0); // First Sample always at the start of the chain
927
928 do {
929 ep.NextE();
930 assert(ep.E()->IsV());
931 ScalarType edgeLen = Distance(ep.VFlip()->cP(), ep.V()->cP());
932 ScalarType d0 = curLen;
933 ScalarType d1 = d0 + edgeLen;
934
935 while (d1 > sampleCnt * sampleDist && sampleCnt < sampleNum)
936 {
937 ScalarType off = (sampleCnt * sampleDist - d0) / edgeLen;
938// printf("edgeLen %f off %f samplecnt %i\n", edgeLen, off, sampleCnt);
939 ps.AddEdge(*(ep.E()), ep.VInd() == 0 ? 1.0 - off : off);
940 sampleCnt++;
941 }
942 curLen += edgeLen;
943 } while(!ep.IsBorder() && ep.V() != startVertex);
944
945 if(ep.V() != startVertex) // if we are not in a loop we add the last vertex
946 {
947 ps.AddEdge(*(ep.E()), ep.VInd() == 0 ? 0.0 : 1.0);
948 }
949 }
950 }
951}
952
953
959static void VertexBorderCorner(MeshType & m, VertexSampler &ps, ScalarType angleRad)
960{
962 for(VertexIterator vi=m.vert.begin();vi!=m.vert.end();++vi)
963 {
964 if(vi->IsS()) ps.AddVert(*vi);
965 }
966}
967
973static void VertexBorder(MeshType & m, VertexSampler &ps)
974{
975 VertexBorderCorner(m,ps,std::numeric_limits<ScalarType>::max());
976}
977
984static void VertexCrease(MeshType & m, VertexSampler &ps)
985{
986 typedef typename UpdateTopology<MeshType>::PEdge SimpleEdge;
987 std::vector< SimpleEdge > Edges;
988 typename std::vector< SimpleEdge >::iterator ei;
990
991 typename MeshType::template PerVertexAttributeHandle <int> hv = tri::Allocator<MeshType>:: template GetPerVertexAttribute<int> (m);
992
993 for(ei=Edges.begin(); ei!=Edges.end(); ++ei)
994 {
995 hv[ei->v[0]]++;
996 hv[ei->v[1]]++;
997 }
998
999 for(VertexIterator vi=m.vert.begin();vi!=m.vert.end();++vi)
1000 {
1001 if(hv[vi]>2)
1002 ps.AddVert(*vi);
1003 }
1004}
1005
1006
1007// AddFace() takes the sample's *barycentric* coordinates inside the face, not a
1008// position: the sampler reconstructs the point as v0*b[0]+v1*b[1]+v2*b[2]. Passing
1009// Barycenter(f), which is a world-space point, made every sample land on an arbitrary
1010// linear combination of the corners -- on a unit sphere, radii from 0.10 to 1.72
1011// instead of ~0.996. The face centroid in barycentric coordinates is simply (1/3,1/3,1/3).
1012static inline CoordType CentroidBarycentricCoord()
1013{
1014 return CoordType(ScalarType(1.0/3.0), ScalarType(1.0/3.0), ScalarType(1.0/3.0));
1015}
1016
1017static void FaceUniform(MeshType & m, VertexSampler &ps, int sampleNum)
1018{
1019 if(sampleNum>=m.fn) {
1020 AllFace(m,ps);
1021 return;
1022 }
1023
1024 std::vector<FacePointer> faceVec;
1025 FillAndShuffleFacePointerVector(m,faceVec);
1026
1027 for(int i =0; i< sampleNum; ++i)
1028 ps.AddFace(*faceVec[i],CentroidBarycentricCoord());
1029}
1030
1031static void AllFace(MeshType & m, VertexSampler &ps)
1032{
1033 FaceIterator fi;
1034 for(fi=m.face.begin();fi!=m.face.end();++fi)
1035 if(!(*fi).IsD())
1036 {
1037 ps.AddFace(*fi,CentroidBarycentricCoord());
1038 }
1039}
1040
1041
1042static void AllEdge(MeshType & m, VertexSampler &ps)
1043{
1044 // Edge sampling.
1045 typedef typename UpdateTopology<MeshType>::PEdge SimpleEdge;
1046 std::vector< SimpleEdge > Edges;
1047 typename std::vector< SimpleEdge >::iterator ei;
1048 UpdateTopology<MeshType>::FillUniqueEdgeVector(m,Edges);
1049
1050 for(ei=Edges.begin(); ei!=Edges.end(); ++ei)
1051 ps.AddFace(*(*ei).f,ei->EdgeBarycentricToFaceBarycentric(0.5));
1052}
1053
1054// Regular Uniform Edge sampling
1055// Each edge is subdivided in a number of pieces proprtional to its length
1056// Samples are chosen without touching the vertices.
1057
1058static void EdgeUniform(MeshType & m, VertexSampler &ps,int sampleNum, bool sampleFauxEdge=true)
1059{
1060 typedef typename UpdateTopology<MeshType>::PEdge SimpleEdge;
1061
1062 std::vector< SimpleEdge > Edges;
1063 UpdateTopology<MeshType>::FillUniqueEdgeVector(m,Edges,sampleFauxEdge);
1064 // First loop compute total edge length;
1065 float edgeSum=0;
1066 typename std::vector< SimpleEdge >::iterator ei;
1067 for(ei=Edges.begin(); ei!=Edges.end(); ++ei)
1068 edgeSum+=Distance((*ei).v[0]->P(),(*ei).v[1]->P());
1069
1070 float sampleLen = edgeSum/sampleNum;
1071 float rest=0;
1072 for(ei=Edges.begin(); ei!=Edges.end(); ++ei)
1073 {
1074 float len = Distance((*ei).v[0]->P(),(*ei).v[1]->P());
1075 float samplePerEdge = floor((len+rest)/sampleLen);
1076 rest = (len+rest) - samplePerEdge * sampleLen;
1077 float step = 1.0/(samplePerEdge+1);
1078 for(int i=0;i<samplePerEdge;++i)
1079 {
1080 CoordType interp(0,0,0);
1081 interp[ (*ei).z ]=step*(i+1);
1082 interp[((*ei).z+1)%3]=1.0-step*(i+1);
1083 ps.AddFace(*(*ei).f,interp);
1084 }
1085 }
1086}
1087
1088// Generate the barycentric coords of a random point over a single face,
1089// with a uniform distribution over the triangle.
1090// It uses the parallelogram folding trick.
1091static CoordType RandomBarycentric()
1092{
1093 return math::GenerateBarycentricUniform<ScalarType>(SamplingRandomGenerator());
1094}
1095
1096// Given a triangle return a random point over it
1097static CoordType RandomPointInTriangle(const FaceType &f)
1098{
1099 CoordType u = RandomBarycentric();
1100 return f.cP(0)*u[0] + f.cP(1)*u[1] + f.cP(2)*u[2];
1101}
1102
1103static void StratifiedMontecarlo(MeshType & m, VertexSampler &ps,int sampleNum)
1104{
1105 ScalarType area = Stat<MeshType>::ComputeMeshArea(m);
1106 ScalarType samplePerAreaUnit = sampleNum/area;
1107 // Montecarlo sampling.
1108 double floatSampleNum = 0.0;
1109
1110 FaceIterator fi;
1111 for(fi=m.face.begin(); fi != m.face.end(); fi++)
1112 if(!(*fi).IsD())
1113 {
1114 // compute # samples in the current face (taking into account of the remainders)
1115 floatSampleNum += 0.5*DoubleArea(*fi) * samplePerAreaUnit;
1116 int faceSampleNum = (int) floatSampleNum;
1117
1118 // for every sample p_i in T...
1119 for(int i=0; i < faceSampleNum; i++)
1120 ps.AddFace(*fi,RandomBarycentric());
1121 floatSampleNum -= (double) faceSampleNum;
1122 }
1123}
1124
1139static void MontecarloPoisson(MeshType & m, VertexSampler &ps,int sampleNum)
1140{
1141 ScalarType area = Stat<MeshType>::ComputeMeshArea(m);
1142 ScalarType samplePerAreaUnit = sampleNum/area;
1143
1144 FaceIterator fi;
1145 for(fi=m.face.begin(); fi != m.face.end(); fi++)
1146 if(!(*fi).IsD())
1147 {
1148 float areaT=DoubleArea(*fi) * 0.5f;
1149 int faceSampleNum = Poisson(areaT*samplePerAreaUnit);
1150
1151 // for every sample p_i in T...
1152 for(int i=0; i < faceSampleNum; i++)
1153 ps.AddFace(*fi,RandomBarycentric());
1154// SampleNum -= (double) faceSampleNum;
1155 }
1156}
1157
1158
1165static void EdgeMontecarlo(MeshType & m, VertexSampler &ps, int sampleNum, bool sampleAllEdges)
1166{
1167 typedef typename UpdateTopology<MeshType>::PEdge SimpleEdge;
1168 std::vector< SimpleEdge > Edges;
1169 UpdateTopology<MeshType>::FillUniqueEdgeVector(m,Edges,sampleAllEdges);
1170
1171 assert(!Edges.empty());
1172
1173 // The running sum is double for the reasons given in Montecarlo below.
1174 typedef std::pair<double, SimpleEdge*> IntervalType;
1175 std::vector< IntervalType > intervals (Edges.size()+1);
1176 int i=0;
1177 intervals[i]=std::make_pair(0,(SimpleEdge*)(0));
1178 // First loop: build a sequence of consecutive segments proportional to the edge lenghts.
1179 typename std::vector< SimpleEdge >::iterator ei;
1180 for(ei=Edges.begin(); ei != Edges.end(); ei++)
1181 {
1182 intervals[i+1]=std::make_pair(intervals[i].first+Distance((*ei).v[0]->P(),(*ei).v[1]->P()), &*ei);
1183 ++i;
1184 }
1185
1186 // Second Loop get a point on the line 0...Sum(edgeLen) to pick a point;
1187 const double edgeSum = intervals.back().first;
1188 for(i=0;i<sampleNum;++i)
1189 {
1190 const double val = edgeSum * SamplingRandomGenerator().generate01res53();
1191 // The first segment whose end lies beyond val: val is in [0,edgeSum), so this is never
1192 // the sentinel at begin() nor end(), and a zero-length edge is never picked.
1193 auto it = std::upper_bound(intervals.begin(),intervals.end(),val,
1194 [](double v, const IntervalType &e){ return v < e.first; });
1195 assert(it != intervals.end() && it != intervals.begin());
1196 SimpleEdge * ep=(*it).second;
1197 ps.AddFace( *(ep->f), ep->EdgeBarycentricToFaceBarycentric(RandomDouble01()) );
1198 }
1199}
1200
1207static void Montecarlo(MeshType & m, VertexSampler &ps,int sampleNum)
1208{
1209 // The running sum must be double even on a float mesh: a face's chance of being picked is
1210 // the width of its interval, and once a float sum reaches tens of units (tens of millions
1211 // of faces) its spacing exceeds the area of the small faces, which then add nothing and are
1212 // never sampled while slightly larger ones are rounded up and oversampled.
1213 typedef std::pair<double, FacePointer> IntervalType;
1214 std::vector< IntervalType > intervals (m.fn+1);
1215 FaceIterator fi;
1216 int i=0;
1217 intervals[i]=std::make_pair(0,FacePointer(0));
1218 // First loop: build a sequence of consecutive segments proportional to the triangle areas.
1219 for(fi=m.face.begin(); fi != m.face.end(); fi++)
1220 if(!(*fi).IsD())
1221 {
1222 intervals[i+1]=std::make_pair(intervals[i].first+0.5*DoubleArea(*fi), &*fi);
1223 ++i;
1224 }
1225 const double meshArea = intervals.back().first;
1226 for(i=0;i<sampleNum;++i)
1227 {
1228 // 53 bits, not the 32 of RandomDouble01: the lightest of tens of millions of faces
1229 // would otherwise be reachable by only a few distinct values.
1230 const double val = meshArea * SamplingRandomGenerator().generate01res53();
1231 // The first segment whose end lies beyond val: val is in [0,meshArea), so this is
1232 // never the sentinel at begin() nor end(), and a zero-area face is never picked.
1233 auto it = std::upper_bound(intervals.begin(),intervals.end(),val,
1234 [](double v, const IntervalType &e){ return v < e.first; });
1235 assert(it != intervals.end());
1236 assert(it != intervals.begin());
1237 ps.AddFace( *(*it).second, RandomBarycentric() );
1238 }
1239}
1240
1241static ScalarType WeightedArea(FaceType &f, PerVertexFloatAttribute &wH)
1242{
1243 ScalarType averageQ = ( wH[f.V(0)] + wH[f.V(1)] + wH[f.V(2)] )/3.0;
1244 return averageQ*averageQ*DoubleArea(f)/2.0;
1245}
1246
1255static void WeightedMontecarlo(MeshType & m, VertexSampler &ps,int sampleNum, float variance)
1256{
1257 tri::RequirePerVertexQuality(m);
1258 tri::RequireCompactness(m);
1259 PerVertexFloatAttribute rH = tri::Allocator<MeshType>:: template GetPerVertexAttribute<float> (m,"radius");
1260 InitRadiusHandleFromQuality(m, rH, 1.0, variance, true);
1261
1262 double weightedArea = 0; // double for the reason given in Stat::ComputeMeshArea
1263 for(FaceIterator fi = m.face.begin(); fi != m.face.end(); ++fi)
1264 weightedArea += WeightedArea(*fi,rH);
1265
1266 ScalarType samplePerAreaUnit = ScalarType(sampleNum/weightedArea);
1267 // Montecarlo sampling.
1268 double floatSampleNum = 0.0;
1269 for(FaceIterator fi=m.face.begin(); fi != m.face.end(); fi++)
1270 {
1271 // compute # samples in the current face (taking into account of the remainders)
1272 floatSampleNum += WeightedArea(*fi,rH) * samplePerAreaUnit;
1273 int faceSampleNum = (int) floatSampleNum;
1274
1275 // for every sample p_i in T...
1276 for(int i=0; i < faceSampleNum; i++)
1277 ps.AddFace(*fi,RandomBarycentric());
1278
1279 floatSampleNum -= (double) faceSampleNum;
1280 }
1281}
1282
1283
1284// Subdivision sampling of a single face.
1285// return number of added samples
1286
1287static int SingleFaceSubdivision(int sampleNum, const CoordType & v0, const CoordType & v1, const CoordType & v2, VertexSampler &ps, FacePointer fp, bool randSample)
1288{
1289 // recursive face subdivision.
1290 if(sampleNum == 1)
1291 {
1292 // ground case.
1293 CoordType SamplePoint;
1294 if(randSample)
1295 {
1296 CoordType rb=RandomBarycentric();
1297 SamplePoint=v0*rb[0]+v1*rb[1]+v2*rb[2];
1298 }
1299 else SamplePoint=((v0+v1+v2)*(1.0f/3.0f));
1300
1301 ps.AddFace(*fp,SamplePoint);
1302 return 1;
1303 }
1304
1305 int s0 = sampleNum /2;
1306 int s1 = sampleNum-s0;
1307 assert(s0>0);
1308 assert(s1>0);
1309
1310 ScalarType w0 = ScalarType(s1)/ScalarType(sampleNum);
1311 ScalarType w1 = 1.0-w0;
1312 // compute the longest edge.
1313 ScalarType maxd01 = SquaredDistance(v0,v1);
1314 ScalarType maxd12 = SquaredDistance(v1,v2);
1315 ScalarType maxd20 = SquaredDistance(v2,v0);
1316 int res;
1317 if(maxd01 > maxd12)
1318 if(maxd01 > maxd20) res = 0;
1319 else res = 2;
1320 else
1321 if(maxd12 > maxd20) res = 1;
1322 else res = 2;
1323
1324 int faceSampleNum=0;
1325 // break the input triangle along the midpoint of the longest edge.
1326 CoordType pp;
1327 switch(res)
1328 {
1329 case 0 : pp = v0*w0 + v1*w1;
1330 faceSampleNum+=SingleFaceSubdivision(s0,v0,pp,v2,ps,fp,randSample);
1331 faceSampleNum+=SingleFaceSubdivision(s1,pp,v1,v2,ps,fp,randSample);
1332 break;
1333 case 1 : pp = v1*w0 + v2*w1;
1334 faceSampleNum+=SingleFaceSubdivision(s0,v0,v1,pp,ps,fp,randSample);
1335 faceSampleNum+=SingleFaceSubdivision(s1,v0,pp,v2,ps,fp,randSample);
1336 break;
1337 case 2 : pp = v0*w0 + v2*w1;
1338 faceSampleNum+=SingleFaceSubdivision(s0,v0,v1,pp,ps,fp,randSample);
1339 faceSampleNum+=SingleFaceSubdivision(s1,pp,v1,v2,ps,fp,randSample);
1340 break;
1341 }
1342 return faceSampleNum;
1343}
1344
1345
1347static void FaceSubdivision(MeshType & m, VertexSampler &ps,int sampleNum, bool randSample)
1348{
1349
1350 ScalarType area = Stat<MeshType>::ComputeMeshArea(m);
1351 ScalarType samplePerAreaUnit = sampleNum/area;
1352 std::vector<FacePointer> faceVec;
1353 FillAndShuffleFacePointerVector(m,faceVec);
1355 double floatSampleNum = 0.0;
1356 int faceSampleNum;
1357 // Subdivision sampling.
1358 typename std::vector<FacePointer>::iterator fi;
1359 for(fi=faceVec.begin(); fi!=faceVec.end(); fi++)
1360 {
1361 const CoordType b0(1.0, 0.0, 0.0);
1362 const CoordType b1(0.0, 1.0, 0.0);
1363 const CoordType b2(0.0, 0.0, 1.0);
1364 // compute # samples in the current face.
1365 floatSampleNum += 0.5*DoubleArea(**fi) * samplePerAreaUnit;
1366 faceSampleNum = (int) floatSampleNum;
1367 if(faceSampleNum>0)
1368 faceSampleNum = SingleFaceSubdivision(faceSampleNum,b0,b1,b2,ps,*fi,randSample);
1369 floatSampleNum -= (double) faceSampleNum;
1370 }
1371}
1372//---------
1373// Subdivision sampling of a single face.
1374// return number of added samples
1375
1376static int SingleFaceSubdivisionOld(int sampleNum, const CoordType & v0, const CoordType & v1, const CoordType & v2, VertexSampler &ps, FacePointer fp, bool randSample)
1377{
1378 // recursive face subdivision.
1379 if(sampleNum == 1)
1380 {
1381 // ground case.
1382 CoordType SamplePoint;
1383 if(randSample)
1384 {
1385 CoordType rb=RandomBarycentric();
1386 SamplePoint=v0*rb[0]+v1*rb[1]+v2*rb[2];
1387 }
1388 else SamplePoint=((v0+v1+v2)*(1.0f/3.0f));
1389
1390 CoordType SampleBary;
1391 InterpolationParameters(*fp,SamplePoint,SampleBary);
1392 ps.AddFace(*fp,SampleBary);
1393 return 1;
1394 }
1395
1396 int s0 = sampleNum /2;
1397 int s1 = sampleNum-s0;
1398 assert(s0>0);
1399 assert(s1>0);
1400
1401 ScalarType w0 = ScalarType(s1)/ScalarType(sampleNum);
1402 ScalarType w1 = 1.0-w0;
1403 // compute the longest edge.
1404 ScalarType maxd01 = SquaredDistance(v0,v1);
1405 ScalarType maxd12 = SquaredDistance(v1,v2);
1406 ScalarType maxd20 = SquaredDistance(v2,v0);
1407 int res;
1408 if(maxd01 > maxd12)
1409 if(maxd01 > maxd20) res = 0;
1410 else res = 2;
1411 else
1412 if(maxd12 > maxd20) res = 1;
1413 else res = 2;
1414
1415 int faceSampleNum=0;
1416 // break the input triangle along the midpoint of the longest edge.
1417 CoordType pp;
1418 switch(res)
1419 {
1420 case 0 : pp = v0*w0 + v1*w1;
1421 faceSampleNum+=SingleFaceSubdivision(s0,v0,pp,v2,ps,fp,randSample);
1422 faceSampleNum+=SingleFaceSubdivision(s1,pp,v1,v2,ps,fp,randSample);
1423 break;
1424 case 1 : pp = v1*w0 + v2*w1;
1425 faceSampleNum+=SingleFaceSubdivision(s0,v0,v1,pp,ps,fp,randSample);
1426 faceSampleNum+=SingleFaceSubdivision(s1,v0,pp,v2,ps,fp,randSample);
1427 break;
1428 case 2 : pp = v0*w0 + v2*w1;
1429 faceSampleNum+=SingleFaceSubdivision(s0,v0,v1,pp,ps,fp,randSample);
1430 faceSampleNum+=SingleFaceSubdivision(s1,pp,v1,v2,ps,fp,randSample);
1431 break;
1432 }
1433 return faceSampleNum;
1434}
1435
1436
1438static void FaceSubdivisionOld(MeshType & m, VertexSampler &ps,int sampleNum, bool randSample)
1439{
1440
1441 ScalarType area = Stat<MeshType>::ComputeMeshArea(m);
1442 ScalarType samplePerAreaUnit = sampleNum/area;
1443 std::vector<FacePointer> faceVec;
1444 FillAndShuffleFacePointerVector(m,faceVec);
1446 double floatSampleNum = 0.0;
1447 int faceSampleNum;
1448 // Subdivision sampling.
1449 typename std::vector<FacePointer>::iterator fi;
1450 for(fi=faceVec.begin(); fi!=faceVec.end(); fi++)
1451 {
1452 // compute # samples in the current face.
1453 floatSampleNum += 0.5*DoubleArea(**fi) * samplePerAreaUnit;
1454 faceSampleNum = (int) floatSampleNum;
1455 if(faceSampleNum>0)
1456 faceSampleNum = SingleFaceSubdivision(faceSampleNum,(**fi).V(0)->cP(), (**fi).V(1)->cP(), (**fi).V(2)->cP(),ps,*fi,randSample);
1457 floatSampleNum -= (double) faceSampleNum;
1458 }
1459}
1460
1461
1462//---------
1463
1464// Similar Triangles sampling.
1465// Skip vertex and edges
1466// Sample per edges includes vertexes, so here we should expect n_samples_per_edge >=4
1467
1468static int SingleFaceSimilar(FacePointer fp, VertexSampler &ps, int n_samples_per_edge)
1469{
1470 int n_samples=0;
1471 int i, j;
1472 float segmentNum=n_samples_per_edge -1 ;
1473 float segmentLen = 1.0/segmentNum;
1474 // face sampling.
1475 for(i=1; i < n_samples_per_edge-1; i++)
1476 for(j=1; j < n_samples_per_edge-1-i; j++)
1477 {
1478 //AddSample( v0 + (V1*(double)i + V2*(double)j) );
1479 CoordType sampleBary(i*segmentLen,j*segmentLen, 1.0 - (i*segmentLen+j*segmentLen) ) ;
1480 n_samples++;
1481 ps.AddFace(*fp,sampleBary);
1482 }
1483 return n_samples;
1484}
1485static int SingleFaceSimilarDual(FacePointer fp, VertexSampler &ps, int n_samples_per_edge, bool randomFlag)
1486{
1487 int n_samples=0;
1488 float i, j;
1489 float segmentNum=n_samples_per_edge -1 ;
1490 float segmentLen = 1.0/segmentNum;
1491 // face sampling.
1492 for(i=0; i < n_samples_per_edge-1; i++)
1493 for(j=0; j < n_samples_per_edge-1-i; j++)
1494 {
1495 //AddSample( v0 + (V1*(double)i + V2*(double)j) );
1496 CoordType V0((i+0)*segmentLen,(j+0)*segmentLen, 1.0 - ((i+0)*segmentLen+(j+0)*segmentLen) ) ;
1497 CoordType V1((i+1)*segmentLen,(j+0)*segmentLen, 1.0 - ((i+1)*segmentLen+(j+0)*segmentLen) ) ;
1498 CoordType V2((i+0)*segmentLen,(j+1)*segmentLen, 1.0 - ((i+0)*segmentLen+(j+1)*segmentLen) ) ;
1499 n_samples++;
1500 if(randomFlag) {
1501 CoordType rb=RandomBarycentric();
1502 ps.AddFace(*fp, V0*rb[0]+V1*rb[1]+V2*rb[2]);
1503 } else ps.AddFace(*fp,(V0+V1+V2)/3.0);
1504
1505 if( j < n_samples_per_edge-i-2 )
1506 {
1507 CoordType V3((i+1)*segmentLen,(j+1)*segmentLen, 1.0 - ((i+1)*segmentLen+(j+1)*segmentLen) ) ;
1508 n_samples++;
1509 if(randomFlag) {
1510 CoordType rb=RandomBarycentric();
1511 ps.AddFace(*fp, V3*rb[0]+V1*rb[1]+V2*rb[2]);
1512 } else ps.AddFace(*fp,(V3+V1+V2)/3.0);
1513 }
1514 }
1515 return n_samples;
1516}
1517
1518// Similar sampling
1519// Each triangle is subdivided into similar triangles following a generalization of the classical 1-to-4 splitting rule of triangles.
1520// According to the level of subdivision <k> you get 1, 4 , 9, 16 , <k^2> triangles.
1521// Depending on the kind of the sampling strategies we can have two different approach to choosing the sample points.
1522// 1) you have already sampled both edges and vertices
1523// 2) you are not going to take samples on edges and vertices.
1524//
1525// In the first case you have to consider only internal vertices of the subdivided triangles (to avoid multiple sampling of edges and vertices).
1526// Therefore the number of internal points is ((k-3)*(k-2))/2. where k is the number of points on an edge (vertex included)
1527// E.g. for k=4 you get 3 segments on each edges and the original triangle is subdivided
1528// into 9 smaller triangles and you get (1*2)/2 == 1 only a single internal point.
1529// So if you want N samples in a triangle you have to solve k^2 -5k +6 - 2N = 0
1530// from which you get:
1531//
1532// 5 + sqrt( 1 + 8N )
1533// k = -------------------
1534// 2
1535//
1536// In the second case if you are not interested to skip the sampling on edges and vertices you have to consider as sample number the number of triangles.
1537// So if you want N samples in a triangle, the number <k> of points on an edge (vertex included) should be simply:
1538// k = 1 + sqrt(N)
1539// examples:
1540// N = 4 -> k = 3
1541// N = 9 -> k = 4
1542
1543
1544
1545//template <class MeshType>
1546//void Sampling<MeshType>::SimilarFaceSampling()
1547static void FaceSimilar(MeshType & m, VertexSampler &ps,int sampleNum, bool dualFlag, bool randomFlag)
1548{
1549 ScalarType area = Stat<MeshType>::ComputeMeshArea(m);
1550 ScalarType samplePerAreaUnit = sampleNum/area;
1551
1552 // Similar Triangles sampling.
1553 int n_samples_per_edge;
1554 double n_samples_decimal = 0.0;
1555 FaceIterator fi;
1556
1557 for(fi=m.face.begin(); fi != m.face.end(); fi++)
1558 {
1559 // compute # samples in the current face.
1560 n_samples_decimal += 0.5*DoubleArea(*fi) * samplePerAreaUnit;
1561 int n_samples = (int) n_samples_decimal;
1562 if(n_samples>0)
1563 {
1564 // face sampling.
1565 if(dualFlag)
1566 {
1567 n_samples_per_edge = (int)((sqrt(1.0+8.0*(double)n_samples) +5.0)/2.0); // original for non dual case
1568 n_samples = SingleFaceSimilar(&*fi,ps, n_samples_per_edge);
1569 } else {
1570 n_samples_per_edge = (int)(sqrt((double)n_samples) +1.0);
1571 n_samples = SingleFaceSimilarDual(&*fi,ps, n_samples_per_edge,randomFlag);
1572 }
1573 }
1574 n_samples_decimal -= (double) n_samples;
1575 }
1576}
1577
1578
1579 // Rasterization fuction
1580 // Take a triangle
1581 // T deve essere una classe funzionale che ha l'operatore ()
1582 // con due parametri x,y di tipo S esempio:
1583 // class Foo { public void operator()(int x, int y ) { ??? } };
1584
1585// This function does rasterization with a safety buffer area, thus accounting some points actually outside triangle area
1586// The safety area samples are generated according to face flag BORDER which should be true for texture space border edges
1587// Use correctSafePointsBaryCoords = true to map safety texels to closest point barycentric coords (on edge).
1588 static void SingleFaceRaster(typename MeshType::FaceType &f, VertexSampler &ps,
1589 const Point2<typename MeshType::ScalarType> & v0,
1590 const Point2<typename MeshType::ScalarType> & v1,
1591 const Point2<typename MeshType::ScalarType> & v2,
1592 bool correctSafePointsBaryCoords=true)
1593 {
1594 typedef typename MeshType::ScalarType S;
1595 // Calcolo bounding box
1596 Box2i bbox;
1597 Box2<S> bboxf;
1598 bboxf.Add(v0);
1599 bboxf.Add(v1);
1600 bboxf.Add(v2);
1601
1602 bbox.min[0] = floor(bboxf.min[0]);
1603 bbox.min[1] = floor(bboxf.min[1]);
1604 bbox.max[0] = ceil(bboxf.max[0]);
1605 bbox.max[1] = ceil(bboxf.max[1]);
1606
1607 // Calcolo versori degli spigoli
1608 Point2<S> d10 = v1 - v0;
1609 Point2<S> d21 = v2 - v1;
1610 Point2<S> d02 = v0 - v2;
1611
1612 // Preparazione prodotti scalari
1613 S b0 = (bbox.min[0]-v0[0])*d10[1] - (bbox.min[1]-v0[1])*d10[0];
1614 S b1 = (bbox.min[0]-v1[0])*d21[1] - (bbox.min[1]-v1[1])*d21[0];
1615 S b2 = (bbox.min[0]-v2[0])*d02[1] - (bbox.min[1]-v2[1])*d02[0];
1616 // Preparazione degli steps
1617 S db0 = d10[1];
1618 S db1 = d21[1];
1619 S db2 = d02[1];
1620 // Preparazione segni
1621 S dn0 = -d10[0];
1622 S dn1 = -d21[0];
1623 S dn2 = -d02[0];
1624
1625 //Calculating orientation
1626 bool flipped = !(d02 * vcg::Point2<S>(-d10[1], d10[0]) >= 0);
1627
1628 // Calculating border edges
1629 Segment2<S> borderEdges[3];
1630 S edgeLength[3];
1631 unsigned char edgeMask = 0;
1632
1633 if (f.IsB(0)) {
1634 borderEdges[0] = Segment2<S>(v0, v1);
1635 edgeLength[0] = borderEdges[0].Length();
1636 edgeMask |= 1;
1637 }
1638 if (f.IsB(1)) {
1639 borderEdges[1] = Segment2<S>(v1, v2);
1640 edgeLength[1] = borderEdges[1].Length();
1641 edgeMask |= 2;
1642 }
1643 if (f.IsB(2)) {
1644 borderEdges[2] = Segment2<S>(v2, v0);
1645 edgeLength[2] = borderEdges[2].Length();
1646 edgeMask |= 4;
1647 }
1648
1649 // Rasterizzazione
1650 double de = v0[0]*v1[1]-v0[0]*v2[1]-v1[0]*v0[1]+v1[0]*v2[1]-v2[0]*v1[1]+v2[0]*v0[1];
1651
1652 for(int x=bbox.min[0]-1;x<=bbox.max[0]+1;++x)
1653 {
1654 bool in = false;
1655 S n[3] = { b0-db0-dn0, b1-db1-dn1, b2-db2-dn2};
1656 for(int y=bbox.min[1]-1;y<=bbox.max[1]+1;++y)
1657 {
1658 if( ((n[0]>=0 && n[1]>=0 && n[2]>=0) || (n[0]<=0 && n[1]<=0 && n[2]<=0)) && (de != 0))
1659 {
1660 typename MeshType::CoordType baryCoord;
1661 baryCoord[0] = double(-y*v1[0]+v2[0]*y+v1[1]*x-v2[0]*v1[1]+v1[0]*v2[1]-x*v2[1])/de;
1662 baryCoord[1] = -double( x*v0[1]-x*v2[1]-v0[0]*y+v0[0]*v2[1]-v2[0]*v0[1]+v2[0]*y)/de;
1663 baryCoord[2] = 1-baryCoord[0]-baryCoord[1];
1664
1665 ps.AddTextureSample(f, baryCoord, Point2i(x,y), 0);
1666 in = true;
1667 } else {
1668 // Check whether a pixel outside (on a border edge side) triangle affects color inside it
1669 Point2<S> px(x, y);
1670 Point2<S> closePoint;
1671 int closeEdge = -1;
1672 S minDst = FLT_MAX;
1673
1674 // find the closest point (on some edge) that lies on the 2x2 squared neighborhood of the considered point
1675 for (int i=0; i<3; ++i)
1676 {
1677 if (edgeMask & (1 << i))
1678 {
1679 Point2<S> close;
1680 S dst;
1681 if ( ((!flipped) && (n[i]<0)) ||
1682 ( flipped && (n[i]>0)) )
1683 {
1684 dst = ((close = ClosestPoint(borderEdges[i], px)) - px).Norm();
1685 if(dst < minDst &&
1686 close.X() > px.X()-1 && close.X() < px.X()+1 &&
1687 close.Y() > px.Y()-1 && close.Y() < px.Y()+1)
1688 {
1689 minDst = dst;
1690 closePoint = close;
1691 closeEdge = i;
1692 }
1693 }
1694 }
1695 }
1696
1697 if (closeEdge >= 0)
1698 {
1699 typename MeshType::CoordType baryCoord;
1700 if (correctSafePointsBaryCoords)
1701 {
1702 // Add x,y sample with closePoint barycentric coords (on edge)
1703 baryCoord[closeEdge] = (closePoint - borderEdges[closeEdge].P1()).Norm()/edgeLength[closeEdge];
1704 baryCoord[(closeEdge+1)%3] = 1 - baryCoord[closeEdge];
1705 baryCoord[(closeEdge+2)%3] = 0;
1706 } else {
1707 // Add x,y sample with his own barycentric coords (off edge)
1708 baryCoord[0] = double(-y*v1[0]+v2[0]*y+v1[1]*x-v2[0]*v1[1]+v1[0]*v2[1]-x*v2[1])/de;
1709 baryCoord[1] = -double( x*v0[1]-x*v2[1]-v0[0]*y+v0[0]*v2[1]-v2[0]*v0[1]+v2[0]*y)/de;
1710 baryCoord[2] = 1-baryCoord[0]-baryCoord[1];
1711 }
1712 ps.AddTextureSample(f, baryCoord, Point2i(x,y), minDst);
1713 in = true;
1714 }
1715 }
1716 n[0] += dn0;
1717 n[1] += dn1;
1718 n[2] += dn2;
1719 }
1720 b0 += db0;
1721 b1 += db1;
1722 b2 += db2;
1723 }
1724}
1725
1726// check the radius constrain
1727static bool checkPoissonDisk(SampleSHT & sht, const Point3<ScalarType> & p, ScalarType radius)
1728{
1729 // get the samples closest to the given one
1730 std::vector<VertexType*> closests;
1731 typedef EmptyTMark<MeshType> MarkerVert;
1732 static MarkerVert mv;
1733
1734 Box3f bb(p-Point3f(radius,radius,radius),p+Point3f(radius,radius,radius));
1735 GridGetInBox(sht, mv, bb, closests);
1736
1737 ScalarType r2 = radius*radius;
1738 for(int i=0; i<closests.size(); ++i)
1739 if(SquaredDistance(p,closests[i]->cP()) < r2)
1740 return false;
1741
1742 return true;
1743}
1744
1746{
1748 {
1749 adaptiveRadiusFlag = false;
1750 bestSampleChoiceFlag = true;
1751 bestSamplePoolSize = 10;
1752 radiusVariance =1;
1753 MAXLEVELS = 5;
1754 invertQuality = false;
1755 preGenFlag = false;
1756 preGenMesh = NULL;
1757 geodesicDistanceFlag = false;
1758 randomSeed = 0;
1759 }
1760
1761 struct Stat
1762 {
1763 int montecarloTime;
1764 int gridTime;
1765 int pruneTime;
1766 int totalTime;
1767 Point3i gridSize;
1768 int gridCellNum;
1769 size_t sampleNum;
1770 int montecarloSampleNum;
1771 };
1772
1773 bool geodesicDistanceFlag;
1774 bool bestSampleChoiceFlag; // In poisson disk pruning when we choose a sample in a cell, we choose the sample that remove the minimal number of other samples. This previlege the "on boundary" samples.
1775 int bestSamplePoolSize;
1776 bool adaptiveRadiusFlag;
1777 float radiusVariance;
1778 bool invertQuality;
1779 bool preGenFlag; // when generating a poisson distribution, you can initialize the set of computed points with
1780 // ALL the vertices of another mesh. Useful for building progressive//prioritize refinements.
1781 MeshType *preGenMesh; // There are two ways of passing the pregen vertexes to the pruning, 1) is with a mesh pointer
1782 // 2) with a per vertex attribute.
1783 int MAXLEVELS;
1784 int randomSeed;
1785
1786 Stat pds;
1787};
1788
1789
1790// generate Poisson-disk sample using a set of pre-generated samples (with the Montecarlo algorithm)
1791// It always return a point.
1792static VertexPointer getSampleFromCell(Point3i &cell, MontecarloSHT & samplepool)
1793{
1794 MontecarloSHTIterator cellBegin, cellEnd;
1795 samplepool.Grid(cell, cellBegin, cellEnd);
1796 return *cellBegin;
1797}
1798
1799// Given a cell of the grid it search the point that remove the minimum number of other samples
1800// it linearly scan all the points of a cell.
1801
1802static VertexPointer getBestPrecomputedMontecarloSample(Point3i &cell, MontecarloSHT & samplepool, ScalarType diskRadius, const PoissonDiskParam &pp)
1803{
1804 MontecarloSHTIterator cellBegin,cellEnd;
1805 samplepool.Grid(cell, cellBegin, cellEnd);
1806 VertexPointer bestSample=0;
1807 int minRemoveCnt = std::numeric_limits<int>::max();
1808 std::vector<typename MontecarloSHT::HashIterator> inSphVec;
1809 int i=0;
1810 for(MontecarloSHTIterator ci=cellBegin; ci!=cellEnd && i<pp.bestSamplePoolSize; ++ci,i++)
1811 {
1812 VertexPointer sp = *ci;
1813 if(pp.adaptiveRadiusFlag) diskRadius = sp->Q();
1814 int curRemoveCnt = samplepool.CountInSphere(sp->cP(),diskRadius,inSphVec);
1815 if(curRemoveCnt < minRemoveCnt)
1816 {
1817 bestSample = sp;
1818 minRemoveCnt = curRemoveCnt;
1819 }
1820 }
1821 return bestSample;
1822}
1823
1826static ScalarType ComputePoissonDiskRadius(MeshType &origMesh, int sampleNum)
1827{
1828 ScalarType meshArea = Stat<MeshType>::ComputeMeshArea(origMesh);
1829 // Manage approximately the PointCloud Case, use the half a area of the bbox.
1830 // TODO: If you had the radius a much better approximation could be done.
1831 if(meshArea ==0)
1832 {
1833 meshArea = (origMesh.bbox.DimX()*origMesh.bbox.DimY() +
1834 origMesh.bbox.DimX()*origMesh.bbox.DimZ() +
1835 origMesh.bbox.DimY()*origMesh.bbox.DimZ());
1836 }
1837 ScalarType diskRadius = sqrt(meshArea / (0.7 * M_PI * sampleNum)); // 0.7 is a density factor
1838 return diskRadius;
1839}
1840
1841static int ComputePoissonSampleNum(MeshType &origMesh, ScalarType diskRadius)
1842{
1843 ScalarType meshArea = Stat<MeshType>::ComputeMeshArea(origMesh);
1844 int sampleNum = meshArea / (diskRadius*diskRadius *M_PI *0.7) ; // 0.7 is a density factor
1845 return sampleNum;
1846}
1847
1854
1855static void InitRadiusHandleFromQuality(MeshType &sampleMesh, PerVertexFloatAttribute &rH, ScalarType diskRadius, ScalarType radiusVariance, bool invert)
1856{
1857 std::pair<float,float> minmax = tri::Stat<MeshType>::ComputePerVertexQualityMinMax( sampleMesh);
1858 float minRad = diskRadius ;
1859 float maxRad = diskRadius * radiusVariance;
1860 float deltaQ = minmax.second-minmax.first;
1861 float deltaRad = maxRad-minRad;
1862 for (VertexIterator vi = sampleMesh.vert.begin(); vi != sampleMesh.vert.end(); vi++)
1863 {
1864 rH[*vi] = minRad + deltaRad*((invert ? minmax.second - (*vi).Q() : (*vi).Q() - minmax.first )/deltaQ);
1865 }
1866}
1867
1868// initialize spatial hash table for searching
1869// radius is the radius of empty disk centered over the samples (e.g. twice of the empty space disk)
1870// This radius implies that when we pick a sample in a cell all that cell probably will not be touched again.
1871// Howvever we must ensure that we do not put too many vertices inside each hash cell
1872
1873static void InitSpatialHashTable(MeshType &montecarloMesh, MontecarloSHT &montecarloSHT, ScalarType diskRadius,
1874 struct PoissonDiskParam pp=PoissonDiskParam())
1875{
1876 ScalarType cellsize = 2.0f* diskRadius / sqrt(3.0);
1877 float occupancyRatio=0;
1878 do
1879 {
1880 // inflating
1881 BoxType bb=montecarloMesh.bbox;
1882 assert(!bb.IsNull());
1883 bb.Offset(cellsize);
1884
1885 int sizeX = std::max(1,int(bb.DimX() / cellsize));
1886 int sizeY = std::max(1,int(bb.DimY() / cellsize));
1887 int sizeZ = std::max(1,int(bb.DimZ() / cellsize));
1888 Point3i gridsize(sizeX, sizeY, sizeZ);
1889
1890 montecarloSHT.InitEmpty(bb, gridsize);
1891
1892 for (VertexIterator vi = montecarloMesh.vert.begin(); vi != montecarloMesh.vert.end(); vi++)
1893 if(!(*vi).IsD())
1894 {
1895 montecarloSHT.Add(&(*vi));
1896 }
1897
1898 montecarloSHT.UpdateAllocatedCells();
1899 pp.pds.gridSize = gridsize;
1900 pp.pds.gridCellNum = (int)montecarloSHT.AllocatedCells.size();
1901 cellsize/=2.0f;
1902 occupancyRatio = float(montecarloMesh.vn) / float(montecarloSHT.AllocatedCells.size());
1903 // qDebug(" %i / %i = %6.3f", montecarloMesh.vn , montecarloSHT.AllocatedCells.size(),occupancyRatio);
1904 }
1905 while( occupancyRatio> 100);
1906}
1907
1908static void PoissonDiskPruningByNumber(VertexSampler &ps, MeshType &m,
1909 size_t sampleNum, ScalarType &diskRadius,
1910 PoissonDiskParam &pp,
1911 float tolerance=0.04,
1912 int maxIter=20)
1913
1914{
1915 size_t sampleNumMin = int(float(sampleNum)*(1.0f-tolerance));
1916 size_t sampleNumMax = int(float(sampleNum)*(1.0f+tolerance));
1917 float RangeMinRad = m.bbox.Diag()/50.0;
1918 float RangeMaxRad = m.bbox.Diag()/50.0;
1919 size_t RangeMinRadNum;
1920 size_t RangeMaxRadNum;
1921 // Note RangeMinRad < RangeMaxRad
1922 // but RangeMinRadNum > sampleNum > RangeMaxRadNum
1923 do {
1924 ps.reset();
1925 RangeMinRad/=2.0f;
1926 PoissonDiskPruning(ps, m ,RangeMinRad,pp);
1927 RangeMinRadNum = pp.pds.sampleNum;
1928// qDebug("PoissonDiskPruning Iteratin Min (%6.3f:%5i) instead of %i",RangeMinRad,RangeMinRadNum,sampleNum);
1929 } while(RangeMinRadNum < sampleNum); // if the number of sample is still smaller you have to make radius larger.
1930
1931 do {
1932 ps.reset();
1933 RangeMaxRad*=2.0f;
1934 PoissonDiskPruning(ps, m ,RangeMaxRad,pp);
1935 RangeMaxRadNum = pp.pds.sampleNum;
1936// qDebug("PoissonDiskPruning Iteratin Max (%6.3f:%5i) instead of %i",RangeMaxRad,RangeMaxRadNum,sampleNum);
1937 } while(RangeMaxRadNum > sampleNum);
1938
1939
1940 float curRadius=RangeMaxRad;
1941 int iterCnt=0;
1942 while(iterCnt<maxIter &&
1943 (pp.pds.sampleNum < sampleNumMin || pp.pds.sampleNum > sampleNumMax) )
1944 {
1945 iterCnt++;
1946 ps.reset();
1947 curRadius=(RangeMaxRad+RangeMinRad)/2.0f;
1948 PoissonDiskPruning(ps, m ,curRadius,pp);
1949// qDebug("PoissonDiskPruning Iteratin (%6.3f:%5lu %6.3f:%5lu) Cur Radius %f -> %lu sample instead of %lu",RangeMinRad,RangeMinRadNum,RangeMaxRad,RangeMaxRadNum,curRadius,pp.pds.sampleNum,sampleNum);
1950 if(pp.pds.sampleNum > sampleNum){
1951 RangeMinRad = curRadius;
1952 RangeMinRadNum = pp.pds.sampleNum;
1953 }
1954 if(pp.pds.sampleNum < sampleNum){
1955 RangeMaxRad = curRadius;
1956 RangeMaxRadNum = pp.pds.sampleNum;
1957 }
1958 }
1959 diskRadius = curRadius;
1960}
1961
1962
1972static void PoissonDiskPruning(VertexSampler &ps, MeshType &montecarloMesh,
1973 ScalarType diskRadius, PoissonDiskParam &pp)
1974{
1975 tri::RequireCompactness(montecarloMesh);
1976 if(pp.randomSeed) SamplingRandomGenerator().initialize(pp.randomSeed);
1977 if(pp.adaptiveRadiusFlag)
1978 tri::RequirePerVertexQuality(montecarloMesh);
1979 int t0 = clock();
1980 // spatial index of montecarlo samples - used to choose a new sample to insert
1981 MontecarloSHT montecarloSHT;
1982 InitSpatialHashTable(montecarloMesh,montecarloSHT,diskRadius,pp);
1983
1984 // if we are doing variable density sampling we have to prepare the handle that keeps the the random samples expected radii.
1985 // At this point we just assume that there is the quality values as sampled from the base mesh
1986 // The radii are temporary: a named attribute would be left on the caller's mesh, and would
1987 // overwrite one of the caller's own that happened to be called "radius".
1988 PerVertexFloatAttribute rH = tri::Allocator<MeshType>:: template AddPerVertexAttribute<float> (montecarloMesh);
1989 if(pp.adaptiveRadiusFlag)
1990 InitRadiusHandleFromQuality(montecarloMesh, rH, diskRadius, pp.radiusVariance, pp.invertQuality);
1991
1992 //unsigned int (*p_myrandom)(unsigned int) = RandomInt;
1993// std::random_device rd;
1994// std::mt19937 g(rd());
1995// std::shuffle(montecarloSHT.AllocatedCells.begin(),montecarloSHT.AllocatedCells.end(), g);
1997 std::shuffle(montecarloSHT.AllocatedCells.begin(),montecarloSHT.AllocatedCells.end(), g);
1998 int t1 = clock();
1999 pp.pds.montecarloSampleNum = montecarloMesh.vn;
2000 pp.pds.sampleNum =0;
2001 int removedCnt=0;
2002 // Initial pass for pruning the Hashed grid with the an eventual pre initialized set of samples
2003 if(pp.preGenFlag)
2004 {
2005 if(pp.preGenMesh==0)
2006 {
2007 typename MeshType::template PerVertexAttributeHandle<bool> fixed;
2008 fixed = tri::Allocator<MeshType>:: template GetPerVertexAttribute<bool> (montecarloMesh,"fixed");
2009 for(VertexIterator vi=montecarloMesh.vert.begin();vi!=montecarloMesh.vert.end();++vi)
2010 if(fixed[*vi]) {
2011 pp.pds.sampleNum++;
2012 ps.AddVert(*vi);
2013 removedCnt += montecarloSHT.RemoveInSphere(vi->cP(),diskRadius);
2014 }
2015 }
2016 else
2017 {
2018 for(VertexIterator vi =pp.preGenMesh->vert.begin(); vi!=pp.preGenMesh->vert.end();++vi)
2019 {
2020 ps.AddVert(*vi);
2021 pp.pds.sampleNum++;
2022 removedCnt += montecarloSHT.RemoveInSphere(vi->cP(),diskRadius);
2023 }
2024 }
2025 montecarloSHT.UpdateAllocatedCells();
2026 }
2027 vertex::ApproximateGeodesicDistanceFunctor<VertexType> GDF;
2028 while(!montecarloSHT.AllocatedCells.empty())
2029 {
2030 removedCnt=0;
2031 for (size_t i = 0; i < montecarloSHT.AllocatedCells.size(); i++)
2032 {
2033 if( montecarloSHT.EmptyCell(montecarloSHT.AllocatedCells[i]) ) continue;
2034 ScalarType currentRadius =diskRadius;
2035 VertexPointer sp;
2036 if(pp.bestSampleChoiceFlag)
2037 sp = getBestPrecomputedMontecarloSample(montecarloSHT.AllocatedCells[i], montecarloSHT, diskRadius, pp);
2038 else
2039 sp = getSampleFromCell(montecarloSHT.AllocatedCells[i], montecarloSHT);
2040
2041 if(pp.adaptiveRadiusFlag)
2042 currentRadius = rH[sp];
2043
2044 ps.AddVert(*sp);
2045 pp.pds.sampleNum++;
2046 if(pp.geodesicDistanceFlag) removedCnt += montecarloSHT.RemoveInSphereNormal(sp->cP(),sp->cN(),GDF,currentRadius);
2047 else removedCnt += montecarloSHT.RemoveInSphere(sp->cP(),currentRadius);
2048 }
2049 montecarloSHT.UpdateAllocatedCells();
2050 }
2052 int t2 = clock();
2053 pp.pds.gridTime = t1-t0;
2054 pp.pds.pruneTime = t2-t1;
2055}
2056
2067static void HierarchicalPoissonDisk(MeshType &origMesh, VertexSampler &ps, MeshType &montecarloMesh, ScalarType diskRadius, const struct PoissonDiskParam pp=PoissonDiskParam())
2068{
2069// int t0=clock();
2070 // spatial index of montecarlo samples - used to choose a new sample to insert
2071 MontecarloSHT montecarloSHTVec[5];
2072
2073
2074
2075 // initialize spatial hash table for searching
2076 // radius is the radius of empty disk centered over the samples (e.g. twice of the empty space disk)
2077 // This radius implies that when we pick a sample in a cell all that cell will not be touched again.
2078 ScalarType cellsize = 2.0f* diskRadius / sqrt(3.0);
2079
2080 // inflating
2081 BoxType bb=origMesh.bbox;
2082 bb.Offset(cellsize);
2083
2084 int sizeX = std::max(1.0f,bb.DimX() / cellsize);
2085 int sizeY = std::max(1.0f,bb.DimY() / cellsize);
2086 int sizeZ = std::max(1.0f,bb.DimZ() / cellsize);
2087 Point3i gridsize(sizeX, sizeY, sizeZ);
2088
2089 // spatial hash table of the generated samples - used to check the radius constrain
2090 SampleSHT checkSHT;
2091 checkSHT.InitEmpty(bb, gridsize);
2092
2093
2094 // sampling algorithm
2095 // ------------------
2096 //
2097 // - generate millions of samples using montecarlo algorithm
2098 // - extract a cell (C) from the active cell list (with probability proportional to cell's volume)
2099 // - generate a sample inside C by choosing one of the contained pre-generated samples
2100 // - if the sample violates the radius constrain discard it, and add the cell to the cells-to-subdivide list
2101 // - iterate until the active cell list is empty or a pre-defined number of subdivisions is reached
2102 //
2103
2104 int level = 0;
2105
2106 // initialize spatial hash to index pre-generated samples
2107 montecarloSHTVec[0].InitEmpty(bb, gridsize);
2108 // create active cell list
2109 for (VertexIterator vi = montecarloMesh.vert.begin(); vi != montecarloMesh.vert.end(); vi++)
2110 montecarloSHTVec[0].Add(&(*vi));
2111 montecarloSHTVec[0].UpdateAllocatedCells();
2112
2113 // if we are doing variable density sampling we have to prepare the random samples quality with the correct expected radii.
2114 // Temporary, as in PoissonDiskPruning.
2115 PerVertexFloatAttribute rH = tri::Allocator<MeshType>:: template AddPerVertexAttribute<float> (montecarloMesh);
2116 if(pp.adaptiveRadiusFlag)
2117 InitRadiusHandleFromQuality(montecarloMesh, rH, diskRadius, pp.radiusVariance, pp.invertQuality);
2118
2119 do
2120 {
2121 MontecarloSHT &montecarloSHT = montecarloSHTVec[level];
2122
2123 if(level>0)
2124 {// initialize spatial hash with the remaining points
2125 montecarloSHT.InitEmpty(bb, gridsize);
2126 // create active cell list
2127 for (typename MontecarloSHT::HashIterator hi = montecarloSHTVec[level-1].hash_table.begin(); hi != montecarloSHTVec[level-1].hash_table.end(); hi++)
2128 montecarloSHT.Add((*hi).second);
2129 montecarloSHT.UpdateAllocatedCells();
2130 }
2131 // shuffle active cells
2132 //unsigned int (*p_myrandom)(unsigned int) = RandomInt;
2133 std::random_device rd;
2134// std::mt19937 g(rd());
2135// std::shuffle(montecarloSHT.AllocatedCells.begin(),montecarloSHT.AllocatedCells.end(), g);
2137 std::shuffle(montecarloSHT.AllocatedCells.begin(),montecarloSHT.AllocatedCells.end(), g);
2138
2139 // generate a sample inside C by choosing one of the contained pre-generated samples
2141 int removedCnt=montecarloSHT.hash_table.size();
2142 int addedCnt=checkSHT.hash_table.size();
2143 for (int i = 0; i < montecarloSHT.AllocatedCells.size(); i++)
2144 {
2145 for(int j=0;j<4;j++)
2146 {
2147 if( montecarloSHT.EmptyCell(montecarloSHT.AllocatedCells[i]) ) continue;
2148
2149 // generate a sample chosen from the pre-generated one
2150 typename MontecarloSHT::HashIterator hi = montecarloSHT.hash_table.find(montecarloSHT.AllocatedCells[i]);
2151
2152 if(hi==montecarloSHT.hash_table.end()) {break;}
2153 VertexPointer sp = (*hi).second;
2154 // vr spans between 3.0*r and r / 4.0 according to vertex quality
2155 ScalarType sampleRadius = diskRadius;
2156 if(pp.adaptiveRadiusFlag) sampleRadius = rH[sp];
2157 if (checkPoissonDisk(checkSHT, sp->cP(), sampleRadius))
2158 {
2159 ps.AddVert(*sp);
2160 montecarloSHT.RemoveCell(sp);
2161 checkSHT.Add(sp);
2162 break;
2163 }
2164 else
2165 montecarloSHT.RemovePunctual(sp);
2166 }
2167 }
2168 addedCnt = checkSHT.hash_table.size()-addedCnt;
2169 removedCnt = removedCnt-montecarloSHT.hash_table.size();
2170
2171 // proceed to the next level of subdivision
2172 // increase grid resolution
2173 gridsize *= 2;
2174
2175 //
2176 level++;
2177 } while(level < 5);
2179}
2180
2181//template <class MeshType>
2182//void Sampling<MeshType>::SimilarFaceSampling()
2183
2184// This function also generates samples outside faces if those affects faces in texture space.
2185// Use correctSafePointsBaryCoords = true to map safety texels to closest point barycentric coords (on edge)
2186// otherwise obtained samples will map to barycentric coord actually outside face
2187//
2188// If you don't need to get those extra points clear faces Border Flags
2189// vcg::tri::UpdateFlags<Mesh>::FaceClearB(m);
2190//
2191// Else make sure to update border flags from texture space FFadj
2192// vcg::tri::UpdateTopology<Mesh>::FaceFaceFromTexCoord(m);
2193// vcg::tri::UpdateFlags<Mesh>::FaceBorderFromFF(m);
2194static void Texture(MeshType & m, VertexSampler &ps, int textureWidth, int textureHeight, bool correctSafePointsBaryCoords=true)
2195{
2196typedef Point2<ScalarType> Point2x;
2197 //printf("Similar Triangles face sampling\n");
2198 for(FaceIterator fi=m.face.begin(); fi != m.face.end(); fi++)
2199 if (!fi->IsD())
2200 {
2201 Point2x ti[3];
2202 for(int i=0;i<3;++i)
2203 ti[i]=Point2x((*fi).WT(i).U() * textureWidth - 0.5, (*fi).WT(i).V() * textureHeight - 0.5);
2204 // - 0.5 constants are used to obtain correct texture mapping
2205
2206 SingleFaceRaster(*fi, ps, ti[0],ti[1],ti[2], correctSafePointsBaryCoords);
2207 }
2208}
2209
2210typedef GridStaticPtr<FaceType, ScalarType > TriMeshGrid;
2211
2213{
2214public:
2215float offset;
2216float minDiag;
2217tri::FaceTmark<MeshType> markerFunctor;
2218TriMeshGrid gM;
2219};
2220
2221static void RegularRecursiveOffset(MeshType & m, std::vector<CoordType> &pvec, ScalarType offset, float minDiag)
2222{
2223 Box3<ScalarType> bb=m.bbox;
2224 bb.Offset(offset*2.0);
2225
2226 RRParam rrp;
2227
2228 rrp.markerFunctor.SetMesh(&m);
2229
2230 // Let the grid inflate its own box: the explicit-box Set() expects an inflated one,
2231 // and bb is flat for a planar mesh at zero offset, which gave a zero voxel side
2232 // and a NaN-to-int cell index (no samples at all on x86).
2233 rrp.gM.Set(m.face.begin(),m.face.end());
2234
2235
2236 rrp.offset=offset;
2237 rrp.minDiag=minDiag;
2238 SubdivideAndSample(m, pvec, bb, rrp, bb.Diag());
2239}
2240
2241static void SubdivideAndSample(MeshType & m, std::vector<CoordType> &pvec, const Box3<ScalarType> bb, RRParam &rrp, float curDiag)
2242{
2243 CoordType startPt = bb.Center();
2244
2245 ScalarType dist;
2246 // Compute mesh point nearest to bb center
2247 FaceType *nearestF=0;
2248 ScalarType dist_upper_bound = curDiag+rrp.offset;
2249 CoordType closestPt;
2250 vcg::face::PointDistanceBaseFunctor<ScalarType> PDistFunct;
2251 dist=dist_upper_bound;
2252 nearestF = rrp.gM.GetClosest(PDistFunct,rrp.markerFunctor,startPt,dist_upper_bound,dist,closestPt);
2253 curDiag /=2;
2254 if(dist < dist_upper_bound)
2255 {
2256 if(curDiag/3 < rrp.minDiag) //store points only for the last level of recursion (?)
2257 {
2258 if(rrp.offset==0)
2259 pvec.push_back(closestPt);
2260 else
2261 {
2262 if(dist>rrp.offset) // points below the offset threshold cannot be displaced at the right offset distance, we can only make points nearer.
2263 {
2264 CoordType delta = startPt-closestPt;
2265 pvec.push_back(closestPt+delta*(rrp.offset/dist));
2266 }
2267 }
2268 }
2269 if(curDiag < rrp.minDiag) return;
2270 CoordType hs = (bb.max-bb.min)/2;
2271 for(int i=0;i<2;i++)
2272 for(int j=0;j<2;j++)
2273 for(int k=0;k<2;k++)
2274 SubdivideAndSample(m, pvec,
2275 BoxType(CoordType( bb.min[0]+i*hs[0], bb.min[1]+j*hs[1], bb.min[2]+k*hs[2]),
2276 CoordType(startPt[0]+i*hs[0], startPt[1]+j*hs[1], startPt[2]+k*hs[2]) ),
2277 rrp,curDiag
2278 );
2279
2280 }
2281}
2282}; // end sampling class
2283
2284template <class MeshType>
2285typename MeshType::ScalarType ComputePoissonDiskRadius(MeshType &origMesh, int sampleNum)
2286{
2287 typedef typename MeshType::ScalarType ScalarType;
2288 ScalarType meshArea = Stat<MeshType>::ComputeMeshArea(origMesh);
2289 // Manage approximately the PointCloud Case, use the half a area of the bbox.
2290 // TODO: If you had the radius a much better approximation could be done.
2291 if(meshArea ==0)
2292 {
2293 meshArea = (origMesh.bbox.DimX()*origMesh.bbox.DimY() +
2294 origMesh.bbox.DimX()*origMesh.bbox.DimZ() +
2295 origMesh.bbox.DimY()*origMesh.bbox.DimZ());
2296 }
2297 ScalarType diskRadius = sqrt(meshArea / (0.7 * M_PI * sampleNum)); // 0.7 is a density factor
2298 return diskRadius;
2299}
2300
2301
2302
2303template <class MeshType>
2304void MontecarloSampling(MeshType &m, // the mesh that has to be sampled
2305 MeshType &mm, // the mesh that will contain the samples
2306 int sampleNum) // the desired number sample, if zero you must set the radius to the wanted value
2307{
2308 typedef tri::MeshSampler<MeshType> BaseSampler;
2309 MeshSampler<MeshType> mcSampler(&mm);
2311}
2312
2313
2314template <class MeshType>
2315void MontecarloSampling(MeshType &m, // the mesh that has to be sampled
2316 std::vector<Point3f> &montercarloSamples, // the vector that will contain the set of points
2317 int sampleNum) // the desired number sample, if zero you must set the radius to the wanted value
2318{
2319 typedef tri::TrivialSampler<MeshType> BaseSampler;
2320 BaseSampler mcSampler(montercarloSamples);
2322}
2323
2324// Yet another simpler wrapper for the generation of a poisson disk distribution over a mesh.
2325//
2326template <class MeshType>
2327void PoissonSampling(MeshType &m, // the mesh that has to be sampled
2328 std::vector<typename MeshType::CoordType> &poissonSamples, // the vector that will contain the set of points
2329 int sampleNum, // the desired number sample, if zero you must set the radius to the wanted value
2330 typename MeshType::ScalarType &radius, // the Poisson Disk Radius (used if sampleNum==0, setted if sampleNum!=0)
2331 typename MeshType::ScalarType radiusVariance=1,
2332 typename MeshType::ScalarType PruningByNumberTolerance=0.04f,
2333 unsigned int randSeed=0)
2334
2335{
2336 typedef tri::TrivialSampler<MeshType> BaseSampler;
2337 typedef tri::MeshSampler<MeshType> MontecarloSampler;
2338
2339 typename tri::SurfaceSampling<MeshType, BaseSampler>::PoissonDiskParam pp;
2340 int t0=clock();
2341
2342// if(sampleNum>0) radius = tri::SurfaceSampling<MeshType,BaseSampler>::ComputePoissonDiskRadius(m,sampleNum);
2343 if(radius>0 && sampleNum==0) sampleNum = tri::SurfaceSampling<MeshType,BaseSampler>::ComputePoissonSampleNum(m,radius);
2344
2345 pp.pds.sampleNum = sampleNum;
2346 pp.randomSeed = randSeed;
2347 poissonSamples.clear();
2348// std::vector<Point3f> MontecarloSamples;
2349 MeshType MontecarloMesh;
2350
2351 // First step build the sampling
2352 MontecarloSampler mcSampler(MontecarloMesh);
2353 BaseSampler pdSampler(poissonSamples);
2354
2355 if(randSeed) tri::SurfaceSampling<MeshType,MontecarloSampler>::SamplingRandomGenerator().initialize(randSeed);
2356 tri::SurfaceSampling<MeshType,MontecarloSampler>::Montecarlo(m, mcSampler, std::max(10000,sampleNum*40));
2357 tri::UpdateBounding<MeshType>::Box(MontecarloMesh);
2358// tri::Build(MontecarloMesh, MontecarloSamples);
2359 int t1=clock();
2360 pp.pds.montecarloTime = t1-t0;
2361
2362 if(radiusVariance !=1)
2363 {
2364 pp.adaptiveRadiusFlag=true;
2365 pp.radiusVariance=radiusVariance;
2366 }
2367 if(sampleNum==0) tri::SurfaceSampling<MeshType,BaseSampler>::PoissonDiskPruning(pdSampler, MontecarloMesh, radius,pp);
2368 else tri::SurfaceSampling<MeshType,BaseSampler>::PoissonDiskPruningByNumber(pdSampler, MontecarloMesh, sampleNum, radius,pp,PruningByNumberTolerance);
2369 int t2=clock();
2370 pp.pds.totalTime = t2-t0;
2371}
2372
2376//
2377template <class MeshType>
2378void PoissonPruning(MeshType &m, // the mesh that has to be pruned
2379 std::vector<typename MeshType::VertexPointer> &poissonSamples, // the vector that will contain the chosen set of points
2380 float radius, unsigned int randSeed=0)
2381{
2382 typedef tri::TrivialPointerSampler<MeshType> BaseSampler;
2384 pp.randomSeed = randSeed;
2385
2387 BaseSampler pdSampler;
2389 poissonSamples = pdSampler.sampleVec;
2390}
2391
2392
2398template <class MeshType>
2399void PoissonPruning(MeshType &m, // the mesh that has to be pruned
2400 std::vector<typename MeshType::CoordType> &poissonSamples, // the vector that will contain the chosen set of points
2401 float radius, unsigned int randSeed=0)
2402{
2403 std::vector<typename MeshType::VertexPointer> poissonSamplesVP;
2404 PoissonPruning(m,poissonSamplesVP,radius,randSeed);
2405 poissonSamples.resize(poissonSamplesVP.size());
2406 for(size_t i=0;i<poissonSamplesVP.size();++i)
2407 poissonSamples[i]=poissonSamplesVP[i]->P();
2408}
2409
2410
2411
2417template <class MeshType>
2418void PoissonPruningExact(MeshType &m,
2419 std::vector<typename MeshType::VertexPointer> &poissonSamples,
2420 typename MeshType::ScalarType & radius,
2421 int sampleNum,
2422 float tolerance=0.04,
2423 int maxIter=20,
2424 unsigned int randSeed=0)
2425{
2426 size_t sampleNumMin = int(float(sampleNum)*(1.0f-tolerance)); // the expected values range.
2427 size_t sampleNumMax = int(float(sampleNum)*(1.0f+tolerance)); // e.g. any sampling in [sampleNumMin, sampleNumMax] is OK
2428 float RangeMinRad = m.bbox.Diag()/10.0f;
2429 float RangeMaxRad = m.bbox.Diag()/10.0f;
2430 size_t RangeMinSampleNum;
2431 size_t RangeMaxSampleNum;
2432 std::vector<typename MeshType::VertexPointer> poissonSamplesTmp;
2433
2434 do
2435 {
2436 RangeMinRad/=2.0f;
2437 PoissonPruning(m,poissonSamplesTmp,RangeMinRad,randSeed);
2438 RangeMinSampleNum = poissonSamplesTmp.size();
2439 } while(RangeMinSampleNum < sampleNumMin);
2440
2441 do
2442 {
2443 RangeMaxRad*=2.0f;
2444 PoissonPruning(m,poissonSamplesTmp,RangeMaxRad,randSeed);
2445 RangeMaxSampleNum = poissonSamplesTmp.size();
2446 } while(RangeMaxSampleNum > sampleNumMax);
2447
2448 float curRadius;
2449 int iterCnt=0;
2450 while(iterCnt<maxIter &&
2451 (poissonSamplesTmp.size() < sampleNumMin || poissonSamplesTmp.size() > sampleNumMax) )
2452 {
2453 curRadius=(RangeMaxRad+RangeMinRad)/2.0f;
2454 PoissonPruning(m,poissonSamplesTmp,curRadius,randSeed);
2455 //qDebug("(%6.3f:%5i %6.3f:%5i) Cur Radius %f -> %i sample instead of %i",RangeMinRad,RangeMinSampleNum,RangeMaxRad,RangeMaxSampleNum,curRadius,poissonSamplesTmp.size(),sampleNum);
2456 if(poissonSamplesTmp.size() > size_t(sampleNum))
2457 RangeMinRad = curRadius;
2458 if(poissonSamplesTmp.size() < size_t(sampleNum))
2459 RangeMaxRad = curRadius;
2460 }
2461
2462 swap(poissonSamples,poissonSamplesTmp);
2463 radius = curRadius;
2464}
2465} // end namespace tri
2466} // end namespace vcg
2467
2468#endif
2469
Definition: box3.h:42
Point3< BoxScalarType > max
max coordinate point
Definition: box3.h:51
Point3< BoxScalarType > Center() const
Return the center of the box.
Definition: box3.h:250
void Offset(const BoxScalarType s)
Definition: box3.h:75
Point3< BoxScalarType > min
min coordinate point
Definition: box3.h:49
BoxScalarType Diag() const
Return the lenght of the diagonal of the box .
Definition: box3.h:240
Class to safely add and delete elements in a mesh.
Definition: allocate.h:99
static VertexIterator AddVertices(MeshType &m, size_t n, PointerUpdater< VertexPointer > &pu)
Add n vertices to the mesh. Function to add n vertices to the mesh. The elements are added always to ...
Definition: allocate.h:197
static void DeletePerVertexAttribute(MeshType &m, typename MeshType::template PerVertexAttributeHandle< ATTR_TYPE > &h)
If the per-vertex attribute exists, delete it.
Definition: allocate.h:1660
Definition: point_sampling.h:230
double volume
from the wikipedia defintion RMS DIST is sqrt(Sum(distances^2)/n), here we store Sum(distances^2)
Definition: point_sampling.h:258
MeshType * closestPtMesh
the mesh containing the sample points
Definition: point_sampling.h:248
MeshType * samplePtMesh
the mesh for which we search the closest points.
Definition: point_sampling.h:247
MetroMeshVertexGrid unifGridVert
the mesh containing the corresponding closest points that have been found
Definition: point_sampling.h:250
Definition: point_sampling.h:177
Definition: point_sampling.h:363
CallBackPos * cb
the source mesh for which we search the closest points (e.g. the mesh from which we take colors etc).
Definition: point_sampling.h:376
Definition: point_sampling.h:531
Definition: point_sampling.h:2213
Main Class of the Sampling framework.
Definition: point_sampling.h:476
static void Montecarlo(MeshType &m, VertexSampler &ps, int sampleNum)
Definition: point_sampling.h:1207
static void HierarchicalPoissonDisk(MeshType &origMesh, VertexSampler &ps, MeshType &montecarloMesh, ScalarType diskRadius, const struct PoissonDiskParam pp=PoissonDiskParam())
Definition: point_sampling.h:2067
static void VertexAreaUniform(MeshType &m, VertexSampler &ps, int sampleNum)
Definition: point_sampling.h:725
static void VertexWeighted(MeshType &m, VertexSampler &ps, int sampleNum)
Definition: point_sampling.h:687
static void VertexCrease(MeshType &m, VertexSampler &ps)
Definition: point_sampling.h:984
static void VertexUniform(MeshType &m, VertexSampler &ps, int sampleNum, bool onlySelected)
Sample the vertices in a uniform way. Each vertex has the same probabiltiy of being chosen.
Definition: point_sampling.h:773
static void FaceSubdivision(MeshType &m, VertexSampler &ps, int sampleNum, bool randSample)
Compute a sampling of the surface where the points are regularly scattered over the face surface usin...
Definition: point_sampling.h:1347
static void PoissonDiskPruning(VertexSampler &ps, MeshType &montecarloMesh, ScalarType diskRadius, PoissonDiskParam &pp)
Definition: point_sampling.h:1972
static ScalarType ComputePoissonDiskRadius(MeshType &origMesh, int sampleNum)
Estimate the radius r that you should give to get a certain number of samples in a Poissson Disk Dist...
Definition: point_sampling.h:1826
EdgeSamplingRoundingStrategy
The EdgeSamplingStrategy enum determines the sampling strategy for edge meshes. Given a sampling radi...
Definition: point_sampling.h:808
static void VertexBorder(MeshType &m, VertexSampler &ps)
Sample all the border vertices.
Definition: point_sampling.h:973
static void VertexBorderCorner(MeshType &m, VertexSampler &ps, ScalarType angleRad)
Sample all the border corner vertices.
Definition: point_sampling.h:959
static void EdgeMeshUniform(MeshType &m, VertexSampler &ps, float radius, EdgeSamplingRoundingStrategy strategy=Floor)
Definition: point_sampling.h:830
static void FaceSubdivisionOld(MeshType &m, VertexSampler &ps, int sampleNum, bool randSample)
Compute a sampling of the surface where the points are regularly scattered over the face surface usin...
Definition: point_sampling.h:1438
static int Poisson(double lambda)
Definition: point_sampling.h:647
static void MontecarloPoisson(MeshType &m, VertexSampler &ps, int sampleNum)
Definition: point_sampling.h:1139
static void WeightedMontecarlo(MeshType &m, VertexSampler &ps, int sampleNum, float variance)
Definition: point_sampling.h:1255
static void InitRadiusHandleFromQuality(MeshType &sampleMesh, PerVertexFloatAttribute &rH, ScalarType diskRadius, ScalarType radiusVariance, bool invert)
Definition: point_sampling.h:1855
static void EdgeMontecarlo(MeshType &m, VertexSampler &ps, int sampleNum, bool sampleAllEdges)
Definition: point_sampling.h:1165
Definition: point_sampling.h:137
A basic sampler class that show the required interface used by the SurfaceSampling class.
Definition: point_sampling.h:72
static void Box(ComputeMeshType &m)
Calculates the bounding box of the given mesh m.
Definition: bounding.h:45
Management, updating and computation of per-vertex and per-face flags (like border flags).
Definition: flag.h:44
static void PerFaceNormalized(ComputeMeshType &m)
Equivalent to PerFace() and NormalizePerFace()
Definition: normal.h:276
static size_t VertexCornerBorder(MeshType &m, ScalarType angleRad, bool preserveSelection=false)
Select the border vertices that form a corner along the border with an angle that is below a certain ...
Definition: selection.h:656
Auxiliary data structure for computing face face adjacency information.
Definition: topology.h:149
Generation of per-vertex and per-face topological information.
Definition: topology.h:43
void PoissonPruningExact(MeshType &m, std::vector< typename MeshType::VertexPointer > &poissonSamples, typename MeshType::ScalarType &radius, int sampleNum, float tolerance=0.04, int maxIter=20, unsigned int randSeed=0)
Very simple wrapping for the Exact Poisson Disk Pruning.
Definition: point_sampling.h:2418
void PoissonPruning(MeshType &m, std::vector< typename MeshType::VertexPointer > &poissonSamples, float radius, unsigned int randSeed=0)
Low level wrapper for Poisson Disk Pruning.
Definition: point_sampling.h:2378
Definition: color4.h:30
Definition: point_sampling.h:1762
Definition: point_sampling.h:1746