OGS
GEOModels.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// ** INCLUDES **
5#include "GEOModels.h"
6
8#include "Base/OGSError.h"
9#include "BaseLib/Logging.h"
10#include "GeoLib/Triangle.h"
11#include "GeoTreeModel.h"
12#include "StationTreeModel.h"
13
14GEOModels::GEOModels(GeoLib::GEOObjects& geo_objects, QObject* parent /*= 0*/)
15 : QObject(parent), _geo_objects(geo_objects)
16{
17 _geo_objects._callbacks = std::make_unique<GEOModelsCallbacks>(*this);
18
19 _geoModel = new GeoTreeModel();
21}
22
24{
25 delete _stationModel;
26 delete _geoModel;
27}
28
29void GEOModels::updateGeometry(const std::string& geo_name)
30{
31 if (auto const stations = _geo_objects.getStationVec(geo_name);
32 stations != nullptr)
33 {
34 emit stationVectorRemoved(_stationModel, geo_name);
35 _stationModel->removeStationList(geo_name);
36 _stationModel->addStationList(QString::fromStdString(geo_name),
37 stations);
38 emit stationVectorAdded(_stationModel, geo_name);
39 return;
40 }
41
42 if (auto const points = _geo_objects.getPointVecObj(geo_name);
43 points != nullptr)
44 {
46 this->_geoModel->removeGeoList(geo_name, GeoLib::GEOTYPE::POINT);
47 _geoModel->addPointList(QString::fromStdString(geo_name), *points);
49
50 if (auto const lines = _geo_objects.getPolylineVecObj(geo_name);
51 lines != nullptr)
52 {
54 this->_geoModel->removeGeoList(geo_name, GeoLib::GEOTYPE::POLYLINE);
55 _geoModel->addPolylineList(QString::fromStdString(geo_name),
56 *lines);
58 }
59
60 if (auto const surfaces = _geo_objects.getSurfaceVecObj(geo_name);
61 surfaces != nullptr)
62 {
64 this->_geoModel->removeGeoList(geo_name, GeoLib::GEOTYPE::SURFACE);
65 _geoModel->addSurfaceList(QString::fromStdString(geo_name),
66 *surfaces);
68 }
69 }
70 else
71 ERR("GEOModels::updateGeometry() - Geometry '{:s}' not found.",
72 geo_name);
73}
74
75void GEOModels::removeGeometry(std::string const& geo_name,
76 GeoLib::GEOTYPE const type)
77{
78 if (type == GeoLib::GEOTYPE::SURFACE)
79 {
80 _geo_objects.removeSurfaceVec(geo_name);
81 }
82 if (type == GeoLib::GEOTYPE::POLYLINE)
83 {
84 _geo_objects.removePolylineVec(geo_name);
85 }
86 if (type == GeoLib::GEOTYPE::POINT)
87 {
88 _geo_objects.removePointVec(geo_name);
89 }
90}
91
92std::vector<std::string> GEOModels::getGeometryNames() const
93{
94 return _geo_objects.getGeometryNames();
95}
96const std::vector<GeoLib::Point*>* GEOModels::getPointVec(
97 const std::string& name) const
98{
99 return _geo_objects.getPointVec(name);
100}
101
102void GEOModels::addPointVec(std::string const& name)
103{
104 _geoModel->addPointList(QString::fromStdString(name),
105 *_geo_objects.getPointVecObj(name));
107}
108
109void GEOModels::removePointVec(std::string const& name)
110{
111 assert(!_geo_objects.isPntVecUsed(name));
112
114 this->_geoModel->removeGeoList(name, GeoLib::GEOTYPE::POINT);
115}
116
117void GEOModels::addStationVec(std::string const& name)
118{
119 _stationModel->addStationList(QString::fromStdString(name),
120 _geo_objects.getStationVec(name));
122}
123
124void GEOModels::removeStationVec(std::string const& name)
125{
127 _stationModel->removeStationList(name);
128}
129
130void GEOModels::addPolylineVec(std::string const& name)
131{
132 _geoModel->addPolylineList(QString::fromStdString(name),
133 *_geo_objects.getPolylineVecObj(name));
135}
136
137void GEOModels::appendPolylineVec(std::string const& name)
138{
139 this->_geoModel->appendPolylines(name,
140 *_geo_objects.getPolylineVecObj(name));
141}
142
143void GEOModels::removePolylineVec(std::string const& name)
144{
146 this->_geoModel->removeGeoList(name, GeoLib::GEOTYPE::POLYLINE);
147}
148
149void GEOModels::addSurfaceVec(std::string const& name)
150{
151 _geoModel->addSurfaceList(QString::fromStdString(name),
152 *_geo_objects.getSurfaceVecObj(name));
154}
155
156void GEOModels::appendSurfaceVec(std::string const& name)
157{
158 _geoModel->appendSurfaces(name, *_geo_objects.getSurfaceVecObj(name));
159}
160
161void GEOModels::removeSurfaceVec(std::string const& name)
162{
164 _geoModel->removeGeoList(name, GeoLib::GEOTYPE::SURFACE);
165}
166
167void GEOModels::renameGeometry(std::string const& old_name,
168 std::string const& new_name)
169{
170 _geoModel->renameGeometry(old_name, new_name);
171 updateGeometry(new_name);
172}
173
175 const std::string& geoName, std::vector<std::size_t> const& indexlist,
176 double const proximity, std::string const& ply_name, bool const closePly,
177 bool const triangulatePly)
178{
179 GeoLib::PolylineVec* plyVec = _geo_objects.getPolylineVecObj(geoName);
180
181 if (plyVec)
182 {
183 auto const& polylines = plyVec->getVector();
184 std::vector<GeoLib::Polyline*> ply_list;
185 std::transform(indexlist.begin(), indexlist.end(),
186 std::back_inserter(ply_list),
187 [polylines](auto const& ply_index)
188 { return polylines[ply_index]; });
189
190 // connect polylines
191 GeoLib::Polyline* new_line =
193 proximity);
194
195 if (new_line)
196 {
197 // insert result in a new vector of polylines (because the
198 // GEOObjects::appendPolylines()-method requires a vector)
199 std::vector<GeoLib::Polyline*> connected_ply;
200
201 connected_ply.push_back(new_line);
202 _geo_objects.appendPolylineVec(connected_ply, geoName);
203
204 if (closePly)
205 {
206 new_line->closePolyline();
207
208 if (triangulatePly)
209 {
210 INFO(
211 "Creating a surface by triangulation of the polyline "
212 "...");
213 if (auto sfc =
215 {
216 std::vector<GeoLib::Surface*> new_sfc;
217 new_sfc.push_back(sfc.release());
218 _geo_objects.appendSurfaceVec(new_sfc, geoName);
219 INFO("\t done");
220 }
221 else
222 {
223 WARN(
224 "\t Creating a surface by triangulation of the "
225 "polyline failed.");
226 }
227 plyVec = _geo_objects.getPolylineVecObj(geoName);
228 }
229 }
230
231 if (!ply_name.empty())
232 {
233 plyVec->setNameOfElementByID(polylines.size(), ply_name);
234 }
235 }
236 else
237 {
238 OGSError::box("Error connecting polyines.");
239 }
240 }
241 else
242 {
243 OGSError::box("Corresponding geometry not found.");
244 }
245}
246
247void GEOModels::addNameForElement(std::string const& geometry_name,
248 GeoLib::GEOTYPE const object_type,
249 std::size_t const id,
250 std::string const& new_name)
251{
252 if (object_type == GeoLib::GEOTYPE::POINT)
253 {
254 _geo_objects.getPointVecObj(geometry_name)
255 ->setNameForElement(id, new_name);
256 }
257 else if (object_type == GeoLib::GEOTYPE::POLYLINE)
258 {
259 _geo_objects.getPolylineVecObj(geometry_name)
260 ->setNameForElement(id, new_name);
261 }
262 else if (object_type == GeoLib::GEOTYPE::SURFACE)
263 {
264 _geo_objects.getSurfaceVecObj(geometry_name)
265 ->setNameForElement(id, new_name);
266 }
267 else
268 ERR("GEOModels::addNameForElement() - Unknown GEOTYPE {:s}.",
269 GeoLib::convertGeoTypeToString(object_type));
270}
271
272void GEOModels::addNameForObjectPoints(const std::string& geometry_name,
273 const GeoLib::GEOTYPE object_type,
274 const std::string& geo_object_name,
275 const std::string& new_name)
276{
277 const GeoLib::GeoObject* obj =
278 _geo_objects.getGeoObject(geometry_name, object_type, geo_object_name);
279 GeoLib::PointVec* pnt_vec = _geo_objects.getPointVecObj(geometry_name);
280 if (object_type == GeoLib::GEOTYPE::POLYLINE)
281 {
282 const auto* ply = dynamic_cast<const GeoLib::Polyline*>(obj);
283 std::size_t nPoints = ply->getNumberOfPoints();
284 for (std::size_t i = 0; i < nPoints; i++)
285 {
286 pnt_vec->setNameForElement(
287 ply->getPointID(i),
288 new_name + "_Point" + std::to_string(ply->getPointID(i)));
289 }
290 }
291 else if (object_type == GeoLib::GEOTYPE::SURFACE)
292 {
293 const auto* sfc = dynamic_cast<const GeoLib::Surface*>(obj);
294 std::size_t nTriangles = sfc->getNumberOfTriangles();
295 for (std::size_t i = 0; i < nTriangles; i++)
296 {
297 const GeoLib::Triangle* tri = (*sfc)[i];
298 pnt_vec->setNameForElement(
299 (*tri)[0], new_name + "_Point" + std::to_string((*tri)[0]));
300 pnt_vec->setNameForElement(
301 (*tri)[1], new_name + "_Point" + std::to_string((*tri)[1]));
302 pnt_vec->setNameForElement(
303 (*tri)[2], new_name + "_Point" + std::to_string((*tri)[2]));
304 }
305 }
306 else
307 ERR("GEOModels::addNameForObjectPoints() - Unknown GEOTYPE {:s}.",
308 GeoLib::convertGeoTypeToString(object_type));
309}
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
std::vector< std::string > getGeometryNames() const
Definition GEOModels.cpp:92
GeoLib::GEOObjects & _geo_objects
Definition GEOModels.h:99
void stationVectorAdded(StationTreeModel *model, std::string name)
void addStationVec(std::string const &name)
void addNameForObjectPoints(const std::string &geometry_name, const GeoLib::GEOTYPE object_type, const std::string &geo_object_name, const std::string &new_name)
Adds a generic name to all points that are part of the specified geo-object.
void appendSurfaceVec(std::string const &name)
void renameGeometry(std::string const &old_name, std::string const &new_name)
void addSurfaceVec(std::string const &name)
void removePointVec(std::string const &name)
void addPointVec(std::string const &name)
StationTreeModel * _stationModel
Definition GEOModels.h:96
void stationVectorRemoved(StationTreeModel *model, std::string name)
void geoDataAdded(GeoTreeModel *, std::string, GeoLib::GEOTYPE)
void removeStationVec(std::string const &name)
void geoDataRemoved(GeoTreeModel *, std::string, GeoLib::GEOTYPE)
const std::vector< GeoLib::Point * > * getPointVec(const std::string &name) const
Definition GEOModels.cpp:96
void updateGeometry(const std::string &geo_name)
Definition GEOModels.cpp:29
void addNameForElement(std::string const &geometry_name, GeoLib::GEOTYPE const object_type, std::size_t const id, std::string const &new_name)
Adds the name 'new_name' for the geo-object specified by the parameters.
~GEOModels() override
Definition GEOModels.cpp:23
void removePolylineVec(std::string const &name)
GEOModels(GeoLib::GEOObjects &geo_objects, QObject *parent=nullptr)
Definition GEOModels.cpp:14
void connectPolylineSegments(const std::string &geoName, std::vector< std::size_t > const &indexlist, double const proximity, std::string const &ply_name, bool const closePly, bool const triangulatePly)
void appendPolylineVec(std::string const &name)
void removeSurfaceVec(std::string const &name)
virtual void removeGeometry(std::string const &geo_name, GeoLib::GEOTYPE const type)
Definition GEOModels.cpp:75
void addPolylineVec(std::string const &name)
GeoTreeModel * _geoModel
Definition GEOModels.h:95
Container class for geometric objects.
Definition GEOObjects.h:46
This class manages pointers to Points in a std::vector along with a name. It also handles the deletio...
Definition PointVec.h:25
void setNameForElement(std::size_t id, std::string const &name) override
Sets the given name for the element of the given ID.
Definition PointVec.cpp:244
Class Polyline consists mainly of a reference to a point vector and a vector that stores the indices ...
Definition Polyline.h:29
static Polyline * constructPolylineFromSegments(const std::vector< Polyline * > &ply_vec, double prox=0.0)
Definition Polyline.cpp:179
std::size_t getNumberOfPoints() const
Definition Polyline.cpp:98
A Surface is represented by Triangles. It consists of a reference to a vector of (pointers to) points...
std::size_t getNumberOfTriangles() const
Definition Surface.cpp:76
void setNameOfElementByID(std::size_t id, std::string const &element_name)
Return the name of an element based on its ID.
std::vector< T * > const & getVector() const
Definition TemplateVec.h:94
Class Triangle consists of a reference to a point vector and a vector that stores the indices in the ...
Definition Triangle.h:21
A model for the GeoTreeView implementing a tree as a double-linked list.
static void box(const QString &e)
Definition OGSError.cpp:13
A model for the StationTreeView implementing a tree as a double-linked list.
std::unique_ptr< GeoLib::Surface > createSurfaceWithEarClipping(GeoLib::Polyline const &line)
std::string convertGeoTypeToString(GEOTYPE geo_type)
Definition GeoType.cpp:12
GEOTYPE
Definition GeoType.h:12
TemplateVec< GeoLib::Polyline > PolylineVec
class PolylineVec encapsulate a std::vector of Polylines additional one can give the vector of polyli...
Definition PolylineVec.h:16