17#include <lagrange/Mesh.h>
18#include <lagrange/MeshTrait.h>
19#include <lagrange/common.h>
20#include <lagrange/legacy/inline.h>
21#include <lagrange/utils/range.h>
35template <
typename MeshType>
38 using Scalar =
typename MeshType::Scalar;
39 using Index =
typename MeshType::Index;
40 using Vector = Eigen::Matrix<Scalar, 3, 1>;
42 const Index dim = mesh.get_dim();
43 const Index num_facets = mesh.get_num_facets();
44 const auto& vertices = mesh.get_vertices();
45 const auto& facets = mesh.get_facets();
46 AttributeArrayOf<MeshType> areas(num_facets, 1);
49 for (Index i = 0; i < num_facets; i++) {
50 const Index i0 = facets(i, 0);
51 const Index i1 = facets(i, 1);
52 const Index i2 = facets(i, 2);
54 v0 << vertices(i0, 0), vertices(i0, 1), 0.0;
55 v1 << vertices(i1, 0), vertices(i1, 1), 0.0;
56 v2 << vertices(i2, 0), vertices(i2, 1), 0.0;
59 areas(i, 0) = Scalar(0.5) * (e1.cross(e2)).norm();
61 }
else if (dim == 3) {
62 for (Index i = 0; i < num_facets; i++) {
63 const Index i0 = facets(i, 0);
64 const Index i1 = facets(i, 1);
65 const Index i2 = facets(i, 2);
67 v0 << vertices(i0, 0), vertices(i0, 1), vertices(i0, 2);
68 v1 << vertices(i1, 0), vertices(i1, 1), vertices(i1, 2);
69 v2 << vertices(i2, 0), vertices(i2, 1), vertices(i2, 2);
72 areas(i, 0) = Scalar(0.5) * (e1.cross(e2)).norm();
75 throw std::runtime_error(
"Unsupported dimention.");
90template <
typename MeshType>
93 using Scalar =
typename MeshType::Scalar;
94 using Index =
typename MeshType::Index;
95 using Vector = Eigen::Matrix<Scalar, 3, 1>;
97 const Index dim = mesh.get_dim();
98 const Index num_facets = mesh.get_num_facets();
99 const auto& vertices = mesh.get_vertices();
100 const auto& facets = mesh.get_facets();
101 AttributeArrayOf<MeshType> areas(num_facets, 1);
104 for (Index i = 0; i < num_facets; i++) {
105 const Index i0 = facets(i, 0);
106 const Index i1 = facets(i, 1);
107 const Index i2 = facets(i, 2);
108 const Index i3 = facets(i, 3);
110 v0 << vertices(i0, 0), vertices(i0, 1), 0.0f;
111 v1 << vertices(i1, 0), vertices(i1, 1), 0.0f;
112 v2 << vertices(i2, 0), vertices(i2, 1), 0.0f;
113 v3 << vertices(i3, 0), vertices(i3, 1), 0.0f;
118 areas(i, 0) = 0.5f * (e10.cross(e30)).norm() + 0.5f * (e12.cross(e32)).norm();
120 }
else if (dim == 3) {
121 for (Index i = 0; i < num_facets; i++) {
122 const Index i0 = facets(i, 0);
123 const Index i1 = facets(i, 1);
124 const Index i2 = facets(i, 2);
125 const Index i3 = facets(i, 3);
127 v0 << vertices(i0, 0), vertices(i0, 1), vertices(i0, 2);
128 v1 << vertices(i1, 0), vertices(i1, 1), vertices(i1, 2);
129 v2 << vertices(i2, 0), vertices(i2, 1), vertices(i2, 2);
130 v3 << vertices(i3, 0), vertices(i3, 1), vertices(i3, 2);
135 areas(i, 0) = 0.5f * (e10.cross(e30)).norm() + 0.5f * (e12.cross(e32)).norm();
138 throw std::runtime_error(
"Unsupported dimention.");
145LAGRANGE_LEGACY_INLINE
157template <
typename MeshType>
158auto compute_facet_area_raw(
const MeshType& mesh) -> AttributeArrayOf<MeshType>
160 static_assert(MeshTrait<MeshType>::is_mesh(),
"Input type is not Mesh");
161 using Index =
typename MeshType::Index;
162 const Index vertex_per_facet = mesh.get_vertex_per_facet();
163 if (vertex_per_facet == 3) {
165 }
else if (vertex_per_facet == 4) {
168 throw std::runtime_error(
"Unsupported facet type.");
179template <
typename MeshType>
182 static_assert(MeshTrait<MeshType>::is_mesh(),
"Input type is not Mesh");
183 auto areas = compute_facet_area_raw(mesh);
184 mesh.add_facet_attribute(
"area");
185 mesh.import_facet_attribute(
"area", areas);
199template <
typename DerivedUV,
typename DerivedF>
200Eigen::Matrix<typename DerivedUV::Scalar, Eigen::Dynamic, 1> compute_uv_area_raw(
201 const Eigen::MatrixBase<DerivedUV>& uv,
202 const Eigen::MatrixBase<DerivedF>& triangles)
204 using Scalar =
typename DerivedUV::Scalar;
205 using Index =
typename DerivedF::Scalar;
209 const auto num_triangles = triangles.rows();
211 auto compute_single_triangle_area = [&uv, &triangles](Index i) {
212 const auto& f = triangles.row(i);
213 const auto& v0 = uv.row(f[0]);
214 const auto& v1 = uv.row(f[1]);
215 const auto& v2 = uv.row(f[2]);
216 return 0.5f * (v0[0] * v1[1] + v1[0] * v2[1] + v2[0] * v0[1] - v0[0] * v2[1] -
217 v1[0] * v0[1] - v2[0] * v1[1]);
220 Eigen::Matrix<Scalar, Eigen::Dynamic, 1> areas(num_triangles);
221 for (
auto i :
range(safe_cast<Index>(num_triangles))) {
222 areas[i] = compute_single_triangle_area(i);
AttributeId compute_facet_area(SurfaceMesh< Scalar, Index > &mesh, FacetAreaOptions options={})
Compute per-facet area.
Definition: compute_area.cpp:304
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Type alias for one-dimensional column Eigen vectors.
Definition: views.h:79
#define la_runtime_assert(...)
Runtime assertion check.
Definition: assert.h:169
internal::Range< Index > range(Index end)
Returns an iterable object representing the range [0, end).
Definition: range.h:176
auto compute_quad_areas(const MeshType &mesh) -> AttributeArrayOf< MeshType >
Calculates the quad areas.
Definition: compute_facet_area.h:91
auto compute_triangle_areas(const MeshType &mesh) -> AttributeArrayOf< MeshType >
Calculates the triangle areas.
Definition: compute_facet_area.h:36
Main namespace for Lagrange.
Definition: AABBIGL.h:30