我在平面上有一个2D点(N个元素)矢量。我想制作这些点的凸包。之后,我想检索凸包中每个顶点的矢量索引,我该怎么做?
我知道,利用vector<pair<Point_2, unsigned> >
进行三角测量有可能,但是当我使用成对点制作凸包时,会产生一堆错误。
这是我使用的相关代码:
#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;
}
答案 0 :(得分:2)
您需要提供ConvexHullTraits
的特征类模型,对类型是您的点类型。在实践中它很简单,你只需要一个包含所有仿函数的结构,操作符只需使用pair::first
转发到CGAL traits类仿函数。
这是一个简单的例子:
#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;
}
这是一个更复杂的例子,其中不复制点,算法使用点索引:
#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;
}