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