]> git.mxchange.org Git - simgear.git/blob - simgear/math/interpolater.cxx
Ignore generated binaries.
[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 Library General Public
20 // License along with this library; if not, write to the
21 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 // Boston, MA  02111-1307, USA.
23 //
24 // $Id$
25
26
27 #include <simgear/compiler.h>
28
29 #include <stdlib.h> // for exit()
30
31 #include STL_STRING
32
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/misc/sgstream.hxx>
35
36 #include "interpolater.hxx"
37
38 SG_USING_STD(string);
39
40 // Constructor -- starts with an empty table.
41 SGInterpTable::SGInterpTable()
42     : size(0)
43 {
44 }
45
46
47 // Constructor -- loads the interpolation table from the specified
48 // file
49 SGInterpTable::SGInterpTable( const string& file ) 
50   : size(0)
51 {
52     SG_LOG( SG_MATH, SG_INFO, "Initializing Interpolator for " << file );
53
54     sg_gzifstream in( file );
55     if ( !in.is_open() ) {
56         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
57         exit(-1);
58     }
59
60     in >> skipcomment;
61     while ( in ) {
62       double ind, dep;
63       in >> ind >> dep;
64       in >> skipws;
65       table.push_back(Entry(ind, dep));
66       size++;
67     }
68 }
69
70
71 // Add an entry to the table.
72 void SGInterpTable::addEntry (double ind, double dep)
73 {
74   table.push_back(Entry(ind,dep));
75   size++;
76 }
77
78
79 // Given an x value, linearly interpolate the y value from the table
80 double SGInterpTable::interpolate(double x) const
81 {
82     int i;
83     double y;
84
85     if (size == 0.0) {
86       return 0.0;
87     }
88
89     i = 0;
90
91     while ( (i < size) && (x > table[i].ind) ) {
92         // cout << "  i = " << i << " table[i].ind = " << table[i].ind << endl;
93         // cout << "  size = " << size << endl;
94         i++;
95     }
96
97     // printf ("i = %d ", i);
98
99     if ( i <= 0 ) {
100         SG_LOG( SG_MATH, SG_DEBUG, 
101                 "interpolate(): lookup error, x to small = " << x );
102         return table[0].dep;
103     }
104
105     // cout << " table[size-1].ind = " << table[size-1].ind << endl;
106     if ( i >= size ) {
107         SG_LOG( SG_MATH, SG_DEBUG, 
108                 "interpolate(): lookup error, x to big = " << x );
109         return table[size-1].dep;
110     }
111
112     // y = y1 + (y0 - y1)(x - x1) / (x0 - x1)
113     y = table[i].dep + 
114         ( (table[i-1].dep - table[i].dep) * 
115           (x - table[i].ind) ) /
116         (table[i-1].ind - table[i].ind);
117
118     return(y);
119 }
120
121
122 // Destructor
123 SGInterpTable::~SGInterpTable() {
124 }
125
126