Visual Computing Library  devel
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.h>
27#include <vclib/mesh.h>
28
29namespace vcl {
30
31template<PointConcept PointT = Point3d>
33{
34 std::vector<PointT> mSamples;
35
36public:
37 using PointType = PointT;
38 using ScalarType = PointT::ScalarType;
39 using ConstIterator = std::vector<PointT>::const_iterator;
40
41 PointSampler() = default;
42
43 const std::vector<PointT>& samples() const { return mSamples; }
44
45 const PointT& sample(uint i) const { return mSamples[i]; }
46
47 std::size_t size() const { return mSamples.size(); }
48
49 void clear() { mSamples.clear(); }
50
51 void reserve(uint n) { mSamples.reserve(n); }
52
53 void resize(uint n) { mSamples.resize(n); }
54
55 void add(const PointT& p) { mSamples.push_back(p); }
56
57 void set(uint i, const PointT& p) { mSamples[i] = p; }
58
59 template<VertexConcept VertexType>
60 void add(const VertexType& v)
61 {
62 mSamples.push_back(v.position());
63 }
64
65 template<VertexConcept VertexType>
66 void set(uint i, const VertexType& v)
67 {
68 mSamples[i] = v.position();
69 }
70
71 template<EdgeConcept EdgeType>
72 void add(const EdgeType& e, double u = 0.5)
73 {
74 mSamples.push_back(
75 (e.vertex(0).position() * (1 - u)) + (e.vertex(1).position() * u));
76 }
77
78 template<EdgeConcept EdgeType>
79 void set(uint i, const EdgeType& e, double u = 0.5)
80 {
81 mSamples[i] =
82 (e.vertex(0).position() * (1 - u)) + (e.vertex(1).position() * u);
83 }
84
85 template<FaceConcept FaceType>
86 void add(const FaceType& f)
87 {
88 mSamples.push_back(faceBarycenter(f));
89 }
90
91 template<FaceConcept FaceType>
92 void set(uint i, const FaceType& f)
93 {
94 mSamples[i] = faceBarycenter(f);
95 }
96
97 template<FaceConcept FaceType>
98 void add(const FaceType& f, const std::vector<ScalarType>& barCoords)
99 {
100 assert(f.vertexNumber() <= barCoords.size());
101
102 PointT p;
103 for (uint i = 0; i < f.vertexNumber(); i++)
104 p += f.vertex(i)->position() * barCoords[i];
105
106 mSamples.push_back(p);
107 }
108
109 template<FaceConcept FaceType>
110 void set(
111 uint i,
112 const FaceType& f,
113 const std::vector<ScalarType>& barCoords)
114 {
115 assert(f.vertexNumber() <= barCoords.size());
116
117 PointT p;
118 for (uint i = 0; i < f.vertexNumber(); i++)
119 p += f.vertex(i)->position() * barCoords[i];
120
121 mSamples[i] = p;
122 }
123
124 template<FaceConcept FaceType>
125 void add(const FaceType& f, const PointT& barCoords)
126 {
127 static_assert(FaceType::NV == 3 || FaceType::NV == -1);
128 if constexpr (FaceType::NV == -1) {
129 assert(f.vertexNumber() == 3);
130 }
131
133
134 mSamples.push_back(p);
135 }
136
137 template<FaceConcept FaceType>
138 void set(uint i, const FaceType& f, const PointT& barCoords)
139 {
140 static_assert(FaceType::NV == 3 || FaceType::NV == -1);
141 if constexpr (FaceType::NV == -1) {
142 assert(f.vertexNumber() == 3);
143 }
144
146
147 mSamples[i] = p;
148 }
149
150 ConstIterator begin() const { return mSamples.begin(); }
151
152 ConstIterator end() const { return mSamples.end(); }
153};
154
155} // namespace vcl
156
157#endif // VCL_SPACE_COMPLEX_SAMPLER_POINT_SAMPLER_H
A class representing a box in N-dimensional space.
Definition box.h:46
PointT size() const
Computes the size of the box.
Definition box.h:267
Definition point_sampler.h:33
FaceType::VertexType::PositionType 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:81