Main MRPT website > C++ reference
MRPT logo
CSplineInterpolator1D.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef CSplineInterpolator1D_H
29 #define CSplineInterpolator1D_H
30 
31 #include <mrpt/utils/utils_defs.h>
34 
35 namespace mrpt
36 {
37  namespace math
38  {
39  // This must be added to any CSerializable derived class:
41 
42  /** A (persistent) sequence of (x,y) coordinates, allowing queries of intermediate points through spline interpolation, where possible.
43  * This class internally relies on mrpt::math::spline. Optionally the y coordinate can be set as wrapped in ]-pi,pi].
44  * For querying interpolated points, see
45  * \ sa mrpt::math::spline, mrpt::poses::CPose3DInterpolator
46  * \ingroup interpolation_grp
47  */
48  class BASE_IMPEXP CSplineInterpolator1D : public mrpt::utils::CSerializable
49  {
50  // This must be added to any CSerializable derived class:
52 
53  private:
54  /** The placeholders for the data */
55  std::map<double,double> m_x2y;
56 
57  bool m_wrap2pi; //!< Whether to wrap "y"
58 
59  public:
60  /** Constructor with optional initial values. */
61  template <class VECTOR>
63  const VECTOR &initial_x,
64  const VECTOR &initial_y,
65  bool wrap2pi = false ) : m_wrap2pi(wrap2pi)
66  {
67  setXY(initial_x, initial_y);
68  }
69 
70  /** Constructor */
71  CSplineInterpolator1D( bool wrap2pi = false );
72 
73  /** If set to true, the interpolated data will be wrapped to ]-pi,pi] */
74  void setWrap2pi(bool wrap) { m_wrap2pi=wrap; }
75 
76  /** Return the wrap property */
77  bool getWrap2pi() { return m_wrap2pi; }
78 
79  /** Set all the data at once .
80  * The vectors must have the same length.
81  */
82  template <class VECTOR>
83  void setXY( const VECTOR &x, const VECTOR &y, bool clearPreviousContent = true )
84  {
86  if (clearPreviousContent) m_x2y.clear();
87  ASSERT_EQUAL_(x.size(),y.size())
88  const size_t n = size_t(x.size());
89  for (size_t i=0;i<n;i++)
90  m_x2y[ x[i] ] = y[i];
91  MRPT_END
92  }
93 
94  /** Append a new point: */
95  void appendXY( double x, double y );
96 
97  /** Clears all stored points */
98  void clear() { m_x2y.clear(); }
99 
100  /** Query an interpolation of the curve at some "x".
101  * The result is stored in "y". If the "x" point is out of range, "valid_out" is set to false.
102  * \return A reference to "y"
103  * \sa queryVector
104  */
105  double &query( double x, double &y, bool &out_valid ) const;
106 
107  /** As query, but for a whole vector at once.
108  * \return false if there is at least one value that couldn't be interpolated (in this case the output is indeterminate).
109  * \sa query
110  */
111  template <class VECTOR1,class VECTOR2>
112  bool queryVector( const VECTOR1 &x, VECTOR2 &out_y ) const
113  {
114  const size_t n = size_t(x.size());
115  out_y.resize(n);
116  bool valid, anyValid=false;
117  for (size_t i =0;i<n;i++)
118  {
119  query( x[i], out_y[i], valid );
120  if (valid) anyValid=true;
121  }
122  return anyValid;
123  }
124 
125  };
126 
127  } // End of namespace
128 } // End of namespace
129 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013