OGS
ExtractSurface.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#include <tclap/CmdLine.h>
5
6#include <algorithm>
7#include <memory>
8#include <string>
9#include <vector>
10
11#include "BaseLib/FileTools.h"
12#include "BaseLib/Logging.h"
13#include "BaseLib/MPI.h"
14#include "BaseLib/StringTools.h"
16#include "InfoLib/GitInfo.h"
20#include "MeshLib/Mesh.h"
22
23int main(int argc, char* argv[])
24{
25 TCLAP::CmdLine cmd(
26 "Extracts a 2D surface from a 3D input mesh by specifying a normal "
27 "vector and an angle. (The surface normal (0, 0, 0) will extract the "
28 "complete outer boundary of the 3D mesh.)\n"
29 "An extensive documentation can be found at "
30 "https://docs.opengeosys.org/docs/tools/meshing-submeshes/"
31 "extract-surface\n"
32 "OpenGeoSys-6 software, version " +
34 ".\n"
35 "Copyright (c) 2012-2026, OpenGeoSys Community "
36 "(http://www.opengeosys.org)",
38 TCLAP::SwitchArg use_ascii_arg("", "ascii-output",
39 "If the switch is set use ascii instead of "
40 "binary format for data in the vtu output.",
41 false);
42 cmd.add(use_ascii_arg);
43 TCLAP::ValueArg<double> angle_arg(
44 "a", "angle",
45 "tolerated angle (in degrees) between given normal and "
46 "element normal, (min = 0), (max = 360)",
47 false, 90, "ANGLE");
48 cmd.add(angle_arg);
49 TCLAP::ValueArg<double> z("z", "z-component", "z component of the normal",
50 false, -1.0, "Z-COMPONENT");
51 cmd.add(z);
52 TCLAP::ValueArg<double> y("y", "y-component", "y component of the normal",
53 false, 0, "Y-COMPONENT");
54 cmd.add(y);
55 TCLAP::ValueArg<double> x("x", "x-component", "x component of the normal",
56 false, 0, "X-COMPONENT");
57 cmd.add(x);
58 TCLAP::ValueArg<std::string> mesh_out(
59 "o", "mesh-output-file",
60 "Output (.vtu). The name of the file the surface mesh should be "
61 "written to",
62 false, "", "OUTPUT_FILE");
63 cmd.add(mesh_out);
64 TCLAP::ValueArg<std::string> mesh_in(
65 "i", "mesh-input-file",
66 "Input (.vtu). The name of the file containing the input mesh", true,
67 "", "INPUT_FILE");
68 cmd.add(mesh_in);
69 auto log_level_arg = BaseLib::makeLogLevelArg();
70 cmd.add(log_level_arg);
71 cmd.parse(argc, argv);
72
73 BaseLib::MPI::Setup mpi_setup(argc, argv);
74 BaseLib::initOGSLogger(log_level_arg.getValue());
75
76 std::unique_ptr<MeshLib::Mesh const> mesh(MeshLib::IO::readMeshFromFile(
77 mesh_in.getValue(), true /* compute_element_neighbors */));
78
79 if (!mesh)
80 {
81 ERR("Error reading mesh file.");
82 return EXIT_FAILURE;
83 }
84
85 if (mesh->getDimension() != 3)
86 {
87 ERR("Surfaces can currently only be extracted from 3D meshes.");
88 return EXIT_FAILURE;
89 }
90
91 INFO("Mesh read: {:d} nodes, {:d} elements.", mesh->getNumberOfNodes(),
92 mesh->getNumberOfElements());
93
94 // extract surface
95 Eigen::Vector3d const dir({x.getValue(), y.getValue(), z.getValue()});
96 double const angle(angle_arg.getValue());
97 std::unique_ptr<MeshLib::Mesh> surface_mesh(
99 *mesh, dir, angle, getBulkIDString(MeshLib::MeshItemType::Node),
100 getBulkIDString(MeshLib::MeshItemType::Cell),
101 getBulkIDString(MeshLib::MeshItemType::Face)));
102
103 std::string out_fname(mesh_out.getValue());
104 if (out_fname.empty())
105 {
106 out_fname = BaseLib::dropFileExtension(mesh_in.getValue()) + "_sfc.vtu";
107 }
108
109 auto const data_mode =
110 use_ascii_arg.getValue() ? vtkXMLWriter::Ascii : vtkXMLWriter::Binary;
111 MeshLib::IO::writeVtu(*surface_mesh, out_fname, data_mode);
112
113 return EXIT_SUCCESS;
114}
int main(int argc, char *argv[])
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
static MeshLib::Mesh * getMeshSurface(const MeshLib::Mesh &subsfc_mesh, Eigen::Vector3d const &dir, double angle, std::string_view subsfc_node_id_prop_name="", std::string_view subsfc_element_id_prop_name="", std::string_view face_id_prop_name="")
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:56
std::string dropFileExtension(std::string const &filename)
GITINFOLIB_EXPORT const std::string ogs_version
int writeVtu(MeshLib::Mesh const &mesh, std::string const &file_name, int const data_mode)
MeshLib::Mesh * readMeshFromFile(const std::string &file_name, bool const compute_element_neighbors)