CGAL: convex hull of points with info

匿名 (未验证) 提交于 2019-12-03 09:02:45

问题:

I have a vector of 2D points (N elements) in the plane. I want to make the convex hull of these points. After that, I want to retrieve the vector index of each vertex in the convex hull, how can I do this?

I know that, there is such possibility for triangulation by making use of vector<pair<Point_2, unsigned> >, but when I use paired point in making convex hull, it produces a bunch of errors. This is the related piece of code that I use:

#include <iostream> #include <CGAL/Exact_predicates_inexact_constructions_kernel.h> #include <CGAL/convex_hull_2.h> #include <CGAL/convex_hull_traits_2.h>  using namespace std;  typedef CGAL::Exact_predicates_inexact_constructions_kernel K; typedef K::Point_2 Point_2; typedef pair<Point_2, unsigned> Paired; typedef CGAL::convex_hull_traits_2<Paired> ch_traits;  typedef vector<Paired> Vector;  int main() {     Vector points;     Vector result;      for (int i = 0; i < 5; i++)          points.push_back(make_pair(Point_2(i, i), i));     CGAL::convex_hull_2( points.begin(), points.end(), back_inserter(result), const Traits &ch_traits );    return 0; } 

回答1:

You need to provide a traits class model of ConvexHullTraits, the pair type being your point type. In practice it is rather simple, you simply need a struct containing all functors, with the operators simply forwarding to the CGAL traits class functor using pair::first.

Here is a simple example:

#include <iostream> #include <CGAL/Exact_predicates_inexact_constructions_kernel.h> #include <CGAL/convex_hull_2.h> #include <CGAL/convex_hull_traits_2.h>  #include <boost/foreach.hpp>  typedef CGAL::Exact_predicates_inexact_constructions_kernel K; typedef K::Point_2 Point_2; typedef std::pair<Point_2, unsigned> Point_with_info;  template <class F> struct Forward_functor   : public F {   template <class Point_2>   bool operator() (const Point_2& p, const Point_2& q) const   {     return static_cast<const F*>(this)->operator()(p.first, q.first);   }    template <class Point_2>   bool operator() (const Point_2& p, const Point_2& q, const Point_2& r) const   {     return static_cast<const F*>(this)->operator()(p.first, q.first, r.first);   }    template <class Point_2>   bool operator() (const Point_2& p, const Point_2& q, const Point_2& r, const Point_2& s) const   {     return static_cast<const F*>(this)->operator()(p.first, q.first, r.first, s.first);   } };  struct CH_traits_for_point_with_info {   typedef Point_with_info Point_2;   typedef CGAL::Convex_hull_traits_2<K> Base;   typedef Forward_functor<Base::Less_xy_2> Less_xy_2;   typedef Forward_functor<Base::Less_yx_2> Less_yx_2;   typedef Forward_functor<Base::Less_signed_distance_to_line_2> Less_signed_distance_to_line_2;   typedef Forward_functor<Base::Less_rotate_ccw_2> Less_rotate_ccw_2;   typedef Forward_functor<Base::Left_turn_2> Left_turn_2;   typedef Forward_functor<Base::Equal_2> Equal_2;    struct Orientation_2   {     CGAL::Orientation     operator()(const Point_2& p, const Point_2& q, const Point_2& r) const     {       return Base::Orientation_2()(p.first, q.first, r.first);     }   };    Equal_2 equal_2_object () const   {     return Equal_2();   }    Less_xy_2 less_xy_2_object () const   {     return Less_xy_2();   }    Less_yx_2 less_yx_2_object () const   {     return Less_yx_2();   }    Less_signed_distance_to_line_2 less_signed_distance_to_line_2_object () const   {     return Less_signed_distance_to_line_2();   }    Less_rotate_ccw_2 less_rotate_ccw_2_object () const   {     return Less_rotate_ccw_2();   }    Left_turn_2 left_turn_2_object () const   {     return Left_turn_2();   }    Orientation_2 orientation_2_object () const   {     return Orientation_2();   } };  int main() {   std::vector<Point_with_info> input_points;   std::vector<Point_with_info> result;    input_points.push_back( Point_with_info(Point_2(0,0), 0) );   input_points.push_back( Point_with_info(Point_2(1,0), 1) );   input_points.push_back( Point_with_info(Point_2(0,1), 2) );   input_points.push_back( Point_with_info(Point_2(0.25,0.25), 3) );    CGAL::convex_hull_2(input_points.begin(), input_points.end(),                       back_inserter(result), CH_traits_for_point_with_info() );    BOOST_FOREACH(const Point_with_info& p, result)   {     std::cout << p.first << " - " << p.second << "\n";   }    return 0; } 

