Visual Computing Library  devel
Loading...
Searching...
No Matches
quality.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_ALGORITHMS_MESH_STAT_QUALITY_H
24#define VCL_ALGORITHMS_MESH_STAT_QUALITY_H
25
26#include <vclib/mesh.h>
27
28#include <numeric>
29
30namespace vcl {
31
44template<uint ELEM_ID, MeshConcept MeshType>
45auto elementQualityMinMax(const MeshType& m)
46{
47 requirePerElementComponent<ELEM_ID, CompId::QUALITY>(m);
48
49 auto [min, max] =
50 std::ranges::minmax(m.template elements<ELEM_ID>() | views::quality);
51
52 return std::pair {min, max};
53}
54
65template<uint ELEM_ID, MeshConcept MeshType>
66auto elementQualityAverage(const MeshType& m)
67{
68 requirePerElementComponent<ELEM_ID, CompId::QUALITY>(m);
69
70 auto eq = m.template elements<ELEM_ID>() | views::quality;
71 return std::accumulate(std::ranges::begin(eq), std::ranges::end(eq), 0.0) /
72 m.template number<ELEM_ID>();
73}
74
75template<uint ELEM_ID, MeshConcept MeshType>
76auto elementQualityHistogram(
77 const MeshType& m,
78 bool selectionOnly = false,
79 uint histSize = 10000)
80{
81 using QualityType =
82 typename MeshType::template ElementType<ELEM_ID>::QualityType;
83
84 requirePerElementComponent<ELEM_ID, CompId::QUALITY>(m);
85
86 auto minmax = elementQualityMinMax<ELEM_ID>(m);
87
88 Histogram<QualityType> h(minmax.first, minmax.second, histSize);
89 for (const auto& e : m.template elements<ELEM_ID>()) {
90 if (!selectionOnly || e.selected()) {
91 assert(!isDegenerate(e.quality()));
92 h.addValue(e.quality());
93 }
94 }
95 return h;
96}
97
111template<MeshConcept MeshType>
112auto vertexQualityMinMax(const MeshType& m)
113{
114 return elementQualityMinMax<ElemId::VERTEX>(m);
115}
116
130template<FaceMeshConcept MeshType>
131auto faceQualityMinMax(const MeshType& m)
132{
133 return elementQualityMinMax<ElemId::FACE>(m);
134}
135
147template<MeshConcept MeshType>
148auto vertexQualityAverage(const MeshType& m)
149{
150 return elementQualityAverage<ElemId::VERTEX>(m);
151}
152
164template<FaceMeshConcept MeshType>
165auto faceQualityAverage(const MeshType& m)
166{
167 return elementQualityAverage<ElemId::FACE>(m);
168}
169
183template<MeshConcept MeshType>
184std::vector<typename MeshType::VertexType::QualityType> vertexRadiusFromQuality(
185 const MeshType& m,
186 double diskRadius,
187 double radiusVariance,
188 bool invert = false)
189{
190 requirePerVertexQuality(m);
191
192 using VertexType = MeshType::VertexType;
193 using QualityType = VertexType::QualityType;
194
195 std::vector<QualityType> radius(m.vertexContainerSize());
196 std::pair<QualityType, QualityType> minmax = vertexQualityMinMax(m);
197
198 float minRad = diskRadius;
199 float maxRad = diskRadius * radiusVariance;
200 float deltaQ = minmax.second - minmax.first;
201 float deltaRad = maxRad - minRad;
202 for (const VertexType& v : m.vertices()) {
203 radius[m.index(v)] =
204 minRad + deltaRad * ((invert ? minmax.second - v.quality() :
205 v.quality() - minmax.first) /
206 deltaQ);
207 }
208
209 return radius;
210}
211
212template<MeshConcept MeshType, typename HScalar = double>
213Histogram<HScalar> vertexQualityHistogram(
214 const MeshType& m,
215 bool selectionOnly = false,
216 uint histSize = 10000)
217{
218 return elementQualityHistogram<ElemId::VERTEX>(m, selectionOnly, histSize);
219}
220
221template<FaceMeshConcept MeshType, typename HScalar = double>
222Histogram<HScalar> faceQualityHistogram(
223 const MeshType& m,
224 bool selectionOnly = false,
225 uint histSize = 10000)
226{
227 return elementQualityHistogram<ElemId::FACE>(m, selectionOnly, histSize);
228}
229
230} // namespace vcl
231
232#endif // VCL_ALGORITHMS_MESH_STAT_QUALITY_H
constexpr auto max(const T &p1, const T &p2)
Returns the maximum between the two parameters.
Definition min_max.h:81
bool isDegenerate(Scalar number)
Checks if a floating point number is degenerate.
Definition math.h:43
constexpr auto min(const T &p1, const T &p2)
Returns the minimum between the two parameters.
Definition min_max.h:40
constexpr detail::VerticesView vertices
A view that allows to iterate over the Vertex elements of an object.
Definition vertex.h:92