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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
58 FGTable::FGTable(int NRows) : nRows(NRows), nCols(1), PropertyManager(0)
67 lastRowIndex=lastColumnIndex=2;
70 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 FGTable::FGTable(const FGTable& t) : PropertyManager(t.PropertyManager)
75 colCounter = t.colCounter;
76 rowCounter = t.rowCounter;
77 tableCounter = t.tableCounter;
81 dimension = t.dimension;
82 internal = t.internal;
86 for (int r=0; r<=nRows; r++) {
87 for (int c=0; c<=nCols; c++) {
88 Data[r][c] = t.Data[r][c];
91 lastRowIndex = t.lastRowIndex;
92 lastColumnIndex = t.lastColumnIndex;
93 lastTableIndex = t.lastTableIndex;
96 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98 FGTable::FGTable(FGPropertyManager* propMan, Element* el) : PropertyManager(propMan)
103 string property_string;
107 FGPropertyManager* node;
109 Element *parent_element;
110 Element *axisElement;
111 string operation_types = "function, product, sum, difference, quotient,"
112 "pow, abs, sin, cos, asin, acos, tan, atan, table";
116 // Is this an internal lookup table?
119 call_type = el->GetAttributeValue("type");
120 if (call_type == string("internal")) {
121 parent_element = el->GetParent();
122 parent_type = parent_element->GetName();
123 if (operation_types.find(parent_type) == string::npos) {
126 // internal table is a child element of a restricted type
127 cerr << endl << fgred << " An internal table cannot be nested within another type," << endl;
128 cerr << " such as a function. The 'internal' keyword is ignored." << fgdef << endl << endl;
130 } else if (!call_type.empty()) {
131 cerr << endl << fgred << " An unknown table type attribute is listed: " << call_type
132 << ". Execution cannot continue." << fgdef << endl << endl;
136 // Determine and store the lookup properties for this table unless this table
137 // is part of a 3D table, in which case its independentVar property indexes will
138 // be set by a call from the owning table during creation
142 axisElement = el->FindElement("independentVar");
145 // The 'internal' attribute of the table element cannot be specified
146 // at the same time that independentVars are specified.
148 cerr << endl << fgred << " This table specifies both 'internal' call type" << endl;
149 cerr << " and specific lookup properties via the 'independentVar' element." << endl;
150 cerr << " These are mutually exclusive specifications. The 'internal'" << endl;
151 cerr << " attribute will be ignored." << fgdef << endl << endl;
155 for (i=0; i<3; i++) lookupProperty[i] = 0;
157 while (axisElement) {
158 property_string = axisElement->GetDataLine();
159 node = PropertyManager->GetNode(property_string);
161 lookup_axis = axisElement->GetAttributeValue("lookup");
162 if (lookup_axis == string("row")) {
163 lookupProperty[eRow] = node;
164 } else if (lookup_axis == string("column")) {
165 lookupProperty[eColumn] = node;
166 } else if (lookup_axis == string("table")) {
167 lookupProperty[eTable] = node;
168 } else { // assumed single dimension table; row lookup
169 lookupProperty[eRow] = node;
172 axisElement = el->FindNextElement("independentVar");
175 } else if (internal) { // This table is an internal table
177 // determine how many rows, columns, and tables in this table (dimension).
179 if (el->GetNumElements("tableData") > 1) {
180 dimension = 3; // this is a 3D table
182 tableData = el->FindElement("tableData");
183 string test_line = tableData->GetDataLine(1); // examine second line in table for dimension
184 if (FindNumColumns(test_line) == 2) dimension = 1; // 1D table
185 else if (FindNumColumns(test_line) > 2) dimension = 2; // 2D table
187 cerr << "Invalid number of columns in table" << endl;
191 } else { // no independentVars found, and table is not marked as internal
192 cerr << endl << fgred << "No independent variable found for table." << fgdef << endl << endl;
195 // end lookup property code
197 tableData = el->FindElement("tableData");
198 for (i=0; i<tableData->GetNumDataLines(); i++) {
199 buf << tableData->GetDataLine(i) << string(" ");
203 nRows = tableData->GetNumDataLines();
210 lastRowIndex = lastColumnIndex = 2;
214 nRows = tableData->GetNumDataLines()-1;
216 if (nRows >= 2) nCols = FindNumColumns(tableData->GetDataLine(0));
218 cerr << endl << fgred << "Not enough rows in this table." << fgdef << endl;
227 lastRowIndex = lastColumnIndex = 2;
231 nTables = el->GetNumElements("tableData");
238 Data = Allocate(); // this data array will contain the keys for the associated tables
239 Tables.reserve(nTables); // necessary?
240 tableData = el->FindElement("tableData");
241 for (i=0; i<nTables; i++) {
242 Tables.push_back(new FGTable(PropertyManager, tableData));
243 Data[i+1][1] = tableData->GetAttributeValueAsNumber("breakPoint");
244 Tables[i]->SetRowIndexProperty(lookupProperty[eRow]);
245 Tables[i]->SetColumnIndexProperty(lookupProperty[eColumn]);
246 tableData = el->FindNextElement("tableData");
252 cout << "No dimension given" << endl;
255 if (debug_lvl & 1) Print();
258 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
260 double** FGTable::Allocate(void)
262 Data = new double*[nRows+1];
263 for (int r=0; r<=nRows; r++) {
264 Data[r] = new double[nCols+1];
265 for (int c=0; c<=nCols; c++) {
272 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277 cout << "nTables = " << nTables << endl;
278 for (int i=0; i<nTables; i++) delete Tables[i];
281 for (int r=0; r<=nRows; r++) if (Data[r]) delete[] Data[r];
282 if (Data) delete[] Data;
286 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
288 int FGTable::FindNumColumns(string test_line)
290 // determine number of data columns in table (first column is row lookup - don't count)
293 while ((position = test_line.find_first_not_of(" \t", position)) != string::npos) {
295 position = test_line.find_first_of(" \t", position);
300 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302 double FGTable::GetValue(void) const
309 temp = lookupProperty[eRow]->getDoubleValue();
310 temp2 = GetValue(temp);
313 return GetValue(lookupProperty[eRow]->getDoubleValue(),
314 lookupProperty[eColumn]->getDoubleValue());
316 return GetValue(lookupProperty[eRow]->getDoubleValue(),
317 lookupProperty[eColumn]->getDoubleValue(),
318 lookupProperty[eTable]->getDoubleValue());
320 cerr << "Attempted to GetValue() for invalid/unknown table type" << endl;
321 throw(string("Attempted to GetValue() for invalid/unknown table type"));
325 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
327 double FGTable::GetValue(double key) const
329 double Factor, Value, Span;
332 //if the key is off the end of the table, just return the
333 //end-of-table value, do not extrapolate
334 if( key <= Data[1][0] ) {
336 //cout << "Key underneath table: " << key << endl;
338 } else if ( key >= Data[nRows][0] ) {
340 //cout << "Key over table: " << key << endl;
341 return Data[nRows][1];
344 // the key is somewhere in the middle, search for the right breakpoint
345 // assume the correct breakpoint has not changed since last frame or
346 // has only changed very little
348 if ( r > 2 && Data[r-1][0] > key ) {
349 while( Data[r-1][0] > key && r > 2) { r--; }
350 } else if ( Data[r][0] < key ) {
351 while( Data[r][0] <= key && r <= nRows) { r++; }
355 // make sure denominator below does not go to zero.
357 Span = Data[r][0] - Data[r-1][0];
359 Factor = (key - Data[r-1][0]) / Span;
360 if (Factor > 1.0) Factor = 1.0;
365 Value = Factor*(Data[r][1] - Data[r-1][1]) + Data[r-1][1];
370 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
372 double FGTable::GetValue(double rowKey, double colKey) const
374 double rFactor, cFactor, col1temp, col2temp, Value;
376 int c=lastColumnIndex;
378 if ( r > 2 && Data[r-1][0] > rowKey ) {
379 while ( Data[r-1][0] > rowKey && r > 2) { r--; }
380 } else if ( Data[r][0] < rowKey ) {
381 while ( r <= nRows && Data[r][0] <= rowKey ) { r++; }
382 if ( r > nRows ) r = nRows;
385 if ( c > 2 && Data[0][c-1] > colKey ) {
386 while( Data[0][c-1] > colKey && c > 2) { c--; }
387 } else if ( Data[0][c] < colKey ) {
388 while( Data[0][c] <= colKey && c <= nCols) { c++; }
389 if ( c > nCols ) c = nCols;
395 rFactor = (rowKey - Data[r-1][0]) / (Data[r][0] - Data[r-1][0]);
396 cFactor = (colKey - Data[0][c-1]) / (Data[0][c] - Data[0][c-1]);
398 if (rFactor > 1.0) rFactor = 1.0;
399 else if (rFactor < 0.0) rFactor = 0.0;
401 if (cFactor > 1.0) cFactor = 1.0;
402 else if (cFactor < 0.0) cFactor = 0.0;
404 col1temp = rFactor*(Data[r][c-1] - Data[r-1][c-1]) + Data[r-1][c-1];
405 col2temp = rFactor*(Data[r][c] - Data[r-1][c]) + Data[r-1][c];
407 Value = col1temp + cFactor*(col2temp - col1temp);
412 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
414 double FGTable::GetValue(double rowKey, double colKey, double tableKey) const
416 double Factor, Value, Span;
419 //if the key is off the end (or before the beginning) of the table,
420 // just return the boundary-table value, do not extrapolate
422 if( tableKey <= Data[1][1] ) {
424 return Tables[0]->GetValue(rowKey, colKey);
425 } else if ( tableKey >= Data[nRows][1] ) {
427 return Tables[nRows-1]->GetValue(rowKey, colKey);
430 // the key is somewhere in the middle, search for the right breakpoint
431 // assume the correct breakpoint has not changed since last frame or
432 // has only changed very little
434 if ( r > 2 && Data[r-1][1] > tableKey ) {
435 while( Data[r-1][1] > tableKey && r > 2) { r--; }
436 } else if ( Data[r][1] < tableKey ) {
437 while( Data[r][1] <= tableKey && r <= nRows) { r++; }
441 // make sure denominator below does not go to zero.
443 Span = Data[r][1] - Data[r-1][1];
445 Factor = (tableKey - Data[r-1][1]) / Span;
446 if (Factor > 1.0) Factor = 1.0;
451 Value = Factor*(Tables[r-1]->GetValue(rowKey, colKey) - Tables[r-2]->GetValue(rowKey, colKey))
452 + Tables[r-2]->GetValue(rowKey, colKey);
457 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
459 void FGTable::operator<<(stringstream& in_stream)
464 if (Type == tt1D || Type == tt3D) startRow = 1;
465 if (Type == tt3D) startCol = 1;
467 for (int r=startRow; r<=nRows; r++) {
468 for (int c=startCol; c<=nCols; c++) {
469 if (r != 0 || c != 0) {
470 in_stream >> Data[r][c];
476 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
478 FGTable& FGTable::operator<<(const double n)
480 Data[rowCounter][colCounter] = n;
481 if (colCounter == nCols) {
490 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
492 FGTable& FGTable::operator<<(const int n)
498 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
500 void FGTable::Print(void)
505 if (Type == tt1D || Type == tt3D) startRow = 1;
506 if (Type == tt3D) startCol = 1;
508 #if defined (sgi) && !defined(__GNUC__) && (_COMPILER_VERSION < 740)
509 unsigned long flags = cout.setf(ios::fixed);
511 ios::fmtflags flags = cout.setf(ios::fixed); // set up output stream
516 cout << " 1 dimensional table with " << nRows << " rows." << endl;
519 cout << " 2 dimensional table with " << nRows << " rows, " << nCols << " columns." << endl;
522 cout << " 3 dimensional table with " << nRows << " rows, "
523 << nCols << " columns "
524 << nTables << " tables." << endl;
528 for (int r=startRow; r<=nRows; r++) {
530 for (int c=startCol; c<=nCols; c++) {
531 if (r == 0 && c == 0) {
534 cout << Data[r][c] << " ";
537 Tables[r-1]->Print();
543 cout.setf(flags); // reset
546 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
547 // The bitmasked value choices are as follows:
548 // unset: In this case (the default) JSBSim would only print
549 // out the normally expected messages, essentially echoing
550 // the config files as they are read. If the environment
551 // variable is not set, debug_lvl is set to 1 internally
552 // 0: This requests JSBSim not to output any messages
554 // 1: This value explicity requests the normal JSBSim
556 // 2: This value asks for a message to be printed out when
557 // a class is instantiated
558 // 4: When this value is set, a message is displayed when a
559 // FGModel object executes its Run() method
560 // 8: When this value is set, various runtime state variables
561 // are printed out periodically
562 // 16: When set various parameters are sanity checked and
563 // a message is printed out when they go out of bounds
565 void FGTable::Debug(int from)
567 if (debug_lvl <= 0) return;
569 if (debug_lvl & 1) { // Standard console startup message output
570 if (from == 0) { // Constructor
574 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
575 if (from == 0) cout << "Instantiated: FGTable" << endl;
576 if (from == 1) cout << "Destroyed: FGTable" << endl;
578 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
580 if (debug_lvl & 8 ) { // Runtime state variables
582 if (debug_lvl & 16) { // Sanity checking
584 if (debug_lvl & 64) {
585 if (from == 0) { // Constructor
586 cout << IdSrc << endl;
587 cout << IdHdr << endl;