1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
// Copyright (c) 2003 INRIA Sophia-Antipolis (France).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
//
// $URL: https://github.com/CGAL/cgal/blob/v6.1.1/Interpolation/include/CGAL/sibson_gradient_fitting.h $
// $Id: include/CGAL/sibson_gradient_fitting.h 08b27d3db14 $
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial
//
//
// Author(s) : Julia Floetotto
#ifndef CGAL_SIBSON_GRADIENT_FITTING_H
#define CGAL_SIBSON_GRADIENT_FITTING_H
#include <CGAL/license/Interpolation.h>
#include <CGAL/Interpolation/internal/helpers.h>
#include <CGAL/natural_neighbor_coordinates_2.h>
#include <CGAL/regular_neighbor_coordinates_2.h>
#include <CGAL/Origin.h>
#include <functional>
#include <any>
#include <boost/utility/result_of.hpp>
#include <iterator>
#include <utility>
#include <vector>
#include <type_traits>
#include <functional>
namespace CGAL {
template < class ForwardIterator, class ValueFunctor, class Traits, class Point >
typename Traits::Vector_d
sibson_gradient_fitting(ForwardIterator first,
ForwardIterator beyond,
const typename std::iterator_traits<ForwardIterator>::value_type::second_type& norm,
const Point& p,
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;
typename Traits::Vector_d pn = traits.construct_vector_d_object()(NULL_VECTOR);
Aff_transformation scaling, m, Hn(traits.construct_null_matrix_d_object()());
Interpolation::internal::Extract_bare_point<Traits> cp(traits);
const Bare_point& bp = cp(p);
for(; first!=beyond; ++first)
{
const typename Traits::Point_d& bare_f = cp(first->first);
Coord_type square_dist = traits.compute_squared_distance_d_object()(bare_f, bp);
CGAL_assertion(square_dist != 0);
Coord_type scale = first->second / (norm * square_dist);
typename Traits::Vector_d d = traits.construct_vector_d_object()(bp, bare_f);
// compute the vector pn:
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));
// compute the matrix Hn:
m = traits.construct_outer_product_d_object()(d);
scaling = traits.construct_scaling_matrix_d_object()(scale);
Hn = traits.construct_sum_matrix_d_object()(Hn, scaling * m);
}
return Hn.inverse().transform(pn);
}
// for backward compatibility
template < class ForwardIterator, class ValueFunctor, class Traits >
typename Traits::Vector_d
sibson_gradient_fitting(ForwardIterator first,
ForwardIterator beyond,
const typename std::iterator_traits<ForwardIterator>::value_type::second_type& norm,
const typename std::iterator_traits<ForwardIterator>::value_type::first_type& p,
ValueFunctor value_function,
const Traits& traits)
{
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);
}
// The next three functions are used to call the value functor for different
// types of arguments and pass a final (bare) point + value to the function above.
template < class ForwardIterator, class ValueFunctor, class Traits, class VH >
typename Traits::Vector_d
sibson_gradient_fitting_internal_with_dummy(ForwardIterator first,
ForwardIterator beyond,
const typename std::iterator_traits<
ForwardIterator>::value_type::second_type& norm,
VH vh,
ValueFunctor value_function,
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());
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);
}
template < class ForwardIterator, class ValueFunctor, class Traits, class VH >
typename Traits::Vector_d
sibson_gradient_fitting_internal_with_dummy(ForwardIterator first,
ForwardIterator beyond,
const typename std::iterator_traits<
ForwardIterator>::value_type::second_type& norm,
VH vh,
ValueFunctor value_function,
const Traits& traits,
const typename Traits::Weighted_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;
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);
}
template < class ForwardIterator, class ValueFunctor, class Traits, class VH >
typename Traits::Vector_d
sibson_gradient_fitting_internal_with_dummy(ForwardIterator first,
ForwardIterator beyond,
const typename std::iterator_traits<
ForwardIterator>::value_type::second_type& norm,
VH vh,
ValueFunctor value_function,
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());
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 ValueFunctorArgType,
class Tr, class OutputIterator, class OutputFunctor,
class ValueFunctor, class CoordFunctor, class Traits >
OutputIterator
sibson_gradient_fitting_internal(const Tr& tr,
OutputIterator out,
OutputFunctor fct,
ValueFunctor value_function,
CoordFunctor compute_coordinates,
const Traits& traits)
{
typedef typename Traits::FT Coord_type;
typedef typename CoordFunctor::Function Coord_OutputFunctor;
typedef typename Tr::Vertex_handle Vertex_handle;
Coord_type norm;
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)
{
// test if vit is a convex hull vertex, otherwise do nothing
if(!tr.is_edge(vit, tr.infinite_vertex()))
{
norm = compute_coordinates(tr, vit, std::back_inserter(coords), Coord_OutputFunctor()).second;
*out++ = fct(std::make_pair(vit,
sibson_gradient_fitting_internal_with_dummy(coords.begin(),
coords.end(),
norm,
Vertex_handle(vit),
value_function,
traits,
ValueFunctorArgType())));
coords.clear();
}
}
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
// -> _rn2: regular_neighbor_coordinates_2
// -> _sn2_3: surface_neighbor_coordinates_2_3
// The ugly distinction below is needed to make it work with lambdas for C++11 because std::is_constructible
// is used, which is C++11 (there is a boost equivalent, but it is said (by boost) to be relying on C++11 features
// to properly work...)
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,
// Some SFINAE to distinguish whether the argument type
// of the value functor is 'DT::Point' or 'DT::Vertex_handle'
std::enable_if_t<
std::is_constructible<
std::function<std::any(typename Dt::Point)>,
ValueFunctor
>::value>* = nullptr)
{
typedef typename Traits::FT FT;
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;
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,
std::enable_if_t<
std::is_constructible<
std::function<std::any(typename Dt::Vertex_handle)>,
ValueFunctor
>::value>* = nullptr)
{
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;
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,
OutputIterator out,
ValueFunctor value_function,
const Traits& traits)
{
typedef typename Traits::Vector_d Vector_d;
typedef Interpolation::internal::Extract_point_in_pair<Dt, Vector_d> OutputFunctor;
return sibson_gradient_fitting_nn_2(dt, out, OutputFunctor(), value_function, traits);
}
// See above for the explanation.
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'
std::enable_if_t<
std::is_constructible<
std::function<std::any(typename Rt::Point)>,
ValueFunctor
>::value>* = nullptr)
{
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
sibson_gradient_fitting_rn_2(const Rt& rt,
OutputIterator out,
OutputFunctor fct,
ValueFunctor value_function,
const Traits& traits,
std::enable_if_t<
std::is_constructible<
std::function<std::any(typename Rt::Vertex_handle)>,
ValueFunctor
>::value>* = nullptr)
{
typedef typename Traits::FT FT;
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;
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
sibson_gradient_fitting_rn_2(const Rt& rt,
OutputIterator out,
ValueFunctor value_function,
const Traits& traits)
{
typedef typename Traits::Vector_d Vector_d;
typedef Interpolation::internal::Extract_point_in_pair<Rt, Vector_d> OutputFunctor;
return sibson_gradient_fitting_rn_2(rt, out, OutputFunctor(), value_function, traits);
}
} //namespace CGAL
#endif // CGAL_SIBSON_GRADIENT_FITTING_H
|