]> git.mxchange.org Git - simgear.git/blob - simgear/misc/interpolator.hxx
Property interpolator subsystem. A utility, primarily for Nasal scripts
[simgear.git] / simgear / misc / interpolator.hxx
1 #ifndef __SG_INTERPOLATOR_HXX
2 #define __SG_INTERPOLATOR_HXX
3
4 // SGInterpolator
5 //   Subsystem that manages smooth linear interpolation of property
6 //   values across multiple data points and arbitrary time intervals.
7
8 // Written by Andrew J. Ross, started December 2003
9 //
10 // Copyright (C) 2003  Andrew J. Ross - andy@plausible.org
11 //
12 // This library is free software; you can redistribute it and/or
13 // modify it under the terms of the GNU Library General Public
14 // License as published by the Free Software Foundation; either
15 // version 2 of the License, or (at your option) any later version.
16 //
17 // This library is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 // Library General Public License for more details.
21 //
22 // You should have received a copy of the GNU Library General Public
23 // License along with this library; if not, write to the
24 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 // Boston, MA  02111-1307, USA.
26
27 #include <simgear/props/props.hxx>
28 #include <simgear/structure/subsystem_mgr.hxx>
29
30 // TODO: support a callback upon interpolation completion so that user
31 // code can register another one immediately without worrying about
32 // timer aliasing.
33
34 class SGInterpolator : public SGSubsystem {
35 public:
36     SGInterpolator() { _list = 0; }
37     virtual void init() {}
38     virtual void update(double delta_time_sec);
39
40     // Simple method that interpolates a double property value from
41     // its current value (default of zero) to the specified target
42     // over the specified time.
43     void interpolate(SGPropertyNode* prop, double value, double dt_sec);
44
45     // More elaborate version that takes a pointer to lists of
46     // arbitrary size.
47     void interpolate(SGPropertyNode* prop, int nPoints,
48                      double* values, double* deltas);
49
50     // Cancels any interpolation of the specified property, leaving
51     // its value at the current (mid-interpolation) state.
52     void cancel(SGPropertyNode* prop);
53
54 private:
55     struct Interp {
56         SGPropertyNode_ptr target;
57         int nPoints;
58         double* curve; // time0, val0, time1, val1, ...
59         Interp* next;
60
61         ~Interp() { delete[] curve; }
62         double& dt(int i)  { return curve[2*i]; }
63         double& val(int i) { return curve[2*i + 1]; }
64     };
65     Interp* _list;
66
67     bool interp(Interp* rec, double dt);
68     void addNew(SGPropertyNode* prop, int nPoints);
69 };
70
71 #endif // __SG_INTERPOLATOR_HXX