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