]> git.mxchange.org Git - simgear.git/blob - simgear/bucket/newbucket.hxx
2ba7de551e669d4a0da76fa0f5c901cf5265b8b4
[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 - http://www.flightgear.org/~curt
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 General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21  *
22  * $Id$
23  **************************************************************************/
24
25 /** \file newbucket.hxx
26  * A class and associated utiltity functions to manage world scenery tiling.
27  */
28
29 #ifndef _NEWBUCKET_HXX
30 #define _NEWBUCKET_HXX
31
32 #include <simgear/compiler.h>
33 #include <simgear/constants.h>
34
35 #ifdef SG_HAVE_STD_INCLUDES
36 #  include <cmath>
37 #  include <cstdio> // sprintf()
38 #else
39 #  include <math.h>
40 #  include <stdio.h> // sprintf()
41 #endif
42
43 #include STL_IOSTREAM
44
45 // I don't understand ... <math.h> or <cmath> should be included
46 // already depending on how you defined SG_HAVE_STD_INCLUDES, but I
47 // can go ahead and add this -- CLO
48 #ifdef __MWERKS__
49 SG_USING_STD(sprintf);
50 SG_USING_STD(fabs);
51 #endif
52
53 #include STL_STRING
54
55 SG_USING_STD(string);
56 SG_USING_STD(ostream);
57
58
59 /**
60  * standard size of a bucket in degrees (1/8 of a degree)
61  */
62 #define SG_BUCKET_SPAN      0.125
63
64 /**
65  * half of a standard SG_BUCKET_SPAN
66  */
67 #define SG_HALF_BUCKET_SPAN ( 0.5 * SG_BUCKET_SPAN )
68
69
70 // return the horizontal tile span factor based on latitude
71 static double sg_bucket_span( double l ) {
72     if ( l >= 89.0 ) {
73         return 360.0;
74     } else if ( l >= 88.0 ) {
75         return 8.0;
76     } else if ( l >= 86.0 ) {
77         return 4.0;
78     } else if ( l >= 83.0 ) {
79         return 2.0;
80     } else if ( l >= 76.0 ) {
81         return 1.0;
82     } else if ( l >= 62.0 ) {
83         return 0.5;
84     } else if ( l >= 22.0 ) {
85         return 0.25;
86     } else if ( l >= -22.0 ) {
87         return 0.125;
88     } else if ( l >= -62.0 ) {
89         return 0.25;
90     } else if ( l >= -76.0 ) {
91         return 0.5;
92     } else if ( l >= -83.0 ) {
93         return 1.0;
94     } else if ( l >= -86.0 ) {
95         return 2.0;
96     } else if ( l >= -88.0 ) {
97         return 4.0;
98     } else if ( l >= -89.0 ) {
99         return 8.0;
100     } else {
101         return 360.0;
102     }
103 }
104
105
106 /**
107  * A class to manage world scenery tiling.
108  * This class encapsulates the world tiling scheme.  It provides ways
109  * to calculate a unique tile index from a lat/lon, and it can provide
110  * information such as the dimensions of a given tile.
111  */
112
113 class SGBucket {
114
115 private:
116     double cx, cy;  // centerpoint (lon, lat) in degrees of bucket
117     short lon;        // longitude index (-180 to 179)
118     short lat;        // latitude index (-90 to 89)
119     char x;          // x subdivision (0 to 7)
120     char y;          // y subdivision (0 to 7)
121
122 public:
123
124     /**
125      * Default constructor.
126      */
127     SGBucket();
128
129     /**
130      * Construct a bucket given a specific location.
131      * @param dlon longitude specified in degrees
132      * @param dlat latitude specified in degrees
133      */
134     SGBucket(const double dlon, const double dlat);
135
136     /** Construct a bucket.
137      *  @param is_good if false, create an invalid bucket.  This is
138      *  useful * if you are comparing cur_bucket to last_bucket and
139      *  you want to * make sure last_bucket starts out as something
140      *  impossible.
141      */
142     SGBucket(const bool is_good);
143
144     /** Construct a bucket given a unique bucket index number.
145      * @param bindex unique bucket index
146      */
147     SGBucket(const long int bindex);
148
149     /**
150      * Default destructor.
151      */
152     ~SGBucket();
153
154     /**
155      * Reset a bucket to represent a new lat and lon
156      * @param dlon longitude specified in degrees
157      * @param dlat latitude specified in degrees
158      */
159     void set_bucket( double dlon, double dlat );
160
161     /**
162      * Reset a bucket to represent a new lat and lon
163      * @param lonlat an array of double[2] holding lon and lat
164      * (specified) in degrees
165      */
166     void set_bucket( double *lonlat );
167
168     /**
169      * Create an impossible bucket.
170      * This is useful if you are comparing cur_bucket to last_bucket
171      * and you want to make sure last_bucket starts out as something
172      * impossible.
173      */
174     inline void make_bad() {
175         set_bucket(0.0, 0.0);
176         lon = -1000;
177     }
178
179     /**
180      * Generate the unique scenery tile index for this bucket
181      *
182      * The index is constructed as follows:
183      * 
184      * 9 bits - to represent 360 degrees of longitude (-180 to 179)
185      * 8 bits - to represent 180 degrees of latitude (-90 to 89)
186      *
187      * Each 1 degree by 1 degree tile is further broken down into an 8x8
188      * grid.  So we also need:
189      *
190      * 3 bits - to represent x (0 to 7)
191      * 3 bits - to represent y (0 to 7)
192      * @return tile index
193      */
194     inline long int gen_index() const {
195         return ((lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x;
196     }
197
198     /**
199      * Generate the unique scenery tile index for this bucket in ascii
200      * string form.
201      * @return tile index in string form
202      */
203     inline string gen_index_str() const {
204         char tmp[20];
205         sprintf(tmp, "%ld", 
206                 (((long)lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x);
207         return (string)tmp;
208     }
209
210     /**
211      * Build the base path name for this bucket.
212      * @return base path in string form
213      */
214     string gen_base_path() const;
215
216     /**
217      * @return the center lon of a tile.
218      */
219     inline double get_center_lon() const {
220         double span = sg_bucket_span( lat + y / 8.0 + SG_HALF_BUCKET_SPAN );
221
222         if ( span >= 1.0 ) {
223             return lon + span / 2.0;
224         } else {
225             return lon + x * span + span / 2.0;
226         }
227     }
228
229     /**
230      * @return the center lat of a tile.
231      */
232     inline double get_center_lat() const {
233         return lat + y / 8.0 + SG_HALF_BUCKET_SPAN;
234     }
235
236     /**
237      * @return the width of the tile in degrees.
238      */
239     double get_width() const;
240
241     /**
242      * @return the height of the tile in degrees.
243      */
244     double get_height() const;
245
246     /**
247      * @return the width of the tile in meters.
248      */
249     double get_width_m() const; 
250
251     /**
252      * @return the height of the tile in meters.
253      */
254     double get_height_m() const;
255  
256     // Informational methods.
257
258     /**
259      * @return the lon of the lower left corner of 
260      * the 1x1 chunk containing this tile.
261      */
262     inline int get_chunk_lon() const { return lon; }
263
264     /**
265      * @return the lat of the lower left corner of 
266      * the 1x1 chunk containing this tile.
267      */
268     inline int get_chunk_lat() const { return lat; }
269
270     /**
271      * @return the x coord within the 1x1 degree chunk this tile.
272      */
273     inline int get_x() const { return x; }
274
275     /**
276      * @return the y coord within the 1x1 degree chunk this tile.
277      */
278     inline int get_y() const { return y; }
279
280     // friends
281
282     friend ostream& operator<< ( ostream&, const SGBucket& );
283     friend bool operator== ( const SGBucket&, const SGBucket& );
284 };
285
286
287 /**
288  * \relates SGBucket
289  * Return the bucket which is offset from the specified dlon, dlat by
290  * the specified tile units in the X & Y direction.
291  * @param dlon starting lon in degrees
292  * @param dlat starting lat in degrees
293  * @param x number of bucket units to offset in x (lon) direction
294  * @param y number of bucket units to offset in y (lat) direction
295  * @return offset bucket
296  */
297 SGBucket sgBucketOffset( double dlon, double dlat, int x, int y );
298
299
300 /**
301  * \relates SGBucket
302  * Calculate the offset between two buckets (in quantity of buckets).
303  * @param b1 bucket 1
304  * @param b2 bucket 2
305  * @param dx offset distance (lon) in tile units
306  * @param dy offset distance (lat) in tile units
307  */
308 void sgBucketDiff( const SGBucket& b1, const SGBucket& b2, int *dx, int *dy );
309
310
311 /**
312  * Write the bucket lon, lat, x, and y to the output stream.
313  * @param out output stream
314  * @param b bucket
315  */
316 inline ostream&
317 operator<< ( ostream& out, const SGBucket& b )
318 {
319     return out << b.lon << ":" << b.x << ", " << b.lat << ":" << b.y;
320 }
321
322
323 /**
324  * Compare two bucket structures for equality.
325  * @param b1 bucket 1
326  * @param b2 bucket 2
327  * @return comparison result
328  */
329 inline bool
330 operator== ( const SGBucket& b1, const SGBucket& b2 )
331 {
332     return ( b1.lon == b2.lon &&
333              b1.lat == b2.lat &&
334              b1.x == b2.x &&
335              b1.y == b2.y );
336 }
337
338
339 #endif // _NEWBUCKET_HXX
340
341