Here is a more complicated example where the points are not copied and the algorithm is working with point indices:

#include <iostream> #include <CGAL/Exact_predicates_inexact_constructions_kernel.h> #include <CGAL/convex_hull_2.h> #include <CGAL/convex_hull_traits_2.h>  #include <boost/foreach.hpp>  typedef CGAL::Exact_predicates_inexact_constructions_kernel K; typedef K::Point_2 Point_2;   template <class F> struct Forward_functor   : public F {   const std::vector<Point_2>& points;    Forward_functor(const std::vector<Point_2>& points)     : points(points)   {}    template <class Id>   bool operator() (const Id& p, const Id& q) const   {     return static_cast<const F*>(this)->operator()(points[p], points[q]);   }    template <class Id>   bool operator() (const Id& p, const Id& q, const Id& r) const   {     return static_cast<const F*>(this)->operator()(points[p], points[q], points[r]);   }    template <class Id>   bool operator() (const Id& p, const Id& q, const Id& r, const Id& s) const   {     return static_cast<const F*>(this)->operator()(points[p], points[q], points[r], points[s]);   } };  struct CH_traits_for_point_with_info {   const std::vector<K::Point_2>& points;    CH_traits_for_point_with_info(const std::vector<K::Point_2>& points)     : points(points)   {}    typedef unsigned Point_2;   typedef CGAL::Convex_hull_traits_2<K> Base;   typedef Forward_functor<Base::Less_xy_2> Less_xy_2;   typedef Forward_functor<Base::Less_yx_2> Less_yx_2;   typedef Forward_functor<Base::Less_signed_distance_to_line_2> Less_signed_distance_to_line_2;   typedef Forward_functor<Base::Less_rotate_ccw_2> Less_rotate_ccw_2;   typedef Forward_functor<Base::Left_turn_2> Left_turn_2;   typedef Forward_functor<Base::Equal_2> Equal_2;    struct Orientation_2   {     const std::vector<K::Point_2>& points;      Orientation_2(const std::vector<K::Point_2>& points)       : points(points)     {}      CGAL::Orientation     operator()(Point_2 p, Point_2 q, Point_2 r) const     {       return Base::Orientation_2()(points[p], points[q], points[r]);     }   };    Equal_2 equal_2_object () const   {     return Equal_2(points);   }    Less_xy_2 less_xy_2_object () const   {     return Less_xy_2(points);   }    Less_yx_2 less_yx_2_object () const   {     return Less_yx_2(points);   }    Less_signed_distance_to_line_2 less_signed_distance_to_line_2_object () const   {     return Less_signed_distance_to_line_2(points);   }    Less_rotate_ccw_2 less_rotate_ccw_2_object () const   {     return Less_rotate_ccw_2(points);   }    Left_turn_2 left_turn_2_object () const   {     return Left_turn_2(points);   }    Orientation_2 orientation_2_object () const   {     return Orientation_2(points);   } };  int main() {   std::vector<Point_2> input_points;   std::vector<unsigned> ids;   std::vector<unsigned> result;    input_points.push_back( Point_2(0,0) );   input_points.push_back( Point_2(0,1) );   input_points.push_back( Point_2(1,0) );   input_points.push_back( Point_2(0.25,0.25) );    ids.push_back(0);   ids.push_back(1);   ids.push_back(2);   ids.push_back(3);    CGAL::convex_hull_2(ids.begin(), ids.end(),                       back_inserter(result), CH_traits_for_point_with_info(input_points) );    BOOST_FOREACH(unsigned i, result)   {     std::cout << input_points[i] << " - " << i << "\n";   }    return 0; } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!