]> git.mxchange.org Git - simgear.git/blob - simgear/bucket/newbucket.hxx
Added simgear/magvar which impliments WMM 2000 world magnetic variance model.
[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 make_bad ( void );
94
95     // Generate the unique scenery tile index for this bucket
96     long int gen_index();
97     string gen_index_str() const;
98
99     // Build the path name for this bucket
100     string gen_base_path() const;
101
102     // return the center lon of a tile
103     double get_center_lon() const;
104
105     // return width of the tile
106     double get_width() const;
107
108     // return the center lat of a tile
109     double get_center_lat() const;
110
111     // return height of the tile
112     double get_height() const;
113
114     // Informational methods
115     inline int get_lon() const { return lon; }
116     inline int get_lat() const { return lat; }
117     inline int get_x() const { return x; }
118     inline int get_y() const { return y; }
119
120     // friends
121     friend ostream& operator<< ( ostream&, const FGBucket& );
122     friend bool operator== ( const FGBucket&, const FGBucket& );
123 };
124
125
126 // return the horizontal tile span factor based on latitude
127 inline double bucket_span( double l ) {
128     if ( l >= 89.0 ) {
129         return 360.0;
130     } else if ( l >= 88.0 ) {
131         return 8.0;
132     } else if ( l >= 86.0 ) {
133         return 4.0;
134     } else if ( l >= 83.0 ) {
135         return 2.0;
136     } else if ( l >= 76.0 ) {
137         return 1.0;
138     } else if ( l >= 62.0 ) {
139         return 0.5;
140     } else if ( l >= 22.0 ) {
141         return 0.25;
142     } else if ( l >= -22.0 ) {
143         return 0.125;
144     } else if ( l >= -62.0 ) {
145         return 0.25;
146     } else if ( l >= -76.0 ) {
147         return 0.5;
148     } else if ( l >= -83.0 ) {
149         return 1.0;
150     } else if ( l >= -86.0 ) {
151         return 2.0;
152     } else if ( l >= -88.0 ) {
153         return 4.0;
154     } else if ( l >= -89.0 ) {
155         return 8.0;
156     } else {
157         return 360.0;
158     }
159 }
160
161
162 // Set the bucket params for the specified lat and lon
163 inline void FGBucket::set_bucket( double dlon, double dlat ) {
164     //
165     // latitude first
166     //
167     double span = bucket_span( dlat );
168     double diff = dlon - (double)(int)dlon;
169
170     // cout << "diff = " << diff << "  span = " << span << endl;
171
172     if ( (dlon >= 0) || (fabs(diff) < FG_EPSILON) ) {
173         lon = (int)dlon;
174     } else {
175         lon = (int)dlon - 1;
176     }
177
178     // find subdivision or super lon if needed
179     if ( span < FG_EPSILON ) {
180         // polar cap
181         lon = 0;
182         x = 0;
183     } else if ( span <= 1.0 ) {
184         x = (int)((dlon - lon) / span);
185     } else {
186         if ( (dlon >= 0) || (fabs(diff) < FG_EPSILON) ) {
187             lon = (int)( (int)(lon / span) * span);
188         } else {
189             // cout << " lon = " << lon 
190             //  << "  tmp = " << (int)((lon-1) / span) << endl;
191             lon = (int)( (int)((lon + 1) / span) * span - span);
192             if ( lon < -180 ) {
193                 lon = -180;
194             }
195         }
196         x = 0;
197     }
198
199     //
200     // then latitude
201     //
202     diff = dlat - (double)(int)dlat;
203
204     if ( (dlat >= 0) || (fabs(diff) < FG_EPSILON) ) {
205         lat = (int)dlat;
206     } else {
207         lat = (int)dlat - 1;
208     }
209     y = (int)((dlat - lat) * 8);
210 }
211
212
213 // default constructor
214 inline FGBucket::FGBucket() {}
215
216
217 // constructor for specified location
218 inline FGBucket::FGBucket(const double dlon, const double dlat) {
219     set_bucket(dlon, dlat);
220 }
221
222
223 // create an impossible bucket if false
224 inline FGBucket::FGBucket(const bool is_good) {
225     set_bucket(0.0, 0.0);
226     if ( !is_good ) {
227         lon = -1000;
228     }
229 }
230     
231
232 // Parse a unique scenery tile index and find the lon, lat, x, and y
233 inline FGBucket::FGBucket(const long int bindex) {
234     long int index = bindex;
235
236     lon = index >> 14;
237     index -= lon << 14;
238     lon -= 180;
239     
240     lat = index >> 6;
241     index -= lat << 6;
242     lat -= 90;
243     
244     y = index >> 3;
245     index -= y << 3;
246
247     x = index;
248 }
249
250
251 // default destructor
252 inline FGBucket::~FGBucket() {}
253
254
255 // Generate the unique scenery tile index for this bucket
256 // 
257 // The index is constructed as follows:
258 // 
259 // 9 bits - to represent 360 degrees of longitude (-180 to 179)
260 // 8 bits - to represent 180 degrees of latitude (-90 to 89)
261 //
262 // Each 1 degree by 1 degree tile is further broken down into an 8x8
263 // grid.  So we also need:
264 //
265 // 3 bits - to represent x (0 to 7)
266 // 3 bits - to represent y (0 to 7)
267
268 inline long int FGBucket::gen_index() {
269     return ((lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x;
270 }
271
272 inline string FGBucket::gen_index_str() const {
273     char tmp[20];
274     sprintf(tmp, "%ld", 
275             (((long)lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x);
276     return (string)tmp;
277 }
278
279
280 // return the center lon of a tile
281 inline double FGBucket::get_center_lon() const {
282     double span = bucket_span( lat + y / 8.0 + FG_HALF_BUCKET_SPAN );
283
284     if ( span >= 1.0 ) {
285         return lon + span / 2.0;
286     } else {
287         return lon + x * span + span / 2.0;
288     }
289 }
290
291
292 // return the center lat of a tile
293 inline double FGBucket::get_center_lat() const {
294     return lat + y / 8.0 + FG_HALF_BUCKET_SPAN;
295 }
296
297
298 // return width of the tile
299 inline double FGBucket::get_width() const {
300     return bucket_span( get_center_lat() );
301 }
302
303
304 // return height of the tile
305 inline double FGBucket::get_height() const {
306     return FG_BUCKET_SPAN;
307 }
308
309
310 // create an impossible bucket
311 inline void FGBucket::make_bad( void ) {
312     set_bucket(0.0, 0.0);
313         lon = -1000;
314 }
315     
316
317 // offset a bucket struct by the specified tile units in the X & Y
318 // direction
319 FGBucket fgBucketOffset( double dlon, double dlat, int x, int y );
320
321
322 // calculate the offset between two buckets
323 void fgBucketDiff( const FGBucket& b1, const FGBucket& b2, int *dx, int *dy );
324
325
326 /*
327 // Given a lat/lon, fill in the local tile index array
328 void fgBucketGenIdxArray(fgBUCKET *p1, fgBUCKET *tiles, int width, int height);
329 */
330
331
332 inline ostream&
333 operator<< ( ostream& out, const FGBucket& b )
334 {
335     return out << b.lon << ":" << b.x << ", " << b.lat << ":" << b.y;
336 }
337
338
339 inline bool
340 operator== ( const FGBucket& b1, const FGBucket& b2 )
341 {
342     return ( b1.lon == b2.lon &&
343              b1.lat == b2.lat &&
344              b1.x == b2.x &&
345              b1.y == b2.y );
346 }
347
348
349 #endif // _NEWBUCKET_HXX
350
351