]> git.mxchange.org Git - simgear.git/blob - simgear/bucket/newbucket.cxx
9a913628cbf39a777f3fab541cdff8e25c29cfe4
[simgear.git] / simgear / bucket / newbucket.cxx
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 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
27 #ifdef HAVE_CONFIG_H
28 #  include <simgear_config.h>
29 #endif
30
31 #include <math.h>
32
33 #include <simgear/misc/sg_path.hxx>
34
35 #include "newbucket.hxx"
36
37
38 // default constructor
39 SGBucket::SGBucket() {
40 }
41
42
43 // constructor for specified location
44 SGBucket::SGBucket(const double dlon, const double dlat) {
45     set_bucket(dlon, dlat);
46 }
47
48
49 // create an impossible bucket if false
50 SGBucket::SGBucket(const bool is_good) {
51     set_bucket(0.0, 0.0);
52     if ( !is_good ) {
53         lon = -1000;
54     }
55 }
56
57
58 // Parse a unique scenery tile index and find the lon, lat, x, and y
59 SGBucket::SGBucket(const long int bindex) {
60     long int index = bindex;
61         
62     lon = index >> 14;
63     index -= lon << 14;
64     lon -= 180;
65
66     lat = index >> 6;
67     index -= lat << 6;
68     lat -= 90;
69
70     y = index >> 3;
71     index -= y << 3;
72
73     x = index;
74 }
75
76
77 // default destructor
78 SGBucket::~SGBucket() {
79 }
80
81
82 // Set the bucket params for the specified lat and lon
83 void SGBucket::set_bucket( double *lonlat ) {
84     set_bucket( lonlat[0], lonlat[1] );
85 }       
86
87
88 // Set the bucket params for the specified lat and lon
89 void SGBucket::set_bucket( double dlon, double dlat ) {
90     //
91     // latitude first
92     //
93     double span = sg_bucket_span( dlat );
94     double diff = dlon - (double)(int)dlon;
95
96     // cout << "diff = " << diff << "  span = " << span << endl;
97
98     if ( (dlon >= 0) || (fabs(diff) < SG_EPSILON) ) {
99         lon = (int)dlon;
100     } else {
101         lon = (int)dlon - 1;
102     }
103
104     // find subdivision or super lon if needed
105     if ( span < SG_EPSILON ) {
106         // polar cap
107         lon = 0;
108         x = 0;
109     } else if ( span <= 1.0 ) {
110         x = (int)((dlon - lon) / span);
111     } else {
112         if ( (dlon >= 0) || (fabs(diff) < SG_EPSILON) ) {
113             lon = (int)( (int)(lon / span) * span);
114         } else {
115             // cout << " lon = " << lon 
116             //  << "  tmp = " << (int)((lon-1) / span) << endl;
117             lon = (int)( (int)((lon + 1) / span) * span - span);
118             if ( lon < -180 ) {
119                 lon = -180;
120             }
121         }
122         x = 0;
123     }
124
125     //
126     // then latitude
127     //
128     diff = dlat - (double)(int)dlat;
129
130     if ( (dlat >= 0) || (fabs(diff) < SG_EPSILON) ) {
131         lat = (int)dlat;
132     } else {
133         lat = (int)dlat - 1;
134     }
135     y = (int)((dlat - lat) * 8);
136 }
137
138
139 // Build the path name for this bucket
140 string SGBucket::gen_base_path() const {
141     // long int index;
142     int top_lon, top_lat, main_lon, main_lat;
143     char hem, pole;
144     char raw_path[256];
145
146     top_lon = lon / 10;
147     main_lon = lon;
148     if ( (lon < 0) && (top_lon * 10 != lon) ) {
149         top_lon -= 1;
150     }
151     top_lon *= 10;
152     if ( top_lon >= 0 ) {
153         hem = 'e';
154     } else {
155         hem = 'w';
156         top_lon *= -1;
157     }
158     if ( main_lon < 0 ) {
159         main_lon *= -1;
160     }
161     
162     top_lat = lat / 10;
163     main_lat = lat;
164     if ( (lat < 0) && (top_lat * 10 != lat) ) {
165         top_lat -= 1;
166     }
167     top_lat *= 10;
168     if ( top_lat >= 0 ) {
169         pole = 'n';
170     } else {
171         pole = 's';
172         top_lat *= -1;
173     }
174     if ( main_lat < 0 ) {
175         main_lat *= -1;
176     }
177
178     sprintf(raw_path, "%c%03d%c%02d/%c%03d%c%02d", 
179             hem, top_lon, pole, top_lat, 
180             hem, main_lon, pole, main_lat);
181
182     SGPath path( raw_path );
183
184     return path.str();
185 }
186
187
188 // return width of the tile in degrees
189 double SGBucket::get_width() const {
190     return sg_bucket_span( get_center_lat() );
191 }
192
193
194 // return height of the tile in degrees
195 double SGBucket::get_height() const {
196     return SG_BUCKET_SPAN;
197 }
198
199
200 // return width of the tile in meters
201 double SGBucket::get_width_m() const {
202     double clat = (int)get_center_lat();
203     if ( clat > 0 ) {
204         clat = (int)clat + 0.5;
205     } else {
206         clat = (int)clat - 0.5;
207     }
208     double clat_rad = clat * SGD_DEGREES_TO_RADIANS;
209     double cos_lat = cos( clat_rad );
210     double local_radius = cos_lat * SG_EQUATORIAL_RADIUS_M;
211     double local_perimeter = local_radius * SGD_2PI;
212     double degree_width = local_perimeter / 360.0;
213
214     return sg_bucket_span( get_center_lat() ) * degree_width;
215 }
216
217
218 // return height of the tile in meters
219 double SGBucket::get_height_m() const {
220     double perimeter = SG_EQUATORIAL_RADIUS_M * SGD_2PI;
221     double degree_height = perimeter / 360.0;
222
223     return SG_BUCKET_SPAN * degree_height;
224 }
225
226
227 // find the bucket which is offset by the specified tile units in the
228 // X & Y direction.  We need the current lon and lat to resolve
229 // ambiguities when going from a wider tile to a narrower one above or
230 // below.  This assumes that we are feeding in
231 SGBucket sgBucketOffset( double dlon, double dlat, int dx, int dy ) {
232     SGBucket result( dlon, dlat );
233     double clat = result.get_center_lat() + dy * SG_BUCKET_SPAN;
234
235     // walk dy units in the lat direction
236     result.set_bucket( dlon, clat );
237
238     // find the lon span for the new latitude
239     double span = sg_bucket_span( clat );
240
241     // walk dx units in the lon direction
242     double tmp = dlon + dx * span;
243     while ( tmp < -180.0 ) {
244         tmp += 360.0;
245     }
246     while ( tmp >= 180.0 ) {
247         tmp -= 360.0;
248     }
249     result.set_bucket( tmp, clat );
250
251     return result;
252 }
253
254
255 // calculate the offset between two buckets
256 void sgBucketDiff( const SGBucket& b1, const SGBucket& b2, int *dx, int *dy ) {
257
258     // Latitude difference
259     double c1_lat = b1.get_center_lat();
260     double c2_lat = b2.get_center_lat();
261     double diff_lat = c2_lat - c1_lat;
262
263 #ifdef HAVE_RINT
264     *dy = (int)rint( diff_lat / SG_BUCKET_SPAN );
265 #else
266     if ( diff_lat > 0 ) {
267         *dy = (int)( diff_lat / SG_BUCKET_SPAN + 0.5 );
268     } else {
269         *dy = (int)( diff_lat / SG_BUCKET_SPAN - 0.5 );
270     }
271 #endif
272
273     // longitude difference
274     double diff_lon=0.0;
275     double span=0.0;
276
277     SGBucket tmp_bucket;
278     // To handle crossing the bucket size boundary
279     //  we need to account for different size buckets.
280
281     if ( sg_bucket_span(c1_lat) <= sg_bucket_span(c2_lat) )
282     {
283         span = sg_bucket_span(c1_lat);
284     } else {
285         span = sg_bucket_span(c2_lat);
286     }
287
288     diff_lon = b2.get_center_lon() - b1.get_center_lon();
289
290     if (diff_lon <0.0)
291     {
292        diff_lon -= b1.get_width()*0.5 + b2.get_width()*0.5 - span;
293     } 
294     else
295     {
296        diff_lon += b1.get_width()*0.5 + b2.get_width()*0.5 - span;
297     }
298
299
300 #ifdef HAVE_RINT
301     *dx = (int)rint( diff_lon / span );
302 #else
303     if ( diff_lon > 0 ) {
304         *dx = (int)( diff_lon / span + 0.5 );
305     } else {
306         *dx = (int)( diff_lon / span - 0.5 );
307     }
308 #endif
309 }
310
311