OGS
RasterDataToMesh.cpp
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#include "RasterDataToMesh.h"
5
6#include <range/v3/algorithm/transform.hpp>
7
8#include "BaseLib/Error.h"
11#include "MeshLib/Node.h"
12
13namespace MeshToolsLib
14{
16{
17static bool checkMesh(MeshLib::Mesh const& mesh)
18{
19 if (mesh.getDimension() > 2)
20 {
21 ERR("This functionality is currently only available for 2D meshes.");
22 return false;
23 }
24 return true;
25}
26
27static double evaluatePixel(double const value, double const no_data,
28 double const replacement)
29{
30 if (std::abs(value - no_data) < std::numeric_limits<double>::epsilon())
31 {
32 return replacement;
33 }
34 return value;
35}
36
39{
40 return node;
41}
42
44{
45 return getCenterOfGravity(element);
46}
47
51template <typename MeshItems>
53 GeoLib::Raster const& raster,
54 double const default_replacement,
55 std::string const& array_name,
56 MeshLib::MeshItemType const item_type,
57 MeshItems const& mesh_items)
58{
59 if (!checkMesh(mesh))
60 {
61 return false;
62 }
63
64 auto& props = mesh.getProperties();
65 std::string const name =
66 BaseLib::getUniqueName(props.getPropertyVectorNames(), array_name);
67 auto* const values = props.createNewPropertyVector<double>(
68 name, item_type, mesh_items.size(), 1);
69 if (values == nullptr)
70 {
71 // MeshLib::Properties keys its property vectors by name alone, and the
72 // name above is unique among the existing ones, so this is a broken
73 // invariant rather than malformed input.
74 OGS_FATAL("Could not create the property vector '{:s}'.", name);
75 }
76
77 double const no_data = raster.getHeader().no_data;
78 ranges::transform(mesh_items, values->begin(),
79 [&](auto const item)
80 {
81 return evaluatePixel(
82 raster.getValueAtPoint(evaluationPoint(*item)),
83 no_data, default_replacement);
84 });
85 return true;
86}
87
89 double const default_replacement,
90 std::string const& array_name)
91{
92 return projectToMeshItems(mesh, raster, default_replacement, array_name,
94}
95
97 double const default_replacement,
98 std::string const& array_name)
99{
100 return projectToMeshItems(mesh, raster, default_replacement, array_name,
102}
103
104} // end namespace RasterDataToMesh
105} // namespace MeshToolsLib
#define OGS_FATAL(...)
Definition Error.h:10
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
Class Raster is used for managing raster data.
Definition Raster.h:39
RasterHeader const & getHeader() const
Returns the complete header information.
Definition Raster.h:75
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition Mesh.h:98
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:101
unsigned getDimension() const
Definition Mesh.h:80
Properties & getProperties()
Definition Mesh.h:127
std::string getUniqueName(std::vector< std::string > const &existing_names, std::string const &input_name)
Append '-' and a number such that the name is unique.
Adding pixel values from a raster onto nodes or cells of a mesh.
static double evaluatePixel(double const value, double const no_data, double const replacement)
static bool checkMesh(MeshLib::Mesh const &mesh)
static bool projectToMeshItems(MeshLib::Mesh &mesh, GeoLib::Raster const &raster, double const default_replacement, std::string const &array_name, MeshLib::MeshItemType const item_type, MeshItems const &mesh_items)
bool projectToNodes(MeshLib::Mesh &mesh, GeoLib::Raster const &raster, double const default_replacement, std::string const &array_name)
bool projectToElements(MeshLib::Mesh &mesh, GeoLib::Raster const &raster, double const default_replacement, std::string const &array_name)
static MathLib::Point3d const & evaluationPoint(MeshLib::Node const &node)
The point at which the raster is sampled for a mesh item.