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