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 <Include/compiler.h>
31 #ifdef FG_HAVE_STD_INCLUDES
33 # include <cstdio> // sprintf()
37 # include <stdio.h> // sprintf()
38 # include <iostream.h>
45 #if ! defined( FG_HAVE_NATIVE_SGI_COMPILERS )
46 FG_USING_STD(ostream);
49 #include <Include/fg_constants.h>
52 #define FG_BUCKET_SPAN 0.125 // 1/8 of a degree
53 #define FG_HALF_BUCKET_SPAN 0.0625 // 1/2 of 1/8 of a degree = 1/16 = 0.0625
56 ostream& operator<< ( ostream&, const FGBucket& );
57 bool operator== ( const FGBucket&, const FGBucket& );
62 double cx, cy; // centerpoint (lon, lat) in degrees of bucket
63 int lon; // longitude index (-180 to 179)
64 int lat; // latitude index (-90 to 89)
65 int x; // x subdivision (0 to 7)
66 int y; // y subdivision (0 to 7)
70 // default constructor
73 // create a bucket which would contain the specified lon/lat
74 FGBucket(const double lon, const double lat);
76 // create a bucket based on "long int" index
77 FGBucket(const long int bindex);
79 // create an impossible bucket if false
80 FGBucket(const bool is_good);
84 // Set the bucket params for the specified lat and lon
85 void set_bucket( double dlon, double dlat );
86 void make_bad ( void );
88 // Generate the unique scenery tile index for this bucket
90 string gen_index_str() const;
92 // Build the path name for this bucket
93 string gen_base_path() const;
95 // return the center lon of a tile
96 double get_center_lon() const;
98 // return width of the tile
99 double get_width() const;
101 // return the center lat of a tile
102 double get_center_lat() const;
104 // return height of the tile
105 double get_height() const;
107 // Informational methods
108 inline int get_lon() const { return lon; }
109 inline int get_lat() const { return lat; }
110 inline int get_x() const { return x; }
111 inline int get_y() const { return y; }
114 friend ostream& operator<< ( ostream&, const FGBucket& );
115 friend bool operator== ( const FGBucket&, const FGBucket& );
119 // return the horizontal tile span factor based on latitude
120 inline double bucket_span( double l ) {
123 } else if ( l >= 88.0 ) {
125 } else if ( l >= 86.0 ) {
127 } else if ( l >= 83.0 ) {
129 } else if ( l >= 76.0 ) {
131 } else if ( l >= 62.0 ) {
133 } else if ( l >= 22.0 ) {
135 } else if ( l >= -22.0 ) {
137 } else if ( l >= -62.0 ) {
139 } else if ( l >= -76.0 ) {
141 } else if ( l >= -83.0 ) {
143 } else if ( l >= -86.0 ) {
145 } else if ( l >= -88.0 ) {
147 } else if ( l >= -89.0 ) {
155 // Set the bucket params for the specified lat and lon
156 inline void FGBucket::set_bucket( double dlon, double dlat ) {
160 double span = bucket_span( dlat );
161 double diff = dlon - (double)(int)dlon;
163 // cout << "diff = " << diff << " span = " << span << endl;
165 if ( (dlon >= 0) || (fabs(diff) < FG_EPSILON) ) {
171 // find subdivision or super lon if needed
172 if ( span < FG_EPSILON ) {
176 } else if ( span <= 1.0 ) {
177 x = (int)((dlon - lon) / span);
179 if ( (dlon >= 0) || (fabs(diff) < FG_EPSILON) ) {
180 lon = (int)( (int)(lon / span) * span);
182 // cout << " lon = " << lon
183 // << " tmp = " << (int)((lon-1) / span) << endl;
184 lon = (int)( (int)((lon + 1) / span) * span - span);
195 diff = dlat - (double)(int)dlat;
197 if ( (dlat >= 0) || (fabs(diff) < FG_EPSILON) ) {
202 y = (int)((dlat - lat) * 8);
206 // default constructor
207 inline FGBucket::FGBucket() {}
210 // constructor for specified location
211 inline FGBucket::FGBucket(const double dlon, const double dlat) {
212 set_bucket(dlon, dlat);
216 // create an impossible bucket if false
217 inline FGBucket::FGBucket(const bool is_good) {
218 set_bucket(0.0, 0.0);
225 // Parse a unique scenery tile index and find the lon, lat, x, and y
226 inline FGBucket::FGBucket(const long int bindex) {
227 long int index = bindex;
244 // default destructor
245 inline FGBucket::~FGBucket() {}
248 // Generate the unique scenery tile index for this bucket
250 // The index is constructed as follows:
252 // 9 bits - to represent 360 degrees of longitude (-180 to 179)
253 // 8 bits - to represent 180 degrees of latitude (-90 to 89)
255 // Each 1 degree by 1 degree tile is further broken down into an 8x8
256 // grid. So we also need:
258 // 3 bits - to represent x (0 to 7)
259 // 3 bits - to represent y (0 to 7)
261 inline long int FGBucket::gen_index() {
262 return ((lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x;
265 inline string FGBucket::gen_index_str() const {
268 (((long)lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x);
273 // return the center lon of a tile
274 inline double FGBucket::get_center_lon() const {
275 double span = bucket_span( lat + y / 8.0 + FG_HALF_BUCKET_SPAN );
278 return lon + span / 2.0;
280 return lon + x * span + span / 2.0;
285 // return the center lat of a tile
286 inline double FGBucket::get_center_lat() const {
287 return lat + y / 8.0 + FG_HALF_BUCKET_SPAN;
291 // return width of the tile
292 inline double FGBucket::get_width() const {
293 return bucket_span( get_center_lat() );
297 // return height of the tile
298 inline double FGBucket::get_height() const {
299 return FG_BUCKET_SPAN;
303 // create an impossible bucket
304 inline void FGBucket::make_bad( void ) {
305 set_bucket(0.0, 0.0);
310 // offset a bucket struct by the specified tile units in the X & Y
312 FGBucket fgBucketOffset( double dlon, double dlat, int x, int y );
315 // calculate the offset between two buckets
316 void fgBucketDiff( const FGBucket& b1, const FGBucket& b2, int *dx, int *dy );
320 // Given a lat/lon, fill in the local tile index array
321 void fgBucketGenIdxArray(fgBUCKET *p1, fgBUCKET *tiles, int width, int height);
326 operator<< ( ostream& out, const FGBucket& b )
328 return out << b.lon << ":" << b.x << ", " << b.lat << ":" << b.y;
333 operator== ( const FGBucket& b1, const FGBucket& b2 )
335 return ( b1.lon == b2.lon &&
342 #endif // _NEWBUCKET_HXX