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