]> git.mxchange.org Git - flightgear.git/blob - GenAirports/area.cxx
Continued progress in implementing the convex hull algorithm.
[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     while ( current != last ) {
84         p = cart_to_polar_2d( *current );
85         out_list.push_back(p);
86         ++current;
87     }
88
89     return out_list;
90 }
91
92
93 // given a set of 2d coordinates relative to a center point, and the
94 // lon, lat of that center point (specified in degrees), as well as a
95 // potential orientation angle, generate the corresponding lon and lat
96 // of the original 2d verticies.
97 list < point2d >
98 gen_area(point2d origin, double angle, list < point2d > cart_list)
99 {
100     list < point2d > rad_list;
101     list < point2d > result_list;
102     list < point2d > :: iterator current;                          
103     list < point2d > :: iterator last;                    
104     point2d origin_rad, p;
105
106     origin_rad.lon = origin.lon * DEG_TO_RAD;
107     origin_rad.lat = origin.lat * DEG_TO_RAD;
108         
109     // convert to polar coordinates
110     rad_list = batch_cart_to_polar_2d(cart_list);
111
112     /*
113     // display points
114     printf("converted to polar\n");
115     current = rad_list.begin();
116     last = rad_list.end();
117     while ( current != last ) {
118         printf("(%.2f, %.2f)\n", current->theta, current->dist);
119         ++current;
120     }
121     printf("\n");
122     */
123
124     // rotate by specified angle
125     // printf("Rotating points by %.2f\n", angle);
126     current = rad_list.begin();
127     last = rad_list.end();
128     while ( current != last ) {
129         current->theta -= angle;
130         while ( current->theta > FG_2PI ) {
131             current->theta -= FG_2PI;
132         }
133         // printf("(%.2f, %.2f)\n", current->theta, current->dist);
134         ++current;
135     }
136     // printf("\n");
137
138     // find actual lon,lat of coordinates
139     // printf("convert to lon, lat relative to %.2f %.2f\n", 
140     //        origin.lon, origin.lat);
141     current = rad_list.begin();
142     last = rad_list.end();
143     while ( current != last ) {
144         p = calc_lon_lat(origin_rad, *current);
145         // convert from radians to degress
146         p.lon *= RAD_TO_DEG;
147         p.lat *= RAD_TO_DEG;
148         // printf("(%.8f, %.8f)\n", p.lon, p.lat);
149         result_list.push_back(p);
150         ++current;
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.3  1998/09/09 16:26:31  curt
222 // Continued progress in implementing the convex hull algorithm.
223 //
224 // Revision 1.2  1998/09/04 23:04:48  curt
225 // Beginning of convex hull genereration routine.
226 //
227 // Revision 1.1  1998/09/01 19:34:33  curt
228 // Initial revision.
229 //
230 // Revision 1.1  1998/07/20 12:54:05  curt
231 // Initial revision.
232 //
233 //