Get rid of index using pointer squatting + before/after functions

This commit is contained in:
Simon Giraudot 2020-08-12 15:56:46 +02:00
parent caf5b31af6
commit acb96cba86
3 changed files with 53 additions and 18 deletions

View File

@ -90,6 +90,11 @@ public:
/*! Destructor. */ /*! Destructor. */
virtual ~Arr_vertex_base() {} virtual ~Arr_vertex_base() {}
// Access/modification for pointer squatting
void* inc() const { return p_inc; }
void set_inc(void * inc) const
{ const_cast<Arr_vertex_base&>(*this).p_inc = inc; }
/*! Check if the point pointer is nullptr. */ /*! Check if the point pointer is nullptr. */
bool has_null_point() const { return (p_pt == nullptr); } bool has_null_point() const { return (p_pt == nullptr); }
@ -287,8 +292,6 @@ public:
typedef Arr_halfedge<V,H,F> Halfedge; typedef Arr_halfedge<V,H,F> Halfedge;
typedef Arr_isolated_vertex<V,H,F> Isolated_vertex; typedef Arr_isolated_vertex<V,H,F> Isolated_vertex;
mutable std::size_t index;
/*! Default constructor. */ /*! Default constructor. */
Arr_vertex() {} Arr_vertex() {}

View File

@ -40,24 +40,31 @@
namespace CGAL { namespace CGAL {
template <typename Curve> template <typename Arr1, typename Arr2, typename Curve>
class Indexed_sweep_accessor class Indexed_sweep_accessor
{ {
std::size_t nbv; const Arr1& arr1;
const Arr2& arr2;
mutable std::vector<void*> backup_inc;
public: 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 std::size_t source_index (const Curve& c) const
{ {
return halfedge(c)->target()->index; return reinterpret_cast<std::size_t>(halfedge(c)->target()->inc());
} }
std::size_t target_index (const Curve& c) const std::size_t target_index (const Curve& c) const
{ {
return halfedge(c)->source()->index; return reinterpret_cast<std::size_t>(halfedge(c)->source()->inc());
} }
const Curve& curve (const Curve& c) const const Curve& curve (const Curve& c) const
@ -65,6 +72,35 @@ public:
return c; 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<void*>(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<void*>(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: private:
typename Curve::Halfedge_handle halfedge (const Curve& c) const typename Curve::Halfedge_handle halfedge (const Curve& c) const
@ -168,15 +204,6 @@ overlay(const Arrangement_on_surface_2<GeometryTraitsA_2, TopologyTraitsA>& arr1
CGAL_precondition(((void*)(&arr) != (void*)(&arr1)) && CGAL_precondition(((void*)(&arr) != (void*)(&arr1)) &&
((void*)(&arr) != (void*)(&arr2))); ((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 // Prepare a vector of extended x-monotone curves that represent all edges
// in both input arrangements. Each curve is associated with a halfedge // in both input arrangements. Each curve is associated with a halfedge
// directed from right to left. // directed from right to left.
@ -231,7 +258,10 @@ overlay(const Arrangement_on_surface_2<GeometryTraitsA_2, TopologyTraitsA>& arr1
if (total_iso_verts == 0) { if (total_iso_verts == 0) {
// Clear the result arrangement and perform the sweep to construct it. // Clear the result arrangement and perform the sweep to construct it.
arr.clear(); arr.clear();
surface_sweep.indexed_sweep (xcvs_vec, Indexed_sweep_accessor<Ovl_x_monotone_curve_2>(idx)); surface_sweep.indexed_sweep (xcvs_vec,
Indexed_sweep_accessor
<Arr_a, Arr_b, Ovl_x_monotone_curve_2>
(arr1, arr2));
xcvs_vec.clear(); xcvs_vec.clear();
return; return;
} }

View File

@ -314,7 +314,9 @@ public:
const Accessor& accessor) const Accessor& accessor)
{ {
m_visitor->before_sweep(); m_visitor->before_sweep();
accessor.before_init();
_init_indexed_sweep(edges, accessor); _init_indexed_sweep(edges, accessor);
accessor.after_init();
_sweep(); _sweep();
_complete_sweep(); _complete_sweep();
m_visitor->after_sweep(); m_visitor->after_sweep();