]> git.mxchange.org Git - simgear.git/blob - simgear/misc/interpolator.hxx
Removal of PLIB/SG from SimGear
[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 General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
25
26 #include <simgear/props/props.hxx>
27 #include <simgear/structure/subsystem_mgr.hxx>
28
29 // TODO: support a callback upon interpolation completion so that user
30 // code can register another one immediately without worrying about
31 // timer aliasing.
32
33 class SGInterpolator : public SGSubsystem {
34 public:
35     SGInterpolator() { _list = 0; }
36     virtual void init() {}
37     virtual void update(double delta_time_sec);
38
39     // Simple method that interpolates a double property value from
40     // its current value (default of zero) to the specified target
41     // over the specified time.
42     void interpolate(SGPropertyNode* prop, double value, double dt_sec);
43
44     // More elaborate version that takes a pointer to lists of
45     // arbitrary size.
46     void interpolate(SGPropertyNode* prop, int nPoints,
47                      double* values, double* deltas);
48
49     // Cancels any interpolation of the specified property, leaving
50     // its value at the current (mid-interpolation) state.
51     void cancel(SGPropertyNode* prop);
52
53 private:
54     struct Interp {
55         SGPropertyNode_ptr target;
56         int nPoints;
57         double* curve; // time0, val0, time1, val1, ...
58         Interp* next;
59
60         ~Interp() { delete[] curve; }
61         double& dt(int i)  { return curve[2*i]; }
62         double& val(int i) { return curve[2*i + 1]; }
63     };
64     Interp* _list;
65
66     bool interp(Interp* rec, double dt);
67     void addNew(SGPropertyNode* prop, int nPoints);
68 };
69
70 #endif // __SG_INTERPOLATOR_HXX