]> git.mxchange.org Git - simgear.git/blob - Math/interpolater.cxx
FreeBSD support.
[simgear.git] / 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 <string>
28
29 #include <Debug/logstream.hxx>
30 #include <Include/fg_zlib.h>
31 #include <Misc/fgstream.hxx>
32
33 #include "interpolater.hxx"
34
35
36 // Constructor -- loads the interpolation table from the specified
37 // file
38 fgINTERPTABLE::fgINTERPTABLE( const string& file ) {
39     string fgfile, line;
40
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.5  1998/11/06 21:17:27  curt
107 // Converted to new logstream debugging facility.  This allows release
108 // builds with no messages at all (and no performance impact) by using
109 // the -DFG_NDEBUG flag.
110 //
111 // Revision 1.4  1998/05/13 18:24:25  curt
112 // Wrapped zlib calls so zlib can be optionally disabled.
113 //
114 // Revision 1.3  1998/04/25 15:05:01  curt
115 // Changed "r" to "rb" in gzopen() options.  This fixes bad behavior in win32.
116 //
117 // Revision 1.2  1998/04/22 13:18:10  curt
118 // C++ - ified comments.  Make file open errors fatal.
119 //
120 // Revision 1.1  1998/04/21 19:14:23  curt
121 // Modified Files:
122 //     Makefile.am Makefile.in
123 // Added Files:
124 //     interpolater.cxx interpolater.hxx
125 //