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__ )
47 static const char *IdSrc = "$Id$";
48 static const char *IdHdr = ID_TABLE;
50 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
56 FGTable::FGTable(int NRows, int NCols) : nRows(NRows), nCols(NCols)
62 } else if (NCols == 1) {
67 cerr << "FGTable cannot accept 'Rows=0'" << endl;
71 lastRowIndex=lastColumnIndex=2;
75 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 FGTable::FGTable(int NRows) : nRows(NRows), nCols(1)
85 lastRowIndex=lastColumnIndex=2;
88 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 double** FGTable::Allocate(void)
92 Data = new double*[nRows+1];
93 for (int r=0; r<=nRows; r++) {
94 Data[r] = new double[nCols+1];
95 for (int c=0; c<=nCols; c++) {
102 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 for (int r=0; r<=nRows; r++) if (Data[r]) delete[] Data[r];
107 if (Data) delete[] Data;
111 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113 double FGTable::GetValue(double key)
115 double Factor, Value, Span;
118 //if the key is off the end of the table, just return the
119 //end-of-table value, do not extrapolate
120 if( key <= Data[1][0] ) {
122 //cout << "Key underneath table: " << key << endl;
124 } else if ( key >= Data[nRows][0] ) {
126 //cout << "Key over table: " << key << endl;
127 return Data[nRows][1];
130 // the key is somewhere in the middle, search for the right breakpoint
131 // assume the correct breakpoint has not changed since last frame or
132 // has only changed very little
134 if ( r > 2 && Data[r-1][0] > key ) {
135 while( Data[r-1][0] > key && r > 2) { r--; }
136 } else if ( Data[r][0] < key ) {
137 while( Data[r][0] <= key && r <= nRows) { r++; }
141 // make sure denominator below does not go to zero.
143 Span = Data[r][0] - Data[r-1][0];
145 Factor = (key - Data[r-1][0]) / Span;
146 if (Factor > 1.0) Factor = 1.0;
151 Value = Factor*(Data[r][1] - Data[r-1][1]) + Data[r-1][1];
156 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159 double FGTable::GetValue(double rowKey, double colKey)
161 double rFactor, cFactor, col1temp, col2temp, Value;
163 int c=lastColumnIndex;
165 if ( r > 2 && Data[r-1][0] > rowKey ) {
166 while ( Data[r-1][0] > rowKey && r > 2) { r--; }
167 } else if ( Data[r][0] < rowKey ) {
168 // cout << Data[r][0] << endl;
169 while ( r <= nRows && Data[r][0] <= rowKey ) { r++; }
170 if ( r > nRows ) r = nRows;
173 if ( c > 2 && Data[0][c-1] > colKey ) {
174 while( Data[0][c-1] > colKey && c > 2) { c--; }
175 } else if ( Data[0][c] < colKey ) {
176 while( Data[0][c] <= colKey && c <= nCols) { c++; }
177 if ( c > nCols ) c = nCols;
183 rFactor = (rowKey - Data[r-1][0]) / (Data[r][0] - Data[r-1][0]);
184 cFactor = (colKey - Data[0][c-1]) / (Data[0][c] - Data[0][c-1]);
186 if (rFactor > 1.0) rFactor = 1.0;
187 else if (rFactor < 0.0) rFactor = 0.0;
189 if (cFactor > 1.0) cFactor = 1.0;
190 else if (cFactor < 0.0) cFactor = 0.0;
192 col1temp = rFactor*(Data[r][c-1] - Data[r-1][c-1]) + Data[r-1][c-1];
193 col2temp = rFactor*(Data[r][c] - Data[r-1][c]) + Data[r-1][c];
195 Value = col1temp + cFactor*(col2temp - col1temp);
200 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202 void FGTable::operator<<(FGConfigFile& infile)
206 if (Type == tt1D) startRow = 1;
209 for (int r=startRow; r<=nRows; r++) {
210 for (int c=0; c<=nCols; c++) {
211 if (r != 0 || c != 0) {
212 infile >> Data[r][c];
218 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
220 FGTable& FGTable::operator<<(const double n)
222 Data[rowCounter][colCounter] = n;
223 if (colCounter == nCols) {
232 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234 FGTable& FGTable::operator<<(const int n)
240 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
242 void FGTable::Print(void)
246 if (Type == tt1D) startRow = 1;
249 #if defined (sgi) && !defined(__GNUC__)
250 unsigned long flags = cout.setf(ios::fixed);
252 ios::fmtflags flags = cout.setf(ios::fixed); // set up output stream
257 for (int r=startRow; r<=nRows; r++) {
259 for (int c=0; c<=nCols; c++) {
260 if (r == 0 && c == 0) {
263 cout << Data[r][c] << " ";
268 cout.setf(flags); // reset
271 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272 // The bitmasked value choices are as follows:
273 // unset: In this case (the default) JSBSim would only print
274 // out the normally expected messages, essentially echoing
275 // the config files as they are read. If the environment
276 // variable is not set, debug_lvl is set to 1 internally
277 // 0: This requests JSBSim not to output any messages
279 // 1: This value explicity requests the normal JSBSim
281 // 2: This value asks for a message to be printed out when
282 // a class is instantiated
283 // 4: When this value is set, a message is displayed when a
284 // FGModel object executes its Run() method
285 // 8: When this value is set, various runtime state variables
286 // are printed out periodically
287 // 16: When set various parameters are sanity checked and
288 // a message is printed out when they go out of bounds
290 void FGTable::Debug(int from)
292 if (debug_lvl <= 0) return;
294 if (debug_lvl & 1) { // Standard console startup message output
295 if (from == 0) { // Constructor
299 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
300 if (from == 0) cout << "Instantiated: FGTable" << endl;
301 if (from == 1) cout << "Destroyed: FGTable" << endl;
303 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
305 if (debug_lvl & 8 ) { // Runtime state variables
307 if (debug_lvl & 16) { // Sanity checking
309 if (debug_lvl & 64) {
310 if (from == 0) { // Constructor
311 cout << IdSrc << endl;
312 cout << IdHdr << endl;