]> git.mxchange.org Git - simgear.git/blob - simgear/bucket/newbucket.hxx
MacOS tweaks contributed by Darrell Walisser.
[simgear.git] / simgear / bucket / newbucket.hxx
1 /**************************************************************************
2  * newbucket.hxx -- new bucket routines for better world modeling
3  *
4  * Written by Curtis L. Olson, started February 1999.
5  *
6  * Copyright (C) 1999  Curtis L. Olson - curt@flightgear.org
7  *
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.
12  *
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.
17  *
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.
22  *
23  * $Id$
24  **************************************************************************/
25
26
27 #ifndef _NEWBUCKET_HXX
28 #define _NEWBUCKET_HXX
29
30 #include <simgear/compiler.h>
31 #include <simgear/constants.h>
32
33 #ifdef FG_HAVE_STD_INCLUDES
34 #  include <cmath>
35 #  include <cstdio> // sprintf()
36 #  include <iostream>
37 #else
38 #  include <math.h>
39 #  include <stdio.h> // sprintf()
40 #  include <iostream.h>
41 #endif
42
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
46 #ifdef __MWERKS__
47 FG_USING_STD(sprintf);
48 FG_USING_STD(fabs);
49 #endif
50
51 #include STL_STRING
52
53 FG_USING_STD(string);
54
55 #if ! defined( FG_HAVE_NATIVE_SGI_COMPILERS )
56 FG_USING_STD(ostream);
57 #endif
58
59
60
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
63
64 class SGBucket;
65 ostream& operator<< ( ostream&, const SGBucket& );
66 bool operator== ( const SGBucket&, const SGBucket& );
67
68
69 // return the horizontal tile span factor based on latitude
70 inline double sg_bucket_span( double l ) {
71     if ( l >= 89.0 ) {
72         return 360.0;
73     } else if ( l >= 88.0 ) {
74         return 8.0;
75     } else if ( l >= 86.0 ) {
76         return 4.0;
77     } else if ( l >= 83.0 ) {
78         return 2.0;
79     } else if ( l >= 76.0 ) {
80         return 1.0;
81     } else if ( l >= 62.0 ) {
82         return 0.5;
83     } else if ( l >= 22.0 ) {
84         return 0.25;
85     } else if ( l >= -22.0 ) {
86         return 0.125;
87     } else if ( l >= -62.0 ) {
88         return 0.25;
89     } else if ( l >= -76.0 ) {
90         return 0.5;
91     } else if ( l >= -83.0 ) {
92         return 1.0;
93     } else if ( l >= -86.0 ) {
94         return 2.0;
95     } else if ( l >= -88.0 ) {
96         return 4.0;
97     } else if ( l >= -89.0 ) {
98         return 8.0;
99     } else {
100         return 360.0;
101     }
102 }
103
104
105 class SGBucket {
106
107 private:
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)
113
114 public:
115
116     // default constructor
117     SGBucket();
118
119     // constructor for specified location
120     SGBucket(const double dlon, const double dlat);
121
122     // create an impossible bucket if false
123     SGBucket(const bool is_good);
124
125     // Parse a unique scenery tile index and find the lon, lat, x, and y
126     SGBucket(const long int bindex);
127
128     // default destructor
129     ~SGBucket();
130
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 );
134
135     // create an impossible bucket
136     inline void SGBucket::make_bad( void ) {
137         set_bucket(0.0, 0.0);
138         lon = -1000;
139     }
140
141     // Generate the unique scenery tile index for this bucket
142     // 
143     // The index is constructed as follows:
144     // 
145     // 9 bits - to represent 360 degrees of longitude (-180 to 179)
146     // 8 bits - to represent 180 degrees of latitude (-90 to 89)
147     //
148     // Each 1 degree by 1 degree tile is further broken down into an 8x8
149     // grid.  So we also need:
150     //
151     // 3 bits - to represent x (0 to 7)
152     // 3 bits - to represent y (0 to 7)
153
154     inline long int gen_index() const {
155         return ((lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x;
156     }
157
158     inline string gen_index_str() const {
159         char tmp[20];
160         sprintf(tmp, "%ld", 
161                 (((long)lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x);
162         return (string)tmp;
163     }
164
165     // Build the path name for this bucket
166     string gen_base_path() const;
167
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 );
171
172         if ( span >= 1.0 ) {
173             return lon + span / 2.0;
174         } else {
175             return lon + x * span + span / 2.0;
176         }
177     }
178
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;
182     }
183
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;
188
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;
193  
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; }
199
200     // friends
201     friend ostream& operator<< ( ostream&, const SGBucket& );
202     friend bool operator== ( const SGBucket&, const SGBucket& );
203 };
204
205
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 );
209
210
211 // calculate the offset between two buckets
212 void sgBucketDiff( const SGBucket& b1, const SGBucket& b2, int *dx, int *dy );
213
214
215 inline ostream&
216 operator<< ( ostream& out, const SGBucket& b )
217 {
218     return out << b.lon << ":" << b.x << ", " << b.lat << ":" << b.y;
219 }
220
221
222 inline bool
223 operator== ( const SGBucket& b1, const SGBucket& b2 )
224 {
225     return ( b1.lon == b2.lon &&
226              b1.lat == b2.lat &&
227              b1.x == b2.x &&
228              b1.y == b2.y );
229 }
230
231
232 #endif // _NEWBUCKET_HXX
233
234