OGS
Raster2Mesh.cpp File Reference
#include <tclap/CmdLine.h>
#include <memory>
#include <string>
#include "BaseLib/Logging.h"
#include "BaseLib/MPI.h"
#include "BaseLib/TCLAPArguments.h"
#include "GeoLib/IO/AsciiRasterInterface.h"
#include "GeoLib/Raster.h"
#include "InfoLib/GitInfo.h"
#include "MeshLib/IO/VtkIO/VtuInterface.h"
#include "MeshLib/Mesh.h"
#include "MeshLib/MeshEnums.h"
#include "MeshToolsLib/MeshGenerators/RasterToMesh.h"
Include dependency graph for Raster2Mesh.cpp:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])

Function Documentation

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 20 of file Raster2Mesh.cpp.

21{
22 TCLAP::CmdLine cmd(
23 "Converts an ASCII raster file (*.asc) into a 2D triangle- or "
24 "quad-mesh. Pixel values can be interpreted as elevation of mesh nodes "
25 "or as scalar values for mesh elements.\nIt is highly recommended to "
26 "create triangle meshes when interpreting pixel values as elevation, "
27 "as it is likely that the resulting mesh will otherwise contain "
28 "deformed (i.e. non-planar) quad-elements.\n\n"
29 "OpenGeoSys-6 software, version " +
31 ".\n"
32 "Copyright (c) 2012-2026, OpenGeoSys Community "
33 "(http://www.opengeosys.org)",
35 TCLAP::ValueArg<std::string> array_name_arg(
36 "n", "arrayname",
37 "Name of the scalar array. Only required if assigning pixel values to "
38 "cell data has been selected (default name is 'Values').",
39 false, "", "ARRAY_NAME");
40 cmd.add(array_name_arg);
41 std::vector<std::string> pixel_vals{"elevation", "materials", "scalar"};
42 TCLAP::ValuesConstraint<std::string> pixel_val_options(pixel_vals);
43 TCLAP::ValueArg<std::string> arg_pixel_type(
44 "p", "pixel-type",
45 "The choice how pixel values should be interpreted by the software: "
46 "'elevation' adjusts z-coordinates; 'materials' sets (integer) "
47 "material IDs; 'scalar' creates a (floating-point) array associated. "
48 "with mesh elements.",
49 true, "", &pixel_val_options);
50 cmd.add(arg_pixel_type);
51 std::vector<std::string> allowed_elems{"tri", "quad"};
52 TCLAP::ValuesConstraint<std::string> allowed_elem_vals(allowed_elems);
53 TCLAP::ValueArg<std::string> arg_elem_type(
54 "e", "elem-type", "The element type used in the resulting OGS mesh",
55 true, "", &allowed_elem_vals);
56 cmd.add(arg_elem_type);
57 TCLAP::ValueArg<std::string> output_arg(
58 "o", "output", "Output (.vtu). Name of the output mesh file", true, "",
59 "OUTPUT_FILE");
60 cmd.add(output_arg);
61 TCLAP::ValueArg<std::string> input_arg(
62 "i", "input", "Input (.asc). Name of the input raster file", true, "",
63 "INPUT_FILE");
64 cmd.add(input_arg);
65 auto log_level_arg = BaseLib::makeLogLevelArg();
66 cmd.add(log_level_arg);
67 cmd.parse(argc, argv);
68
69 BaseLib::MPI::Setup mpi_setup(argc, argv);
70 BaseLib::initOGSLogger(log_level_arg.getValue());
71
72 std::string const input_name = input_arg.getValue().c_str();
73 std::string const output_name = output_arg.getValue().c_str();
74
75 std::unique_ptr<GeoLib::Raster> const raster(
77
78 MeshLib::MeshElemType const elem_type =
79 (arg_elem_type.getValue() == "tri") ? MeshLib::MeshElemType::TRIANGLE
81 MeshLib::UseIntensityAs intensity_type;
82 if (arg_pixel_type.getValue() == "elevation")
84 else if (arg_pixel_type.getValue() == "materials")
86 else
88
89 std::string array_name = "Values";
90 if (intensity_type == MeshLib::UseIntensityAs::DATAVECTOR &&
91 array_name_arg.isSet())
92 array_name = array_name_arg.getValue().c_str();
93 else if (intensity_type == MeshLib::UseIntensityAs::MATERIALS)
94 array_name = "MaterialIDs";
95
96 std::unique_ptr<MeshLib::Mesh> const mesh(
97 MeshToolsLib::RasterToMesh::convert(*raster, elem_type, intensity_type,
98 array_name));
99
100 if (mesh == nullptr)
101 {
102 ERR("Conversion failed.");
103 return EXIT_FAILURE;
104 }
105
106 MeshLib::IO::VtuInterface vtu(mesh.get());
107 vtu.writeToFile(output_name);
108 return EXIT_SUCCESS;
109}
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
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....
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:56
GITINFOLIB_EXPORT const std::string ogs_version
UseIntensityAs
Selection of possible interpretations for intensities.
Definition MeshEnums.h:93
MeshElemType
Types of mesh elements supported by OpenGeoSys. Values are from VTKCellType enum.
Definition MeshEnums.h:37

References MeshToolsLib::RasterToMesh::convert(), MeshLib::DATAVECTOR, MeshLib::ELEVATION, ERR(), FileIO::AsciiRasterInterface::getRasterFromASCFile(), BaseLib::initOGSLogger(), BaseLib::makeLogLevelArg(), MeshLib::MATERIALS, GitInfoLib::GitInfo::ogs_version, MeshLib::QUAD, MeshLib::TRIANGLE, and MeshLib::IO::VtuInterface::writeToFile().