MRPT  2.0.4
CSplineInterpolator1D.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
12 #include <map>
13 
14 namespace mrpt::math
15 {
16 /** A (persistent) sequence of (x,y) coordinates, allowing queries of
17  * intermediate points through spline interpolation, where possible.
18  * This class internally relies on mrpt::math::spline. Optionally the y
19  * coordinate can be set as wrapped in ]-pi,pi].
20  * For querying interpolated points, see
21  * \ sa mrpt::math::spline, mrpt::poses::CPose3DInterpolator
22  * \ingroup interpolation_grp
23  */
25 {
27 
28  private:
29  /** The placeholders for the data */
30  std::map<double, double> m_x2y;
31 
32  /** Whether to wrap "y" */
33  bool m_wrap2pi;
34 
35  public:
36  /** Constructor with optional initial values. */
37  template <class VECTOR>
39  const VECTOR& initial_x, const VECTOR& initial_y, bool wrap2pi = false)
40  : m_wrap2pi(wrap2pi)
41  {
42  setXY(initial_x, initial_y);
43  }
44 
45  /** Constructor */
46  CSplineInterpolator1D(bool wrap2pi = false);
47 
48  /** If set to true, the interpolated data will be wrapped to ]-pi,pi] */
49  void setWrap2pi(bool wrap) { m_wrap2pi = wrap; }
50  /** Return the wrap property */
51  bool getWrap2pi() { return m_wrap2pi; }
52  /** Set all the data at once .
53  * The vectors must have the same length.
54  */
55  template <class VECTOR>
56  void setXY(
57  const VECTOR& x, const VECTOR& y, bool clearPreviousContent = true)
58  {
60  if (clearPreviousContent) m_x2y.clear();
61  ASSERT_EQUAL_(x.size(), y.size());
62  const size_t n = size_t(x.size());
63  for (size_t i = 0; i < n; i++) m_x2y[x[i]] = y[i];
64  MRPT_END
65  }
66 
67  /** Append a new point: */
68  void appendXY(double x, double y);
69 
70  /** Clears all stored points */
71  void clear() { m_x2y.clear(); }
72  /** Query an interpolation of the curve at some "x".
73  * The result is stored in "y". If the "x" point is out of range,
74  * "valid_out" is set to false.
75  * \return A reference to "y"
76  * \sa queryVector
77  */
78  double& query(double x, double& y, bool& out_valid) const;
79 
80  /** As query, but for a whole vector at once.
81  * \return false if there is at least one value that couldn't be
82  * interpolated (in this case the output is indeterminate).
83  * \sa query
84  */
85  template <class VECTOR1, class VECTOR2>
86  bool queryVector(const VECTOR1& x, VECTOR2& out_y) const
87  {
88  const size_t n = size_t(x.size());
89  out_y.resize(n);
90  bool valid, anyValid = false;
91  for (size_t i = 0; i < n; i++)
92  {
93  query(x[i], out_y[i], valid);
94  if (valid) anyValid = true;
95  }
96  return anyValid;
97  }
98 };
99 
100 } // namespace mrpt::math
#define MRPT_START
Definition: exceptions.h:241
std::map< double, double > m_x2y
The placeholders for the data.
void clear()
Clears all stored points.
bool m_wrap2pi
Whether to wrap "y".
void appendXY(double x, double y)
Append a new point:
A (persistent) sequence of (x,y) coordinates, allowing queries of intermediate points through spline ...
This base provides a set of functions for maths stuff.
#define ASSERT_EQUAL_(__A, __B)
Assert comparing two values, reporting their actual values upon failure.
Definition: exceptions.h:137
void setWrap2pi(bool wrap)
If set to true, the interpolated data will be wrapped to ]-pi,pi].
bool queryVector(const VECTOR1 &x, VECTOR2 &out_y) const
As query, but for a whole vector at once.
#define MRPT_END
Definition: exceptions.h:245
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:30
#define DEFINE_SERIALIZABLE(class_name, NS)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
void setXY(const VECTOR &x, const VECTOR &y, bool clearPreviousContent=true)
Set all the data at once .
bool getWrap2pi()
Return the wrap property.
double & query(double x, double &y, bool &out_valid) const
Query an interpolation of the curve at some "x".
CSplineInterpolator1D(const VECTOR &initial_x, const VECTOR &initial_y, bool wrap2pi=false)
Constructor with optional initial values.



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020