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 #ifndef NO_DEPRECATED_API
69 // constructor for specified location
70 SGBucket::SGBucket(const double dlon, const double dlat) {
71 set_bucket(dlon, dlat);
75 SGBucket::SGBucket(const SGGeod& geod) {
76 innerSet(geod.getLongitudeDeg(),
77 geod.getLatitudeDeg());
80 // Parse a unique scenery tile index and find the lon, lat, x, and y
81 SGBucket::SGBucket(const long int bindex) {
82 long int index = bindex;
98 /* Calculate the greatest integral value less than
99 * or equal to the given value (floor(x)),
100 * but attribute coordinates close to the boundary to the next
101 * (increasing) integral
103 static int floorWithEpsilon(double x)
105 return static_cast<int>(floor(x + SG_EPSILON));
108 #ifndef NO_DEPRECATED_API
110 void SGBucket::set_bucket(double dlon, double dlat)
112 innerSet(dlon, dlat);
116 void SGBucket::set_bucket(const SGGeod& geod)
118 innerSet(geod.getLongitudeDeg(), geod.getLatitudeDeg());
123 // Set the bucket params for the specified lat and lon
124 void SGBucket::innerSet( double dlon, double dlat )
126 if ((dlon < -180.0) || (dlon >= 180.0)) {
127 SG_LOG(SG_TERRAIN, SG_WARN, "SGBucket::set_bucket: passed longitude:" << dlon);
128 dlon = SGMiscd::normalizePeriodic(-180.0, 180.0, dlon);
131 if ((dlat < -90.0) || (dlat > 90.0)) {
132 SG_LOG(SG_TERRAIN, SG_WARN, "SGBucket::set_bucket: passed latitude" << dlat);
133 dlat = SGMiscd::clip(dlat, -90.0, 90.0);
139 double span = sg_bucket_span( dlat );
140 // we do NOT need to special case lon=180 here, since
141 // normalizePeriodic will never return 180; it will
142 // return -180, which is what we want.
143 lon = floorWithEpsilon(dlon);
145 // find subdivision or super lon if needed
147 /* We have more than one tile per degree of
148 * longitude, so we need an x offset.
150 x = floorWithEpsilon((dlon - lon) / span);
152 /* We have one or more degrees per tile,
153 * so we need to find the base longitude
156 * First we calculate the integral base longitude
157 * (e.g. -85.5 => -86) and then find the greatest
158 * multiple of span that is less than or equal to
161 * That way, the Greenwich Meridian is always
164 lon=static_cast<int>(floor(lon / span) * span);
171 lat = floorWithEpsilon(dlat);
173 // special case when passing in the north pole point (possibly due to
174 // clipping latitude above). Ensures we generate a valid bucket in this
180 /* Latitude base and offset are easier, as
181 * tiles always are 1/8 degree of latitude wide.
183 y = floorWithEpsilon((dlat - lat) * 8);
187 // Build the path name for this bucket
188 std::string SGBucket::gen_base_path() const {
190 int top_lon, top_lat, main_lon, main_lat;
196 if ( (lon < 0) && (top_lon * 10 != lon) ) {
200 if ( top_lon >= 0 ) {
206 if ( main_lon < 0 ) {
212 if ( (lat < 0) && (top_lat * 10 != lat) ) {
216 if ( top_lat >= 0 ) {
222 if ( main_lat < 0 ) {
226 ::snprintf(raw_path, 256, "%c%03d%c%02d/%c%03d%c%02d",
227 hem, top_lon, pole, top_lat,
228 hem, main_lon, pole, main_lat);
230 SGPath path( raw_path );
236 // return width of the tile in degrees
237 double SGBucket::get_width() const {
238 return sg_bucket_span( get_center_lat() );
242 // return height of the tile in degrees
243 double SGBucket::get_height() const {
244 return SG_BUCKET_SPAN;
247 double SGBucket::get_highest_lat() const
249 unsigned char adjustedY = y;
251 // tile is north of the equator, so we want the top edge. Add one
252 // to y to achieve this.
256 return lat + (adjustedY / 8.0);
260 // return width of the tile in meters. This function is used by the
261 // tile-manager to estimate how many tiles are in the view distance, so
262 // we care about the smallest width, which occurs at the highest latitude.
263 double SGBucket::get_width_m() const
265 double clat_rad = get_highest_lat() * SGD_DEGREES_TO_RADIANS;
266 double cos_lat = cos( clat_rad );
267 if (fabs(cos_lat) < SG_EPSILON) {
268 // happens for polar tiles, since we pass in a latitude of 90
269 // return an arbitrary small value so all tiles are loaded
273 double local_radius = cos_lat * SG_EQUATORIAL_RADIUS_M;
274 double local_perimeter = local_radius * SGD_2PI;
275 double degree_width = local_perimeter / 360.0;
277 return get_width() * degree_width;
281 // return height of the tile in meters
282 double SGBucket::get_height_m() const {
283 double perimeter = SG_EQUATORIAL_RADIUS_M * SGD_2PI;
284 double degree_height = perimeter / 360.0;
286 return SG_BUCKET_SPAN * degree_height;
289 SGBucket SGBucket::sibling(int dx, int dy) const
292 SG_LOG(SG_TERRAIN, SG_WARN, "SGBucket::sibling: requesting sibling of invalid bucket");
296 double clat = get_center_lat() + dy * SG_BUCKET_SPAN;
297 // return invalid here instead of clipping, so callers can discard
298 // invalid buckets without having to check if it's an existing one
299 if ((clat < -90.0) || (clat > 90.0)) {
303 // find the lon span for the new latitude
304 double span = sg_bucket_span( clat );
306 double tmp = get_center_lon() + dx * span;
307 tmp = SGMiscd::normalizePeriodic(-180.0, 180.0, tmp);
310 b.innerSet(tmp, clat);
314 std::string SGBucket::gen_index_str() const
317 ::snprintf(tmp, 20, "%ld",
318 (((long)lon + 180) << 14) + ((lat + 90) << 6)
320 return (std::string)tmp;
323 #ifndef NO_DEPRECATED_API
324 // find the bucket which is offset by the specified tile units in the
325 // X & Y direction. We need the current lon and lat to resolve
326 // ambiguities when going from a wider tile to a narrower one above or
327 // below. This assumes that we are feeding in
328 SGBucket sgBucketOffset( double dlon, double dlat, int dx, int dy ) {
329 SGBucket result( dlon, dlat );
330 double clat = result.get_center_lat() + dy * SG_BUCKET_SPAN;
332 // walk dy units in the lat direction
333 result.set_bucket( dlon, clat );
335 // find the lon span for the new latitude
336 double span = sg_bucket_span( clat );
338 // walk dx units in the lon direction
339 double tmp = dlon + dx * span;
340 while ( tmp < -180.0 ) {
343 while ( tmp >= 180.0 ) {
346 result.set_bucket( tmp, clat );
352 // calculate the offset between two buckets
353 void sgBucketDiff( const SGBucket& b1, const SGBucket& b2, int *dx, int *dy ) {
355 // Latitude difference
356 double c1_lat = b1.get_center_lat();
357 double c2_lat = b2.get_center_lat();
358 double diff_lat = c2_lat - c1_lat;
361 *dy = (int)rint( diff_lat / SG_BUCKET_SPAN );
363 if ( diff_lat > 0 ) {
364 *dy = (int)( diff_lat / SG_BUCKET_SPAN + 0.5 );
366 *dy = (int)( diff_lat / SG_BUCKET_SPAN - 0.5 );
370 // longitude difference
375 // To handle crossing the bucket size boundary
376 // we need to account for different size buckets.
378 if ( sg_bucket_span(c1_lat) <= sg_bucket_span(c2_lat) )
380 span = sg_bucket_span(c1_lat);
382 span = sg_bucket_span(c2_lat);
385 diff_lon = b2.get_center_lon() - b1.get_center_lon();
389 diff_lon -= b1.get_width()*0.5 + b2.get_width()*0.5 - span;
393 diff_lon += b1.get_width()*0.5 + b2.get_width()*0.5 - span;
398 *dx = (int)rint( diff_lon / span );
400 if ( diff_lon > 0 ) {
401 *dx = (int)( diff_lon / span + 0.5 );
403 *dx = (int)( diff_lon / span - 0.5 );
408 void sgGetBuckets( const SGGeod& min, const SGGeod& max, std::vector<SGBucket>& list ) {
409 double lon, lat, span;
411 for (lat = min.getLatitudeDeg(); lat < max.getLatitudeDeg()+SG_BUCKET_SPAN; lat += SG_BUCKET_SPAN) {
412 span = sg_bucket_span( lat );
413 for (lon = min.getLongitudeDeg(); lon <= max.getLongitudeDeg(); lon += span)
415 SGBucket b(SGGeod::fromDeg(lon, lat));
425 std::ostream& operator<< ( std::ostream& out, const SGBucket& b )
427 return out << b.lon << ":" << (int)b.x << ", " << b.lat << ":" << (int)b.y;