MeshLib C++ Docs
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{
17
18
19
20constexpr bool canSolvePolynomial( auto degree )
21{
22 return 1 <= degree && degree <= 4;
23}
24
25constexpr bool canMinimizePolynomial( auto degree )
26{
27 return degree <= 5;
28}
29
32
33template <typename T, size_t degree>
35{
36 static constexpr size_t n = degree + 1;
37
39 MR_BIND_IGNORE Eigen::Vector<T, n> a;
40
41 template <typename NewT>
43 { return { a.template cast<NewT>() }; }
44
45 MRMESH_API T operator() ( T x ) const;
46
47 MRMESH_API std::vector<T> solve( T tol ) const
48 requires ( canSolvePolynomial( degree ) );
49
50 MRMESH_API Polynomial<T, degree == 0 ? 0 : degree - 1> deriv() const;
51
52 MRMESH_API T intervalMin( T a, T b ) const
53 requires ( canMinimizePolynomial( degree ) );
54};
55
56template <size_t degree>
58
59template <size_t degree>
61
62
63template <typename T>
64using Polynomialx = std::variant
72 >;
73
75template <typename T>
77{
79
80 template <size_t degree>
82 poly( p )
83 {}
84
85 MRMESH_API T operator() ( T x ) const;
86
87 MRMESH_API PolynomialWrapper<T> deriv() const;
88
89 MRMESH_API std::optional<T> intervalMin( T a, T b ) const;
90};
91
94
95
96template <typename T, size_t degree>
98{
99public:
101 MRMESH_API explicit BestFitPolynomial( T reg );
102
103 MRMESH_API void addPoint( T x, T y );
104
105 MRMESH_API void addPoint( T x, T y, T weight );
106
109
110private:
111 static constexpr size_t n = degree + 1;
112 T lambda_ {};
113 Eigen::Matrix<T, n, n> XtX_;
114 Eigen::Vector<T, n> XtY_;
115 T sumWeight_ = 0;
116};
117
118template <size_t degree>
120
121template <size_t degree>
123
124
125
126}
Definition MRBestFitPolynomial.h:98
constexpr bool canMinimizePolynomial(auto degree)
Definition MRBestFitPolynomial.h:25
MRMESH_API void addPoint(T x, T y)
constexpr bool canSolvePolynomial(auto degree)
Definition MRBestFitPolynomial.h:20
MR_BIND_IGNORE Eigen::Vector< T, n > a
We're not binding Eigen at the moment, so this has to be hidden.
Definition MRBestFitPolynomial.h:39
static constexpr size_t n
Definition MRBestFitPolynomial.h:36
MRMESH_API T intervalMin(T a, T b) const
MRMESH_API T operator()(T x) const
MRMESH_API BestFitPolynomial(T reg)
Polynomial< NewT, degree > cast() const
Definition MRBestFitPolynomial.h:42
MRMESH_API void addPoint(T x, T y, T weight)
MRMESH_API Polynomial< T, degree > getBestPolynomial() const
MRMESH_API Polynomial< T, degree==0 ? 0 :degree - 1 > deriv() const
PolynomialWrapper(const Polynomial< T, degree > &p)
Definition MRBestFitPolynomial.h:81
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:64
std::array< Vector3f, 3 > MR_BIND_IGNORE
Definition MRMeshBuilderTypes.h:13
MRMESH_API PolynomialWrapper< T > deriv() const
Polynomialx< T > poly
Definition MRBestFitPolynomial.h:78
MRMESH_API std::optional< T > intervalMin(T a, T b) const
MRMESH_API T operator()(T x) const
MRMESH_API std::vector< T > solve(T tol) const
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
This is a unifying interface for a polynomial of some degree, known only in runtime.
Definition MRBestFitPolynomial.h:77
Definition MRBestFitPolynomial.h:35