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