]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGTable.cpp
JSBSim tweaks.
[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 #include <iomanip>
41
42 static const char *IdSrc = "$Id$";
43 static const char *IdHdr = ID_TABLE;
44
45 extern short debug_lvl;
46
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 CLASS IMPLEMENTATION
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
50
51 using namespace std;
52
53 FGTable::FGTable(int NRows, int NCols) : nRows(NRows), nCols(NCols)
54 {
55   Type = tt2D;
56   colCounter = 1;
57   rowCounter = 0;
58
59   Data = Allocate();
60
61   if (debug_lvl & 2) cout << "Instantiated: FGTable" << endl;
62 }
63
64 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65
66 FGTable::FGTable(int NRows) : nRows(NRows), nCols(1)
67 {
68   Type = tt1D;
69   colCounter = 0;
70   rowCounter = 1;
71
72   Data = Allocate();
73   if (debug_lvl & 2) cout << "Instantiated: FGTable" << endl;
74 }
75
76 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77
78 float** FGTable::Allocate(void)
79 {
80   Data = new float*[nRows+1];
81   for (int r=0; r<=nRows; r++) {
82     Data[r] = new float[nCols+1];
83     for (int c=0; c<=nCols; c++) {
84       Data[r][c] = 0.0;
85     }
86   }
87   return Data;
88 }
89
90 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91
92 FGTable::~FGTable()
93 {
94   for (int r=0; r<=nRows; r++) if (Data[r]) delete Data[r];
95   if (Data) delete Data;
96   if (debug_lvl & 2) cout << "Destroyed:    FGTable" << endl;
97 }
98
99 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100
101 float FGTable::GetValue(float key)
102 {
103   float Factor, Value, Span;
104   int r;
105
106   for (r=1; r<=nRows; r++) if (Data[r][0] >= key) break;
107   r = r < 2 ? 2 : (r > nRows ? nRows : r);
108
109   // make sure denominator below does not go to zero.
110
111   Span = Data[r][0] - Data[r-1][0];
112   if (Span != 0.0) {
113     Factor = (key - Data[r-1][0]) / Span;
114     if (Factor > 1.0) Factor = 1.0;
115   } else {
116     Factor = 1.0;
117   }
118
119   Value = Factor*(Data[r][1] - Data[r-1][1]) + Data[r-1][1];
120
121   return Value;
122 }
123
124 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125
126 float FGTable::GetValue(float rowKey, float colKey)
127 {
128   float rFactor, cFactor, col1temp, col2temp, Value;
129   int r, c;
130
131   for (r=1;r<=nRows;r++) if (Data[r][0] >= rowKey) break;
132   for (c=1;c<=nCols;c++) if (Data[0][c] >= colKey) break;
133
134   c = c < 2 ? 2 : (c > nCols ? nCols : c);
135   r = r < 2 ? 2 : (r > nRows ? nRows : r);
136
137   rFactor = (rowKey - Data[r-1][0]) / (Data[r][0] - Data[r-1][0]);
138   cFactor = (colKey - Data[0][c-1]) / (Data[0][c] - Data[0][c-1]);
139
140   if (rFactor > 1.0) rFactor = 1.0;
141   else if (rFactor < 0.0) rFactor = 0.0;
142
143   if (cFactor > 1.0) cFactor = 1.0;
144   else if (cFactor < 0.0) cFactor = 0.0;
145
146   col1temp = rFactor*(Data[r][c-1] - Data[r-1][c-1]) + Data[r-1][c-1];
147   col2temp = rFactor*(Data[r][c] - Data[r-1][c]) + Data[r-1][c];
148
149   Value = col1temp + cFactor*(col2temp - col1temp);
150
151   return Value;
152 }
153
154 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155
156 void FGTable::operator<<(FGConfigFile& infile)
157 {
158   int startRow;
159
160   if (Type == tt1D) startRow = 1;
161   else startRow = 0;
162
163   for (int r=startRow; r<=nRows; r++) {
164     for (int c=0; c<=nCols; c++) {
165       if (r != 0 || c != 0) {
166         infile >> Data[r][c];
167       }
168     }
169   }
170 }
171
172 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
173
174
175 void FGTable::Print(void)
176 {
177   int startRow;
178
179   if (Type == tt1D) startRow = 1;
180   else startRow = 0;
181
182   cout.setf(ios::fixed); // set up output stream
183   cout.precision(4);
184
185   for (int r=startRow; r<=nRows; r++) {
186     cout << "   ";
187     for (int c=0; c<=nCols; c++) {
188       if (r == 0 && c == 0) {
189       cout << " ";
190       } else {
191       cout << Data[r][c] << "   ";
192       }
193     }
194     cout << endl;
195   }
196   cout.setf(0, ios::floatfield); // reset
197 }
198
199 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
200
201 void FGTable::Debug(void)
202 {
203     //TODO: Add your source code here
204 }
205
206