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