diff --git a/FileIO/GMSHInterface.cpp b/FileIO/GMSHInterface.cpp index d09e260ee2f93b30a8e790d2550d6f6a6339834f..e55ca93f30666aab164f57720dcbe9a2b2bb17ca 100644 --- a/FileIO/GMSHInterface.cpp +++ b/FileIO/GMSHInterface.cpp @@ -68,6 +68,15 @@ GMSHInterface::GMSHInterface(GeoLib::GEOObjects & geo_objs, } } +GMSHInterface::~GMSHInterface() +{ + for (auto * gmsh_pnt : _gmsh_pnts) + delete gmsh_pnt; + delete _mesh_density_strategy; + for (auto * polygon_tree : _polygon_tree_list) + delete polygon_tree; +} + int GMSHInterface::writeGeoFile(GeoLib::GEOObjects &geo_objects, std::string const& file_name) { std::vector<std::string> names; diff --git a/FileIO/GMSHInterface.h b/FileIO/GMSHInterface.h index ddd77148dfc4117b13d86020af362b4d350e83a5..64450e738c184b63da93f441e6f85f1c308ac5c5 100644 --- a/FileIO/GMSHInterface.h +++ b/FileIO/GMSHInterface.h @@ -78,6 +78,8 @@ public: double param1, double param2, std::size_t param3, std::vector<std::string> & selected_geometries); + ~GMSHInterface(); + /** * checks if there is a GMSH mesh file header * @param fname the file name of the mesh (including the path) diff --git a/FileIO/GmshIO/GMSHPolygonTree.cpp b/FileIO/GmshIO/GMSHPolygonTree.cpp index 132916260b608a8e3bf90c8b451315fc26f6c56f..e8899e9a46ece13c5beec2c7bcd635bb881231fe 100644 --- a/FileIO/GmshIO/GMSHPolygonTree.cpp +++ b/FileIO/GmshIO/GMSHPolygonTree.cpp @@ -35,18 +35,27 @@ GMSHPolygonTree::GMSHPolygonTree(GeoLib::Polygon* polygon, GMSHPolygonTree* pare {} GMSHPolygonTree::~GMSHPolygonTree() -{} +{ + // the polylines are processed also by the children, but the root is + // responsible to cleanup up + if (_parent == nullptr) { // root + for (auto * polyline : _plys) + delete polyline; + } + // member of GeoLib::SimplePolygonTree, but the ownership is not transmitted + delete _node_polygon; +} bool GMSHPolygonTree::insertStation(GeoLib::Point const* station) { if (_node_polygon->isPntInPolygon(*station)) { // try to insert station into the child nodes - for (std::list<SimplePolygonTree*>::const_iterator it (_childs.begin()); - it != _childs.end(); ++it) { + for (std::list<SimplePolygonTree*>::const_iterator it (_children.begin()); + it != _children.end(); ++it) { if (((*it)->getPolygon())->isPntInPolygon (*station)) { bool rval(dynamic_cast<GMSHPolygonTree*>((*it))->insertStation (station)); // stop recursion if sub SimplePolygonTree is a leaf - if (rval && (*it)->getNChilds() == 0) + if (rval && (*it)->getNChildren() == 0) _stations.push_back (station); return rval; } @@ -62,9 +71,9 @@ bool GMSHPolygonTree::insertStation(GeoLib::Point const* station) void GMSHPolygonTree::insertPolyline (GeoLib::PolylineWithSegmentMarker * ply) { if (_node_polygon->isPartOfPolylineInPolygon(*ply)) { - // check childs - for (std::list<SimplePolygonTree*>::const_iterator it (_childs.begin()); - it != _childs.end(); ++it) { + // check children + for (std::list<SimplePolygonTree*>::const_iterator it (_children.begin()); + it != _children.end(); ++it) { dynamic_cast<GMSHPolygonTree*>((*it))->insertPolyline (ply); } _plys.push_back(ply); @@ -181,8 +190,8 @@ void GMSHPolygonTree::createGMSHPoints(std::vector<FileIO::GMSH::GMSHPoint*> & g } } - // walk through childs - for (std::list<SimplePolygonTree*>::const_iterator it (_childs.begin()); it != _childs.end(); ++it) { + // walk through children + for (std::list<SimplePolygonTree*>::const_iterator it (_children.begin()); it != _children.end(); ++it) { dynamic_cast<GMSHPolygonTree*>((*it))->createGMSHPoints(gmsh_pnts); } } @@ -226,7 +235,7 @@ void GMSHPolygonTree::writeLineConstraints(std::size_t &line_offset, std::size_t void GMSHPolygonTree::writeSubPolygonsAsLineConstraints(std::size_t &line_offset, std::size_t sfc_number, std::ostream& out) const { - for (std::list<SimplePolygonTree*>::const_iterator it (_childs.begin()); it != _childs.end(); ++it) { + for (std::list<SimplePolygonTree*>::const_iterator it (_children.begin()); it != _children.end(); ++it) { dynamic_cast<GMSHPolygonTree*>((*it))->writeSubPolygonsAsLineConstraints(line_offset, sfc_number, out); } @@ -300,7 +309,7 @@ void GMSHPolygonTree::getPointsFromSubPolygons(std::vector<GeoLib::Point const*> pnts.push_back(_node_polygon->getPoint(k)); } - for (std::list<SimplePolygonTree*>::const_iterator it (_childs.begin()); it != _childs.end(); ++it) { + for (std::list<SimplePolygonTree*>::const_iterator it (_children.begin()); it != _children.end(); ++it) { dynamic_cast<GMSHPolygonTree*>((*it))->getPointsFromSubPolygons(pnts); } } @@ -312,7 +321,7 @@ void GMSHPolygonTree::getStationsInsideSubPolygons(std::vector<GeoLib::Point con stations.push_back(_stations[k]); } - for (std::list<SimplePolygonTree*>::const_iterator it (_childs.begin()); it != _childs.end(); ++it) { + for (std::list<SimplePolygonTree*>::const_iterator it (_children.begin()); it != _children.end(); ++it) { dynamic_cast<GMSHPolygonTree*>((*it))->getStationsInsideSubPolygons(stations); } } diff --git a/FileIO/GmshIO/GMSHPolygonTree.h b/FileIO/GmshIO/GMSHPolygonTree.h index f1e30d509036142dc6d17c6fe54d82d7cffcecaa..bc33d49bafe260d37daa0258cef78d594d9f08c3 100644 --- a/FileIO/GmshIO/GMSHPolygonTree.h +++ b/FileIO/GmshIO/GMSHPolygonTree.h @@ -58,7 +58,7 @@ public: * polyline, i.e. the two intersecting line segments are splitt into four line segment. * * Line segments of the polyline that are completely within the polygon are inserted into - * the internal vector _gmsh_lines_for_constraints. The childs of this GMSHPolygonTree node + * the internal vector _gmsh_lines_for_constraints. The children of this GMSHPolygonTree node * are checked recursively. * @param ply the polyline that should be inserted */ @@ -86,8 +86,6 @@ public: private: void getPointsFromSubPolygons(std::vector<GeoLib::Point const*>& pnts); void getStationsInsideSubPolygons(std::vector<GeoLib::Point const*>& stations); - const std::list<SimplePolygonTree*>& getChilds() const; - const std::list<GeoLib::GEOObjects*>& getGeoObjects () const; GeoLib::GEOObjects & _geo_objs; std::string const& _geo_name; diff --git a/GeoLib/QuadTree.h b/GeoLib/QuadTree.h index 61cecd862139b18564ad585755947e3bbbf34606..7e8f16403797a31c7e2493929c1b01a48811ad22 100644 --- a/GeoLib/QuadTree.h +++ b/GeoLib/QuadTree.h @@ -54,9 +54,9 @@ public: { assert (_max_points_per_node > 0); - // init childs + // init children for (std::size_t k(0); k < 4; k++) - _childs[k] = nullptr; + _children[k] = nullptr; if ((_ur[0] - _ll[0]) > (_ur[1] - _ll[1])) _ur[1] = _ll[1] + _ur[0] - _ll[0]; @@ -71,9 +71,9 @@ public: */ ~QuadTree() { - if (_is_leaf) - for (std::size_t k(0); k < 4; k++) - delete _childs[k]; + for (std::size_t k(0); k < 4; k++) { + delete _children[k]; + } } /** @@ -85,14 +85,17 @@ public: bool addPoint (POINT const* pnt) { if ((*pnt)[0] < _ll[0]) return false; - if ((*pnt)[0] > _ur[0]) return false; + if ((*pnt)[0] >= _ur[0]) return false; if ((*pnt)[1] < _ll[1]) return false; - if ((*pnt)[1] > _ur[1]) return false; + if ((*pnt)[1] >= _ur[1]) return false; - if (!_is_leaf) - for (std::size_t k(0); k < 4; k++) - if (_childs[k]->addPoint (pnt)) + if (!_is_leaf) { + for (std::size_t k(0); k < 4; k++) { + if (_children[k]->addPoint (pnt)) return true; + } + return false; + } // check if point is already in quadtree bool pnt_in_quadtree (false); @@ -181,7 +184,7 @@ public: leaf_list.push_back (this); else for (std::size_t k(0); k < 4; k++) - _childs[k]->getLeafs (leaf_list); + _children[k]->getLeafs (leaf_list); } @@ -205,16 +208,16 @@ public: if (pnt[0] <= 0.5 * (_ur[0] + _ll[0])) // WEST { if (pnt[1] <= 0.5 * (_ur[1] + _ll[1])) // SOUTH - _childs[static_cast<int>(Quadrant::SW)]->getLeaf (pnt, ll, ur); + _children[static_cast<int>(Quadrant::SW)]->getLeaf (pnt, ll, ur); else // NORTH - _childs[static_cast<int>(Quadrant::NW)]->getLeaf (pnt, ll, ur); + _children[static_cast<int>(Quadrant::NW)]->getLeaf (pnt, ll, ur); } else // EAST { if (pnt[1] <= 0.5 * (_ur[1] + _ll[1])) // SOUTH - _childs[static_cast<int>(Quadrant::SE)]->getLeaf (pnt, ll, ur); + _children[static_cast<int>(Quadrant::SE)]->getLeaf (pnt, ll, ur); else // NORTH - _childs[static_cast<int>(Quadrant::NE)]->getLeaf (pnt, ll, ur); + _children[static_cast<int>(Quadrant::NE)]->getLeaf (pnt, ll, ur); } } } @@ -226,7 +229,7 @@ public: QuadTree<POINT> const* getChild (Quadrant quadrant) const { - return _childs[quadrant]; + return _children[quadrant]; } /** @@ -240,8 +243,8 @@ public: max_depth = _depth; for (std::size_t k(0); k<4; k++) { - if (_childs[k]) { - _childs[k]->getMaxDepth(max_depth); + if (_children[k]) { + _children[k]->getMaxDepth(max_depth); } } } @@ -255,14 +258,14 @@ public: private: QuadTree<POINT>* getChild (Quadrant quadrant) { - return _childs[static_cast<int>(quadrant)]; + return _children[static_cast<int>(quadrant)]; } bool isLeaf () const { return _is_leaf; } bool isChild (QuadTree<POINT> const* const tree, Quadrant quadrant) const { - if (_childs[static_cast<int>(quadrant)] == tree) return true; + if (_children[static_cast<int>(quadrant)] == tree) return true; return false; } @@ -370,34 +373,38 @@ private: _father (father), _ll (ll), _ur (ur), _depth (depth), _is_leaf (true), _max_points_per_node (max_points_per_node) { - // init childs + // init children for (std::size_t k(0); k < 4; k++) - _childs[k] = nullptr; + _children[k] = nullptr; } void splitNode () { - // create childs + // create children POINT mid_point(_ll); mid_point[0] += (_ur[0] - _ll[0]) / 2.0; mid_point[1] += (_ur[1] - _ll[1]) / 2.0; - _childs[0] = new QuadTree<POINT> (mid_point, _ur, this, _depth + 1, _max_points_per_node); // north east + assert(_children[0] == nullptr); + _children[0] = new QuadTree<POINT> (mid_point, _ur, this, _depth + 1, _max_points_per_node); // north east POINT h_ll(mid_point), h_ur(mid_point); h_ll[0] = _ll[0]; h_ur[1] = _ur[1]; - _childs[1] = new QuadTree<POINT> (h_ll, h_ur, this, _depth + 1, _max_points_per_node); // north west - _childs[2] = new QuadTree<POINT> (_ll, mid_point, this, _depth + 1, _max_points_per_node); // south west + assert(_children[1] == nullptr); + _children[1] = new QuadTree<POINT> (h_ll, h_ur, this, _depth + 1, _max_points_per_node); // north west + assert(_children[2] == nullptr); + _children[2] = new QuadTree<POINT> (_ll, mid_point, this, _depth + 1, _max_points_per_node); // south west h_ll = _ll; h_ll[0] = mid_point[0]; h_ur = _ur; h_ur[1] = mid_point[1]; - _childs[3] = new QuadTree<POINT> (h_ll, h_ur, this, _depth + 1, _max_points_per_node); // south east + assert(_children[3] == nullptr); + _children[3] = new QuadTree<POINT> (h_ll, h_ur, this, _depth + 1, _max_points_per_node); // south east // distribute points to sub quadtrees for (std::size_t j(0); j < _pnts.size(); j++) { bool nfound(true); for (std::size_t k(0); k < 4 && nfound; k++) - if (_childs[k]->addPoint(_pnts[j])) nfound = false; + if (_children[k]->addPoint(_pnts[j])) nfound = false; } _pnts.clear(); @@ -462,13 +469,13 @@ private: QuadTree<POINT>* _father; /** - * childs are sorted: - * _childs[0] is north east child - * _childs[1] is north west child - * _childs[2] is south west child - * _childs[3] is south east child + * children are sorted: + * _children[0] is north east child + * _children[1] is north west child + * _children[2] is south west child + * _children[3] is south east child */ - QuadTree<POINT>* _childs[4]; + QuadTree<POINT>* _children[4]; /** * lower left point of the square */ diff --git a/GeoLib/SimplePolygonTree.cpp b/GeoLib/SimplePolygonTree.cpp index 139ff390c3cfda117300342db0649a12b40a7d0a..e288664a2a824275518438c1c6a7cccce5b74ad9 100644 --- a/GeoLib/SimplePolygonTree.cpp +++ b/GeoLib/SimplePolygonTree.cpp @@ -22,9 +22,8 @@ SimplePolygonTree::SimplePolygonTree(Polygon * polygon, SimplePolygonTree * pare SimplePolygonTree::~SimplePolygonTree() { - for (std::list<SimplePolygonTree*>::const_iterator it (_childs.begin()); - it != _childs.end(); ++it) { - delete *it; + for (auto * child : _children) { + delete child; } } @@ -37,15 +36,15 @@ void SimplePolygonTree::insertSimplePolygonTree (SimplePolygonTree* polygon_hier { const Polygon* polygon (polygon_hierarchy->getPolygon()); bool nfound (true); - for (std::list<SimplePolygonTree*>::const_iterator it (_childs.begin()); - it != _childs.end() && nfound; ++it) { + for (std::list<SimplePolygonTree*>::const_iterator it (_children.begin()); + it != _children.end() && nfound; ++it) { if (((*it)->getPolygon())->isPolylineInPolygon (*(polygon))) { (*it)->insertSimplePolygonTree (polygon_hierarchy); nfound = false; } } if (nfound) { - _childs.push_back (polygon_hierarchy); + _children.push_back (polygon_hierarchy); polygon_hierarchy->setParent(this); } } diff --git a/GeoLib/SimplePolygonTree.h b/GeoLib/SimplePolygonTree.h index 4228077da62c88f51f991137203d45979fc85759..d5b547efe37542d7c5fe4fda06ad6f1824671e8a 100644 --- a/GeoLib/SimplePolygonTree.h +++ b/GeoLib/SimplePolygonTree.h @@ -44,7 +44,7 @@ public: */ bool isPolygonInside (const SimplePolygonTree* polygon_tree) const; /** Either insert the given SimplePolygonTree in one of the existing - * childs or as a new child. + * children or as a new child. */ void insertSimplePolygonTree (SimplePolygonTree* polygon_tree); @@ -54,8 +54,8 @@ public: */ const Polygon* getPolygon () const; - /** returns the number of childs */ - std::size_t getNChilds() const { return _childs.size(); } + /** returns the number of children */ + std::size_t getNChildren() const { return _children.size(); } protected: /** @@ -71,7 +71,7 @@ protected: * list of polygons (represented by SimplePolygonTree nodes) contained * in the _node_polygon */ - std::list<SimplePolygonTree*> _childs; + std::list<SimplePolygonTree*> _children; private: void setParent(SimplePolygonTree* parent) diff --git a/Tests/GeoLib/TestSimplePolygonTree.cpp b/Tests/GeoLib/TestSimplePolygonTree.cpp index 3b05ad8dd36fabbd8b6d00ca46ba4f795c8e0bff..e05fb9a43a513e81b4c113ea1257b070cc425c72 100644 --- a/Tests/GeoLib/TestSimplePolygonTree.cpp +++ b/Tests/GeoLib/TestSimplePolygonTree.cpp @@ -140,7 +140,7 @@ TEST_F(CreatePolygonTreesTest, P0AndP1AndP2) createPolygonTrees(pt_list); ASSERT_EQ(1u, pt_list.size()); - ASSERT_EQ(2u, (*(pt_list.begin()))->getNChilds()); + ASSERT_EQ(2u, (*(pt_list.begin()))->getNChildren()); std::for_each(pt_list.begin(), pt_list.end(), std::default_delete<GeoLib::SimplePolygonTree>()); } @@ -169,7 +169,7 @@ TEST_F(CreatePolygonTreesTest, P0AndP1AndP2AndP3) createPolygonTrees(pt_list); ASSERT_EQ(1u, pt_list.size()); - ASSERT_EQ(2u, (*(pt_list.begin()))->getNChilds()); + ASSERT_EQ(2u, (*(pt_list.begin()))->getNChildren()); std::for_each(pt_list.begin(), pt_list.end(), std::default_delete<GeoLib::SimplePolygonTree>()); }