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