G+Smo  25.01.0
Geometry + Simulation Modules
 
Loading...
Searching...
No Matches
gsFitting.h
Go to the documentation of this file.
1
15#pragma once
16
20
21namespace gismo
22{
23
30template<class T>
32{
33public:
36 {
37 m_basis = nullptr;
38 m_result= nullptr;
39 }
40
46 gsFitting(gsMatrix<T> const & param_values,
47 gsMatrix<T> const & points,
48 gsBasis<T> & basis);
49
51 gsFitting(gsMatrix<T> const & param_values,
52 gsMatrix<T> const & points,
53 gsVector<index_t> offset,
54 gsMappedBasis<2,T> & mbasis) ;
55
57 virtual ~gsFitting();
58
59public:
60
64 void compute(T lambda = 0);
65
70 void updateGeometry(gsMatrix<T> coefficients, gsMatrix<T> parameters);
71
72
77 void initializeGeometry(const gsMatrix<T> & coefficients, const gsMatrix<T> & parameters);
78
81 {
82 tdm_boundary_pdm, // TDM
83 pdm, // PDM
84 hybrid_pdm_tdm_boundary_pdm, // HDM with constant weight
85 hybrid_error_pdm_tdm_boundary_pdm, // HDM with weight based on point-wise error
86 hybrid_curvature_pdm_tdm_boundary_pdm, // HDM with weight based on curvature
87 };
88
97 void compute_tdm(T lambda, T mu, T sigma, const std::vector<index_t> & interpIdx,
98 tdm_method method = hybrid_curvature_pdm_tdm_boundary_pdm);
99
100
102 bool is_corner(gsMatrix<T> & parametric_domain, gsVector<T> & parameter);
103
104
105
109 bool is_point_within_cell(const T x, const T y, const gsMatrix<T>& element);
110
115 bool is_point_inside_support(const T x, const T y, const gsMatrix<T>& support);
116
117
123 void parameterCorrection(T accuracy = 1e-8,
124 index_t maxIter = 10,
125 T tolOrth = 1e-6);
126
131 void parameterProjectionSepBoundary(T accuracy,const std::vector<index_t>& interpIdx);
132
138 void parameterCorrectionSepBoundary_pdm(T accuracy, index_t maxIter, const std::vector<index_t>& sepIndex);
139
148 void parameterCorrectionSepBoundary_tdm(T accuracy, index_t maxIter, T mu, T sigma, const std::vector<index_t>& sepIndex, tdm_method method = hybrid_curvature_pdm_tdm_boundary_pdm);
149
153 void computeErrors();
154
157 gsMatrix<T> pointWiseErrors(const gsMatrix<> & parameters,const gsMatrix<> & points);
158
161 std::vector<T> computeErrors(const gsMatrix<> & param_values,const gsMatrix<> & points);
162
167
170 void computeApproxError(T & error, int type = 0) const;
171
174 void get_Error(std::vector<T>& errors, int type = 0) const;
175
177 T minPointError() const { return m_min_error; }
178
180 T maxPointError() const { return m_max_error; }
181
183 const std::vector<T> & pointWiseErrors() const
184 {
185 return m_pointErrors;
186 }
187
189 size_t numPointsBelow(T threshold) const
190 {
191 const size_t result=
192 std::count_if(m_pointErrors.begin(), m_pointErrors.end(),
193 GS_BIND2ND(std::less<T>(), threshold));
194 return result;
195 }
196
198 void iterativeCompute( T const & tolerance, unsigned const & num_iters = 10);
199
201 void applySmoothing(T lambda, gsSparseMatrix<T> & A_mat);
202 gsSparseMatrix<T> smoothingMatrix(T lambda) const;
205
211 void compute_normals(const index_t & num_int, const gsMatrix<T> & params_int, gsSparseMatrix<T> & N_int);
212
214 gsMatrix<T> fill_pointWiseErrors(const index_t & num_int, T & max_err_int);
215
218
220 gsMatrix<T> inverse_principal_curvatures(const index_t & num_int, const gsMatrix<T> & params_int);
221
231 void blending_weights(const gsSparseMatrix<T> & N_int, const index_t & num_int, const T & mu, const T & sigma,
232 const gsMatrix<T> & params_int, tdm_method method, gsSparseMatrix<T> & NNT);
233
244 void assembleSystem(const gsMatrix<T> & points_int, const gsMatrix<T> & params_int,
245 const gsMatrix<T> & points_bdy, const gsMatrix<T> & params_bdy,
246 const index_t & num_basis, const gsSparseMatrix<T> & NNT,
247 gsSparseMatrix<T> & A_tilde, gsMatrix<T> & rhs);
248
249public:
250
252 gsGeometry<T> * result() const { return m_result; }
253
255 const gsMappedSpline<2,T> & mresult() const { return m_mresult; }
256
258 const gsBasis<T> & getBasis() const {return *static_cast<const gsBasis<T>*>(m_basis);}
259
260 void setBasis(gsBasis<T> & basis) {m_basis=&basis;}
261
264
267
272 void setConstraints(const gsSparseMatrix<T>& lhs, const gsMatrix<T>& rhs)
273 {
274 m_constraintsLHS = lhs;
275 m_constraintsRHS = rhs;
276 }
277
281 void setConstraints(const std::vector<index_t>& indices,
282 const std::vector<gsMatrix<T> >& coefs);
283
286 void setConstraints(const std::vector<boxSide>& fixedSides);
287
291 void setConstraints(const std::vector<boxSide>& fixedSides,
292 const std::vector<gsBSpline<T> >& fixedCurves);
293 void setConstraints(const std::vector<boxSide>& fixedSides,
294 const std::vector<gsGeometry<T> * >& fixedCurves);
295
298 {
299 m_uMin = m_param_values.row(0).minCoeff();
300 m_uMax = m_param_values.row(0).maxCoeff();
301 m_vMin = m_param_values.row(1).minCoeff();
302 m_vMax = m_param_values.row(1).maxCoeff();
303
304 gsInfo << "Parametric domain: ["
305 << m_uMin << ", " << m_uMax << "] x ["
306 << m_vMin << ", " << m_vMax << "]" << std::endl;
307 }
308
310 T lambda() const {return m_last_lambda;}
311
312
313private:
315 void extendSystem(gsSparseMatrix<T>& A_mat, gsMatrix<T>& m_B);
316
317protected:
318
320 void assembleBlockB(const gsMatrix<T>& points,
321 const gsMatrix<T>& params,
322 index_t num_basis,
324 {
325 GISMO_UNUSED(points);
326 GISMO_UNUSED(num_basis);
327 //index_t num_pts = points.rows();
328 gsSparseMatrix<T> sparseColloc = m_result->basis().collocationMatrix(params);
329 threeOnDiag(sparseColloc, result);
330 }
331
333 void assembleBlockX(const gsMatrix<T>& points,
334 gsMatrix<T>& result) const
335 {
336 result.resize(points.rows() * 3, 1);
337 result << points.col(0), points.col(1), points.col(2);
338 }
339
340protected:
341
342 //gsOptionList
343
346
349
350 // Patch offsets
351 gsVector<index_t> m_offset;
352
355
358
360 gsMappedSpline<2,T> m_mresult;
361
362 // All point-wise errors
363 std::vector<T> m_pointErrors;
364
365 // Interior c1 and c2 curvature, as column of the matrix
366 gsMatrix<T> m_pointCurvature;
367
368 mutable T m_last_lambda;
369
372
375
381
387
388 T m_uMin, m_uMax, m_vMin, m_vMax;
389
390private:
391 //void applySmoothing(T lambda, gsMatrix<T> & A_mat);
392
393}; // class gsFitting
394
395
396#ifdef GISMO_WITH_PYBIND11
397
401 void pybind11_init_gsFitting(pybind11::module &m);
402
403#endif // GISMO_WITH_PYBIND11
404
405
406}// namespace gismo
407
408
409#ifndef GISMO_BUILD_LIB
410#include GISMO_HPP_HEADER(gsFitting.hpp)
411#endif
A B-spline function of one argument, with arbitrary target dimension.
Definition gsBSpline.h:51
A basis represents a family of scalar basis functions defined over a common parameter domain.
Definition gsBasis.h:79
Class for performing a fit of a parametrized point cloud with a gsGeometry.
Definition gsFitting.h:32
gsMappedSpline< 2, T > m_mresult
Pointer keeping the resulting multipatch geometry.
Definition gsFitting.h:360
gsMatrix< T > m_param_values
the parameter values of the point cloud
Definition gsFitting.h:345
gsFitting()
default constructor
Definition gsFitting.h:35
tdm_method
choose the method for computing the coefficients: TDM, PDM, HDM with different blending weights
Definition gsFitting.h:81
size_t numPointsBelow(T threshold) const
Computes the number of points below the error threshold (or zero if not fitted)
Definition gsFitting.h:189
bool is_point_within_cell(const gsMatrix< T > &parameter, const gsMatrix< T > &element)
check if the given parameter parameter is within the cell element
gsGeometry< T > * result() const
gives back the computed approximation
Definition gsFitting.h:252
gsMatrix< T > fill_pointWiseErrors(const index_t &num_int, T &max_err_int)
vector of length num_int containing all the point-wise errors; store also the max err value in max_er...
Definition gsFitting.hpp:222
void assembleBlockB(const gsMatrix< T > &points, const gsMatrix< T > &params, index_t num_basis, gsSparseMatrix< T > &result) const
Assembles 3xblock collocation matrix.
Definition gsFitting.h:320
void assembleBlockX(const gsMatrix< T > &points, gsMatrix< T > &result) const
Assembles the right hand side vectors for PDM/TDM.
Definition gsFitting.h:333
void extendSystem(gsSparseMatrix< T > &A_mat, gsMatrix< T > &m_B)
Extends the system of equations by taking constraints into account.
Definition gsFitting.hpp:773
void computeApproxError(T &error, int type=0) const
Definition gsFitting.hpp:1011
void assembleSystem(gsSparseMatrix< T > &A_mat, gsMatrix< T > &B)
Assembles system for the least square fit.
Definition gsFitting.hpp:731
bool is_point_inside_support(const T x, const T y, const gsMatrix< T > &support)
check if the given parameter x, y is inside the support support; same as is_point_inside_support,...
const std::vector< T > & pointWiseErrors() const
Return the errors for each point.
Definition gsFitting.h:183
void initParametricDomain()
Initialize the parametric domain of the point cloud.
Definition gsFitting.h:297
void parameterCorrectionSepBoundary_tdm(T accuracy, index_t maxIter, T mu, T sigma, const std::vector< index_t > &sepIndex, tdm_method method=hybrid_curvature_pdm_tdm_boundary_pdm)
parameterCorrectionSepBoundary_tdm: apply maxIter steps of parameter correction for HDM method,...
Definition gsFitting.hpp:646
void applySmoothing(T lambda, gsSparseMatrix< T > &A_mat)
Adds to the matrix A_mat terms for minimization of second derivative, weighted with parameter lambda.
Definition gsFitting.hpp:816
gsGeometry< T > * m_result
Pointer keeping the resulting geometry.
Definition gsFitting.h:357
T m_max_error
Maximum point-wise error.
Definition gsFitting.h:371
void initializeGeometry(const gsMatrix< T > &coefficients, const gsMatrix< T > &parameters)
initializeGeometry: Initializes the fitted geometry with given coefficients and parameters
Definition gsFitting.hpp:175
gsMatrix< T > inverse_principal_curvatures(const index_t &num_int, const gsMatrix< T > &params_int)
vector of length num_int containing rho = 1/max(c1, c2), where c1, c2 are the principal curvature val...
Definition gsFitting.hpp:266
gsMatrix< T > & returnParamValues()
returns the parameter values
Definition gsFitting.h:263
gsFunctionSet< T > * m_basis
Pointer keeping the basis.
Definition gsFitting.h:354
void compute_normals(const index_t &num_int, const gsMatrix< T > &params_int, gsSparseMatrix< T > &N_int)
compute_normals: Computes the normals of the fitted geometry at the input parameter values params_int
Definition gsFitting.hpp:195
const gsMappedSpline< 2, T > & mresult() const
gives back the computed approximation for multipatch geometry
Definition gsFitting.h:255
void compute_tdm(T lambda, T mu, T sigma, const std::vector< index_t > &interpIdx, tdm_method method=hybrid_curvature_pdm_tdm_boundary_pdm)
compute_hdm: computes the coefficients of the spline geometry via Hybrid Distance Minimization (HDM)
Definition gsFitting.hpp:375
void setConstraints(const gsSparseMatrix< T > &lhs, const gsMatrix< T > &rhs)
Definition gsFitting.h:272
T minPointError() const
Returns the minimum point-wise error from the pount cloud (or zero if not fitted)
Definition gsFitting.h:177
void parameterProjectionSepBoundary(T accuracy, const std::vector< index_t > &interpIdx)
parameterProjectionSepBoundary: project the points onto the fitted geometry, separating interior and ...
Definition gsFitting.hpp:554
const gsBasis< T > & getBasis() const
Returns the basis of the approximation.
Definition gsFitting.h:258
T m_min_error
Minimum point-wise error.
Definition gsFitting.h:374
void get_Error(std::vector< T > &errors, int type=0) const
Definition gsFitting.hpp:1053
void compute(T lambda=0)
compute: Computes the coefficients of the spline geometry via penalized least squares
Definition gsFitting.hpp:77
void computeMaxNormErrors()
Definition gsFitting.hpp:979
gsMatrix< T > returnPoints() const
returns the points
Definition gsFitting.h:266
virtual ~gsFitting()
Destructor.
Definition gsFitting.hpp:31
void parameterCorrectionSepBoundary_pdm(T accuracy, index_t maxIter, const std::vector< index_t > &sepIndex)
parameterCorrectionSepBoundary_pdm: apply maxIter steps of parameter correction for PDM method,...
Definition gsFitting.hpp:670
gsSparseMatrix< T > m_constraintsLHS
Definition gsFitting.h:380
T lambda() const
Returns the smoothing weight used in the last fitting.
Definition gsFitting.h:310
bool is_point_within_cell(const T x, const T y, const gsMatrix< T > &element)
check if the given parameter x, y is within the cell element; same as is_point_within_cell,...
void parameterCorrection(T accuracy=1e-8, index_t maxIter=10, T tolOrth=1e-6)
parameterCorrection: globally apply maxIter steps of parameter correction to the least squares fitted...
Definition gsFitting.hpp:692
gsMatrix< T > m_constraintsRHS
Definition gsFitting.h:386
void blending_weights(const gsSparseMatrix< T > &N_int, const index_t &num_int, const T &mu, const T &sigma, const gsMatrix< T > &params_int, tdm_method method, gsSparseMatrix< T > &NNT)
blending_weights: computes the blending weights mu and sigma for the balance mu * PDM + sigma * TDM i...
Definition gsFitting.hpp:294
void updateGeometry(gsMatrix< T > coefficients, gsMatrix< T > parameters)
updateGeometry: Updates the fitted geometry with new coefficients and parameters
Definition gsFitting.hpp:154
gsMatrix< T > principal_curvatures(const gsMatrix< T > &params)
compute the principal curvatures (c1, c2) at the given parameters params
Definition gsFitting.hpp:237
void iterativeCompute(T const &tolerance, unsigned const &num_iters=10)
Computes the least squares fit for a gsBasis.
gsMatrix< T > m_points
the points of the point cloud
Definition gsFitting.h:348
T maxPointError() const
Returns the maximum point-wise error from the pount cloud (or zero if not fitted)
Definition gsFitting.h:180
bool is_corner(gsMatrix< T > &parametric_domain, gsVector< T > &parameter)
check if the given parameter parameter is a corner of the domain parametric_domain
Definition gsFitting.hpp:484
bool is_point_inside_support(const gsMatrix< T > &parameter, const gsMatrix< T > &support)
void computeErrors()
Definition gsFitting.hpp:907
Interface for the set of functions defined on a domain (the total number of functions in the set equa...
Definition gsFunctionSet.h:219
Abstract base class representing a geometry map.
Definition gsGeometry.h:93
A matrix with arbitrary coefficient type and fixed or dynamic size.
Definition gsMatrix.h:41
Sparse matrix class, based on gsEigen::SparseMatrix.
Definition gsSparseMatrix.h:139
A vector with arbitrary coefficient type and fixed or dynamic size.
Definition gsVector.h:37
#define index_t
Definition gsConfig.h:32
#define GISMO_UNUSED(x)
Definition gsDebug.h:112
#define gsInfo
Definition gsDebug.h:43
Provides forward declarations of types and structs.
Provides declaration of Basis abstract interface.
Provides declaration of Basis abstract interface.
The G+Smo namespace, containing all definitions for the library.
void threeOnDiag(const gsSparseMatrix< T > &block, gsSparseMatrix< T > &result)
Definition gsModelingUtils.hpp:664
GISMO_DEPRECATED bool parameter(int s)
Returns the parameter value (false=0=start, true=1=end) that corresponds to side s.
Definition gsBoundary.h:1069