head 1.4; access; symbols; locks; strict; comment @# @; 1.4 date 2002.09.07.01.50.38; author curt; state Exp; branches; next 1.3; 1.3 date 2002.08.31.19.01.54; author curt; state Exp; branches; next 1.2; 1.2 date 2002.08.31.18.54.00; author curt; state Exp; branches; next 1.1; 1.1 date 2002.02.15.17.21.56; author curt; state Exp; branches; next ; desc @@ 1.4 log @Updated doxygen docs. @ text @
Main Page Class Hierarchy Compound List File List Compound Members File Members

# interpolater.cxx

    00001 //
    00002 // interpolater.cxx -- routines to handle linear interpolation from a table of
    00003 //                     x,y   The table must be sorted by "x" in ascending order
    00004 //
    00005 // Written by Curtis Olson, started April 1998.
    00006 //
    00007 // Copyright (C) 1998  Curtis L. Olson  - curt@@me.umn.edu
    00008 //
    00009 // This library is free software; you can redistribute it and/or
    00010 // modify it under the terms of the GNU Library General Public
    00011 // License as published by the Free Software Foundation; either
    00012 // version 2 of the License, or (at your option) any later version.
    00013 //
    00014 // This library is distributed in the hope that it will be useful,
    00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00017 // Library General Public License for more details.
    00018 //
    00019 // You should have received a copy of the GNU Library General Public
    00020 // License along with this library; if not, write to the
    00021 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00022 // Boston, MA  02111-1307, USA.
    00023 //
    00024 // $Id: interpolater.cxx,v 1.11 2001/06/30 02:01:22 curt Exp $
    00025
    00026
    00027 #include <simgear/compiler.h>
    00028
    00029 #include <stdlib.h> // for exit()
    00030
    00031 #include STL_STRING
    00032
    00033 // depricated - #include <simgear/sg_zlib.h>
    00034 #include <simgear/debug/logstream.hxx>
    00035 #include <simgear/misc/sgstream.hxx>
    00036
    00037 #include "interpolater.hxx"
    00038
    00039 SG_USING_STD(string);
    00040
    00041 // Constructor -- starts with an empty table.
    00042 SGInterpTable::SGInterpTable()
    00043     : size(0)
    00044 {
    00045 }
    00046
    00047
    00048 // Constructor -- loads the interpolation table from the specified
    00049 // file
    00050 SGInterpTable::SGInterpTable( const string& file )
    00051   : size(0)
    00052 {
    00053     SG_LOG( SG_MATH, SG_INFO, "Initializing Interpolator for " << file );
    00054
    00055     sg_gzifstream in( file );
    00056     if ( !in.is_open() ) {
    00057         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
    00058         exit(-1);
    00059     }
    00060
    00061     in >> skipcomment;
    00062     while ( in ) {
    00063       double ind, dep;
    00064       in >> ind >> dep;
    00065       in >> skipws;
    00066       table.push_back(Entry(ind, dep));
    00067       size++;
    00068     }
    00069 }
    00070
    00071
    00072 // Add an entry to the table.
    00073 void SGInterpTable::addEntry (double ind, double dep)
    00074 {
    00075   table.push_back(Entry(ind,dep));
    00076   size++;
    00077 }
    00078
    00079
    00080 // Given an x value, linearly interpolate the y value from the table
    00081 double SGInterpTable::interpolate(double x) const
    00082 {
    00083     int i;
    00084     double y;
    00085
    00086     if (size == 0.0) {
    00087       return 0.0;
    00088     }
    00089
    00090     i = 0;
    00091
    00092     while ( (i < size) && (x > table[i].ind) ) {
    00093         // cout << "  i = " << i << " table[i].ind = " << table[i].ind << endl;
    00094         // cout << "  size = " << size << endl;
    00095         i++;
    00096     }
    00097
    00098     // printf ("i = %d ", i);
    00099
    00100     if ( i <= 0 ) {
    00101         SG_LOG( SG_MATH, SG_DEBUG,
    00102                 "interpolate(): lookup error, x to small = " << x );
    00103         return table[0].dep;
    00104     }
    00105
    00106     // cout << " table[size-1].ind = " << table[size-1].ind << endl;
    00107     if ( i >= size ) {
    00108         SG_LOG( SG_MATH, SG_DEBUG,
    00109                 "interpolate(): lookup error, x to big = " << x );
    00110         return table[size-1].dep;
    00111     }
    00112
    00113     // y = y1 + (y0 - y1)(x - x1) / (x0 - x1)
    00114     y = table[i].dep +
    00115         ( (table[i-1].dep - table[i].dep) *
    00116           (x - table[i].ind) ) /
    00117         (table[i-1].ind - table[i].ind);
    00118
    00119     return(y);
    00120 }
    00121
    00122
    00123 // Destructor
    00124 SGInterpTable::~SGInterpTable() {
    00125 }
    00126
    00127

@ 1.3 log @Oops, try again. @ text @d136 1 a136 1