Visual Computing Library
Loading...
Searching...
No Matches
point_sampler.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
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 Mozilla Public License Version 2.0 as published *
13 * by the Mozilla 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 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_SPACE_COMPLEX_SAMPLER_POINT_SAMPLER_H
24#define VCL_SPACE_COMPLEX_SAMPLER_POINT_SAMPLER_H
25
26#include <vclib/algorithms/core/polygon.h>
27#include <vclib/concepts/mesh/elements/edge.h>
28#include <vclib/concepts/mesh/elements/face.h>
29#include <vclib/concepts/mesh/elements/vertex.h>
30#include <vclib/concepts/space/sampler.h>
31
32namespace vcl {
33
34template<PointConcept PointT = Point3d>
36{
37 std::vector<PointT> mSamples;
38
39public:
40 using PointType = PointT;
41 using ScalarType = PointT::ScalarType;
42 using ConstIterator = std::vector<PointT>::const_iterator;
43
44 PointSampler() = default;
45
46 const std::vector<PointT>& samples() const { return mSamples; }
47
48 const PointT& sample(uint i) const { return mSamples[i]; }
49
50 std::size_t size() const { return mSamples.size(); }
51
52 void clear() { mSamples.clear(); }
53
54 void reserve(uint n) { mSamples.reserve(n); }
55
56 void resize(uint n) { mSamples.resize(n); }
57
58 void add(const PointT& p) { mSamples.push_back(p); }
59
60 void set(uint i, const PointT& p) { mSamples[i] = p; }
61
62 template<VertexConcept VertexType>
63 void add(const VertexType& v)
64 {
65 mSamples.push_back(v.coord());
66 }
67
68 template<VertexConcept VertexType>
69 void set(uint i, const VertexType& v)
70 {
71 mSamples[i] = v.coord();
72 }
73
74 template<EdgeConcept EdgeType>
75 void add(const EdgeType& e, double u = 0.5)
76 {
77 mSamples.push_back(
78 (e.vertex(0).coord() * (1 - u)) + (e.vertex(1).coord() * u));
79 }
80
81 template<EdgeConcept EdgeType>
82 void set(uint i, const EdgeType& e, double u = 0.5)
83 {
84 mSamples[i] =
85 (e.vertex(0).coord() * (1 - u)) + (e.vertex(1).coord() * u);
86 }
87
88 template<FaceConcept FaceType>
89 void add(const FaceType& f)
90 {
91 mSamples.push_back(faceBarycenter(f));
92 }
93
94 template<FaceConcept FaceType>
95 void set(uint i, const FaceType& f)
96 {
97 mSamples[i] = faceBarycenter(f);
98 }
99
100 template<FaceConcept FaceType>
101 void add(const FaceType& f, const std::vector<ScalarType>& barCoords)
102 {
103 assert(f.vertexNumber() <= barCoords.size());
104
105 PointT p;
106 for (uint i = 0; i < f.vertexNumber(); i++)
107 p += f.vertex(i)->coord() * barCoords[i];
108
109 mSamples.push_back(p);
110 }
111
112 template<FaceConcept FaceType>
113 void set(
114 uint i,
115 const FaceType& f,
116 const std::vector<ScalarType>& barCoords)
117 {
118 assert(f.vertexNumber() <= barCoords.size());
119
120 PointT p;
121 for (uint i = 0; i < f.vertexNumber(); i++)
122 p += f.vertex(i)->coord() * barCoords[i];
123
124 mSamples[i] = p;
125 }
126
127 template<FaceConcept FaceType>
128 void add(const FaceType& f, const PointT& barCoords)
129 {
130 static_assert(FaceType::NV == 3 || FaceType::NV == -1);
131 if constexpr (FaceType::NV == -1) {
132 assert(f.vertexNumber() == 3);
133 }
134
136
137 mSamples.push_back(p);
138 }
139
140 template<FaceConcept FaceType>
141 void set(uint i, const FaceType& f, const PointT& barCoords)
142 {
143 static_assert(FaceType::NV == 3 || FaceType::NV == -1);
144 if constexpr (FaceType::NV == -1) {
145 assert(f.vertexNumber() == 3);
146 }
147
149
150 mSamples[i] = p;
151 }
152
153 ConstIterator begin() const { return mSamples.begin(); }
154
155 ConstIterator end() const { return mSamples.end(); }
156};
157
158} // namespace vcl
159
160#endif // VCL_SPACE_COMPLEX_SAMPLER_POINT_SAMPLER_H
Definition point_sampler.h:36
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
FaceType::VertexType::CoordType faceBarycenter(const FaceType &f)
Computes the barycenter of a face. Works both for triangle and polygonal faces, and it is optimized i...
Definition geometry.h:77