]> git.mxchange.org Git - simgear.git/blob - simgear/misc/interpolator.cxx
Removal of PLIB/SG from SimGear
[simgear.git] / simgear / misc / interpolator.cxx
1 // Written by Andrew J. Ross, started December 2003
2 //
3 // Copyright (C) 2003  Andrew J. Ross - andy@plausible.org
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Library General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
19 #include "interpolator.hxx"
20
21 #include <simgear/math/SGMath.hxx>
22
23 void SGInterpolator::addNew(SGPropertyNode* prop, int nPoints)
24 {
25     // Set the property type to a double, if it isn't already, and
26     // make sure we aren't already managing this node.
27     prop->setDoubleValue(prop->getDoubleValue());
28     cancel(prop);
29
30     Interp* iterp = new Interp();
31     iterp->target = prop;
32     iterp->nPoints = nPoints;
33     iterp->curve = new double[2*nPoints];
34
35     // Dirty trick: leave the new value sitting in _list to avoid
36     // having to return a pointer to a private type.
37     iterp->next = _list;
38     _list = iterp;
39 }
40
41 void SGInterpolator::interpolate(SGPropertyNode* prop, int nPoints,
42                                  double* values, double* deltas)
43 {
44     addNew(prop, nPoints);
45     for(int i=0; i<nPoints; i++) {
46         _list->dt(i)  = deltas[i];
47         _list->val(i) = values[i];
48     }
49 }
50
51 void SGInterpolator::interpolate(SGPropertyNode* prop, double val, double dt)
52 {
53     addNew(prop, 1);
54     _list->dt(0) = dt;
55     _list->val(0) = val;
56 }
57
58 //
59 // Delete all the list elements where "expr" is true.
60 //
61 // Silly preprocessor hack to avoid writing the linked list code in
62 // two places.  You would think that an STL set would be the way to
63 // go, but I had terrible trouble getting it to work with the
64 // dynamically allocated "curve" member.  Frankly, this is easier to
65 // write, and the code is smaller to boot...
66 //
67 #define DELETE_WHERE(EXPR)\
68 Interp *p = _list, **last = &_list;      \
69 while(p) {                               \
70     if(EXPR) {                           \
71         *last = p->next;                 \
72         delete p;                        \
73         p = (*last) ? (*last)->next : 0; \
74     } else {                             \
75         last = &(p->next);               \
76         p = p->next; } }
77
78 void SGInterpolator::cancel(SGPropertyNode* prop)
79 {
80     DELETE_WHERE(p->target == prop)
81 }
82
83 void SGInterpolator::update(double dt)
84 {
85     DELETE_WHERE(interp(p, dt))
86 }
87
88 // This is the where the only "real" work happens.  Walk through the
89 // data points until we find one with some time left, slurp it up and
90 // repeat until we run out of dt.
91 bool SGInterpolator::interp(Interp* rec, double dt)
92 {
93     double val = rec->target->getDoubleValue();
94     int i;
95     for(i=0; i < rec->nPoints; i++) {
96         if(rec->dt(i) > 0 && dt < rec->dt(i)) {
97             val += (dt / rec->dt(i)) * (rec->val(i) - val);
98             rec->dt(i) -= dt;
99             break;
100         }
101         dt -= rec->dt(i);
102         val = rec->val(i);
103     }
104     rec->target->setDoubleValue(val);
105
106     // Return true if this one is done
107     return i == rec->nPoints;
108 }