]> git.mxchange.org Git - simgear.git/blob - simgear/math/interpolater.cxx
Merge branch 'maint' into next
[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 <string>
29
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/misc/sgstream.hxx>
32 #include <simgear/props/props.hxx>
33
34 #include "interpolater.hxx"
35
36 using std::string;
37
38 // Constructor -- starts with an empty table.
39 SGInterpTable::SGInterpTable()
40 {
41 }
42
43 SGInterpTable::SGInterpTable(const SGPropertyNode* interpolation) 
44 {
45     if (!interpolation)
46         return;
47     std::vector<SGPropertyNode_ptr> entries = interpolation->getChildren("entry");
48     for (unsigned i = 0; i < entries.size(); ++i)
49         addEntry(entries[i]->getDoubleValue("ind", 0.0),
50                  entries[i]->getDoubleValue("dep", 0.0));
51 }
52
53 // Constructor -- loads the interpolation table from the specified
54 // file
55 SGInterpTable::SGInterpTable( const string& file ) 
56 {
57     SG_LOG( SG_MATH, SG_INFO, "Initializing Interpolator for " << file );
58
59     sg_gzifstream in( file );
60     if ( !in.is_open() ) {
61         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
62         return;
63     }
64
65     in >> skipcomment;
66     while ( in ) {
67       double ind, dep;
68       in >> ind >> dep;
69       in >> skipws;
70       _table[ind] = dep;
71     }
72 }
73
74
75 // Add an entry to the table.
76 void SGInterpTable::addEntry (double ind, double dep)
77 {
78   _table[ind] = dep;
79 }
80
81 // Given an x value, linearly interpolate the y value from the table
82 double SGInterpTable::interpolate(double x) const
83 {
84   // Empty table??
85   if (_table.empty())
86     return 0;
87   
88   // Find the table bounds for the requested input.
89   Table::const_iterator upBoundIt = _table.upper_bound(x);
90   // points to a value outside the map. That is we are out of range.
91   // use the last entry
92   if (upBoundIt == _table.end())
93     return _table.rbegin()->second;
94   
95   // points to the first key must be lower
96   // use the first entry
97   if (upBoundIt == _table.begin())
98     return upBoundIt->second;
99   
100   // we know that we do not stand at the beginning, so it is safe to do so
101   Table::const_iterator loBoundIt = upBoundIt;
102   --loBoundIt;
103   
104   // Just do linear interpolation.
105   double loBound = loBoundIt->first;
106   double upBound = upBoundIt->first;
107   double loVal = loBoundIt->second;
108   double upVal = upBoundIt->second;
109   
110   // division by zero should not happen since the std::map
111   // has sorted out duplicate entries before. Also since we have a
112   // map, we know that we get different first values for different iterators
113   return loVal + (upVal - loVal)*(x - loBound)/(upBound - loBound);
114 }
115
116
117 // Destructor
118 SGInterpTable::~SGInterpTable() {
119 }
120
121