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 - curt@flightgear.org
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 Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
24 **************************************************************************/
26 /** \file newbucket.hxx
27 * A class and associated utiltity functions to manage world scenery tiling.
30 #ifndef _NEWBUCKET_HXX
31 #define _NEWBUCKET_HXX
33 #include <simgear/compiler.h>
34 #include <simgear/constants.h>
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 FG_USING_STD(sprintf);
58 #if ! defined( FG_HAVE_NATIVE_SGI_COMPILERS )
59 FG_USING_STD(ostream);
64 * standard size of a bucket in degrees (1/8 of a degree)
66 #define SG_BUCKET_SPAN 0.125
69 * half of a standard SG_BUCKET_SPAN
71 #define SG_HALF_BUCKET_SPAN ( 0.5 * SG_BUCKET_SPAN )
74 // return the horizontal tile span factor based on latitude
75 static double sg_bucket_span( double l ) {
78 } else if ( l >= 88.0 ) {
80 } else if ( l >= 86.0 ) {
82 } else if ( l >= 83.0 ) {
84 } else if ( l >= 76.0 ) {
86 } else if ( l >= 62.0 ) {
88 } else if ( l >= 22.0 ) {
90 } else if ( l >= -22.0 ) {
92 } else if ( l >= -62.0 ) {
94 } else if ( l >= -76.0 ) {
96 } else if ( l >= -83.0 ) {
98 } else if ( l >= -86.0 ) {
100 } else if ( l >= -88.0 ) {
102 } else if ( l >= -89.0 ) {
111 * A class to manage world scenery tiling.
112 * This class encapsulates the world tiling scheme. It provides ways
113 * to calculate a unique tile index from a lat/lon, and it can provide
114 * information such as the dimensions of a given tile.
120 double cx, cy; // centerpoint (lon, lat) in degrees of bucket
121 int lon; // longitude index (-180 to 179)
122 int lat; // latitude index (-90 to 89)
123 int x; // x subdivision (0 to 7)
124 int y; // y subdivision (0 to 7)
129 * Default constructor.
134 * Construct a bucket given a specific location.
135 * @param dlon longitude specified in degrees
136 * @param dlat latitude specified in degrees
138 SGBucket(const double dlon, const double dlat);
140 /** Construct a bucket.
141 * @param is_good if false, create an invalid bucket. This is
142 * useful * if you are comparing cur_bucket to last_bucket and
143 * you want to * make sure last_bucket starts out as something
146 SGBucket(const bool is_good);
148 /** Construct a bucket given a unique bucket index number.
149 * @param bindex unique bucket index
151 SGBucket(const long int bindex);
154 * Default destructor.
159 * Reset a bucket to represent a new lat and lon
160 * @param dlon longitude specified in degrees
161 * @param dlat latitude specified in degrees
163 void set_bucket( double dlon, double dlat );
166 * Reset a bucket to represent a new lat and lon
167 * @param lonlat an array of double[2] holding lon and lat
168 * (specified) in degrees
170 void set_bucket( double *lonlat );
173 * Create an impossible bucket.
174 * This is useful if you are comparing cur_bucket to last_bucket
175 * and you want to make sure last_bucket starts out as something
178 inline void make_bad() {
179 set_bucket(0.0, 0.0);
184 * Generate the unique scenery tile index for this bucket
186 * The index is constructed as follows:
188 * 9 bits - to represent 360 degrees of longitude (-180 to 179)
189 * 8 bits - to represent 180 degrees of latitude (-90 to 89)
191 * Each 1 degree by 1 degree tile is further broken down into an 8x8
192 * grid. So we also need:
194 * 3 bits - to represent x (0 to 7)
195 * 3 bits - to represent y (0 to 7)
197 inline long int gen_index() const {
198 return ((lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x;
202 * Generate the unique scenery tile index for this bucket in ascii
205 inline string gen_index_str() const {
208 (((long)lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x);
213 * Build the base path name for this bucket.
215 string gen_base_path() const;
218 * Return the center lon of a tile.
220 inline double get_center_lon() const {
221 double span = sg_bucket_span( lat + y / 8.0 + SG_HALF_BUCKET_SPAN );
224 return lon + span / 2.0;
226 return lon + x * span + span / 2.0;
231 * Return the center lat of a tile.
233 inline double get_center_lat() const {
234 return lat + y / 8.0 + SG_HALF_BUCKET_SPAN;
238 * Return width of the tile in degrees.
240 double get_width() const;
243 * Return height of the tile in degrees.
245 double get_height() const;
248 * Return width of the tile in meters.
250 double get_width_m() const;
253 * Return height of the tile in meters.
255 double get_height_m() const;
257 // Informational methods.
260 * Return the lon of the lower left corner of this tile.
262 inline int get_lon() const { return lon; }
265 * Return the lat of the lower left corner of this tile.
267 inline int get_lat() const { return lat; }
270 * Return the x coord within the 1x1 degree chunk this tile.
272 inline int get_x() const { return x; }
275 * Return the y coord within the 1x1 degree chunk this tile.
277 inline int get_y() const { return y; }
281 friend ostream& operator<< ( ostream&, const SGBucket& );
282 friend bool operator== ( const SGBucket&, const SGBucket& );
287 * Return the bucket which is offset from the specified dlon, dlat by
288 * the specified tile units in the X & Y direction.
289 * @param dlon starting lon in degrees
290 * @param dlat starting lat in degrees
291 * @param x number of bucket units to offset in x (lon) direction
292 * @param y number of bucket units to offset in y (lat) direction
294 SGBucket sgBucketOffset( double dlon, double dlat, int x, int y );
298 * Calculate the offset between two buckets (in quantity of buckets).
301 * @param dx offset distance (lon) in tile units
302 * @param dy offset distance (lat) in tile units
304 void sgBucketDiff( const SGBucket& b1, const SGBucket& b2, int *dx, int *dy );
308 * Write the bucket lon, lat, x, and y to the output stream.
309 * @param out output stream
313 operator<< ( ostream& out, const SGBucket& b )
315 return out << b.lon << ":" << b.x << ", " << b.lat << ":" << b.y;
320 * Compare two bucket structures for equality.
325 operator== ( const SGBucket& b1, const SGBucket& b2 )
327 return ( b1.lon == b2.lon &&
334 #endif // _NEWBUCKET_HXX