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