1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 Purpose: Models a lookup table
8 ------------- Copyright (C) 2001 Jon S. Berndt (jon@jsbsim.org) -------------
10 This program is free software; you can redistribute it and/or modify it under
11 the terms of the GNU Lesser 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 Lesser General Public License for more
20 You should have received a copy of the GNU Lesser 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 Lesser 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40 #include "input_output/FGXMLElement.h"
41 #include "input_output/FGPropertyManager.h"
50 static const char *IdSrc = "$Id: FGTable.cpp,v 1.21 2010/04/07 03:08:37 jberndt Exp $";
51 static const char *IdHdr = ID_TABLE;
53 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
57 FGTable::FGTable(int NRows) : nRows(NRows), nCols(1), PropertyManager(0)
66 lastRowIndex=lastColumnIndex=2;
69 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 FGTable::FGTable(const FGTable& t) : PropertyManager(t.PropertyManager)
74 colCounter = t.colCounter;
75 rowCounter = t.rowCounter;
76 tableCounter = t.tableCounter;
80 dimension = t.dimension;
81 internal = t.internal;
83 lookupProperty[0] = t.lookupProperty[0];
84 lookupProperty[1] = t.lookupProperty[1];
85 lookupProperty[2] = t.lookupProperty[2];
89 for (unsigned int r=0; r<=nRows; r++) {
90 for (unsigned int c=0; c<=nCols; c++) {
91 Data[r][c] = t.Data[r][c];
94 lastRowIndex = t.lastRowIndex;
95 lastColumnIndex = t.lastColumnIndex;
96 lastTableIndex = t.lastTableIndex;
99 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101 FGTable::FGTable(FGPropertyManager* propMan, Element* el) : PropertyManager(propMan)
106 string property_string;
111 FGPropertyManager* node;
112 Element *tableData=0;
113 Element *parent_element=0;
114 Element *axisElement=0;
115 string operation_types = "function, product, sum, difference, quotient,"
116 "pow, abs, sin, cos, asin, acos, tan, atan, table";
120 // Is this an internal lookup table?
123 Name = el->GetAttributeValue("name"); // Allow this table to be named with a property
124 call_type = el->GetAttributeValue("type");
125 if (call_type == string("internal")) {
126 parent_element = el->GetParent();
127 parent_type = parent_element->GetName();
128 if (operation_types.find(parent_type) == string::npos) {
131 // internal table is a child element of a restricted type
132 cerr << endl << fgred << " An internal table cannot be nested within another type," << endl;
133 cerr << " such as a function. The 'internal' keyword is ignored." << fgdef << endl << endl;
135 } else if (!call_type.empty()) {
136 cerr << endl << fgred << " An unknown table type attribute is listed: " << call_type
137 << ". Execution cannot continue." << fgdef << endl << endl;
141 // Determine and store the lookup properties for this table unless this table
142 // is part of a 3D table, in which case its independentVar property indexes will
143 // be set by a call from the owning table during creation
147 axisElement = el->FindElement("independentVar");
150 // The 'internal' attribute of the table element cannot be specified
151 // at the same time that independentVars are specified.
153 cerr << endl << fgred << " This table specifies both 'internal' call type" << endl;
154 cerr << " and specific lookup properties via the 'independentVar' element." << endl;
155 cerr << " These are mutually exclusive specifications. The 'internal'" << endl;
156 cerr << " attribute will be ignored." << fgdef << endl << endl;
160 for (i=0; i<3; i++) lookupProperty[i] = 0;
162 while (axisElement) {
163 property_string = axisElement->GetDataLine();
164 // The property string passed into GetNode() must have no spaces or tabs.
165 node = PropertyManager->GetNode(property_string);
168 throw("IndependenVar property, " + property_string + " in Table definition is not defined.");
171 lookup_axis = axisElement->GetAttributeValue("lookup");
172 if (lookup_axis == string("row")) {
173 lookupProperty[eRow] = node;
174 } else if (lookup_axis == string("column")) {
175 lookupProperty[eColumn] = node;
176 } else if (lookup_axis == string("table")) {
177 lookupProperty[eTable] = node;
178 } else if (!lookup_axis.empty()) {
179 throw("Lookup table axis specification not understood: " + lookup_axis);
180 } else { // assumed single dimension table; row lookup
181 lookupProperty[eRow] = node;
184 axisElement = el->FindNextElement("independentVar");
187 } else if (internal) { // This table is an internal table
189 // determine how many rows, columns, and tables in this table (dimension).
191 if (el->GetNumElements("tableData") > 1) {
192 dimension = 3; // this is a 3D table
194 tableData = el->FindElement("tableData");
195 string test_line = tableData->GetDataLine(1); // examine second line in table for dimension
196 if (FindNumColumns(test_line) == 2) dimension = 1; // 1D table
197 else if (FindNumColumns(test_line) > 2) dimension = 2; // 2D table
199 cerr << "Invalid number of columns in table" << endl;
204 brkpt_string = el->GetAttributeValue("breakPoint");
205 if (brkpt_string.empty()) {
206 // no independentVars found, and table is not marked as internal, nor is it a 3D table
207 cerr << endl << fgred << "No independent variable found for table." << fgdef << endl << endl;
211 // end lookup property code
213 if (brkpt_string.empty()) { // Not a 3D table "table element"
214 tableData = el->FindElement("tableData");
215 } else { // This is a table in a 3D table
217 dimension = 2; // Currently, infers 2D table
220 for (i=0; i<tableData->GetNumDataLines(); i++) {
221 buf << tableData->GetDataLine(i) << string(" ");
225 nRows = tableData->GetNumDataLines();
232 lastRowIndex = lastColumnIndex = 2;
236 nRows = tableData->GetNumDataLines()-1;
238 if (nRows >= 2) nCols = FindNumColumns(tableData->GetDataLine(0));
240 cerr << endl << fgred << "Not enough rows in this table." << fgdef << endl;
249 lastRowIndex = lastColumnIndex = 2;
253 nTables = el->GetNumElements("tableData");
259 lastRowIndex = lastColumnIndex = 2;
261 Data = Allocate(); // this data array will contain the keys for the associated tables
262 Tables.reserve(nTables); // necessary?
263 tableData = el->FindElement("tableData");
264 for (i=0; i<nTables; i++) {
265 Tables.push_back(new FGTable(PropertyManager, tableData));
266 Data[i+1][1] = tableData->GetAttributeValueAsNumber("breakPoint");
267 Tables[i]->SetRowIndexProperty(lookupProperty[eRow]);
268 Tables[i]->SetColumnIndexProperty(lookupProperty[eColumn]);
269 tableData = el->FindNextElement("tableData");
275 cout << "No dimension given" << endl;
281 if (debug_lvl & 1) Print();
284 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286 double** FGTable::Allocate(void)
288 Data = new double*[nRows+1];
289 for (unsigned int r=0; r<=nRows; r++) {
290 Data[r] = new double[nCols+1];
291 for (unsigned int c=0; c<=nCols; c++) {
298 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
303 for (unsigned int i=0; i<nTables; i++) delete Tables[i];
306 for (unsigned int r=0; r<=nRows; r++) delete[] Data[r];
312 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
314 unsigned int FGTable::FindNumColumns(const string& test_line)
316 // determine number of data columns in table (first column is row lookup - don't count)
318 unsigned int nCols=0;
319 while ((position = test_line.find_first_not_of(" \t", position)) != string::npos) {
321 position = test_line.find_first_of(" \t", position);
326 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
328 double FGTable::GetValue(void) const
335 temp = lookupProperty[eRow]->getDoubleValue();
336 temp2 = GetValue(temp);
339 return GetValue(lookupProperty[eRow]->getDoubleValue(),
340 lookupProperty[eColumn]->getDoubleValue());
342 return GetValue(lookupProperty[eRow]->getDoubleValue(),
343 lookupProperty[eColumn]->getDoubleValue(),
344 lookupProperty[eTable]->getDoubleValue());
346 cerr << "Attempted to GetValue() for invalid/unknown table type" << endl;
347 throw(string("Attempted to GetValue() for invalid/unknown table type"));
351 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
353 double FGTable::GetValue(double key) const
355 double Factor, Value, Span;
356 unsigned int r = lastRowIndex;
358 //if the key is off the end of the table, just return the
359 //end-of-table value, do not extrapolate
360 if( key <= Data[1][0] ) {
362 //cout << "Key underneath table: " << key << endl;
364 } else if ( key >= Data[nRows][0] ) {
366 //cout << "Key over table: " << key << endl;
367 return Data[nRows][1];
370 // the key is somewhere in the middle, search for the right breakpoint
371 // The search is particularly efficient if
372 // the correct breakpoint has not changed since last frame or
373 // has only changed very little
375 while (r > 2 && Data[r-1][0] > key) { r--; }
376 while (r < nRows && Data[r][0] < key) { r++; }
379 // make sure denominator below does not go to zero.
381 Span = Data[r][0] - Data[r-1][0];
383 Factor = (key - Data[r-1][0]) / Span;
384 if (Factor > 1.0) Factor = 1.0;
389 Value = Factor*(Data[r][1] - Data[r-1][1]) + Data[r-1][1];
394 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
396 double FGTable::GetValue(double rowKey, double colKey) const
398 double rFactor, cFactor, col1temp, col2temp, Value;
399 unsigned int r = lastRowIndex;
400 unsigned int c = lastColumnIndex;
402 while(r > 2 && Data[r-1][0] > rowKey) { r--; }
403 while(r < nRows && Data[r] [0] < rowKey) { r++; }
405 while(c > 2 && Data[0][c-1] > colKey) { c--; }
406 while(c < nCols && Data[0][c] < colKey) { c++; }
411 rFactor = (rowKey - Data[r-1][0]) / (Data[r][0] - Data[r-1][0]);
412 cFactor = (colKey - Data[0][c-1]) / (Data[0][c] - Data[0][c-1]);
414 if (rFactor > 1.0) rFactor = 1.0;
415 else if (rFactor < 0.0) rFactor = 0.0;
417 if (cFactor > 1.0) cFactor = 1.0;
418 else if (cFactor < 0.0) cFactor = 0.0;
420 col1temp = rFactor*(Data[r][c-1] - Data[r-1][c-1]) + Data[r-1][c-1];
421 col2temp = rFactor*(Data[r][c] - Data[r-1][c]) + Data[r-1][c];
423 Value = col1temp + cFactor*(col2temp - col1temp);
428 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
430 double FGTable::GetValue(double rowKey, double colKey, double tableKey) const
432 double Factor, Value, Span;
433 unsigned int r = lastRowIndex;
435 //if the key is off the end (or before the beginning) of the table,
436 // just return the boundary-table value, do not extrapolate
438 if( tableKey <= Data[1][1] ) {
440 return Tables[0]->GetValue(rowKey, colKey);
441 } else if ( tableKey >= Data[nRows][1] ) {
443 return Tables[nRows-1]->GetValue(rowKey, colKey);
446 // the key is somewhere in the middle, search for the right breakpoint
447 // The search is particularly efficient if
448 // the correct breakpoint has not changed since last frame or
449 // has only changed very little
451 while(r > 2 && Data[r-1][1] > tableKey) { r--; }
452 while(r < nRows && Data[r] [1] < tableKey) { r++; }
455 // make sure denominator below does not go to zero.
457 Span = Data[r][1] - Data[r-1][1];
459 Factor = (tableKey - Data[r-1][1]) / Span;
460 if (Factor > 1.0) Factor = 1.0;
465 Value = Factor*(Tables[r-1]->GetValue(rowKey, colKey) - Tables[r-2]->GetValue(rowKey, colKey))
466 + Tables[r-2]->GetValue(rowKey, colKey);
471 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
473 void FGTable::operator<<(istream& in_stream)
478 // In 1D table, no pseudo-row of column-headers (i.e. keys):
479 if (Type == tt1D) startRow = 1;
481 for (unsigned int r=startRow; r<=nRows; r++) {
482 for (unsigned int c=startCol; c<=nCols; c++) {
483 if (r != 0 || c != 0) {
484 in_stream >> Data[r][c];
490 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
492 FGTable& FGTable::operator<<(const double n)
494 Data[rowCounter][colCounter] = n;
495 if (colCounter == (int)nCols) {
504 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
506 FGTable& FGTable::operator<<(const int n)
512 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
514 void FGTable::Print(void)
519 if (Type == tt1D || Type == tt3D) startRow = 1;
520 if (Type == tt3D) startCol = 1;
522 #if defined (sgi) && !defined(__GNUC__) && (_COMPILER_VERSION < 740)
523 unsigned long flags = cout.setf(ios::fixed);
525 ios::fmtflags flags = cout.setf(ios::fixed); // set up output stream
530 cout << " 1 dimensional table with " << nRows << " rows." << endl;
533 cout << " 2 dimensional table with " << nRows << " rows, " << nCols << " columns." << endl;
536 cout << " 3 dimensional table with " << nRows << " rows, "
537 << nCols << " columns "
538 << nTables << " tables." << endl;
542 for (unsigned int r=startRow; r<=nRows; r++) {
544 for (unsigned int c=startCol; c<=nCols; c++) {
545 if (r == 0 && c == 0) {
548 cout << Data[r][c] << " ";
551 Tables[r-1]->Print();
557 cout.setf(flags); // reset
560 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
562 void FGTable::bind(void)
564 typedef double (FGTable::*PMF)(void) const;
565 if ( !Name.empty() && !internal) {
566 string tmp = PropertyManager->mkPropertyName(Name, false); // Allow upper
567 PropertyManager->Tie( tmp, this, (PMF)&FGTable::GetValue);
570 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
571 // The bitmasked value choices are as follows:
572 // unset: In this case (the default) JSBSim would only print
573 // out the normally expected messages, essentially echoing
574 // the config files as they are read. If the environment
575 // variable is not set, debug_lvl is set to 1 internally
576 // 0: This requests JSBSim not to output any messages
578 // 1: This value explicity requests the normal JSBSim
580 // 2: This value asks for a message to be printed out when
581 // a class is instantiated
582 // 4: When this value is set, a message is displayed when a
583 // FGModel object executes its Run() method
584 // 8: When this value is set, various runtime state variables
585 // are printed out periodically
586 // 16: When set various parameters are sanity checked and
587 // a message is printed out when they go out of bounds
589 void FGTable::Debug(int from)
591 if (debug_lvl <= 0) return;
593 if (debug_lvl & 1) { // Standard console startup message output
594 if (from == 0) { // Constructor
598 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
599 if (from == 0) cout << "Instantiated: FGTable" << endl;
600 if (from == 1) cout << "Destroyed: FGTable" << endl;
602 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
604 if (debug_lvl & 8 ) { // Runtime state variables
606 if (debug_lvl & 16) { // Sanity checking
608 if (debug_lvl & 64) {
609 if (from == 0) { // Constructor
610 cout << IdSrc << endl;
611 cout << IdHdr << endl;