]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGTable.cpp
Added flap_deflection so that remote fdm can pass back actual flap deflection
[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
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 }
86
87 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88
89 double** FGTable::Allocate(void)
90 {
91   Data = new double*[nRows+1];
92   for (int r=0; r<=nRows; r++) {
93     Data[r] = new double[nCols+1];
94     for (int c=0; c<=nCols; c++) {
95       Data[r][c] = 0.0;
96     }
97   }
98   return Data;
99 }
100
101 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102
103 FGTable::~FGTable()
104 {
105   for (int r=0; r<=nRows; r++) if (Data[r]) delete[] Data[r];
106   if (Data) delete[] Data;
107   Debug(1);
108 }
109
110 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111
112 double FGTable::GetValue(double key)
113 {
114   double Factor, Value, Span;
115   int r;
116
117   for (r=1; r<=nRows; r++) if (Data[r][0] >= key) break;
118   r   = Clamp(2, r, nRows);
119   key = Clamp(Data[1][0], key, Data[nRows][0]);
120
121   // make sure denominator below does not go to zero.
122
123   Span = Data[r][0] - Data[r-1][0];
124   if (Span != 0.0) {
125     Factor = (key - Data[r-1][0]) / Span;
126     if (Factor > 1.0) Factor = 1.0;
127   } else {
128     Factor = 1.0;
129   }
130
131   Value = Factor*(Data[r][1] - Data[r-1][1]) + Data[r-1][1];
132
133   return Value;
134 }
135
136 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137
138 double FGTable::GetValue(double rowKey, double colKey)
139 {
140   double rFactor, cFactor, col1temp, col2temp, Value;
141   int r, c;
142
143   for (r=1;r<=nRows;r++) if (Data[r][0] >= rowKey) break;
144   r = Clamp(2, r, nRows);
145   rowKey = Clamp(Data[1][0], rowKey, Data[nRows][0]);
146
147   for (c=1;c<=nCols;c++) if (Data[0][c] >= colKey) break;
148   c = Clamp(2, c, nCols);
149   colKey = Clamp(Data[0][1], colKey, Data[0][nCols]);
150
151   rFactor = (rowKey - Data[r-1][0]) / (Data[r][0] - Data[r-1][0]);
152   cFactor = (colKey - Data[0][c-1]) / (Data[0][c] - Data[0][c-1]);
153
154   if (rFactor > 1.0) rFactor = 1.0;
155   else if (rFactor < 0.0) rFactor = 0.0;
156
157   if (cFactor > 1.0) cFactor = 1.0;
158   else if (cFactor < 0.0) cFactor = 0.0;
159
160   col1temp = rFactor*(Data[r][c-1] - Data[r-1][c-1]) + Data[r-1][c-1];
161   col2temp = rFactor*(Data[r][c] - Data[r-1][c]) + Data[r-1][c];
162
163   Value = col1temp + cFactor*(col2temp - col1temp);
164
165   return Value;
166 }
167
168 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
169
170 void FGTable::operator<<(FGConfigFile& infile)
171 {
172   int startRow;
173
174   if (Type == tt1D) startRow = 1;
175   else startRow = 0;
176
177   for (int r=startRow; r<=nRows; r++) {
178     for (int c=0; c<=nCols; c++) {
179       if (r != 0 || c != 0) {
180         infile >> Data[r][c];
181       }
182     }
183   }
184 }
185
186 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187
188 FGTable& FGTable::operator<<(const double n)
189 {
190   Data[rowCounter][colCounter] = n;
191   if (colCounter == nCols) {
192     colCounter = 0;
193     rowCounter++;
194   } else {
195     colCounter++;
196   }
197   return *this;
198 }
199
200 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
201
202 FGTable& FGTable::operator<<(const int n)
203 {
204   *this << (double)n;
205   return *this;
206 }
207
208 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209
210 void FGTable::Print(void)
211 {
212   int startRow;
213
214   if (Type == tt1D) startRow = 1;
215   else startRow = 0;
216
217 #if defined (sgi) && !defined(__GNUC__)
218   unsigned long flags = cout.setf(ios::fixed);
219 #else
220   ios::fmtflags flags = cout.setf(ios::fixed); // set up output stream
221 #endif
222
223   cout.precision(4);
224
225   for (int r=startRow; r<=nRows; r++) {
226     cout << "   ";
227     for (int c=0; c<=nCols; c++) {
228       if (r == 0 && c == 0) {
229       cout << " ";
230       } else {
231       cout << Data[r][c] << "   ";
232       }
233     }
234     cout << endl;
235   }
236   cout.setf(flags); // reset
237 }
238
239 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240 //    The bitmasked value choices are as follows:
241 //    unset: In this case (the default) JSBSim would only print
242 //       out the normally expected messages, essentially echoing
243 //       the config files as they are read. If the environment
244 //       variable is not set, debug_lvl is set to 1 internally
245 //    0: This requests JSBSim not to output any messages
246 //       whatsoever.
247 //    1: This value explicity requests the normal JSBSim
248 //       startup messages
249 //    2: This value asks for a message to be printed out when
250 //       a class is instantiated
251 //    4: When this value is set, a message is displayed when a
252 //       FGModel object executes its Run() method
253 //    8: When this value is set, various runtime state variables
254 //       are printed out periodically
255 //    16: When set various parameters are sanity checked and
256 //       a message is printed out when they go out of bounds
257
258 void FGTable::Debug(int from)
259 {
260   if (debug_lvl <= 0) return;
261
262   if (debug_lvl & 1) { // Standard console startup message output
263     if (from == 0) { // Constructor
264
265     }
266   }
267   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
268     if (from == 0) cout << "Instantiated: FGTable" << endl;
269     if (from == 1) cout << "Destroyed:    FGTable" << endl;
270   }
271   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
272   }
273   if (debug_lvl & 8 ) { // Runtime state variables
274   }
275   if (debug_lvl & 16) { // Sanity checking
276   }
277   if (debug_lvl & 64) {
278     if (from == 0) { // Constructor
279       cout << IdSrc << endl;
280       cout << IdHdr << endl;
281     }
282   }
283 }
284
285
286