From 8890a779eb8d36dd35f8c4f9ec97c705bb9286bf Mon Sep 17 00:00:00 2001 From: Efi Fogel Date: Wed, 25 May 2022 10:48:14 +0300 Subject: [PATCH] ixed drawing of 2D arrangements --- .../include/CGAL/draw_arrangement_2.h | 166 ++++++++++++++---- 1 file changed, 130 insertions(+), 36 deletions(-) diff --git a/Arrangement_on_surface_2/include/CGAL/draw_arrangement_2.h b/Arrangement_on_surface_2/include/CGAL/draw_arrangement_2.h index 9b52b282c74..70dc3b61cc5 100644 --- a/Arrangement_on_surface_2/include/CGAL/draw_arrangement_2.h +++ b/Arrangement_on_surface_2/include/CGAL/draw_arrangement_2.h @@ -6,44 +6,69 @@ #include #include #include +#include namespace CGAL { // Viewer class for Polygon_2 -template -class Arr_2_viewer_qt : public Basic_viewer_qt { +template +class Arr_2_basic_viewer_qt : public Basic_viewer_qt { + typedef GeometryTraits_2 Gt; + typedef CGAL::Arrangement_2 Arr; typedef Basic_viewer_qt Base; - typedef Arrangement_2 Arr; typedef typename Arr::Point_2 Point; + typedef typename Arr::Vertex_const_handle Vertex_const_handle; + typedef typename Arr::Halfedge_const_handle Halfedge_const_handle; typedef typename Arr::Face_const_handle Face_const_handle; + typedef typename Arr::Ccb_halfedge_const_circulator + Ccb_halfedge_const_circulator; public: /// Construct the viewer. /// @param arr the arrangement to view /// @param title the title of the window - Arr_2_viewer_qt(QWidget* parent, const Arr& arr, - const char* title = "2D Arrangement Basic Viewer") : + Arr_2_basic_viewer_qt(QWidget* parent, const Arr& arr, + const char* title = "2D Arrangement Basic Viewer") : // First draw: vertices; edges, faces; multi-color; no inverse normal Base(parent, title, true, true, true, false, false), m_arr(arr) - { add_elements(); } + {} + + //! + void add_elements() { + clear(); + if (m_arr.is_empty()) return; + CGAL::IO::Color c(75,160,255); + for (auto it = m_arr.unbounded_faces_begin(); + it != m_arr.unbounded_faces_end(); ++it) + add_face(it, c); + for (auto it = m_arr.edges_begin(); it != m_arr.edges_end(); ++it) + add_edge(it); + for (auto it = m_arr.vertices_begin(); it != m_arr.vertices_end(); ++it) + add_vertex(it); + } protected: //! - void add_ccb(typename Arr::Ccb_halfedge_const_circulator circ, bool has_area = true) { + virtual void add_ccb(Ccb_halfedge_const_circulator circ) { + std::cout << "1 add_ccb\n"; typename Arr::Ccb_halfedge_const_circulator curr = circ; - do { - typename Arr::Halfedge_const_handle e = curr; - add_segment(e->source()->point(), e->target()->point()); - add_point(e->source()->point()); - if (has_area) add_point_in_face(e->source()->point()); - } while (++curr != circ); + do this->add_point_in_face(curr->source()->point()); + while (++curr != circ); } //! - void add_face(Face_const_handle face, CGAL::IO::Color c) { + virtual void add_edge(Halfedge_const_handle e) + { this->add_segment(e->source()->point(), e->target()->point()); } + + //! + virtual void add_vertex(Vertex_const_handle v) + { this->add_point(v->point()); } + + //! + void add_face(Face_const_handle face, CGAL::IO::Color color) { if (! face->is_unbounded()) { - face_begin(c); + face_begin(color); add_ccb(face->outer_ccb()); for (auto iv = face->isolated_vertices_begin(); iv != face->isolated_vertices_end(); ++iv) @@ -51,28 +76,21 @@ protected: face_end(); } - std::map visited; - for (auto it = face->inner_ccbs_begin(); it != face->inner_ccbs_end(); ++it) { - auto new_face = (*it)->twin()->face(); - if (new_face == face) { - add_ccb(*it, false); - continue; - } - if (visited.find(new_face) != visited.end()) continue; - visited[new_face] = true; - add_face(new_face, c); + for (auto it = face->inner_ccbs_begin(); it != face->inner_ccbs_end(); ++it) + { + std::map visited; + auto curr = *it; + do { + auto new_face = curr->twin()->face(); + if (new_face == face) continue; + if (visited.find(new_face) == visited.end()) { + visited[new_face] = true; + add_face(new_face, color); + } + } while (++curr != *it); } } - //! - void add_elements() { - clear(); - if (m_arr.is_empty()) return; - CGAL::IO::Color c(75,160,255); - for (auto it = m_arr.unbounded_faces_begin(); it != m_arr.unbounded_faces_end(); ++it) - add_face(it, c); - } - //! virtual void keyPressEvent(QKeyEvent* e) { // Test key pressed: @@ -90,9 +108,84 @@ protected: } protected: - const Arrangement_2& m_arr; + //! The arrangement to draw + const Arr& m_arr; }; +//! Basic viewer of a 2D arrangement. +template +class Arr_2_viewer_qt : public Arr_2_basic_viewer_qt { +public: + typedef GeometryTraits_2 Gt; + typedef CGAL::Arrangement_2 Arr; + typedef Arr_2_basic_viewer_qt Base; + typedef typename Arr::Point_2 Point; + typedef typename Arr::Halfedge_const_handle Halfedge_const_handle; + typedef typename Arr::Face_const_handle Face_const_handle; + typedef typename Arr::Ccb_halfedge_const_circulator + Ccb_halfedge_const_circulator; + + /// Construct the viewer. + /// @param arr the arrangement to view + /// @param title the title of the window + Arr_2_viewer_qt(QWidget* parent, const Arr& arr, + const char* title = "2D Arrangement Basic Viewer") : + Base(parent, arr, title) + {} +}; + +//! +template +class Arr_2_viewer_qt, + Dcel> : + public Arr_2_basic_viewer_qt, Dcel> { +public: + typedef Arr_conic_traits_2 Gt; + typedef CGAL::Arrangement_2 Arr; + typedef Arr_2_basic_viewer_qt Base; + typedef typename Arr::Point_2 Point; + typedef typename Arr::Halfedge_const_handle Halfedge_const_handle; + typedef typename Arr::Face_const_handle Face_const_handle; + typedef typename Arr::Ccb_halfedge_const_circulator + Ccb_halfedge_const_circulator; + + /// Construct the viewer. + /// @param arr the arrangement to view + /// @param title the title of the window + Arr_2_viewer_qt(QWidget* parent, const Arr& arr, + const char* title = "2D Arrangement Basic Viewer") : + Base(parent, arr, title) + {} + + //! + virtual void add_ccb(Ccb_halfedge_const_circulator circ) { + const auto* traits = this->m_arr.geometry_traits(); + auto approx = traits->approximate_2_object(); + typename Arr::Ccb_halfedge_const_circulator curr = circ; + do { + std::vector polyline; + approx(curr->curve(), 10, std::back_inserter(polyline)); + for (const auto& p : polyline) this->add_point_in_face(p); + } while (++curr != circ); + } + + //! + virtual void add_edge(Halfedge_const_handle e) { + const auto* traits = this->m_arr.geometry_traits(); + auto approx = traits->approximate_2_object(); + std::vector polyline; + approx(e->curve(), 10, std::back_inserter(polyline)); + + auto it = polyline.begin(); + auto prev = it++; + for (; it != polyline.end(); prev = it++) + this->add_segment(*prev, *it); + } +}; + +//! template void draw(const CGAL::Arrangement_2& arr, const char* title = "2D Arrangement Basic Viewer") { @@ -104,7 +197,8 @@ void draw(const CGAL::Arrangement_2& arr, int argc = 1; const char* argv[2] = {"t2_viewer", nullptr}; QApplication app(argc, const_cast(argv)); - Arr_2_viewer_qtmainwindow(app.activeWindow(), arr, title); + Arr_2_viewer_qt mainwindow(app.activeWindow(), arr, title); + mainwindow.add_elements(); mainwindow.show(); app.exec(); }