From d3ea7e4a5ca20c2fa4fa71bd3a19cc4b32569b72 Mon Sep 17 00:00:00 2001 From: Thomas Fischer <thomas.fischer@ufz.de> Date: Mon, 3 Jan 2022 15:36:40 +0100 Subject: [PATCH] Use new GEOObjects::addSurfaceVec() without unique_ptr args. --- Applications/FileIO/Legacy/OGSIOVer4.cpp | 15 +++--- Applications/FileIO/TetGenInterface.cpp | 13 ++--- GeoLib/DuplicateGeometry.cpp | 20 ++++---- GeoLib/DuplicateGeometry.h | 4 +- GeoLib/GEOObjects.cpp | 18 +++---- .../IO/XmlIO/Boost/BoostXmlGmlInterface.cpp | 15 +++--- GeoLib/IO/XmlIO/Qt/XmlGmlInterface.cpp | 49 ++++++++----------- GeoLib/IO/XmlIO/Qt/XmlGmlInterface.h | 4 +- MeshLib/convertMeshToGeo.cpp | 17 ++++--- Tests/FileIO/TestGmlInterface.h | 25 +++++----- Tests/GeoLib/TestDuplicateGeometry.cpp | 7 +-- 11 files changed, 86 insertions(+), 101 deletions(-) diff --git a/Applications/FileIO/Legacy/OGSIOVer4.cpp b/Applications/FileIO/Legacy/OGSIOVer4.cpp index cc52ef5c79b..ef7eab53a60 100644 --- a/Applications/FileIO/Legacy/OGSIOVer4.cpp +++ b/Applications/FileIO/Legacy/OGSIOVer4.cpp @@ -581,17 +581,17 @@ bool readGLIFileV4(const std::string& fname, geo.getPolylineVecObj(unique_name)->getNameIDMapEnd()}; } - auto sfc_vec = std::make_unique<std::vector<GeoLib::Surface*>>(); - auto sfc_names = std::make_unique<std::map<std::string, std::size_t>>(); + std::vector<GeoLib::Surface*> sfc_vec{}; + GeoLib::SurfaceVec::NameIdMap sfc_names{}; if (tag.find("#SURFACE") != std::string::npos && in) { INFO("GeoLib::readGLIFile(): read surfaces from stream."); - readSurfaces(in, *sfc_vec, *sfc_names, *geo.getPolylineVec(unique_name), + readSurfaces(in, sfc_vec, sfc_names, *geo.getPolylineVec(unique_name), ply_names_copy, point_vec, path, errors, geo, unique_name, gmsh_path); INFO("GeoLib::readGLIFile(): \tok, {:d} surfaces read.", - sfc_vec->size()); + sfc_vec.size()); } else { @@ -599,11 +599,10 @@ bool readGLIFileV4(const std::string& fname, } in.close(); - if (!sfc_vec->empty()) + if (!sfc_vec.empty()) { - geo.addSurfaceVec( - std::move(sfc_vec), unique_name, - std::move(sfc_names)); // KR: insert into GEOObjects if not empty + geo.addSurfaceVec(std::move(sfc_vec), unique_name, + std::move(sfc_names)); } return errors.empty(); diff --git a/Applications/FileIO/TetGenInterface.cpp b/Applications/FileIO/TetGenInterface.cpp index 9b63b01c9df..4949088a308 100644 --- a/Applications/FileIO/TetGenInterface.cpp +++ b/Applications/FileIO/TetGenInterface.cpp @@ -75,17 +75,18 @@ bool TetGenInterface::readTetGenGeometry(std::string const& geo_fname, const std::vector<std::size_t>& id_map( geo_objects.getPointVecObj(geo_name)->getIDMap()); - auto surfaces = std::make_unique<std::vector<GeoLib::Surface*>>(); - if (!parseSmeshFacets( - poly_stream, *surfaces, *geo_objects.getPointVec(geo_name), id_map)) + std::vector<GeoLib::Surface*> surfaces{}; + if (!parseSmeshFacets(poly_stream, surfaces, + *geo_objects.getPointVec(geo_name), id_map)) { // remove surfaces read until now but keep the points - for (std::size_t k = 0; k < surfaces->size(); k++) + for (std::size_t k = 0; k < surfaces.size(); k++) { - delete (*surfaces)[k]; + delete surfaces[k]; } } - geo_objects.addSurfaceVec(std::move(surfaces), geo_name); + geo_objects.addSurfaceVec(std::move(surfaces), geo_name, + GeoLib::SurfaceVec::NameIdMap{}); return true; } diff --git a/GeoLib/DuplicateGeometry.cpp b/GeoLib/DuplicateGeometry.cpp index 71a7803a0be..0f0ec103d8b 100644 --- a/GeoLib/DuplicateGeometry.cpp +++ b/GeoLib/DuplicateGeometry.cpp @@ -68,10 +68,9 @@ void DuplicateGeometry::duplicate(std::string const& input_name) if (sfcs) { auto new_sfcs = copySurfacesVector(*sfcs); - auto sfc_name_id_map = - std::make_unique<std::map<std::string, std::size_t>>( - _geo_objects.getSurfaceVecObj(input_name)->getNameIDMapBegin(), - _geo_objects.getSurfaceVecObj(input_name)->getNameIDMapEnd()); + GeoLib::SurfaceVec::NameIdMap sfc_name_id_map{ + _geo_objects.getSurfaceVecObj(input_name)->getNameIDMapBegin(), + _geo_objects.getSurfaceVecObj(input_name)->getNameIDMapEnd()}; _geo_objects.addSurfaceVec(std::move(new_sfcs), _output_name, std::move(sfc_name_id_map)); } @@ -100,12 +99,11 @@ std::vector<GeoLib::Polyline*> DuplicateGeometry::copyPolylinesVector( return new_lines; } -std::unique_ptr<std::vector<Surface*>> DuplicateGeometry::copySurfacesVector( +std::vector<Surface*> DuplicateGeometry::copySurfacesVector( std::vector<Surface*> const& surfaces) const { std::size_t const n_sfc = surfaces.size(); - auto new_surfaces = - std::make_unique<std::vector<GeoLib::Surface*>>(n_sfc, nullptr); + std::vector<GeoLib::Surface*> new_surfaces{n_sfc, nullptr}; for (std::size_t i = 0; i < n_sfc; ++i) { @@ -113,16 +111,16 @@ std::unique_ptr<std::vector<Surface*>> DuplicateGeometry::copySurfacesVector( { continue; } - (*new_surfaces)[i] = + new_surfaces[i] = new GeoLib::Surface(*_geo_objects.getPointVec(_output_name)); std::size_t const n_tris(surfaces[i]->getNumberOfTriangles()); for (std::size_t j = 0; j < n_tris; ++j) { GeoLib::Triangle const* t = (*surfaces[i])[j]; - (*new_surfaces)[i]->addTriangle(t->getPoint(0)->getID(), - t->getPoint(1)->getID(), - t->getPoint(2)->getID()); + new_surfaces[i]->addTriangle(t->getPoint(0)->getID(), + t->getPoint(1)->getID(), + t->getPoint(2)->getID()); } } return new_surfaces; diff --git a/GeoLib/DuplicateGeometry.h b/GeoLib/DuplicateGeometry.h index 701da729073..5c55db13c68 100644 --- a/GeoLib/DuplicateGeometry.h +++ b/GeoLib/DuplicateGeometry.h @@ -56,8 +56,8 @@ private: std::vector<GeoLib::Polyline*> const& polylines) const; // creates a deep copy of the surface vector - std::unique_ptr<std::vector<GeoLib::Surface*>> copySurfacesVector( - std::vector<GeoLib::Surface*> const& surfaces) const; + std::vector<Surface*> copySurfacesVector( + std::vector<Surface*> const& surfaces) const; std::string _output_name; GeoLib::GEOObjects& _geo_objects; diff --git a/GeoLib/GEOObjects.cpp b/GeoLib/GEOObjects.cpp index fc3852e9d58..46d1ed96954 100644 --- a/GeoLib/GEOObjects.cpp +++ b/GeoLib/GEOObjects.cpp @@ -285,9 +285,8 @@ bool GEOObjects::appendSurfaceVec(const std::vector<Surface*>& surfaces, // the copy is needed because addSurfaceVec is passing it to SurfaceVec // ctor, which needs write access to the surface vector. - auto sfc = std::make_unique<std::vector<GeoLib::Surface*>>(begin(surfaces), - end(surfaces)); - addSurfaceVec(std::move(sfc), name); + std::vector<GeoLib::Surface*> sfc{begin(surfaces), end(surfaces)}; + addSurfaceVec(std::move(sfc), name, SurfaceVec::NameIdMap{}); return false; } @@ -562,9 +561,8 @@ void GEOObjects::mergeSurfaces(std::vector<std::string> const& geo_names, const std::size_t n_geo_names(geo_names.size()); std::vector<std::size_t> sfc_offsets(n_geo_names, 0); - auto merged_sfcs = std::make_unique<std::vector<GeoLib::Surface*>>(); - auto merged_sfc_names = - std::make_unique<std::map<std::string, std::size_t>>(); + std::vector<GeoLib::Surface*> merged_sfcs{}; + SurfaceVec::NameIdMap merged_sfc_names{}; for (std::size_t j(0); j < n_geo_names; j++) { const std::vector<GeoLib::Surface*>* sfcs( @@ -587,14 +585,12 @@ void GEOObjects::mergeSurfaces(std::vector<std::string> const& geo_names, const std::size_t id2(id_map[pnt_offsets[j] + (*tri)[2]]); kth_sfc_new->addTriangle(id0, id1, id2); } - merged_sfcs->push_back(kth_sfc_new); + merged_sfcs.push_back(kth_sfc_new); if (this->getSurfaceVecObj(geo_names[j]) ->getNameOfElementByID(k, tmp_name)) { - merged_sfc_names->insert( - std::pair<std::string, std::size_t>( - tmp_name, sfc_offsets[j] + k)); + merged_sfc_names.emplace(tmp_name, sfc_offsets[j] + k); } } if (n_geo_names - 1 > j) @@ -603,7 +599,7 @@ void GEOObjects::mergeSurfaces(std::vector<std::string> const& geo_names, } } } - if (!merged_sfcs->empty()) + if (!merged_sfcs.empty()) { this->addSurfaceVec(std::move(merged_sfcs), merged_geo_name, std::move(merged_sfc_names)); diff --git a/GeoLib/IO/XmlIO/Boost/BoostXmlGmlInterface.cpp b/GeoLib/IO/XmlIO/Boost/BoostXmlGmlInterface.cpp index 6a911ac07a3..c5dea4183e1 100644 --- a/GeoLib/IO/XmlIO/Boost/BoostXmlGmlInterface.cpp +++ b/GeoLib/IO/XmlIO/Boost/BoostXmlGmlInterface.cpp @@ -45,10 +45,6 @@ bool BoostXmlGmlInterface::readFile(const std::string& fname) doc->ignoreConfigAttribute("xsi:noNamespaceSchemaLocation"); doc->ignoreConfigAttribute("xmlns:ogs"); - auto surfaces = std::make_unique<std::vector<GeoLib::Surface*>>(); - using MapNameId = std::map<std::string, std::size_t>; - auto sfc_names = std::make_unique<MapNameId>(); - //! \ogs_file_param{gml__name} auto geo_name = doc->getConfigParameter<std::string>("name"); if (geo_name.empty()) @@ -78,14 +74,15 @@ bool BoostXmlGmlInterface::readFile(const std::string& fname) ply_names); } + std::vector<GeoLib::Surface*> surfaces{}; + SurfaceVec::NameIdMap sfc_names{}; + //! \ogs_file_param{gml__surfaces} for (auto st : doc->getConfigSubtreeList("surfaces")) { - readSurfaces(st, - *surfaces, - *_geo_objects.getPointVec(geo_name), + readSurfaces(st, surfaces, *_geo_objects.getPointVec(geo_name), _geo_objects.getPointVecObj(geo_name)->getIDMap(), - *sfc_names); + sfc_names); } if (!polylines.empty()) @@ -94,7 +91,7 @@ bool BoostXmlGmlInterface::readFile(const std::string& fname) std::move(ply_names)); } - if (!surfaces->empty()) + if (!surfaces.empty()) { _geo_objects.addSurfaceVec(std::move(surfaces), geo_name, std::move(sfc_names)); diff --git a/GeoLib/IO/XmlIO/Qt/XmlGmlInterface.cpp b/GeoLib/IO/XmlIO/Qt/XmlGmlInterface.cpp index b4b8c454619..4e0722233b0 100644 --- a/GeoLib/IO/XmlIO/Qt/XmlGmlInterface.cpp +++ b/GeoLib/IO/XmlIO/Qt/XmlGmlInterface.cpp @@ -31,24 +31,23 @@ void deletePolylines(std::vector<GeoLib::Polyline*>& polylines) } } -void deleteSurfaces(std::unique_ptr<std::vector<GeoLib::Surface*>> surfaces) +void deleteSurfaces(std::vector<GeoLib::Surface*>& surfaces) { - for (GeoLib::Surface* surface : *surfaces) + for (GeoLib::Surface* surface : surfaces) { delete surface; } } void deleteGeometry(std::vector<GeoLib::Point*>& points, std::vector<GeoLib::Polyline*>& polylines, - std::unique_ptr<std::vector<GeoLib::Surface*>> - surfaces) + std::vector<GeoLib::Surface*>& surfaces) { for (GeoLib::Point* point : points) { delete point; } deletePolylines(polylines); - deleteSurfaces(std::move(surfaces)); + deleteSurfaces(surfaces); } } // namespace @@ -81,12 +80,12 @@ int XmlGmlInterface::readFile(const QString& fileName) std::vector<GeoLib::Point*> points{}; std::vector<GeoLib::Polyline*> polylines{}; - auto surfaces = std::make_unique<std::vector<GeoLib::Surface*>>(); + std::vector<GeoLib::Surface*> surfaces{}; using MapNameId = std::map<std::string, std::size_t>; MapNameId pnt_names; MapNameId ply_names{}; - auto sfc_names = std::make_unique<MapNameId>(); + GeoLib::SurfaceVec::NameIdMap sfc_names{}; QDomNodeList geoTypes = docElement.childNodes(); for (int i = 0; i < geoTypes.count(); i++) @@ -98,7 +97,7 @@ int XmlGmlInterface::readFile(const QString& fileName) if (type_node.toElement().text().isEmpty()) { ERR("XmlGmlInterface::readFile(): <name>-tag is empty."); - deleteGeometry(points, polylines, std::move(surfaces)); + deleteGeometry(points, polylines, surfaces); return 0; } @@ -129,10 +128,9 @@ int XmlGmlInterface::readFile(const QString& fileName) { try { - readSurfaces(type_node, surfaces.get(), - *_geo_objs.getPointVec(gliName), - _geo_objs.getPointVecObj(gliName)->getIDMap(), - sfc_names.get()); + readSurfaces( + type_node, surfaces, *_geo_objs.getPointVec(gliName), + _geo_objs.getPointVecObj(gliName)->getIDMap(), sfc_names); } catch (std::runtime_error const&) { @@ -141,12 +139,6 @@ int XmlGmlInterface::readFile(const QString& fileName) _geo_objs.removePolylineVec(gliName); throw; } - - // if names-map is empty, set it to nullptr because it is not needed - if (sfc_names->empty()) - { - sfc_names.reset(nullptr); - } } } @@ -156,7 +148,7 @@ int XmlGmlInterface::readFile(const QString& fileName) std::move(ply_names)); } - if (!surfaces->empty()) + if (!surfaces.empty()) { _geo_objs.addSurfaceVec(std::move(surfaces), gliName, std::move(sfc_names)); @@ -252,22 +244,21 @@ void XmlGmlInterface::readPolylines(const QDomNode& polylinesRoot, } } -void XmlGmlInterface::readSurfaces( - const QDomNode& surfacesRoot, - std::vector<GeoLib::Surface*>* surfaces, - std::vector<GeoLib::Point*> const& points, - const std::vector<std::size_t>& pnt_id_map, - std::map<std::string, std::size_t>* sfc_names) +void XmlGmlInterface::readSurfaces(const QDomNode& surfacesRoot, + std::vector<GeoLib::Surface*>& surfaces, + std::vector<GeoLib::Point*> const& points, + const std::vector<std::size_t>& pnt_id_map, + GeoLib::SurfaceVec::NameIdMap& sfc_names) { QDomElement surface = surfacesRoot.firstChildElement(); while (!surface.isNull()) { - surfaces->push_back(new GeoLib::Surface(points)); + surfaces.push_back(new GeoLib::Surface(points)); if (surface.hasAttribute("name")) { - sfc_names->insert(std::pair<std::string, std::size_t>( - surface.attribute("name").toStdString(), surfaces->size() - 1)); + sfc_names.insert(std::pair<std::string, std::size_t>( + surface.attribute("name").toStdString(), surfaces.size() - 1)); } auto accessOrError = [this, &surface](auto pt_idx) @@ -297,7 +288,7 @@ void XmlGmlInterface::readSurfaces( pnt_id_map[accessOrError(element.attribute("p2").toInt())]; std::size_t p3 = pnt_id_map[accessOrError(element.attribute("p3").toInt())]; - surfaces->back()->addTriangle(p1, p2, p3); + surfaces.back()->addTriangle(p1, p2, p3); element = element.nextSiblingElement(); } diff --git a/GeoLib/IO/XmlIO/Qt/XmlGmlInterface.h b/GeoLib/IO/XmlIO/Qt/XmlGmlInterface.h index fe5a77e8910..01288d8482d 100644 --- a/GeoLib/IO/XmlIO/Qt/XmlGmlInterface.h +++ b/GeoLib/IO/XmlIO/Qt/XmlGmlInterface.h @@ -61,10 +61,10 @@ private: /// Reads GeoLib::Surface-objects from an xml-file void readSurfaces(const QDomNode& surfacesRoot, - std::vector<GeoLib::Surface*>* surfaces, + std::vector<GeoLib::Surface*>& surfaces, std::vector<GeoLib::Point*> const& points, const std::vector<std::size_t>& pnt_id_map, - std::map<std::string, std::size_t>* sfc_names); + GeoLib::SurfaceVec::NameIdMap& sfc_names); GeoLib::GEOObjects& _geo_objs; std::map<std::size_t, std::size_t> _idx_map; diff --git a/MeshLib/convertMeshToGeo.cpp b/MeshLib/convertMeshToGeo.cpp index f904607be28..aaba27a07e1 100644 --- a/MeshLib/convertMeshToGeo.cpp +++ b/MeshLib/convertMeshToGeo.cpp @@ -109,14 +109,14 @@ bool convertMeshToGeo(const MeshLib::Mesh& mesh, auto const [materialIds, bounds] = get_material_ids_and_bounds(); // elements to surface triangles conversion const unsigned nMatGroups(bounds.second - bounds.first + 1); - auto sfcs = std::make_unique<std::vector<GeoLib::Surface*>>(); - sfcs->reserve(nMatGroups); + std::vector<GeoLib::Surface*> sfcs{}; + sfcs.reserve(nMatGroups); std::string const geoobject_name = convertMeshNodesToGeoPoints(mesh, eps, geo_objects); auto const& points = *geo_objects.getPointVec(geoobject_name); for (unsigned i = 0; i < nMatGroups; ++i) { - sfcs->push_back(new GeoLib::Surface(points)); + sfcs.push_back(new GeoLib::Surface(points)); } const std::vector<std::size_t>& id_map( @@ -127,10 +127,10 @@ bool convertMeshToGeo(const MeshLib::Mesh& mesh, for (unsigned i = 0; i < nElems; ++i) { auto surfaceId = !materialIds ? 0 : ((*materialIds)[i] - bounds.first); - addElementToSurface(*elements[i], id_map, *(*sfcs)[surfaceId]); + addElementToSurface(*elements[i], id_map, *sfcs[surfaceId]); } - std::for_each(sfcs->begin(), sfcs->end(), + std::for_each(sfcs.begin(), sfcs.end(), [](GeoLib::Surface*& sfc) { if (sfc->getNumberOfTriangles() == 0) @@ -139,10 +139,11 @@ bool convertMeshToGeo(const MeshLib::Mesh& mesh, sfc = nullptr; } }); - auto sfcs_end = std::remove(sfcs->begin(), sfcs->end(), nullptr); - sfcs->erase(sfcs_end, sfcs->end()); + auto sfcs_end = std::remove(sfcs.begin(), sfcs.end(), nullptr); + sfcs.erase(sfcs_end, sfcs.end()); - geo_objects.addSurfaceVec(std::move(sfcs), geoobject_name); + geo_objects.addSurfaceVec(std::move(sfcs), geoobject_name, + GeoLib::SurfaceVec::NameIdMap{}); return true; } diff --git a/Tests/FileIO/TestGmlInterface.h b/Tests/FileIO/TestGmlInterface.h index 43087282fb9..ac609945404 100644 --- a/Tests/FileIO/TestGmlInterface.h +++ b/Tests/FileIO/TestGmlInterface.h @@ -159,18 +159,19 @@ public: const std::vector<std::size_t> pnt_id_map( geo_objects.getPointVecObj(geo_name)->getIDMap()); - auto sfcs = std::make_unique<std::vector<GeoLib::Surface*>>(2); - auto sfc_names = std::make_unique<std::map<std::string, std::size_t>>(); - (*sfcs)[0] = new GeoLib::Surface(*points); - (*sfcs)[0]->addTriangle(pnt_id_map[0], pnt_id_map[3], pnt_id_map[1]); - (*sfcs)[0]->addTriangle(pnt_id_map[1], pnt_id_map[3], pnt_id_map[4]); - (*sfcs)[0]->addTriangle(pnt_id_map[1], pnt_id_map[4], pnt_id_map[2]); - (*sfcs)[0]->addTriangle(pnt_id_map[2], pnt_id_map[4], pnt_id_map[5]); - (*sfc_names)["FirstSurface"] = 0; - (*sfcs)[1] = new GeoLib::Surface(*points); - (*sfcs)[1]->addTriangle(pnt_id_map[3], pnt_id_map[6], pnt_id_map[8]); - (*sfcs)[1]->addTriangle(pnt_id_map[3], pnt_id_map[8], pnt_id_map[5]); - (*sfc_names)["SecondSurface"] = 1; + std::vector<GeoLib::Surface*> sfcs{2}; + GeoLib::SurfaceVec::NameIdMap sfc_names{}; + sfcs[0] = new GeoLib::Surface(*points); + sfcs[0]->addTriangle(pnt_id_map[0], pnt_id_map[3], pnt_id_map[1]); + sfcs[0]->addTriangle(pnt_id_map[1], pnt_id_map[3], pnt_id_map[4]); + sfcs[0]->addTriangle(pnt_id_map[1], pnt_id_map[4], pnt_id_map[2]); + sfcs[0]->addTriangle(pnt_id_map[2], pnt_id_map[4], pnt_id_map[5]); + sfc_names["FirstSurface"] = 0; + + sfcs[1] = new GeoLib::Surface(*points); + sfcs[1]->addTriangle(pnt_id_map[3], pnt_id_map[6], pnt_id_map[8]); + sfcs[1]->addTriangle(pnt_id_map[3], pnt_id_map[8], pnt_id_map[5]); + sfc_names["SecondSurface"] = 1; geo_objects.addSurfaceVec(std::move(sfcs), geo_name, std::move(sfc_names)); } diff --git a/Tests/GeoLib/TestDuplicateGeometry.cpp b/Tests/GeoLib/TestDuplicateGeometry.cpp index aa469352102..704cd189901 100644 --- a/Tests/GeoLib/TestDuplicateGeometry.cpp +++ b/Tests/GeoLib/TestDuplicateGeometry.cpp @@ -123,7 +123,7 @@ TEST(GeoLib, DuplicateGeometry) // add surfaces { int const n_sfcs = std::rand() % 10 + 1; - auto sfcs = std::make_unique<std::vector<GeoLib::Surface*>>(); + std::vector<GeoLib::Surface*> sfcs{}; for (int i = 0; i < n_sfcs; ++i) { std::size_t const n_tris = std::rand() % 10 + 1; @@ -135,9 +135,10 @@ TEST(GeoLib, DuplicateGeometry) std::rand() % n_pnts); } ASSERT_GT(sfc->getNumberOfTriangles(), 0); - sfcs->push_back(sfc); + sfcs.push_back(sfc); } - geo.addSurfaceVec(std::move(sfcs), input_name); + geo.addSurfaceVec(std::move(sfcs), input_name, + GeoLib::SurfaceVec::NameIdMap{}); } // duplicate surfaces -- GitLab