OGS
RemoveUnusedPoints.cpp File Reference

Detailed Description

Definition in file RemoveUnusedPoints.cpp.

#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 "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 31 of file RemoveUnusedPoints.cpp.

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

Referenced by main().

◆ main()

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

Definition at line 40 of file RemoveUnusedPoints.cpp.

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

References createMapping(), ERR(), GeoLib::GEOObjects::getGeometryNames(), GeoLib::GEOObjects::getPointVec(), GeoLib::GEOObjects::getPolylineVec(), GeoLib::GEOObjects::getSurfaceVec(), INFO(), GeoLib::markUsedPoints(), GitInfoLib::GitInfo::ogs_version, GeoLib::resetPointIDs(), WARN(), and BaseLib::IO::writeStringToFile().