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://mrpt.sourceforge.net/                            |
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                  */
00047                 class BASE_IMPEXP CSplineInterpolator1D : public mrpt::utils::CSerializable
00048                 {
00049                         // This must be added to any CSerializable derived class:
00050                         DEFINE_SERIALIZABLE( CSplineInterpolator1D )
00051 
00052                 private:
00053                         /** The placeholders for the data */
00054                         std::map<double,double> m_x2y;
00055 
00056                         bool    m_wrap2pi;              //!< Whether to wrap "y"
00057 
00058                 public:
00059                         /** Constructor with optional initial values. */
00060                         template <class VECTOR>
00061                         inline CSplineInterpolator1D(
00062                                 const VECTOR &initial_x,
00063                                 const VECTOR &initial_y,
00064                                 bool  wrap2pi = false ) : m_wrap2pi(wrap2pi)
00065                         {
00066                                 setXY(initial_x, initial_y);
00067                         }
00068 
00069                         /** Constructor */
00070                         CSplineInterpolator1D( bool  wrap2pi = false );
00071 
00072                         /** If set to true, the interpolated data will be wrapped to ]-pi,pi] */
00073                         void setWrap2pi(bool wrap) { m_wrap2pi=wrap; }
00074 
00075                         /** Return the wrap property */
00076                         bool getWrap2pi() { return m_wrap2pi; }
00077 
00078                         /** Set all the data at once .
00079                           *  The vectors must have the same length.
00080                          */
00081                         template <class VECTOR>
00082                         void setXY( const VECTOR &x, const VECTOR &y, bool clearPreviousContent = true )
00083                         {
00084                                 MRPT_START
00085                                 if (clearPreviousContent) m_x2y.clear();
00086                                 ASSERT_EQUAL_(x.size(),y.size())
00087                                 const size_t n = size_t(x.size());
00088                                 for (size_t i=0;i<n;i++)
00089                                         m_x2y[ x[i] ] = y[i];
00090                                 MRPT_END
00091                         }
00092 
00093                         /** Append a new point: */
00094                         void appendXY( double x, double y );
00095 
00096                         /** Clears all stored points */
00097                         void clear() { m_x2y.clear(); }
00098 
00099                         /** Query an interpolation of the curve at some "x".
00100                           *   The result is stored in "y". If the "x" point is out of range, "valid_out" is set to false.
00101                           *  \return A reference to "y"
00102                           * \sa queryVector
00103                           */
00104                         double &query( double x, double &y, bool &out_valid ) const;
00105 
00106                         /** As query, but for a whole vector at once.
00107                           *  \return false if there is at least one value that couldn't be interpolated (in this case the output is indeterminate).
00108                           * \sa query
00109                           */
00110                         template <class VECTOR1,class VECTOR2>
00111                         bool queryVector( const VECTOR1 &x, VECTOR2 &out_y ) const
00112                         {
00113                                 const size_t n = size_t(x.size());
00114                                 out_y.resize(n);
00115                                 bool valid, anyValid=false;
00116                                 for (size_t i =0;i<n;i++)
00117                                 {
00118                                         query( x[i], out_y[i], valid );
00119                                         if (valid) anyValid=true;
00120                                 }
00121                                 return anyValid;
00122                         }
00123 
00124                 };
00125 
00126         } // End of namespace
00127 } // End of namespace
00128 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:16:28 UTC 2011