OGS
RemoveUnusedPoints.cpp File Reference
#include <tclap/CmdLine.h>
#include <algorithm>
#include <memory>
#include <range/v3/algorithm/for_each.hpp>
#include <range/v3/range/conversion.hpp>
#include <range/v3/view/filter.hpp>
#include <range/v3/view/map.hpp>
#include <range/v3/view/transform.hpp>
#include <range/v3/view/zip.hpp>
#include <vector>
#include "BaseLib/Logging.h"
#include "BaseLib/MPI.h"
#include "BaseLib/TCLAPArguments.h"
#include "GeoLib/GEOObjects.h"
#include "GeoLib/IO/XmlIO/Boost/BoostXmlGmlInterface.h"
#include "GeoLib/Point.h"
#include "GeoLib/Polyline.h"
#include "GeoLib/Surface.h"
#include "GeoLib/Triangle.h"
#include "InfoLib/GitInfo.h"
Include dependency graph for RemoveUnusedPoints.cpp:

Go to the source code of this file.

Functions

auto createMapping (std::vector< bool > const &used_points)
int main (int argc, char *argv[])

Function Documentation

◆ createMapping()

auto createMapping ( std::vector< bool > const & used_points)

Definition at line 27 of file RemoveUnusedPoints.cpp.

28{
29 auto getCountOrMax = [cnt = 0u](bool used) mutable
30 { return used ? cnt++ : std::numeric_limits<std::size_t>::max(); };
31
32 return used_points | ranges::views::transform(getCountOrMax) |
33 ranges::to<std::vector>;
34}

Referenced by main().

◆ main()

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

Definition at line 36 of file RemoveUnusedPoints.cpp.

37{
38 TCLAP::CmdLine cmd(
39 "Remove points from geometry that are not used in any polyline nor "
40 "surface. Attention: Names of points are not handled correctly in "
41 "every case.\n\n"
42 "OpenGeoSys-6 software, version " +
44 ".\n"
45 "Copyright (c) 2012-2026, OpenGeoSys Community "
46 "(http://www.opengeosys.org)",
48
49 TCLAP::ValueArg<std::string> geo_output_arg(
50 "o", "output", "Output (.gml) geometry", true, "", "OUTPUT_FILE");
51 cmd.add(geo_output_arg);
52 TCLAP::ValueArg<std::string> geo_input_arg(
53 "i", "input", "Input (.gml) geometry", true, "conceptual model",
54 "INPUT_FILE");
55 cmd.add(geo_input_arg);
56 auto log_level_arg = BaseLib::makeLogLevelArg();
57 cmd.add(log_level_arg);
58 cmd.parse(argc, argv);
59
60 BaseLib::MPI::Setup mpi_setup(argc, argv);
61 BaseLib::initOGSLogger(log_level_arg.getValue());
62
63 GeoLib::GEOObjects geometry;
65 try
66 {
67 if (!xml.readFile(geo_input_arg.getValue()))
68 {
69 ERR("Failed to read file `{:s}'.", geo_input_arg.getValue());
70 return EXIT_FAILURE;
71 }
72 }
73 catch (std::runtime_error const& err)
74 {
75 ERR("Failed to read file `{:s}'.", geo_input_arg.getValue());
76 ERR("{:s}", err.what());
77 return EXIT_FAILURE;
78 }
79
80 auto const geo_name = geometry.getGeometryNames()[0];
81 auto* points = const_cast<std::vector<GeoLib::Point*>*>(
82 geometry.getPointVec(geo_name));
83 auto* polylines = geometry.getPolylineVec(geo_name);
84 auto* surfaces = geometry.getSurfaceVec(geo_name);
85
86 // mark used points
87 auto used_points = std::vector<bool>(points->size(), false);
88
89 auto mark = [&](auto const* const object)
90 { GeoLib::markUsedPoints(*object, used_points); };
91
92 if (polylines)
93 {
94 ranges::for_each(*polylines, mark);
95 }
96 if (surfaces)
97 {
98 ranges::for_each(*surfaces, mark);
99 }
100
101 auto const number_of_used_points = static_cast<std::size_t>(
102 std::count(used_points.begin(), used_points.end(), true));
103 if (number_of_used_points == 0)
104 {
105 INFO(
106 "Geometry consists of points only. A new geometry file won't "
107 "be written.");
108 return EXIT_SUCCESS;
109 }
110 INFO(
111 "{} points in this geometry file are not used in any polyline or "
112 "surface",
113 points->size() - number_of_used_points);
114
115 auto const mapping = createMapping(used_points);
116
117 auto reset = [&mapping](auto* object)
118 { GeoLib::resetPointIDs(*object, mapping); };
119
120 // reset point ids is polylines
121 if (polylines)
122 {
123 ranges::for_each(*polylines, reset);
124 }
125 // reset point ids is surfaces
126 if (surfaces)
127 {
128 ranges::for_each(*surfaces, reset);
129 }
130
131 std::stringstream unused_points_info;
132 unused_points_info << std::fixed;
133
134 // cleanup unused points
135 for (GeoLib::Point*& point :
136 ranges::views::zip(*points, used_points) |
137 ranges::views::filter([](auto&& pair) { return !pair.second; }) |
138 ranges::views::keys)
139 {
140 unused_points_info << point->getID() << " " << *point << '\n';
141 delete point;
142 point = nullptr;
143 }
144
145 std::erase(*points, nullptr);
146 if (!unused_points_info.str().empty())
147 {
148 INFO("Removed the following points:\n{}", unused_points_info.str());
149 }
150
151 WARN("Names of points are not handled correctly in every case.");
152
153 xml.export_name = geometry.getGeometryNames()[0];
155 geo_output_arg.getValue());
156
157 return EXIT_SUCCESS;
158}
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
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34
auto createMapping(std::vector< bool > const &used_points)
std::string writeToString()
Writes the object to a string.
Definition Writer.cpp:20
Container class for geometric objects.
Definition GEOObjects.h:46
std::vector< std::string > getGeometryNames() const
Returns the names of all geometry vectors.
const std::vector< Point * > * getPointVec(const std::string &name) const
const std::vector< Surface * > * getSurfaceVec(const std::string &name) const
Returns the surface vector with the given name as a const.
const std::vector< Polyline * > * getPolylineVec(const std::string &name) const
bool readFile(const std::string &fname) override
Reads an xml-file containing OGS geometry.
int writeStringToFile(std::string_view content, std::filesystem::path const &file_path)
Definition Writer.cpp:34
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:56
void markUsedPoints(Polyline const &polyline, std::vector< bool > &used_points)
Resets the point IDs of the polyline corresponding to the mapping.
Definition Polyline.cpp:479
void resetPointIDs(Polyline &polyline, std::vector< std::size_t > const &mapping)
Resets the point IDs of the polyline corresponding to the mapping.
Definition Polyline.cpp:464
GITINFOLIB_EXPORT const std::string ogs_version

References createMapping(), ERR(), BaseLib::IO::XMLInterface::export_name, GeoLib::GEOObjects::getGeometryNames(), GeoLib::GEOObjects::getPointVec(), GeoLib::GEOObjects::getPolylineVec(), GeoLib::GEOObjects::getSurfaceVec(), INFO(), BaseLib::initOGSLogger(), BaseLib::makeLogLevelArg(), GeoLib::markUsedPoints(), GitInfoLib::GitInfo::ogs_version, GeoLib::IO::BoostXmlGmlInterface::readFile(), GeoLib::resetPointIDs(), WARN(), BaseLib::IO::writeStringToFile(), and BaseLib::IO::Writer::writeToString().