include/vamos/geometry/Spline.h

Go to the documentation of this file.
00001 //  Vamos Automotive Simulator
00002 //  Copyright (C) 2001--2004 Sam Varner
00003 //
00004 //  This program is free software; you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation; either version 2 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program; if not, write to the Free Software
00016 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 #ifndef _SPLINE_H_
00019 #define _SPLINE_H_
00020 
00021 #include <vamos/geometry/Interpolator.h>
00022 #include <vamos/geometry/Two_Point.h>
00023 #include <vector>
00024 
00025 namespace Vamos_Geometry
00026 {
00027   class Spline : public Interpolator
00028   {
00029     // The array of points to interpolate betwen.
00030     std::vector <Two_Point> m_points;
00031 
00032     // The array of calculated second derivatives.
00033     mutable std::vector <double> m_second_deriv;
00034 
00035     // The first derivative of the spline at the first point.
00036     double m_first_slope;
00037 
00038     // The first derivative of the spline at the last point.
00039     double m_last_slope;
00040 
00041     // True if the second derivatives have been calculated.
00042     mutable bool m_calculated;
00043 
00044     // The first derivative at the interpolated point calculated
00045     // during the last call to interpolate().
00046     mutable double m_slope;
00047 
00048     // Calculate the coefficients for interpolation.
00049     void calculate () const;
00050 
00051     // The segment index from the previous interpolation.
00052     mutable size_t m_last_index;
00053 
00054   public:
00055     // Construct an empty curve.
00056     Spline (double first_slope = 0.0, 
00057             double last_slope = 0.0);
00058 
00059     // Construct a cuvre from an array of points.
00060     Spline (const std::vector <Two_Point>& points,
00061             double first_slope = 0.0, 
00062             double last_slope = 0.0);
00063 
00064     // Add a point to the curve.
00065     void load (const Two_Point& point);
00066 
00067     // Add multiple points to the curve.
00068     void load (const std::vector <Two_Point>& points);
00069   
00070     // Remove all points from the curve.
00071     void clear ();
00072 
00073     // Remove points with x > LIMIT.
00074     void remove_greater (double limit);
00075 
00076     // Scale all of the x values by FACTOR.
00077     void scale (double factor);
00078 
00079     // Return the y value at the x value DIST
00080     double interpolate (double dist) const;
00081 
00082     // Return the normal to the tanget at DIST.
00083     Two_Point normal (double dist) const;
00084         
00085         // Return the number of control points.
00086     size_t size () const { return m_points.size (); }
00087   };
00088 }
00089 
00090 #endif

Generated on Thu Oct 19 04:05:48 2006 by  doxygen 1.4.6