1 // area.c -- routines to assist with inserting "areas" into FG terrain
3 // Written by Curtis Olson, started March 1998.
5 // Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
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.
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.
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.
22 // (Log is kept at end of this file)
29 #include <Include/fg_constants.h>
32 #include "point2d.hxx"
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) );
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) );
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 ) {
53 // printf("calc_lon_lat() offset.theta = %.2f offset.dist = %.2f\n",
54 // offset.theta, offset.dist);
56 offset.dist *= METER_TO_NM * NM_TO_RAD;
58 result.lat = asin( sin(orig.lat) * cos(offset.dist) +
59 cos(orig.lat) * sin(offset.dist) * cos(offset.theta) );
61 if ( cos(result.lat) < FG_EPSILON ) {
62 result.lon = orig.lon; // endpoint a pole
65 fmod(orig.lon - asin( sin(offset.theta) * sin(offset.dist) /
66 cos(result.lat) ) + FG_PI, FG_2PI) - FG_PI;
74 batch_cart_to_polar_2d( list < point2d > in_list)
76 list < point2d > out_list;
77 list < point2d > :: iterator current;
78 list < point2d > :: iterator last;
81 current = in_list.begin();
83 for ( ; current != last ; ++current ) {
84 p = cart_to_polar_2d( *current );
85 out_list.push_back(p);
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.
97 gen_area(point2d origin, double angle, list < point2d > cart_list)
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;
105 origin_rad.lon = origin.lon * DEG_TO_RAD;
106 origin_rad.lat = origin.lat * DEG_TO_RAD;
108 // convert to polar coordinates
109 rad_list = batch_cart_to_polar_2d(cart_list);
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);
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;
135 // printf("(%.2f, %.2f)\n", current->theta, current->dist);
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
149 // printf("(%.8f, %.8f)\n", p.lon, p.lat);
150 result_list.push_back(p);
158 // generate an area for a runway
160 gen_runway_area( double lon, double lat, double heading,
161 double length, double width)
163 list < point2d > result_list;
164 list < point2d > tmp_list;
165 list < point2d > :: iterator current;
166 list < point2d > :: iterator last;
174 printf("runway: lon = %.2f lat = %.2f hdg = %.2f len = %.2f width = %.2f\n",
175 lon, lat, heading, length, width);
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);
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);
201 // rotate, transform, and convert points to lon, lat in degrees
202 result_list = gen_area(origin, heading, tmp_list);
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);
221 // Revision 1.5 1998/10/20 15:49:54 curt
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.
229 // Revision 1.3 1998/09/09 16:26:31 curt
230 // Continued progress in implementing the convex hull algorithm.
232 // Revision 1.2 1998/09/04 23:04:48 curt
233 // Beginning of convex hull genereration routine.
235 // Revision 1.1 1998/09/01 19:34:33 curt
238 // Revision 1.1 1998/07/20 12:54:05 curt