Visual Computing Library
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/concepts/mesh.h>
27#include <vclib/mesh/requirements.h>
28
29namespace vcl {
30
42template<MeshConcept MeshType>
43typename MeshType::VertexType::CoordType barycenter(const MeshType& m)
44{
45 using VertexType = MeshType::VertexType;
46 using CoordType = VertexType::CoordType;
47
48 CoordType bar;
49
50 for (const VertexType& v : m.vertices()) {
51 bar += v.coord();
52 }
53
54 return bar / m.vertexNumber();
55}
56
72template<MeshConcept MeshType>
73typename MeshType::VertexType::CoordType scalarWeightedBarycenter(
74 const MeshType& m)
75{
76 requirePerVertexQuality(m);
77
78 using VertexType = MeshType::VertexType;
79 using CoordType = VertexType::CoordType;
80 using QualityType = VertexType::QualityType;
81
82 CoordType bar;
83 QualityType weightedSum = 0;
84
85 for (const VertexType& v : m.vertices()) {
86 bar += v.coord() * v.quality();
87 weightedSum += v.quality();
88 }
89
90 return bar / weightedSum;
91}
92
108template<FaceMeshConcept MeshType>
109typename MeshType::VertexType::CoordType shellBarycenter(const MeshType& m)
110{
111 using VertexType = MeshType::VertexType;
112 using FaceType = MeshType::FaceType;
113 using CoordType = VertexType::CoordType;
114 using ScalarType = CoordType::ScalarType;
115
116 CoordType bar;
117 bar.setZero();
118 ScalarType areaSum = 0;
119
120 for (const FaceType& f : m.faces()) {
121 ScalarType area = faceArea(f);
122 bar += faceBarycenter(f) * area;
123 areaSum += area;
124 }
125
126 return bar / areaSum;
127}
128
129} // namespace vcl
130
131#endif // VCL_ALGORITHMS_MESH_STAT_BARYCENTER_H
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
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:101
constexpr detail::FacesView faces
A view that allows to iterate overt the Face elements of an object.
Definition face.h:52
constexpr detail::VerticesView vertices
A view that allows to iterate over the Vertex elements of an object.
Definition vertex.h:60