Tatooine
natural_neighbor_coordinates_sampler_with_gradients.h
Go to the documentation of this file.
1#if TATOOINE_CGAL_AVAILABLE
2#ifndef TATOOINE_DETAIL_POINTSET_NATURAL_NEIGHBOR_COORDINATES_SAMPLER_WITH_GRADIENTS_H
3#define TATOOINE_DETAIL_POINTSET_NATURAL_NEIGHBOR_COORDINATES_SAMPLER_WITH_GRADIENTS_H
4//==============================================================================
5#include <CGAL/natural_neighbor_coordinates_2.h>
6#include <CGAL/natural_neighbor_coordinates_3.h>
7#include <tatooine/cgal.h>
8#include <tatooine/concepts.h>
10//==============================================================================
12//==============================================================================
13template <floating_point Real, std::size_t NumDimensions, typename ValueType,
14 typename Gradient>
15// requires(tensor_rank<ValueType> + 1 == tensor_rank<Gradient>)
17 : field<natural_neighbor_coordinates_sampler_with_gradients<
18 Real, NumDimensions, ValueType, Gradient>,
19 Real, NumDimensions, ValueType> {
20 using this_type =
22 ValueType, Gradient>;
24 using typename parent_type::pos_type;
25 using typename parent_type::real_type;
26 using typename parent_type::tensor_type;
30 typename pointset_type::template typed_vertex_property_type<ValueType>;
32 typename pointset_type::template typed_vertex_property_type<Gradient>;
33
34 using cgal_kernel = CGAL::Exact_predicates_inexact_constructions_kernel;
38 using cgal_point = typename cgal_triangulation_type::Point;
39
40 //==========================================================================
45 //==========================================================================
47 pointset_type const& ps, vertex_property_type const& property,
48 gradient_vertex_property_type const& gradients_property)
49 : m_p{ps}, m_z{property}, m_g{gradients_property} {
50 auto points = std::vector<std::pair<cgal_point, vertex_handle>>{};
51 points.reserve(ps.vertices().size());
52 for (auto v : ps.vertices()) {
53 auto const& p = ps[v];
54 if (!p.isnan()) {
55 points.emplace_back(cgal_point{ps[v](0), ps[v](1)}, v);
56 }
57 }
58
60 }
61 //--------------------------------------------------------------------------
64 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
67 //--------------------------------------------------------------------------
70 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
73 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
75 //==========================================================================
76 private:
77 template <std::size_t... Is>
78 [[nodiscard]] auto evaluate(pos_type const& x,
79 std::index_sequence<Is...> /*seq*/) const
80 -> tensor_type {
81 using nnc_per_vertex_type =
82 std::vector<std::pair<typename cgal_triangulation_type::Vertex_handle,
83 cgal_kernel::FT>>;
84
85 // coordinates computation
86 auto nnc_per_vertex = nnc_per_vertex_type{};
87 auto const result = CGAL::natural_neighbor_coordinates_2(
88 m_triangulation, cgal_point{x(Is)...},
89 std::back_inserter(nnc_per_vertex),
90 CGAL::Identity<
91 std::pair<typename cgal_triangulation_type::Vertex_handle,
92 cgal_kernel::FT>>{});
93 if (!result.third) {
95 }
96 auto const norm = 1 / result.second;
97
98 auto Z0 = [&] {
99 auto sum = ValueType{};
100 for (auto const& [cgal_handle, coeff] : nnc_per_vertex) {
101 auto const v = cgal_handle->info();
102 auto const lambda_i = coeff * norm;
103 sum += lambda_i * m_z[v];
104 }
105 return sum;
106 }();
107
108 auto xi = [&] {
109 auto numerator = ValueType{};
110 auto denominator = real_type{};
111 for (auto const& [cgal_handle, coeff] : nnc_per_vertex) {
112 auto const v = cgal_handle->info();
113 auto const lambda_i = coeff * norm;
114 auto const& p_i = m_p[v];
115 auto const& g_i = m_g[v];
116 auto const xi_i = [&] {
117 if constexpr (tensor_rank<ValueType> == 0) {
118 return m_z[v] + dot(g_i, x - p_i);
119 } else {
120 return m_z[v] + transposed(g_i) * (x - p_i);
121 }
122 }();
123 auto const w = lambda_i / euclidean_distance(x, p_i);
124 numerator += w * xi_i;
125 denominator += w;
126 }
127 return numerator / denominator;
128 }();
129
130 auto alpha = [&] {
131 auto numerator = ValueType{};
132 auto denominator = real_type{};
133 for (auto const& [cgal_handle, coeff] : nnc_per_vertex) {
134 auto const v = cgal_handle->info();
135 auto const lambda_i = coeff * norm;
136 auto const& p_i = m_p[v];
137 auto const w = lambda_i / squared_euclidean_distance(x, p_i);
138 numerator += lambda_i;
139 denominator += w;
140 }
141 return numerator / denominator;
142 }();
143
144 auto beta = [&] {
145 auto sum = real_type{};
146 for (auto const& [cgal_handle, coeff] : nnc_per_vertex) {
147 auto const v = cgal_handle->info();
148 auto const lambda_i = coeff * norm;
149 auto const& p_i = m_p[v];
150 sum += lambda_i * squared_euclidean_distance(x, p_i);
151 }
152 return sum;
153 }();
154
155 return (alpha * Z0 + beta * xi) / (alpha + beta);
156 }
157 //----------------------------------------------------------------------------
158 public:
159 [[nodiscard]] auto evaluate(pos_type const& x, real_type const /*t*/) const
160 -> tensor_type {
161 return evaluate(x, std::make_index_sequence<NumDimensions>{});
162 }
163};
164//==============================================================================
165} // namespace tatooine::detail::pointset
166//==============================================================================
167#endif
168#endif
delaunay_triangulation< NumDimensions, Traits, triangulation_data_structure< NumDimensions, Traits, triangulation_vertex_base_with_info< NumDimensions, Info, Traits >, SimplexBase > > delaunay_triangulation_with_info
Definition: delaunay_triangulation.h:50
Definition: inverse_distance_weighting_sampler.h:4
auto begin(Range &&range)
Definition: iterator_facade.h:318
auto end(Range &&range)
Definition: iterator_facade.h:322
constexpr auto norm(base_tensor< Tensor, T, N > const &t, unsigned p=2) -> T
Definition: norm.h:23
constexpr auto dot(base_tensor< Tensor0, T0, N > const &lhs, base_tensor< Tensor1, T1, N > const &rhs)
Definition: tensor_operations.h:120
auto transposed(dynamic_tensor auto &&t)
Definition: transposed_tensor.h:170
constexpr auto squared_euclidean_distance(base_tensor< Tensor0, T0, N > const &lhs, base_tensor< Tensor1, T1, N > const &rhs)
Definition: distance.h:11
constexpr auto sum(base_tensor< Tensor, T, VecDim > const &v)
sum of all components of a vector
Definition: tensor_operations.h:110
constexpr auto euclidean_distance(base_tensor< Tensor0, T0, N > const &lhs, base_tensor< Tensor1, T1, N > const &rhs)
Definition: distance.h:19
Definition: natural_neighbor_coordinates_sampler_with_gradients.h:19
gradient_vertex_property_type const & m_g
Definition: natural_neighbor_coordinates_sampler_with_gradients.h:43
vertex_property_type const & m_z
Definition: natural_neighbor_coordinates_sampler_with_gradients.h:42
auto evaluate(pos_type const &x, real_type const) const -> tensor_type
Definition: natural_neighbor_coordinates_sampler_with_gradients.h:159
cgal_triangulation_type m_triangulation
Definition: natural_neighbor_coordinates_sampler_with_gradients.h:44
auto evaluate(pos_type const &x, std::index_sequence< Is... >) const -> tensor_type
Definition: natural_neighbor_coordinates_sampler_with_gradients.h:78
typename pointset_type::template typed_vertex_property_type< ValueType > vertex_property_type
Definition: natural_neighbor_coordinates_sampler_with_gradients.h:30
natural_neighbor_coordinates_sampler_with_gradients(natural_neighbor_coordinates_sampler_with_gradients &&) noexcept=default
natural_neighbor_coordinates_sampler_with_gradients(natural_neighbor_coordinates_sampler_with_gradients const &)=default
typename pointset_type::vertex_handle vertex_handle
Definition: natural_neighbor_coordinates_sampler_with_gradients.h:28
pointset_type const & m_p
Definition: natural_neighbor_coordinates_sampler_with_gradients.h:41
typename cgal_triangulation_type::Point cgal_point
Definition: natural_neighbor_coordinates_sampler_with_gradients.h:38
typename pointset_type::template typed_vertex_property_type< Gradient > gradient_vertex_property_type
Definition: natural_neighbor_coordinates_sampler_with_gradients.h:32
CGAL::Exact_predicates_inexact_constructions_kernel cgal_kernel
Definition: natural_neighbor_coordinates_sampler_with_gradients.h:34
cgal::delaunay_triangulation_with_info< NumDimensions, cgal_kernel, vertex_handle > cgal_triangulation_type
Definition: natural_neighbor_coordinates_sampler_with_gradients.h:37
natural_neighbor_coordinates_sampler_with_gradients(pointset_type const &ps, vertex_property_type const &property, gradient_vertex_property_type const &gradients_property)
Definition: natural_neighbor_coordinates_sampler_with_gradients.h:46
Definition: field.h:134
Real real_type
Definition: field.h:17
vec< real_type, NumDimensions > pos_type
Definition: field.h:20
Tensor tensor_type
Definition: field.h:18
Definition: pointset.h:83
Definition: pointset.h:69
auto vertices() const
Definition: pointset.h:226
static auto constexpr ood_tensor()
Definition: field.h:21
Definition: vec.h:12