Visual Computing Library  devel
Loading...
Searching...
No Matches
barycenter.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_BARYCENTER_H
24#define VCL_ALGORITHMS_MESH_STAT_BARYCENTER_H
25
26#include <vclib/mesh.h>
27
28namespace vcl {
29
41template<MeshConcept MeshType>
42auto barycenter(const MeshType& m) -> MeshType::VertexType::PositionType
43{
44 using VertexType = MeshType::VertexType;
45 using PositionType = VertexType::PositionType;
46
47 PositionType bar;
48
49 for (const VertexType& v : m.vertices()) {
50 bar += v.position();
51 }
52
53 return bar / m.vertexNumber();
54}
55
70template<MeshConcept MeshType>
71auto weightedBarycenter(const MeshType& m, Range auto&& weights)
72 -> MeshType::VertexType::PositionType
73{
74 using VertexType = MeshType::VertexType;
75 using PositionType = VertexType::PositionType;
76 using RType = std::ranges::range_value_t<decltype(weights)>;
77
78 assert(std::ranges::size(weights) == m.vertexNumber());
79
80 PositionType bar;
81 RType weightedSum = 0;
82
83 for (const auto& [v, w] : std::views::zip(m.vertices(), weights)) {
84 bar += v.position() * w;
85 weightedSum += w;
86 }
87
88 return bar / weightedSum;
89}
90
106template<MeshConcept MeshType>
107auto qualityWeightedBarycenter(const MeshType& m)
108 -> MeshType::VertexType::PositionType
109{
110 requirePerVertexQuality(m);
111
112 return weightedBarycenter(m, m.vertices() | views::quality);
113}
114
130template<FaceMeshConcept MeshType>
131auto shellBarycenter(const MeshType& m) -> MeshType::VertexType::PositionType
132{
133 using VertexType = MeshType::VertexType;
134 using FaceType = MeshType::FaceType;
135 using PositionType = VertexType::PositionType;
136 using ScalarType = PositionType::ScalarType;
137
138 PositionType bar;
139 bar.setZero();
140 ScalarType areaSum = 0;
141
142 for (const FaceType& f : m.faces()) {
143 ScalarType area = faceArea(f);
144 bar += faceBarycenter(f) * area;
145 areaSum += area;
146 }
147
148 return bar / areaSum;
149}
150
151} // namespace vcl
152
153#endif // VCL_ALGORITHMS_MESH_STAT_BARYCENTER_H
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
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
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