OGS
ExtractSurface.cpp
Go to the documentation of this file.
1
12#include <tclap/CmdLine.h>
13
14#ifdef USE_PETSC
15#include <mpi.h>
16#endif
17
18#include <algorithm>
19#include <memory>
20#include <string>
21#include <vector>
22
23#include "BaseLib/FileTools.h"
24#include "BaseLib/StringTools.h"
25#include "InfoLib/GitInfo.h"
29#include "MeshLib/Mesh.h"
31
32int main(int argc, char* argv[])
33{
34 TCLAP::CmdLine cmd(
35 "Extracts a 2D surface from a 3D input mesh by specifying a normal "
36 "vector and an angle. (The surface normal (0, 0, 0) will extract the "
37 "complete outer boundary of the 3D mesh.)\n"
38 "An extensive documentation can be found at "
39 "https://docs.opengeosys.org/docs/tools/meshing-submeshes/"
40 "extract-surface\n"
41 "OpenGeoSys-6 software, version " +
43 ".\n"
44 "Copyright (c) 2012-2024, OpenGeoSys Community "
45 "(http://www.opengeosys.org)",
47 TCLAP::SwitchArg use_ascii_arg("", "ascii-output",
48 "If the switch is set use ascii instead of "
49 "binary format for data in the vtu output.",
50 false);
51 cmd.add(use_ascii_arg);
52 TCLAP::ValueArg<double> angle_arg(
53 "a", "angle",
54 "tolerated angle (in degrees) between given normal and element normal",
55 false, 90, "floating point value");
56 cmd.add(angle_arg);
57 TCLAP::ValueArg<double> z("z", "z-component", "z component of the normal",
58 false, -1.0, "floating point value");
59 cmd.add(z);
60 TCLAP::ValueArg<double> y("y", "y-component", "y component of the normal",
61 false, 0, "floating point value");
62 cmd.add(y);
63 TCLAP::ValueArg<double> x("x", "x-component", "x component of the normal",
64 false, 0, "floating point value");
65 cmd.add(x);
66 TCLAP::ValueArg<std::string> mesh_out(
67 "o", "mesh-output-file",
68 "the name of the file the surface mesh should be written to", false, "",
69 "file name of output mesh");
70 cmd.add(mesh_out);
71 TCLAP::ValueArg<std::string> mesh_in(
72 "i", "mesh-input-file",
73 "the name of the file containing the input mesh", true, "",
74 "file name of input mesh");
75 cmd.add(mesh_in);
76 cmd.parse(argc, argv);
77
78#ifdef USE_PETSC
79 MPI_Init(&argc, &argv);
80#endif
81
82 std::unique_ptr<MeshLib::Mesh const> mesh(MeshLib::IO::readMeshFromFile(
83 mesh_in.getValue(), true /* compute_element_neighbors */));
84
85 if (!mesh)
86 {
87 ERR("Error reading mesh file.");
88#ifdef USE_PETSC
89 MPI_Finalize();
90#endif
91 return EXIT_FAILURE;
92 }
93
94 if (mesh->getDimension() != 3)
95 {
96 ERR("Surfaces can currently only be extracted from 3D meshes.");
97#ifdef USE_PETSC
98 MPI_Finalize();
99#endif
100 return EXIT_FAILURE;
101 }
102
103 INFO("Mesh read: {:d} nodes, {:d} elements.", mesh->getNumberOfNodes(),
104 mesh->getNumberOfElements());
105
106 // extract surface
107 Eigen::Vector3d const dir({x.getValue(), y.getValue(), z.getValue()});
108 double const angle(angle_arg.getValue());
109 std::unique_ptr<MeshLib::Mesh> surface_mesh(
111 *mesh, dir, angle, getBulkIDString(MeshLib::MeshItemType::Node),
112 getBulkIDString(MeshLib::MeshItemType::Cell),
113 getBulkIDString(MeshLib::MeshItemType::Face)));
114
115 std::string out_fname(mesh_out.getValue());
116 if (out_fname.empty())
117 {
118 out_fname = BaseLib::dropFileExtension(mesh_in.getValue()) + "_sfc.vtu";
119 }
120
121 auto const data_mode =
122 use_ascii_arg.getValue() ? vtkXMLWriter::Ascii : vtkXMLWriter::Binary;
123 MeshLib::IO::writeVtu(*surface_mesh, out_fname, data_mode);
124
125#ifdef USE_PETSC
126 MPI_Finalize();
127#endif
128 return EXIT_SUCCESS;
129}
int main(int argc, char *argv[])
Filename manipulation routines.
Git information.
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
Definition of the MeshSurfaceExtraction class.
Definition of the Mesh class.
Definition of string helper functions.
Implementation of the VtuInterface class.
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="")
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)
Definition of readMeshFromFile function.