23#ifndef VCL_ALGORITHMS_MESH_POINT_SAMPLING_H
24#define VCL_ALGORITHMS_MESH_POINT_SAMPLING_H
26#include <vclib/algorithms/mesh/shuffle.h>
27#include <vclib/algorithms/mesh/stat.h>
28#include <vclib/math/random.h>
29#include <vclib/mesh/requirements.h>
30#include <vclib/misc/comparators.h>
31#include <vclib/space/complex/sampler.h>
74template<SamplerConcept SamplerType, MeshConcept MeshType>
80 using VertexType = MeshType::VertexType;
94 for (
const VertexType& v :
m.vertices()) {
122template<SamplerConcept SamplerType, MeshConcept MeshType>
157template<SamplerConcept SamplerType, FaceMeshConcept MeshType>
163 using FaceType = MeshType::FaceType;
175 for (
const FaceType& f :
m.faces()) {
207template<SamplerConcept SamplerType, FaceMeshConcept MeshType>
233template<SamplerConcept SamplerType, MeshConcept MeshType>
255 std::uniform_int_distribution<uint> dist(0,
m.vertexContainerSize() - 1);
257 std::random_device
rd;
258 std::mt19937
gen(
rd());
260 gen = std::mt19937(0);
262 std::vector<bool> visited(
m.vertexContainerSize(),
false);
267 if (!
m.vertex(
vi).deleted() && !visited[
vi] &&
271 ps.add(
m.vertex(
vi));
296template<SamplerConcept SamplerType, MeshConcept MeshType>
332template<SamplerConcept SamplerType, FaceMeshConcept MeshType>
353 std::uniform_int_distribution<uint> dist(0,
m.faceContainerSize() - 1);
355 std::random_device
rd;
356 std::mt19937
gen(
rd());
358 gen = std::mt19937(0);
360 std::vector<bool> visited(
m.faceContainerSize(),
false);
365 if (!
m.face(
fi).deleted() && !visited[
fi] &&
398template<SamplerConcept SamplerType, FaceMeshConcept MeshType>
435template<SamplerConcept SamplerType, MeshConcept MeshType,
typename ScalarType>
438 const std::vector<ScalarType>&
weights,
452 std::discrete_distribution<> dist(std::begin(
weights), std::end(
weights));
454 std::random_device
rd;
455 std::mt19937
gen(
rd());
457 gen = std::mt19937(0);
459 std::vector<bool> visited(
m.vertexContainerSize(),
false);
464 if (
vi <
m.vertexContainerSize() && !
m.vertex(
vi).deleted() &&
468 ps.add(
m.vertex(
vi));
498template<SamplerConcept SamplerType, MeshConcept MeshType,
typename ScalarType>
501 const std::vector<ScalarType>&
weights,
538 SamplerConcept SamplerType,
539 FaceMeshConcept MeshType,
543 const std::vector<ScalarType>&
weights,
559 std::discrete_distribution<> dist(std::begin(
weights), std::end(
weights));
561 std::random_device
rd;
562 std::mt19937
gen(
rd());
564 gen = std::mt19937(0);
566 std::vector<bool> visited(
m.faceContainerSize(),
false);
571 if (
fi <
m.faceContainerSize() && !
m.face(
fi).deleted() &&
607 SamplerConcept SamplerType,
608 FaceMeshConcept MeshType,
612 const std::vector<ScalarType>&
weights,
633template<SamplerConcept SamplerType, MeshConcept MeshType>
639 requirePerVertexQuality(
m);
641 using VertexType = MeshType::VertexType;
642 using QualityType = VertexType::QualityType;
644 std::vector<QualityType>
weights;
645 weights.resize(
m.vertexContainerSize(), 0);
646 for (
const VertexType& v :
m.vertices()) {
666template<SamplerConcept SamplerType, FaceMeshConcept MeshType>
674 using FaceType = MeshType::FaceType;
675 using QualityType = FaceType::QualityType;
677 std::vector<QualityType>
weights;
678 weights.resize(
m.faceContainerSize(), 0);
679 for (
const FaceType& f :
m.faces()) {
699template<SamplerConcept SamplerType, FaceMeshConcept MeshType>
705 using VertexType = MeshType::VertexType;
706 using ScalarType = VertexType::ScalarType;
707 using FaceType = MeshType::FaceType;
709 std::vector<ScalarType>
weights(
m.vertexContainerSize(), 0);
710 std::vector<uint> cnt(
m.vertexContainerSize(), 0);
714 for (
const FaceType& f :
m.faces()) {
716 for (
const VertexType* v : f.vertices()) {
744template<SamplerConcept SamplerType, FaceMeshConcept MeshType>
750 using FaceType = MeshType::FaceType;
752 std::vector<double>
weights(
m.faceContainerSize());
754 for (
const FaceType& f :
m.faces()) {
781template<SamplerConcept SamplerType, FaceMeshConcept MeshType>
788 using VertexType = MeshType::VertexType;
789 using ScalarType = VertexType::CoordType::ScalarType;
790 using FaceType = MeshType::FaceType;
791 using Interval = std::pair<ScalarType, const FaceType*>;
801 std::uniform_real_distribution<ScalarType> dist(0, 1);
803 std::random_device
rd;
804 std::mt19937
gen(
rd());
806 gen = std::mt19937(0);
808 std::vector<Interval>
intervals(
m.faceNumber());
811 for (
const FaceType& f :
m.faces()) {
824 typename std::vector<Interval>::iterator
it = std::lower_bound(
827 std::make_pair(
val,
nullptr),
833 it->second->vertexNumber(),
gen));
856template<SamplerConcept SamplerType, FaceMeshConcept MeshType>
867template<SamplerConcept SamplerType, FaceMeshConcept MeshType>
868SamplerType stratifiedMontecarloPointSampling(
871 bool deterministic =
false)
873 using FaceType = MeshType::FaceType;
874 using ScalarType = SamplerType::ScalarType;
878 std::random_device rd;
879 std::mt19937 gen(rd());
881 gen = std::mt19937(0);
883 double area = surfaceArea(m);
884 double samplePerAreaUnit = nSamples / area;
886 double floatSampleNum = 0.0;
888 for (
const FaceType& f : m.faces()) {
891 floatSampleNum +=
faceArea(f) * samplePerAreaUnit;
892 int faceSampleNum = (int) floatSampleNum;
894 for (
int i = 0; i < faceSampleNum; i++)
897 randomPolygonBarycentricCoordinate<ScalarType>(
898 f.vertexNumber(), gen));
899 floatSampleNum -= (double) faceSampleNum;
927template<SamplerConcept SamplerType, FaceMeshConcept MeshType>
933 using FaceType = MeshType::FaceType;
934 using ScalarType = SamplerType::ScalarType;
938 std::random_device
rd;
939 std::mt19937
gen(
rd());
941 gen = std::mt19937(0);
943 ScalarType area = surfaceArea(
m);
946 for (
const FaceType& f :
m.faces()) {
955 f.vertexNumber(),
gen));
962 SamplerConcept SamplerType,
963 FaceMeshConcept MeshType,
965SamplerType vertexWeightedMontecarloPointSampling(
967 const std::vector<ScalarType>& weights,
970 bool deterministic =
false)
972 using FaceType = MeshType::FaceType;
975 auto weightedArea = [](
const FaceType& f,
977 const std::vector<ScalarType>& r) -> ScalarType {
978 ScalarType averageQ = 0;
979 for (uint i = 0; i < f.vertexNumber(); ++i)
980 averageQ += r[m.index(f.vertex(i))];
982 averageQ /= f.vertexNumber();
983 return averageQ * averageQ *
faceArea(f);
988 std::random_device rd;
989 std::mt19937 gen(rd());
991 gen = std::mt19937(0);
993 std::vector<ScalarType> radius =
994 vertexRadiusFromWeights(m, weights, 1.0, variance,
true);
996 ScalarType wArea = 0;
997 for (
const FaceType& f : m.
faces())
998 wArea += weightedArea(f, m, radius);
1000 ScalarType samplePerAreaUnit = nSamples / wArea;
1002 double floatSampleNum = 0.0;
1003 for (
const FaceType& f : m.
faces()) {
1006 floatSampleNum += weightedArea(f, m, radius) * samplePerAreaUnit;
1007 uint faceSampleNum = (uint) floatSampleNum;
1010 for (uint i = 0; i < faceSampleNum; i++)
1013 randomPolygonBarycentricCoordinate<ScalarType>(
1014 f.vertexNumber(), gen));
1016 floatSampleNum -= (double) faceSampleNum;
1022template<SamplerConcept SamplerType, FaceMeshConcept MeshType>
1023SamplerType vertexQualityWeightedMontecarloPointSampling(
1027 bool deterministic =
false)
1029 requirePerVertexQuality(m);
1031 using VertexType = MeshType::VertexType;
1032 using QualityType = VertexType::QualityType;
1034 std::vector<QualityType> weights;
1035 weights.resize(m.vertexContainerSize(), 0);
1036 for (
const VertexType& v : m.
vertices()) {
1037 weights[m.index(v)] = v.quality();
1040 return vertexWeightedMontecarloPointSampling<SamplerType>(
1041 m, weights, nSamples, variance, deterministic);
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
auto faceArea(const FaceType &f)
Computes the area of a face. Works both for triangle and polygonal faces, and it is optimized in case...
Definition geometry.h:101
void requirePerFaceQuality(const MeshType &m)
This function asserts that a Mesh has a FaceContainer, the Face has a Quality Component,...
Definition face_requirements.h:875
int poissonRandomNumber(double lambda, std::mt19937 &gen)
algorithm poisson random number (Knuth): init: Let L ← e^−λ, k ← 0 and p ← 1. do: k ← k + 1....
Definition random.h:118
SamplerType vertexQualityWeightedPointSampling(const MeshType &m, uint nSamples, bool deterministic=false)
Samples the vertices in a weighted way, using the per vertex Quality component. Each vertex has a pro...
Definition point_sampling.h:634
SamplerType allFacesPointSampling(const MeshType &m, std::vector< uint > &birthFaces, bool onlySelected=false)
Returns a SamplerType object that contains all the faces contained in the given mesh.
Definition point_sampling.h:158
SamplerType montecarloPointSampling(const MeshType &m, uint nSamples, std::vector< uint > &birthFaces, bool deterministic=false)
Computes a montecarlo distribution with an exact number of samples. It works by generating a sequence...
Definition point_sampling.h:782
SamplerType faceWeightedPointSampling(const MeshType &m, const std::vector< ScalarType > &weights, uint nSamples, std::vector< uint > &birthFaces, bool deterministic=false)
Returns a SamplerType object that contains the given number of samples taken from the faces of the gi...
Definition point_sampling.h:541
SamplerType vertexWeightedPointSampling(const MeshType &m, const std::vector< ScalarType > &weights, uint nSamples, std::vector< uint > &birthVertices, bool deterministic=false)
Samples the vertices in a weighted way, using the per vertex weights given as input....
Definition point_sampling.h:436
SamplerType vertexAreaWeightedPointSampling(const MeshType &m, uint nSamples, bool deterministic=false)
Samples the vertices in a weighted way, using the area. Each vertex has a probability of being chosen...
Definition point_sampling.h:700
SamplerType allVerticesPointSampling(const MeshType &m, std::vector< uint > &birthVertices, bool onlySelected=false)
Returns a Sampler object that contains all the vertices contained in the given mesh.
Definition point_sampling.h:75
SamplerType vertexUniformPointSampling(const MeshType &m, uint nSamples, std::vector< uint > &birthVertices, bool onlySelected=false, bool deterministic=false)
Returns a SamplerType object that contains the given number of samples taken from the vertices of the...
Definition point_sampling.h:234
SamplerType faceAreaWeightedPointSampling(const MeshType &m, uint nSamples, bool deterministic=false)
Samples the faces in a weighted way, using the per face area. Each face has a probability of being ch...
Definition point_sampling.h:745
SamplerType montecarloPoissonPointSampling(const MeshType &m, uint nSamples, bool deterministic=false)
This function compute montecarlo distribution with an approximate number of samples exploiting the po...
Definition point_sampling.h:928
SamplerType faceUniformPointSampling(const MeshType &m, uint nSamples, std::vector< uint > &birthFaces, bool onlySelected=false, bool deterministic=false)
Returns a SamplerType object that contains the given number of samples taken from the faces of the gi...
Definition point_sampling.h:333
SamplerType faceQualityWeightedPointSampling(const MeshType &m, uint nSamples, bool deterministic=false)
Samples the faces in a weighted way, using the per face Quality component. Each face has a probabilit...
Definition point_sampling.h:667
constexpr detail::FacesView faces
A view that allows to iterate overt the Face elements of an object.
Definition face.h:52
constexpr detail::VerticesView vertices
A view that allows to iterate over the Vertex elements of an object.
Definition vertex.h:60