From acb96cba86842093ea6d975df04750c46dbffb95 Mon Sep 17 00:00:00 2001 From: Simon Giraudot Date: Wed, 12 Aug 2020 15:56:46 +0200 Subject: [PATCH] Get rid of index using pointer squatting + before/after functions --- .../include/CGAL/Arr_dcel_base.h | 7 ++- .../include/CGAL/Arr_overlay_2.h | 62 ++++++++++++++----- .../CGAL/No_intersection_surface_sweep_2.h | 2 + 3 files changed, 53 insertions(+), 18 deletions(-) diff --git a/Arrangement_on_surface_2/include/CGAL/Arr_dcel_base.h b/Arrangement_on_surface_2/include/CGAL/Arr_dcel_base.h index 76f490663f0..2c685385abc 100644 --- a/Arrangement_on_surface_2/include/CGAL/Arr_dcel_base.h +++ b/Arrangement_on_surface_2/include/CGAL/Arr_dcel_base.h @@ -90,6 +90,11 @@ public: /*! Destructor. */ virtual ~Arr_vertex_base() {} + // Access/modification for pointer squatting + void* inc() const { return p_inc; } + void set_inc(void * inc) const + { const_cast(*this).p_inc = inc; } + /*! Check if the point pointer is nullptr. */ bool has_null_point() const { return (p_pt == nullptr); } @@ -287,8 +292,6 @@ public: typedef Arr_halfedge Halfedge; typedef Arr_isolated_vertex Isolated_vertex; - mutable std::size_t index; - /*! Default constructor. */ Arr_vertex() {} diff --git a/Arrangement_on_surface_2/include/CGAL/Arr_overlay_2.h b/Arrangement_on_surface_2/include/CGAL/Arr_overlay_2.h index 569d6e66d93..d6b55cffd2c 100644 --- a/Arrangement_on_surface_2/include/CGAL/Arr_overlay_2.h +++ b/Arrangement_on_surface_2/include/CGAL/Arr_overlay_2.h @@ -40,24 +40,31 @@ namespace CGAL { -template +template class Indexed_sweep_accessor { - std::size_t nbv; + const Arr1& arr1; + const Arr2& arr2; + mutable std::vector backup_inc; public: - Indexed_sweep_accessor (std::size_t nbv) : nbv(nbv) { } - std::size_t nb_vertices() const { return nbv; } + Indexed_sweep_accessor (const Arr1& arr1, const Arr2& arr2) + : arr1(arr1), arr2(arr2) { } + + std::size_t nb_vertices() const + { + return arr1.number_of_vertices() + arr2.number_of_vertices(); + } std::size_t source_index (const Curve& c) const { - return halfedge(c)->target()->index; + return reinterpret_cast(halfedge(c)->target()->inc()); } std::size_t target_index (const Curve& c) const { - return halfedge(c)->source()->index; + return reinterpret_cast(halfedge(c)->source()->inc()); } const Curve& curve (const Curve& c) const @@ -65,6 +72,35 @@ public: return c; } + void before_init() const + { + std::size_t idx = 0; + backup_inc.reserve (nb_vertices()); + for (typename Arr1::Vertex_const_iterator vit = arr1.vertices_begin(); + vit != arr1.vertices_end(); ++vit, ++idx) + { + backup_inc[idx] = vit->inc(); + vit->set_inc (reinterpret_cast(idx)); + } + for (typename Arr2::Vertex_const_iterator vit = arr2.vertices_begin(); + vit != arr2.vertices_end(); ++vit, ++idx) + { + backup_inc[idx] = vit->inc(); + vit->set_inc (reinterpret_cast(idx)); + } + } + + void after_init() const + { + std::size_t idx = 0; + for (typename Arr1::Vertex_const_iterator vit = arr1.vertices_begin(); + vit != arr1.vertices_end(); ++vit, ++idx) + vit->set_inc (backup_inc[idx]); + for (typename Arr2::Vertex_const_iterator vit = arr2.vertices_begin(); + vit != arr2.vertices_end(); ++vit, ++idx) + vit->set_inc (backup_inc[idx]); + } + private: typename Curve::Halfedge_handle halfedge (const Curve& c) const @@ -168,15 +204,6 @@ overlay(const Arrangement_on_surface_2& arr1 CGAL_precondition(((void*)(&arr) != (void*)(&arr1)) && ((void*)(&arr) != (void*)(&arr2))); - // Index vertices for indexed sweep - std::size_t idx = 0; - for (typename Arr_a::Vertex_const_iterator vit = arr1.vertices_begin(); - vit != arr1.vertices_end(); ++vit, ++idx) - vit->index = idx; - for (typename Arr_b::Vertex_const_iterator vit = arr2.vertices_begin(); - vit != arr2.vertices_end(); ++vit, ++idx) - vit->index = idx; - // Prepare a vector of extended x-monotone curves that represent all edges // in both input arrangements. Each curve is associated with a halfedge // directed from right to left. @@ -231,7 +258,10 @@ overlay(const Arrangement_on_surface_2& arr1 if (total_iso_verts == 0) { // Clear the result arrangement and perform the sweep to construct it. arr.clear(); - surface_sweep.indexed_sweep (xcvs_vec, Indexed_sweep_accessor(idx)); + surface_sweep.indexed_sweep (xcvs_vec, + Indexed_sweep_accessor + + (arr1, arr2)); xcvs_vec.clear(); return; } diff --git a/Surface_sweep_2/include/CGAL/No_intersection_surface_sweep_2.h b/Surface_sweep_2/include/CGAL/No_intersection_surface_sweep_2.h index 659624f48b7..34bd84ce139 100644 --- a/Surface_sweep_2/include/CGAL/No_intersection_surface_sweep_2.h +++ b/Surface_sweep_2/include/CGAL/No_intersection_surface_sweep_2.h @@ -314,7 +314,9 @@ public: const Accessor& accessor) { m_visitor->before_sweep(); + accessor.before_init(); _init_indexed_sweep(edges, accessor); + accessor.after_init(); _sweep(); _complete_sweep(); m_visitor->after_sweep();