26int main(
int argc,
char* argv[])
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 " +
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",
43 TCLAP::ValueArg<std::string> output_arg(
44 "o",
"output-file",
"Raster output file (*.asc)",
true,
"",
"string");
46 TCLAP::ValueArg<std::string> input_arg(
"i",
"input-file",
47 "Mesh input file (*.vtu, *.msh)",
50 cmd.parse(argc, argv);
54 INFO(
"Rasterising mesh...");
55 std::unique_ptr<MeshLib::Mesh>
const mesh(
59 ERR(
"Error reading mesh file.");
62 if (mesh->getDimension() != 2)
64 ERR(
"The programme requires a mesh containing two-dimensional elements "
65 "(i.e. triangles or quadrilaterals.");
69 auto const edgeLengths = minMaxEdgeLength(mesh->getElements());
70 double const cellsize =
71 (cell_arg.isSet()) ? cell_arg.getValue() : edgeLengths.first;
72 INFO(
"Cellsize set to {:f}", cellsize);
74 double const max_edge = edgeLengths.second + cellsize;
76 std::vector<MeshLib::Node*>
const& nodes_vec(mesh->getNodes());
77 GeoLib::AABB const bounding_box(nodes_vec.begin(), nodes_vec.end());
80 static_cast<std::size_t
>(std::ceil((max[0] - min[0]) / cellsize));
82 static_cast<std::size_t
>(std::ceil((max[1] - min[1]) / cellsize));
83 double const half_cell = cellsize / 2.0;
86 std::string output_name = output_arg.getValue();
87 std::string
const ext =
".asc";
88 if (std::filesystem::path(output_name).extension() != ext)
90 WARN(
"Adding extension '*.asc' to output file name '{:s}'.",
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 "
102 INFO(
"Writing raster with {:d} x {:d} pixels.", n_cols, n_rows);
106 for (std::size_t row = 0; row < n_rows; ++row)
108 double const y = max[1] - row * cellsize;
109 for (std::size_t column = 0; column < n_cols; ++column)
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 =
120 auto const* element =
124 if (element !=
nullptr)
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}};
137 std::size_t nonzero_count(0);
140 for (std::size_t i = 0; i < 4; ++i)
144 auto const* corner_element =
147 if (corner_element !=
nullptr)
150 *corner_element, node);
154 if (nonzero_count > 0)
157 out << sum / nonzero_count <<
" ";
169 INFO(
"Result written to {:s}", output_name);