Replaced uses of ::result_type and ::argument_type in Interpolation

This allows to pass more generic functors such as lambdas, for example.
This commit is contained in:
Mael Rouxel-Labbé 2018-06-15 12:53:46 +02:00
parent 77ab3dbd00
commit c76dace89b
2 changed files with 176 additions and 73 deletions

View File

@ -27,6 +27,8 @@
#include <CGAL/double.h>
#include <CGAL/use.h>
#include <boost/utility/result_of.hpp>
#include <iterator>
#include <utility>
#include <vector>
@ -58,17 +60,21 @@ struct Data_access
//the interpolation functions:
template < class ForwardIterator, class ValueFunctor >
typename ValueFunctor::result_type::first_type
typename boost::result_of<
ValueFunctor(typename std::iterator_traits<ForwardIterator>::value_type::first_type)>
::type::first_type
linear_interpolation(ForwardIterator first, ForwardIterator beyond,
const typename std::iterator_traits<ForwardIterator>::value_type::second_type& norm,
ValueFunctor value_function)
{
CGAL_precondition(norm > 0);
typedef typename ValueFunctor::result_type::first_type Value_type;
typedef typename std::iterator_traits<ForwardIterator>::value_type::first_type arg_type;
typedef typename boost::result_of<ValueFunctor(arg_type)>::type result_type;
typedef typename result_type::first_type Value_type;
Value_type result(0);
typename ValueFunctor::result_type val;
result_type val;
for(; first!=beyond; ++first)
{
val = value_function(first->first);
@ -78,9 +84,12 @@ linear_interpolation(ForwardIterator first, ForwardIterator beyond,
return result;
}
template < class ForwardIterator, class ValueFunctor, class GradFunctor, class Traits, class Point >
std::pair< typename ValueFunctor::result_type::first_type, bool>
std::pair<
typename boost::result_of<
ValueFunctor(typename std::iterator_traits<ForwardIterator>::value_type::first_type)>
::type::first_type,
bool>
quadratic_interpolation(ForwardIterator first, ForwardIterator beyond,
const typename std::iterator_traits<ForwardIterator>::value_type::second_type& norm,
const Point& p,
@ -89,15 +98,20 @@ quadratic_interpolation(ForwardIterator first, ForwardIterator beyond,
const Traits& traits)
{
CGAL_precondition(norm > 0);
typedef typename ValueFunctor::result_type::first_type Value_type;
typedef typename std::iterator_traits<ForwardIterator>::value_type::first_type arg_type;
typedef typename boost::result_of<ValueFunctor(arg_type)>::type value_functor_result_type;
typedef typename boost::result_of<GradFunctor(arg_type)>::type gradient_functor_result_type;
typedef typename value_functor_result_type::first_type Value_type;
typedef typename Traits::Point_d Bare_point;
Interpolation::internal::Extract_bare_point<Traits> cp(traits);
const Bare_point& bp = cp(p);
Value_type result(0);
typename ValueFunctor::result_type f;
typename GradFunctor::result_type grad;
value_functor_result_type f;
gradient_functor_result_type grad;
for(; first!=beyond; ++first)
{
@ -119,7 +133,11 @@ quadratic_interpolation(ForwardIterator first, ForwardIterator beyond,
template < class ForwardIterator, class ValueFunctor, class GradFunctor, class Traits, class Point >
std::pair< typename ValueFunctor::result_type::first_type, bool >
std::pair<
typename boost::result_of<
ValueFunctor(typename std::iterator_traits<ForwardIterator>::value_type::first_type)>
::type::first_type,
bool>
sibson_c1_interpolation(ForwardIterator first, ForwardIterator beyond,
const typename std::iterator_traits<ForwardIterator>::value_type::second_type& norm,
const Point& p,
@ -129,7 +147,11 @@ sibson_c1_interpolation(ForwardIterator first, ForwardIterator beyond,
{
CGAL_precondition(norm >0);
typedef typename ValueFunctor::result_type::first_type Value_type;
typedef typename std::iterator_traits<ForwardIterator>::value_type::first_type arg_type;
typedef typename boost::result_of<ValueFunctor(arg_type)>::type value_functor_result_type;
typedef typename boost::result_of<GradFunctor(arg_type)>::type gradient_functor_result_type;
typedef typename value_functor_result_type::first_type Value_type;
typedef typename Traits::FT Coord_type;
typedef typename Traits::Point_d Bare_point;
@ -138,8 +160,8 @@ sibson_c1_interpolation(ForwardIterator first, ForwardIterator beyond,
Coord_type term1(0), term2(term1), term3(term1), term4(term1);
Value_type linear_int(0), gradient_int(0);
typename ValueFunctor::result_type f;
typename GradFunctor::result_type grad;
value_functor_result_type f;
gradient_functor_result_type grad;
for(; first!=beyond; ++first)
{
@ -193,7 +215,11 @@ sibson_c1_interpolation(ForwardIterator first, ForwardIterator beyond,
// gradient_int += (coeff/inv_weight) * (vh->get_value()+ vh->get_gradient() * (p - vh->point()));
template < class ForwardIterator, class ValueFunctor, class GradFunctor, class Traits, class Point >
std::pair< typename ValueFunctor::result_type::first_type, bool >
std::pair<
typename boost::result_of<
ValueFunctor(typename std::iterator_traits<ForwardIterator>::value_type::first_type)>
::type::first_type,
bool>
sibson_c1_interpolation_square(ForwardIterator first, ForwardIterator beyond,
const typename std::iterator_traits<ForwardIterator>::value_type::second_type& norm,
const Point& p,
@ -203,7 +229,11 @@ sibson_c1_interpolation_square(ForwardIterator first, ForwardIterator beyond,
{
CGAL_precondition(norm > 0);
typedef typename ValueFunctor::result_type::first_type Value_type;
typedef typename std::iterator_traits<ForwardIterator>::value_type::first_type arg_type;
typedef typename boost::result_of<ValueFunctor(arg_type)>::type value_functor_result_type;
typedef typename boost::result_of<GradFunctor(arg_type)>::type gradient_functor_result_type;
typedef typename value_functor_result_type::first_type Value_type;
typedef typename Traits::FT Coord_type;
typedef typename Traits::Point_d Bare_point;
@ -212,8 +242,8 @@ sibson_c1_interpolation_square(ForwardIterator first, ForwardIterator beyond,
Coord_type term1(0), term2(term1), term3(term1), term4(term1);
Value_type linear_int(0), gradient_int(0);
typename ValueFunctor::result_type f;
typename GradFunctor::result_type grad;
value_functor_result_type f;
gradient_functor_result_type grad;
for(; first!=beyond; ++first)
{
@ -257,7 +287,11 @@ sibson_c1_interpolation_square(ForwardIterator first, ForwardIterator beyond,
template < class RandomAccessIterator, class ValueFunctor, class GradFunctor,
class Traits, class Point_>
std::pair< typename ValueFunctor::result_type::first_type, bool>
std::pair<
typename boost::result_of<
ValueFunctor(typename std::iterator_traits<RandomAccessIterator>::value_type::first_type)>
::type::first_type,
bool>
farin_c1_interpolation(RandomAccessIterator first,
RandomAccessIterator beyond,
const typename std::iterator_traits<RandomAccessIterator>::value_type::second_type& norm,
@ -268,14 +302,16 @@ farin_c1_interpolation(RandomAccessIterator first,
{
CGAL_precondition(norm >0);
// the function value is available for all points
// if a gradient value is not availble: function returns false
typedef typename ValueFunctor::result_type::first_type Value_type;
typedef typename std::iterator_traits<RandomAccessIterator>::value_type::first_type arg_type;
typedef typename boost::result_of<ValueFunctor(arg_type)>::type value_functor_result_type;
typedef typename boost::result_of<GradFunctor(arg_type)>::type gradient_functor_result_type;
typedef typename value_functor_result_type::first_type Value_type;
typedef typename Traits::FT Coord_type;
Interpolation::internal::Extract_bare_point<Traits> cp(traits);
typename ValueFunctor::result_type f;
typename GradFunctor::result_type grad;
value_functor_result_type f;
gradient_functor_result_type grad;
int n = static_cast<int>(beyond - first);
if(n == 1)

View File

@ -29,9 +29,11 @@
#include <CGAL/regular_neighbor_coordinates_2.h>
#include <CGAL/Origin.h>
#include <CGAL/function.h>
#include <boost/mpl/if.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/type_traits/is_constructible.hpp>
#include <boost/any.hpp>
#include <iterator>
#include <utility>
@ -45,12 +47,17 @@ sibson_gradient_fitting(ForwardIterator first,
ForwardIterator beyond,
const typename std::iterator_traits<ForwardIterator>::value_type::second_type& norm,
const Point& p,
const typename ValueFunctor::result_type::first_type fn,
const typename boost::result_of<
ValueFunctor(typename std::iterator_traits<ForwardIterator>::value_type::first_type)>
::type::first_type fn,
ValueFunctor value_function,
const Traits& traits)
{
CGAL_precondition( first != beyond && norm != 0);
typedef typename std::iterator_traits<ForwardIterator>::value_type::first_type arg_type;
typedef typename boost::result_of<ValueFunctor(arg_type)>::type value_functor_result_type;
typedef typename Traits::Aff_transformation_d Aff_transformation;
typedef typename Traits::FT Coord_type;
typedef typename Traits::Point_d Bare_point;
@ -69,7 +76,7 @@ sibson_gradient_fitting(ForwardIterator first,
typename Traits::Vector_d d = traits.construct_vector_d_object()(bp, bare_f);
// compute the vector pn:
typename ValueFunctor::result_type f = value_function(first->first);
value_functor_result_type f = value_function(first->first);
CGAL_assertion(f.second); // function value of first->first is valid
pn = pn + traits.construct_scaled_vector_d_object()(d, scale * (f.first - fn));
@ -91,7 +98,10 @@ sibson_gradient_fitting(ForwardIterator first,
ValueFunctor value_function,
const Traits& traits)
{
typename ValueFunctor::result_type fn = value_function(p);
typedef typename std::iterator_traits<ForwardIterator>::value_type::first_type arg_type;
typedef typename boost::result_of<ValueFunctor(arg_type)>::type value_functor_result_type;
value_functor_result_type fn = value_function(p);
CGAL_assertion(fn.second);
return sibson_gradient_fitting(first, beyond, norm, p, fn.first, value_function, traits);
@ -110,8 +120,11 @@ sibson_gradient_fitting_internal(ForwardIterator first,
const Traits& traits,
const typename Traits::Point_d& /*dummy*/)
{
typedef typename std::iterator_traits<ForwardIterator>::value_type::first_type arg_type;
typedef typename boost::result_of<ValueFunctor(arg_type)>::type value_functor_result_type;
const typename Traits::Point_d& bare_p = traits.construct_point_d_object()(vh->point());
typename ValueFunctor::result_type fn = value_function(bare_p);
value_functor_result_type fn = value_function(bare_p);
CGAL_assertion(fn.second);
return sibson_gradient_fitting(first, beyond, norm, bare_p, fn.first, value_function, traits);
@ -129,7 +142,10 @@ sibson_gradient_fitting_internal(ForwardIterator first,
const Traits& traits,
const typename Traits::Weighted_point_d& /*dummy*/)
{
typename ValueFunctor::result_type fn = value_function(vh->point());
typedef typename std::iterator_traits<ForwardIterator>::value_type::first_type arg_type;
typedef typename boost::result_of<ValueFunctor(arg_type)>::type value_functor_result_type;
value_functor_result_type fn = value_function(vh->point());
CGAL_assertion(fn.second);
return sibson_gradient_fitting(first, beyond, norm, vh->point(), fn.first, value_function, traits);
@ -147,15 +163,19 @@ sibson_gradient_fitting_internal(ForwardIterator first,
const Traits& traits,
VH /*dummy*/)
{
typedef typename std::iterator_traits<ForwardIterator>::value_type::first_type arg_type;
typedef typename boost::result_of<ValueFunctor(arg_type)>::type value_functor_result_type;
const typename Traits::Point_d& bare_p = traits.construct_point_d_object()(vh->point());
typename ValueFunctor::result_type fn = value_function(vh);
value_functor_result_type fn = value_function(vh);
CGAL_assertion(fn.second);
return sibson_gradient_fitting(first, beyond, norm, bare_p, fn.first, value_function, traits);
}
template < class Tr, class OutputIterator, class OutputFunctor,
template < class ValueFunctorArgType,
class Tr, class OutputIterator, class OutputFunctor,
class ValueFunctor, class CoordFunctor, class Traits >
OutputIterator
sibson_gradient_fitting_internal(const Tr& tr,
@ -170,7 +190,7 @@ sibson_gradient_fitting_internal(const Tr& tr,
typedef typename Tr::Vertex_handle Vertex_handle;
Coord_type norm;
std::vector<std::pair<typename ValueFunctor::argument_type, Coord_type> > coords;
std::vector<std::pair<ValueFunctorArgType, Coord_type> > coords;
typename Tr::Finite_vertices_iterator vit = tr.finite_vertices_begin();
for(; vit != tr.finite_vertices_end(); ++vit)
@ -187,7 +207,7 @@ sibson_gradient_fitting_internal(const Tr& tr,
Vertex_handle(vit),
value_function,
traits,
typename ValueFunctor::argument_type())));
ValueFunctorArgType())));
coords.clear();
}
@ -196,7 +216,6 @@ sibson_gradient_fitting_internal(const Tr& tr,
return out;
}
// The following functions allow to fit the gradients for all points in
// a triangulation except the convex hull points.
// -> _nn2: natural_neighbor_coordinates_2
@ -208,31 +227,56 @@ sibson_gradient_fitting_nn_2(const Dt& dt,
OutputIterator out,
OutputFunctor fct,
ValueFunctor value_function,
const Traits& traits)
const Traits& traits,
// Some SFINAE to distinguish whether the argument type
// of the value functor is 'DT::Point' or 'DT::Vertex_handle'
typename boost::enable_if_c<
boost::is_constructible<
cpp11::function<boost::any(typename Dt::Point)>,
ValueFunctor
>::value>::type* = NULL)
{
typedef typename Traits::FT FT;
typedef typename ValueFunctor::argument_type VF_arg_type;
typedef typename Dt::Point VF_arg_type;
typedef typename std::back_insert_iterator<std::vector<
std::pair<VF_arg_type, FT> > > CoordInserter;
typedef Interpolation::internal::Extract_point_in_pair<Dt, FT> Coord_OutputFunctor;
// If the functor evaluates at points (and not vertices), then we must convert
// the output of the coordinates computations - a pair<vertex_handle, FT> -
// to a pair<point, FT>
typedef typename boost::mpl::if_<
boost::is_same<VF_arg_type, typename Dt::Point>,
Interpolation::internal::Extract_point_in_pair<Dt, FT>,
CGAL::Identity<std::pair<VF_arg_type, FT> >
>::type Coord_OutputFunctor;
return sibson_gradient_fitting_internal(dt, out, fct, value_function,
return sibson_gradient_fitting_internal<VF_arg_type>(dt, out, fct, value_function,
natural_neighbor_coordinates_2_object<Dt,
CoordInserter,
Coord_OutputFunctor>(),
traits);
}
template < class Dt, class OutputIterator, class OutputFunctor, class ValueFunctor, class Traits >
OutputIterator
sibson_gradient_fitting_nn_2(const Dt& dt,
OutputIterator out,
OutputFunctor fct,
ValueFunctor value_function,
const Traits& traits,
typename boost::enable_if_c<
boost::is_constructible<
cpp11::function<boost::any(typename Dt::Vertex_handle)>,
ValueFunctor
>::value>::type* = NULL)
{
typedef typename Traits::FT FT;
typedef typename Dt::Vertex_handle VF_arg_type;
typedef typename std::back_insert_iterator<std::vector<
std::pair<VF_arg_type, FT> > > CoordInserter;
typedef CGAL::Identity<std::pair<VF_arg_type, FT> > Coord_OutputFunctor;
// Same as above but without OutputFunctor. Default to extracting the point, for backward compatibility.
return sibson_gradient_fitting_internal<VF_arg_type>(dt, out, fct, value_function,
natural_neighbor_coordinates_2_object<Dt,
CoordInserter,
Coord_OutputFunctor>(),
traits);
}
// Same as above but without OutputFunctor.
// Defaults to extracting the point, for backward compatibility.
template < class Dt, class OutputIterator, class ValueFunctor, class Traits >
OutputIterator
sibson_gradient_fitting_nn_2(const Dt& dt,
@ -246,6 +290,33 @@ sibson_gradient_fitting_nn_2(const Dt& dt,
return sibson_gradient_fitting_nn_2(dt, out, OutputFunctor(), value_function, traits);
}
template < class Rt, class OutputIterator, class OutputFunctor, class ValueFunctor, class Traits >
OutputIterator
sibson_gradient_fitting_rn_2(const Rt& rt,
OutputIterator out,
OutputFunctor fct,
ValueFunctor value_function,
const Traits& traits,
// Some SFINAE to distinguish whether the argument type
// of the value functor is 'Rt::Point' (weighted point) or 'Rt::Vertex_handle'
typename boost::enable_if_c<
boost::is_constructible<
cpp11::function<boost::any(typename Rt::Point)>,
ValueFunctor
>::value>::type* = NULL)
{
typedef typename Traits::FT FT;
typedef typename Rt::Point VF_arg_type;
typedef typename std::back_insert_iterator<std::vector<
std::pair<VF_arg_type, FT> > > CoordInserter;
typedef Interpolation::internal::Extract_point_in_pair<Rt, FT> Coord_OutputFunctor;
return sibson_gradient_fitting_internal<VF_arg_type>(rt, out, fct, value_function,
regular_neighbor_coordinates_2_object<Rt,
CoordInserter,
Coord_OutputFunctor>(),
traits);
}
template < class Rt, class OutputIterator, class OutputFunctor, class ValueFunctor, class Traits >
OutputIterator
@ -253,30 +324,26 @@ sibson_gradient_fitting_rn_2(const Rt& rt,
OutputIterator out,
OutputFunctor fct,
ValueFunctor value_function,
const Traits& traits)
const Traits& traits,
typename boost::enable_if_c<
boost::is_constructible<
cpp11::function<boost::any(typename Rt::Vertex_handle)>,
ValueFunctor
>::value>::type* = NULL)
{
typedef typename Traits::FT FT;
typedef typename ValueFunctor::argument_type VF_arg_type;
typedef typename Rt::Vertex_handle VF_arg_type;
typedef typename std::back_insert_iterator<std::vector<
std::pair<VF_arg_type, FT> > > CoordInserter;
typedef CGAL::Identity<std::pair<VF_arg_type, FT> > Coord_OutputFunctor;
// If the functor evaluates at points (and not vertices), then we must convert
// the output of the coordinates computations - a pair<vertex_handle, FT> -
// to a pair<point, FT>
typedef typename boost::mpl::if_<
boost::is_same<VF_arg_type, typename Rt::Weighted_point>,
Interpolation::internal::Extract_point_in_pair<Rt, FT>,
CGAL::Identity<std::pair<VF_arg_type, FT> >
>::type Coord_OutputFunctor;
return sibson_gradient_fitting_internal(rt, out, fct, value_function,
return sibson_gradient_fitting_internal<VF_arg_type>(rt, out, fct, value_function,
regular_neighbor_coordinates_2_object<Rt,
CoordInserter,
Coord_OutputFunctor>(),
traits);
}
// Same as above but without OutputFunctor. Default to extracting the point, for backward compatibility.
template < class Rt, class OutputIterator, class ValueFunctor, class Traits >
OutputIterator