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 **************************************************************************/
27 #ifndef _NEWBUCKET_HXX
28 #define _NEWBUCKET_HXX
30 #include <simgear/compiler.h>
31 #include <simgear/constants.h>
33 #ifdef FG_HAVE_STD_INCLUDES
35 # include <cstdio> // sprintf()
39 # include <stdio.h> // sprintf()
40 # include <iostream.h>
43 // I don't understand ... <math.h> or <cmath> should be included
44 // already depending on how you defined FG_HAVE_STD_INCLUDES, but I
45 // can go ahead and add this -- CLO
47 # include <math.h> // needed fabs()
54 #if ! defined( FG_HAVE_NATIVE_SGI_COMPILERS )
55 FG_USING_STD(ostream);
60 #define FG_BUCKET_SPAN 0.125 // 1/8 of a degree
61 #define FG_HALF_BUCKET_SPAN 0.0625 // 1/2 of 1/8 of a degree = 1/16 = 0.0625
64 ostream& operator<< ( ostream&, const FGBucket& );
65 bool operator== ( const FGBucket&, const FGBucket& );
70 double cx, cy; // centerpoint (lon, lat) in degrees of bucket
71 int lon; // longitude index (-180 to 179)
72 int lat; // latitude index (-90 to 89)
73 int x; // x subdivision (0 to 7)
74 int y; // y subdivision (0 to 7)
78 // default constructor
81 // create a bucket which would contain the specified lon/lat
82 FGBucket(const double lon, const double lat);
84 // create a bucket based on "long int" index
85 FGBucket(const long int bindex);
87 // create an impossible bucket if false
88 FGBucket(const bool is_good);
92 // Set the bucket params for the specified lat and lon
93 void set_bucket( double dlon, double dlat );
94 void set_bucket( double *lonlat ) {
95 set_bucket( lonlat[0], lonlat[1] );
98 void make_bad ( void );
100 // Generate the unique scenery tile index for this bucket
101 long int gen_index() const;
102 string gen_index_str() const;
104 // Build the path name for this bucket
105 string gen_base_path() const;
107 // return the center lon of a tile
108 double get_center_lon() const;
110 // return width of the tile in degrees
111 double get_width() const;
112 // return width of the tile in meters
113 double get_width_m() const;
114 // return height of the tile in degrees
115 double get_height() const;
116 // return height of the tile in meters
117 double get_height_m() const;
119 // return the center lat of a tile
120 double get_center_lat() const;
123 // Informational methods
124 inline int get_lon() const { return lon; }
125 inline int get_lat() const { return lat; }
126 inline int get_x() const { return x; }
127 inline int get_y() const { return y; }
130 friend ostream& operator<< ( ostream&, const FGBucket& );
131 friend bool operator== ( const FGBucket&, const FGBucket& );
135 // return the horizontal tile span factor based on latitude
136 inline double bucket_span( double l ) {
139 } else if ( l >= 88.0 ) {
141 } else if ( l >= 86.0 ) {
143 } else if ( l >= 83.0 ) {
145 } else if ( l >= 76.0 ) {
147 } else if ( l >= 62.0 ) {
149 } else if ( l >= 22.0 ) {
151 } else if ( l >= -22.0 ) {
153 } else if ( l >= -62.0 ) {
155 } else if ( l >= -76.0 ) {
157 } else if ( l >= -83.0 ) {
159 } else if ( l >= -86.0 ) {
161 } else if ( l >= -88.0 ) {
163 } else if ( l >= -89.0 ) {
171 // Set the bucket params for the specified lat and lon
172 inline void FGBucket::set_bucket( double dlon, double dlat ) {
176 double span = bucket_span( dlat );
177 double diff = dlon - (double)(int)dlon;
179 // cout << "diff = " << diff << " span = " << span << endl;
181 if ( (dlon >= 0) || (fabs(diff) < FG_EPSILON) ) {
187 // find subdivision or super lon if needed
188 if ( span < FG_EPSILON ) {
192 } else if ( span <= 1.0 ) {
193 x = (int)((dlon - lon) / span);
195 if ( (dlon >= 0) || (fabs(diff) < FG_EPSILON) ) {
196 lon = (int)( (int)(lon / span) * span);
198 // cout << " lon = " << lon
199 // << " tmp = " << (int)((lon-1) / span) << endl;
200 lon = (int)( (int)((lon + 1) / span) * span - span);
211 diff = dlat - (double)(int)dlat;
213 if ( (dlat >= 0) || (fabs(diff) < FG_EPSILON) ) {
218 y = (int)((dlat - lat) * 8);
222 // default constructor
223 inline FGBucket::FGBucket() {}
226 // constructor for specified location
227 inline FGBucket::FGBucket(const double dlon, const double dlat) {
228 set_bucket(dlon, dlat);
232 // create an impossible bucket if false
233 inline FGBucket::FGBucket(const bool is_good) {
234 set_bucket(0.0, 0.0);
241 // Parse a unique scenery tile index and find the lon, lat, x, and y
242 inline FGBucket::FGBucket(const long int bindex) {
243 long int index = bindex;
260 // default destructor
261 inline FGBucket::~FGBucket() {}
264 // Generate the unique scenery tile index for this bucket
266 // The index is constructed as follows:
268 // 9 bits - to represent 360 degrees of longitude (-180 to 179)
269 // 8 bits - to represent 180 degrees of latitude (-90 to 89)
271 // Each 1 degree by 1 degree tile is further broken down into an 8x8
272 // grid. So we also need:
274 // 3 bits - to represent x (0 to 7)
275 // 3 bits - to represent y (0 to 7)
277 inline long int FGBucket::gen_index() const {
278 return ((lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x;
281 inline string FGBucket::gen_index_str() const {
284 (((long)lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x);
289 // return the center lon of a tile
290 inline double FGBucket::get_center_lon() const {
291 double span = bucket_span( lat + y / 8.0 + FG_HALF_BUCKET_SPAN );
294 return lon + span / 2.0;
296 return lon + x * span + span / 2.0;
301 // return the center lat of a tile
302 inline double FGBucket::get_center_lat() const {
303 return lat + y / 8.0 + FG_HALF_BUCKET_SPAN;
307 // return width of the tile in degrees
308 inline double FGBucket::get_width() const {
309 return bucket_span( get_center_lat() );
313 // return height of the tile in degrees
314 inline double FGBucket::get_height() const {
315 return FG_BUCKET_SPAN;
319 // create an impossible bucket
320 inline void FGBucket::make_bad( void ) {
321 set_bucket(0.0, 0.0);
326 // offset a bucket specified by dlon, dlat by the specified tile units
327 // in the X & Y direction
328 FGBucket fgBucketOffset( double dlon, double dlat, int x, int y );
331 // calculate the offset between two buckets
332 void fgBucketDiff( const FGBucket& b1, const FGBucket& b2, int *dx, int *dy );
336 operator<< ( ostream& out, const FGBucket& b )
338 return out << b.lon << ":" << b.x << ", " << b.lat << ":" << b.y;
343 operator== ( const FGBucket& b1, const FGBucket& b2 )
345 return ( b1.lon == b2.lon &&
352 #endif // _NEWBUCKET_HXX