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 // The property string passed into GetNode() must have no spaces or tabs.
160 node = PropertyManager->GetNode(property_string);
163 cerr << "IndependenVar property, " << property_string
164 << " in Table definition is not defined." << endl;
168 lookup_axis = axisElement->GetAttributeValue("lookup");
169 if (lookup_axis == string("row")) {
170 lookupProperty[eRow] = node;
171 } else if (lookup_axis == string("column")) {
172 lookupProperty[eColumn] = node;
173 } else if (lookup_axis == string("table")) {
174 lookupProperty[eTable] = node;
175 } else { // assumed single dimension table; row lookup
176 lookupProperty[eRow] = node;
179 axisElement = el->FindNextElement("independentVar");
182 } else if (internal) { // This table is an internal table
184 // determine how many rows, columns, and tables in this table (dimension).
186 if (el->GetNumElements("tableData") > 1) {
187 dimension = 3; // this is a 3D table
189 tableData = el->FindElement("tableData");
190 string test_line = tableData->GetDataLine(1); // examine second line in table for dimension
191 if (FindNumColumns(test_line) == 2) dimension = 1; // 1D table
192 else if (FindNumColumns(test_line) > 2) dimension = 2; // 2D table
194 cerr << "Invalid number of columns in table" << endl;
198 } else { // no independentVars found, and table is not marked as internal
199 cerr << endl << fgred << "No independent variable found for table." << fgdef << endl << endl;
202 // end lookup property code
204 tableData = el->FindElement("tableData");
205 for (i=0; i<tableData->GetNumDataLines(); i++) {
206 buf << tableData->GetDataLine(i) << string(" ");
210 nRows = tableData->GetNumDataLines();
217 lastRowIndex = lastColumnIndex = 2;
221 nRows = tableData->GetNumDataLines()-1;
223 if (nRows >= 2) nCols = FindNumColumns(tableData->GetDataLine(0));
225 cerr << endl << fgred << "Not enough rows in this table." << fgdef << endl;
234 lastRowIndex = lastColumnIndex = 2;
238 nTables = el->GetNumElements("tableData");
245 Data = Allocate(); // this data array will contain the keys for the associated tables
246 Tables.reserve(nTables); // necessary?
247 tableData = el->FindElement("tableData");
248 for (i=0; i<nTables; i++) {
249 Tables.push_back(new FGTable(PropertyManager, tableData));
250 Data[i+1][1] = tableData->GetAttributeValueAsNumber("breakPoint");
251 Tables[i]->SetRowIndexProperty(lookupProperty[eRow]);
252 Tables[i]->SetColumnIndexProperty(lookupProperty[eColumn]);
253 tableData = el->FindNextElement("tableData");
259 cout << "No dimension given" << endl;
262 if (debug_lvl & 1) Print();
265 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
267 double** FGTable::Allocate(void)
269 Data = new double*[nRows+1];
270 for (int r=0; r<=nRows; r++) {
271 Data[r] = new double[nCols+1];
272 for (int c=0; c<=nCols; c++) {
279 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
284 cout << "nTables = " << nTables << endl;
285 for (int i=0; i<nTables; i++) delete Tables[i];
288 for (int r=0; r<=nRows; r++) if (Data[r]) delete[] Data[r];
289 if (Data) delete[] Data;
293 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
295 int FGTable::FindNumColumns(string test_line)
297 // determine number of data columns in table (first column is row lookup - don't count)
300 while ((position = test_line.find_first_not_of(" \t", position)) != string::npos) {
302 position = test_line.find_first_of(" \t", position);
307 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309 double FGTable::GetValue(void) const
316 temp = lookupProperty[eRow]->getDoubleValue();
317 temp2 = GetValue(temp);
320 return GetValue(lookupProperty[eRow]->getDoubleValue(),
321 lookupProperty[eColumn]->getDoubleValue());
323 return GetValue(lookupProperty[eRow]->getDoubleValue(),
324 lookupProperty[eColumn]->getDoubleValue(),
325 lookupProperty[eTable]->getDoubleValue());
327 cerr << "Attempted to GetValue() for invalid/unknown table type" << endl;
328 throw(string("Attempted to GetValue() for invalid/unknown table type"));
332 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
334 double FGTable::GetValue(double key) const
336 double Factor, Value, Span;
339 //if the key is off the end of the table, just return the
340 //end-of-table value, do not extrapolate
341 if( key <= Data[1][0] ) {
343 //cout << "Key underneath table: " << key << endl;
345 } else if ( key >= Data[nRows][0] ) {
347 //cout << "Key over table: " << key << endl;
348 return Data[nRows][1];
351 // the key is somewhere in the middle, search for the right breakpoint
352 // assume the correct breakpoint has not changed since last frame or
353 // has only changed very little
355 if ( r > 2 && Data[r-1][0] > key ) {
356 while( Data[r-1][0] > key && r > 2) { r--; }
357 } else if ( Data[r][0] < key ) {
358 while( Data[r][0] <= key && r <= nRows) { r++; }
362 // make sure denominator below does not go to zero.
364 Span = Data[r][0] - Data[r-1][0];
366 Factor = (key - Data[r-1][0]) / Span;
367 if (Factor > 1.0) Factor = 1.0;
372 Value = Factor*(Data[r][1] - Data[r-1][1]) + Data[r-1][1];
377 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
379 double FGTable::GetValue(double rowKey, double colKey) const
381 double rFactor, cFactor, col1temp, col2temp, Value;
383 int c=lastColumnIndex;
385 if ( r > 2 && Data[r-1][0] > rowKey ) {
386 while ( Data[r-1][0] > rowKey && r > 2) { r--; }
387 } else if ( Data[r][0] < rowKey ) {
388 while ( r <= nRows && Data[r][0] <= rowKey ) { r++; }
389 if ( r > nRows ) r = nRows;
392 if ( c > 2 && Data[0][c-1] > colKey ) {
393 while( Data[0][c-1] > colKey && c > 2) { c--; }
394 } else if ( Data[0][c] < colKey ) {
395 while( Data[0][c] <= colKey && c <= nCols) { c++; }
396 if ( c > nCols ) c = nCols;
402 rFactor = (rowKey - Data[r-1][0]) / (Data[r][0] - Data[r-1][0]);
403 cFactor = (colKey - Data[0][c-1]) / (Data[0][c] - Data[0][c-1]);
405 if (rFactor > 1.0) rFactor = 1.0;
406 else if (rFactor < 0.0) rFactor = 0.0;
408 if (cFactor > 1.0) cFactor = 1.0;
409 else if (cFactor < 0.0) cFactor = 0.0;
411 col1temp = rFactor*(Data[r][c-1] - Data[r-1][c-1]) + Data[r-1][c-1];
412 col2temp = rFactor*(Data[r][c] - Data[r-1][c]) + Data[r-1][c];
414 Value = col1temp + cFactor*(col2temp - col1temp);
419 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
421 double FGTable::GetValue(double rowKey, double colKey, double tableKey) const
423 double Factor, Value, Span;
426 //if the key is off the end (or before the beginning) of the table,
427 // just return the boundary-table value, do not extrapolate
429 if( tableKey <= Data[1][1] ) {
431 return Tables[0]->GetValue(rowKey, colKey);
432 } else if ( tableKey >= Data[nRows][1] ) {
434 return Tables[nRows-1]->GetValue(rowKey, colKey);
437 // the key is somewhere in the middle, search for the right breakpoint
438 // assume the correct breakpoint has not changed since last frame or
439 // has only changed very little
441 if ( r > 2 && Data[r-1][1] > tableKey ) {
442 while( Data[r-1][1] > tableKey && r > 2) { r--; }
443 } else if ( Data[r][1] < tableKey ) {
444 while( Data[r][1] <= tableKey && r <= nRows) { r++; }
448 // make sure denominator below does not go to zero.
450 Span = Data[r][1] - Data[r-1][1];
452 Factor = (tableKey - Data[r-1][1]) / Span;
453 if (Factor > 1.0) Factor = 1.0;
458 Value = Factor*(Tables[r-1]->GetValue(rowKey, colKey) - Tables[r-2]->GetValue(rowKey, colKey))
459 + Tables[r-2]->GetValue(rowKey, colKey);
464 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
466 void FGTable::operator<<(stringstream& in_stream)
471 if (Type == tt1D || Type == tt3D) startRow = 1;
472 if (Type == tt3D) startCol = 1;
474 for (int r=startRow; r<=nRows; r++) {
475 for (int c=startCol; c<=nCols; c++) {
476 if (r != 0 || c != 0) {
477 in_stream >> Data[r][c];
483 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
485 FGTable& FGTable::operator<<(const double n)
487 Data[rowCounter][colCounter] = n;
488 if (colCounter == nCols) {
497 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
499 FGTable& FGTable::operator<<(const int n)
505 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
507 void FGTable::Print(void)
512 if (Type == tt1D || Type == tt3D) startRow = 1;
513 if (Type == tt3D) startCol = 1;
515 #if defined (sgi) && !defined(__GNUC__) && (_COMPILER_VERSION < 740)
516 unsigned long flags = cout.setf(ios::fixed);
518 ios::fmtflags flags = cout.setf(ios::fixed); // set up output stream
523 cout << " 1 dimensional table with " << nRows << " rows." << endl;
526 cout << " 2 dimensional table with " << nRows << " rows, " << nCols << " columns." << endl;
529 cout << " 3 dimensional table with " << nRows << " rows, "
530 << nCols << " columns "
531 << nTables << " tables." << endl;
535 for (int r=startRow; r<=nRows; r++) {
537 for (int c=startCol; c<=nCols; c++) {
538 if (r == 0 && c == 0) {
541 cout << Data[r][c] << " ";
544 Tables[r-1]->Print();
550 cout.setf(flags); // reset
553 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
554 // The bitmasked value choices are as follows:
555 // unset: In this case (the default) JSBSim would only print
556 // out the normally expected messages, essentially echoing
557 // the config files as they are read. If the environment
558 // variable is not set, debug_lvl is set to 1 internally
559 // 0: This requests JSBSim not to output any messages
561 // 1: This value explicity requests the normal JSBSim
563 // 2: This value asks for a message to be printed out when
564 // a class is instantiated
565 // 4: When this value is set, a message is displayed when a
566 // FGModel object executes its Run() method
567 // 8: When this value is set, various runtime state variables
568 // are printed out periodically
569 // 16: When set various parameters are sanity checked and
570 // a message is printed out when they go out of bounds
572 void FGTable::Debug(int from)
574 if (debug_lvl <= 0) return;
576 if (debug_lvl & 1) { // Standard console startup message output
577 if (from == 0) { // Constructor
581 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
582 if (from == 0) cout << "Instantiated: FGTable" << endl;
583 if (from == 1) cout << "Destroyed: FGTable" << endl;
585 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
587 if (debug_lvl & 8 ) { // Runtime state variables
589 if (debug_lvl & 16) { // Sanity checking
591 if (debug_lvl & 64) {
592 if (from == 0) { // Constructor
593 cout << IdSrc << endl;
594 cout << IdHdr << endl;