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