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