1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 Purpose: Models a lookup table
8 ------------- Copyright (C) 2001 Jon S. Berndt (jsb@hal-pc.org) -------------
10 This program is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free Software
12 Foundation; either version 2 of the License, or (at your option) any later
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 You should have received a copy of the GNU General Public License along with
21 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22 Place - Suite 330, Boston, MA 02111-1307, USA.
24 Further information about the GNU General Public License can also be found on
25 the world wide web at http://www.gnu.org.
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
32 --------------------------------------------------------------------------------
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
41 #if defined ( sgi ) && !defined( __GNUC__ )
47 static const char *IdSrc = "$Id$";
48 static const char *IdHdr = ID_TABLE;
50 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
56 FGTable::FGTable(int NRows, int NCols) : nRows(NRows), nCols(NCols)
64 if (debug_lvl & 2) cout << "Instantiated: FGTable" << endl;
67 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69 FGTable::FGTable(int NRows) : nRows(NRows), nCols(1)
76 if (debug_lvl & 2) cout << "Instantiated: FGTable" << endl;
79 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 double** FGTable::Allocate(void)
83 Data = new double*[nRows+1];
84 for (int r=0; r<=nRows; r++) {
85 Data[r] = new double[nCols+1];
86 for (int c=0; c<=nCols; c++) {
93 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97 for (int r=0; r<=nRows; r++) if (Data[r]) delete[] Data[r];
98 if (Data) delete[] Data;
99 if (debug_lvl & 2) cout << "Destroyed: FGTable" << endl;
102 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104 double FGTable::GetValue(double key)
106 double Factor, Value, Span;
109 for (r=1; r<=nRows; r++) if (Data[r][0] >= key) break;
110 r = r < 2 ? 2 : (r > nRows ? nRows : r);
112 // make sure denominator below does not go to zero.
114 Span = Data[r][0] - Data[r-1][0];
116 Factor = (key - Data[r-1][0]) / Span;
117 if (Factor > 1.0) Factor = 1.0;
122 Value = Factor*(Data[r][1] - Data[r-1][1]) + Data[r-1][1];
127 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129 double FGTable::GetValue(double rowKey, double colKey)
131 double rFactor, cFactor, col1temp, col2temp, Value;
134 for (r=1;r<=nRows;r++) if (Data[r][0] >= rowKey) break;
135 for (c=1;c<=nCols;c++) if (Data[0][c] >= colKey) break;
137 c = c < 2 ? 2 : (c > nCols ? nCols : c);
138 r = r < 2 ? 2 : (r > nRows ? nRows : r);
140 rFactor = (rowKey - Data[r-1][0]) / (Data[r][0] - Data[r-1][0]);
141 cFactor = (colKey - Data[0][c-1]) / (Data[0][c] - Data[0][c-1]);
143 if (rFactor > 1.0) rFactor = 1.0;
144 else if (rFactor < 0.0) rFactor = 0.0;
146 if (cFactor > 1.0) cFactor = 1.0;
147 else if (cFactor < 0.0) cFactor = 0.0;
149 col1temp = rFactor*(Data[r][c-1] - Data[r-1][c-1]) + Data[r-1][c-1];
150 col2temp = rFactor*(Data[r][c] - Data[r-1][c]) + Data[r-1][c];
152 Value = col1temp + cFactor*(col2temp - col1temp);
157 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159 void FGTable::operator<<(FGConfigFile& infile)
163 if (Type == tt1D) startRow = 1;
166 for (int r=startRow; r<=nRows; r++) {
167 for (int c=0; c<=nCols; c++) {
168 if (r != 0 || c != 0) {
169 infile >> Data[r][c];
175 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
177 FGTable& FGTable::operator<<(const double n)
179 Data[rowCounter][colCounter] = n;
180 if (colCounter == nCols) {
189 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191 FGTable& FGTable::operator<<(const int n)
197 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
199 FGTable& FGTable::operator<<(const double n)
205 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
207 void FGTable::Print(void)
211 if (Type == tt1D) startRow = 1;
214 cout.setf(ios::fixed); // set up output stream
217 for (int r=startRow; r<=nRows; r++) {
219 for (int c=0; c<=nCols; c++) {
220 if (r == 0 && c == 0) {
223 cout << Data[r][c] << " ";
228 cout.setf(0, ios::floatfield); // reset
231 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233 void FGTable::Debug(void)
235 //TODO: Add your source code here