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__ )
51 static const char *IdSrc = "$Id$";
52 static const char *IdHdr = ID_TABLE;
54 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
59 FGTable::FGTable(int NRows, int NCols) : nRows(NRows), nCols(NCols)
65 } else if (NCols == 1) {
70 cerr << "FGTable cannot accept 'Rows=0'" << endl;
74 lastRowIndex=lastColumnIndex=2;
78 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 FGTable::FGTable(int NRows) : nRows(NRows), nCols(1)
88 lastRowIndex=lastColumnIndex=2;
91 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93 double** FGTable::Allocate(void)
95 Data = new double*[nRows+1];
96 for (int r=0; r<=nRows; r++) {
97 Data[r] = new double[nCols+1];
98 for (int c=0; c<=nCols; c++) {
105 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109 for (int r=0; r<=nRows; r++) if (Data[r]) delete[] Data[r];
110 if (Data) delete[] Data;
114 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116 double FGTable::GetValue(double key)
118 double Factor, Value, Span;
121 //if the key is off the end of the table, just return the
122 //end-of-table value, do not extrapolate
123 if( key <= Data[1][0] ) {
125 //cout << "Key underneath table: " << key << endl;
127 } else if ( key >= Data[nRows][0] ) {
129 //cout << "Key over table: " << key << endl;
130 return Data[nRows][1];
133 // the key is somewhere in the middle, search for the right breakpoint
134 // assume the correct breakpoint has not changed since last frame or
135 // has only changed very little
137 if ( r > 2 && Data[r-1][0] > key ) {
138 while( Data[r-1][0] > key && r > 2) { r--; }
139 } else if ( Data[r][0] < key ) {
140 while( Data[r][0] <= key && r <= nRows) { r++; }
144 // make sure denominator below does not go to zero.
146 Span = Data[r][0] - Data[r-1][0];
148 Factor = (key - Data[r-1][0]) / Span;
149 if (Factor > 1.0) Factor = 1.0;
154 Value = Factor*(Data[r][1] - Data[r-1][1]) + Data[r-1][1];
159 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
162 double FGTable::GetValue(double rowKey, double colKey)
164 double rFactor, cFactor, col1temp, col2temp, Value;
166 int c=lastColumnIndex;
168 if ( r > 2 && Data[r-1][0] > rowKey ) {
169 while ( Data[r-1][0] > rowKey && r > 2) { r--; }
170 } else if ( Data[r][0] < rowKey ) {
171 // cout << Data[r][0] << endl;
172 while ( r <= nRows && Data[r][0] <= rowKey ) { r++; }
173 if ( r > nRows ) r = nRows;
176 if ( c > 2 && Data[0][c-1] > colKey ) {
177 while( Data[0][c-1] > colKey && c > 2) { c--; }
178 } else if ( Data[0][c] < colKey ) {
179 while( Data[0][c] <= colKey && c <= nCols) { c++; }
180 if ( c > nCols ) c = nCols;
186 rFactor = (rowKey - Data[r-1][0]) / (Data[r][0] - Data[r-1][0]);
187 cFactor = (colKey - Data[0][c-1]) / (Data[0][c] - Data[0][c-1]);
189 if (rFactor > 1.0) rFactor = 1.0;
190 else if (rFactor < 0.0) rFactor = 0.0;
192 if (cFactor > 1.0) cFactor = 1.0;
193 else if (cFactor < 0.0) cFactor = 0.0;
195 col1temp = rFactor*(Data[r][c-1] - Data[r-1][c-1]) + Data[r-1][c-1];
196 col2temp = rFactor*(Data[r][c] - Data[r-1][c]) + Data[r-1][c];
198 Value = col1temp + cFactor*(col2temp - col1temp);
203 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
205 void FGTable::operator<<(FGConfigFile& infile)
209 if (Type == tt1D) startRow = 1;
212 for (int r=startRow; r<=nRows; r++) {
213 for (int c=0; c<=nCols; c++) {
214 if (r != 0 || c != 0) {
215 infile >> Data[r][c];
221 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223 FGTable& FGTable::operator<<(const double n)
225 Data[rowCounter][colCounter] = n;
226 if (colCounter == nCols) {
235 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
237 FGTable& FGTable::operator<<(const int n)
243 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245 void FGTable::Print(void)
249 if (Type == tt1D) startRow = 1;
252 #if defined (sgi) && !defined(__GNUC__)
253 unsigned long flags = cout.setf(ios::fixed);
255 ios::fmtflags flags = cout.setf(ios::fixed); // set up output stream
260 for (int r=startRow; r<=nRows; r++) {
262 for (int c=0; c<=nCols; c++) {
263 if (r == 0 && c == 0) {
266 cout << Data[r][c] << " ";
271 cout.setf(flags); // reset
274 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275 // The bitmasked value choices are as follows:
276 // unset: In this case (the default) JSBSim would only print
277 // out the normally expected messages, essentially echoing
278 // the config files as they are read. If the environment
279 // variable is not set, debug_lvl is set to 1 internally
280 // 0: This requests JSBSim not to output any messages
282 // 1: This value explicity requests the normal JSBSim
284 // 2: This value asks for a message to be printed out when
285 // a class is instantiated
286 // 4: When this value is set, a message is displayed when a
287 // FGModel object executes its Run() method
288 // 8: When this value is set, various runtime state variables
289 // are printed out periodically
290 // 16: When set various parameters are sanity checked and
291 // a message is printed out when they go out of bounds
293 void FGTable::Debug(int from)
295 if (debug_lvl <= 0) return;
297 if (debug_lvl & 1) { // Standard console startup message output
298 if (from == 0) { // Constructor
302 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
303 if (from == 0) cout << "Instantiated: FGTable" << endl;
304 if (from == 1) cout << "Destroyed: FGTable" << endl;
306 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
308 if (debug_lvl & 8 ) { // Runtime state variables
310 if (debug_lvl & 16) { // Sanity checking
312 if (debug_lvl & 64) {
313 if (from == 0) { // Constructor
314 cout << IdSrc << endl;
315 cout << IdHdr << endl;