]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGTable.cpp
Updated to match changes in radiostack.[ch]xx
[flightgear.git] / src / FDM / JSBSim / FGTable.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGTable.cpp
4  Author:       Jon S. Berndt
5  Date started: 1/9/2001
6  Purpose:      Models a lookup table
7
8  ------------- Copyright (C) 2001  Jon S. Berndt (jsb@hal-pc.org) -------------
9
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
13  version.
14
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
18  details.
19
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.
23
24  Further information about the GNU General Public License can also be found on
25  the world wide web at http://www.gnu.org.
26
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
29 Models a lookup table
30
31 HISTORY
32 --------------------------------------------------------------------------------
33 JSB  1/9/00          Created
34
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 INCLUDES
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
38
39 #include "FGTable.h"
40
41 #if defined ( sgi ) && !defined( __GNUC__ )
42 #include <iomanip.h>
43 #else
44 #include <iomanip>
45 #endif
46
47 static const char *IdSrc = "$Id$";
48 static const char *IdHdr = ID_TABLE;
49
50 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
51 CLASS IMPLEMENTATION
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
53
54 using namespace std;
55
56 FGTable::FGTable(int NRows, int NCols) : nRows(NRows), nCols(NCols)
57 {
58   if (NCols > 1) {
59     Type = tt2D;
60     colCounter = 1;
61     rowCounter = 0;
62   } else if (NCols == 1) {
63     Type = tt1D;
64     colCounter = 0;
65     rowCounter = 1;
66   } else {
67     cerr << "FGTable cannot accept 'Rows=0'" << endl;
68   }
69
70   Data = Allocate();
71   lastRowIndex=lastColumnIndex=2;
72   Debug(0);
73 }
74
75 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76
77 FGTable::FGTable(int NRows) : nRows(NRows), nCols(1)
78 {
79   Type = tt1D;
80   colCounter = 0;
81   rowCounter = 1;
82
83   Data = Allocate();
84   Debug(0);
85   lastRowIndex=lastColumnIndex=2;
86 }
87
88 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89
90 double** FGTable::Allocate(void)
91 {
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++) {
96       Data[r][c] = 0.0;
97     }
98   }
99   return Data;
100 }
101
102 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103
104 FGTable::~FGTable()
105 {
106   for (int r=0; r<=nRows; r++) if (Data[r]) delete[] Data[r];
107   if (Data) delete[] Data;
108   Debug(1);
109 }
110
111 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112
113 double FGTable::GetValue(double key)
114 {
115   double Factor, Value, Span;
116   int r=lastRowIndex;
117   
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] ) {
121     lastRowIndex=2;
122     //cout << "Key underneath table: " << key << endl;
123     return Data[1][1];
124   } else if ( key >= Data[nRows][0] ) {
125     lastRowIndex=nRows;
126     //cout << "Key over table: " << key << endl;
127     return Data[nRows][1];
128   }    
129
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
133   
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++; }  
138   }  
139   
140   lastRowIndex=r;  
141   // make sure denominator below does not go to zero.
142   
143   Span = Data[r][0] - Data[r-1][0];
144   if (Span != 0.0) {
145     Factor = (key - Data[r-1][0]) / Span;
146     if (Factor > 1.0) Factor = 1.0;
147   } else {
148     Factor = 1.0;
149   }
150
151   Value = Factor*(Data[r][1] - Data[r-1][1]) + Data[r-1][1];
152
153   return Value;
154 }
155
156 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157
158
159 double FGTable::GetValue(double rowKey, double colKey)
160 {
161   double rFactor, cFactor, col1temp, col2temp, Value;
162   int r=lastRowIndex;
163   int c=lastColumnIndex;
164   
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;  
171   }  
172   
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;  
178   }  
179
180   lastRowIndex=r;
181   lastColumnIndex=c;
182   
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]);
185
186   if (rFactor > 1.0) rFactor = 1.0;
187   else if (rFactor < 0.0) rFactor = 0.0;
188
189   if (cFactor > 1.0) cFactor = 1.0;
190   else if (cFactor < 0.0) cFactor = 0.0;
191
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];
194
195   Value = col1temp + cFactor*(col2temp - col1temp);
196
197   return Value;
198 }
199
200 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
201
202 void FGTable::operator<<(FGConfigFile& infile)
203 {
204   int startRow;
205
206   if (Type == tt1D) startRow = 1;
207   else startRow = 0;
208
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];
213       }
214     }
215   }
216 }
217
218 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219
220 FGTable& FGTable::operator<<(const double n)
221 {
222   Data[rowCounter][colCounter] = n;
223   if (colCounter == nCols) {
224     colCounter = 0;
225     rowCounter++;
226   } else {
227     colCounter++;
228   }
229   return *this;
230 }
231
232 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233
234 FGTable& FGTable::operator<<(const int n)
235 {
236   *this << (double)n;
237   return *this;
238 }
239
240 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241
242 void FGTable::Print(void)
243 {
244   int startRow;
245
246   if (Type == tt1D) startRow = 1;
247   else startRow = 0;
248
249 #if defined (sgi) && !defined(__GNUC__)
250   unsigned long flags = cout.setf(ios::fixed);
251 #else
252   ios::fmtflags flags = cout.setf(ios::fixed); // set up output stream
253 #endif
254
255   cout.precision(4);
256
257   for (int r=startRow; r<=nRows; r++) {
258     cout << "   ";
259     for (int c=0; c<=nCols; c++) {
260       if (r == 0 && c == 0) {
261       cout << " ";
262       } else {
263       cout << Data[r][c] << "   ";
264       }
265     }
266     cout << endl;
267   }
268   cout.setf(flags); // reset
269 }
270
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
278 //       whatsoever.
279 //    1: This value explicity requests the normal JSBSim
280 //       startup messages
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
289
290 void FGTable::Debug(int from)
291 {
292   if (debug_lvl <= 0) return;
293
294   if (debug_lvl & 1) { // Standard console startup message output
295     if (from == 0) { // Constructor
296
297     }
298   }
299   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
300     if (from == 0) cout << "Instantiated: FGTable" << endl;
301     if (from == 1) cout << "Destroyed:    FGTable" << endl;
302   }
303   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
304   }
305   if (debug_lvl & 8 ) { // Runtime state variables
306   }
307   if (debug_lvl & 16) { // Sanity checking
308   }
309   if (debug_lvl & 64) {
310     if (from == 0) { // Constructor
311       cout << IdSrc << endl;
312       cout << IdHdr << endl;
313     }
314   }
315 }
316
317
318