]> git.mxchange.org Git - simgear.git/blob - simgear/bucket/newbucket.hxx
Updates to remove unneeded dependencies on FlightGear and SimGear.
[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 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.
12  *
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.
17  *
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.
21  *
22  * $Id$
23  **************************************************************************/
24
25
26 #ifndef _NEWBUCKET_HXX
27 #define _NEWBUCKET_HXX
28
29 #include <simgear/compiler.h>
30 #include <simgear/constants.h>
31
32 #ifdef FG_HAVE_STD_INCLUDES
33 #  include <cmath>
34 #  include <cstdio> // sprintf()
35 #  include <iostream>
36 #else
37 #  include <math.h>
38 #  include <stdio.h> // sprintf()
39 #  include <iostream.h>
40 #endif
41
42 // I don't understand ... <math.h> or <cmath> should be included
43 // already depending on how you defined FG_HAVE_STD_INCLUDES, but I
44 // can go ahead and add this -- CLO
45 #ifdef __MWERKS__
46 #  include <math.h> // needed fabs()
47 #endif
48
49 #include STL_STRING
50
51 FG_USING_STD(string);
52
53 #if ! defined( FG_HAVE_NATIVE_SGI_COMPILERS )
54 FG_USING_STD(ostream);
55 #endif
56
57
58
59 #define FG_BUCKET_SPAN      0.125   // 1/8 of a degree
60 #define FG_HALF_BUCKET_SPAN 0.0625  // 1/2 of 1/8 of a degree = 1/16 = 0.0625
61
62 class FGBucket;
63 ostream& operator<< ( ostream&, const FGBucket& );
64 bool operator== ( const FGBucket&, const FGBucket& );
65
66 class FGBucket {
67
68 private:
69     double cx, cy;  // centerpoint (lon, lat) in degrees of bucket
70     int lon;        // longitude index (-180 to 179)
71     int lat;        // latitude index (-90 to 89)
72     int x;          // x subdivision (0 to 7)
73     int y;          // y subdivision (0 to 7)
74
75 public:
76
77     // default constructor
78     FGBucket();
79
80     // create a bucket which would contain the specified lon/lat
81     FGBucket(const double lon, const double lat);
82
83     // create a bucket based on "long int" index
84     FGBucket(const long int bindex);
85
86     // create an impossible bucket if false
87     FGBucket(const bool is_good);
88
89     ~FGBucket();
90
91     // Set the bucket params for the specified lat and lon
92     void set_bucket( double dlon, double dlat );
93     void set_bucket( double *lonlat ) {
94         set_bucket( lonlat[0], lonlat[1] );
95     }   
96
97     void make_bad ( void );
98
99     // Generate the unique scenery tile index for this bucket
100     long int gen_index();
101     string gen_index_str() const;
102
103     // Build the path name for this bucket
104     string gen_base_path() const;
105
106     // return the center lon of a tile
107     double get_center_lon() const;
108
109     // return width of the tile
110     double get_width() const;
111
112     // return the center lat of a tile
113     double get_center_lat() const;
114
115     // return height of the tile
116     double get_height() const;
117
118     // Informational methods
119     inline int get_lon() const { return lon; }
120     inline int get_lat() const { return lat; }
121     inline int get_x() const { return x; }
122     inline int get_y() const { return y; }
123
124     // friends
125     friend ostream& operator<< ( ostream&, const FGBucket& );
126     friend bool operator== ( const FGBucket&, const FGBucket& );
127 };
128
129
130 // return the horizontal tile span factor based on latitude
131 inline double bucket_span( double l ) {
132     if ( l >= 89.0 ) {
133         return 360.0;
134     } else if ( l >= 88.0 ) {
135         return 8.0;
136     } else if ( l >= 86.0 ) {
137         return 4.0;
138     } else if ( l >= 83.0 ) {
139         return 2.0;
140     } else if ( l >= 76.0 ) {
141         return 1.0;
142     } else if ( l >= 62.0 ) {
143         return 0.5;
144     } else if ( l >= 22.0 ) {
145         return 0.25;
146     } else if ( l >= -22.0 ) {
147         return 0.125;
148     } else if ( l >= -62.0 ) {
149         return 0.25;
150     } else if ( l >= -76.0 ) {
151         return 0.5;
152     } else if ( l >= -83.0 ) {
153         return 1.0;
154     } else if ( l >= -86.0 ) {
155         return 2.0;
156     } else if ( l >= -88.0 ) {
157         return 4.0;
158     } else if ( l >= -89.0 ) {
159         return 8.0;
160     } else {
161         return 360.0;
162     }
163 }
164
165
166 // Set the bucket params for the specified lat and lon
167 inline void FGBucket::set_bucket( double dlon, double dlat ) {
168     //
169     // latitude first
170     //
171     double span = bucket_span( dlat );
172     double diff = dlon - (double)(int)dlon;
173
174     // cout << "diff = " << diff << "  span = " << span << endl;
175
176     if ( (dlon >= 0) || (fabs(diff) < FG_EPSILON) ) {
177         lon = (int)dlon;
178     } else {
179         lon = (int)dlon - 1;
180     }
181
182     // find subdivision or super lon if needed
183     if ( span < FG_EPSILON ) {
184         // polar cap
185         lon = 0;
186         x = 0;
187     } else if ( span <= 1.0 ) {
188         x = (int)((dlon - lon) / span);
189     } else {
190         if ( (dlon >= 0) || (fabs(diff) < FG_EPSILON) ) {
191             lon = (int)( (int)(lon / span) * span);
192         } else {
193             // cout << " lon = " << lon 
194             //  << "  tmp = " << (int)((lon-1) / span) << endl;
195             lon = (int)( (int)((lon + 1) / span) * span - span);
196             if ( lon < -180 ) {
197                 lon = -180;
198             }
199         }
200         x = 0;
201     }
202
203     //
204     // then latitude
205     //
206     diff = dlat - (double)(int)dlat;
207
208     if ( (dlat >= 0) || (fabs(diff) < FG_EPSILON) ) {
209         lat = (int)dlat;
210     } else {
211         lat = (int)dlat - 1;
212     }
213     y = (int)((dlat - lat) * 8);
214 }
215
216
217 // default constructor
218 inline FGBucket::FGBucket() {}
219
220
221 // constructor for specified location
222 inline FGBucket::FGBucket(const double dlon, const double dlat) {
223     set_bucket(dlon, dlat);
224 }
225
226
227 // create an impossible bucket if false
228 inline FGBucket::FGBucket(const bool is_good) {
229     set_bucket(0.0, 0.0);
230     if ( !is_good ) {
231         lon = -1000;
232     }
233 }
234
235
236 // Parse a unique scenery tile index and find the lon, lat, x, and y
237 inline FGBucket::FGBucket(const long int bindex) {
238     long int index = bindex;
239
240     lon = index >> 14;
241     index -= lon << 14;
242     lon -= 180;
243
244     lat = index >> 6;
245     index -= lat << 6;
246     lat -= 90;
247
248     y = index >> 3;
249     index -= y << 3;
250
251     x = index;
252 }
253
254
255 // default destructor
256 inline FGBucket::~FGBucket() {}
257
258
259 // Generate the unique scenery tile index for this bucket
260 // 
261 // The index is constructed as follows:
262 // 
263 // 9 bits - to represent 360 degrees of longitude (-180 to 179)
264 // 8 bits - to represent 180 degrees of latitude (-90 to 89)
265 //
266 // Each 1 degree by 1 degree tile is further broken down into an 8x8
267 // grid.  So we also need:
268 //
269 // 3 bits - to represent x (0 to 7)
270 // 3 bits - to represent y (0 to 7)
271
272 inline long int FGBucket::gen_index() {
273     return ((lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x;
274 }
275
276 inline string FGBucket::gen_index_str() const {
277     char tmp[20];
278     sprintf(tmp, "%ld", 
279             (((long)lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x);
280     return (string)tmp;
281 }
282
283
284 // return the center lon of a tile
285 inline double FGBucket::get_center_lon() const {
286     double span = bucket_span( lat + y / 8.0 + FG_HALF_BUCKET_SPAN );
287
288     if ( span >= 1.0 ) {
289         return lon + span / 2.0;
290     } else {
291         return lon + x * span + span / 2.0;
292     }
293 }
294
295
296 // return the center lat of a tile
297 inline double FGBucket::get_center_lat() const {
298     return lat + y / 8.0 + FG_HALF_BUCKET_SPAN;
299 }
300
301
302 // return width of the tile
303 inline double FGBucket::get_width() const {
304     return bucket_span( get_center_lat() );
305 }
306
307
308 // return height of the tile
309 inline double FGBucket::get_height() const {
310     return FG_BUCKET_SPAN;
311 }
312
313
314 // create an impossible bucket
315 inline void FGBucket::make_bad( void ) {
316     set_bucket(0.0, 0.0);
317     lon = -1000;
318 }
319
320
321 // offset a bucket struct by the specified tile units in the X & Y
322 // direction
323 FGBucket fgBucketOffset( double dlon, double dlat, int x, int y );
324
325
326 // calculate the offset between two buckets
327 void fgBucketDiff( const FGBucket& b1, const FGBucket& b2, int *dx, int *dy );
328
329
330 inline ostream&
331 operator<< ( ostream& out, const FGBucket& b )
332 {
333     return out << b.lon << ":" << b.x << ", " << b.lat << ":" << b.y;
334 }
335
336
337 inline bool
338 operator== ( const FGBucket& b1, const FGBucket& b2 )
339 {
340     return ( b1.lon == b2.lon &&
341              b1.lat == b2.lat &&
342              b1.x == b2.x &&
343              b1.y == b2.y );
344 }
345
346
347 #endif // _NEWBUCKET_HXX
348
349