82 std::string
const& name,
bool const on_top,
83 bool const copy_material_ids,
84 std::optional<int>
const layer_id)
86 INFO(
"Extracting top surface of mesh '{:s}' ... ", mesh.
getName());
87 double const flag = on_top ? -1.0 : 1.0;
88 Eigen::Vector3d
const dir({0, 0, flag});
89 double const angle(90);
90 std::unique_ptr<MeshLib::Mesh> sfc_mesh(
nullptr);
92 auto const prop_name =
98 mesh, dir, angle, prop_name));
102 sfc_mesh = (on_top) ? std::make_unique<MeshLib::Mesh>(mesh)
103 : std::unique_ptr<MeshLib::Mesh>(
107 sfc_mesh->getProperties().createNewPropertyVector<std::size_t>(
109 sfc_mesh->getNumberOfNodes(), 1);
112 pv->assign(ranges::views::iota(0u, sfc_mesh->getNumberOfNodes()));
116 ERR(
"Could not create and initialize property '{}'.", prop_name);
123 std::vector<MeshLib::Node*> subsfc_nodes =
125 std::vector<MeshLib::Element*> subsfc_elements =
128 std::size_t
const n_subsfc_nodes(subsfc_nodes.size());
130 std::vector<MeshLib::Node*>
const& sfc_nodes(sfc_mesh->getNodes());
131 std::size_t
const n_sfc_nodes(sfc_nodes.size());
133 if (!sfc_mesh->getProperties().existsPropertyVector<std::size_t>(prop_name))
135 ERR(
"Need subsurface node ids, but the property '{:s}' is not "
141 auto const*
const node_id_pv =
142 sfc_mesh->getProperties().getPropertyVector<std::size_t>(prop_name);
145 std::map<std::size_t, std::size_t> subsfc_sfc_id_map;
146 for (std::size_t k(0); k < n_sfc_nodes; ++k)
148 std::size_t
const subsfc_id((*node_id_pv)[k]);
149 std::size_t
const sfc_id(k + n_subsfc_nodes);
150 subsfc_sfc_id_map.insert(std::make_pair(subsfc_id, sfc_id));
153 node[0], node[1], node[2] - (flag * thickness), sfc_id));
157 std::vector<MeshLib::Element*>
const& sfc_elements(sfc_mesh->getElements());
158 std::size_t
const n_sfc_elements(sfc_elements.size());
159 for (std::size_t k(0); k < n_sfc_elements; ++k)
162 subsfc_nodes, *sfc_elements[k], *node_id_pv, subsfc_sfc_id_map));
165 auto new_mesh =
new MeshLib::Mesh(name, subsfc_nodes, subsfc_elements,
170 ERR(
"Could not copy the property 'MaterialIDs' since the original mesh "
171 "does not contain such a property.");
174 auto const*
const materials =
177 auto*
const new_materials =
178 new_mesh->getProperties().createNewPropertyVector<
int>(
182 ERR(
"Can not set material properties for new layer");
186 int next_material_id = 0;
188 if (materials !=
nullptr)
190 next_material_id = *ranges::max_element(*materials) + 1;
193 auto initial_values =
194 materials ? ranges::any_view<int const>{*materials}
195 : ranges::views::repeat_n(layer_id.value_or(next_material_id),
198 auto additional_values = [&]() -> ranges::any_view<int const>
200 if (copy_material_ids &&
201 sfc_mesh->getProperties().existsPropertyVector<
int>(
"MaterialIDs"))
203 return ranges::any_view<int const>{
204 *sfc_mesh->getProperties().getPropertyVector<
int>(
209 int const new_layer_id = layer_id.value_or(next_material_id);
210 auto const n_new_props =
212 return ranges::views::repeat_n(new_layer_id, n_new_props);
216 new_materials->assign(ranges::views::common(
217 ranges::views::concat(initial_values, additional_values)));