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