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 *lonlat ) {
81 set_bucket( lonlat[0], lonlat[1] );
85 // Set the bucket params for the specified lat and lon
86 void SGBucket::set_bucket( double dlon, double dlat ) {
90 double span = sg_bucket_span( dlat );
91 double diff = dlon - (double)(int)dlon;
93 // cout << "diff = " << diff << " span = " << span << endl;
95 if ( (dlon >= 0) || (fabs(diff) < SG_EPSILON) ) {
101 // find subdivision or super lon if needed
102 if ( span < SG_EPSILON ) {
106 } else if ( span <= 1.0 ) {
107 x = (int)((dlon - lon) / span);
110 lon = (int)( (int)(lon / span) * span);
112 // cout << " lon = " << lon
113 // << " tmp = " << (int)((lon-1) / span) << endl;
114 lon = (int)( (int)((lon + 1) / span) * span - span);
125 diff = dlat - (double)(int)dlat;
127 if ( (dlat >= 0) || (fabs(diff) < SG_EPSILON) ) {
132 y = (int)((dlat - lat) * 8);
136 void SGBucket::set_bucket(const SGGeod& geod)
138 set_bucket(geod.getLongitudeDeg(), geod.getLatitudeDeg());
141 // Build the path name for this bucket
142 std::string SGBucket::gen_base_path() const {
144 int top_lon, top_lat, main_lon, main_lat;
150 if ( (lon < 0) && (top_lon * 10 != lon) ) {
154 if ( top_lon >= 0 ) {
160 if ( main_lon < 0 ) {
166 if ( (lat < 0) && (top_lat * 10 != lat) ) {
170 if ( top_lat >= 0 ) {
176 if ( main_lat < 0 ) {
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);
184 SGPath path( raw_path );
190 // return width of the tile in degrees
191 double SGBucket::get_width() const {
192 return sg_bucket_span( get_center_lat() );
196 // return height of the tile in degrees
197 double SGBucket::get_height() const {
198 return SG_BUCKET_SPAN;
202 // return width of the tile in meters
203 double SGBucket::get_width_m() const {
204 double clat = (int)get_center_lat();
206 clat = (int)clat + 0.5;
208 clat = (int)clat - 0.5;
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;
216 return sg_bucket_span( get_center_lat() ) * degree_width;
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;
225 return SG_BUCKET_SPAN * degree_height;
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;
237 // walk dy units in the lat direction
238 result.set_bucket( dlon, clat );
240 // find the lon span for the new latitude
241 double span = sg_bucket_span( clat );
243 // walk dx units in the lon direction
244 double tmp = dlon + dx * span;
245 while ( tmp < -180.0 ) {
248 while ( tmp >= 180.0 ) {
251 result.set_bucket( tmp, clat );
257 // calculate the offset between two buckets
258 void sgBucketDiff( const SGBucket& b1, const SGBucket& b2, int *dx, int *dy ) {
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;
266 *dy = (int)rint( diff_lat / SG_BUCKET_SPAN );
268 if ( diff_lat > 0 ) {
269 *dy = (int)( diff_lat / SG_BUCKET_SPAN + 0.5 );
271 *dy = (int)( diff_lat / SG_BUCKET_SPAN - 0.5 );
275 // longitude difference
280 // To handle crossing the bucket size boundary
281 // we need to account for different size buckets.
283 if ( sg_bucket_span(c1_lat) <= sg_bucket_span(c2_lat) )
285 span = sg_bucket_span(c1_lat);
287 span = sg_bucket_span(c2_lat);
290 diff_lon = b2.get_center_lon() - b1.get_center_lon();
294 diff_lon -= b1.get_width()*0.5 + b2.get_width()*0.5 - span;
298 diff_lon += b1.get_width()*0.5 + b2.get_width()*0.5 - span;
303 *dx = (int)rint( diff_lon / span );
305 if ( diff_lon > 0 ) {
306 *dx = (int)( diff_lon / span + 0.5 );
308 *dx = (int)( diff_lon / span - 0.5 );