Main MRPT website > C++ reference
MRPT logo
CSplineInterpolator1D.h
Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                       http://www.mrpt.org/                                |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef CSplineInterpolator1D_H
00029 #define CSplineInterpolator1D_H
00030 
00031 #include <mrpt/utils/utils_defs.h>
00032 #include <mrpt/utils/CSerializable.h>
00033 #include <mrpt/utils/stl_extensions.h>
00034 
00035 namespace mrpt
00036 {
00037         namespace math
00038         {
00039                 // This must be added to any CSerializable derived class:
00040                 DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE( CSplineInterpolator1D, mrpt::utils::CSerializable )
00041 
00042                 /** A (persistent) sequence of (x,y) coordinates, allowing queries of intermediate points through spline interpolation, where possible.
00043                   *  This class internally relies on mrpt::math::spline. Optionally the y coordinate can be set as wrapped in ]-pi,pi].
00044                   *  For querying interpolated points, see
00045                   * \ sa mrpt::math::spline, mrpt::poses::CPose3DInterpolator
00046                  * \ingroup interpolation_grp
00047                  */
00048                 class BASE_IMPEXP CSplineInterpolator1D : public mrpt::utils::CSerializable
00049                 {
00050                         // This must be added to any CSerializable derived class:
00051                         DEFINE_SERIALIZABLE( CSplineInterpolator1D )
00052 
00053                 private:
00054                         /** The placeholders for the data */
00055                         std::map<double,double> m_x2y;
00056 
00057                         bool    m_wrap2pi;              //!< Whether to wrap "y"
00058 
00059                 public:
00060                         /** Constructor with optional initial values. */
00061                         template <class VECTOR>
00062                         inline CSplineInterpolator1D(
00063                                 const VECTOR &initial_x,
00064                                 const VECTOR &initial_y,
00065                                 bool  wrap2pi = false ) : m_wrap2pi(wrap2pi)
00066                         {
00067                                 setXY(initial_x, initial_y);
00068                         }
00069 
00070                         /** Constructor */
00071                         CSplineInterpolator1D( bool  wrap2pi = false );
00072 
00073                         /** If set to true, the interpolated data will be wrapped to ]-pi,pi] */
00074                         void setWrap2pi(bool wrap) { m_wrap2pi=wrap; }
00075 
00076                         /** Return the wrap property */
00077                         bool getWrap2pi() { return m_wrap2pi; }
00078 
00079                         /** Set all the data at once .
00080                           *  The vectors must have the same length.
00081                          */
00082                         template <class VECTOR>
00083                         void setXY( const VECTOR &x, const VECTOR &y, bool clearPreviousContent = true )
00084                         {
00085                                 MRPT_START
00086                                 if (clearPreviousContent) m_x2y.clear();
00087                                 ASSERT_EQUAL_(x.size(),y.size())
00088                                 const size_t n = size_t(x.size());
00089                                 for (size_t i=0;i<n;i++)
00090                                         m_x2y[ x[i] ] = y[i];
00091                                 MRPT_END
00092                         }
00093 
00094                         /** Append a new point: */
00095                         void appendXY( double x, double y );
00096 
00097                         /** Clears all stored points */
00098                         void clear() { m_x2y.clear(); }
00099 
00100                         /** Query an interpolation of the curve at some "x".
00101                           *   The result is stored in "y". If the "x" point is out of range, "valid_out" is set to false.
00102                           *  \return A reference to "y"
00103                           * \sa queryVector
00104                           */
00105                         double &query( double x, double &y, bool &out_valid ) const;
00106 
00107                         /** As query, but for a whole vector at once.
00108                           *  \return false if there is at least one value that couldn't be interpolated (in this case the output is indeterminate).
00109                           * \sa query
00110                           */
00111                         template <class VECTOR1,class VECTOR2>
00112                         bool queryVector( const VECTOR1 &x, VECTOR2 &out_y ) const
00113                         {
00114                                 const size_t n = size_t(x.size());
00115                                 out_y.resize(n);
00116                                 bool valid, anyValid=false;
00117                                 for (size_t i =0;i<n;i++)
00118                                 {
00119                                         query( x[i], out_y[i], valid );
00120                                         if (valid) anyValid=true;
00121                                 }
00122                                 return anyValid;
00123                         }
00124 
00125                 };
00126 
00127         } // End of namespace
00128 } // End of namespace
00129 #endif



Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011