Ponca  292d8d7132745e0d4242ff9a01e93697fc96b204
Point Cloud Analysis library
Loading...
Searching...
No Matches
hrbf.h
1/* This Source Code Form is subject to the terms of the Mozilla Public License,
2 * v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/.
4 */
13
14#pragma once
15
16#include "../defines.h"
17#include "../concepts.h"
18#include "../compute.h"
19#include "../Filters/weightKernel.h"
20#include "../Filters/weightFilter.h"
21
22#include <Eigen/Dense>
23
24#define HRBF_REQUIREMENTS ProvidesNormal<P>
25
26namespace Ponca
27{
28
38 template <class P, typename _WeightKernel = Pow3WeightKernel<typename P::Scalar>>
39 requires HRBF_REQUIREMENTS
40 class HRBF : ComputeObject<HRBF<P, _WeightKernel>>
41 {
42 public:
43 using DataPoint = P;
44 using Scalar = typename DataPoint::Scalar;
45 using MatrixType = typename DataPoint::MatrixType;
46 using VectorType = typename DataPoint::VectorType;
47 using MatrixDX = typename Eigen::Matrix<Scalar, DataPoint::Dim, Eigen::Dynamic>;
48 using DenseVector = Eigen::Vector<Scalar, Eigen::Dynamic>;
49 using DenseMatrix = Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>;
50 using WeightKernel = _WeightKernel;
51
52 static constexpr Scalar eps = Eigen::NumTraits<Scalar>::epsilon();
53
54 protected:
58 DenseVector m_alphas;
60 MatrixDX m_betas;
61
62 FIT_RESULT m_eCurrentState{UNDEFINED};
63
64 public:
66 inline void init()
67 {
68 m_eCurrentState = UNDEFINED;
69
70 // Clear matrices to make sure they can't be reused
71 m_node_centers.resize(DataPoint::Dim, 0);
72 m_alphas.resize(0);
73 m_betas.resize(DataPoint::Dim, 0);
74 }
75
81 template <typename IteratorBegin, typename IteratorEnd>
82 inline FIT_RESULT compute(const IteratorBegin& begin, const IteratorEnd& end)
83 {
84 constexpr int DIM = DataPoint::Dim;
85
86 const int nb_points = std::distance(begin, end);
87 const int nb_hrbf_constraints = (DIM + 1) * nb_points;
88 const int nb_constraints = nb_hrbf_constraints;
89 const int nb_coeffs = (DIM + 1) * nb_points;
90 const WeightKernel wk;
91
92 m_node_centers.resize(DIM, nb_points);
93 m_betas.resize(DIM, nb_points);
94 m_alphas.resize(nb_points);
95
96 // Assemble the "design" and "value" matrix and vector
97 DenseMatrix D(nb_constraints, nb_coeffs);
98 DenseVector f(nb_constraints);
99
100 // copy the node centers
101 auto it = begin;
102 for (int i = 0; i < nb_points; ++i, ++it)
103 m_node_centers.col(i) = (*it).pos();
104
105 it = begin;
106 for (int i = 0; i < nb_points; ++i, ++it)
107 {
108 const auto& p = (*it).pos();
109 const auto& n = (*it).normal();
110
111 const int io = (DIM + 1) * i;
112 f(io) = 0;
113 f.template segment<DIM>(io + 1) = n;
114
115 for (int j = 0; j < nb_points; ++j)
116 {
117 const int jo = (DIM + 1) * j;
118 const VectorType diff = p - m_node_centers.col(j);
119 const Scalar l = diff.norm();
120
121 if (l < eps)
122 {
123 D.template block<DIM + 1, DIM + 1>(io, jo).setZero();
124 }
125 else
126 {
127 const Scalar w = wk.f(l);
128 const Scalar dw_l = wk.df(l) / l;
129 const Scalar ddw = wk.ddf(l);
130 const VectorType g = diff * dw_l;
131
132 D(io, jo) = w;
133 D.row(io).template segment<DIM>(jo + 1) = g.transpose();
134 D.col(jo).template segment<DIM>(io + 1) = g;
135 D.template block<DIM, DIM>(io + 1, jo + 1) = (ddw - dw_l) / (l * l) * (diff * diff.transpose());
136 D.template block<DIM, DIM>(io + 1, jo + 1).diagonal().array() += dw_l;
137 }
138 }
139 }
140
141 const DenseVector x = D.lu().solve(f);
142 Eigen::Map<const Eigen::Matrix<Scalar, DIM + 1, Eigen::Dynamic>> mx(x.data(), DIM + 1, nb_points);
143
144 m_alphas = mx.row(0);
145 m_betas = mx.template bottomRows<DIM>();
146
147 // Catches cases where the system is ill-conditionned (duplicate / 'colinear' points)
148 if (!x.allFinite() || (D * x - f).norm() > std::sqrt(eps) * f.norm())
149 return m_eCurrentState = UNSTABLE;
150
151 return m_eCurrentState = STABLE;
152 }
153
159 template <typename IndexRange, typename PointContainer>
160 inline FIT_RESULT computeWithIds(const IndexRange& ids, const PointContainer& points)
161 {
162 std::vector<DataPoint> adapter;
163
164 auto begin = points.begin();
165 for (const auto& idx : ids)
166 {
167 auto current = std::next(begin, idx);
168 adapter.push_back(*current);
169 }
170
171 return compute(adapter.begin(), adapter.end());
172 }
173
174 [[nodiscard]] inline Scalar potential(const VectorType& x) const
175 {
176 const int nb_nodes = m_node_centers.cols();
177 const WeightKernel wk;
178
179 Scalar ret = 0;
180 for (int i = 0; i < nb_nodes; ++i)
181 {
182 const VectorType diff = x - m_node_centers.col(i);
183 const Scalar l = diff.norm();
184
185 if (l > eps)
186 {
187 ret += m_alphas(i) * wk.f(l);
188 ret += m_betas.col(i).dot(diff) * wk.df(l) / l;
189 }
190 }
191 return ret;
192 }
193
194 [[nodiscard]] inline VectorType primitiveGradient(const VectorType& x) const
195 {
196 const int nb_nodes = m_node_centers.cols();
197 const WeightKernel wk;
198
199 VectorType grad = VectorType::Zero();
200 for (int i = 0; i < nb_nodes; i++)
201 {
202 const VectorType node = m_node_centers.col(i);
203 const VectorType beta = m_betas.col(i);
204 const VectorType diff = x - node;
205 const Scalar alpha = m_alphas(i);
206 const Scalar l = diff.norm();
207
208 if (l > eps)
209 {
210 const VectorType diffNormalized = diff / l;
211
212 const Scalar dphi = wk.df(l);
213 const Scalar ddphi = wk.ddf(l);
214
215 const Scalar alpha_dphi = alpha * dphi;
216
217 const Scalar bDotd_l = beta.dot(diff) / l;
218 const Scalar squared_l = diff.squaredNorm();
219
220 grad += alpha_dphi * diffNormalized;
221 grad += bDotd_l * (ddphi * diffNormalized - diff * dphi / squared_l) + beta * dphi / l;
222 }
223 }
224 return grad;
225 }
226
228 PONCA_MULTIARCH [[nodiscard]] inline bool isStable() const { return m_eCurrentState == STABLE; }
229
232
233 PONCA_FITTING_APIDOC_SETWFUNC
234 PONCA_MULTIARCH inline bool setNeighborFilter(const NeighborFilter&) { return false; }
235
236 PONCA_MULTIARCH inline const VectorType& project(const VectorType& x) const { return x; }
237
238 PONCA_FITTING_IS_SIGNED(true)
239 }; // class HRBF
240
241} // namespace Ponca
242
Weight neighbors according to the Euclidean distance between a query and a reference position.
Hermite Radial Basis Function (HRBF) implicit surface interpolation.
Definition hrbf.h:41
DistWeightFilter< DataPoint, ConstantWeightKernel< Scalar > > NeighborFilter
writing implementations of function that are required to use HRBF fitting alongside Basket)
Definition hrbf.h:231
MatrixDX m_betas
Each column represents beta_i: VectorX bi = _betas.col(i);.
Definition hrbf.h:60
void init()
Set the scalar field values to 0 and reset the isNormalized() status.
Definition hrbf.h:66
DenseVector m_alphas
Vector of scalar values alpha.
Definition hrbf.h:58
FIT_RESULT compute(const IteratorBegin &begin, const IteratorEnd &end)
Convenience function for STL-like iterators Add neighbors stored in a container using STL-like iterat...
Definition hrbf.h:82
FIT_RESULT computeWithIds(const IndexRange &ids, const PointContainer &points)
Compute function that iterates over a subset of sampled points from an STL-Like container.
Definition hrbf.h:160
MatrixDX m_node_centers
Each column represents p_i: VectorX pi = _node_centers.col(i);.
Definition hrbf.h:56
bool isStable() const
Is the fitted primitive ready to use (finalize has been called and the result is stable).
Definition hrbf.h:228
This Source Code Form is subject to the terms of the Mozilla Public License, v.
Definition concepts.h:11
FIT_RESULT
Enum corresponding to the state of a fitting method (and what the finalize function returns).
Definition enums.h:15
@ UNDEFINED
The fitting is undefined, you can't use it for valid results.
Definition enums.h:22
@ STABLE
The fitting is stable and ready to use.
Definition enums.h:17
@ UNSTABLE
The fitting is ready to use but it is considered as unstable (if the number of neighbors is low for e...
Definition enums.h:20
ComputeObject is a virtual object that represents an algorithm which can be used with the compute fun...
Definition compute.h:24