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 **************************************************************************/
29 #include <simgear/misc/sg_path.hxx>
31 #include "newbucket.hxx"
34 // default constructor
35 SGBucket::SGBucket() {
39 // constructor for specified location
40 SGBucket::SGBucket(const double dlon, const double dlat) {
41 set_bucket(dlon, dlat);
45 // create an impossible bucket if false
46 SGBucket::SGBucket(const bool is_good) {
54 // Parse a unique scenery tile index and find the lon, lat, x, and y
55 SGBucket::SGBucket(const long int bindex) {
56 long int index = bindex;
74 SGBucket::~SGBucket() {
78 // Set the bucket params for the specified lat and lon
79 void SGBucket::set_bucket( double *lonlat ) {
80 set_bucket( lonlat[0], lonlat[1] );
84 // Set the bucket params for the specified lat and lon
85 void SGBucket::set_bucket( double dlon, double dlat ) {
89 double span = sg_bucket_span( dlat );
90 double diff = dlon - (double)(int)dlon;
92 // cout << "diff = " << diff << " span = " << span << endl;
94 if ( (dlon >= 0) || (fabs(diff) < SG_EPSILON) ) {
100 // find subdivision or super lon if needed
101 if ( span < SG_EPSILON ) {
105 } else if ( span <= 1.0 ) {
106 x = (int)((dlon - lon) / span);
108 if ( (dlon >= 0) || (fabs(diff) < SG_EPSILON) ) {
109 lon = (int)( (int)(lon / span) * span);
111 // cout << " lon = " << lon
112 // << " tmp = " << (int)((lon-1) / span) << endl;
113 lon = (int)( (int)((lon + 1) / span) * span - span);
124 diff = dlat - (double)(int)dlat;
126 if ( (dlat >= 0) || (fabs(diff) < SG_EPSILON) ) {
131 y = (int)((dlat - lat) * 8);
135 // Build the path name for this bucket
136 string SGBucket::gen_base_path() const {
138 int top_lon, top_lat, main_lon, main_lat;
144 if ( (lon < 0) && (top_lon * 10 != lon) ) {
148 if ( top_lon >= 0 ) {
154 if ( main_lon < 0 ) {
160 if ( (lat < 0) && (top_lat * 10 != lat) ) {
164 if ( top_lat >= 0 ) {
170 if ( main_lat < 0 ) {
174 sprintf(raw_path, "%c%03d%c%02d/%c%03d%c%02d",
175 hem, top_lon, pole, top_lat,
176 hem, main_lon, pole, main_lat);
178 SGPath path( raw_path );
184 // return width of the tile in degrees
185 double SGBucket::get_width() const {
186 return sg_bucket_span( get_center_lat() );
190 // return height of the tile in degrees
191 double SGBucket::get_height() const {
192 return SG_BUCKET_SPAN;
196 // return width of the tile in meters
197 double SGBucket::get_width_m() const {
198 double clat = (int)get_center_lat();
200 clat = (int)clat + 0.5;
202 clat = (int)clat - 0.5;
204 double clat_rad = clat * SGD_DEGREES_TO_RADIANS;
205 double cos_lat = cos( clat_rad );
206 double local_radius = cos_lat * SG_EQUATORIAL_RADIUS_M;
207 double local_perimeter = 2.0 * local_radius * SGD_PI;
208 double degree_width = local_perimeter / 360.0;
210 return sg_bucket_span( get_center_lat() ) * degree_width;
214 // return height of the tile in meters
215 double SGBucket::get_height_m() const {
216 double perimeter = 2.0 * SG_EQUATORIAL_RADIUS_M * SGD_PI;
217 double degree_height = perimeter / 360.0;
219 return SG_BUCKET_SPAN * degree_height;
223 // find the bucket which is offset by the specified tile units in the
224 // X & Y direction. We need the current lon and lat to resolve
225 // ambiguities when going from a wider tile to a narrower one above or
226 // below. This assumes that we are feeding in
227 SGBucket sgBucketOffset( double dlon, double dlat, int dx, int dy ) {
228 SGBucket result( dlon, dlat );
229 double clat = result.get_center_lat() + dy * SG_BUCKET_SPAN;
231 // walk dy units in the lat direction
232 result.set_bucket( dlon, clat );
234 // find the lon span for the new latitude
235 double span = sg_bucket_span( clat );
237 // walk dx units in the lon direction
238 double tmp = dlon + dx * span;
239 while ( tmp < -180.0 ) {
242 while ( tmp >= 180.0 ) {
245 result.set_bucket( tmp, clat );
251 // calculate the offset between two buckets
252 void sgBucketDiff( const SGBucket& b1, const SGBucket& b2, int *dx, int *dy ) {
254 // Latitude difference
255 double c1_lat = b1.get_center_lat();
256 double c2_lat = b2.get_center_lat();
257 double diff_lat = c2_lat - c1_lat;
260 *dy = (int)rint( diff_lat / SG_BUCKET_SPAN );
262 if ( diff_lat > 0 ) {
263 *dy = (int)( diff_lat / SG_BUCKET_SPAN + 0.5 );
265 *dy = (int)( diff_lat / SG_BUCKET_SPAN - 0.5 );
269 // longitude difference
270 double c1_lon = b1.get_center_lon();
271 double c2_lon = b2.get_center_lon();
272 double diff_lon = c2_lon - c1_lon;
274 if ( sg_bucket_span(c1_lat) <= sg_bucket_span(c2_lat) ) {
275 span = sg_bucket_span(c1_lat);
277 span = sg_bucket_span(c2_lat);
281 *dx = (int)rint( diff_lon / span );
283 if ( diff_lon > 0 ) {
284 *dx = (int)( diff_lon / span + 0.5 );
286 *dx = (int)( diff_lon / span - 0.5 );