OGS
swapNodeCoordinateAxes.cpp
Go to the documentation of this file.
1
10#include <tclap/CmdLine.h>
11
12#include <array>
13#include <memory>
14#include <string>
15
16#include "BaseLib/Logging.h"
17#include "BaseLib/MPI.h"
18#include "InfoLib/GitInfo.h"
21#include "MeshLib/Mesh.h"
22#include "MeshLib/Node.h"
23
24static void swapNodeCoordinateAxes(MeshLib::Mesh const& mesh,
25 std::array<int, 3> const& new_axes_indices)
26{
27 double new_coords[3] = {};
28 for (MeshLib::Node* node : mesh.getNodes())
29 {
30 for (int i = 0; i < 3; i++)
31 {
32 new_coords[i] = (*node)[new_axes_indices[i]];
33 }
34 for (int i = 0; i < 3; i++)
35 {
36 (*node)[i] = new_coords[i];
37 }
38 }
39}
40
41static bool parseNewOrder(std::string const& str_order,
42 std::array<int, 3>& new_axes_indices)
43{
44 if (str_order.length() != 3)
45 {
46 ERR("Invalid argument for the new order. The argument should contain "
47 "three characters.");
48 return false;
49 }
50
51 new_axes_indices.fill(-1);
52
53 for (int i = 0; i < 3; i++)
54 {
55 if (str_order[i] == 'x')
56 {
57 new_axes_indices[i] = 0;
58 }
59 else if (str_order[i] == 'y')
60 {
61 new_axes_indices[i] = 1;
62 }
63 else if (str_order[i] == 'z')
64 {
65 new_axes_indices[i] = 2;
66 }
67 else
68 {
69 ERR("Invalid argument for the new order. The given argument "
70 "contains a character other than 'x', 'y', 'z'.");
71 return false;
72 }
73 }
74
75 bool isAxisSet[3] = {false};
76 for (int new_axes_indice : new_axes_indices)
77 {
78 if (isAxisSet[new_axes_indice])
79 {
80 ERR("Invalid argument for the new order. The argument contains "
81 "some character used more than once.");
82 return false;
83 }
84 isAxisSet[new_axes_indice] = true;
85 }
86
87 return true;
88}
89
90int main(int argc, char* argv[])
91{
92 TCLAP::CmdLine cmd(
93 "Swap node coordinate values.\n\n"
94 "OpenGeoSys-6 software, version " +
96 ".\n"
97 "Copyright (c) 2012-2024, OpenGeoSys Community "
98 "(http://www.opengeosys.org)",
100 TCLAP::ValueArg<std::string> input_arg(
101 "i", "input-mesh-file", "input mesh file", true, "", "string");
102 cmd.add(input_arg);
103 TCLAP::ValueArg<std::string> output_arg(
104 "o", "output-mesh-file", "output mesh file", true, "", "string");
105 cmd.add(output_arg);
106 TCLAP::ValueArg<std::string> new_order_arg(
107 "n", "new-order",
108 "the new order of swapped coordinate values (e.g. 'xzy' for converting "
109 "XYZ values to XZY values)",
110 true, "", "string");
111 cmd.add(new_order_arg);
112 cmd.parse(argc, argv);
113
114 BaseLib::MPI::Setup mpi_setup(argc, argv);
115
116 const std::string str_order = new_order_arg.getValue();
117 std::array<int, 3> new_order = {{}};
118 if (!parseNewOrder(str_order, new_order))
119 {
120 return EXIT_FAILURE;
121 }
122
123 std::unique_ptr<MeshLib::Mesh> mesh(
124 MeshLib::IO::readMeshFromFile(input_arg.getValue()));
125 if (!mesh)
126 {
127 return EXIT_FAILURE;
128 }
129
130 if (mesh->getDimension() == 3)
131 {
132 WARN(
133 "Swapping coordinate values of 3D elements can result in incorrect "
134 "node-ordering.");
135 }
136
137 INFO("Exchange node coordinates from xyz to {:s}",
138 new_order_arg.getValue().data());
139 swapNodeCoordinateAxes(*mesh, new_order);
140
141 INFO("Save the new mesh into a file");
142 MeshLib::IO::writeMeshToFile(*mesh, output_arg.getValue());
143
144 return EXIT_SUCCESS;
145}
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
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
Definition of the Mesh class.
Definition of the Node class.
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition Mesh.h:106
GITINFOLIB_EXPORT const std::string ogs_version
MeshLib::Mesh * readMeshFromFile(const std::string &file_name, bool const compute_element_neighbors)
int writeMeshToFile(const MeshLib::Mesh &mesh, std::filesystem::path const &file_path, std::set< std::string > variable_output_names)
Definition of readMeshFromFile function.
int main(int argc, char *argv[])
static bool parseNewOrder(std::string const &str_order, std::array< int, 3 > &new_axes_indices)
static void swapNodeCoordinateAxes(MeshLib::Mesh const &mesh, std::array< int, 3 > const &new_axes_indices)