From a72234f0c7a2d45a0c7b038f155fc81c1b15f5fc Mon Sep 17 00:00:00 2001 From: abn Date: Thu, 12 Jun 2014 15:36:55 +0200 Subject: [PATCH] Intersect2DMeshWith1DLine: variable name formatting + introducing reverse iterator on ComposedEdge. --- .../InterpKernelGeo2DAbstractEdge.cxx | 38 +++++++++++ .../InterpKernelGeo2DAbstractEdge.hxx | 29 ++++++++ .../InterpKernelGeo2DComposedEdge.hxx | 1 + .../InterpKernelGeo2DQuadraticPolygon.cxx | 68 +++++++++---------- .../InterpKernelGeo2DQuadraticPolygon.hxx | 6 +- src/MEDCoupling/MEDCouplingUMesh.cxx | 6 +- src/MEDCoupling_Swig/MEDCouplingBasicsTest.py | 4 +- 7 files changed, 110 insertions(+), 42 deletions(-) diff --git a/src/INTERP_KERNEL/Geometric2D/InterpKernelGeo2DAbstractEdge.cxx b/src/INTERP_KERNEL/Geometric2D/InterpKernelGeo2DAbstractEdge.cxx index c68875298..8059388e5 100644 --- a/src/INTERP_KERNEL/Geometric2D/InterpKernelGeo2DAbstractEdge.cxx +++ b/src/INTERP_KERNEL/Geometric2D/InterpKernelGeo2DAbstractEdge.cxx @@ -116,3 +116,41 @@ void IteratorOnComposedEdge::insertElemEdges(ComposedEdge *elems, bool changeMyS } } + +ReverseIteratorOnComposedEdge::ReverseIteratorOnComposedEdge():_list_handle(0) +{ +} + +ReverseIteratorOnComposedEdge::ReverseIteratorOnComposedEdge(ComposedEdge *compEdges):_list_handle(compEdges->getListBehind()) +{ + first(); +} + +void ReverseIteratorOnComposedEdge::operator=(const ReverseIteratorOnComposedEdge& other) +{ + _deep_it = other._deep_it; + _list_handle = other._list_handle; +} + +void ReverseIteratorOnComposedEdge::last() +{ + _deep_it = _list_handle->rend(); + _deep_it--; +} + +void ReverseIteratorOnComposedEdge::nextLoop() +{ + _deep_it++; + if(_deep_it == _list_handle->rend()) + first(); +} + +void ReverseIteratorOnComposedEdge::previousLoop() +{ + if(_deep_it != _list_handle->rbegin()) + _deep_it--; + else + last(); +} + + diff --git a/src/INTERP_KERNEL/Geometric2D/InterpKernelGeo2DAbstractEdge.hxx b/src/INTERP_KERNEL/Geometric2D/InterpKernelGeo2DAbstractEdge.hxx index 952262ef3..c99ea5ede 100644 --- a/src/INTERP_KERNEL/Geometric2D/InterpKernelGeo2DAbstractEdge.hxx +++ b/src/INTERP_KERNEL/Geometric2D/InterpKernelGeo2DAbstractEdge.hxx @@ -63,6 +63,35 @@ namespace INTERP_KERNEL std::list::iterator _deep_it; std::list* _list_handle; }; + + /** + * Reverse iterator on a composed edge. The looping goes from first to last. + * nextLoop() moves in the direction first->last, and prevLoop iterates in the direction + * last->first. + */ + class ReverseIteratorOnComposedEdge + { + friend class ComposedEdge; + friend class ElementaryEdge; + friend class QuadraticPolygon; + public: + INTERPKERNEL_EXPORT ReverseIteratorOnComposedEdge(); + INTERPKERNEL_EXPORT ReverseIteratorOnComposedEdge(ComposedEdge *compEdges); + INTERPKERNEL_EXPORT bool isValid() const { return _list_handle!=0; } + INTERPKERNEL_EXPORT void operator=(const ReverseIteratorOnComposedEdge& other); + INTERPKERNEL_EXPORT void first() { _deep_it=_list_handle->rbegin(); } + INTERPKERNEL_EXPORT void next() { _deep_it++; } + INTERPKERNEL_EXPORT void last(); + INTERPKERNEL_EXPORT void nextLoop(); + INTERPKERNEL_EXPORT void previousLoop(); + INTERPKERNEL_EXPORT bool finished() const { return _deep_it==_list_handle->rend(); } + INTERPKERNEL_EXPORT ElementaryEdge *current() { return *_deep_it; } + private: + std::list::reverse_iterator _deep_it; + std::list* _list_handle; + }; + } + #endif diff --git a/src/INTERP_KERNEL/Geometric2D/InterpKernelGeo2DComposedEdge.hxx b/src/INTERP_KERNEL/Geometric2D/InterpKernelGeo2DComposedEdge.hxx index 8d815c501..efcb4055d 100644 --- a/src/INTERP_KERNEL/Geometric2D/InterpKernelGeo2DComposedEdge.hxx +++ b/src/INTERP_KERNEL/Geometric2D/InterpKernelGeo2DComposedEdge.hxx @@ -45,6 +45,7 @@ namespace INTERP_KERNEL class ComposedEdge { friend class IteratorOnComposedEdge; + friend class ReverseIteratorOnComposedEdge; public: INTERPKERNEL_EXPORT ComposedEdge() { } INTERPKERNEL_EXPORT ComposedEdge(const ComposedEdge& other); diff --git a/src/INTERP_KERNEL/Geometric2D/InterpKernelGeo2DQuadraticPolygon.cxx b/src/INTERP_KERNEL/Geometric2D/InterpKernelGeo2DQuadraticPolygon.cxx index e5eac67e5..b0ca34748 100644 --- a/src/INTERP_KERNEL/Geometric2D/InterpKernelGeo2DQuadraticPolygon.cxx +++ b/src/INTERP_KERNEL/Geometric2D/InterpKernelGeo2DQuadraticPolygon.cxx @@ -708,7 +708,7 @@ void QuadraticPolygon::buildPartitionsAbs(QuadraticPolygon& other, std::set & zip_list, +void QuadraticPolygon::BuildPartitionFromZipList(const QuadraticPolygon & pol1, const std::list & zipList, std::set & edgesThis, std::set & edgesBoundaryOther, const std::map& mapp, const int idThis, const std::vector > & mapZipTo2, @@ -717,8 +717,8 @@ void QuadraticPolygon::BuildPartitionFromZipList(const QuadraticPolygon & pol1, std::vector& nbThis, std::vector& nbOther, std::vector& nbOtherI) { std::vector res; - if(!zip_list.empty()) - ClosePolygonsSimple(pol1, zip_list, res, mapZipTo2, nbOther,nbOtherI); + if(!zipList.empty()) + ClosePolygonsSimple(pol1, zipList, res, mapZipTo2, nbOther,nbOtherI); for(std::vector::iterator it=res.begin();it!=res.end();it++) { @@ -1206,23 +1206,23 @@ void QuadraticPolygon::ClosePolygons(std::list& pol2Zip, con } } -void QuadraticPolygon::ClosePolygonsSimple(const QuadraticPolygon& pol1, const std::list & zip_list, +void QuadraticPolygon::ClosePolygonsSimple(const QuadraticPolygon& pol1, const std::list & zipList, std::vector& results,const std::vector > & mapZipTo2, std::vector& nbOther, std::vector& nbOtherI) { // Register all edges of pol1 as 'alive' to start with. They will be consumed as they are - // added to the newly formed cells. Edges of pol2s (in zip_list) might be used more than once. - std::set edges1_alive; + // added to the newly formed cells. Edges of pol2s (in zipList) might be used more than once. + std::set edges1Alive; IteratorOnComposedEdge it(const_cast(&pol1)); for(; !it.finished(); it.next()) - edges1_alive.insert(it.current()); + edges1Alive.insert(it.current()); - int inf_loop_detect, i; + int infLoopDetect, i; std::list::const_iterator itt; - for (inf_loop_detect = pol1.recursiveSize() + 1, itt = zip_list.begin(); itt != zip_list.end(); itt++) - inf_loop_detect += 2*(*itt)->recursiveSize(); + for (infLoopDetect = pol1.recursiveSize() + 1, itt = zipList.begin(); itt != zipList.end(); itt++) + infLoopDetect += 2*(*itt)->recursiveSize(); - for (i = 0, it.first(); !edges1_alive.empty() && i <= inf_loop_detect;) + for (i = 0, it.first(); !edges1Alive.empty() && i <= infLoopDetect;) { QuadraticPolygon * qp = new QuadraticPolygon(); results.push_back(qp); @@ -1230,23 +1230,23 @@ void QuadraticPolygon::ClosePolygonsSimple(const QuadraticPolygon& pol1, const s int nbElemFrom2 = 0; // Start anywhere valid (=alive) on pol1 ElementaryEdge * startE; - for (startE = it.current(); edges1_alive.find(it.current()) == edges1_alive.end(); it.nextLoop(), startE = it.current()); + for (startE = it.current(); edges1Alive.find(it.current()) == edges1Alive.end(); it.nextLoop(), startE = it.current()); // Close the new cell: do { - ElementaryEdge *tmp_e = it.current(); - qp->pushBack(tmp_e->clone()); - edges1_alive.erase(tmp_e); - Node * nodeToTest = tmp_e->getEndNode(); + ElementaryEdge *tmpEd = it.current(); + qp->pushBack(tmpEd->clone()); + edges1Alive.erase(tmpEd); + Node * nodeToTest = tmpEd->getEndNode(); it.nextLoop(); i++; // Try to match the end node of the latest added edge from pol1 with one of the node from - // one of the ComposedEdge in zip_list + // one of the ComposedEdge in zipList std::list::const_iterator ret; bool direction; int idx; // for mapping - ret = CheckInList2(nodeToTest, zip_list, direction, idx); - if (ret == zip_list.end()) + ret = CheckInList2(nodeToTest, zipList, direction, idx); + if (ret == zipList.end()) continue; size_t oldSz = nbOther.size(); size_t addSz = std::distance(mapZipTo2[idx].begin(), mapZipTo2[idx].end()); @@ -1255,33 +1255,33 @@ void QuadraticPolygon::ClosePolygonsSimple(const QuadraticPolygon& pol1, const s nbElemFrom2 += addSz; if (!direction) (*ret)->reverse(); // TODO: discuss with Anthony - API of IteratorOnComposedEdge should allow an easy reverse looping? - // Switch to zip_list - IteratorOnComposedEdge it_pol2(*ret); - ElementaryEdge *tmp_e2; - for(/* it_pol2.first() */; !it_pol2.finished(); it_pol2.next(), i++) + // Switch to zipList + IteratorOnComposedEdge itPol2(*ret); + ElementaryEdge *tmpEd2; + for(/* itPol2.first() */; !itPol2.finished(); itPol2.next(), i++) { - tmp_e2 = it_pol2.current(); - qp->pushBack(tmp_e2->clone()); + tmpEd2 = itPol2.current(); + qp->pushBack(tmpEd2->clone()); } // search where we landed on pol1 to resume looping on it. - if (!edges1_alive.empty()) + if (!edges1Alive.empty()) { int j; - for(j = 0; it.current()->getStartNode() != tmp_e2->getEndNode() && j < inf_loop_detect; j++) // better way of doing this? + for(j = 0; it.current()->getStartNode() != tmpEd2->getEndNode() && j < infLoopDetect; j++) // better way of doing this? it.nextLoop(); - if (j >= inf_loop_detect) // could be a bit more precise and stop before ... + if (j >= infLoopDetect) // could be a bit more precise and stop before ... throw Exception("QuadraticPolygon::ClosePolygonsSimple() - Internal error - 1D intersection was looping infinitely!"); // sanity check - if we are not done with the cell completion, we should land // on a live edge. - if(edges1_alive.find(it.current()) == edges1_alive.end() && it.current() != startE) + if(edges1Alive.find(it.current()) == edges1Alive.end() && it.current() != startE) throw Exception("QuadraticPolygon::ClosePolygonsSimple() - Internal error ! Should never happen."); } } - while(it.current() != startE && !edges1_alive.empty() && i < inf_loop_detect); + while(it.current() != startE && !edges1Alive.empty() && i < infLoopDetect); // Complete mapping nbOtherI.push_back(nbElemFrom2+nbOtherI.back()); } - if (i >= inf_loop_detect) + if (i >= infLoopDetect) throw Exception("QuadraticPolygon::ClosePolygonsSimple() - Internal error - 1D intersection was looping infinitely!"); } @@ -1384,12 +1384,12 @@ std::list::iterator QuadraticPolygon::CheckInList(Node *n, s return iEnd; } -std::list::const_iterator QuadraticPolygon::CheckInList2(Node *n, const std::list & zip_list, +std::list::const_iterator QuadraticPolygon::CheckInList2(Node *n, const std::list & zipList, bool & direction, int & index) { direction = true; index = 0; - for(std::list::const_iterator iter = zip_list.begin(); iter!=zip_list.end(); iter++, index++) + for(std::list::const_iterator iter = zipList.begin(); iter!=zipList.end(); iter++, index++) { if ((*iter)->front()->getStartNode() == n) return iter; @@ -1402,7 +1402,7 @@ std::list::const_iterator QuadraticPolygon::CheckInList2(Nod } } } - return zip_list.end(); + return zipList.end(); } diff --git a/src/INTERP_KERNEL/Geometric2D/InterpKernelGeo2DQuadraticPolygon.hxx b/src/INTERP_KERNEL/Geometric2D/InterpKernelGeo2DQuadraticPolygon.hxx index b44dd3b5a..da2cf12f1 100644 --- a/src/INTERP_KERNEL/Geometric2D/InterpKernelGeo2DQuadraticPolygon.hxx +++ b/src/INTERP_KERNEL/Geometric2D/InterpKernelGeo2DQuadraticPolygon.hxx @@ -84,7 +84,7 @@ namespace INTERP_KERNEL INTERPKERNEL_EXPORT void appendCrudeData(const std::map& mapp, int offset, std::vector& addCoordsQuadratic, std::vector& conn, std::vector& connI) const; INTERPKERNEL_EXPORT void buildPartitionsAbs(QuadraticPolygon& other, std::set& edgesThis, std::set& edgesBoundaryOther, const std::map& mapp, int idThis, int idOther, int offset, std::vector& addCoordsQuadratic, std::vector& conn, std::vector& connI, std::vector& nb1, std::vector& nb2); - INTERPKERNEL_EXPORT static void BuildPartitionFromZipList(const QuadraticPolygon & pol1, const std::list & zip_list, + INTERPKERNEL_EXPORT static void BuildPartitionFromZipList(const QuadraticPolygon & pol1, const std::list & zipList, std::set & edgesThis, std::set & edgesBoundaryOther, const std::map& mapp, const int idThis, const std::vector > & mapZipTo2, @@ -133,7 +133,7 @@ namespace INTERP_KERNEL std::list zipConsecutiveInSegments() const; void dumpInXfigFile(std::ostream& stream, int resolution, const Bounds& box) const; static void ClosePolygons(std::list& pol2Zip, const QuadraticPolygon& pol1, const QuadraticPolygon& pol2, std::vector& results); - static void ClosePolygonsSimple(const QuadraticPolygon& pol1, const std::list & zip_list, + static void ClosePolygonsSimple(const QuadraticPolygon& pol1, const std::list & zipList, std::vector& results, const std::vector > & mapZipTo2, std::vector& nbOther, std::vector& nbOtherI); template @@ -146,7 +146,7 @@ namespace INTERP_KERNEL bool direction); static std::list::iterator CheckInList(Node *n, std::list::iterator iStart, std::list::iterator iEnd); - static std::list::const_iterator CheckInList2(Node *n, const std::list & zip_list, + static std::list::const_iterator CheckInList2(Node *n, const std::list & zipList, bool & direction, int & index); }; } diff --git a/src/MEDCoupling/MEDCouplingUMesh.cxx b/src/MEDCoupling/MEDCouplingUMesh.cxx index c98ad7159..e163c9a40 100644 --- a/src/MEDCoupling/MEDCouplingUMesh.cxx +++ b/src/MEDCoupling/MEDCouplingUMesh.cxx @@ -8972,10 +8972,10 @@ void MEDCouplingUMesh::Build2DCellsFrom1DCut(double eps, const MEDCouplingUMesh // Zip consecutive IN segments from the list of candidates, and discard the pieces which are ending nowhere (e.g. an IN polyline // with an end-point in the middle of the cell). std::vector > mapZipTo2; - std::list zip_list = INTERP_KERNEL::QuadraticPolygon::ZipConsecutiveSegments2(candidates2, pol2s, mapZipTo2); + std::list zipList = INTERP_KERNEL::QuadraticPolygon::ZipConsecutiveSegments2(candidates2, pol2s, mapZipTo2); // Now the core of the algo - main output is in cr, crI, cNb1, cNb2 and cNbI2. - INTERP_KERNEL::QuadraticPolygon::BuildPartitionFromZipList(pol1, zip_list, edges1, edgesBoundary2, mapp, i, mapZipTo2, + INTERP_KERNEL::QuadraticPolygon::BuildPartitionFromZipList(pol1, zipList, edges1, edgesBoundary2, mapp, i, mapZipTo2, offset3,addCoordsQuadratic,cr,crI, cNb1,cNb2, cNbI2); // Deals with remaining (non-consumed) edges from m1: these are the edges that were never touched // by m2 but that we still want to keep in the final result. @@ -8994,7 +8994,7 @@ void MEDCouplingUMesh::Build2DCellsFrom1DCut(double eps, const MEDCouplingUMesh // Memory clean-up for(std::map::const_iterator it=mappRev.begin();it!=mappRev.end();it++) (*it).second->decrRef(); - for (std::list::const_iterator itt = zip_list.begin(); itt != zip_list.end(); itt++) + for (std::list::const_iterator itt = zipList.begin(); itt != zipList.end(); itt++) delete(*itt); } } diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index a9888ec30..44dcae499 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -14742,9 +14742,9 @@ class MEDCouplingBasicsTest(unittest.TestCase): m2.setConnectivity(conn2, connI2) # End of construction of input meshes m1bis and m2 -> start of specific part of the test - m3, map1, map2, mapI2 = MEDCouplingUMesh.Intersect2DMeshWith1DLine(m1, m2, 1e-10) + m3, map1, map2, mapI2 = MEDCouplingUMesh.Intersect2DMeshWith1DLine(m1, m2, 1e-10) m3.mergeNodes(1.0e-8) - + self.assertEqual(3,m3.getNumberOfCells()) self.assertEqual(20,m3.getNumberOfNodes()) self.assertEqual(2,m3.getSpaceDimension()) -- 2.39.2