libcrn  3.9.5
A document image processing library
•All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNPolynomialRegression.h
Go to the documentation of this file.
1 /* Copyright 2011-2016 CoReNum, INSA-Lyon, Université Paris Descartes, ENS-Lyon
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNPolynomialRegression.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef CRNPolynomialRegression_HEADER
23 #define CRNPolynomialRegression_HEADER
24 
27 #include <CRNException.h>
28 
29 namespace crn
30 {
41  {
42  public:
44  enum class Extrapolation { LINEAR, POLY };
45 
53  template<typename Iter> PolynomialRegression(Iter beg, Iter en, size_t dim):
54  dimension(dim),
55  data(std::distance(beg, en)),
56  coefficients(dim + 1),
57  extrapolation(Extrapolation::POLY)
58  {
59  if (dim == 0)
60  throw crn::ExceptionDomain("Null order");
61  if (data.size() < dimension)
62  throw crn::ExceptionDimension("There must be more samples than the dimension.");
63  size_t cnt = 0;
64  for (Iter tmp = beg; tmp != en; ++tmp)
65  data[cnt++] = makePoint(*tmp);
66  computeCoeffs();
67  }
72  virtual ~PolynomialRegression() override {}
73 
75  void SetExtrapolationMode(Extrapolation ex) noexcept { extrapolation = ex; }
76 
78  void TranslateY(int increment);
79 
81  virtual double operator[](double x) const override;
83  using Cartesian2DFunction::operator[];
84 
86  size_t GetDimension() const noexcept { return dimension; }
88  const std::vector<crn::Point2DDouble>& GetData() const noexcept { return data; }
90  const std::vector<double>& GetCoefficients() const noexcept { return coefficients; }
91 
92  private:
94  template<typename T> Point2DDouble makePoint(const crn::Point2D<T> &p)
95  { return crn::Point2DDouble(double(p.X), double(p.Y)); }
97  template<typename T> Point2DDouble makePoint(const crn::Point2D<T> *p)
98  { return crn::Point2DDouble(double(p->X), double(p->Y)); }
100  template<typename T, typename Y> Point2DDouble makePoint(const std::pair<T, Y> &p)
101  { return crn::Point2DDouble(double(p.first), double(p.second)); }
103  void computeCoeffs();
104 
105  std::vector<double> coefficients;
106  std::vector<crn::Point2DDouble> data;
107  Extrapolation extrapolation;
108  size_t dimension;
109 
111  };
112  template<> struct IsClonable<PolynomialRegression> : public std::true_type {};
113 
115 }
116 
117 #endif
118 
PolynomialRegression(Iter beg, Iter en, size_t dim)
Constructor from a list of points.
void TranslateY(int increment)
Translates the polynomial.
A 2D point class.
PolynomialRegression & operator=(const PolynomialRegression &)=default
const std::vector< crn::Point2DDouble > & GetData() const noexcept
Access to the sorted control point.
A 2D point class.
Definition: CRNPoint2D.h:45
virtual double operator[](double x) const override
Gets ordinate at x (double)
A generic domain error.
Definition: CRNException.h:83
Extrapolation
Extrapolation modes.
value_type X
Definition: CRNPoint2D.h:63
A dimension error.
Definition: CRNException.h:119
size_t GetDimension() const noexcept
Returns the dimension of the polynomial.
value_type Y
Definition: CRNPoint2D.h:63
const std::vector< double > & GetCoefficients() const noexcept
Access to the coefficients of the polynomial.
virtual ~PolynomialRegression() override
Base class for 2D functions.
#define CRN_DECLARE_CLASS_CONSTRUCTOR(classname)
Declares a class constructor.
Definition: CRNObject.h:173
void SetExtrapolationMode(Extrapolation ex) noexcept
Sets the behaviour for points before the first control point and after the last control point...
CRN_ALIAS_SMART_PTR(ImageBW)