interpolator.hxx

00001 #ifndef __SG_INTERPOLATOR_HXX
00002 #define __SG_INTERPOLATOR_HXX
00003 
00004 // SGInterpolator
00005 //   Subsystem that manages smooth linear interpolation of property
00006 //   values across multiple data points and arbitrary time intervals.
00007 
00008 // Written by Andrew J. Ross, started December 2003
00009 //
00010 // Copyright (C) 2003  Andrew J. Ross - andy@plausible.org
00011 //
00012 // This library is free software; you can redistribute it and/or
00013 // modify it under the terms of the GNU Library General Public
00014 // License as published by the Free Software Foundation; either
00015 // version 2 of the License, or (at your option) any later version.
00016 //
00017 // This library is distributed in the hope that it will be useful,
00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020 // Library General Public License for more details.
00021 //
00022 // You should have received a copy of the GNU General Public License
00023 // along with this program; if not, write to the Free Software
00024 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00025 
00026 #include <simgear/props/props.hxx>
00027 #include <simgear/structure/subsystem_mgr.hxx>
00028 
00029 // TODO: support a callback upon interpolation completion so that user
00030 // code can register another one immediately without worrying about
00031 // timer aliasing.
00032 
00033 class SGInterpolator : public SGSubsystem {
00034 public:
00035     SGInterpolator() { _list = 0; }
00036     virtual void init() {}
00037     virtual void update(double delta_time_sec);
00038 
00039     // Simple method that interpolates a double property value from
00040     // its current value (default of zero) to the specified target
00041     // over the specified time.
00042     void interpolate(SGPropertyNode* prop, double value, double dt_sec);
00043 
00044     // More elaborate version that takes a pointer to lists of
00045     // arbitrary size.
00046     void interpolate(SGPropertyNode* prop, int nPoints,
00047                      double* values, double* deltas);
00048 
00049     // Cancels any interpolation of the specified property, leaving
00050     // its value at the current (mid-interpolation) state.
00051     void cancel(SGPropertyNode* prop);
00052 
00053 private:
00054     struct Interp {
00055         SGPropertyNode_ptr target;
00056         int nPoints;
00057         double* curve; // time0, val0, time1, val1, ...
00058         Interp* next;
00059 
00060         ~Interp() { delete[] curve; }
00061         double& dt(int i)  { return curve[2*i]; }
00062         double& val(int i) { return curve[2*i + 1]; }
00063     };
00064     Interp* _list;
00065 
00066     bool interp(Interp* rec, double dt);
00067     void addNew(SGPropertyNode* prop, int nPoints);
00068 };
00069 
00070 #endif // __SG_INTERPOLATOR_HXX

Generated on Mon Dec 17 09:30:54 2007 for SimGear by  doxygen 1.5.1