libcrn  3.9.5
A document image processing library
•All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNCubicSpline.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: CRNCubicSpline.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef CRNCubicSpline_HEADER
23 #define CRNCubicSpline_HEADER
24 
27 #include <CRNException.h>
28 
29 namespace crn
30 {
41  {
42  public:
44  enum class Extrapolation { LINEAR, CUBIC };
45 
51  template<typename Iter> CubicSpline(Iter beg, Iter en):
52  data(std::distance(beg, en)),
53  extrapolation(Extrapolation::CUBIC)
54  {
55  if (data.size() < 3)
56  throw crn::ExceptionDimension("There must be at least 3 samples to create a spline");
57  size_t cnt = 0;
58  for (Iter tmp = beg; tmp != en; ++tmp)
59  data[cnt++] = makePoint(*tmp);
60  computeCoeffs();
61  }
62  CubicSpline(const CubicSpline&) = default;
63  CubicSpline(CubicSpline&&) = default;
64  virtual ~CubicSpline() override {}
65  CubicSpline& operator=(const CubicSpline&) = default;
66  CubicSpline& operator=(CubicSpline&&) = default;
67 
69  void SetExtrapolationMode(Extrapolation ex) noexcept { extrapolation = ex; }
70 
72  virtual double operator[](double x) const override;
74  using Cartesian2DFunction::operator[];
75 
77  const std::vector<crn::Point2DDouble>& GetData() const noexcept { return data; }
78 
79  private:
81  template<typename T> Point2DDouble makePoint(const crn::Point2D<T> &p)
82  { return crn::Point2DDouble(double(p.X), double(p.Y)); }
84  template<typename T> Point2DDouble makePoint(const crn::Point2D<T> *p)
85  { return crn::Point2DDouble(double(p->X), double(p->Y)); }
87  template<typename T, typename Y> Point2DDouble makePoint(const std::pair<T, Y> &p)
88  { return crn::Point2DDouble(double(p.first), double(p.second)); }
90  void computeCoeffs();
91 
92  std::vector<double> z;
93  std::vector<crn::Point2DDouble> data;
94  Extrapolation extrapolation;
97  };
98  template<> struct IsClonable<CubicSpline> : public std::true_type {};
99 
101 }
102 
103 #endif
104 
A 2D point class.
void SetExtrapolationMode(Extrapolation ex) noexcept
Sets the behaviour for points before the first control point and after the last control point...
A 2D point class.
Definition: CRNPoint2D.h:45
CubicSpline(Iter beg, Iter en)
Constructor from a list of points.
value_type X
Definition: CRNPoint2D.h:63
A dimension error.
Definition: CRNException.h:119
CubicSpline & operator=(const CubicSpline &)=default
virtual double operator[](double x) const override
Gets ordinate at x.
virtual ~CubicSpline() override
Extrapolation
Extrapolation modes.
Cubic spline interpolation.
value_type Y
Definition: CRNPoint2D.h:63
Base class for 2D functions.
#define CRN_DECLARE_CLASS_CONSTRUCTOR(classname)
Declares a class constructor.
Definition: CRNObject.h:173
CRN_ALIAS_SMART_PTR(ImageBW)
const std::vector< crn::Point2DDouble > & GetData() const noexcept
Access to the sorted control point.