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>
29#include <vclib/mesh.h>
30#include <vclib/space/complex.h>
73template<SamplerConcept SamplerType, MeshConcept MeshType>
79 using VertexType = MeshType::VertexType;
93 for (
const VertexType& v :
m.vertices()) {
121template<SamplerConcept SamplerType, MeshConcept MeshType>
156template<SamplerConcept SamplerType, FaceMeshConcept MeshType>
162 using FaceType = MeshType::FaceType;
174 for (
const FaceType& f :
m.faces()) {
206template<SamplerConcept SamplerType, FaceMeshConcept MeshType>
233template<SamplerConcept SamplerType, MeshConcept MeshType>
239 std::optional<uint>
seed = std::nullopt)
255 std::uniform_int_distribution<uint> dist(0,
m.vertexContainerSize() - 1);
259 std::vector<bool> visited(
m.vertexContainerSize(),
false);
264 if (!
m.vertex(
vi).deleted() && !visited[
vi] &&
294template<SamplerConcept SamplerType, MeshConcept MeshType>
299 std::optional<uint>
seed = std::nullopt)
331template<SamplerConcept SamplerType, FaceMeshConcept MeshType>
337 std::optional<uint>
seed = std::nullopt)
352 std::uniform_int_distribution<uint> dist(0,
m.faceContainerSize() - 1);
356 std::vector<bool> visited(
m.faceContainerSize(),
false);
361 if (!
m.face(
fi).deleted() && !visited[
fi] &&
395template<SamplerConcept SamplerType, FaceMeshConcept MeshType>
400 std::optional<uint>
seed = std::nullopt)
432template<SamplerConcept SamplerType, MeshConcept MeshType,
typename ScalarType>
435 const std::vector<ScalarType>&
weights,
438 std::optional<uint>
seed = std::nullopt)
449 std::discrete_distribution<> dist(std::begin(
weights), std::end(
weights));
453 std::vector<bool> visited(
m.vertexContainerSize(),
false);
458 if (
vi <
m.vertexContainerSize() && !
m.vertex(
vi).deleted() &&
492template<SamplerConcept SamplerType, MeshConcept MeshType,
typename ScalarType>
495 const std::vector<ScalarType>&
weights,
497 std::optional<uint>
seed = std::nullopt)
533 SamplerConcept SamplerType,
534 FaceMeshConcept MeshType,
538 const std::vector<ScalarType>&
weights,
541 std::optional<uint>
seed = std::nullopt)
554 std::discrete_distribution<> dist(std::begin(
weights), std::end(
weights));
558 std::vector<bool> visited(
m.faceContainerSize(),
false);
563 if (
fi <
m.faceContainerSize() && !
m.face(
fi).deleted() &&
600 SamplerConcept SamplerType,
601 FaceMeshConcept MeshType,
605 const std::vector<ScalarType>&
weights,
607 std::optional<uint>
seed = std::nullopt)
627template<SamplerConcept SamplerType, MeshConcept MeshType>
631 std::optional<uint>
seed = std::nullopt)
633 requirePerVertexQuality(
m);
635 using VertexType = MeshType::VertexType;
636 using QualityType = VertexType::QualityType;
638 std::vector<QualityType>
weights;
639 weights.resize(
m.vertexContainerSize(), 0);
640 for (
const VertexType& v :
m.vertices()) {
660template<SamplerConcept SamplerType, FaceMeshConcept MeshType>
664 std::optional<uint>
seed = std::nullopt)
668 using FaceType = MeshType::FaceType;
669 using QualityType = FaceType::QualityType;
671 std::vector<QualityType>
weights;
672 weights.resize(
m.faceContainerSize(), 0);
673 for (
const FaceType& f :
m.faces()) {
693template<SamplerConcept SamplerType, FaceMeshConcept MeshType>
697 std::optional<uint>
seed = std::nullopt)
699 using VertexType = MeshType::VertexType;
700 using ScalarType = VertexType::ScalarType;
701 using FaceType = MeshType::FaceType;
703 std::vector<ScalarType>
weights(
m.vertexContainerSize(), 0);
704 std::vector<uint> cnt(
m.vertexContainerSize(), 0);
708 for (
const FaceType& f :
m.faces()) {
710 for (
const VertexType* v : f.vertices()) {
738template<SamplerConcept SamplerType, FaceMeshConcept MeshType>
742 std::optional<uint>
seed = std::nullopt)
744 using FaceType = MeshType::FaceType;
746 std::vector<double>
weights(
m.faceContainerSize());
748 for (
const FaceType& f :
m.faces()) {
776template<SamplerConcept SamplerType, FaceMeshConcept MeshType>
781 std::optional<uint>
seed = std::nullopt)
783 using VertexType = MeshType::VertexType;
784 using ScalarType = VertexType::PositionType::ScalarType;
785 using FaceType = MeshType::FaceType;
786 using Interval = std::pair<ScalarType, const FaceType*>;
796 std::uniform_real_distribution<ScalarType> dist(0, 1);
800 std::vector<Interval>
intervals(
m.faceNumber());
803 for (
const FaceType& f :
m.faces()) {
816 typename std::vector<Interval>::iterator
it = std::lower_bound(
819 std::make_pair(
val,
nullptr),
825 it->second->vertexNumber(),
gen));
849template<SamplerConcept SamplerType, FaceMeshConcept MeshType>
853 std::optional<uint>
seed = std::nullopt)
859template<SamplerConcept SamplerType, FaceMeshConcept MeshType>
860SamplerType stratifiedMontecarloPointSampling(
863 std::optional<uint> seed = std::nullopt)
865 using FaceType = MeshType::FaceType;
866 using ScalarType = SamplerType::ScalarType;
872 double area = surfaceArea(m);
873 double samplePerAreaUnit = nSamples / area;
875 double floatSampleNum = 0.0;
877 for (
const FaceType& f : m.faces()) {
880 floatSampleNum +=
faceArea(f) * samplePerAreaUnit;
881 int faceSampleNum = (int) floatSampleNum;
883 for (
int i = 0; i < faceSampleNum; i++)
886 randomPolygonBarycentricCoordinate<ScalarType>(
887 f.vertexNumber(), gen));
888 floatSampleNum -= (double) faceSampleNum;
917template<SamplerConcept SamplerType, FaceMeshConcept MeshType>
921 std::optional<uint>
seed = std::nullopt)
923 using FaceType = MeshType::FaceType;
924 using ScalarType = SamplerType::ScalarType;
930 ScalarType area = surfaceArea(
m);
933 for (
const FaceType& f :
m.faces()) {
942 f.vertexNumber(),
gen));
949 SamplerConcept SamplerType,
950 FaceMeshConcept MeshType,
952SamplerType vertexWeightedMontecarloPointSampling(
954 const std::vector<ScalarType>& weights,
957 std::optional<uint> seed = std::nullopt)
959 using FaceType = MeshType::FaceType;
962 auto weightedArea = [](
const FaceType& f,
964 const std::vector<ScalarType>& r) -> ScalarType {
965 ScalarType averageQ = 0;
966 for (uint i = 0; i < f.vertexNumber(); ++i)
967 averageQ += r[m.index(f.vertex(i))];
969 averageQ /= f.vertexNumber();
970 return averageQ * averageQ *
faceArea(f);
977 std::vector<ScalarType> radius =
978 vertexRadiusFromWeights(m, weights, 1.0, variance,
true);
980 ScalarType wArea = 0;
981 for (
const FaceType& f : m.
faces())
982 wArea += weightedArea(f, m, radius);
984 ScalarType samplePerAreaUnit = nSamples / wArea;
986 double floatSampleNum = 0.0;
987 for (
const FaceType& f : m.
faces()) {
990 floatSampleNum += weightedArea(f, m, radius) * samplePerAreaUnit;
991 uint faceSampleNum = (uint) floatSampleNum;
994 for (uint i = 0; i < faceSampleNum; i++)
997 randomPolygonBarycentricCoordinate<ScalarType>(
998 f.vertexNumber(), gen));
1000 floatSampleNum -= (double) faceSampleNum;
1006template<SamplerConcept SamplerType, FaceMeshConcept MeshType>
1007SamplerType vertexQualityWeightedMontecarloPointSampling(
1011 std::optional<uint> seed = std::nullopt)
1013 requirePerVertexQuality(m);
1015 using VertexType = MeshType::VertexType;
1016 using QualityType = VertexType::QualityType;
1018 std::vector<QualityType> weights;
1019 weights.resize(m.vertexContainerSize(), 0);
1020 for (
const VertexType& v : m.
vertices()) {
1021 weights[m.index(v)] = v.quality();
1024 return vertexWeightedMontecarloPointSampling<SamplerType>(
1025 m, weights, nSamples, variance, seed);
A class representing a box in N-dimensional space.
Definition box.h:46
void add(const PointT &p)
Adds the given point to the current box, expanding this box in order to contain also the values of th...
Definition box.h:385
PointT size() const
Computes the size of the box.
Definition box.h:267
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:121
std::mt19937 randomGenerator(std::optional< uint > seed=std::nullopt)
Creates a random number generator with an optional seed.
Definition random.h:45
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:108
void requirePerFaceQuality(const MeshType &m)
This function asserts that a Mesh has a FaceContainer, the Face has a Quality Component,...
Definition face_requirements.h:1137
SamplerType montecarloPoissonPointSampling(const MeshType &m, uint nSamples, std::optional< uint > seed=std::nullopt)
This function compute montecarlo distribution with an approximate number of samples exploiting the po...
Definition point_sampling.h:918
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:157
SamplerType faceAreaWeightedPointSampling(const MeshType &m, uint nSamples, std::optional< uint > seed=std::nullopt)
Samples the faces in a weighted way, using the per face area. Each face has a probability of being ch...
Definition point_sampling.h:739
SamplerType vertexWeightedPointSampling(const MeshType &m, const std::vector< ScalarType > &weights, uint nSamples, std::vector< uint > &birthVertices, std::optional< uint > seed=std::nullopt)
Samples the vertices in a weighted way, using the per vertex weights given as input....
Definition point_sampling.h:433
SamplerType vertexUniformPointSampling(const MeshType &m, uint nSamples, std::vector< uint > &birthVertices, bool onlySelected=false, std::optional< uint > seed=std::nullopt)
Returns a SamplerType object that contains the given number of samples taken from the vertices of the...
Definition point_sampling.h:234
SamplerType faceWeightedPointSampling(const MeshType &m, const std::vector< ScalarType > &weights, uint nSamples, std::vector< uint > &birthFaces, std::optional< uint > seed=std::nullopt)
Returns a SamplerType object that contains the given number of samples taken from the faces of the gi...
Definition point_sampling.h:536
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:74
SamplerType vertexAreaWeightedPointSampling(const MeshType &m, uint nSamples, std::optional< uint > seed=std::nullopt)
Samples the vertices in a weighted way, using the area. Each vertex has a probability of being chosen...
Definition point_sampling.h:694
SamplerType vertexQualityWeightedPointSampling(const MeshType &m, uint nSamples, std::optional< uint > seed=std::nullopt)
Samples the vertices in a weighted way, using the per vertex Quality component. Each vertex has a pro...
Definition point_sampling.h:628
SamplerType faceQualityWeightedPointSampling(const MeshType &m, uint nSamples, std::optional< uint > seed=std::nullopt)
Samples the faces in a weighted way, using the per face Quality component. Each face has a probabilit...
Definition point_sampling.h:661
SamplerType montecarloPointSampling(const MeshType &m, uint nSamples, std::vector< uint > &birthFaces, std::optional< uint > seed=std::nullopt)
Computes a montecarlo distribution with an exact number of samples. It works by generating a sequence...
Definition point_sampling.h:777
SamplerType faceUniformPointSampling(const MeshType &m, uint nSamples, std::vector< uint > &birthFaces, bool onlySelected=false, std::optional< uint > seed=std::nullopt)
Returns a SamplerType object that contains the given number of samples taken from the faces of the gi...
Definition point_sampling.h:332
constexpr detail::FacesView faces
A view that allows to iterate overt the Face elements of an object.
Definition face.h:84
constexpr detail::VerticesView vertices
A view that allows to iterate over the Vertex elements of an object.
Definition vertex.h:92