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));
69 lastRowIndex=lastColumnIndex=2;
74 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 FGTable::FGTable(int NRows, int NCols) : nRows(NRows), nCols(NCols)
82 } else if (NCols == 1) {
87 cerr << "FGTable cannot accept 'Rows=0'" << endl;
91 lastRowIndex=lastColumnIndex=2;
95 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97 FGTable::FGTable(int NRows) : nRows(NRows), nCols(1)
105 lastRowIndex=lastColumnIndex=2;
108 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 FGTable::FGTable(const FGTable& t)
113 colCounter = t.colCounter;
114 rowCounter = t.rowCounter;
115 tableCounter = t.tableCounter;
123 for (int r=0; r<=nRows; r++) {
124 for (int c=0; c<=nCols; c++) {
125 Data[r][c] = t.Data[r][c];
128 lastRowIndex = t.lastRowIndex;
129 lastColumnIndex = t.lastColumnIndex;
130 lastTableIndex = t.lastTableIndex;
133 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135 double** FGTable::Allocate(void)
137 Data = new double*[nRows+1];
138 for (int r=0; r<=nRows; r++) {
139 Data[r] = new double[nCols+1];
140 for (int c=0; c<=nCols; c++) {
147 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 if (nTables > 0) Tables.clear();
152 for (int r=0; r<=nRows; r++) if (Data[r]) delete[] Data[r];
153 if (Data) delete[] Data;
157 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159 double FGTable::GetValue(double key)
161 double Factor, Value, Span;
164 //if the key is off the end of the table, just return the
165 //end-of-table value, do not extrapolate
166 if( key <= Data[1][0] ) {
168 //cout << "Key underneath table: " << key << endl;
170 } else if ( key >= Data[nRows][0] ) {
172 //cout << "Key over table: " << key << endl;
173 return Data[nRows][1];
176 // the key is somewhere in the middle, search for the right breakpoint
177 // assume the correct breakpoint has not changed since last frame or
178 // has only changed very little
180 if ( r > 2 && Data[r-1][0] > key ) {
181 while( Data[r-1][0] > key && r > 2) { r--; }
182 } else if ( Data[r][0] < key ) {
183 while( Data[r][0] <= key && r <= nRows) { r++; }
187 // make sure denominator below does not go to zero.
189 Span = Data[r][0] - Data[r-1][0];
191 Factor = (key - Data[r-1][0]) / Span;
192 if (Factor > 1.0) Factor = 1.0;
197 Value = Factor*(Data[r][1] - Data[r-1][1]) + Data[r-1][1];
202 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
205 double FGTable::GetValue(double rowKey, double colKey)
207 double rFactor, cFactor, col1temp, col2temp, Value;
209 int c=lastColumnIndex;
211 if ( r > 2 && Data[r-1][0] > rowKey ) {
212 while ( Data[r-1][0] > rowKey && r > 2) { r--; }
213 } else if ( Data[r][0] < rowKey ) {
214 // cout << Data[r][0] << endl;
215 while ( r <= nRows && Data[r][0] <= rowKey ) { r++; }
216 if ( r > nRows ) r = nRows;
219 if ( c > 2 && Data[0][c-1] > colKey ) {
220 while( Data[0][c-1] > colKey && c > 2) { c--; }
221 } else if ( Data[0][c] < colKey ) {
222 while( Data[0][c] <= colKey && c <= nCols) { c++; }
223 if ( c > nCols ) c = nCols;
229 rFactor = (rowKey - Data[r-1][0]) / (Data[r][0] - Data[r-1][0]);
230 cFactor = (colKey - Data[0][c-1]) / (Data[0][c] - Data[0][c-1]);
232 if (rFactor > 1.0) rFactor = 1.0;
233 else if (rFactor < 0.0) rFactor = 0.0;
235 if (cFactor > 1.0) cFactor = 1.0;
236 else if (cFactor < 0.0) cFactor = 0.0;
238 col1temp = rFactor*(Data[r][c-1] - Data[r-1][c-1]) + Data[r-1][c-1];
239 col2temp = rFactor*(Data[r][c] - Data[r-1][c]) + Data[r-1][c];
241 Value = col1temp + cFactor*(col2temp - col1temp);
246 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248 double FGTable::GetValue(double rowKey, double colKey, double tableKey)
250 double Factor, Value, Span;
253 //if the key is off the end (or before the beginning) of the table,
254 // just return the boundary-table value, do not extrapolate
256 if( tableKey <= Data[1][1] ) {
258 return Tables[0].GetValue(rowKey, colKey);
259 } else if ( tableKey >= Data[nRows][1] ) {
261 return Tables[nRows-1].GetValue(rowKey, colKey);
264 // the key is somewhere in the middle, search for the right breakpoint
265 // assume the correct breakpoint has not changed since last frame or
266 // has only changed very little
268 if ( r > 2 && Data[r-1][1] > tableKey ) {
269 while( Data[r-1][1] > tableKey && r > 2) { r--; }
270 } else if ( Data[r][1] < tableKey ) {
271 while( Data[r][1] <= tableKey && r <= nRows) { r++; }
275 // make sure denominator below does not go to zero.
277 Span = Data[r][1] - Data[r-1][1];
279 Factor = (tableKey - Data[r-1][1]) / Span;
280 if (Factor > 1.0) Factor = 1.0;
285 Value = Factor*(Tables[r-1].GetValue(rowKey, colKey) - Tables[r-2].GetValue(rowKey, colKey))
286 + Tables[r-2].GetValue(rowKey, colKey);
291 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293 void FGTable::operator<<(FGConfigFile& infile)
299 if (Type == tt1D || Type == tt3D) startRow = 1;
300 if (Type == tt3D) startCol = 1;
302 for (int r=startRow; r<=nRows; r++) {
303 for (int c=startCol; c<=nCols; c++) {
304 if (r != 0 || c != 0) {
305 infile >> Data[r][c];
307 Tables[tableCtr] << infile;
315 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
317 FGTable& FGTable::operator<<(const double n)
319 Data[rowCounter][colCounter] = n;
320 if (colCounter == nCols) {
329 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
331 FGTable& FGTable::operator<<(const int n)
337 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
339 void FGTable::Print(int spaces)
345 if (Type == tt1D || Type == tt3D) startRow = 1;
346 if (Type == tt3D) startCol = 1;
348 #if defined (sgi) && !defined(__GNUC__) && (_COMPILER_VERSION < 740)
349 unsigned long flags = cout.setf(ios::fixed);
351 ios::fmtflags flags = cout.setf(ios::fixed); // set up output stream
354 for (int i=0;i<spaces;i++) tabspace+=" ";
357 for (int r=startRow; r<=nRows; r++) {
359 for (int c=startCol; c<=nCols; c++) {
360 if (r == 0 && c == 0) {
363 cout << Data[r][c] << " ";
366 Tables[r-1].Print(spaces);
372 cout.setf(flags); // reset
375 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
376 // The bitmasked value choices are as follows:
377 // unset: In this case (the default) JSBSim would only print
378 // out the normally expected messages, essentially echoing
379 // the config files as they are read. If the environment
380 // variable is not set, debug_lvl is set to 1 internally
381 // 0: This requests JSBSim not to output any messages
383 // 1: This value explicity requests the normal JSBSim
385 // 2: This value asks for a message to be printed out when
386 // a class is instantiated
387 // 4: When this value is set, a message is displayed when a
388 // FGModel object executes its Run() method
389 // 8: When this value is set, various runtime state variables
390 // are printed out periodically
391 // 16: When set various parameters are sanity checked and
392 // a message is printed out when they go out of bounds
394 void FGTable::Debug(int from)
396 if (debug_lvl <= 0) return;
398 if (debug_lvl & 1) { // Standard console startup message output
399 if (from == 0) { // Constructor
403 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
404 if (from == 0) cout << "Instantiated: FGTable" << endl;
405 if (from == 1) cout << "Destroyed: FGTable" << endl;
407 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
409 if (debug_lvl & 8 ) { // Runtime state variables
411 if (debug_lvl & 16) { // Sanity checking
413 if (debug_lvl & 64) {
414 if (from == 0) { // Constructor
415 cout << IdSrc << endl;
416 cout << IdHdr << endl;