CGAL:带有信息的凸凸包 [英] CGAL: convex hull of points with info

查看:179
本文介绍了CGAL:带有信息的凸凸包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在平面上有一个2D点(N个元素)的向量。我想把这些点做成凸包。之后,我想检索凸包中每个顶点的矢量索引,该怎么做?

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?

我知道,通过利用 vector< pair< Point_2,unsigned> > ,但是当我在制作凸包时使用配对点时,会产生很多错误。
这是我使用的相关代码段:

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;
}


推荐答案

您需要提供特征 ConvexHullTraits ,对类型是您的点类型。实际上,这很简单,您只需要一个包含所有函子的结构,操作员只需使用 pair :: first 将其转发到CGAL traits类函子。

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.

这是一个简单的示例:

#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;
}

这篇关于CGAL:带有信息的凸凸包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