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 * (Log is kept at end of this file)
24 **************************************************************************/
27 #ifndef _NEWBUCKET_HXX
28 #define _NEWBUCKET_HXX
30 #include <Include/compiler.h>
34 #ifdef FG_HAVE_STD_INCLUDES
35 # include <cstdio> // sprintf()
38 # include <stdio.h> // sprintf()
39 # include <iostream.h>
43 FG_USING_STD(ostream);
45 #include <Include/fg_constants.h>
48 #define FG_BUCKET_SPAN 0.125 // 1/8 of a degree
49 #define FG_HALF_BUCKET_SPAN 0.0625 // 1/2 of 1/8 of a degree = 1/16 = 0.0625
52 ostream& operator<< ( ostream&, const FGBucket& );
53 bool operator== ( const FGBucket&, const FGBucket& );
58 double cx, cy; // centerpoint (lon, lat) in degrees of bucket
59 int lon; // longitude index (-180 to 179)
60 int lat; // latitude index (-90 to 89)
61 int x; // x subdivision (0 to 7)
62 int y; // y subdivision (0 to 7)
66 // default constructor
69 // create a bucket which would contain the specified lon/lat
70 FGBucket(const double lon, const double lat);
72 // create a bucket based on "long int" index
73 FGBucket(const long int bindex);
75 // create an impossible bucket if false
76 FGBucket(const bool is_good);
80 // Set the bucket params for the specified lat and lon
81 void set_bucket( double dlon, double dlat );
83 // Generate the unique scenery tile index for this bucket
85 string gen_index_str() const;
87 // Build the path name for this bucket
88 string gen_base_path() const;
90 // return the center lon of a tile
91 double get_center_lon() const;
93 // return width of the tile
94 double get_width() const;
96 // return the center lat of a tile
97 double get_center_lat() const;
99 // return height of the tile
100 double get_height() const;
102 // Informational methods
103 inline int get_lon() const { return lon; }
104 inline int get_lat() const { return lat; }
105 inline int get_x() const { return x; }
106 inline int get_y() const { return y; }
109 friend ostream& operator<< ( ostream&, const FGBucket& );
110 friend bool operator== ( const FGBucket&, const FGBucket& );
114 // return the horizontal tile span factor based on latitude
115 inline double bucket_span( double l ) {
118 } else if ( l >= 88.0 ) {
120 } else if ( l >= 86.0 ) {
122 } else if ( l >= 83.0 ) {
124 } else if ( l >= 76.0 ) {
126 } else if ( l >= 62.0 ) {
128 } else if ( l >= 22.0 ) {
130 } else if ( l >= -22.0 ) {
132 } else if ( l >= -62.0 ) {
134 } else if ( l >= -76.0 ) {
136 } else if ( l >= -83.0 ) {
138 } else if ( l >= -86.0 ) {
140 } else if ( l >= -88.0 ) {
142 } else if ( l >= -89.0 ) {
150 // Set the bucket params for the specified lat and lon
151 inline void FGBucket::set_bucket( double dlon, double dlat ) {
155 double span = bucket_span( dlat );
156 double diff = dlon - (double)(int)dlon;
158 // cout << "diff = " << diff << " span = " << span << endl;
160 if ( (dlon >= 0) || (fabs(diff) < FG_EPSILON) ) {
166 // find subdivision or super lon if needed
167 if ( span < FG_EPSILON ) {
171 } else if ( span <= 1.0 ) {
172 x = (int)((dlon - lon) / span);
174 if ( (dlon >= 0) || (fabs(diff) < FG_EPSILON) ) {
175 lon = (int)( (int)(lon / span) * span);
177 // cout << " lon = " << lon
178 // << " tmp = " << (int)((lon-1) / span) << endl;
179 lon = (int)( (int)((lon + 1) / span) * span - span);
190 diff = dlat - (double)(int)dlat;
192 if ( (dlat >= 0) || (fabs(diff) < FG_EPSILON) ) {
197 y = (int)((dlat - lat) * 8);
201 // default constructor
202 inline FGBucket::FGBucket() {}
205 // constructor for specified location
206 inline FGBucket::FGBucket(const double dlon, const double dlat) {
207 set_bucket(dlon, dlat);
210 // create an impossible bucket if false
211 inline FGBucket::FGBucket(const bool is_good) {
212 set_bucket(0.0, 0.0);
219 // Parse a unique scenery tile index and find the lon, lat, x, and y
220 inline FGBucket::FGBucket(const long int bindex) {
221 long int index = bindex;
238 // default destructor
239 inline FGBucket::~FGBucket() {}
242 // Generate the unique scenery tile index for this bucket
244 // The index is constructed as follows:
246 // 9 bits - to represent 360 degrees of longitude (-180 to 179)
247 // 8 bits - to represent 180 degrees of latitude (-90 to 89)
249 // Each 1 degree by 1 degree tile is further broken down into an 8x8
250 // grid. So we also need:
252 // 3 bits - to represent x (0 to 7)
253 // 3 bits - to represent y (0 to 7)
255 inline long int FGBucket::gen_index() {
256 return ((lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x;
259 inline string FGBucket::gen_index_str() const {
262 (((long)lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x);
267 // return the center lon of a tile
268 inline double FGBucket::get_center_lon() const {
269 double span = bucket_span( lat + y / 8.0 + FG_HALF_BUCKET_SPAN );
272 return lon + span / 2.0;
274 return lon + x * span + span / 2.0;
279 // return width of the tile
280 inline double FGBucket::get_width() const {
281 return bucket_span( get_center_lat() );
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 height of the tile
292 inline double FGBucket::get_height() const {
293 return FG_BUCKET_SPAN;
297 // offset a bucket struct by the specified tile units in the X & Y
299 FGBucket fgBucketOffset( double dlon, double dlat, int x, int y );
302 // calculate the offset between two buckets
303 void fgBucketDiff( const FGBucket& b1, const FGBucket& b2, int *dx, int *dy );
307 // Given a lat/lon, fill in the local tile index array
308 void fgBucketGenIdxArray(fgBUCKET *p1, fgBUCKET *tiles, int width, int height);
313 operator<< ( ostream& out, const FGBucket& b )
315 return out << b.lon << ":" << b.x << ", " << b.lat << ":" << b.y;
320 operator== ( const FGBucket& b1, const FGBucket& b2 )
322 return ( b1.lon == b2.lon &&
329 #endif // _NEWBUCKET_HXX
333 // Revision 1.8 1999/03/27 05:34:06 curt
334 // Elimitated some const warnings from the compiler.
336 // Revision 1.7 1999/03/25 19:01:51 curt
337 // Jettisoned old bucketutils.[ch] for newbucket.[ch]xx
339 // Revision 1.6 1999/03/15 17:58:41 curt
340 // MSVC++ portability tweaks contributed by Bernie Bright.
341 // Added using std::ostream declaration.
342 // Added forward declarations to work around a MSVC bug.
344 // Revision 1.5 1999/03/12 22:51:18 curt
345 // Added some informational methods.
347 // Revision 1.4 1999/03/02 01:01:43 curt
348 // Tweaks for compiling with native SGI compilers.
350 // Revision 1.3 1999/02/26 22:07:55 curt
351 // Added initial support for native SGI compilers.
353 // Revision 1.2 1999/02/11 01:09:34 curt
354 // Added a routine to calculate the offset in bucket units between two buckets.
356 // Revision 1.1 1999/02/08 23:52:16 curt
357 // Added a new "newbucket.[ch]xx" FGBucket class to replace the old
358 // fgBUCKET struct and C routines. This FGBucket class adjusts the tile
359 // width towards the poles to ensure the tiles are at least 8 miles wide.