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 program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
23 **************************************************************************/
26 #ifndef _NEWBUCKET_HXX
27 #define _NEWBUCKET_HXX
29 #include <simgear/compiler.h>
30 #include <simgear/constants.h>
32 #ifdef FG_HAVE_STD_INCLUDES
34 # include <cstdio> // sprintf()
38 # include <stdio.h> // sprintf()
39 # include <iostream.h>
42 // I don't understand ... <math.h> or <cmath> should be included
43 // already depending on how you defined FG_HAVE_STD_INCLUDES, but I
44 // can go ahead and add this -- CLO
46 # include <math.h> // needed fabs()
53 #if ! defined( FG_HAVE_NATIVE_SGI_COMPILERS )
54 FG_USING_STD(ostream);
59 #define FG_BUCKET_SPAN 0.125 // 1/8 of a degree
60 #define FG_HALF_BUCKET_SPAN 0.0625 // 1/2 of 1/8 of a degree = 1/16 = 0.0625
63 ostream& operator<< ( ostream&, const FGBucket& );
64 bool operator== ( const FGBucket&, const FGBucket& );
69 double cx, cy; // centerpoint (lon, lat) in degrees of bucket
70 int lon; // longitude index (-180 to 179)
71 int lat; // latitude index (-90 to 89)
72 int x; // x subdivision (0 to 7)
73 int y; // y subdivision (0 to 7)
77 // default constructor
80 // create a bucket which would contain the specified lon/lat
81 FGBucket(const double lon, const double lat);
83 // create a bucket based on "long int" index
84 FGBucket(const long int bindex);
86 // create an impossible bucket if false
87 FGBucket(const bool is_good);
91 // Set the bucket params for the specified lat and lon
92 void set_bucket( double dlon, double dlat );
93 void set_bucket( double *lonlat ) {
94 set_bucket( lonlat[0], lonlat[1] );
97 void make_bad ( void );
99 // Generate the unique scenery tile index for this bucket
100 long int gen_index();
101 string gen_index_str() const;
103 // Build the path name for this bucket
104 string gen_base_path() const;
106 // return the center lon of a tile
107 double get_center_lon() const;
109 // return width of the tile
110 double get_width() const;
112 // return the center lat of a tile
113 double get_center_lat() const;
115 // return height of the tile
116 double get_height() const;
118 // Informational methods
119 inline int get_lon() const { return lon; }
120 inline int get_lat() const { return lat; }
121 inline int get_x() const { return x; }
122 inline int get_y() const { return y; }
125 friend ostream& operator<< ( ostream&, const FGBucket& );
126 friend bool operator== ( const FGBucket&, const FGBucket& );
130 // return the horizontal tile span factor based on latitude
131 inline double bucket_span( double l ) {
134 } else if ( l >= 88.0 ) {
136 } else if ( l >= 86.0 ) {
138 } else if ( l >= 83.0 ) {
140 } else if ( l >= 76.0 ) {
142 } else if ( l >= 62.0 ) {
144 } else if ( l >= 22.0 ) {
146 } else if ( l >= -22.0 ) {
148 } else if ( l >= -62.0 ) {
150 } else if ( l >= -76.0 ) {
152 } else if ( l >= -83.0 ) {
154 } else if ( l >= -86.0 ) {
156 } else if ( l >= -88.0 ) {
158 } else if ( l >= -89.0 ) {
166 // Set the bucket params for the specified lat and lon
167 inline void FGBucket::set_bucket( double dlon, double dlat ) {
171 double span = bucket_span( dlat );
172 double diff = dlon - (double)(int)dlon;
174 // cout << "diff = " << diff << " span = " << span << endl;
176 if ( (dlon >= 0) || (fabs(diff) < FG_EPSILON) ) {
182 // find subdivision or super lon if needed
183 if ( span < FG_EPSILON ) {
187 } else if ( span <= 1.0 ) {
188 x = (int)((dlon - lon) / span);
190 if ( (dlon >= 0) || (fabs(diff) < FG_EPSILON) ) {
191 lon = (int)( (int)(lon / span) * span);
193 // cout << " lon = " << lon
194 // << " tmp = " << (int)((lon-1) / span) << endl;
195 lon = (int)( (int)((lon + 1) / span) * span - span);
206 diff = dlat - (double)(int)dlat;
208 if ( (dlat >= 0) || (fabs(diff) < FG_EPSILON) ) {
213 y = (int)((dlat - lat) * 8);
217 // default constructor
218 inline FGBucket::FGBucket() {}
221 // constructor for specified location
222 inline FGBucket::FGBucket(const double dlon, const double dlat) {
223 set_bucket(dlon, dlat);
227 // create an impossible bucket if false
228 inline FGBucket::FGBucket(const bool is_good) {
229 set_bucket(0.0, 0.0);
236 // Parse a unique scenery tile index and find the lon, lat, x, and y
237 inline FGBucket::FGBucket(const long int bindex) {
238 long int index = bindex;
255 // default destructor
256 inline FGBucket::~FGBucket() {}
259 // Generate the unique scenery tile index for this bucket
261 // The index is constructed as follows:
263 // 9 bits - to represent 360 degrees of longitude (-180 to 179)
264 // 8 bits - to represent 180 degrees of latitude (-90 to 89)
266 // Each 1 degree by 1 degree tile is further broken down into an 8x8
267 // grid. So we also need:
269 // 3 bits - to represent x (0 to 7)
270 // 3 bits - to represent y (0 to 7)
272 inline long int FGBucket::gen_index() {
273 return ((lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x;
276 inline string FGBucket::gen_index_str() const {
279 (((long)lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x);
284 // return the center lon of a tile
285 inline double FGBucket::get_center_lon() const {
286 double span = bucket_span( lat + y / 8.0 + FG_HALF_BUCKET_SPAN );
289 return lon + span / 2.0;
291 return lon + x * span + span / 2.0;
296 // return the center lat of a tile
297 inline double FGBucket::get_center_lat() const {
298 return lat + y / 8.0 + FG_HALF_BUCKET_SPAN;
302 // return width of the tile
303 inline double FGBucket::get_width() const {
304 return bucket_span( get_center_lat() );
308 // return height of the tile
309 inline double FGBucket::get_height() const {
310 return FG_BUCKET_SPAN;
314 // create an impossible bucket
315 inline void FGBucket::make_bad( void ) {
316 set_bucket(0.0, 0.0);
321 // offset a bucket struct by the specified tile units in the X & Y
323 FGBucket fgBucketOffset( double dlon, double dlat, int x, int y );
326 // calculate the offset between two buckets
327 void fgBucketDiff( const FGBucket& b1, const FGBucket& b2, int *dx, int *dy );
331 operator<< ( ostream& out, const FGBucket& b )
333 return out << b.lon << ":" << b.x << ", " << b.lat << ":" << b.y;
338 operator== ( const FGBucket& b1, const FGBucket& b2 )
340 return ( b1.lon == b2.lon &&
347 #endif // _NEWBUCKET_HXX