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 make_bad ( void );
95 // Generate the unique scenery tile index for this bucket
97 string gen_index_str() const;
99 // Build the path name for this bucket
100 string gen_base_path() const;
102 // return the center lon of a tile
103 double get_center_lon() const;
105 // return width of the tile
106 double get_width() const;
108 // return the center lat of a tile
109 double get_center_lat() const;
111 // return height of the tile
112 double get_height() const;
114 // Informational methods
115 inline int get_lon() const { return lon; }
116 inline int get_lat() const { return lat; }
117 inline int get_x() const { return x; }
118 inline int get_y() const { return y; }
121 friend ostream& operator<< ( ostream&, const FGBucket& );
122 friend bool operator== ( const FGBucket&, const FGBucket& );
126 // return the horizontal tile span factor based on latitude
127 inline double bucket_span( double l ) {
130 } else if ( l >= 88.0 ) {
132 } else if ( l >= 86.0 ) {
134 } else if ( l >= 83.0 ) {
136 } else if ( l >= 76.0 ) {
138 } else if ( l >= 62.0 ) {
140 } else if ( l >= 22.0 ) {
142 } else if ( l >= -22.0 ) {
144 } else if ( l >= -62.0 ) {
146 } else if ( l >= -76.0 ) {
148 } else if ( l >= -83.0 ) {
150 } else if ( l >= -86.0 ) {
152 } else if ( l >= -88.0 ) {
154 } else if ( l >= -89.0 ) {
162 // Set the bucket params for the specified lat and lon
163 inline void FGBucket::set_bucket( double dlon, double dlat ) {
167 double span = bucket_span( dlat );
168 double diff = dlon - (double)(int)dlon;
170 // cout << "diff = " << diff << " span = " << span << endl;
172 if ( (dlon >= 0) || (fabs(diff) < FG_EPSILON) ) {
178 // find subdivision or super lon if needed
179 if ( span < FG_EPSILON ) {
183 } else if ( span <= 1.0 ) {
184 x = (int)((dlon - lon) / span);
186 if ( (dlon >= 0) || (fabs(diff) < FG_EPSILON) ) {
187 lon = (int)( (int)(lon / span) * span);
189 // cout << " lon = " << lon
190 // << " tmp = " << (int)((lon-1) / span) << endl;
191 lon = (int)( (int)((lon + 1) / span) * span - span);
202 diff = dlat - (double)(int)dlat;
204 if ( (dlat >= 0) || (fabs(diff) < FG_EPSILON) ) {
209 y = (int)((dlat - lat) * 8);
213 // default constructor
214 inline FGBucket::FGBucket() {}
217 // constructor for specified location
218 inline FGBucket::FGBucket(const double dlon, const double dlat) {
219 set_bucket(dlon, dlat);
223 // create an impossible bucket if false
224 inline FGBucket::FGBucket(const bool is_good) {
225 set_bucket(0.0, 0.0);
232 // Parse a unique scenery tile index and find the lon, lat, x, and y
233 inline FGBucket::FGBucket(const long int bindex) {
234 long int index = bindex;
251 // default destructor
252 inline FGBucket::~FGBucket() {}
255 // Generate the unique scenery tile index for this bucket
257 // The index is constructed as follows:
259 // 9 bits - to represent 360 degrees of longitude (-180 to 179)
260 // 8 bits - to represent 180 degrees of latitude (-90 to 89)
262 // Each 1 degree by 1 degree tile is further broken down into an 8x8
263 // grid. So we also need:
265 // 3 bits - to represent x (0 to 7)
266 // 3 bits - to represent y (0 to 7)
268 inline long int FGBucket::gen_index() {
269 return ((lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x;
272 inline string FGBucket::gen_index_str() const {
275 (((long)lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x);
280 // return the center lon of a tile
281 inline double FGBucket::get_center_lon() const {
282 double span = bucket_span( lat + y / 8.0 + FG_HALF_BUCKET_SPAN );
285 return lon + span / 2.0;
287 return lon + x * span + span / 2.0;
292 // return the center lat of a tile
293 inline double FGBucket::get_center_lat() const {
294 return lat + y / 8.0 + FG_HALF_BUCKET_SPAN;
298 // return width of the tile
299 inline double FGBucket::get_width() const {
300 return bucket_span( get_center_lat() );
304 // return height of the tile
305 inline double FGBucket::get_height() const {
306 return FG_BUCKET_SPAN;
310 // create an impossible bucket
311 inline void FGBucket::make_bad( void ) {
312 set_bucket(0.0, 0.0);
317 // offset a bucket struct by the specified tile units in the X & Y
319 FGBucket fgBucketOffset( double dlon, double dlat, int x, int y );
322 // calculate the offset between two buckets
323 void fgBucketDiff( const FGBucket& b1, const FGBucket& b2, int *dx, int *dy );
327 // Given a lat/lon, fill in the local tile index array
328 void fgBucketGenIdxArray(fgBUCKET *p1, fgBUCKET *tiles, int width, int height);
333 operator<< ( ostream& out, const FGBucket& b )
335 return out << b.lon << ":" << b.x << ", " << b.lat << ":" << b.y;
340 operator== ( const FGBucket& b1, const FGBucket& b2 )
342 return ( b1.lon == b2.lon &&
349 #endif // _NEWBUCKET_HXX