1 /**************************************************************************
2 * newbucket.hxx -- new bucket routines for better world modeling
4 * Written by Curtis L. Olson, started February 1999.
6 * Copyright (C) 1999 Curtis L. Olson - http://www.flightgear.org/~curt/
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.
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.
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.
23 **************************************************************************/
27 # include <simgear_config.h>
32 #include <simgear/misc/sg_path.hxx>
34 #include "newbucket.hxx"
37 // default constructor
38 SGBucket::SGBucket() {
42 // constructor for specified location
43 SGBucket::SGBucket(const double dlon, const double dlat) {
44 set_bucket(dlon, dlat);
47 SGBucket::SGBucket(const SGGeod& geod) {
51 // create an impossible bucket if false
52 SGBucket::SGBucket(const bool is_good) {
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;
79 // Set the bucket params for the specified lat and lon
80 void SGBucket::set_bucket( double dlon, double dlat ) {
84 double span = sg_bucket_span( dlat );
85 double diff = dlon - (double)(int)dlon;
87 // cout << "diff = " << diff << " span = " << span << endl;
89 /* Calculate the greatest integral longitude less than
90 * or equal to the given longitude (floor(dlon)),
91 * but attribute coordinates near the east border
94 if ( (dlon >= 0) || (fabs(diff) < SG_EPSILON) ) {
100 // find subdivision or super lon if needed
101 if ( span < SG_EPSILON ) {
102 /* sg_bucket_span() never returns 0.0
103 * or anything near it, so this really
104 * should not occur at any time.
109 } else if ( span <= 1.0 ) {
110 /* We have more than one tile per degree of
111 * longitude, so we need an x offset.
113 x = (int)((dlon - lon) / span);
115 /* We have one or more degrees per tile,
116 * so we need to find the base longitude
119 * First we calculate the integral base longitude
120 * (e.g. -85.5 => -86) and then find the greatest
121 * multiple of span that is less than or equal to
124 * That way, the Greenwich Meridian is always
127 * This gets us into trouble with the polar caps,
128 * which have width 360 and thus either span
129 * the range from 0 to 360 or from -360 to 0
130 * degrees, depending on whether lon is positive
133 * We also get into trouble with the 8 degree tiles
134 * north of 88N and south of 88S, because the west-
135 * and east-most tiles in that range will cover 184W
136 * to 176W and 176E to 184E respectively, with their
139 lon=(int)floor(floor((lon+SG_EPSILON)/span)*span);
140 /* Correct the polar cap issue */
150 diff = dlat - (double)(int)dlat;
152 /* Again, a modified floor() function (see longitude) */
153 if ( (dlat >= 0) || (fabs(diff) < SG_EPSILON) ) {
158 /* Latitude base and offset are easier, as
159 * tiles always are 1/8 degree of latitude wide.
161 y = (int)((dlat - lat) * 8);
165 void SGBucket::set_bucket(const SGGeod& geod)
167 set_bucket(geod.getLongitudeDeg(), geod.getLatitudeDeg());
170 // Build the path name for this bucket
171 std::string SGBucket::gen_base_path() const {
173 int top_lon, top_lat, main_lon, main_lat;
179 if ( (lon < 0) && (top_lon * 10 != lon) ) {
183 if ( top_lon >= 0 ) {
189 if ( main_lon < 0 ) {
195 if ( (lat < 0) && (top_lat * 10 != lat) ) {
199 if ( top_lat >= 0 ) {
205 if ( main_lat < 0 ) {
209 snprintf(raw_path, 256, "%c%03d%c%02d/%c%03d%c%02d",
210 hem, top_lon, pole, top_lat,
211 hem, main_lon, pole, main_lat);
213 SGPath path( raw_path );
219 // return width of the tile in degrees
220 double SGBucket::get_width() const {
221 return sg_bucket_span( get_center_lat() );
225 // return height of the tile in degrees
226 double SGBucket::get_height() const {
227 return SG_BUCKET_SPAN;
231 // return width of the tile in meters
232 double SGBucket::get_width_m() const {
233 double clat = (int)get_center_lat();
235 clat = (int)clat + 0.5;
237 clat = (int)clat - 0.5;
239 double clat_rad = clat * SGD_DEGREES_TO_RADIANS;
240 double cos_lat = cos( clat_rad );
241 double local_radius = cos_lat * SG_EQUATORIAL_RADIUS_M;
242 double local_perimeter = local_radius * SGD_2PI;
243 double degree_width = local_perimeter / 360.0;
245 return get_width() * degree_width;
249 // return height of the tile in meters
250 double SGBucket::get_height_m() const {
251 double perimeter = SG_EQUATORIAL_RADIUS_M * SGD_2PI;
252 double degree_height = perimeter / 360.0;
254 return SG_BUCKET_SPAN * degree_height;
258 // find the bucket which is offset by the specified tile units in the
259 // X & Y direction. We need the current lon and lat to resolve
260 // ambiguities when going from a wider tile to a narrower one above or
261 // below. This assumes that we are feeding in
262 SGBucket sgBucketOffset( double dlon, double dlat, int dx, int dy ) {
263 SGBucket result( dlon, dlat );
264 double clat = result.get_center_lat() + dy * SG_BUCKET_SPAN;
266 // walk dy units in the lat direction
267 result.set_bucket( dlon, clat );
269 // find the lon span for the new latitude
270 double span = sg_bucket_span( clat );
272 // walk dx units in the lon direction
273 double tmp = dlon + dx * span;
274 while ( tmp < -180.0 ) {
277 while ( tmp >= 180.0 ) {
280 result.set_bucket( tmp, clat );
286 // calculate the offset between two buckets
287 void sgBucketDiff( const SGBucket& b1, const SGBucket& b2, int *dx, int *dy ) {
289 // Latitude difference
290 double c1_lat = b1.get_center_lat();
291 double c2_lat = b2.get_center_lat();
292 double diff_lat = c2_lat - c1_lat;
295 *dy = (int)rint( diff_lat / SG_BUCKET_SPAN );
297 if ( diff_lat > 0 ) {
298 *dy = (int)( diff_lat / SG_BUCKET_SPAN + 0.5 );
300 *dy = (int)( diff_lat / SG_BUCKET_SPAN - 0.5 );
304 // longitude difference
309 // To handle crossing the bucket size boundary
310 // we need to account for different size buckets.
312 if ( sg_bucket_span(c1_lat) <= sg_bucket_span(c2_lat) )
314 span = sg_bucket_span(c1_lat);
316 span = sg_bucket_span(c2_lat);
319 diff_lon = b2.get_center_lon() - b1.get_center_lon();
323 diff_lon -= b1.get_width()*0.5 + b2.get_width()*0.5 - span;
327 diff_lon += b1.get_width()*0.5 + b2.get_width()*0.5 - span;
332 *dx = (int)rint( diff_lon / span );
334 if ( diff_lon > 0 ) {
335 *dx = (int)( diff_lon / span + 0.5 );
337 *dx = (int)( diff_lon / span - 0.5 );
342 void sgGetBuckets( const SGGeod& min, const SGGeod& max, std::vector<SGBucket>& list ) {
343 double lon, lat, span;
345 for (lat = min.getLatitudeDeg(); lat <= max.getLatitudeDeg(); lat += SG_BUCKET_SPAN) {
346 span = sg_bucket_span( lat );
347 for (lon = min.getLongitudeDeg(); lon <= max.getLongitudeDeg(); lon += span) {
348 list.push_back( SGBucket(lon , lat) );