]> git.mxchange.org Git - simgear.git/blob - simgear/math/interpolater.cxx
Converted to the LGPL licencing terms.
[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 #ifdef __MWERKS__
30 #include <stdlib.h> // for exit()
31 #endif
32
33 #include STL_STRING
34
35 #include <simgear/fg_zlib.h>
36 #include <simgear/debug/logstream.hxx>
37 #include <simgear/misc/fgstream.hxx>
38
39 #include "interpolater.hxx"
40
41
42 // Constructor -- loads the interpolation table from the specified
43 // file
44 fgINTERPTABLE::fgINTERPTABLE( const string& file ) {
45     FG_LOG( FG_MATH, FG_INFO, "Initializing Interpolator for " << file );
46
47     fg_gzifstream in( file );
48     if ( !in ) {
49         FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << file );
50         exit(-1);
51     }
52
53     size = 0;
54     in >> skipcomment;
55     while ( in ) {
56         if ( size < MAX_TABLE_SIZE ) {
57             in >> table[size][0] >> table[size][1];
58             size++;
59         } else {
60             FG_LOG( FG_MATH, FG_ALERT,
61                     "fgInterpolateInit(): Exceed max table size = "
62                     << MAX_TABLE_SIZE );
63             exit(-1);
64         }
65     }
66 }
67
68
69 // Given an x value, linearly interpolate the y value from the table
70 double fgINTERPTABLE::interpolate(double x) {
71     int i;
72     double y;
73
74     i = 0;
75
76     while ( (x > table[i][0]) && (i < size) ) {
77         i++;
78     }
79
80     // printf ("i = %d ", i);
81
82     if ( (i == 0) && (x < table[0][0]) ) {
83         FG_LOG( FG_MATH, FG_ALERT, 
84                 "fgInterpolateInit(): lookup error, x to small = " << x );
85         return(0.0);
86     }
87
88     if ( x > table[i][0] ) {
89         FG_LOG( FG_MATH, FG_ALERT, 
90                 "fgInterpolateInit(): lookup error, x to big = " << x );
91         return(0.0);
92     }
93
94     // y = y1 + (y0 - y1)(x - x1) / (x0 - x1)
95     y = table[i][1] + 
96         ( (table[i-1][1] - table[i][1]) * 
97           (x - table[i][0]) ) /
98         (table[i-1][0] - table[i][0]);
99
100     return(y);
101 }
102
103
104 // Destructor
105 fgINTERPTABLE::~fgINTERPTABLE( void ) {
106 }
107
108