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>
31 #include <cstdio> // some platforms need this for ::snprintf
34 #include <simgear/misc/sg_path.hxx>
35 #include <simgear/debug/logstream.hxx>
37 #include "newbucket.hxx"
40 // default constructor
41 SGBucket::SGBucket() :
49 bool SGBucket::isValid() const
51 // The most northerly valid latitude is 89, not 90. There is no tile
52 // whose *bottom* latitude is 90. Similar there is no tile whose left egde
54 return (lon >= -180) &&
61 void SGBucket::make_bad()
67 // constructor for specified location
68 SGBucket::SGBucket(const double dlon, const double dlat) {
69 set_bucket(dlon, dlat);
72 SGBucket::SGBucket(const SGGeod& geod) {
73 set_bucket(geod.getLongitudeDeg(),
74 geod.getLatitudeDeg());
77 // Parse a unique scenery tile index and find the lon, lat, x, and y
78 SGBucket::SGBucket(const long int bindex) {
79 long int index = bindex;
95 /* Calculate the greatest integral value less than
96 * or equal to the given value (floor(x)),
97 * but attribute coordinates close to the boundary to the next
98 * (increasing) integral
100 static int floorWithEpsilon(double x)
102 double diff = x - static_cast<int>(x);
103 if ( (x >= 0.0) || (fabs(diff) < SG_EPSILON) ) {
104 return static_cast<int>(x);
106 return static_cast<int>(x) - 1;
110 // Set the bucket params for the specified lat and lon
111 void SGBucket::set_bucket( double dlon, double dlat )
113 if ((dlon < -180.0) || (dlon >= 180.0)) {
114 SG_LOG(SG_TERRAIN, SG_WARN, "SGBucket::set_bucket: passed longitude:" << dlon);
115 dlon = SGMiscd::normalizePeriodic(-180.0, 180.0, dlon);
118 if ((dlat < -90.0) || (dlat > 90.0)) {
119 SG_LOG(SG_TERRAIN, SG_WARN, "SGBucket::set_bucket: passed latitude" << dlat);
120 dlat = SGMiscd::clip(dlat, -90.0, 90.0);
126 double span = sg_bucket_span( dlat );
127 // we do NOT need to special case lon=180 here, since
128 // normalizePeriodic will never return 180; it will
129 // return -180, which is what we want.
130 lon = floorWithEpsilon(dlon);
132 // find subdivision or super lon if needed
134 /* We have more than one tile per degree of
135 * longitude, so we need an x offset.
137 x = (int)((dlon - lon) / span);
139 /* We have one or more degrees per tile,
140 * so we need to find the base longitude
143 * First we calculate the integral base longitude
144 * (e.g. -85.5 => -86) and then find the greatest
145 * multiple of span that is less than or equal to
148 * That way, the Greenwich Meridian is always
151 lon=static_cast<int>(floor(lon / span) * span);
158 lat = floorWithEpsilon(dlat);
160 // special case when passing in the north pole point (possibly due to
161 // clipping latitude above). Ensures we generate a valid bucket in this
167 /* Latitude base and offset are easier, as
168 * tiles always are 1/8 degree of latitude wide.
170 y = (int)((dlat - lat) * 8);
174 void SGBucket::set_bucket(const SGGeod& geod)
176 set_bucket(geod.getLongitudeDeg(), geod.getLatitudeDeg());
179 // Build the path name for this bucket
180 std::string SGBucket::gen_base_path() const {
182 int top_lon, top_lat, main_lon, main_lat;
188 if ( (lon < 0) && (top_lon * 10 != lon) ) {
192 if ( top_lon >= 0 ) {
198 if ( main_lon < 0 ) {
204 if ( (lat < 0) && (top_lat * 10 != lat) ) {
208 if ( top_lat >= 0 ) {
214 if ( main_lat < 0 ) {
218 ::snprintf(raw_path, 256, "%c%03d%c%02d/%c%03d%c%02d",
219 hem, top_lon, pole, top_lat,
220 hem, main_lon, pole, main_lat);
222 SGPath path( raw_path );
228 // return width of the tile in degrees
229 double SGBucket::get_width() const {
230 return sg_bucket_span( get_center_lat() );
234 // return height of the tile in degrees
235 double SGBucket::get_height() const {
236 return SG_BUCKET_SPAN;
240 // return width of the tile in meters
241 double SGBucket::get_width_m() const {
242 double clat = (int)get_center_lat();
244 clat = (int)clat + 0.5;
246 clat = (int)clat - 0.5;
248 double clat_rad = clat * SGD_DEGREES_TO_RADIANS;
249 double cos_lat = cos( clat_rad );
250 double local_radius = cos_lat * SG_EQUATORIAL_RADIUS_M;
251 double local_perimeter = local_radius * SGD_2PI;
252 double degree_width = local_perimeter / 360.0;
254 return get_width() * degree_width;
258 // return height of the tile in meters
259 double SGBucket::get_height_m() const {
260 double perimeter = SG_EQUATORIAL_RADIUS_M * SGD_2PI;
261 double degree_height = perimeter / 360.0;
263 return SG_BUCKET_SPAN * degree_height;
266 SGBucket SGBucket::sibling(int dx, int dy) const
269 SG_LOG(SG_TERRAIN, SG_WARN, "SGBucket::sibling: requesting sibling of invalid bucket");
273 double clat = get_center_lat() + dy * SG_BUCKET_SPAN;
274 // return invalid here instead of clipping, so callers can discard
275 // invalid buckets without having to check if it's an existing one
276 if ((clat < -90.0) || (clat > 90.0)) {
280 // find the lon span for the new latitude
281 double span = sg_bucket_span( clat );
283 double tmp = get_center_lon() + dx * span;
284 tmp = SGMiscd::normalizePeriodic(-180.0, 180.0, tmp);
285 return SGBucket(tmp, clat);
288 std::string SGBucket::gen_index_str() const
291 ::snprintf(tmp, 20, "%ld",
292 (((long)lon + 180) << 14) + ((lat + 90) << 6)
294 return (std::string)tmp;
297 // find the bucket which is offset by the specified tile units in the
298 // X & Y direction. We need the current lon and lat to resolve
299 // ambiguities when going from a wider tile to a narrower one above or
300 // below. This assumes that we are feeding in
301 SGBucket sgBucketOffset( double dlon, double dlat, int dx, int dy ) {
302 SGBucket result( dlon, dlat );
303 double clat = result.get_center_lat() + dy * SG_BUCKET_SPAN;
305 // walk dy units in the lat direction
306 result.set_bucket( dlon, clat );
308 // find the lon span for the new latitude
309 double span = sg_bucket_span( clat );
311 // walk dx units in the lon direction
312 double tmp = dlon + dx * span;
313 while ( tmp < -180.0 ) {
316 while ( tmp >= 180.0 ) {
319 result.set_bucket( tmp, clat );
325 // calculate the offset between two buckets
326 void sgBucketDiff( const SGBucket& b1, const SGBucket& b2, int *dx, int *dy ) {
328 // Latitude difference
329 double c1_lat = b1.get_center_lat();
330 double c2_lat = b2.get_center_lat();
331 double diff_lat = c2_lat - c1_lat;
334 *dy = (int)rint( diff_lat / SG_BUCKET_SPAN );
336 if ( diff_lat > 0 ) {
337 *dy = (int)( diff_lat / SG_BUCKET_SPAN + 0.5 );
339 *dy = (int)( diff_lat / SG_BUCKET_SPAN - 0.5 );
343 // longitude difference
348 // To handle crossing the bucket size boundary
349 // we need to account for different size buckets.
351 if ( sg_bucket_span(c1_lat) <= sg_bucket_span(c2_lat) )
353 span = sg_bucket_span(c1_lat);
355 span = sg_bucket_span(c2_lat);
358 diff_lon = b2.get_center_lon() - b1.get_center_lon();
362 diff_lon -= b1.get_width()*0.5 + b2.get_width()*0.5 - span;
366 diff_lon += b1.get_width()*0.5 + b2.get_width()*0.5 - span;
371 *dx = (int)rint( diff_lon / span );
373 if ( diff_lon > 0 ) {
374 *dx = (int)( diff_lon / span + 0.5 );
376 *dx = (int)( diff_lon / span - 0.5 );
381 void sgGetBuckets( const SGGeod& min, const SGGeod& max, std::vector<SGBucket>& list ) {
382 double lon, lat, span;
384 for (lat = min.getLatitudeDeg(); lat <= max.getLatitudeDeg(); lat += SG_BUCKET_SPAN) {
385 span = sg_bucket_span( lat );
386 for (lon = min.getLongitudeDeg(); lon <= max.getLongitudeDeg(); lon += span)
388 SGBucket b(lon, lat);
398 std::ostream& operator<< ( std::ostream& out, const SGBucket& b )
400 return out << b.lon << ":" << (int)b.x << ", " << b.lat << ":" << (int)b.y;