]> git.mxchange.org Git - flightgear.git/blob - Lib/Math/interpolater.cxx
Merge FG_Lib as subdirectory
[flightgear.git] / Lib / 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 program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 // 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., 675 Mass Ave, Cambridge, MA 02139, USA.
22 //
23 // $Id$
24 // (Log is kept at end of this file)
25
26
27 #include <Include/compiler.h>
28
29 #include STL_STRING
30
31 #include <Debug/logstream.hxx>
32 #include <Include/fg_zlib.h>
33 #include <Misc/fgstream.hxx>
34
35 #include "interpolater.hxx"
36
37
38 // Constructor -- loads the interpolation table from the specified
39 // file
40 fgINTERPTABLE::fgINTERPTABLE( const string& file ) {
41     FG_LOG( FG_MATH, FG_INFO, "Initializing Interpolator for " << file );
42
43     fg_gzifstream in( file );
44     if ( !in ) {
45         FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << file );
46         exit(-1);
47     }
48
49     size = 0;
50     in >> skipcomment;
51     while ( ! in.eof() ){
52         if ( size < MAX_TABLE_SIZE ) {
53             in >> table[size][0] >> table[size][1];
54             size++;
55         } else {
56             FG_LOG( FG_MATH, FG_ALERT,
57                     "fgInterpolateInit(): Exceed max table size = "
58                     << MAX_TABLE_SIZE );
59             exit(-1);
60         }
61     }
62 }
63
64
65 // Given an x value, linearly interpolate the y value from the table
66 double fgINTERPTABLE::interpolate(double x) {
67     int i;
68     double y;
69
70     i = 0;
71
72     while ( (x > table[i][0]) && (i < size) ) {
73         i++;
74     }
75
76     // printf ("i = %d ", i);
77
78     if ( (i == 0) && (x < table[0][0]) ) {
79         FG_LOG( FG_MATH, FG_ALERT, 
80                 "fgInterpolateInit(): lookup error, x to small = " << x );
81         return(0.0);
82     }
83
84     if ( x > table[i][0] ) {
85         FG_LOG( FG_MATH, FG_ALERT, 
86                 "fgInterpolateInit(): lookup error, x to big = " << x );
87         return(0.0);
88     }
89
90     // y = y1 + (y0 - y1)(x - x1) / (x0 - x1)
91     y = table[i][1] + 
92         ( (table[i-1][1] - table[i][1]) * 
93           (x - table[i][0]) ) /
94         (table[i-1][0] - table[i][0]);
95
96     return(y);
97 }
98
99
100 // Destructor
101 fgINTERPTABLE::~fgINTERPTABLE( void ) {
102 }
103
104
105 // $Log$
106 // Revision 1.7  1999/02/26 22:08:03  curt
107 // Added initial support for native SGI compilers.
108 //
109 // Revision 1.6  1999/01/27 04:46:16  curt
110 // Portability tweaks by Bernie Bright.
111 //
112 // Revision 1.5  1998/11/06 21:17:27  curt
113 // Converted to new logstream debugging facility.  This allows release
114 // builds with no messages at all (and no performance impact) by using
115 // the -DFG_NDEBUG flag.
116 //
117 // Revision 1.4  1998/05/13 18:24:25  curt
118 // Wrapped zlib calls so zlib can be optionally disabled.
119 //
120 // Revision 1.3  1998/04/25 15:05:01  curt
121 // Changed "r" to "rb" in gzopen() options.  This fixes bad behavior in win32.
122 //
123 // Revision 1.2  1998/04/22 13:18:10  curt
124 // C++ - ified comments.  Make file open errors fatal.
125 //
126 // Revision 1.1  1998/04/21 19:14:23  curt
127 // Modified Files:
128 //     Makefile.am Makefile.in
129 // Added Files:
130 //     interpolater.cxx interpolater.hxx
131 //