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