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);
162 cerr << "IndependenVar property, " << property_string
163 << " in Table definition is not defined." << endl;
167 lookup_axis = axisElement->GetAttributeValue("lookup");
168 if (lookup_axis == string("row")) {
169 lookupProperty[eRow] = node;
170 } else if (lookup_axis == string("column")) {
171 lookupProperty[eColumn] = node;
172 } else if (lookup_axis == string("table")) {
173 lookupProperty[eTable] = node;
174 } else { // assumed single dimension table; row lookup
175 lookupProperty[eRow] = node;
178 axisElement = el->FindNextElement("independentVar");
181 } else if (internal) { // This table is an internal table
183 // determine how many rows, columns, and tables in this table (dimension).
185 if (el->GetNumElements("tableData") > 1) {
186 dimension = 3; // this is a 3D table
188 tableData = el->FindElement("tableData");
189 string test_line = tableData->GetDataLine(1); // examine second line in table for dimension
190 if (FindNumColumns(test_line) == 2) dimension = 1; // 1D table
191 else if (FindNumColumns(test_line) > 2) dimension = 2; // 2D table
193 cerr << "Invalid number of columns in table" << endl;
197 } else { // no independentVars found, and table is not marked as internal
198 cerr << endl << fgred << "No independent variable found for table." << fgdef << endl << endl;
201 // end lookup property code
203 tableData = el->FindElement("tableData");
204 for (i=0; i<tableData->GetNumDataLines(); i++) {
205 buf << tableData->GetDataLine(i) << string(" ");
209 nRows = tableData->GetNumDataLines();
216 lastRowIndex = lastColumnIndex = 2;
220 nRows = tableData->GetNumDataLines()-1;
222 if (nRows >= 2) nCols = FindNumColumns(tableData->GetDataLine(0));
224 cerr << endl << fgred << "Not enough rows in this table." << fgdef << endl;
233 lastRowIndex = lastColumnIndex = 2;
237 nTables = el->GetNumElements("tableData");
244 Data = Allocate(); // this data array will contain the keys for the associated tables
245 Tables.reserve(nTables); // necessary?
246 tableData = el->FindElement("tableData");
247 for (i=0; i<nTables; i++) {
248 Tables.push_back(new FGTable(PropertyManager, tableData));
249 Data[i+1][1] = tableData->GetAttributeValueAsNumber("breakPoint");
250 Tables[i]->SetRowIndexProperty(lookupProperty[eRow]);
251 Tables[i]->SetColumnIndexProperty(lookupProperty[eColumn]);
252 tableData = el->FindNextElement("tableData");
258 cout << "No dimension given" << endl;
261 if (debug_lvl & 1) Print();
264 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266 double** FGTable::Allocate(void)
268 Data = new double*[nRows+1];
269 for (int r=0; r<=nRows; r++) {
270 Data[r] = new double[nCols+1];
271 for (int c=0; c<=nCols; c++) {
278 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
283 cout << "nTables = " << nTables << endl;
284 for (int i=0; i<nTables; i++) delete Tables[i];
287 for (int r=0; r<=nRows; r++) if (Data[r]) delete[] Data[r];
288 if (Data) delete[] Data;
292 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294 int FGTable::FindNumColumns(string test_line)
296 // determine number of data columns in table (first column is row lookup - don't count)
299 while ((position = test_line.find_first_not_of(" \t", position)) != string::npos) {
301 position = test_line.find_first_of(" \t", position);
306 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
308 double FGTable::GetValue(void) const
315 temp = lookupProperty[eRow]->getDoubleValue();
316 temp2 = GetValue(temp);
319 return GetValue(lookupProperty[eRow]->getDoubleValue(),
320 lookupProperty[eColumn]->getDoubleValue());
322 return GetValue(lookupProperty[eRow]->getDoubleValue(),
323 lookupProperty[eColumn]->getDoubleValue(),
324 lookupProperty[eTable]->getDoubleValue());
326 cerr << "Attempted to GetValue() for invalid/unknown table type" << endl;
327 throw(string("Attempted to GetValue() for invalid/unknown table type"));
331 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
333 double FGTable::GetValue(double key) const
335 double Factor, Value, Span;
338 //if the key is off the end of the table, just return the
339 //end-of-table value, do not extrapolate
340 if( key <= Data[1][0] ) {
342 //cout << "Key underneath table: " << key << endl;
344 } else if ( key >= Data[nRows][0] ) {
346 //cout << "Key over table: " << key << endl;
347 return Data[nRows][1];
350 // the key is somewhere in the middle, search for the right breakpoint
351 // assume the correct breakpoint has not changed since last frame or
352 // has only changed very little
354 if ( r > 2 && Data[r-1][0] > key ) {
355 while( Data[r-1][0] > key && r > 2) { r--; }
356 } else if ( Data[r][0] < key ) {
357 while( Data[r][0] <= key && r <= nRows) { r++; }
361 // make sure denominator below does not go to zero.
363 Span = Data[r][0] - Data[r-1][0];
365 Factor = (key - Data[r-1][0]) / Span;
366 if (Factor > 1.0) Factor = 1.0;
371 Value = Factor*(Data[r][1] - Data[r-1][1]) + Data[r-1][1];
376 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
378 double FGTable::GetValue(double rowKey, double colKey) const
380 double rFactor, cFactor, col1temp, col2temp, Value;
382 int c=lastColumnIndex;
384 if ( r > 2 && Data[r-1][0] > rowKey ) {
385 while ( Data[r-1][0] > rowKey && r > 2) { r--; }
386 } else if ( Data[r][0] < rowKey ) {
387 while ( r <= nRows && Data[r][0] <= rowKey ) { r++; }
388 if ( r > nRows ) r = nRows;
391 if ( c > 2 && Data[0][c-1] > colKey ) {
392 while( Data[0][c-1] > colKey && c > 2) { c--; }
393 } else if ( Data[0][c] < colKey ) {
394 while( Data[0][c] <= colKey && c <= nCols) { c++; }
395 if ( c > nCols ) c = nCols;
401 rFactor = (rowKey - Data[r-1][0]) / (Data[r][0] - Data[r-1][0]);
402 cFactor = (colKey - Data[0][c-1]) / (Data[0][c] - Data[0][c-1]);
404 if (rFactor > 1.0) rFactor = 1.0;
405 else if (rFactor < 0.0) rFactor = 0.0;
407 if (cFactor > 1.0) cFactor = 1.0;
408 else if (cFactor < 0.0) cFactor = 0.0;
410 col1temp = rFactor*(Data[r][c-1] - Data[r-1][c-1]) + Data[r-1][c-1];
411 col2temp = rFactor*(Data[r][c] - Data[r-1][c]) + Data[r-1][c];
413 Value = col1temp + cFactor*(col2temp - col1temp);
418 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
420 double FGTable::GetValue(double rowKey, double colKey, double tableKey) const
422 double Factor, Value, Span;
425 //if the key is off the end (or before the beginning) of the table,
426 // just return the boundary-table value, do not extrapolate
428 if( tableKey <= Data[1][1] ) {
430 return Tables[0]->GetValue(rowKey, colKey);
431 } else if ( tableKey >= Data[nRows][1] ) {
433 return Tables[nRows-1]->GetValue(rowKey, colKey);
436 // the key is somewhere in the middle, search for the right breakpoint
437 // assume the correct breakpoint has not changed since last frame or
438 // has only changed very little
440 if ( r > 2 && Data[r-1][1] > tableKey ) {
441 while( Data[r-1][1] > tableKey && r > 2) { r--; }
442 } else if ( Data[r][1] < tableKey ) {
443 while( Data[r][1] <= tableKey && r <= nRows) { r++; }
447 // make sure denominator below does not go to zero.
449 Span = Data[r][1] - Data[r-1][1];
451 Factor = (tableKey - Data[r-1][1]) / Span;
452 if (Factor > 1.0) Factor = 1.0;
457 Value = Factor*(Tables[r-1]->GetValue(rowKey, colKey) - Tables[r-2]->GetValue(rowKey, colKey))
458 + Tables[r-2]->GetValue(rowKey, colKey);
463 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
465 void FGTable::operator<<(stringstream& in_stream)
470 if (Type == tt1D || Type == tt3D) startRow = 1;
471 if (Type == tt3D) startCol = 1;
473 for (int r=startRow; r<=nRows; r++) {
474 for (int c=startCol; c<=nCols; c++) {
475 if (r != 0 || c != 0) {
476 in_stream >> Data[r][c];
482 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
484 FGTable& FGTable::operator<<(const double n)
486 Data[rowCounter][colCounter] = n;
487 if (colCounter == nCols) {
496 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
498 FGTable& FGTable::operator<<(const int n)
504 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
506 void FGTable::Print(void)
511 if (Type == tt1D || Type == tt3D) startRow = 1;
512 if (Type == tt3D) startCol = 1;
514 #if defined (sgi) && !defined(__GNUC__) && (_COMPILER_VERSION < 740)
515 unsigned long flags = cout.setf(ios::fixed);
517 ios::fmtflags flags = cout.setf(ios::fixed); // set up output stream
522 cout << " 1 dimensional table with " << nRows << " rows." << endl;
525 cout << " 2 dimensional table with " << nRows << " rows, " << nCols << " columns." << endl;
528 cout << " 3 dimensional table with " << nRows << " rows, "
529 << nCols << " columns "
530 << nTables << " tables." << endl;
534 for (int r=startRow; r<=nRows; r++) {
536 for (int c=startCol; c<=nCols; c++) {
537 if (r == 0 && c == 0) {
540 cout << Data[r][c] << " ";
543 Tables[r-1]->Print();
549 cout.setf(flags); // reset
552 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
553 // The bitmasked value choices are as follows:
554 // unset: In this case (the default) JSBSim would only print
555 // out the normally expected messages, essentially echoing
556 // the config files as they are read. If the environment
557 // variable is not set, debug_lvl is set to 1 internally
558 // 0: This requests JSBSim not to output any messages
560 // 1: This value explicity requests the normal JSBSim
562 // 2: This value asks for a message to be printed out when
563 // a class is instantiated
564 // 4: When this value is set, a message is displayed when a
565 // FGModel object executes its Run() method
566 // 8: When this value is set, various runtime state variables
567 // are printed out periodically
568 // 16: When set various parameters are sanity checked and
569 // a message is printed out when they go out of bounds
571 void FGTable::Debug(int from)
573 if (debug_lvl <= 0) return;
575 if (debug_lvl & 1) { // Standard console startup message output
576 if (from == 0) { // Constructor
580 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
581 if (from == 0) cout << "Instantiated: FGTable" << endl;
582 if (from == 1) cout << "Destroyed: FGTable" << endl;
584 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
586 if (debug_lvl & 8 ) { // Runtime state variables
588 if (debug_lvl & 16) { // Sanity checking
590 if (debug_lvl & 64) {
591 if (from == 0) { // Constructor
592 cout << IdSrc << endl;
593 cout << IdHdr << endl;