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 **************************************************************************/
25 /** \file newbucket.hxx
26 * A class and associated utiltity functions to manage world scenery tiling.
28 * Tile borders are aligned along circles of latitude and longitude.
29 * All tiles are 1/8 degree of latitude high and their width in degrees
30 * longitude depends on their latitude, adjusted in such a way that
31 * all tiles cover about the same amount of area of the earth surface.
34 #ifndef _NEWBUCKET_HXX
35 #define _NEWBUCKET_HXX
37 #include <simgear/compiler.h>
38 #include <simgear/constants.h>
39 #include <simgear/math/SGMath.hxx>
42 #include <cstdio> // sprintf()
48 * standard size of a bucket in degrees (1/8 of a degree)
50 #define SG_BUCKET_SPAN 0.125
53 * half of a standard SG_BUCKET_SPAN
55 #define SG_HALF_BUCKET_SPAN ( 0.5 * SG_BUCKET_SPAN )
58 // return the horizontal tile span factor based on latitude
59 static double sg_bucket_span( double l ) {
62 } else if ( l >= 86.0 ) {
64 } else if ( l >= 83.0 ) {
66 } else if ( l >= 76.0 ) {
68 } else if ( l >= 62.0 ) {
70 } else if ( l >= 22.0 ) {
72 } else if ( l >= -22.0 ) {
74 } else if ( l >= -62.0 ) {
76 } else if ( l >= -76.0 ) {
78 } else if ( l >= -83.0 ) {
80 } else if ( l >= -86.0 ) {
82 } else if ( l >= -89.0 ) {
91 * A class to manage world scenery tiling.
92 * This class encapsulates the world tiling scheme. It provides ways
93 * to calculate a unique tile index from a lat/lon, and it can provide
94 * information such as the dimensions of a given tile.
100 short lon; // longitude index (-180 to 179)
101 short lat; // latitude index (-90 to 89)
102 char x; // x subdivision (0 to 7)
103 char y; // y subdivision (0 to 7)
108 * Default constructor.
113 * Construct a bucket given a specific location.
114 * @param dlon longitude specified in degrees
115 * @param dlat latitude specified in degrees
117 SGBucket(const double dlon, const double dlat);
120 * Construct a bucket given a specific location.
121 * @param dlon longitude specified in degrees
122 * @param dlat latitude specified in degrees
124 SGBucket(const SGGeod& geod);
126 /** Construct a bucket.
127 * @param is_good if false, create an invalid bucket. This is
128 * useful * if you are comparing cur_bucket to last_bucket and
129 * you want to * make sure last_bucket starts out as something
132 SGBucket(const bool is_good);
134 /** Construct a bucket given a unique bucket index number.
135 * @param bindex unique bucket index
137 SGBucket(const long int bindex);
140 * Reset a bucket to represent a new lat and lon
141 * @param dlon longitude specified in degrees
142 * @param dlat latitude specified in degrees
144 void set_bucket(const SGGeod& geod);
147 * Reset a bucket to represent a new lat and lon
148 * @param dlon longitude specified in degrees
149 * @param dlat latitude specified in degrees
151 void set_bucket( double dlon, double dlat );
154 * Create an impossible bucket.
155 * This is useful if you are comparing cur_bucket to last_bucket
156 * and you want to make sure last_bucket starts out as something
159 inline void make_bad() {
160 set_bucket(0.0, 0.0);
165 * Generate the unique scenery tile index for this bucket
167 * The index is constructed as follows:
169 * 9 bits - to represent 360 degrees of longitude (-180 to 179)
170 * 8 bits - to represent 180 degrees of latitude (-90 to 89)
172 * Each 1 degree by 1 degree tile is further broken down into an 8x8
173 * grid. So we also need:
175 * 3 bits - to represent x (0 to 7)
176 * 3 bits - to represent y (0 to 7)
179 inline long int gen_index() const {
180 return ((lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x;
184 * Generate the unique scenery tile index for this bucket in ascii
186 * @return tile index in string form
188 inline std::string gen_index_str() const {
190 std::sprintf(tmp, "%ld",
191 (((long)lon + 180) << 14) + ((lat + 90) << 6)
193 return (std::string)tmp;
197 * Build the base path name for this bucket.
198 * @return base path in string form
200 std::string gen_base_path() const;
203 * @return the center lon of a tile.
205 inline double get_center_lon() const {
206 double span = sg_bucket_span( lat + y / 8.0 + SG_HALF_BUCKET_SPAN );
209 return lon + get_width() / 2.0;
211 return lon + x * span + get_width() / 2.0;
216 * @return the center lat of a tile.
218 inline double get_center_lat() const {
219 return lat + y / 8.0 + SG_HALF_BUCKET_SPAN;
223 * @return the width of the tile in degrees.
225 double get_width() const;
228 * @return the height of the tile in degrees.
230 double get_height() const;
233 * @return the width of the tile in meters.
235 double get_width_m() const;
238 * @return the height of the tile in meters.
240 double get_height_m() const;
243 * @return the center of the bucket in geodetic coordinates.
245 SGGeod get_center() const
246 { return SGGeod::fromDeg(get_center_lon(), get_center_lat()); }
249 * @return the center of the bucket in geodetic coordinates.
251 SGGeod get_corner(unsigned num) const
253 double lonFac = ((num + 1) & 2) ? 0.5 : -0.5;
254 double latFac = ((num ) & 2) ? 0.5 : -0.5;
255 return SGGeod::fromDeg(get_center_lon() + lonFac*get_width(),
256 get_center_lat() + latFac*get_height());
259 // Informational methods.
262 * @return the lon of the lower left corner of
263 * the 1x1 chunk containing this tile.
265 inline int get_chunk_lon() const { return lon; }
268 * @return the lat of the lower left corner of
269 * the 1x1 chunk containing this tile.
271 inline int get_chunk_lat() const { return lat; }
274 * @return the x coord within the 1x1 degree chunk this tile.
276 inline int get_x() const { return x; }
279 * @return the y coord within the 1x1 degree chunk this tile.
281 inline int get_y() const { return y; }
285 friend std::ostream& operator<< ( std::ostream&, const SGBucket& );
286 friend bool operator== ( const SGBucket&, const SGBucket& );
289 inline bool operator!= (const SGBucket& lhs, const SGBucket& rhs)
291 return !(lhs == rhs);
297 * Return the bucket which is offset from the specified dlon, dlat by
298 * the specified tile units in the X & Y direction.
299 * @param dlon starting lon in degrees
300 * @param dlat starting lat in degrees
301 * @param x number of bucket units to offset in x (lon) direction
302 * @param y number of bucket units to offset in y (lat) direction
303 * @return offset bucket
305 SGBucket sgBucketOffset( double dlon, double dlat, int x, int y );
310 * Calculate the offset between two buckets (in quantity of buckets).
313 * @param dx offset distance (lon) in tile units
314 * @param dy offset distance (lat) in tile units
316 void sgBucketDiff( const SGBucket& b1, const SGBucket& b2, int *dx, int *dy );
321 * retrieve a list of buckets in the given bounding box
322 * @param min min lon,lat of bounding box in degrees
323 * @param max max lon,lat of bounding box in degrees
324 * @param list standard vector of buckets within the bounding box
326 void sgGetBuckets( const SGGeod& min, const SGGeod& max, std::vector<SGBucket>& list );
329 * Write the bucket lon, lat, x, and y to the output stream.
330 * @param out output stream
334 operator<< ( std::ostream& out, const SGBucket& b )
336 return out << b.lon << ":" << (int)b.x << ", " << b.lat << ":" << (int)b.y;
341 * Compare two bucket structures for equality.
344 * @return comparison result
347 operator== ( const SGBucket& b1, const SGBucket& b2 )
349 return ( b1.lon == b2.lon &&
356 #endif // _NEWBUCKET_HXX