libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNLinearInterpolation.h
Go to the documentation of this file.
1 /* Copyright 2012-2016 CoReNum, 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: CRNLinearInterpolation.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef CRNLinearInterpolation_HEADER
23 #define CRNLinearInterpolation_HEADER
24 
27 #include <CRNException.h>
28 
29 namespace crn
30 {
41  {
42  public:
48  template<typename Iter> LinearInterpolation(Iter beg, Iter en):
49  data(std::distance(beg, en))
50  {
51  if (data.size() < 2)
52  throw crn::ExceptionDimension("There must be at least 2 samples to interpolate anything.");
53  size_t cnt = 0;
54  for (Iter tmp = beg; tmp != en; ++tmp)
55  data[cnt++] = makePoint(*tmp);
56  std::sort(data.begin(), data.end(), Point2DDouble::Comparer(Direction::LEFT));
57  }
58  LinearInterpolation(const LinearInterpolation&) = default;
60  virtual ~LinearInterpolation() override {}
63 
65  virtual double operator[](double x) const override;
67  using Cartesian2DFunction::operator[];
68 
70  const std::vector<crn::Point2DDouble>& GetData() const noexcept { return data; }
71 
72  void Deserialize(xml::Element &el);
73  xml::Element Serialize(xml::Element &parent) const;
74 
75  private:
77  template<typename T> Point2DDouble makePoint(const crn::Point2D<T> &p)
78  { return crn::Point2DDouble(double(p.X), double(p.Y)); }
80  template<typename T> Point2DDouble makePoint(const crn::Point2D<T> *p)
81  { return crn::Point2DDouble(double(p->X), double(p->Y)); }
83  template<typename T, typename Y> Point2DDouble makePoint(const std::pair<T, Y> &p)
84  { return crn::Point2DDouble(double(p.first), double(p.second)); }
85 
86  std::vector<crn::Point2DDouble> data;
90  };
91  template<> struct IsSerializable<LinearInterpolation> : public std::true_type {};
92  template<> struct IsClonable<LinearInterpolation> : public std::true_type {};
93 
95 }
96 
97 #endif
98 
#define CRN_SERIALIZATION_CONSTRUCTOR(classname)
Defines a default constructor from xml element.
Definition: CRNObject.h:165
xml::Element Serialize(xml::Element &parent) const
A 2D point class.
XML element.
Definition: CRNXml.h:135
const std::vector< crn::Point2DDouble > & GetData() const noexcept
Access to the sorted control point.
A 2D point class.
Definition: CRNPoint2D.h:45
Linear interpolation.
void Deserialize(xml::Element &el)
value_type X
Definition: CRNPoint2D.h:63
LinearInterpolation(Iter beg, Iter en)
Constructor from a list of points.
A dimension error.
Definition: CRNException.h:119
virtual ~LinearInterpolation() override
value_type Y
Definition: CRNPoint2D.h:63
virtual double operator[](double x) const override
Gets ordinate at x.
LinearInterpolation & operator=(const LinearInterpolation &)=default
Base class for 2D functions.
#define CRN_DECLARE_CLASS_CONSTRUCTOR(classname)
Declares a class constructor.
Definition: CRNObject.h:173
CRN_ALIAS_SMART_PTR(ImageBW)