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();
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
111 double get_width() const;
113 // return the center lat of a tile
114 double get_center_lat() const;
116 // return height of the tile
117 double get_height() const;
119 // Informational methods
120 inline int get_lon() const { return lon; }
121 inline int get_lat() const { return lat; }
122 inline int get_x() const { return x; }
123 inline int get_y() const { return y; }
126 friend ostream& operator<< ( ostream&, const FGBucket& );
127 friend bool operator== ( const FGBucket&, const FGBucket& );
131 // return the horizontal tile span factor based on latitude
132 inline double bucket_span( double l ) {
135 } else if ( l >= 88.0 ) {
137 } else if ( l >= 86.0 ) {
139 } else if ( l >= 83.0 ) {
141 } else if ( l >= 76.0 ) {
143 } else if ( l >= 62.0 ) {
145 } else if ( l >= 22.0 ) {
147 } else if ( l >= -22.0 ) {
149 } else if ( l >= -62.0 ) {
151 } else if ( l >= -76.0 ) {
153 } else if ( l >= -83.0 ) {
155 } else if ( l >= -86.0 ) {
157 } else if ( l >= -88.0 ) {
159 } else if ( l >= -89.0 ) {
167 // Set the bucket params for the specified lat and lon
168 inline void FGBucket::set_bucket( double dlon, double dlat ) {
172 double span = bucket_span( dlat );
173 double diff = dlon - (double)(int)dlon;
175 // cout << "diff = " << diff << " span = " << span << endl;
177 if ( (dlon >= 0) || (fabs(diff) < FG_EPSILON) ) {
183 // find subdivision or super lon if needed
184 if ( span < FG_EPSILON ) {
188 } else if ( span <= 1.0 ) {
189 x = (int)((dlon - lon) / span);
191 if ( (dlon >= 0) || (fabs(diff) < FG_EPSILON) ) {
192 lon = (int)( (int)(lon / span) * span);
194 // cout << " lon = " << lon
195 // << " tmp = " << (int)((lon-1) / span) << endl;
196 lon = (int)( (int)((lon + 1) / span) * span - span);
207 diff = dlat - (double)(int)dlat;
209 if ( (dlat >= 0) || (fabs(diff) < FG_EPSILON) ) {
214 y = (int)((dlat - lat) * 8);
218 // default constructor
219 inline FGBucket::FGBucket() {}
222 // constructor for specified location
223 inline FGBucket::FGBucket(const double dlon, const double dlat) {
224 set_bucket(dlon, dlat);
228 // create an impossible bucket if false
229 inline FGBucket::FGBucket(const bool is_good) {
230 set_bucket(0.0, 0.0);
237 // Parse a unique scenery tile index and find the lon, lat, x, and y
238 inline FGBucket::FGBucket(const long int bindex) {
239 long int index = bindex;
256 // default destructor
257 inline FGBucket::~FGBucket() {}
260 // Generate the unique scenery tile index for this bucket
262 // The index is constructed as follows:
264 // 9 bits - to represent 360 degrees of longitude (-180 to 179)
265 // 8 bits - to represent 180 degrees of latitude (-90 to 89)
267 // Each 1 degree by 1 degree tile is further broken down into an 8x8
268 // grid. So we also need:
270 // 3 bits - to represent x (0 to 7)
271 // 3 bits - to represent y (0 to 7)
273 inline long int FGBucket::gen_index() {
274 return ((lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x;
277 inline string FGBucket::gen_index_str() const {
280 (((long)lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x);
285 // return the center lon of a tile
286 inline double FGBucket::get_center_lon() const {
287 double span = bucket_span( lat + y / 8.0 + FG_HALF_BUCKET_SPAN );
290 return lon + span / 2.0;
292 return lon + x * span + span / 2.0;
297 // return the center lat of a tile
298 inline double FGBucket::get_center_lat() const {
299 return lat + y / 8.0 + FG_HALF_BUCKET_SPAN;
303 // return width of the tile
304 inline double FGBucket::get_width() const {
305 return bucket_span( get_center_lat() );
309 // return height of the tile
310 inline double FGBucket::get_height() const {
311 return FG_BUCKET_SPAN;
315 // create an impossible bucket
316 inline void FGBucket::make_bad( void ) {
317 set_bucket(0.0, 0.0);
322 // offset a bucket struct by the specified tile units in the X & Y
324 FGBucket fgBucketOffset( double dlon, double dlat, int x, int y );
327 // calculate the offset between two buckets
328 void fgBucketDiff( const FGBucket& b1, const FGBucket& b2, int *dx, int *dy );
332 operator<< ( ostream& out, const FGBucket& b )
334 return out << b.lon << ":" << b.x << ", " << b.lat << ":" << b.y;
339 operator== ( const FGBucket& b1, const FGBucket& b2 )
341 return ( b1.lon == b2.lon &&
348 #endif // _NEWBUCKET_HXX