OGS
ResetMeshElementProperty.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#pragma once
5
6#include <algorithm>
7#include <cstdlib>
8#include <range/v3/algorithm/all_of.hpp>
9#include <range/v3/algorithm/any_of.hpp>
10#include <range/v3/view/transform.hpp>
11#include <vector>
12
13#include "GeoLib/GEOObjects.h"
14#include "GeoLib/Polygon.h"
19#include "MeshLib/Mesh.h"
20#include "MeshLib/Node.h"
22
23namespace MeshGeoToolsLib
24{
25template <typename PT>
27 GeoLib::Polygon const& polygon,
28 std::string const& property_name,
29 PT new_property_value,
30 int restrict_to_material_id,
31 bool const any_of)
32{
34 mesh, property_name, MeshLib::MeshItemType::Cell, 1);
35
36 if (pv->getMeshItemType() != MeshLib::MeshItemType::Cell)
37 {
38 ERR("Values of the PropertyVector are not assigned to cells.");
39 return;
40 }
41 auto const* material_ids =
42 mesh.getProperties().getPropertyVector<int>("MaterialIDs");
43
44 if (restrict_to_material_id != -1 && !material_ids)
45 {
47 "Restriction of resetting a property in a polygonal region "
48 "requires that a MaterialIDs data array is available in the "
49 "mesh.");
50 }
51
52 auto has_element_required_material_id = [&](int const element_id)
53 {
54 return restrict_to_material_id == -1 ||
55 (*material_ids)[element_id] == restrict_to_material_id;
56 };
57
58 auto const outside = markNodesOutSideOfPolygon(mesh.getNodes(), polygon);
59
60 auto is_node_outside = [&outside](std::size_t const node_id)
61 { return outside[node_id]; };
62
63 auto is_element_outside = [&](MeshLib::Element const& e)
64 {
65 auto ids = e.nodes() | MeshLib::views::ids;
66 return any_of ? ranges::all_of(ids, is_node_outside)
67 : ranges::any_of(ids, is_node_outside);
68 };
69
70 auto is_valid_element = [&](MeshLib::Element const& e)
71 {
72 return !is_element_outside(e) &&
73 has_element_required_material_id(e.getID());
74 };
75
76 auto compute_value = [&](MeshLib::Element const* const e) -> PT
77 { return is_valid_element(*e) ? new_property_value : (*pv)[e->getID()]; };
78
79 pv->assign(mesh.getElements() | ranges::views::transform(compute_value));
80}
81
82} // namespace MeshGeoToolsLib
#define OGS_FATAL(...)
Definition Error.h:19
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition Mesh.h:97
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:100
Properties & getProperties()
Definition Mesh.h:125
PropertyVector< T > const * getPropertyVector(std::string_view name) const
std::vector< bool > markNodesOutSideOfPolygon(std::vector< MeshLib::Node * > const &nodes, GeoLib::Polygon const &polygon)
void resetMeshElementProperty(MeshLib::Mesh &mesh, GeoLib::Polygon const &polygon, std::string const &property_name, PT new_property_value, int restrict_to_material_id, bool const any_of)
constexpr ranges::views::view_closure ids
For an element of a range view return its id.
Definition Mesh.h:216
PropertyVector< T > * getOrCreateMeshProperty(Mesh &mesh, std::string const &property_name, MeshItemType const item_type, int const number_of_components)