Main MRPT website > C++ reference for MRPT 1.9.9
CSplineInterpolator1D.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "base-precomp.h" // Precompiled headers
11 
12 #include <mrpt/utils/types_math.h> // for dynamic_vector, CVector...
13 #include <mrpt/math/CSplineInterpolator1D.h> // for CSplineInterpolator1D
14 #include <mrpt/utils/CStream.h> // for CStream, operator<<
15 #include <mrpt/utils/stl_serialization.h> // for operator<<, operator>>
16 #include <map> // for _Rb_tree_const_iterator
17 #include <mrpt/math/interp_fit.hpp> // for spline
18 #include <mrpt/utils/CObject.h> // for CSplineInterpolator1D::...
19 #include <mrpt/utils/CSerializable.h> // for CSerializable, CSeriali...
20 #include <mrpt/utils/bits.h> // for format
21 #include <mrpt/utils/mrpt_macros.h> // for MRPT_THROW_UNKNOWN_SERI...
22 
23 using namespace mrpt;
24 using namespace mrpt::math;
25 using namespace mrpt::utils;
26 using namespace std;
27 
28 // This must be added to any CSerializable class implementation file.
30 
31 /*---------------------------------------------------------------
32  Constructor
33  ---------------------------------------------------------------*/
34 CSplineInterpolator1D::CSplineInterpolator1D(bool wrap2pi) : m_wrap2pi(wrap2pi)
35 {
36 }
37 
38 /*---------------------------------------------------------------
39  appendXY
40  ---------------------------------------------------------------*/
41 void CSplineInterpolator1D::appendXY(double x, double y) { m_x2y[x] = y; }
42 /*---------------------------------------------------------------
43  query
44  ---------------------------------------------------------------*/
45 double& CSplineInterpolator1D::query(double x, double& y, bool& out_valid) const
46 {
47  out_valid = false;
48  y = 0;
49 
50  std::pair<double, double> p1, p2, p3, p4;
51 
52  std::map<double, double>::const_iterator it_ge1 = m_x2y.lower_bound(x);
53 
54  // Exact match?
55  if (it_ge1 != m_x2y.end() && it_ge1->first == x)
56  {
57  y = it_ge1->second;
58  out_valid = true;
59  return y;
60  }
61 
62  // Are we in the beginning or the end of the path?
63  if (it_ge1 == m_x2y.end() || it_ge1 == m_x2y.begin())
64  {
65  return y;
66  }
67 
68  p3 = *it_ge1; // Third pair
69  ++it_ge1;
70  if (it_ge1 == m_x2y.end())
71  {
72  return y;
73  }
74  p4 = *it_ge1; // Fourth pair
75 
76  --it_ge1;
77  --it_ge1;
78  p2 = *it_ge1; // Second pair
79 
80  if (it_ge1 == m_x2y.begin())
81  {
82  return y;
83  }
84 
85  p1 = *(--it_ge1); // First pair
86 
87  // ---------------------------------------
88  // SPLINE INTERPOLATION
89  // ---------------------------------------
90  CVectorDouble xs(4);
91  xs[0] = p1.first;
92  xs[1] = p2.first;
93  xs[2] = p3.first;
94  xs[3] = p4.first;
95 
96  CVectorDouble ys(4);
97  ys[0] = p1.second;
98  ys[1] = p2.second;
99  ys[2] = p3.second;
100  ys[3] = p4.second;
101 
102  out_valid = true;
103  return y = math::spline(x, xs, ys, m_wrap2pi);
104 }
105 
106 /*---------------------------------------------------------------
107  Implements the writing to a CStream capability of
108  CSerializable objects
109  ---------------------------------------------------------------*/
111  mrpt::utils::CStream& out, int* version) const
112 {
113  if (version)
114  *version = 0;
115  else
116  {
117  out << m_x2y << m_wrap2pi;
118  }
119 }
120 
121 /*---------------------------------------------------------------
122  Implements the reading from a CStream capability of
123  CSerializable objects
124  ---------------------------------------------------------------*/
126  mrpt::utils::CStream& in, int version)
127 {
128  switch (version)
129  {
130  case 0: // floats
131  {
132  in >> m_x2y >> m_wrap2pi;
133  }
134  break;
135  default:
137  };
138 }
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:44
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction...
Definition: eigen_frwds.h:42
void appendXY(double x, double y)
Append a new point:
void readFromStream(mrpt::utils::CStream &in, int version) override
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
STL namespace.
const Scalar * const_iterator
Definition: eigen_plugins.h:27
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
A (persistent) sequence of (x,y) coordinates, allowing queries of intermediate points through spline ...
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
NUMTYPE spline(const NUMTYPE t, const VECTORLIKE &x, const VECTORLIKE &y, bool wrap2pi=false)
Interpolates the value of a function in a point "t" given 4 SORTED points where "t" is between the tw...
Definition: interp_fit.hpp:37
GLuint in
Definition: glext.h:7274
GLenum GLint GLint y
Definition: glext.h:3538
GLenum GLint x
Definition: glext.h:3538
double & query(double x, double &y, bool &out_valid) const
Query an interpolation of the curve at some "x".



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019