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