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