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.
29 #ifndef _NEWBUCKET_HXX
30 #define _NEWBUCKET_HXX
32 #include <simgear/compiler.h>
33 #include <simgear/constants.h>
34 #include <simgear/math/SGMath.hxx>
36 #ifdef SG_HAVE_STD_INCLUDES
38 # include <cstdio> // sprintf()
41 # include <stdio.h> // sprintf()
46 // I don't understand ... <math.h> or <cmath> should be included
47 // already depending on how you defined SG_HAVE_STD_INCLUDES, but I
48 // can go ahead and add this -- CLO
50 SG_USING_STD(sprintf);
57 SG_USING_STD(ostream);
61 * standard size of a bucket in degrees (1/8 of a degree)
63 #define SG_BUCKET_SPAN 0.125
66 * half of a standard SG_BUCKET_SPAN
68 #define SG_HALF_BUCKET_SPAN ( 0.5 * SG_BUCKET_SPAN )
71 // return the horizontal tile span factor based on latitude
72 static double sg_bucket_span( double l ) {
75 } else if ( l >= 88.0 ) {
77 } else if ( l >= 86.0 ) {
79 } else if ( l >= 83.0 ) {
81 } else if ( l >= 76.0 ) {
83 } else if ( l >= 62.0 ) {
85 } else if ( l >= 22.0 ) {
87 } else if ( l >= -22.0 ) {
89 } else if ( l >= -62.0 ) {
91 } else if ( l >= -76.0 ) {
93 } else if ( l >= -83.0 ) {
95 } else if ( l >= -86.0 ) {
97 } else if ( l >= -88.0 ) {
99 } else if ( l >= -89.0 ) {
108 * A class to manage world scenery tiling.
109 * This class encapsulates the world tiling scheme. It provides ways
110 * to calculate a unique tile index from a lat/lon, and it can provide
111 * information such as the dimensions of a given tile.
117 double cx, cy; // centerpoint (lon, lat) in degrees of bucket
118 short lon; // longitude index (-180 to 179)
119 short lat; // latitude index (-90 to 89)
120 char x; // x subdivision (0 to 7)
121 char y; // y subdivision (0 to 7)
126 * Default constructor.
131 * Construct a bucket given a specific location.
132 * @param dlon longitude specified in degrees
133 * @param dlat latitude specified in degrees
135 SGBucket(const double dlon, const double dlat);
138 * Construct a bucket given a specific location.
139 * @param dlon longitude specified in degrees
140 * @param dlat latitude specified in degrees
142 SGBucket(const SGGeod& geod);
144 /** Construct a bucket.
145 * @param is_good if false, create an invalid bucket. This is
146 * useful * if you are comparing cur_bucket to last_bucket and
147 * you want to * make sure last_bucket starts out as something
150 SGBucket(const bool is_good);
152 /** Construct a bucket given a unique bucket index number.
153 * @param bindex unique bucket index
155 SGBucket(const long int bindex);
158 * Reset a bucket to represent a new lat and lon
159 * @param dlon longitude specified in degrees
160 * @param dlat latitude specified in degrees
162 void set_bucket( double dlon, double dlat );
165 * Reset a bucket to represent a new lat and lon
166 * @param lonlat an array of double[2] holding lon and lat
167 * (specified) in degrees
169 void set_bucket( double *lonlat );
172 * Reset a bucket to represent a new lat and lon
173 * @param dlon longitude specified in degrees
174 * @param dlat latitude specified in degrees
176 void set_bucket(const SGGeod& geod);
179 * Create an impossible bucket.
180 * This is useful if you are comparing cur_bucket to last_bucket
181 * and you want to make sure last_bucket starts out as something
184 inline void make_bad() {
185 set_bucket(0.0, 0.0);
190 * Generate the unique scenery tile index for this bucket
192 * The index is constructed as follows:
194 * 9 bits - to represent 360 degrees of longitude (-180 to 179)
195 * 8 bits - to represent 180 degrees of latitude (-90 to 89)
197 * Each 1 degree by 1 degree tile is further broken down into an 8x8
198 * grid. So we also need:
200 * 3 bits - to represent x (0 to 7)
201 * 3 bits - to represent y (0 to 7)
204 inline long int gen_index() const {
205 return ((lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x;
209 * Generate the unique scenery tile index for this bucket in ascii
211 * @return tile index in string form
213 inline string gen_index_str() const {
216 (((long)lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x);
221 * Build the base path name for this bucket.
222 * @return base path in string form
224 string gen_base_path() const;
227 * @return the center lon of a tile.
229 inline double get_center_lon() const {
230 double span = sg_bucket_span( lat + y / 8.0 + SG_HALF_BUCKET_SPAN );
233 return lon + span / 2.0;
235 return lon + x * span + span / 2.0;
240 * @return the center lat of a tile.
242 inline double get_center_lat() const {
243 return lat + y / 8.0 + SG_HALF_BUCKET_SPAN;
247 * @return the width of the tile in degrees.
249 double get_width() const;
252 * @return the height of the tile in degrees.
254 double get_height() const;
257 * @return the width of the tile in meters.
259 double get_width_m() const;
262 * @return the height of the tile in meters.
264 double get_height_m() const;
267 * @return the center of the bucket in geodetic coordinates.
269 SGGeod get_center() const
270 { return SGGeod::fromDeg(get_center_lon(), get_center_lat()); }
273 * @return the center of the bucket in geodetic coordinates.
275 SGGeod get_corner(unsigned num) const
277 double lonFac = ((num + 1) & 2) ? 0.5 : -0.5;
278 double latFac = ((num ) & 2) ? 0.5 : -0.5;
279 return SGGeod::fromDeg(get_center_lon() + lonFac*get_width(),
280 get_center_lat() + latFac*get_height());
283 // Informational methods.
286 * @return the lon of the lower left corner of
287 * the 1x1 chunk containing this tile.
289 inline int get_chunk_lon() const { return lon; }
292 * @return the lat of the lower left corner of
293 * the 1x1 chunk containing this tile.
295 inline int get_chunk_lat() const { return lat; }
298 * @return the x coord within the 1x1 degree chunk this tile.
300 inline int get_x() const { return x; }
303 * @return the y coord within the 1x1 degree chunk this tile.
305 inline int get_y() const { return y; }
309 friend ostream& operator<< ( ostream&, const SGBucket& );
310 friend bool operator== ( const SGBucket&, const SGBucket& );
313 inline bool operator!= (const SGBucket& lhs, const SGBucket& rhs)
315 return !(lhs == rhs);
321 * Return the bucket which is offset from the specified dlon, dlat by
322 * the specified tile units in the X & Y direction.
323 * @param dlon starting lon in degrees
324 * @param dlat starting lat in degrees
325 * @param x number of bucket units to offset in x (lon) direction
326 * @param y number of bucket units to offset in y (lat) direction
327 * @return offset bucket
329 SGBucket sgBucketOffset( double dlon, double dlat, int x, int y );
334 * Calculate the offset between two buckets (in quantity of buckets).
337 * @param dx offset distance (lon) in tile units
338 * @param dy offset distance (lat) in tile units
340 void sgBucketDiff( const SGBucket& b1, const SGBucket& b2, int *dx, int *dy );
344 * Write the bucket lon, lat, x, and y to the output stream.
345 * @param out output stream
349 operator<< ( ostream& out, const SGBucket& b )
351 return out << b.lon << ":" << b.x << ", " << b.lat << ":" << b.y;
356 * Compare two bucket structures for equality.
359 * @return comparison result
362 operator== ( const SGBucket& b1, const SGBucket& b2 )
364 return ( b1.lon == b2.lon &&
371 #endif // _NEWBUCKET_HXX