Lagrange
compute_mesh_centroid.h
1/*
2 * Copyright 2019 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <Eigen/Core>
15
16#include <lagrange/Mesh.h>
17#include <lagrange/legacy/inline.h>
18#include <lagrange/utils/range.h>
19
20namespace lagrange {
21LAGRANGE_LEGACY_INLINE
22namespace legacy {
23
24template <typename Scalar>
26{
27 // We get this for free when computing the centroid.
28 Scalar area;
29 // The centroid itself. Will be row vector to be consistent with lagrange.
30 Eigen::Matrix<Scalar, 1, Eigen::Dynamic> centroid;
31};
32
33// Compute the centroid of certain facets in a mesh
34// mesh_ref, reference to the mesh
35// active_facets, the facets that are included in the centroid computation.
36// Empty would imply all facets.
37template <typename MeshType>
39 const MeshType& mesh_ref,
40 const typename MeshType::IndexList& active_facets = typename MeshType::IndexList())
41{ //
42
43 using Scalar = typename MeshType::Scalar;
44 using Vertex3 = Eigen::Matrix<Scalar, 1, 3>;
46
47 const auto& vertices = mesh_ref.get_vertices();
48 const auto& facets = mesh_ref.get_facets();
49
50 la_runtime_assert(vertices.cols() == 3, "Currently, only 3 dimensions are supported");
51 la_runtime_assert(facets.cols() == 3, "Currently, only triangles are supported");
52
53 Vertex3 centroid = Vertex3::Zero();
54 Scalar area = 0;
55
56 for (auto facet_id : range_sparse(mesh_ref.get_num_facets(), active_facets)) {
57 const Vertex3 v0 = vertices.row(facets(facet_id, 0));
58 const Vertex3 v1 = vertices.row(facets(facet_id, 1));
59 const Vertex3 v2 = vertices.row(facets(facet_id, 2));
60 const Vertex3 tri_centroid = (v0 + v1 + v2) / 3.f;
61 const Scalar tri_area = (v1 - v0).cross(v2 - v0).norm() / 2.f;
62 centroid += tri_area * tri_centroid;
63 area += tri_area;
64 } // over triangles
65 centroid /= area;
66
67 // Return
68 Output output;
69 output.area = area;
70 output.centroid = centroid;
71 return output;
72}
73
74
75} // namespace legacy
76} // namespace lagrange
Definition: Mesh.h:48
void compute_mesh_centroid(const SurfaceMesh< Scalar, Index > &mesh, span< Scalar > centroid, MeshCentroidOptions options={})
Compute mesh centroid, where mesh centroid is defined as the weighted sum of facet centroids.
Definition: compute_centroid.cpp:74
#define la_runtime_assert(...)
Runtime assertion check.
Definition: assert.h:169
internal::SparseRange< Index > range_sparse(Index max, const std::vector< Index > &active)
Returns an iterable object representing a subset of the range [0, max).
Definition: range.h:217
Main namespace for Lagrange.
Definition: AABBIGL.h:30
Definition: compute_mesh_centroid.h:26