From dee84a26e9ffb8cf64fdf2eb3db96f363da08e2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Loriot?= Date: Tue, 14 Nov 2017 10:14:50 +0100 Subject: [PATCH] avoid creating a new overlapping curve if it has already been created this avoid some mismatch issues in the left events and the status line --- .../CGAL/Sweep_line_2/Sweep_line_2_impl.h | 45 +++++++++++++------ .../CGAL/Sweep_line_2/Sweep_line_subcurve.h | 26 +++++++++++ 2 files changed, 57 insertions(+), 14 deletions(-) diff --git a/Sweep_line_2/include/CGAL/Sweep_line_2/Sweep_line_2_impl.h b/Sweep_line_2/include/CGAL/Sweep_line_2/Sweep_line_2_impl.h index b1ad937d986..c8c332753f2 100644 --- a/Sweep_line_2/include/CGAL/Sweep_line_2/Sweep_line_2_impl.h +++ b/Sweep_line_2/include/CGAL/Sweep_line_2/Sweep_line_2_impl.h @@ -865,22 +865,39 @@ _create_overlapping_curve(const X_monotone_curve_2& overlap_cv, left_event->remove_curve_from_right(c2); // Allocate the new Subcurve for the overlap - Subcurve* overlap_sc; + Subcurve* overlap_sc=NULL; if (all_leaves_diff.empty()) { - CGAL_SL_PRINT_TEXT("Allocate a new subcurve for the overlap (no common subcurves)"); - CGAL_SL_PRINT_EOL(); - // no duplicate only one curve is needed - overlap_sc = this->m_subCurveAlloc.allocate(1); - this->m_subCurveAlloc.construct(overlap_sc, this->m_masterSubcurve); - overlap_sc->set_hint(this->m_statusLine.end()); - overlap_sc->init(overlap_cv); - overlap_sc->set_left_event(left_event); - overlap_sc->set_right_event(right_event); - m_overlap_subCurves.push_back(overlap_sc); - // sets the two originating subcurves of overlap_sc - overlap_sc->set_originating_subcurve1(c1); - overlap_sc->set_originating_subcurve2(c2); + // first check that an equivalent curve is not already in left_event + for (Subcurve_iterator iter = left_event->right_curves_begin(); iter != left_event->right_curves_end(); + ++iter) + { + if ( (*iter)->has_same_leaves(c1, c2) ) + { + CGAL_SL_PRINT_TEXT("Reuse overlapping curve "); + CGAL_SL_PRINT_CURVE(*iter); + CGAL_SL_PRINT_EOL(); + overlap_sc=*iter; + break; + } + } + + if (overlap_sc==NULL) + { + CGAL_SL_PRINT_TEXT("Allocate a new subcurve for the overlap (no common subcurves)"); + CGAL_SL_PRINT_EOL(); + // no duplicate only one curve is needed + overlap_sc = this->m_subCurveAlloc.allocate(1); + this->m_subCurveAlloc.construct(overlap_sc, this->m_masterSubcurve); + overlap_sc->set_hint(this->m_statusLine.end()); + overlap_sc->init(overlap_cv); + overlap_sc->set_left_event(left_event); + overlap_sc->set_right_event(right_event); + m_overlap_subCurves.push_back(overlap_sc); + // sets the two originating subcurves of overlap_sc + overlap_sc->set_originating_subcurve1(c1); + overlap_sc->set_originating_subcurve2(c2); + } } else{ CGAL_SL_PRINT_TEXT("Allocate new subcurves for the overlap (common subcurves)"); diff --git a/Sweep_line_2/include/CGAL/Sweep_line_2/Sweep_line_subcurve.h b/Sweep_line_2/include/CGAL/Sweep_line_2/Sweep_line_subcurve.h index 3efbb9b7be5..5120ba27486 100644 --- a/Sweep_line_2/include/CGAL/Sweep_line_2/Sweep_line_subcurve.h +++ b/Sweep_line_2/include/CGAL/Sweep_line_2/Sweep_line_subcurve.h @@ -229,6 +229,32 @@ public: return true; } + /*! Check if the two hierarchies (s1+s2 considered as an overlapping curve not already created) contain the same leaf nodes. */ + bool has_same_leaves(Self* s1, Self* s2) + { + std::list my_leaves; + std::list other_leaves; + + this->template all_leaves(std::back_inserter(my_leaves)); + s1->template all_leaves(std::back_inserter(other_leaves)); + s2->template all_leaves(std::back_inserter(other_leaves)); + + typename std::list::iterator iter; + for (iter = my_leaves.begin(); iter != my_leaves.end(); ++iter) { + if (std::find(other_leaves.begin(), other_leaves.end(), *iter) == + other_leaves.end()) + return false; + } + + for (iter = other_leaves.begin(); iter != other_leaves.end(); ++iter) { + if (std::find(my_leaves.begin(), my_leaves.end(), *iter) == + my_leaves.end()) + return false; + } + + return true; + } + /*! Check if the two hierarchies contain a common leaf node. */ bool has_common_leaf(Self *s) {