]> git.mxchange.org Git - flightgear.git/blob - GenAirports/area.cxx
Removed forced -g compile flag.
[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         // (*current).theta -= angle;
132         // while ( (*current).theta > FG_2PI ) {
133         //     (*current).theta -= FG_2PI;
134         }
135         // printf("(%.2f, %.2f)\n", current->theta, current->dist);
136     }
137     // printf("\n");
138
139     // find actual lon,lat of coordinates
140     // printf("convert to lon, lat relative to %.2f %.2f\n", 
141     //        origin.lon, origin.lat);
142     current = rad_list.begin();
143     last = rad_list.end();
144     for ( ; current != last ; ++current ) {
145         p = calc_lon_lat(origin_rad, *current);
146         // convert from radians to degress
147         p.lon *= RAD_TO_DEG;
148         p.lat *= RAD_TO_DEG;
149         // printf("(%.8f, %.8f)\n", p.lon, p.lat);
150         result_list.push_back(p);
151     }
152     // printf("\n");
153
154     return result_list;
155 }
156
157
158 // generate an area for a runway
159 list < point2d >
160 gen_runway_area( double lon, double lat, double heading, 
161                       double length, double width) 
162 {
163     list < point2d > result_list;
164     list < point2d > tmp_list;
165     list < point2d > :: iterator current;                          
166     list < point2d > :: iterator last;                    
167
168     point2d p;
169     point2d origin;
170     double l, w;
171     int i;
172
173     /*
174     printf("runway: lon = %.2f lat = %.2f hdg = %.2f len = %.2f width = %.2f\n",
175            lon, lat, heading, length, width);
176     */
177
178     origin.lon = lon;
179     origin.lat = lat;
180     l = length / 2.0;
181     w = width / 2.0;
182
183     // generate untransformed runway area vertices
184     p.x =  l; p.y =  w; tmp_list.push_back(p);
185     p.x =  l; p.y = -w; tmp_list.push_back(p);
186     p.x = -l; p.y = -w; tmp_list.push_back(p);
187     p.x = -l; p.y =  w; tmp_list.push_back(p);
188
189     /*
190     // display points
191     printf("Untransformed, unrotated runway\n");
192     current = tmp_list.begin();
193     last = tmp_list.end();
194     while ( current != last ) {
195         printf("(%.2f, %.2f)\n", current->x, current->y);
196         ++current;
197     }
198     printf("\n");
199     */
200
201     // rotate, transform, and convert points to lon, lat in degrees
202     result_list = gen_area(origin, heading, tmp_list);
203
204     /*
205     // display points
206     printf("Results in radians.\n");
207     current = result_list.begin();
208     last = result_list.end();
209     while ( current != last ) {
210         printf("(%.8f, %.8f)\n", current->lon, current->lat);
211         ++current;
212     }
213     printf("\n");
214     */
215
216     return result_list;
217 }
218
219
220 // $Log$
221 // Revision 1.5  1998/10/20 15:49:54  curt
222 // tweak ...
223 //
224 // Revision 1.4  1998/09/09 20:59:53  curt
225 // Loop construct tweaks for STL usage.
226 // Output airport file to be used to generate airport scenery on the fly
227 //   by the run time sim.
228 //
229 // Revision 1.3  1998/09/09 16:26:31  curt
230 // Continued progress in implementing the convex hull algorithm.
231 //
232 // Revision 1.2  1998/09/04 23:04:48  curt
233 // Beginning of convex hull genereration routine.
234 //
235 // Revision 1.1  1998/09/01 19:34:33  curt
236 // Initial revision.
237 //
238 // Revision 1.1  1998/07/20 12:54:05  curt
239 // Initial revision.
240 //
241 //