MeshLib Documentation
Loading...
Searching...
No Matches
MRBestFitPolynomial.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMeshFwd.h"
4#include "MRPch/MRBindingMacros.h"
5#include <MRPch/MREigenCore.h>
6
7#include <cstddef>
8#include <vector>
9#include <variant>
10#include <optional>
11
12
13namespace MR
14{
15
16
17constexpr bool canSolvePolynomial( auto degree )
18{
19 return 1 <= degree && degree <= 4;
20}
21
22constexpr bool canMinimizePolynomial( auto degree )
23{
24 return degree <= 5;
25}
26
27// Please note that these template classes are explicitly instantiated in the corresponding .cpp files.
28// The following degrees are instantiated: [2; 6].
29
30template <typename T, size_t degree>
32{
33 static constexpr size_t n = degree + 1;
34
35 // We're not binding Eigen at the moment, so this has to be hidden.
36 MR_BIND_IGNORE Eigen::Vector<T, n> a;
37
38 template <typename NewT>
40 { return { a.template cast<NewT>() }; }
41
42 MRMESH_API T operator() ( T x ) const;
43
44 MRMESH_API std::vector<T> solve( T tol ) const
45 requires ( canSolvePolynomial( degree ) );
46
47 MRMESH_API Polynomial<T, degree == 0 ? 0 : degree - 1> deriv() const;
48
49 MRMESH_API T intervalMin( T a, T b ) const
50 requires ( canMinimizePolynomial( degree ) );
51};
52
53template <size_t degree>
55
56template <size_t degree>
58
59
60template <typename T>
61using Polynomialx = std::variant
69 >;
70
72template <typename T>
74{
76
77 template <size_t degree>
79 poly( p )
80 {}
81
82 MRMESH_API T operator() ( T x ) const;
83
85
86 MRMESH_API std::optional<T> intervalMin( T a, T b ) const;
87};
88
91
92
93template <typename T, size_t degree>
95{
96public:
98 MRMESH_API explicit BestFitPolynomial( T reg );
99
100 MRMESH_API void addPoint( T x, T y );
101
102 MRMESH_API void addPoint( T x, T y, T weight );
103
106
107private:
108 static constexpr size_t n = degree + 1;
109 T lambda_ {};
110 Eigen::Matrix<T, n, n> XtX_;
111 Eigen::Vector<T, n> XtY_;
112 T sumWeight_ = 0;
113};
114
115template <size_t degree>
117
118template <size_t degree>
120
121
122
123}
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:46
Definition MRBestFitPolynomial.h:95
MRMESH_API void addPoint(T x, T y)
MRMESH_API BestFitPolynomial(T reg)
MRMESH_API void addPoint(T x, T y, T weight)
MRMESH_API Polynomial< T, degree > getBestPolynomial() const
Definition MRCameraOrientationPlugin.h:8
constexpr bool canMinimizePolynomial(auto degree)
Definition MRBestFitPolynomial.h:22
constexpr bool canSolvePolynomial(auto degree)
Definition MRBestFitPolynomial.h:17
std::variant< Polynomial< T, 0 >, Polynomial< T, 1 >, Polynomial< T, 2 >, Polynomial< T, 3 >, Polynomial< T, 4 >, Polynomial< T, 5 >, Polynomial< T, 6 > > Polynomialx
Definition MRBestFitPolynomial.h:61
This is a unifying interface for a polynomial of some degree, known only in runtime.
Definition MRBestFitPolynomial.h:74
PolynomialWrapper(const Polynomial< T, degree > &p)
Definition MRBestFitPolynomial.h:78
MRMESH_API PolynomialWrapper< T > deriv() const
Polynomialx< T > poly
Definition MRBestFitPolynomial.h:75
MRMESH_API std::optional< T > intervalMin(T a, T b) const
MRMESH_API T operator()(T x) const
Definition MRBestFitPolynomial.h:32
MR_BIND_IGNORE Eigen::Vector< T, n > a
Definition MRBestFitPolynomial.h:36
static constexpr size_t n
Definition MRBestFitPolynomial.h:33
MRMESH_API T intervalMin(T a, T b) const
MRMESH_API T operator()(T x) const
Polynomial< NewT, degree > cast() const
Definition MRBestFitPolynomial.h:39
MRMESH_API Polynomial< T, degree==0 ? 0 :degree - 1 > deriv() const
MRMESH_API std::vector< T > solve(T tol) const