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