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 - curt@flightgear.org
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 Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
24 **************************************************************************/
28 # include <simgear_config.h>
33 #include <simgear/misc/sg_path.hxx>
35 #include "newbucket.hxx"
38 // default constructor
39 SGBucket::SGBucket() {
43 // constructor for specified location
44 SGBucket::SGBucket(const double dlon, const double dlat) {
45 set_bucket(dlon, dlat);
49 // create an impossible bucket if false
50 SGBucket::SGBucket(const bool is_good) {
58 // Parse a unique scenery tile index and find the lon, lat, x, and y
59 SGBucket::SGBucket(const long int bindex) {
60 long int index = bindex;
78 SGBucket::~SGBucket() {
82 // Set the bucket params for the specified lat and lon
83 void SGBucket::set_bucket( double *lonlat ) {
84 set_bucket( lonlat[0], lonlat[1] );
88 // Set the bucket params for the specified lat and lon
89 void SGBucket::set_bucket( double dlon, double dlat ) {
93 double span = sg_bucket_span( dlat );
94 double diff = dlon - (double)(int)dlon;
96 // cout << "diff = " << diff << " span = " << span << endl;
98 if ( (dlon >= 0) || (fabs(diff) < SG_EPSILON) ) {
104 // find subdivision or super lon if needed
105 if ( span < SG_EPSILON ) {
109 } else if ( span <= 1.0 ) {
110 x = (int)((dlon - lon) / span);
112 if ( (dlon >= 0) || (fabs(diff) < SG_EPSILON) ) {
113 lon = (int)( (int)(lon / span) * span);
115 // cout << " lon = " << lon
116 // << " tmp = " << (int)((lon-1) / span) << endl;
117 lon = (int)( (int)((lon + 1) / span) * span - span);
128 diff = dlat - (double)(int)dlat;
130 if ( (dlat >= 0) || (fabs(diff) < SG_EPSILON) ) {
135 y = (int)((dlat - lat) * 8);
139 // Build the path name for this bucket
140 string SGBucket::gen_base_path() const {
142 int top_lon, top_lat, main_lon, main_lat;
148 if ( (lon < 0) && (top_lon * 10 != lon) ) {
152 if ( top_lon >= 0 ) {
158 if ( main_lon < 0 ) {
164 if ( (lat < 0) && (top_lat * 10 != lat) ) {
168 if ( top_lat >= 0 ) {
174 if ( main_lat < 0 ) {
178 sprintf(raw_path, "%c%03d%c%02d/%c%03d%c%02d",
179 hem, top_lon, pole, top_lat,
180 hem, main_lon, pole, main_lat);
182 SGPath path( raw_path );
188 // return width of the tile in degrees
189 double SGBucket::get_width() const {
190 return sg_bucket_span( get_center_lat() );
194 // return height of the tile in degrees
195 double SGBucket::get_height() const {
196 return SG_BUCKET_SPAN;
200 // return width of the tile in meters
201 double SGBucket::get_width_m() const {
202 double clat = (int)get_center_lat();
204 clat = (int)clat + 0.5;
206 clat = (int)clat - 0.5;
208 double clat_rad = clat * SGD_DEGREES_TO_RADIANS;
209 double cos_lat = cos( clat_rad );
210 double local_radius = cos_lat * SG_EQUATORIAL_RADIUS_M;
211 double local_perimeter = 2.0 * local_radius * SGD_PI;
212 double degree_width = local_perimeter / 360.0;
214 return sg_bucket_span( get_center_lat() ) * degree_width;
218 // return height of the tile in meters
219 double SGBucket::get_height_m() const {
220 double perimeter = 2.0 * SG_EQUATORIAL_RADIUS_M * SGD_PI;
221 double degree_height = perimeter / 360.0;
223 return SG_BUCKET_SPAN * degree_height;
227 // find the bucket which is offset by the specified tile units in the
228 // X & Y direction. We need the current lon and lat to resolve
229 // ambiguities when going from a wider tile to a narrower one above or
230 // below. This assumes that we are feeding in
231 SGBucket sgBucketOffset( double dlon, double dlat, int dx, int dy ) {
232 SGBucket result( dlon, dlat );
233 double clat = result.get_center_lat() + dy * SG_BUCKET_SPAN;
235 // walk dy units in the lat direction
236 result.set_bucket( dlon, clat );
238 // find the lon span for the new latitude
239 double span = sg_bucket_span( clat );
241 // walk dx units in the lon direction
242 double tmp = dlon + dx * span;
243 while ( tmp < -180.0 ) {
246 while ( tmp >= 180.0 ) {
249 result.set_bucket( tmp, clat );
255 // calculate the offset between two buckets
256 void sgBucketDiff( const SGBucket& b1, const SGBucket& b2, int *dx, int *dy ) {
258 // Latitude difference
259 double c1_lat = b1.get_center_lat();
260 double c2_lat = b2.get_center_lat();
261 double diff_lat = c2_lat - c1_lat;
264 *dy = (int)rint( diff_lat / SG_BUCKET_SPAN );
266 if ( diff_lat > 0 ) {
267 *dy = (int)( diff_lat / SG_BUCKET_SPAN + 0.5 );
269 *dy = (int)( diff_lat / SG_BUCKET_SPAN - 0.5 );
273 // longitude difference
274 double c1_lon = b1.get_center_lon();
275 double c2_lon = b2.get_center_lon();
276 double diff_lon = c2_lon - c1_lon;
278 if ( sg_bucket_span(c1_lat) <= sg_bucket_span(c2_lat) ) {
279 span = sg_bucket_span(c1_lat);
281 span = sg_bucket_span(c2_lat);
285 *dx = (int)rint( diff_lon / span );
287 if ( diff_lon > 0 ) {
288 *dx = (int)( diff_lon / span + 0.5 );
290 *dx = (int)( diff_lon / span - 0.5 );