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;
75 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 FGTable::FGTable(int NRows) : nRows(NRows), nCols(1)
87 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89 double** FGTable::Allocate(void)
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++) {
101 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 for (int r=0; r<=nRows; r++) if (Data[r]) delete[] Data[r];
106 if (Data) delete[] Data;
110 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112 double FGTable::GetValue(double key)
114 double Factor, Value, Span;
117 for (r=1; r<=nRows; r++) if (Data[r][0] >= key) break;
118 r = r < 2 ? 2 : (r > nRows ? nRows : r);
120 // make sure denominator below does not go to zero.
122 Span = Data[r][0] - Data[r-1][0];
124 Factor = (key - Data[r-1][0]) / Span;
125 if (Factor > 1.0) Factor = 1.0;
130 Value = Factor*(Data[r][1] - Data[r-1][1]) + Data[r-1][1];
135 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137 double FGTable::GetValue(double rowKey, double colKey)
139 double rFactor, cFactor, col1temp, col2temp, Value;
142 for (r=1;r<=nRows;r++) if (Data[r][0] >= rowKey) break;
143 for (c=1;c<=nCols;c++) if (Data[0][c] >= colKey) break;
145 c = c < 2 ? 2 : (c > nCols ? nCols : c);
146 r = r < 2 ? 2 : (r > nRows ? nRows : r);
148 rFactor = (rowKey - Data[r-1][0]) / (Data[r][0] - Data[r-1][0]);
149 cFactor = (colKey - Data[0][c-1]) / (Data[0][c] - Data[0][c-1]);
151 if (rFactor > 1.0) rFactor = 1.0;
152 else if (rFactor < 0.0) rFactor = 0.0;
154 if (cFactor > 1.0) cFactor = 1.0;
155 else if (cFactor < 0.0) cFactor = 0.0;
157 col1temp = rFactor*(Data[r][c-1] - Data[r-1][c-1]) + Data[r-1][c-1];
158 col2temp = rFactor*(Data[r][c] - Data[r-1][c]) + Data[r-1][c];
160 Value = col1temp + cFactor*(col2temp - col1temp);
165 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167 void FGTable::operator<<(FGConfigFile& infile)
171 if (Type == tt1D) startRow = 1;
174 for (int r=startRow; r<=nRows; r++) {
175 for (int c=0; c<=nCols; c++) {
176 if (r != 0 || c != 0) {
177 infile >> Data[r][c];
183 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
185 FGTable& FGTable::operator<<(const double n)
187 Data[rowCounter][colCounter] = n;
188 if (colCounter == nCols) {
197 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
199 FGTable& FGTable::operator<<(const int n)
205 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
207 void FGTable::Print(void)
211 if (Type == tt1D) startRow = 1;
214 ios::fmtflags flags = cout.setf(ios::fixed); // set up output stream
217 for (int r=startRow; r<=nRows; r++) {
219 for (int c=0; c<=nCols; c++) {
220 if (r == 0 && c == 0) {
223 cout << Data[r][c] << " ";
228 cout.setf(flags); // reset
231 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232 // The bitmasked value choices are as follows:
233 // unset: In this case (the default) JSBSim would only print
234 // out the normally expected messages, essentially echoing
235 // the config files as they are read. If the environment
236 // variable is not set, debug_lvl is set to 1 internally
237 // 0: This requests JSBSim not to output any messages
239 // 1: This value explicity requests the normal JSBSim
241 // 2: This value asks for a message to be printed out when
242 // a class is instantiated
243 // 4: When this value is set, a message is displayed when a
244 // FGModel object executes its Run() method
245 // 8: When this value is set, various runtime state variables
246 // are printed out periodically
247 // 16: When set various parameters are sanity checked and
248 // a message is printed out when they go out of bounds
250 void FGTable::Debug(int from)
252 if (debug_lvl <= 0) return;
254 if (debug_lvl & 1) { // Standard console startup message output
255 if (from == 0) { // Constructor
259 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
260 if (from == 0) cout << "Instantiated: FGTable" << endl;
261 if (from == 1) cout << "Destroyed: FGTable" << endl;
263 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
265 if (debug_lvl & 8 ) { // Runtime state variables
267 if (debug_lvl & 16) { // Sanity checking
269 if (debug_lvl & 64) {
270 if (from == 0) { // Constructor
271 cout << IdSrc << endl;
272 cout << IdHdr << endl;