14#include <lagrange/Mesh.h>
15#include <lagrange/MeshTrait.h>
16#include <lagrange/legacy/inline.h>
17#include <lagrange/utils/range.h>
18#include <tbb/parallel_for.h>
31template <
typename MeshType>
32void eval_as_vertex_attribute(
34 const std::string& attribute_name,
35 const std::function<
typename MeshType::Scalar(
typename MeshType::Index v_idx)>& func,
38 static_assert(MeshTrait<MeshType>::is_mesh(),
"Input type is not Mesh");
40 using AttributeArray =
typename MeshType::AttributeArray;
41 using Index =
typename MeshType::Index;
43 const auto num_vertices = mesh.get_num_vertices();
44 AttributeArray attr(num_vertices, 1);
48 tbb::blocked_range<Index>(0, num_vertices),
49 [&](
const tbb::blocked_range<Index>& tbb_range) {
50 for (
auto v_idx = tbb_range.begin(); v_idx != tbb_range.end(); ++v_idx) {
51 attr(v_idx, 0) = func(v_idx);
55 for (
auto v_idx :
range(num_vertices)) {
56 attr(v_idx, 0) = func(v_idx);
59 if (!mesh.has_vertex_attribute(attribute_name)) {
60 mesh.add_vertex_attribute(attribute_name);
62 mesh.import_vertex_attribute(attribute_name, attr);
65template <
typename MeshType>
66void eval_as_vertex_attribute(
68 const std::string& attribute_name,
69 const std::function<
typename MeshType::Scalar(
const typename MeshType::VertexType& V)>& func,
72 static_assert(MeshTrait<MeshType>::is_mesh(),
"Input type is not Mesh");
74 using Index =
typename MeshType::Index;
76 const auto& V = mesh.get_vertices();
77 return eval_as_vertex_attribute(
80 [&V, &func](Index v_idx) {
return func(V.row(v_idx)); },
84template <
typename MeshType>
85void eval_as_vertex_attribute(
87 const std::string& attribute_name,
88 const std::function<
typename MeshType::Scalar(
89 typename MeshType::Scalar x,
90 typename MeshType::Scalar y,
91 typename MeshType::Scalar z)>& func,
94 static_assert(MeshTrait<MeshType>::is_mesh(),
"Input type is not Mesh");
96 using Index =
typename MeshType::Index;
98 const auto& V = mesh.get_vertices();
99 return eval_as_vertex_attribute(
102 [&V, &func](Index v_idx) {
return func(V(v_idx, 0), V(v_idx, 1), V(v_idx, 2)); },
106template <
typename MeshType>
107void eval_as_facet_attribute(
109 const std::string& attribute_name,
110 const std::function<
typename MeshType::Scalar(
typename MeshType::Index f_idx)>& func,
111 bool parallel =
true)
113 static_assert(MeshTrait<MeshType>::is_mesh(),
"Input type is not Mesh");
115 using AttributeArray =
typename MeshType::AttributeArray;
116 using Index =
typename MeshType::Index;
118 const auto num_facets = mesh.get_num_facets();
119 AttributeArray attr(num_facets, 1);
123 tbb::blocked_range<Index>(0, num_facets),
124 [&](
const tbb::blocked_range<Index>& tbb_range) {
125 for (
auto f_idx = tbb_range.begin(); f_idx != tbb_range.end(); ++f_idx) {
126 attr(f_idx, 0) = func(f_idx);
130 for (
auto f_idx :
range(num_facets)) {
131 attr(f_idx, 0) = func(f_idx);
135 if (!mesh.has_facet_attribute(attribute_name)) {
136 mesh.add_facet_attribute(attribute_name);
138 mesh.import_facet_attribute(attribute_name, attr);
141template <
typename MeshType>
142void eval_as_edge_attribute_new(
144 const std::string& attribute_name,
145 const std::function<
typename MeshType::Scalar(
typename MeshType::Index e_idx)>& func,
146 bool parallel =
true)
148 static_assert(MeshTrait<MeshType>::is_mesh(),
"Input type is not Mesh");
150 using AttributeArray =
typename MeshType::AttributeArray;
151 using Index =
typename MeshType::Index;
153 const auto num_edges = mesh.get_num_edges();
154 AttributeArray attr(num_edges, 1);
158 tbb::blocked_range<Index>(0, num_edges),
159 [&](
const tbb::blocked_range<Index>& tbb_range) {
160 for (
auto e_idx = tbb_range.begin(); e_idx != tbb_range.end(); ++e_idx) {
161 attr(e_idx, 0) = func(e_idx);
165 for (
auto e_idx :
range(num_edges)) {
166 attr(e_idx, 0) = func(e_idx);
170 if (!mesh.has_edge_attribute(attribute_name)) {
171 mesh.add_edge_attribute(attribute_name);
173 mesh.import_edge_attribute(attribute_name, attr);
internal::Range< Index > range(Index end)
Returns an iterable object representing the range [0, end).
Definition: range.h:176
Main namespace for Lagrange.
Definition: AABBIGL.h:30