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
5 // Written by Curtis Olson, started April 1998.
7 // Copyright (C) 1998 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
26 # include <simgear_config.h>
29 #include <simgear/compiler.h>
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/misc/sgstream.hxx>
35 #include <simgear/misc/sg_path.hxx>
36 #include <simgear/props/props.hxx>
38 #include "interpolater.hxx"
40 // Constructor -- starts with an empty table.
41 SGInterpTable::SGInterpTable()
45 SGInterpTable::SGInterpTable(const SGPropertyNode* interpolation)
49 std::vector<SGPropertyNode_ptr> entries = interpolation->getChildren("entry");
50 for (unsigned i = 0; i < entries.size(); ++i)
51 addEntry(entries[i]->getDoubleValue("ind", 0.0),
52 entries[i]->getDoubleValue("dep", 0.0));
55 // Constructor -- loads the interpolation table from the specified
57 SGInterpTable::SGInterpTable( const std::string& file )
59 SG_LOG( SG_MATH, SG_INFO, "Initializing Interpolator for " << file );
61 sg_gzifstream in( SGPath::fromUtf8(file) );
62 if ( !in.is_open() ) {
63 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
77 // Add an entry to the table.
78 void SGInterpTable::addEntry (double ind, double dep)
83 // Given an x value, linearly interpolate the y value from the table
84 double SGInterpTable::interpolate(double x) const
90 // Find the table bounds for the requested input.
91 Table::const_iterator upBoundIt = _table.upper_bound(x);
92 // points to a value outside the map. That is we are out of range.
94 if (upBoundIt == _table.end())
95 return _table.rbegin()->second;
97 // points to the first key must be lower
98 // use the first entry
99 if (upBoundIt == _table.begin())
100 return upBoundIt->second;
102 // we know that we do not stand at the beginning, so it is safe to do so
103 Table::const_iterator loBoundIt = upBoundIt;
106 // Just do linear interpolation.
107 double loBound = loBoundIt->first;
108 double upBound = upBoundIt->first;
109 double loVal = loBoundIt->second;
110 double upVal = upBoundIt->second;
112 // division by zero should not happen since the std::map
113 // has sorted out duplicate entries before. Also since we have a
114 // map, we know that we get different first values for different iterators
115 return loVal + (upVal - loVal)*(x - loBound)/(upBound - loBound);
120 SGInterpTable::~SGInterpTable() {