1 /**********************************************************************
3 FILENAME: uiuc_2Dinterpolation.cpp
5 ----------------------------------------------------------------------
7 DESCRIPTION: reads in the zData, yData, and xData arrays and the
8 values of x and y to be interpolated on; performs 2D
9 interpolation, i.e. z=f(x,y)
11 ----------------------------------------------------------------------
15 ----------------------------------------------------------------------
17 REFERENCES: similar to 2D interpolation in Selig's propid code
19 mathematics based on linear interpolation functions
20 (see 1Dinterpolation.cpp for references)
22 ----------------------------------------------------------------------
24 HISTORY: 02/06/2000 initial release
26 ----------------------------------------------------------------------
28 AUTHOR(S): Jeff Scott <jscott@mail.com>
30 ----------------------------------------------------------------------
34 ----------------------------------------------------------------------
36 INPUTS: -2D array of x data for each y case
38 -2D array of z data for each y case
39 -1D array of max number of x-z data sets for each y case
41 -x value to be interpolated on
42 -y value to be interpolated on
44 ----------------------------------------------------------------------
46 OUTPUTS: -z as function of x and y
48 ----------------------------------------------------------------------
50 CALLED BY: uiuc_coefficients.cpp
52 ----------------------------------------------------------------------
56 ----------------------------------------------------------------------
58 COPYRIGHT: (C) 2000 by Michael Selig
60 This program is free software; you can redistribute it and/or
61 modify it under the terms of the GNU General Public License
62 as published by the Free Software Foundation.
64 This program is distributed in the hope that it will be useful,
65 but WITHOUT ANY WARRANTY; without even the implied warranty of
66 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
67 GNU General Public License for more details.
69 You should have received a copy of the GNU General Public License
70 along with this program; if not, write to the Free Software
71 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
73 **********************************************************************/
74 #include <simgear/compiler.h> // MSVC: to disable C4244 d to f warning
76 #include "uiuc_2Dinterpolation.h"
79 double uiuc_2Dinterpolation( double xData[100][100],
81 double zData[100][100],
88 double x11=0, x12=0, x21=0, x22=0, y1=0, y2=0;
89 double z11=0, z12=0, z21=0, z22=0, z1=0, z2=0;
90 double L11=0, L12=0, L21=0, L22=0, L1=0, L2=0;
94 bool luby=false; //upper bound on y (deflection)
95 bool llby=false; //lower bound on y
96 bool lubx1=false; //upper x bound on lower y
97 bool llbx1=false; //lower x bound on lower y
98 bool lubx2=false; //upper x bound on upper y
99 bool llbx2=false; //lower x bound on upper y
101 // check bounds on y (control deflection) to see if data range encloses it
102 // NOTE: [1] is first element of all arrays, [0] not used
103 if (y <= yData[1]) //if y less than lowest y
110 else if (y >= yData[ymax]) //if y greater than greatest y
118 else //y between ymax and ymin
120 /* loop increases j until y is less than a known y,
121 e.g. elevator from LaRCsim less than de given in
122 tabulated data; once this value is found, j becomes
123 the upper bound and j-1 the lower bound on y */
124 while (yData[j] <= y) //bracket upper bound
128 y2 = yData[j]; //set upper bound on y
129 y1 = yData[j-1]; //set lower bound on y
131 xmaxu = xmax[j]; //set max x on upper y
132 xmaxl = xmax[j-1]; //set max x on lower y
135 //check bounds on x (alpha) to see if data range encloses it
136 //x less than lowest x on lower y curve:
137 if (x <= xData[j-1][1])
143 //x greater than greatest x on lower y curve:
144 if (x >= xData[j-1][xmaxl])
147 z12 = zData[j-1][xmaxl];
150 //x less than lowest x on upper y curve:
151 if (x <= xData[j][1])
157 //x greater than greatest x on upper y curve:
158 if (x >= xData[j][xmaxu])
161 z22 = zData[j][xmaxu];
165 //x between xmax and x min
166 //interpolate on lower y-curve
167 if (llbx1 == false && lubx1 == false)
169 /* loop increases i until x is less than a known x,
170 e.g. Alpha from LaRCsim less than Alpha given in
171 tabulated data; once this value is found, i becomes
172 the upper bound and i-1 the lower bound */
173 //bracket x bounds on lower y curve
174 while (xData[j-1][i] <= x)
178 x12 = xData[j-1][i]; //set upper x and z on lower y
180 x11 = xData[j-1][i-1]; //set lower x and z on lower y
181 z11 = zData[j-1][i-1];
183 //do linear interpolation on x1 terms (lower y-curve)
184 L11 = (x - x12) / (x11 - x12);
185 L12 = (x - x11) / (x12 - x11);
186 z1 = L11 * z11 + L12 * z12;
188 //interpolate on upper y-curve
189 if (llbx2 == false && lubx2 == false)
191 //bracket x bounds on upper y curve
193 while (xData[j][i] <= x)
197 x22 = xData[j][i]; //set upper x and z on upper y
199 x21 = xData[j][i-1]; //set lower x and z on upper y
202 //do linear interpolation on x2 terms (upper y-curve)
203 L21 = (x - x22) / (x21 - x22);
204 L22 = (x - x21) / (x22 - x21);
205 z2 = L21 * z21 + L22 * z22;
208 //now have all data needed to find coefficient, check cases:
211 if (llbx1 == true || llbx2 == true)
216 if (lubx1 == true || lubx2 == true)
226 else if (luby == true)
228 if (llbx1 == true || llbx2 == true)
233 if (lubx1 == true || lubx2 == true)
244 //do linear interpolation on y terms
245 L1 = (y - y2) / (y1 - y2);
246 L2 = (y - y1) / (y2 - y1);
249 zfxy = L1 * z1 + L2 * z2;
254 // end uiuc_2Dinterpolation.cpp