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);
48 // create an impossible bucket if false
49 SGBucket::SGBucket(const bool is_good) {
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;
77 SGBucket::~SGBucket() {
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] );
87 // Set the bucket params for the specified lat and lon
88 void SGBucket::set_bucket( double dlon, double dlat ) {
92 double span = sg_bucket_span( dlat );
93 double diff = dlon - (double)(int)dlon;
95 // cout << "diff = " << diff << " span = " << span << endl;
97 if ( (dlon >= 0) || (fabs(diff) < SG_EPSILON) ) {
103 // find subdivision or super lon if needed
104 if ( span < SG_EPSILON ) {
108 } else if ( span <= 1.0 ) {
109 x = (int)((dlon - lon) / span);
111 if ( (dlon >= 0) || (fabs(diff) < SG_EPSILON) ) {
112 lon = (int)( (int)(lon / span) * span);
114 // cout << " lon = " << lon
115 // << " tmp = " << (int)((lon-1) / span) << endl;
116 lon = (int)( (int)((lon + 1) / span) * span - span);
127 diff = dlat - (double)(int)dlat;
129 if ( (dlat >= 0) || (fabs(diff) < SG_EPSILON) ) {
134 y = (int)((dlat - lat) * 8);
138 // Build the path name for this bucket
139 string SGBucket::gen_base_path() const {
141 int top_lon, top_lat, main_lon, main_lat;
147 if ( (lon < 0) && (top_lon * 10 != lon) ) {
151 if ( top_lon >= 0 ) {
157 if ( main_lon < 0 ) {
163 if ( (lat < 0) && (top_lat * 10 != lat) ) {
167 if ( top_lat >= 0 ) {
173 if ( main_lat < 0 ) {
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);
181 SGPath path( raw_path );
187 // return width of the tile in degrees
188 double SGBucket::get_width() const {
189 return sg_bucket_span( get_center_lat() );
193 // return height of the tile in degrees
194 double SGBucket::get_height() const {
195 return SG_BUCKET_SPAN;
199 // return width of the tile in meters
200 double SGBucket::get_width_m() const {
201 double clat = (int)get_center_lat();
203 clat = (int)clat + 0.5;
205 clat = (int)clat - 0.5;
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;
213 return sg_bucket_span( get_center_lat() ) * degree_width;
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;
222 return SG_BUCKET_SPAN * degree_height;
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;
234 // walk dy units in the lat direction
235 result.set_bucket( dlon, clat );
237 // find the lon span for the new latitude
238 double span = sg_bucket_span( clat );
240 // walk dx units in the lon direction
241 double tmp = dlon + dx * span;
242 while ( tmp < -180.0 ) {
245 while ( tmp >= 180.0 ) {
248 result.set_bucket( tmp, clat );
254 // calculate the offset between two buckets
255 void sgBucketDiff( const SGBucket& b1, const SGBucket& b2, int *dx, int *dy ) {
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;
263 *dy = (int)rint( diff_lat / SG_BUCKET_SPAN );
265 if ( diff_lat > 0 ) {
266 *dy = (int)( diff_lat / SG_BUCKET_SPAN + 0.5 );
268 *dy = (int)( diff_lat / SG_BUCKET_SPAN - 0.5 );
272 // longitude difference
277 // To handle crossing the bucket size boundary
278 // we need to account for different size buckets.
280 if ( sg_bucket_span(c1_lat) <= sg_bucket_span(c2_lat) )
282 span = sg_bucket_span(c1_lat);
284 span = sg_bucket_span(c2_lat);
287 diff_lon = b2.get_center_lon() - b1.get_center_lon();
291 diff_lon -= b1.get_width()*0.5 + b2.get_width()*0.5 - span;
295 diff_lon += b1.get_width()*0.5 + b2.get_width()*0.5 - span;
300 *dx = (int)rint( diff_lon / span );
302 if ( diff_lon > 0 ) {
303 *dx = (int)( diff_lon / span + 0.5 );
305 *dx = (int)( diff_lon / span - 0.5 );