Merge pull request #2108 from sloriot/CH2-add_missing_requirement

Document a missing requirement in 2D convex hull traits concept

(This merge also merge releases/CGAL-4.10-branch, so that `master` stays
ahead of it.)
This commit is contained in:
Laurent Rineau 2017-05-19 10:33:24 +02:00
commit 25f5ca6d3e
3 changed files with 157 additions and 0 deletions

View File

@ -58,6 +58,11 @@ typedef R::Left_turn_2 Left_turn_2;
*/
typedef R::Equal_2 Equal_2;
/*!
*/
typedef R::Orientation_2 Orientation_2;
/// @}
/// \name Creation
@ -104,6 +109,11 @@ Left_turn_2 left_turn_2_object();
*/
Equal_2 equal_2_object();
/*!
*/
Orientation_2 orientation_2_object();
/// @}
}; /* end Convex_hull_traits_2 */

View File

@ -89,6 +89,15 @@ is smaller!
*/
typedef unspecified_type Less_rotate_ccw_2;
/*!
Predicate object type that must provide
`Orientation operator()(Point_2 e, Point_2 p,Point_2 q)`,
that returns `CGAL::LEFT_TURN`, if `r` lies to the left of the oriented line `l`
defined by `p` and `q`, returns `CGAL::RIGHT_TURN` if `r` lies to the right of `l`,
and returns `CGAL::COLLINEAR` if `r` lies on `l`.
*/
typedef unspecified_type Orientation_2;
/// @}
/// \name Creation
@ -137,6 +146,11 @@ Less_rotate_ccw_2 less_rotate_ccw_2_object( );
*/
Left_turn_2 left_turn_2_object();
/*!
*/
Orientation_2 orientation_2_object();
/// @}
}; /* end ConvexHullTraits_2 */

View File

@ -0,0 +1,133 @@
#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/iterator/counting_iterator.hpp>
#include <boost/foreach.hpp>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_2 Point_2;
template <class F, class Point>
struct Forward_bool_functor
: public F
{
const std::vector<Point>& points;
Forward_bool_functor(const std::vector<Point>& 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]);
}
};
template <class K>
struct CH_traits_for_point_ids
{
const std::vector<typename K::Point_2>& points;
CH_traits_for_point_ids(const std::vector<typename K::Point_2>& points)
: points(points)
{}
typedef std::size_t Point_2;
typedef CGAL::Convex_hull_traits_2<K> Base;
typedef Forward_bool_functor<typename Base::Less_xy_2, typename K::Point_2> Less_xy_2;
typedef Forward_bool_functor<typename Base::Less_yx_2, typename K::Point_2> Less_yx_2;
typedef Forward_bool_functor<typename Base::Less_signed_distance_to_line_2, typename K::Point_2> Less_signed_distance_to_line_2;
typedef Forward_bool_functor<typename Base::Less_rotate_ccw_2, typename K::Point_2> Less_rotate_ccw_2;
typedef Forward_bool_functor<typename Base::Left_turn_2, typename K::Point_2> Left_turn_2;
typedef Forward_bool_functor<typename Base::Equal_2, typename K::Point_2> Equal_2;
struct Orientation_2
{
const std::vector<typename K::Point_2>& points;
Orientation_2(const std::vector<typename K::Point_2>& points)
: points(points)
{}
CGAL::Orientation
operator()(Point_2 p, Point_2 q, Point_2 r) const
{
return typename 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<std::size_t> 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) );
CGAL::convex_hull_2( boost::counting_iterator<std::size_t>(0),
boost::counting_iterator<std::size_t>(input_points.size()),
std::back_inserter(result), CH_traits_for_point_ids<K>(input_points) );
BOOST_FOREACH(std::size_t i, result)
{
std::cout << input_points[i] << " - " << i << "\n";
}
assert( result.size() == 3 );
return 0;
}