]> git.mxchange.org Git - simgear.git/blob - simgear/bucket/newbucket.cxx
Merge branch 'maint' into next
[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 SGBucket::SGBucket(const SGGeod& geod) {
48     set_bucket(geod);
49 }
50
51 // create an impossible bucket if false
52 SGBucket::SGBucket(const bool is_good) {
53     set_bucket(0.0, 0.0);
54     if ( !is_good ) {
55         lon = -1000;
56     }
57 }
58
59
60 // Parse a unique scenery tile index and find the lon, lat, x, and y
61 SGBucket::SGBucket(const long int bindex) {
62     long int index = bindex;
63         
64     lon = index >> 14;
65     index -= lon << 14;
66     lon -= 180;
67
68     lat = index >> 6;
69     index -= lat << 6;
70     lat -= 90;
71
72     y = index >> 3;
73     index -= y << 3;
74
75     x = index;
76 }
77
78
79 // Set the bucket params for the specified lat and lon
80 void SGBucket::set_bucket( double *lonlat ) {
81     set_bucket( lonlat[0], lonlat[1] );
82 }       
83
84
85 // Set the bucket params for the specified lat and lon
86 void SGBucket::set_bucket( double dlon, double dlat ) {
87     //
88     // latitude first
89     //
90     double span = sg_bucket_span( dlat );
91     double diff = dlon - (double)(int)dlon;
92
93     // cout << "diff = " << diff << "  span = " << span << endl;
94
95     if ( (dlon >= 0) || (fabs(diff) < SG_EPSILON) ) {
96         lon = (int)dlon;
97     } else {
98         lon = (int)dlon - 1;
99     }
100
101     // find subdivision or super lon if needed
102     if ( span < SG_EPSILON ) {
103         // polar cap
104         lon = 0;
105         x = 0;
106     } else if ( span <= 1.0 ) {
107         x = (int)((dlon - lon) / span);
108     } else {
109         if ( dlon >= 0 ) {
110             lon = (int)( (int)(lon / span) * span);
111         } else {
112             // cout << " lon = " << lon 
113             //  << "  tmp = " << (int)((lon-1) / span) << endl;
114             lon = (int)( (int)((lon + 1) / span) * span - span);
115             if ( lon < -180 ) {
116                 lon = -180;
117             }
118         }
119         x = 0;
120     }
121
122     //
123     // then latitude
124     //
125     diff = dlat - (double)(int)dlat;
126
127     if ( (dlat >= 0) || (fabs(diff) < SG_EPSILON) ) {
128         lat = (int)dlat;
129     } else {
130         lat = (int)dlat - 1;
131     }
132     y = (int)((dlat - lat) * 8);
133 }
134
135
136 void SGBucket::set_bucket(const SGGeod& geod)
137 {
138     set_bucket(geod.getLongitudeDeg(), geod.getLatitudeDeg());
139 }
140
141 // Build the path name for this bucket
142 string SGBucket::gen_base_path() const {
143     // long int index;
144     int top_lon, top_lat, main_lon, main_lat;
145     char hem, pole;
146     char raw_path[256];
147
148     top_lon = lon / 10;
149     main_lon = lon;
150     if ( (lon < 0) && (top_lon * 10 != lon) ) {
151         top_lon -= 1;
152     }
153     top_lon *= 10;
154     if ( top_lon >= 0 ) {
155         hem = 'e';
156     } else {
157         hem = 'w';
158         top_lon *= -1;
159     }
160     if ( main_lon < 0 ) {
161         main_lon *= -1;
162     }
163     
164     top_lat = lat / 10;
165     main_lat = lat;
166     if ( (lat < 0) && (top_lat * 10 != lat) ) {
167         top_lat -= 1;
168     }
169     top_lat *= 10;
170     if ( top_lat >= 0 ) {
171         pole = 'n';
172     } else {
173         pole = 's';
174         top_lat *= -1;
175     }
176     if ( main_lat < 0 ) {
177         main_lat *= -1;
178     }
179
180     sprintf(raw_path, "%c%03d%c%02d/%c%03d%c%02d", 
181             hem, top_lon, pole, top_lat, 
182             hem, main_lon, pole, main_lat);
183
184     SGPath path( raw_path );
185
186     return path.str();
187 }
188
189
190 // return width of the tile in degrees
191 double SGBucket::get_width() const {
192     return sg_bucket_span( get_center_lat() );
193 }
194
195
196 // return height of the tile in degrees
197 double SGBucket::get_height() const {
198     return SG_BUCKET_SPAN;
199 }
200
201
202 // return width of the tile in meters
203 double SGBucket::get_width_m() const {
204     double clat = (int)get_center_lat();
205     if ( clat > 0 ) {
206         clat = (int)clat + 0.5;
207     } else {
208         clat = (int)clat - 0.5;
209     }
210     double clat_rad = clat * SGD_DEGREES_TO_RADIANS;
211     double cos_lat = cos( clat_rad );
212     double local_radius = cos_lat * SG_EQUATORIAL_RADIUS_M;
213     double local_perimeter = local_radius * SGD_2PI;
214     double degree_width = local_perimeter / 360.0;
215
216     return sg_bucket_span( get_center_lat() ) * degree_width;
217 }
218
219
220 // return height of the tile in meters
221 double SGBucket::get_height_m() const {
222     double perimeter = SG_EQUATORIAL_RADIUS_M * SGD_2PI;
223     double degree_height = perimeter / 360.0;
224
225     return SG_BUCKET_SPAN * degree_height;
226 }
227
228
229 // find the bucket which is offset by the specified tile units in the
230 // X & Y direction.  We need the current lon and lat to resolve
231 // ambiguities when going from a wider tile to a narrower one above or
232 // below.  This assumes that we are feeding in
233 SGBucket sgBucketOffset( double dlon, double dlat, int dx, int dy ) {
234     SGBucket result( dlon, dlat );
235     double clat = result.get_center_lat() + dy * SG_BUCKET_SPAN;
236
237     // walk dy units in the lat direction
238     result.set_bucket( dlon, clat );
239
240     // find the lon span for the new latitude
241     double span = sg_bucket_span( clat );
242
243     // walk dx units in the lon direction
244     double tmp = dlon + dx * span;
245     while ( tmp < -180.0 ) {
246         tmp += 360.0;
247     }
248     while ( tmp >= 180.0 ) {
249         tmp -= 360.0;
250     }
251     result.set_bucket( tmp, clat );
252
253     return result;
254 }
255
256
257 // calculate the offset between two buckets
258 void sgBucketDiff( const SGBucket& b1, const SGBucket& b2, int *dx, int *dy ) {
259
260     // Latitude difference
261     double c1_lat = b1.get_center_lat();
262     double c2_lat = b2.get_center_lat();
263     double diff_lat = c2_lat - c1_lat;
264
265 #ifdef HAVE_RINT
266     *dy = (int)rint( diff_lat / SG_BUCKET_SPAN );
267 #else
268     if ( diff_lat > 0 ) {
269         *dy = (int)( diff_lat / SG_BUCKET_SPAN + 0.5 );
270     } else {
271         *dy = (int)( diff_lat / SG_BUCKET_SPAN - 0.5 );
272     }
273 #endif
274
275     // longitude difference
276     double diff_lon=0.0;
277     double span=0.0;
278
279     SGBucket tmp_bucket;
280     // To handle crossing the bucket size boundary
281     //  we need to account for different size buckets.
282
283     if ( sg_bucket_span(c1_lat) <= sg_bucket_span(c2_lat) )
284     {
285         span = sg_bucket_span(c1_lat);
286     } else {
287         span = sg_bucket_span(c2_lat);
288     }
289
290     diff_lon = b2.get_center_lon() - b1.get_center_lon();
291
292     if (diff_lon <0.0)
293     {
294        diff_lon -= b1.get_width()*0.5 + b2.get_width()*0.5 - span;
295     } 
296     else
297     {
298        diff_lon += b1.get_width()*0.5 + b2.get_width()*0.5 - span;
299     }
300
301
302 #ifdef HAVE_RINT
303     *dx = (int)rint( diff_lon / span );
304 #else
305     if ( diff_lon > 0 ) {
306         *dx = (int)( diff_lon / span + 0.5 );
307     } else {
308         *dx = (int)( diff_lon / span - 0.5 );
309     }
310 #endif
311 }
312
313