OGS
ResetMeshElementProperty.h
Go to the documentation of this file.
1/*
2 * \file
3 * \copyright
4 * Copyright (c) 2012-2025, OpenGeoSys Community (http://www.opengeosys.org)
5 * Distributed under a Modified BSD License.
6 * See accompanying file LICENSE.txt or
7 * http://www.opengeosys.org/project/license
8 *
9 */
10
11#pragma once
12
13#include <algorithm>
14#include <cstdlib>
15#include <range/v3/algorithm/all_of.hpp>
16#include <range/v3/algorithm/any_of.hpp>
17#include <range/v3/view/transform.hpp>
18#include <vector>
19
20#include "GeoLib/GEOObjects.h"
21#include "GeoLib/Polygon.h"
26#include "MeshLib/Mesh.h"
27#include "MeshLib/Node.h"
29
30namespace MeshGeoToolsLib
31{
32template <typename PT>
34 GeoLib::Polygon const& polygon,
35 std::string const& property_name,
36 PT new_property_value,
37 int restrict_to_material_id,
38 bool const any_of)
39{
41 mesh, property_name, MeshLib::MeshItemType::Cell, 1);
42
43 if (pv->getMeshItemType() != MeshLib::MeshItemType::Cell)
44 {
45 ERR("Values of the PropertyVector are not assigned to cells.");
46 return;
47 }
48 auto const* material_ids =
49 mesh.getProperties().getPropertyVector<int>("MaterialIDs");
50
51 if (restrict_to_material_id != -1 && !material_ids)
52 {
54 "Restriction of resetting a property in a polygonal region "
55 "requires that a MaterialIDs data array is available in the "
56 "mesh.");
57 }
58
59 auto has_element_required_material_id = [&](int const element_id)
60 {
61 return restrict_to_material_id == -1 ||
62 (*material_ids)[element_id] == restrict_to_material_id;
63 };
64
65 auto const outside = markNodesOutSideOfPolygon(mesh.getNodes(), polygon);
66
67 auto is_node_outside = [&outside](std::size_t const node_id)
68 { return outside[node_id]; };
69
70 auto is_element_outside = [&](MeshLib::Element const& e)
71 {
72 auto ids = e.nodes() | MeshLib::views::ids;
73 return any_of ? ranges::all_of(ids, is_node_outside)
74 : ranges::any_of(ids, is_node_outside);
75 };
76
77 auto is_valid_element = [&](MeshLib::Element const& e)
78 {
79 return !is_element_outside(e) &&
80 has_element_required_material_id(e.getID());
81 };
82
83 auto compute_value = [&](MeshLib::Element const* const e) -> PT
84 { return is_valid_element(*e) ? new_property_value : (*pv)[e->getID()]; };
85
86 pv->assign(mesh.getElements() | ranges::views::transform(compute_value));
87}
88
89} // namespace MeshGeoToolsLib
Definition of the Element class.
#define OGS_FATAL(...)
Definition Error.h:26
Definition of the GEOObjects class.
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
Definition of the Mesh class.
Definition of the Node class.
Definition of the Polygon class.
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition Mesh.h:108
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:111
Properties & getProperties()
Definition Mesh.h:136
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:227
PropertyVector< T > * getOrCreateMeshProperty(Mesh &mesh, std::string const &property_name, MeshItemType const item_type, int const number_of_components)
Definition of readMeshFromFile function.