X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fbucket%2Fnewbucket.cxx;h=227d9979382c064590384a2dd8434e682db6af77;hb=c82df0590dd46347e2f1d0dca4cc3712f67f3654;hp=e2b334fa54bd870cf6514e7f89e4957e3971d3c1;hpb=4c3b4219fee6bd2b5e1c7b26cc7153929ee1d230;p=simgear.git diff --git a/simgear/bucket/newbucket.cxx b/simgear/bucket/newbucket.cxx index e2b334fa..227d9979 100644 --- a/simgear/bucket/newbucket.cxx +++ b/simgear/bucket/newbucket.cxx @@ -3,7 +3,7 @@ * * Written by Curtis L. Olson, started February 1999. * - * Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org + * Copyright (C) 1999 Curtis L. Olson - http://www.flightgear.org/~curt/ * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -15,23 +15,21 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * $Id$ **************************************************************************/ #ifdef HAVE_CONFIG_H -# include +# include #endif - #include -#include +#include #include "newbucket.hxx" @@ -46,6 +44,9 @@ SGBucket::SGBucket(const double dlon, const double dlat) { set_bucket(dlon, dlat); } +SGBucket::SGBucket(const SGGeod& geod) { + set_bucket(geod); +} // create an impossible bucket if false SGBucket::SGBucket(const bool is_good) { @@ -75,11 +76,6 @@ SGBucket::SGBucket(const long int bindex) { } -// default destructor -SGBucket::~SGBucket() { -} - - // Set the bucket params for the specified lat and lon void SGBucket::set_bucket( double *lonlat ) { set_bucket( lonlat[0], lonlat[1] ); @@ -96,30 +92,61 @@ void SGBucket::set_bucket( double dlon, double dlat ) { // cout << "diff = " << diff << " span = " << span << endl; - if ( (dlon >= 0) || (fabs(diff) < FG_EPSILON) ) { + /* Calculate the greatest integral longitude less than + * or equal to the given longitude (floor(dlon)), + * but attribute coordinates near the east border + * to the next tile. + */ + if ( (dlon >= 0) || (fabs(diff) < SG_EPSILON) ) { lon = (int)dlon; } else { lon = (int)dlon - 1; } // find subdivision or super lon if needed - if ( span < FG_EPSILON ) { + if ( span < SG_EPSILON ) { + /* sg_bucket_span() never returns 0.0 + * or anything near it, so this really + * should not occur at any time. + */ // polar cap lon = 0; x = 0; } else if ( span <= 1.0 ) { + /* We have more than one tile per degree of + * longitude, so we need an x offset. + */ x = (int)((dlon - lon) / span); } else { - if ( (dlon >= 0) || (fabs(diff) < FG_EPSILON) ) { - lon = (int)( (int)(lon / span) * span); - } else { - // cout << " lon = " << lon - // << " tmp = " << (int)((lon-1) / span) << endl; - lon = (int)( (int)((lon + 1) / span) * span - span); - if ( lon < -180 ) { - lon = -180; - } - } + /* We have one or more degrees per tile, + * so we need to find the base longitude + * of that tile. + * + * First we calculate the integral base longitude + * (e.g. -85.5 => -86) and then find the greatest + * multiple of span that is less than or equal to + * that longitude. + * + * That way, the Greenwich Meridian is always + * a tile border. + * + * This gets us into trouble with the polar caps, + * which have width 360 and thus either span + * the range from 0 to 360 or from -360 to 0 + * degrees, depending on whether lon is positive + * or negative! + * + * We also get into trouble with the 8 degree tiles + * north of 88N and south of 88S, because the west- + * and east-most tiles in that range will cover 184W + * to 176W and 176E to 184E respectively, with their + * center at 180E/W! + */ + lon=(int)floor(floor((lon+SG_EPSILON)/span)*span); + /* Correct the polar cap issue */ + if ( lon < -180 ) { + lon = -180; + } x = 0; } @@ -128,17 +155,26 @@ void SGBucket::set_bucket( double dlon, double dlat ) { // diff = dlat - (double)(int)dlat; - if ( (dlat >= 0) || (fabs(diff) < FG_EPSILON) ) { + /* Again, a modified floor() function (see longitude) */ + if ( (dlat >= 0) || (fabs(diff) < SG_EPSILON) ) { lat = (int)dlat; } else { lat = (int)dlat - 1; } + /* Latitude base and offset are easier, as + * tiles always are 1/8 degree of latitude wide. + */ y = (int)((dlat - lat) * 8); } +void SGBucket::set_bucket(const SGGeod& geod) +{ + set_bucket(geod.getLongitudeDeg(), geod.getLatitudeDeg()); +} + // Build the path name for this bucket -string SGBucket::gen_base_path() const { +std::string SGBucket::gen_base_path() const { // long int index; int top_lon, top_lat, main_lon, main_lat; char hem, pole; @@ -180,7 +216,7 @@ string SGBucket::gen_base_path() const { hem, top_lon, pole, top_lat, hem, main_lon, pole, main_lat); - FGPath path( raw_path ); + SGPath path( raw_path ); return path.str(); } @@ -188,6 +224,17 @@ string SGBucket::gen_base_path() const { // return width of the tile in degrees double SGBucket::get_width() const { + if (lon==-180 && (lat==-89 || lat==88) ) { + /* Normally the tile at 180W in 88N and 89S + * would cover 184W to 176W and the next + * on the east side starts at 176W. + * To correct, make this a special tile + * from 180W to 176W with 4 degrees width + * instead of the normal 8 degrees at + * that latitude. + */ + return 4.0; + } return sg_bucket_span( get_center_lat() ); } @@ -206,19 +253,19 @@ double SGBucket::get_width_m() const { } else { clat = (int)clat - 0.5; } - double clat_rad = clat * DEG_TO_RAD; + double clat_rad = clat * SGD_DEGREES_TO_RADIANS; double cos_lat = cos( clat_rad ); - double local_radius = cos_lat * EQUATORIAL_RADIUS_M; - double local_perimeter = 2.0 * local_radius * FG_PI; + double local_radius = cos_lat * SG_EQUATORIAL_RADIUS_M; + double local_perimeter = local_radius * SGD_2PI; double degree_width = local_perimeter / 360.0; - return sg_bucket_span( get_center_lat() ) * degree_width; + return get_width() * degree_width; } // return height of the tile in meters double SGBucket::get_height_m() const { - double perimeter = 2.0 * EQUATORIAL_RADIUS_M * FG_PI; + double perimeter = SG_EQUATORIAL_RADIUS_M * SGD_2PI; double degree_height = perimeter / 360.0; return SG_BUCKET_SPAN * degree_height; @@ -272,16 +319,32 @@ void sgBucketDiff( const SGBucket& b1, const SGBucket& b2, int *dx, int *dy ) { #endif // longitude difference - double c1_lon = b1.get_center_lon(); - double c2_lon = b2.get_center_lon(); - double diff_lon = c2_lon - c1_lon; - double span; - if ( sg_bucket_span(c1_lat) <= sg_bucket_span(c2_lat) ) { + double diff_lon=0.0; + double span=0.0; + + SGBucket tmp_bucket; + // To handle crossing the bucket size boundary + // we need to account for different size buckets. + + if ( sg_bucket_span(c1_lat) <= sg_bucket_span(c2_lat) ) + { span = sg_bucket_span(c1_lat); } else { span = sg_bucket_span(c2_lat); } + diff_lon = b2.get_center_lon() - b1.get_center_lon(); + + if (diff_lon <0.0) + { + diff_lon -= b1.get_width()*0.5 + b2.get_width()*0.5 - span; + } + else + { + diff_lon += b1.get_width()*0.5 + b2.get_width()*0.5 - span; + } + + #ifdef HAVE_RINT *dx = (int)rint( diff_lon / span ); #else