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 FG_USING_STD(sprintf);
55 #if ! defined( FG_HAVE_NATIVE_SGI_COMPILERS )
56 FG_USING_STD(ostream);
61 #define SG_BUCKET_SPAN 0.125 // 1/8 of a degree
62 #define SG_HALF_BUCKET_SPAN 0.0625 // 1/2 of 1/8 of a degree = 1/16 = 0.0625
65 ostream& operator<< ( ostream&, const SGBucket& );
66 bool operator== ( const SGBucket&, const SGBucket& );
69 // return the horizontal tile span factor based on latitude
70 inline double sg_bucket_span( double l ) {
73 } else if ( l >= 88.0 ) {
75 } else if ( l >= 86.0 ) {
77 } else if ( l >= 83.0 ) {
79 } else if ( l >= 76.0 ) {
81 } else if ( l >= 62.0 ) {
83 } else if ( l >= 22.0 ) {
85 } else if ( l >= -22.0 ) {
87 } else if ( l >= -62.0 ) {
89 } else if ( l >= -76.0 ) {
91 } else if ( l >= -83.0 ) {
93 } else if ( l >= -86.0 ) {
95 } else if ( l >= -88.0 ) {
97 } else if ( l >= -89.0 ) {
108 double cx, cy; // centerpoint (lon, lat) in degrees of bucket
109 int lon; // longitude index (-180 to 179)
110 int lat; // latitude index (-90 to 89)
111 int x; // x subdivision (0 to 7)
112 int y; // y subdivision (0 to 7)
116 // default constructor
119 // constructor for specified location
120 SGBucket(const double dlon, const double dlat);
122 // create an impossible bucket if false
123 SGBucket(const bool is_good);
125 // Parse a unique scenery tile index and find the lon, lat, x, and y
126 SGBucket(const long int bindex);
128 // default destructor
131 // Set the bucket params for the specified lat and lon
132 void set_bucket( double dlon, double dlat );
133 void set_bucket( double *lonlat );
135 // create an impossible bucket
136 inline void make_bad( void ) {
137 set_bucket(0.0, 0.0);
141 // Generate the unique scenery tile index for this bucket
143 // The index is constructed as follows:
145 // 9 bits - to represent 360 degrees of longitude (-180 to 179)
146 // 8 bits - to represent 180 degrees of latitude (-90 to 89)
148 // Each 1 degree by 1 degree tile is further broken down into an 8x8
149 // grid. So we also need:
151 // 3 bits - to represent x (0 to 7)
152 // 3 bits - to represent y (0 to 7)
154 inline long int gen_index() const {
155 return ((lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x;
158 inline string gen_index_str() const {
161 (((long)lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x);
165 // Build the path name for this bucket
166 string gen_base_path() const;
168 // return the center lon of a tile
169 inline double get_center_lon() const {
170 double span = sg_bucket_span( lat + y / 8.0 + SG_HALF_BUCKET_SPAN );
173 return lon + span / 2.0;
175 return lon + x * span + span / 2.0;
179 // return the center lat of a tile
180 inline double get_center_lat() const {
181 return lat + y / 8.0 + SG_HALF_BUCKET_SPAN;
184 // return width of the tile in degrees
185 double get_width() const;
186 // return height of the tile in degrees
187 double get_height() const;
189 // return width of the tile in meters
190 double get_width_m() const;
191 // return height of the tile in meters
192 double get_height_m() const;
194 // Informational methods
195 inline int get_lon() const { return lon; }
196 inline int get_lat() const { return lat; }
197 inline int get_x() const { return x; }
198 inline int get_y() const { return y; }
201 friend ostream& operator<< ( ostream&, const SGBucket& );
202 friend bool operator== ( const SGBucket&, const SGBucket& );
206 // offset a bucket specified by dlon, dlat by the specified tile units
207 // in the X & Y direction
208 SGBucket sgBucketOffset( double dlon, double dlat, int x, int y );
211 // calculate the offset between two buckets
212 void sgBucketDiff( const SGBucket& b1, const SGBucket& b2, int *dx, int *dy );
216 operator<< ( ostream& out, const SGBucket& b )
218 return out << b.lon << ":" << b.x << ", " << b.lat << ":" << b.y;
223 operator== ( const SGBucket& b1, const SGBucket& b2 )
225 return ( b1.lon == b2.lon &&
232 #endif // _NEWBUCKET_HXX