]> git.mxchange.org Git - simgear.git/blob - simgear/math/interpolater.cxx
Updates to remove unneeded and old version of zlib source.
[simgear.git] / simgear / math / interpolater.cxx
1 //
2 // interpolater.cxx -- routines to handle linear interpolation from a table of
3 //                     x,y   The table must be sorted by "x" in ascending order
4 //
5 // Written by Curtis Olson, started April 1998.
6 //
7 // Copyright (C) 1998  Curtis L. Olson  - http://www.flightgear.org/~curt
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Library General Public
11 // License as published by the Free Software Foundation; either
12 // version 2 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 // Library General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22 //
23 // $Id$
24
25
26 #include <simgear/compiler.h>
27
28 #include <stdlib.h> // for exit()
29
30 #include STL_STRING
31
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/misc/sgstream.hxx>
34
35 #include "interpolater.hxx"
36
37 SG_USING_STD(string);
38
39 // Constructor -- starts with an empty table.
40 SGInterpTable::SGInterpTable()
41     : size(0)
42 {
43 }
44
45
46 // Constructor -- loads the interpolation table from the specified
47 // file
48 SGInterpTable::SGInterpTable( const string& file ) 
49   : size(0)
50 {
51     SG_LOG( SG_MATH, SG_INFO, "Initializing Interpolator for " << file );
52
53     sg_gzifstream in( file );
54     if ( !in.is_open() ) {
55         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
56         exit(-1);
57     }
58
59     in >> skipcomment;
60     while ( in ) {
61       double ind, dep;
62       in >> ind >> dep;
63       in >> skipws;
64       table.push_back(Entry(ind, dep));
65       size++;
66     }
67 }
68
69
70 // Add an entry to the table.
71 void SGInterpTable::addEntry (double ind, double dep)
72 {
73   table.push_back(Entry(ind,dep));
74   size++;
75 }
76
77
78 // Given an x value, linearly interpolate the y value from the table
79 double SGInterpTable::interpolate(double x) const
80 {
81     int i;
82     double y;
83
84     if (size == 0.0) {
85       return 0.0;
86     }
87
88     i = 0;
89
90     while ( (i < size) && (x > table[i].ind) ) {
91         // cout << "  i = " << i << " table[i].ind = " << table[i].ind << endl;
92         // cout << "  size = " << size << endl;
93         i++;
94     }
95
96     // printf ("i = %d ", i);
97
98     if ( i <= 0 ) {
99         SG_LOG( SG_MATH, SG_DEBUG, 
100                 "interpolate(): lookup error, x to small = " << x );
101         return table[0].dep;
102     }
103
104     // cout << " table[size-1].ind = " << table[size-1].ind << endl;
105     if ( i >= size ) {
106         SG_LOG( SG_MATH, SG_DEBUG, 
107                 "interpolate(): lookup error, x to big = " << x );
108         return table[size-1].dep;
109     }
110
111     // y = y1 + (y0 - y1)(x - x1) / (x0 - x1)
112     y = table[i].dep + 
113         ( (table[i-1].dep - table[i].dep) * 
114           (x - table[i].ind) ) /
115         (table[i-1].ind - table[i].ind);
116
117     return(y);
118 }
119
120
121 // Destructor
122 SGInterpTable::~SGInterpTable() {
123 }
124
125