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