OGS
swapNodeCoordinateAxes.cpp File Reference

Detailed Description

Definition in file swapNodeCoordinateAxes.cpp.

#include <tclap/CmdLine.h>
#include <array>
#include <memory>
#include <string>
#include "BaseLib/Logging.h"
#include "BaseLib/MPI.h"
#include "BaseLib/TCLAPArguments.h"
#include "InfoLib/GitInfo.h"
#include "MeshLib/IO/readMeshFromFile.h"
#include "MeshLib/IO/writeMeshToFile.h"
#include "MeshLib/Mesh.h"
#include "MeshLib/Node.h"
Include dependency graph for swapNodeCoordinateAxes.cpp:

Go to the source code of this file.

Functions

static void swapNodeCoordinateAxes (MeshLib::Mesh const &mesh, std::array< int, 3 > const &new_axes_indices)
 
static bool parseNewOrder (std::string const &str_order, std::array< int, 3 > &new_axes_indices)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

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

Definition at line 91 of file swapNodeCoordinateAxes.cpp.

92{
93 TCLAP::CmdLine cmd(
94 "Swap node coordinate values.\n\n"
95 "OpenGeoSys-6 software, version " +
97 ".\n"
98 "Copyright (c) 2012-2025, OpenGeoSys Community "
99 "(http://www.opengeosys.org)",
101 TCLAP::ValueArg<std::string> input_arg("i", "input-mesh-file",
102 "Input (.vtu) mesh file", true, "",
103 "INPUT_FILE");
104 cmd.add(input_arg);
105 TCLAP::ValueArg<std::string> output_arg("o", "output-mesh-file",
106 "Output (.vtu) mesh file", true, "",
107 "OUTPUT_FILE");
108 cmd.add(output_arg);
109 TCLAP::ValueArg<std::string> new_order_arg(
110 "n", "new-order",
111 "the new order of swapped coordinate values (e.g. 'xzy' for converting "
112 "XYZ values to XZY values)",
113 true, "", "NEW_ORDER");
114 cmd.add(new_order_arg);
115 auto log_level_arg = BaseLib::makeLogLevelArg();
116 cmd.add(log_level_arg);
117 cmd.parse(argc, argv);
118
119 BaseLib::MPI::Setup mpi_setup(argc, argv);
120 BaseLib::initOGSLogger(log_level_arg.getValue());
121
122 const std::string str_order = new_order_arg.getValue();
123 std::array<int, 3> new_order = {{}};
124 if (!parseNewOrder(str_order, new_order))
125 {
126 return EXIT_FAILURE;
127 }
128
129 std::unique_ptr<MeshLib::Mesh> mesh(
130 MeshLib::IO::readMeshFromFile(input_arg.getValue()));
131 if (!mesh)
132 {
133 return EXIT_FAILURE;
134 }
135
136 if (mesh->getDimension() == 3)
137 {
138 WARN(
139 "Swapping coordinate values of 3D elements can result in incorrect "
140 "node-ordering.");
141 }
142
143 INFO("Exchange node coordinates from xyz to {:s}",
144 new_order_arg.getValue().data());
145 swapNodeCoordinateAxes(*mesh, new_order);
146
147 INFO("Save the new mesh into a file");
148 MeshLib::IO::writeMeshToFile(*mesh, output_arg.getValue());
149
150 return EXIT_SUCCESS;
151}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:36
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:42
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:64
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)
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)

References INFO(), BaseLib::initOGSLogger(), BaseLib::makeLogLevelArg(), GitInfoLib::GitInfo::ogs_version, parseNewOrder(), MeshLib::IO::readMeshFromFile(), swapNodeCoordinateAxes(), WARN(), and MeshLib::IO::writeMeshToFile().

◆ parseNewOrder()

static bool parseNewOrder ( std::string const & str_order,
std::array< int, 3 > & new_axes_indices )
static

Definition at line 42 of file swapNodeCoordinateAxes.cpp.

44{
45 if (str_order.length() != 3)
46 {
47 ERR("Invalid argument for the new order. The argument should contain "
48 "three characters.");
49 return false;
50 }
51
52 new_axes_indices.fill(-1);
53
54 for (int i = 0; i < 3; i++)
55 {
56 if (str_order[i] == 'x')
57 {
58 new_axes_indices[i] = 0;
59 }
60 else if (str_order[i] == 'y')
61 {
62 new_axes_indices[i] = 1;
63 }
64 else if (str_order[i] == 'z')
65 {
66 new_axes_indices[i] = 2;
67 }
68 else
69 {
70 ERR("Invalid argument for the new order. The given argument "
71 "contains a character other than 'x', 'y', 'z'.");
72 return false;
73 }
74 }
75
76 bool isAxisSet[3] = {false};
77 for (int new_axes_indice : new_axes_indices)
78 {
79 if (isAxisSet[new_axes_indice])
80 {
81 ERR("Invalid argument for the new order. The argument contains "
82 "some character used more than once.");
83 return false;
84 }
85 isAxisSet[new_axes_indice] = true;
86 }
87
88 return true;
89}
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:48

References ERR().

Referenced by main().

◆ swapNodeCoordinateAxes()

static void swapNodeCoordinateAxes ( MeshLib::Mesh const & mesh,
std::array< int, 3 > const & new_axes_indices )
static

Definition at line 25 of file swapNodeCoordinateAxes.cpp.

27{
28 double new_coords[3] = {};
29 for (MeshLib::Node* node : mesh.getNodes())
30 {
31 for (int i = 0; i < 3; i++)
32 {
33 new_coords[i] = (*node)[new_axes_indices[i]];
34 }
35 for (int i = 0; i < 3; i++)
36 {
37 (*node)[i] = new_coords[i];
38 }
39 }
40}
std::vector< std::size_t > getNodes(GeoLib::Point const &pnt, std::vector< MeshLib::Node * > const &nodes, MeshLib::PropertyVector< int > const &mat_ids, std::pair< int, int > const &mat_limits, std::pair< double, double > const &elevation_limits, MeshLib::Mesh const &mesh)

References MeshLib::Mesh::getNodes().

Referenced by main().