OGS
DuplicateGeometry.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#include "DuplicateGeometry.h"
5
6#include <map>
7#include <utility>
8
9#include "BaseLib/Logging.h"
10#include "GeoLib/GEOObjects.h"
11#include "GeoLib/Point.h"
12#include "GeoLib/Polyline.h"
13#include "GeoLib/Surface.h"
14#include "GeoLib/Triangle.h"
15
16namespace GeoLib
17{
19 std::string const& input_name,
20 std::string output_name)
21 : _output_name(std::move(output_name)), _geo_objects(geo_objects)
22{
23 duplicate(input_name);
24}
25
26void DuplicateGeometry::duplicate(std::string const& input_name)
27{
28 std::vector<GeoLib::Point*> const* const pnts(
29 _geo_objects.getPointVec(input_name));
30 if (pnts == nullptr)
31 {
32 ERR("Geometry '{:s}' not found.", input_name);
33 return;
34 }
35
36 std::vector<GeoLib::Point*> new_pnts{};
37 new_pnts.reserve(pnts->size());
38 std::transform(pnts->cbegin(), pnts->cend(), std::back_inserter(new_pnts),
39 [](GeoLib::Point* point)
40 { return new GeoLib::Point(*point); });
41 PointVec::NameIdMap pnt_name_id_map(
42 _geo_objects.getPointVecObj(input_name)->getNameIDMapBegin(),
43 _geo_objects.getPointVecObj(input_name)->getNameIDMapEnd());
44 _geo_objects.addPointVec(std::move(new_pnts), _output_name,
45 std::move(pnt_name_id_map));
46
47 std::vector<GeoLib::Polyline*> const* plys(
48 _geo_objects.getPolylineVec(input_name));
49 if (plys)
50 {
51 auto new_plys = copyPolylinesVector(*plys);
52 PolylineVec::NameIdMap ply_name_id_map{
53 _geo_objects.getPolylineVecObj(input_name)->getNameIDMapBegin(),
54 _geo_objects.getPolylineVecObj(input_name)->getNameIDMapEnd()};
55 _geo_objects.addPolylineVec(std::move(new_plys), _output_name,
56 std::move(ply_name_id_map));
57 }
58
59 std::vector<GeoLib::Surface*> const* sfcs(
60 _geo_objects.getSurfaceVec(input_name));
61 if (sfcs)
62 {
63 auto new_sfcs = copySurfacesVector(*sfcs);
64 GeoLib::SurfaceVec::NameIdMap sfc_name_id_map{
65 _geo_objects.getSurfaceVecObj(input_name)->getNameIDMapBegin(),
66 _geo_objects.getSurfaceVecObj(input_name)->getNameIDMapEnd()};
67 _geo_objects.addSurfaceVec(std::move(new_sfcs), _output_name,
68 std::move(sfc_name_id_map));
69 }
70}
71
72std::vector<GeoLib::Polyline*> DuplicateGeometry::copyPolylinesVector(
73 std::vector<GeoLib::Polyline*> const& polylines) const
74{
75 std::size_t const n_plys = polylines.size();
76 std::vector<GeoLib::Polyline*> new_lines{n_plys, nullptr};
77
78 for (std::size_t i = 0; i < n_plys; ++i)
79 {
80 if (polylines[i] == nullptr)
81 {
82 continue;
83 }
84 new_lines[i] =
86 std::size_t const nLinePnts(polylines[i]->getNumberOfPoints());
87 for (std::size_t j = 0; j < nLinePnts; ++j)
88 {
89 new_lines[i]->addPoint(polylines[i]->getPointID(j));
90 }
91 }
92 return new_lines;
93}
94
96 std::vector<Surface*> const& surfaces) const
97{
98 std::size_t const n_sfc = surfaces.size();
99 std::vector<GeoLib::Surface*> new_surfaces{n_sfc, nullptr};
100
101 for (std::size_t i = 0; i < n_sfc; ++i)
102 {
103 if (surfaces[i] == nullptr)
104 {
105 continue;
106 }
107 new_surfaces[i] =
108 new GeoLib::Surface(*_geo_objects.getPointVec(_output_name));
109
110 std::size_t const n_tris(surfaces[i]->getNumberOfTriangles());
111 for (std::size_t j = 0; j < n_tris; ++j)
112 {
113 GeoLib::Triangle const* t = (*surfaces[i])[j];
114 new_surfaces[i]->addTriangle(t->getPoint(0)->getID(),
115 t->getPoint(1)->getID(),
116 t->getPoint(2)->getID());
117 }
118 }
119 return new_surfaces;
120}
121
122std::vector<GeoLib::Point*>& DuplicateGeometry::getPointVectorCopy()
123{
124 return const_cast<std::vector<GeoLib::Point*>&>(
125 *_geo_objects.getPointVec(_output_name));
126}
127
128std::vector<GeoLib::Polyline*>& DuplicateGeometry::getPolylineVectorCopy()
129{
130 return const_cast<std::vector<GeoLib::Polyline*>&>(
131 *_geo_objects.getPolylineVec(_output_name));
132}
133
134std::vector<GeoLib::Surface*>& DuplicateGeometry::getSurfaceVectorCopy()
135{
136 return const_cast<std::vector<GeoLib::Surface*>&>(
137 *_geo_objects.getSurfaceVec(_output_name));
138}
139
140} // namespace GeoLib
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
std::vector< GeoLib::Surface * > & getSurfaceVectorCopy()
void duplicate(std::string const &input_name)
GeoLib::GEOObjects & _geo_objects
std::vector< Surface * > copySurfacesVector(std::vector< Surface * > const &surfaces) const
DuplicateGeometry(GeoLib::GEOObjects &geo_objects, std::string const &input_name, std::string output_name)
std::vector< GeoLib::Point * > & getPointVectorCopy()
std::vector< GeoLib::Polyline * > & getPolylineVectorCopy()
std::vector< GeoLib::Polyline * > copyPolylinesVector(std::vector< GeoLib::Polyline * > const &polylines) const
Container class for geometric objects.
Definition GEOObjects.h:46
Class Polyline consists mainly of a reference to a point vector and a vector that stores the indices ...
Definition Polyline.h:29
A Surface is represented by Triangles. It consists of a reference to a vector of (pointers to) points...
std::map< std::string, std::size_t > NameIdMap
Definition TemplateVec.h:30
Class Triangle consists of a reference to a point vector and a vector that stores the indices in the ...
Definition Triangle.h:21
const Point * getPoint(std::size_t i) const
const access operator to access the i-th triangle Point
Definition Triangle.h:44
std::size_t getID() const