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