OGS
Raster2Mesh.cpp
Go to the documentation of this file.
1
10#include <tclap/CmdLine.h>
11
12#include <memory>
13#include <string>
14
15#include "BaseLib/Logging.h"
16#include "BaseLib/MPI.h"
19#include "GeoLib/Raster.h"
20#include "InfoLib/GitInfo.h"
22#include "MeshLib/Mesh.h"
23#include "MeshLib/MeshEnums.h"
25
26int main(int argc, char* argv[])
27{
28 TCLAP::CmdLine cmd(
29 "Converts an ASCII raster file (*.asc) into a 2D triangle- or "
30 "quad-mesh. Pixel values can be interpreted as elevation of mesh nodes "
31 "or as scalar values for mesh elements.\nIt is highly recommended to "
32 "create triangle meshes when interpreting pixel values as elevation, "
33 "as it is likely that the resulting mesh will otherwise contain "
34 "deformed (i.e. non-planar) quad-elements.\n\n"
35 "OpenGeoSys-6 software, version " +
37 ".\n"
38 "Copyright (c) 2012-2025, OpenGeoSys Community "
39 "(http://www.opengeosys.org)",
41 TCLAP::ValueArg<std::string> array_name_arg(
42 "n", "arrayname",
43 "Name of the scalar array. Only required if assigning pixel values to "
44 "cell data has been selected (default name is 'Values').",
45 false, "", "ARRAY_NAME");
46 cmd.add(array_name_arg);
47 std::vector<std::string> pixel_vals{"elevation", "materials", "scalar"};
48 TCLAP::ValuesConstraint<std::string> pixel_val_options(pixel_vals);
49 TCLAP::ValueArg<std::string> arg_pixel_type(
50 "p", "pixel-type",
51 "The choice how pixel values should be interpreted by the software: "
52 "'elevation' adjusts z-coordinates; 'materials' sets (integer) "
53 "material IDs; 'scalar' creates a (floating-point) array associated. "
54 "with mesh elements.",
55 true, "", &pixel_val_options);
56 cmd.add(arg_pixel_type);
57 std::vector<std::string> allowed_elems{"tri", "quad"};
58 TCLAP::ValuesConstraint<std::string> allowed_elem_vals(allowed_elems);
59 TCLAP::ValueArg<std::string> arg_elem_type(
60 "e", "elem-type", "The element type used in the resulting OGS mesh",
61 true, "", &allowed_elem_vals);
62 cmd.add(arg_elem_type);
63 TCLAP::ValueArg<std::string> output_arg(
64 "o", "output", "Output (.vtu). Name of the output mesh file", true, "",
65 "OUTPUT_FILE");
66 cmd.add(output_arg);
67 TCLAP::ValueArg<std::string> input_arg(
68 "i", "input", "Input (.asc). Name of the input raster file", true, "",
69 "INPUT_FILE");
70 cmd.add(input_arg);
71 auto log_level_arg = BaseLib::makeLogLevelArg();
72 cmd.add(log_level_arg);
73 cmd.parse(argc, argv);
74
75 BaseLib::MPI::Setup mpi_setup(argc, argv);
76 BaseLib::initOGSLogger(log_level_arg.getValue());
77
78 std::string const input_name = input_arg.getValue().c_str();
79 std::string const output_name = output_arg.getValue().c_str();
80
81 std::unique_ptr<GeoLib::Raster> const raster(
83
84 MeshLib::MeshElemType const elem_type =
85 (arg_elem_type.getValue() == "tri") ? MeshLib::MeshElemType::TRIANGLE
87 MeshLib::UseIntensityAs intensity_type;
88 if (arg_pixel_type.getValue() == "elevation")
90 else if (arg_pixel_type.getValue() == "materials")
92 else
94
95 std::string array_name = "Values";
96 if (intensity_type == MeshLib::UseIntensityAs::DATAVECTOR &&
97 array_name_arg.isSet())
98 array_name = array_name_arg.getValue().c_str();
99 else if (intensity_type == MeshLib::UseIntensityAs::MATERIALS)
100 array_name = "MaterialIDs";
101
102 std::unique_ptr<MeshLib::Mesh> const mesh(
103 MeshToolsLib::RasterToMesh::convert(*raster, elem_type, intensity_type,
104 array_name));
105
106 if (mesh == nullptr)
107 {
108 ERR("Conversion failed.");
109 return EXIT_FAILURE;
110 }
111
112 MeshLib::IO::VtuInterface vtu(mesh.get());
113 vtu.writeToFile(output_name);
114 return EXIT_SUCCESS;
115}
Definition of the AsciiRasterInterface class.
Git information.
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:48
Definition of mesh-related Enumerations.
Definition of the Mesh class.
int main(int argc, char *argv[])
Definition of the GeoLib::Raster class.
Implementation of the VtuInterface class.
static GeoLib::Raster * getRasterFromASCFile(std::string const &fname)
Reads an ArcGis ASC raster file.
Reads and writes VtkXMLUnstructuredGrid-files (vtu) to and from OGS data structures....
bool writeToFile(std::filesystem::path const &file_path)
static std::unique_ptr< MeshLib::Mesh > convert(GeoLib::Raster const &raster, MeshLib::MeshElemType elem_type, MeshLib::UseIntensityAs intensity_type, std::string const &array_name="Colour")
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:64
GITINFOLIB_EXPORT const std::string ogs_version
UseIntensityAs
Selection of possible interpretations for intensities.
Definition MeshEnums.h:104
MeshElemType
Types of mesh elements supported by OpenGeoSys. Values are from VTKCellType enum.
Definition MeshEnums.h:48