]> git.mxchange.org Git - simgear.git/blob - simgear/math/interpolater.cxx
ccb71d6be5b783e5aea6e06750040e5e106ba306
[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  - curt@me.umn.edu
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 // depricated - #include <simgear/sg_zlib.h>
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/misc/sgstream.hxx>
36
37 #include "interpolater.hxx"
38
39 SG_USING_STD(string);
40
41 // Constructor -- starts with an empty table.
42 SGInterpTable::SGInterpTable()
43     : size(0)
44 {
45 }
46
47
48 // Constructor -- loads the interpolation table from the specified
49 // file
50 SGInterpTable::SGInterpTable( const string& file ) 
51   : size(0)
52 {
53     SG_LOG( SG_MATH, SG_INFO, "Initializing Interpolator for " << file );
54
55     sg_gzifstream in( file );
56     if ( !in.is_open() ) {
57         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
58         exit(-1);
59     }
60
61     in >> skipcomment;
62     while ( in ) {
63       double ind, dep;
64       in >> ind >> dep;
65       in >> skipws;
66       table.push_back(Entry(ind, dep));
67       size++;
68     }
69 }
70
71
72 // Add an entry to the table.
73 void SGInterpTable::addEntry (double ind, double dep)
74 {
75   table.push_back(Entry(ind,dep));
76   size++;
77 }
78
79
80 // Given an x value, linearly interpolate the y value from the table
81 double SGInterpTable::interpolate(double x) const
82 {
83     int i;
84     double y;
85
86     if (size == 0.0) {
87       return 0.0;
88     }
89
90     i = 0;
91
92     while ( (i < size) && (x > table[i].ind) ) {
93         // cout << "  i = " << i << " table[i].ind = " << table[i].ind << endl;
94         // cout << "  size = " << size << endl;
95         i++;
96     }
97
98     // printf ("i = %d ", i);
99
100     if ( i <= 0 ) {
101         SG_LOG( SG_MATH, SG_DEBUG, 
102                 "interpolate(): lookup error, x to small = " << x );
103         return table[0].dep;
104     }
105
106     // cout << " table[size-1].ind = " << table[size-1].ind << endl;
107     if ( i >= size ) {
108         SG_LOG( SG_MATH, SG_DEBUG, 
109                 "interpolate(): lookup error, x to big = " << x );
110         return table[size-1].dep;
111     }
112
113     // y = y1 + (y0 - y1)(x - x1) / (x0 - x1)
114     y = table[i].dep + 
115         ( (table[i-1].dep - table[i].dep) * 
116           (x - table[i].ind) ) /
117         (table[i-1].ind - table[i].ind);
118
119     return(y);
120 }
121
122
123 // Destructor
124 SGInterpTable::~SGInterpTable() {
125 }
126
127