libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNLinearInterpolation.cpp
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.cpp
19  * \author Yann LEYDIER
20  */
21 
23 #include <CRNData/CRNDataFactory.h>
24 #include <CRNi18n.h>
25 
26 using namespace crn;
27 
28 double LinearInterpolation::operator[](double x) const
29 {
30  double slope, x0, y0;
31  if (x < data.front().X)
32  {
33  x0 = data[0].X;
34  y0 = data[0].Y;
35  slope = (data[1].Y - y0) / (data[1].X - x0);
36  }
37  if (x > data.back().X)
38  {
39  x0 = data.back().X;
40  y0 = data.back().Y;
41  slope = (data[data.size() - 2].Y - y0) / (data[data.size() - 2].X - x0);
42  }
43  else
44  {
45  for (size_t tmp = 0; tmp < data.size() - 1; ++tmp)
46  {
47  if (data[tmp].X == x)
48  return data[tmp].Y;
49  else if ((data[tmp].X <= x) && (data[tmp + 1].X >= x))
50  {
51  x0 = data[tmp].X;
52  y0 = data[tmp].Y;
53  slope = (data[tmp + 1].Y - y0) / (data[tmp + 1].X - x0);
54  break;
55  }
56  }
57  }
58  return y0 + slope * (x - x0);
59 }
60 
71 {
72  if (el.GetName() != "LinearInterpolation")
73  {
74  throw ExceptionInvalidArgument(StringUTF8("void LinearInterpolation::Deserialize(xml::Element &el): ") +
75  _("Wrong XML element."));
76  }
77 
78  std::vector<crn::Point2DDouble> newdata;
79  for (xml::Element sel = el.BeginElement(); sel != el.EndElement(); ++sel)
80  {
81  newdata.push_back(Point2DDouble());
82  newdata.back().Deserialize(sel); // may throw
83  }
84  data.swap(newdata);
85 }
86 
94 {
95  xml::Element el(parent.PushBackElement("LinearInterpolation"));
96 
97  for (const Point2DDouble &p : data)
98  p.Serialize(el);
99 
100  return el;
101 }
102 
104  CRN_DATA_FACTORY_REGISTER(U"LinearInterpolation", LinearInterpolation)
105  Cloner::Register<LinearInterpolation>();
106 CRN_END_CLASS_CONSTRUCTOR(LinearInterpolation)
107 
108 
xml::Element Serialize(xml::Element &parent) const
A 2D point class.
XML element.
Definition: CRNXml.h:135
StringUTF8 GetName() const
Gets the label of the element.
Definition: CRNXml.h:146
#define _(String)
Definition: CRNi18n.h:51
Element BeginElement()
Gets the first child element.
Definition: CRNXml.h:174
#define CRN_END_CLASS_CONSTRUCTOR(classname)
Defines a class constructor.
Definition: CRNObject.h:198
Linear interpolation.
void Deserialize(xml::Element &el)
#define CRN_DATA_FACTORY_REGISTER(elemname, classname)
Registers a class to the data factory.
virtual double operator[](double x) const override
Gets ordinate at x.
A character string class.
Definition: CRNStringUTF8.h:49
Element PushBackElement(const StringUTF8 &name)
Adds an element at the end of the children list.
Definition: CRNXml.cpp:355
Element EndElement()
Gets a null node.
Definition: CRNXml.h:176
Invalid argument error (e.g.: nullptr pointer)
Definition: CRNException.h:107
#define CRN_BEGIN_CLASS_CONSTRUCTOR(classname)
Defines a class constructor.
Definition: CRNObject.h:185