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__ ) && (_COMPILER_VERSION < 740)
51 static const char *IdSrc = "$Id$";
52 static const char *IdHdr = ID_TABLE;
54 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
59 FGTable::FGTable(int NRows, int NCols, int NTables)
60 : nRows(NTables), nCols(1), nTables(NTables)
66 Data = Allocate(); // this data array will contain the keys for the associated tables
67 Tables.reserve(nTables);
68 for (int i=0; i<nTables; i++) Tables.push_back(FGTable(NRows, NCols));
73 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 FGTable::FGTable(int NRows, int NCols) : nRows(NRows), nCols(NCols)
81 } else if (NCols == 1) {
86 cerr << "FGTable cannot accept 'Rows=0'" << endl;
90 lastRowIndex=lastColumnIndex=2;
94 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96 FGTable::FGTable(int NRows) : nRows(NRows), nCols(1)
104 lastRowIndex=lastColumnIndex=2;
107 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109 FGTable::FGTable(const FGTable& t)
112 colCounter = t.colCounter;
113 rowCounter = t.rowCounter;
114 tableCounter = t.tableCounter;
122 for (int r=0; r<=nRows; r++) {
123 for (int c=0; c<=nCols; c++) {
124 Data[r][c] = t.Data[r][c];
127 lastRowIndex = t.lastRowIndex;
128 lastColumnIndex = t.lastColumnIndex;
129 lastTableIndex = t.lastTableIndex;
132 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134 double** FGTable::Allocate(void)
136 Data = new double*[nRows+1];
137 for (int r=0; r<=nRows; r++) {
138 Data[r] = new double[nCols+1];
139 for (int c=0; c<=nCols; c++) {
146 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 if (nTables > 0) Tables.clear();
151 for (int r=0; r<=nRows; r++) if (Data[r]) delete[] Data[r];
152 if (Data) delete[] Data;
156 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
158 double FGTable::GetValue(double key)
160 double Factor, Value, Span;
163 //if the key is off the end of the table, just return the
164 //end-of-table value, do not extrapolate
165 if( key <= Data[1][0] ) {
167 //cout << "Key underneath table: " << key << endl;
169 } else if ( key >= Data[nRows][0] ) {
171 //cout << "Key over table: " << key << endl;
172 return Data[nRows][1];
175 // the key is somewhere in the middle, search for the right breakpoint
176 // assume the correct breakpoint has not changed since last frame or
177 // has only changed very little
179 if ( r > 2 && Data[r-1][0] > key ) {
180 while( Data[r-1][0] > key && r > 2) { r--; }
181 } else if ( Data[r][0] < key ) {
182 while( Data[r][0] <= key && r <= nRows) { r++; }
186 // make sure denominator below does not go to zero.
188 Span = Data[r][0] - Data[r-1][0];
190 Factor = (key - Data[r-1][0]) / Span;
191 if (Factor > 1.0) Factor = 1.0;
196 Value = Factor*(Data[r][1] - Data[r-1][1]) + Data[r-1][1];
201 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204 double FGTable::GetValue(double rowKey, double colKey)
206 double rFactor, cFactor, col1temp, col2temp, Value;
208 int c=lastColumnIndex;
210 if ( r > 2 && Data[r-1][0] > rowKey ) {
211 while ( Data[r-1][0] > rowKey && r > 2) { r--; }
212 } else if ( Data[r][0] < rowKey ) {
213 // cout << Data[r][0] << endl;
214 while ( r <= nRows && Data[r][0] <= rowKey ) { r++; }
215 if ( r > nRows ) r = nRows;
218 if ( c > 2 && Data[0][c-1] > colKey ) {
219 while( Data[0][c-1] > colKey && c > 2) { c--; }
220 } else if ( Data[0][c] < colKey ) {
221 while( Data[0][c] <= colKey && c <= nCols) { c++; }
222 if ( c > nCols ) c = nCols;
228 rFactor = (rowKey - Data[r-1][0]) / (Data[r][0] - Data[r-1][0]);
229 cFactor = (colKey - Data[0][c-1]) / (Data[0][c] - Data[0][c-1]);
231 if (rFactor > 1.0) rFactor = 1.0;
232 else if (rFactor < 0.0) rFactor = 0.0;
234 if (cFactor > 1.0) cFactor = 1.0;
235 else if (cFactor < 0.0) cFactor = 0.0;
237 col1temp = rFactor*(Data[r][c-1] - Data[r-1][c-1]) + Data[r-1][c-1];
238 col2temp = rFactor*(Data[r][c] - Data[r-1][c]) + Data[r-1][c];
240 Value = col1temp + cFactor*(col2temp - col1temp);
245 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 double FGTable::GetValue(double rowKey, double colKey, double tableKey)
249 double Factor, Value, Span;
252 //if the key is off the end (or before the beginning) of the table,
253 // just return the boundary-table value, do not extrapolate
255 if( tableKey <= Data[1][1] ) {
257 return Tables[0].GetValue(rowKey, colKey);
258 } else if ( tableKey >= Data[nRows][1] ) {
260 return Tables[nRows-1].GetValue(rowKey, colKey);
263 // the key is somewhere in the middle, search for the right breakpoint
264 // assume the correct breakpoint has not changed since last frame or
265 // has only changed very little
267 if ( r > 2 && Data[r-1][1] > tableKey ) {
268 while( Data[r-1][1] > tableKey && r > 2) { r--; }
269 } else if ( Data[r][1] < tableKey ) {
270 while( Data[r][1] <= tableKey && r <= nRows) { r++; }
274 // make sure denominator below does not go to zero.
276 Span = Data[r][1] - Data[r-1][1];
278 Factor = (tableKey - Data[r-1][1]) / Span;
279 if (Factor > 1.0) Factor = 1.0;
284 Value = Factor*(Tables[r-1].GetValue(rowKey, colKey) - Tables[r-2].GetValue(rowKey, colKey))
285 + Tables[r-1].GetValue(rowKey, colKey);
290 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
292 void FGTable::operator<<(FGConfigFile& infile)
298 if (Type == tt1D || Type == tt3D) startRow = 1;
299 if (Type == tt3D) startCol = 1;
301 for (int r=startRow; r<=nRows; r++) {
302 for (int c=startCol; c<=nCols; c++) {
303 if (r != 0 || c != 0) {
304 infile >> Data[r][c];
306 Tables[tableCtr] << infile;
314 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
316 FGTable& FGTable::operator<<(const double n)
318 Data[rowCounter][colCounter] = n;
319 if (colCounter == nCols) {
328 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
330 FGTable& FGTable::operator<<(const int n)
336 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
338 void FGTable::Print(void)
343 if (Type == tt1D || Type == tt3D) startRow = 1;
344 if (Type == tt3D) startCol = 1;
346 #if defined (sgi) && !defined(__GNUC__) && (_COMPILER_VERSION < 740)
347 unsigned long flags = cout.setf(ios::fixed);
349 ios::fmtflags flags = cout.setf(ios::fixed); // set up output stream
353 for (int r=startRow; r<=nRows; r++) {
355 for (int c=startCol; c<=nCols; c++) {
356 if (r == 0 && c == 0) {
359 cout << Data[r][c] << " ";
368 cout.setf(flags); // reset
371 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
372 // The bitmasked value choices are as follows:
373 // unset: In this case (the default) JSBSim would only print
374 // out the normally expected messages, essentially echoing
375 // the config files as they are read. If the environment
376 // variable is not set, debug_lvl is set to 1 internally
377 // 0: This requests JSBSim not to output any messages
379 // 1: This value explicity requests the normal JSBSim
381 // 2: This value asks for a message to be printed out when
382 // a class is instantiated
383 // 4: When this value is set, a message is displayed when a
384 // FGModel object executes its Run() method
385 // 8: When this value is set, various runtime state variables
386 // are printed out periodically
387 // 16: When set various parameters are sanity checked and
388 // a message is printed out when they go out of bounds
390 void FGTable::Debug(int from)
392 if (debug_lvl <= 0) return;
394 if (debug_lvl & 1) { // Standard console startup message output
395 if (from == 0) { // Constructor
399 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
400 if (from == 0) cout << "Instantiated: FGTable" << endl;
401 if (from == 1) cout << "Destroyed: FGTable" << endl;
403 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
405 if (debug_lvl & 8 ) { // Runtime state variables
407 if (debug_lvl & 16) { // Sanity checking
409 if (debug_lvl & 64) {
410 if (from == 0) { // Constructor
411 cout << IdSrc << endl;
412 cout << IdHdr << endl;