]> git.mxchange.org Git - simgear.git/blob - simgear/bucket/newbucket.cxx
74c3ce299c970bf14dc99e69d6bb3d06894b6afd
[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 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31
32 #include <math.h>
33
34 #include <simgear/misc/fgpath.hxx>
35
36 #include "newbucket.hxx"
37
38
39 // Build the path name for this bucket
40 string FGBucket::gen_base_path() const {
41     // long int index;
42     int top_lon, top_lat, main_lon, main_lat;
43     char hem, pole;
44     char raw_path[256];
45
46     top_lon = lon / 10;
47     main_lon = lon;
48     if ( (lon < 0) && (top_lon * 10 != lon) ) {
49         top_lon -= 1;
50     }
51     top_lon *= 10;
52     if ( top_lon >= 0 ) {
53         hem = 'e';
54     } else {
55         hem = 'w';
56         top_lon *= -1;
57     }
58     if ( main_lon < 0 ) {
59         main_lon *= -1;
60     }
61     
62     top_lat = lat / 10;
63     main_lat = lat;
64     if ( (lat < 0) && (top_lat * 10 != lat) ) {
65         top_lat -= 1;
66     }
67     top_lat *= 10;
68     if ( top_lat >= 0 ) {
69         pole = 'n';
70     } else {
71         pole = 's';
72         top_lat *= -1;
73     }
74     if ( main_lat < 0 ) {
75         main_lat *= -1;
76     }
77
78     sprintf(raw_path, "%c%03d%c%02d/%c%03d%c%02d", 
79             hem, top_lon, pole, top_lat, 
80             hem, main_lon, pole, main_lat);
81
82     FGPath path( raw_path );
83
84     return path.str();
85 }
86
87
88 // return width of the tile in meters
89 double FGBucket::get_width_m() const {
90     double clat = (int)get_center_lat();
91     if ( clat > 0 ) {
92         clat = (int)clat + 0.5;
93     } else {
94         clat = (int)clat - 0.5;
95     }
96     double clat_rad = clat * DEG_TO_RAD;
97     double cos_lat = cos( clat_rad );
98     double local_radius = cos_lat * EQUATORIAL_RADIUS_M;
99     double local_perimeter = 2.0 * local_radius * FG_PI;
100     double degree_width = local_perimeter / 360.0;
101
102     return bucket_span( get_center_lat() ) * degree_width;
103 }
104
105
106 // return height of the tile in meters
107 double FGBucket::get_height_m() const {
108     double perimeter = 2.0 * EQUATORIAL_RADIUS_M * FG_PI;
109     double degree_height = perimeter / 360.0;
110
111     return FG_BUCKET_SPAN * degree_height;
112 }
113
114
115 // find the bucket which is offset by the specified tile units in the
116 // X & Y direction.  We need the current lon and lat to resolve
117 // ambiguities when going from a wider tile to a narrower one above or
118 // below.  This assumes that we are feeding in
119 FGBucket fgBucketOffset( double dlon, double dlat, int dx, int dy ) {
120     FGBucket result( dlon, dlat );
121     double clat = result.get_center_lat() + dy * FG_BUCKET_SPAN;
122
123     // walk dy units in the lat direction
124     result.set_bucket( dlon, clat );
125
126     // find the lon span for the new latitude
127     double span = bucket_span( clat );
128
129     // walk dx units in the lon direction
130     double tmp = dlon + dx * span;
131     while ( tmp < -180.0 ) {
132         tmp += 360.0;
133     }
134     while ( tmp >= 180.0 ) {
135         tmp -= 360.0;
136     }
137     result.set_bucket( tmp, clat );
138
139     return result;
140 }
141
142
143 // calculate the offset between two buckets
144 void fgBucketDiff( const FGBucket& b1, const FGBucket& b2, int *dx, int *dy ) {
145
146     // Latitude difference
147     double c1_lat = b1.get_center_lat();
148     double c2_lat = b2.get_center_lat();
149     double diff_lat = c2_lat - c1_lat;
150
151 #ifdef HAVE_RINT
152     *dy = (int)rint( diff_lat / FG_BUCKET_SPAN );
153 #else
154     if ( diff_lat > 0 ) {
155         *dy = (int)( diff_lat / FG_BUCKET_SPAN + 0.5 );
156     } else {
157         *dy = (int)( diff_lat / FG_BUCKET_SPAN - 0.5 );
158     }
159 #endif
160
161     // longitude difference
162     double c1_lon = b1.get_center_lon();
163     double c2_lon = b2.get_center_lon();
164     double diff_lon = c2_lon - c1_lon;
165     double span;
166     if ( bucket_span(c1_lat) <= bucket_span(c2_lat) ) {
167         span = bucket_span(c1_lat);
168     } else {
169         span = bucket_span(c2_lat);
170     }
171
172 #ifdef HAVE_RINT
173     *dx = (int)rint( diff_lon / span );
174 #else
175     if ( diff_lon > 0 ) {
176         *dx = (int)( diff_lon / span + 0.5 );
177     } else {
178         *dx = (int)( diff_lon / span - 0.5 );
179     }
180 #endif
181 }
182
183