]> git.mxchange.org Git - simgear.git/blob - simgear/bucket/newbucket.cxx
Modified Files:
[simgear.git] / simgear / bucket / newbucket.cxx
1 /**************************************************************************
2  * newbucket.hxx -- new bucket routines for better world modeling
3  *
4  * Written by Curtis L. Olson, started February 1999.
5  *
6  * Copyright (C) 1999  Curtis L. Olson - http://www.flightgear.org/~curt/
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21  *
22  * $Id$
23  **************************************************************************/
24
25
26 #ifdef HAVE_CONFIG_H
27 #  include <simgear_config.h>
28 #endif
29
30 #include <math.h>
31
32 #include <simgear/misc/sg_path.hxx>
33
34 #include "newbucket.hxx"
35
36
37 // default constructor
38 SGBucket::SGBucket() {
39 }
40
41
42 // constructor for specified location
43 SGBucket::SGBucket(const double dlon, const double dlat) {
44     set_bucket(dlon, dlat);
45 }
46
47
48 // create an impossible bucket if false
49 SGBucket::SGBucket(const bool is_good) {
50     set_bucket(0.0, 0.0);
51     if ( !is_good ) {
52         lon = -1000;
53     }
54 }
55
56
57 // Parse a unique scenery tile index and find the lon, lat, x, and y
58 SGBucket::SGBucket(const long int bindex) {
59     long int index = bindex;
60         
61     lon = index >> 14;
62     index -= lon << 14;
63     lon -= 180;
64
65     lat = index >> 6;
66     index -= lat << 6;
67     lat -= 90;
68
69     y = index >> 3;
70     index -= y << 3;
71
72     x = index;
73 }
74
75
76 // default destructor
77 SGBucket::~SGBucket() {
78 }
79
80
81 // Set the bucket params for the specified lat and lon
82 void SGBucket::set_bucket( double *lonlat ) {
83     set_bucket( lonlat[0], lonlat[1] );
84 }       
85
86
87 // Set the bucket params for the specified lat and lon
88 void SGBucket::set_bucket( double dlon, double dlat ) {
89     //
90     // latitude first
91     //
92     double span = sg_bucket_span( dlat );
93     double diff = dlon - (double)(int)dlon;
94
95     // cout << "diff = " << diff << "  span = " << span << endl;
96
97     if ( (dlon >= 0) || (fabs(diff) < SG_EPSILON) ) {
98         lon = (int)dlon;
99     } else {
100         lon = (int)dlon - 1;
101     }
102
103     // find subdivision or super lon if needed
104     if ( span < SG_EPSILON ) {
105         // polar cap
106         lon = 0;
107         x = 0;
108     } else if ( span <= 1.0 ) {
109         x = (int)((dlon - lon) / span);
110     } else {
111         if ( (dlon >= 0) || (fabs(diff) < SG_EPSILON) ) {
112             lon = (int)( (int)(lon / span) * span);
113         } else {
114             // cout << " lon = " << lon 
115             //  << "  tmp = " << (int)((lon-1) / span) << endl;
116             lon = (int)( (int)((lon + 1) / span) * span - span);
117             if ( lon < -180 ) {
118                 lon = -180;
119             }
120         }
121         x = 0;
122     }
123
124     //
125     // then latitude
126     //
127     diff = dlat - (double)(int)dlat;
128
129     if ( (dlat >= 0) || (fabs(diff) < SG_EPSILON) ) {
130         lat = (int)dlat;
131     } else {
132         lat = (int)dlat - 1;
133     }
134     y = (int)((dlat - lat) * 8);
135 }
136
137
138 // Build the path name for this bucket
139 string SGBucket::gen_base_path() const {
140     // long int index;
141     int top_lon, top_lat, main_lon, main_lat;
142     char hem, pole;
143     char raw_path[256];
144
145     top_lon = lon / 10;
146     main_lon = lon;
147     if ( (lon < 0) && (top_lon * 10 != lon) ) {
148         top_lon -= 1;
149     }
150     top_lon *= 10;
151     if ( top_lon >= 0 ) {
152         hem = 'e';
153     } else {
154         hem = 'w';
155         top_lon *= -1;
156     }
157     if ( main_lon < 0 ) {
158         main_lon *= -1;
159     }
160     
161     top_lat = lat / 10;
162     main_lat = lat;
163     if ( (lat < 0) && (top_lat * 10 != lat) ) {
164         top_lat -= 1;
165     }
166     top_lat *= 10;
167     if ( top_lat >= 0 ) {
168         pole = 'n';
169     } else {
170         pole = 's';
171         top_lat *= -1;
172     }
173     if ( main_lat < 0 ) {
174         main_lat *= -1;
175     }
176
177     sprintf(raw_path, "%c%03d%c%02d/%c%03d%c%02d", 
178             hem, top_lon, pole, top_lat, 
179             hem, main_lon, pole, main_lat);
180
181     SGPath path( raw_path );
182
183     return path.str();
184 }
185
186
187 // return width of the tile in degrees
188 double SGBucket::get_width() const {
189     return sg_bucket_span( get_center_lat() );
190 }
191
192
193 // return height of the tile in degrees
194 double SGBucket::get_height() const {
195     return SG_BUCKET_SPAN;
196 }
197
198
199 // return width of the tile in meters
200 double SGBucket::get_width_m() const {
201     double clat = (int)get_center_lat();
202     if ( clat > 0 ) {
203         clat = (int)clat + 0.5;
204     } else {
205         clat = (int)clat - 0.5;
206     }
207     double clat_rad = clat * SGD_DEGREES_TO_RADIANS;
208     double cos_lat = cos( clat_rad );
209     double local_radius = cos_lat * SG_EQUATORIAL_RADIUS_M;
210     double local_perimeter = local_radius * SGD_2PI;
211     double degree_width = local_perimeter / 360.0;
212
213     return sg_bucket_span( get_center_lat() ) * degree_width;
214 }
215
216
217 // return height of the tile in meters
218 double SGBucket::get_height_m() const {
219     double perimeter = SG_EQUATORIAL_RADIUS_M * SGD_2PI;
220     double degree_height = perimeter / 360.0;
221
222     return SG_BUCKET_SPAN * degree_height;
223 }
224
225
226 // find the bucket which is offset by the specified tile units in the
227 // X & Y direction.  We need the current lon and lat to resolve
228 // ambiguities when going from a wider tile to a narrower one above or
229 // below.  This assumes that we are feeding in
230 SGBucket sgBucketOffset( double dlon, double dlat, int dx, int dy ) {
231     SGBucket result( dlon, dlat );
232     double clat = result.get_center_lat() + dy * SG_BUCKET_SPAN;
233
234     // walk dy units in the lat direction
235     result.set_bucket( dlon, clat );
236
237     // find the lon span for the new latitude
238     double span = sg_bucket_span( clat );
239
240     // walk dx units in the lon direction
241     double tmp = dlon + dx * span;
242     while ( tmp < -180.0 ) {
243         tmp += 360.0;
244     }
245     while ( tmp >= 180.0 ) {
246         tmp -= 360.0;
247     }
248     result.set_bucket( tmp, clat );
249
250     return result;
251 }
252
253
254 // calculate the offset between two buckets
255 void sgBucketDiff( const SGBucket& b1, const SGBucket& b2, int *dx, int *dy ) {
256
257     // Latitude difference
258     double c1_lat = b1.get_center_lat();
259     double c2_lat = b2.get_center_lat();
260     double diff_lat = c2_lat - c1_lat;
261
262 #ifdef HAVE_RINT
263     *dy = (int)rint( diff_lat / SG_BUCKET_SPAN );
264 #else
265     if ( diff_lat > 0 ) {
266         *dy = (int)( diff_lat / SG_BUCKET_SPAN + 0.5 );
267     } else {
268         *dy = (int)( diff_lat / SG_BUCKET_SPAN - 0.5 );
269     }
270 #endif
271
272     // longitude difference
273     double diff_lon=0.0;
274     double span=0.0;
275
276     SGBucket tmp_bucket;
277     // To handle crossing the bucket size boundary
278     //  we need to account for different size buckets.
279
280     if ( sg_bucket_span(c1_lat) <= sg_bucket_span(c2_lat) )
281     {
282         span = sg_bucket_span(c1_lat);
283     } else {
284         span = sg_bucket_span(c2_lat);
285     }
286
287     diff_lon = b2.get_center_lon() - b1.get_center_lon();
288
289     if (diff_lon <0.0)
290     {
291        diff_lon -= b1.get_width()*0.5 + b2.get_width()*0.5 - span;
292     } 
293     else
294     {
295        diff_lon += b1.get_width()*0.5 + b2.get_width()*0.5 - span;
296     }
297
298
299 #ifdef HAVE_RINT
300     *dx = (int)rint( diff_lon / span );
301 #else
302     if ( diff_lon > 0 ) {
303         *dx = (int)( diff_lon / span + 0.5 );
304     } else {
305         *dx = (int)( diff_lon / span - 0.5 );
306     }
307 #endif
308 }
309
310