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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
72 USA or view http://www.gnu.org/copyleft/gpl.html.
74 **********************************************************************/
75 #include <simgear/compiler.h> // MSVC: to disable C4244 d to f warning
77 #include "uiuc_2Dinterpolation.h"
80 double uiuc_2Dinterpolation( double xData[100][100],
82 double zData[100][100],
89 double x11=0, x12=0, x21=0, x22=0, y1=0, y2=0;
90 double z11=0, z12=0, z21=0, z22=0, z1=0, z2=0;
91 double L11=0, L12=0, L21=0, L22=0, L1=0, L2=0;
95 bool luby=false; //upper bound on y (deflection)
96 bool llby=false; //lower bound on y
97 bool lubx1=false; //upper x bound on lower y
98 bool llbx1=false; //lower x bound on lower y
99 bool lubx2=false; //upper x bound on upper y
100 bool llbx2=false; //lower x bound on upper y
102 // check bounds on y (control deflection) to see if data range encloses it
103 // NOTE: [1] is first element of all arrays, [0] not used
104 if (y <= yData[1]) //if y less than lowest y
111 else if (y >= yData[ymax]) //if y greater than greatest y
119 else //y between ymax and ymin
121 /* loop increases j until y is less than a known y,
122 e.g. elevator from LaRCsim less than de given in
123 tabulated data; once this value is found, j becomes
124 the upper bound and j-1 the lower bound on y */
125 while (yData[j] <= y) //bracket upper bound
129 y2 = yData[j]; //set upper bound on y
130 y1 = yData[j-1]; //set lower bound on y
132 xmaxu = xmax[j]; //set max x on upper y
133 xmaxl = xmax[j-1]; //set max x on lower y
136 //check bounds on x (alpha) to see if data range encloses it
137 //x less than lowest x on lower y curve:
138 if (x <= xData[j-1][1])
144 //x greater than greatest x on lower y curve:
145 if (x >= xData[j-1][xmaxl])
148 z12 = zData[j-1][xmaxl];
151 //x less than lowest x on upper y curve:
152 if (x <= xData[j][1])
158 //x greater than greatest x on upper y curve:
159 if (x >= xData[j][xmaxu])
162 z22 = zData[j][xmaxu];
166 //x between xmax and x min
167 //interpolate on lower y-curve
168 if (llbx1 == false && lubx1 == false)
170 /* loop increases i until x is less than a known x,
171 e.g. Alpha from LaRCsim less than Alpha given in
172 tabulated data; once this value is found, i becomes
173 the upper bound and i-1 the lower bound */
174 //bracket x bounds on lower y curve
175 while (xData[j-1][i] <= x)
179 x12 = xData[j-1][i]; //set upper x and z on lower y
181 x11 = xData[j-1][i-1]; //set lower x and z on lower y
182 z11 = zData[j-1][i-1];
184 //do linear interpolation on x1 terms (lower y-curve)
185 L11 = (x - x12) / (x11 - x12);
186 L12 = (x - x11) / (x12 - x11);
187 z1 = L11 * z11 + L12 * z12;
189 //interpolate on upper y-curve
190 if (llbx2 == false && lubx2 == false)
192 //bracket x bounds on upper y curve
194 while (xData[j][i] <= x)
198 x22 = xData[j][i]; //set upper x and z on upper y
200 x21 = xData[j][i-1]; //set lower x and z on upper y
203 //do linear interpolation on x2 terms (upper y-curve)
204 L21 = (x - x22) / (x21 - x22);
205 L22 = (x - x21) / (x22 - x21);
206 z2 = L21 * z21 + L22 * z22;
209 //now have all data needed to find coefficient, check cases:
212 if (llbx1 == true || llbx2 == true)
217 if (lubx1 == true || lubx2 == true)
227 else if (luby == true)
229 if (llbx1 == true || llbx2 == true)
234 if (lubx1 == true || lubx2 == true)
245 //do linear interpolation on y terms
246 L1 = (y - y2) / (y1 - y2);
247 L2 = (y - y1) / (y2 - y1);
250 zfxy = L1 * z1 + L2 * z2;
255 // end uiuc_2Dinterpolation.cpp