27{
28 TCLAP::CmdLine cmd(
29 "Mesh to raster converter.\n"
30 "Rasterises a 2D mesh, pixel values are set to the elevation of a "
31 "regular grid superimposed on the mesh. If no mesh element is located "
32 "beneath a pixel it is set to NODATA.\n\n"
33 "OpenGeoSys-6 software, version " +
35 ".\n"
36 "Copyright (c) 2012-2024, OpenGeoSys Community "
37 "(http://www.opengeosys.org)",
39 TCLAP::ValueArg<double> cell_arg("c", "cellsize",
40 "edge length of raster cells in result",
41 false, 1, "real");
42 cmd.add(cell_arg);
43 TCLAP::ValueArg<std::string> output_arg(
44 "o", "output-file", "Raster output file (*.asc)", true, "", "string");
45 cmd.add(output_arg);
46 TCLAP::ValueArg<std::string> input_arg("i", "input-file",
47 "Mesh input file (*.vtu, *.msh)",
48 true, "", "string");
49 cmd.add(input_arg);
50 cmd.parse(argc, argv);
51
53
54 INFO(
"Rasterising mesh...");
55 std::unique_ptr<MeshLib::Mesh> const mesh(
57 if (mesh == nullptr)
58 {
59 ERR(
"Error reading mesh file.");
60 return 1;
61 }
62 if (mesh->getDimension() != 2)
63 {
64 ERR(
"The programme requires a mesh containing two-dimensional elements "
65 "(i.e. triangles or quadrilaterals.");
66 return 2;
67 }
68
70 double const cellsize =
71 (cell_arg.isSet()) ? cell_arg.getValue() : edgeLengths.first;
72 INFO(
"Cellsize set to {:f}", cellsize);
73
74 double const max_edge = edgeLengths.second + cellsize;
75
76 std::vector<MeshLib::Node*> const& nodes_vec(mesh->getNodes());
77 GeoLib::AABB const bounding_box(nodes_vec.begin(), nodes_vec.end());
78 auto const& [min, max] = bounding_box.getMinMaxPoints();
79 auto const n_cols =
80 static_cast<std::size_t>(std::ceil((max[0] - min[0]) / cellsize));
81 auto const n_rows =
82 static_cast<std::size_t>(std::ceil((max[1] - min[1]) / cellsize));
83 double const half_cell = cellsize / 2.0;
84
85
86 std::string output_name = output_arg.getValue();
87 std::string const ext = ".asc";
88 if (std::filesystem::path(output_name).extension() != ext)
89 {
90 WARN(
"Adding extension '*.asc' to output file name '{:s}'.",
91 output_name);
92 output_name += ext;
93 }
94 std::ofstream out(output_name);
95 out << "ncols " << n_cols << "\n";
96 out << "nrows " << n_rows << "\n";
97 out << std::fixed << "xllcorner " << (min[0] - half_cell) << "\n";
98 out << std::fixed << "yllcorner " << (min[1] - half_cell) << "\n";
99 out << std::fixed << "cellsize " << cellsize << "\n";
100 out << "NODATA_value "
101 << "-9999\n";
102 INFO(
"Writing raster with {:d} x {:d} pixels.", n_cols, n_rows);
103
105
106 for (std::size_t row = 0; row < n_rows; ++row)
107 {
108 double const y = max[1] - row * cellsize;
109 for (std::size_t column = 0; column < n_cols; ++column)
110 {
111
112 double const x = min[0] + column * cellsize;
115 -std::numeric_limits<double>::max()}};
117 std::numeric_limits<double>::max()}};
118 std::vector<const MeshLib::Element*> const& elems =
119 grid.getElementsInVolume(min_vol, max_vol);
120 auto const* element =
122 node);
123
124 if (element != nullptr)
125 {
127 node)
128 << " ";
129 }
130 else
131 {
132 std::array<double, 4> const x_off{
133 {-half_cell, half_cell, -half_cell, half_cell}};
134 std::array<double, 4> const y_off{
135 {-half_cell, -half_cell, half_cell, half_cell}};
136 double sum(0);
137 std::size_t nonzero_count(0);
138
139
140 for (std::size_t i = 0; i < 4; ++i)
141 {
143 0);
144 auto const* corner_element =
146 elems, corner_node);
147 if (corner_element != nullptr)
148 {
150 *corner_element, node);
151 nonzero_count++;
152 }
153 }
154 if (nonzero_count > 0)
155 {
156
157 out << sum / nonzero_count << " ";
158 }
159 else
160 {
161
162 out << "-9999 ";
163 }
164 }
165 }
166 out << "\n";
167 }
168 out.close();
169 INFO(
"Result written to {:s}", output_name);
170 return 0;
171}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Class AABB is an axis aligned bounding box around a given set of geometric points of (template) type ...
GITINFOLIB_EXPORT const std::string ogs_version
MeshLib::Mesh * readMeshFromFile(const std::string &file_name, bool const compute_element_neighbors)
std::pair< double, double > minMaxEdgeLength(std::vector< Element * > const &elements)
Returns the minimum and maximum edge length for given elements.