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 SG_BUCKET_SPAN 0.125 // 1/8 of a degree
61 #define SG_HALF_BUCKET_SPAN 0.0625 // 1/2 of 1/8 of a degree = 1/16 = 0.0625
64 ostream& operator<< ( ostream&, const SGBucket& );
65 bool operator== ( const SGBucket&, const SGBucket& );
68 // return the horizontal tile span factor based on latitude
69 inline double sg_bucket_span( double l ) {
72 } else if ( l >= 88.0 ) {
74 } else if ( l >= 86.0 ) {
76 } else if ( l >= 83.0 ) {
78 } else if ( l >= 76.0 ) {
80 } else if ( l >= 62.0 ) {
82 } else if ( l >= 22.0 ) {
84 } else if ( l >= -22.0 ) {
86 } else if ( l >= -62.0 ) {
88 } else if ( l >= -76.0 ) {
90 } else if ( l >= -83.0 ) {
92 } else if ( l >= -86.0 ) {
94 } else if ( l >= -88.0 ) {
96 } else if ( l >= -89.0 ) {
107 double cx, cy; // centerpoint (lon, lat) in degrees of bucket
108 int lon; // longitude index (-180 to 179)
109 int lat; // latitude index (-90 to 89)
110 int x; // x subdivision (0 to 7)
111 int y; // y subdivision (0 to 7)
115 // default constructor
118 // constructor for specified location
119 inline SGBucket(const double dlon, const double dlat);
121 // create an impossible bucket if false
122 inline SGBucket(const bool is_good);
124 // Parse a unique scenery tile index and find the lon, lat, x, and y
125 inline SGBucket(const long int bindex);
127 // default destructor
130 // Set the bucket params for the specified lat and lon
131 void set_bucket( double dlon, double dlat );
132 void set_bucket( double *lonlat );
134 // create an impossible bucket
135 inline void SGBucket::make_bad( void ) {
136 set_bucket(0.0, 0.0);
140 // Generate the unique scenery tile index for this bucket
142 // The index is constructed as follows:
144 // 9 bits - to represent 360 degrees of longitude (-180 to 179)
145 // 8 bits - to represent 180 degrees of latitude (-90 to 89)
147 // Each 1 degree by 1 degree tile is further broken down into an 8x8
148 // grid. So we also need:
150 // 3 bits - to represent x (0 to 7)
151 // 3 bits - to represent y (0 to 7)
153 inline long int gen_index() const {
154 return ((lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x;
157 inline string gen_index_str() const {
160 (((long)lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x);
164 // Build the path name for this bucket
165 string gen_base_path() const;
167 // return the center lon of a tile
168 inline double get_center_lon() const {
169 double span = sg_bucket_span( lat + y / 8.0 + SG_HALF_BUCKET_SPAN );
172 return lon + span / 2.0;
174 return lon + x * span + span / 2.0;
178 // return the center lat of a tile
179 inline double get_center_lat() const {
180 return lat + y / 8.0 + SG_HALF_BUCKET_SPAN;
183 // return width of the tile in degrees
184 double get_width() const;
185 // return height of the tile in degrees
186 double get_height() const;
188 // return width of the tile in meters
189 double get_width_m() const;
190 // return height of the tile in meters
191 double get_height_m() const;
193 // Informational methods
194 inline int get_lon() const { return lon; }
195 inline int get_lat() const { return lat; }
196 inline int get_x() const { return x; }
197 inline int get_y() const { return y; }
200 friend ostream& operator<< ( ostream&, const SGBucket& );
201 friend bool operator== ( const SGBucket&, const SGBucket& );
205 // offset a bucket specified by dlon, dlat by the specified tile units
206 // in the X & Y direction
207 SGBucket sgBucketOffset( double dlon, double dlat, int x, int y );
210 // calculate the offset between two buckets
211 void sgBucketDiff( const SGBucket& b1, const SGBucket& b2, int *dx, int *dy );
215 operator<< ( ostream& out, const SGBucket& b )
217 return out << b.lon << ":" << b.x << ", " << b.lat << ":" << b.y;
222 operator== ( const SGBucket& b1, const SGBucket& b2 )
224 return ( b1.lon == b2.lon &&
231 #endif // _NEWBUCKET_HXX