]> git.mxchange.org Git - flightgear.git/blob - GenAirports/area.cxx
Loop construct tweaks for STL usage.
[flightgear.git] / GenAirports / area.cxx
1 // area.c -- routines to assist with inserting "areas" into FG terrain
2 //
3 // Written by Curtis Olson, started March 1998.
4 //
5 // Copyright (C) 1998  Curtis L. Olson  - curt@me.umn.edu
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22 // (Log is kept at end of this file)
23 //
24
25
26 #include <math.h>
27 #include <stdio.h>
28
29 #include <Include/fg_constants.h>
30
31 #include "area.hxx"
32 #include "point2d.hxx"
33
34
35 // calc new x, y for a rotation
36 double rot_x(double x, double y, double theta) {
37     return ( x * cos(theta) + y * sin(theta) );
38 }
39
40
41 // calc new x, y for a rotation
42 double rot_y(double x, double y, double theta) {
43     return ( -x * sin(theta) + y * cos(theta) );
44 }
45
46
47 // calc new lon/lat given starting lon/lat, and offset radial, and
48 // distance.  NOTE: distance is specified in meters (and converted
49 // internally to radians)
50 point2d calc_lon_lat( point2d orig, point2d offset ) {
51     point2d result;
52
53     // printf("calc_lon_lat()  offset.theta = %.2f offset.dist = %.2f\n",
54     //        offset.theta, offset.dist);
55
56     offset.dist *= METER_TO_NM * NM_TO_RAD;
57
58     result.lat = asin( sin(orig.lat) * cos(offset.dist) + 
59                        cos(orig.lat) * sin(offset.dist) * cos(offset.theta) );
60
61     if ( cos(result.lat) < FG_EPSILON ) {
62         result.lon = orig.lon;      // endpoint a pole
63     } else {
64         result.lon = 
65             fmod(orig.lon - asin( sin(offset.theta) * sin(offset.dist) / 
66                                   cos(result.lat) ) + FG_PI, FG_2PI) - FG_PI;
67     }
68
69     return(result);
70 }
71
72
73 list < point2d >
74 batch_cart_to_polar_2d( list < point2d > in_list)
75 {
76     list < point2d > out_list;
77     list < point2d > :: iterator current;                          
78     list < point2d > :: iterator last;                    
79     point2d p;
80
81     current = in_list.begin();
82     last = in_list.end();
83     for ( ; current != last ; ++current ) {
84         p = cart_to_polar_2d( *current );
85         out_list.push_back(p);
86     }
87
88     return out_list;
89 }
90
91
92 // given a set of 2d coordinates relative to a center point, and the
93 // lon, lat of that center point (specified in degrees), as well as a
94 // potential orientation angle, generate the corresponding lon and lat
95 // of the original 2d verticies.
96 list < point2d >
97 gen_area(point2d origin, double angle, list < point2d > cart_list)
98 {
99     list < point2d > rad_list;
100     list < point2d > result_list;
101     list < point2d > :: iterator current;                          
102     list < point2d > :: iterator last;                    
103     point2d origin_rad, p;
104
105     origin_rad.lon = origin.lon * DEG_TO_RAD;
106     origin_rad.lat = origin.lat * DEG_TO_RAD;
107         
108     // convert to polar coordinates
109     rad_list = batch_cart_to_polar_2d(cart_list);
110
111     /*
112     // display points
113     printf("converted to polar\n");
114     current = rad_list.begin();
115     last = rad_list.end();
116     while ( current != last ) {
117         printf("(%.2f, %.2f)\n", current->theta, current->dist);
118         ++current;
119     }
120     printf("\n");
121     */
122
123     // rotate by specified angle
124     // printf("Rotating points by %.2f\n", angle);
125     current = rad_list.begin();
126     last = rad_list.end();
127     for ( ; current != last ; ++current ) {
128         current->theta -= angle;
129         while ( current->theta > FG_2PI ) {
130             current->theta -= FG_2PI;
131         }
132         // printf("(%.2f, %.2f)\n", current->theta, current->dist);
133     }
134     // printf("\n");
135
136     // find actual lon,lat of coordinates
137     // printf("convert to lon, lat relative to %.2f %.2f\n", 
138     //        origin.lon, origin.lat);
139     current = rad_list.begin();
140     last = rad_list.end();
141     for ( ; current != last ; ++current ) {
142         p = calc_lon_lat(origin_rad, *current);
143         // convert from radians to degress
144         p.lon *= RAD_TO_DEG;
145         p.lat *= RAD_TO_DEG;
146         // printf("(%.8f, %.8f)\n", p.lon, p.lat);
147         result_list.push_back(p);
148     }
149     // printf("\n");
150
151     return result_list;
152 }
153
154
155 // generate an area for a runway
156 list < point2d >
157 gen_runway_area( double lon, double lat, double heading, 
158                       double length, double width) 
159 {
160     list < point2d > result_list;
161     list < point2d > tmp_list;
162     list < point2d > :: iterator current;                          
163     list < point2d > :: iterator last;                    
164
165     point2d p;
166     point2d origin;
167     double l, w;
168     int i;
169
170     /*
171     printf("runway: lon = %.2f lat = %.2f hdg = %.2f len = %.2f width = %.2f\n",
172            lon, lat, heading, length, width);
173     */
174
175     origin.lon = lon;
176     origin.lat = lat;
177     l = length / 2.0;
178     w = width / 2.0;
179
180     // generate untransformed runway area vertices
181     p.x =  l; p.y =  w; tmp_list.push_back(p);
182     p.x =  l; p.y = -w; tmp_list.push_back(p);
183     p.x = -l; p.y = -w; tmp_list.push_back(p);
184     p.x = -l; p.y =  w; tmp_list.push_back(p);
185
186     /*
187     // display points
188     printf("Untransformed, unrotated runway\n");
189     current = tmp_list.begin();
190     last = tmp_list.end();
191     while ( current != last ) {
192         printf("(%.2f, %.2f)\n", current->x, current->y);
193         ++current;
194     }
195     printf("\n");
196     */
197
198     // rotate, transform, and convert points to lon, lat in degrees
199     result_list = gen_area(origin, heading, tmp_list);
200
201     /*
202     // display points
203     printf("Results in radians.\n");
204     current = result_list.begin();
205     last = result_list.end();
206     while ( current != last ) {
207         printf("(%.8f, %.8f)\n", current->lon, current->lat);
208         ++current;
209     }
210     printf("\n");
211     */
212
213     return result_list;
214 }
215
216
217 // $Log$
218 // Revision 1.4  1998/09/09 20:59:53  curt
219 // Loop construct tweaks for STL usage.
220 // Output airport file to be used to generate airport scenery on the fly
221 //   by the run time sim.
222 //
223 // Revision 1.3  1998/09/09 16:26:31  curt
224 // Continued progress in implementing the convex hull algorithm.
225 //
226 // Revision 1.2  1998/09/04 23:04:48  curt
227 // Beginning of convex hull genereration routine.
228 //
229 // Revision 1.1  1998/09/01 19:34:33  curt
230 // Initial revision.
231 //
232 // Revision 1.1  1998/07/20 12:54:05  curt
233 // Initial revision.
234 //
235 //