From 5e9b8e3bbf599979ba789b0e9cdbde1f6c95fdad Mon Sep 17 00:00:00 2001 From: Andreas Fabri Date: Wed, 3 Apr 2019 16:23:58 +0200 Subject: [PATCH] Add an adapter so that one can sort arbitary things and access the point via a property map --- .../Convex_hull_2/convex_hull_indices.cpp | 39 ++++++ .../CGAL/Convex_hull_traits_adapter_2.h | 128 ++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 Convex_hull_2/examples/Convex_hull_2/convex_hull_indices.cpp create mode 100644 Convex_hull_2/include/CGAL/Convex_hull_traits_adapter_2.h diff --git a/Convex_hull_2/examples/Convex_hull_2/convex_hull_indices.cpp b/Convex_hull_2/examples/Convex_hull_2/convex_hull_indices.cpp new file mode 100644 index 00000000000..9e03ce2f528 --- /dev/null +++ b/Convex_hull_2/examples/Convex_hull_2/convex_hull_indices.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include + +typedef CGAL::Exact_predicates_inexact_constructions_kernel K; +typedef K::Point_2 Point_2; +typedef CGAL::Convex_hull_traits_adapter_2::type > Convex_hull_traits_2; + +int main() +{ + std::vector points; + + points.push_back(Point_2(10,0)); + points.push_back(Point_2(0,10)); + points.push_back(Point_2(1,1)); + points.push_back(Point_2(3,4)); + points.push_back(Point_2(0,0)); + points.push_back(Point_2(10,10)); + points.push_back(Point_2(2,6)); + + std::vector indices(points.size()), out; + + for(int i=0; i < indices.size();i++){ + indices[i] = i; + } + + + CGAL::convex_hull_2(indices.begin(), indices.end(), std::back_inserter(out), + Convex_hull_traits_2(CGAL::make_property_map(points))); + + for( std::size_t i : out){ + std::cout << "points[" << i << "] = " << points[i] << std::endl; + } + + return 0; +} diff --git a/Convex_hull_2/include/CGAL/Convex_hull_traits_adapter_2.h b/Convex_hull_2/include/CGAL/Convex_hull_traits_adapter_2.h new file mode 100644 index 00000000000..75e4a746232 --- /dev/null +++ b/Convex_hull_2/include/CGAL/Convex_hull_traits_adapter_2.h @@ -0,0 +1,128 @@ +// Copyright (c) 2019 GeometryFactory (France). +// All rights reserved. +// +// This file is part of CGAL (www.cgal.org); you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation; either version 3 of the License, +// or (at your option) any later version. +// +// Licensees holding a valid commercial license may use this file in +// accordance with the commercial license agreement provided with the software. +// +// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +// +// $URL$ +// $Id$ +// SPDX-License-Identifier: LGPL-3.0+ +// +// Author(s) : Sebastien Loriot + + +#ifndef CGAL_CONVEX_HULL_TRAITS_ADAPTER_2_H +#define CGAL_CONVEX_HULL_TRAITS_ADAPTER_2_H + +#include + +#include + +#include + + +namespace CGAL{ + +using ::get; + +template +class Convex_hull_traits_adapter_2:public Base_traits{ + PointPropertyMap ppmap_; +public: + Convex_hull_traits_adapter_2(Base_traits base=Base_traits()):Base_traits(base){} + + Convex_hull_traits_adapter_2(const PointPropertyMap& ppmap,Base_traits base=Base_traits()) + :Base_traits(base),ppmap_(ppmap){} + + typedef Base_traits Gt; + typedef typename boost::property_traits::key_type Point_2; + typedef typename boost::call_traits::param_type Arg_type; + + struct Less_xy_2 : public Base_traits::Less_xy_2{ + Less_xy_2(const PointPropertyMap& ppmap,const typename Base_traits::Less_xy_2& base): + Base_traits::Less_xy_2(base),ppmap_(ppmap){} + const PointPropertyMap& ppmap_; + bool operator()(Arg_type p,Arg_type q) const { + return static_cast(this)->operator()(get(ppmap_,p),get(ppmap_,q)); + } + }; + + struct Equal_2 : public Base_traits::Equal_2{ + Equal_2(const PointPropertyMap& ppmap,const typename Base_traits::Equal_2& base): + Base_traits::Equal_2(base),ppmap_(ppmap){} + const PointPropertyMap& ppmap_; + bool operator()(Arg_type p,Arg_type q) const { + return static_cast(this)->operator()(get(ppmap_,p),get(ppmap_,q)); + } + }; + + struct Less_yx_2 : public Base_traits::Less_yx_2{ + Less_yx_2(const PointPropertyMap& ppmap,const typename Base_traits::Less_yx_2& base): + Base_traits::Less_yx_2(base),ppmap_(ppmap){} + const PointPropertyMap& ppmap_; + bool operator()(Arg_type p,Arg_type q) const { + return static_cast(this)->operator()(get(ppmap_,p),get(ppmap_,q)); + } + }; + + struct Left_turn_2 : public Base_traits::Left_turn_2{ + Left_turn_2(const PointPropertyMap& ppmap,const typename Base_traits::Left_turn_2& base): + Base_traits::Left_turn_2(base),ppmap_(ppmap){} + const PointPropertyMap& ppmap_; + bool operator()(Arg_type p,Arg_type q, Arg_type r) const { + return static_cast(this)->operator()(get(ppmap_,p),get(ppmap_,q),get(ppmap_,r)); + } + }; + + struct Orientation_2 : public Base_traits::Orientation_2{ + Orientation_2(const PointPropertyMap& ppmap,const typename Base_traits::Orientation_2& base): + Base_traits::Orientation_2(base),ppmap_(ppmap){} + const PointPropertyMap& ppmap_; + CGAL::Orientation operator()(Arg_type p,Arg_type q, Arg_type r) const { + return static_cast(this)->operator()(get(ppmap_,p),get(ppmap_,q),get(ppmap_,r)); + } + }; + + struct Less_rotate_ccw_2 : public Base_traits::Less_rotate_ccw_2{ + Less_rotate_ccw_2(const PointPropertyMap& ppmap,const typename Base_traits::Less_rotate_ccw_2& base): + Base_traits::Less_rotate_ccw_2(base),ppmap_(ppmap){} + const PointPropertyMap& ppmap_; + bool operator()(Arg_type p,Arg_type q, Arg_type r) const { + return static_cast(this)->operator()(get(ppmap_,p),get(ppmap_,q),get(ppmap_,r)); + } + }; + + struct Less_signed_distance_to_line_2 : public Base_traits::Less_signed_distance_to_line_2{ + Less_signed_distance_to_line_2(const PointPropertyMap& ppmap,const typename Base_traits::Less_signed_distance_to_line_2& base): + Base_traits::Less_signed_distance_to_line_2(base),ppmap_(ppmap){} + const PointPropertyMap& ppmap_; + bool operator()(Arg_type p,Arg_type q, Arg_type r, Arg_type s) const { + return static_cast(this)->operator()(get(ppmap_,p),get(ppmap_,q),get(ppmap_,r),get(ppmap_,s)); + } + }; + + Equal_2 equal_2_object () const {return Equal_2(ppmap_,static_cast(this)->equal_2_object() );} + Left_turn_2 left_turn_2_object () const {return Left_turn_2(ppmap_,static_cast(this)->left_turn_2_object() );} + Orientation_2 orientation_2_object () const {return Orientation_2(ppmap_,static_cast(this)->orientation_2_object() );} + Less_rotate_ccw_2 less_rotate_ccw_2_object () const {return Less_rotate_ccw_2(ppmap_,static_cast(this)->less_rotate_ccw_2_object() );} + Less_signed_distance_to_line_2 less_signed_distance_to_line_2_object () const {return Less_signed_distance_to_line_2(ppmap_,static_cast(this)->less_signed_distance_to_line_2_object() );} + Less_xy_2 less_xy_2_object () const {return Less_xy_2(ppmap_,static_cast(this)->less_xy_2_object() );} + Less_yx_2 less_yx_2_object () const {return Less_yx_2(ppmap_,static_cast(this)->less_yx_2_object() );} + + const PointPropertyMap& point_property_map() const {return ppmap_;} + +}; + +} //namespace CGAL + +#include + +#endif //CGAL_CONVEX_HULL_TRAITS_ADAPTER_2_H