]> git.mxchange.org Git - simgear.git/blob - simgear/misc/interpolator.cxx
Don't waste space with too huge stl containers.
[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 void SGInterpolator::addNew(SGPropertyNode* prop, int nPoints)
22 {
23     // Set the property type to a double, if it isn't already, and
24     // make sure we aren't already managing this node.
25     prop->setDoubleValue(prop->getDoubleValue());
26     cancel(prop);
27
28     Interp* iterp = new Interp();
29     iterp->target = prop;
30     iterp->nPoints = nPoints;
31     iterp->curve = new double[2*nPoints];
32
33     // Dirty trick: leave the new value sitting in _list to avoid
34     // having to return a pointer to a private type.
35     iterp->next = _list;
36     _list = iterp;
37 }
38
39 void SGInterpolator::interpolate(SGPropertyNode* prop, int nPoints,
40                                  double* values, double* deltas)
41 {
42     addNew(prop, nPoints);
43     for(int i=0; i<nPoints; i++) {
44         _list->dt(i)  = deltas[i];
45         _list->val(i) = values[i];
46     }
47 }
48
49 void SGInterpolator::interpolate(SGPropertyNode* prop, double val, double dt)
50 {
51     addNew(prop, 1);
52     _list->dt(0) = dt;
53     _list->val(0) = val;
54 }
55
56 //
57 // Delete all the list elements where "expr" is true.
58 //
59 // Silly preprocessor hack to avoid writing the linked list code in
60 // two places.  You would think that an STL set would be the way to
61 // go, but I had terrible trouble getting it to work with the
62 // dynamically allocated "curve" member.  Frankly, this is easier to
63 // write, and the code is smaller to boot...
64 //
65 #define DELETE_WHERE(EXPR)\
66 Interp *p = _list, **last = &_list;      \
67 while(p) {                               \
68     if(EXPR) {                           \
69         *last = p->next;                 \
70         delete p;                        \
71         p = (*last) ? (*last)->next : 0; \
72     } else {                             \
73         last = &(p->next);               \
74         p = p->next; } }
75
76 void SGInterpolator::cancel(SGPropertyNode* prop)
77 {
78     DELETE_WHERE(p->target == prop)
79 }
80
81 void SGInterpolator::update(double dt)
82 {
83     DELETE_WHERE(interp(p, dt))
84 }
85
86 // This is the where the only "real" work happens.  Walk through the
87 // data points until we find one with some time left, slurp it up and
88 // repeat until we run out of dt.
89 bool SGInterpolator::interp(Interp* rec, double dt)
90 {
91     double val = rec->target->getDoubleValue();
92     int i;
93     for(i=0; i < rec->nPoints; i++) {
94         if(rec->dt(i) > 0 && dt < rec->dt(i)) {
95             val += (dt / rec->dt(i)) * (rec->val(i) - val);
96             rec->dt(i) -= dt;
97             break;
98         }
99         dt -= rec->dt(i);
100         val = rec->val(i);
101     }
102     rec->target->setDoubleValue(val);
103
104     // Return true if this one is done
105     return i == rec->nPoints;
106 }