1 /**************************************************************************
2 * newbucket.hxx -- new bucket routines for better world modeling
4 * Written by Curtis L. Olson, started February 1999.
6 * Copyright (C) 1999 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
24 **************************************************************************/
34 #include <simgear/misc/fgpath.hxx>
36 #include "newbucket.hxx"
39 // Build the path name for this bucket
40 string FGBucket::gen_base_path() const {
42 int top_lon, top_lat, main_lon, main_lat;
48 if ( (lon < 0) && (top_lon * 10 != lon) ) {
64 if ( (lat < 0) && (top_lat * 10 != lat) ) {
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);
82 FGPath path( raw_path );
88 // find the bucket which is offset by the specified tile units in the
89 // X & Y direction. We need the current lon and lat to resolve
90 // ambiguities when going from a wider tile to a narrower one above or
91 // below. This assumes that we are feeding in
92 FGBucket fgBucketOffset( double dlon, double dlat, int dx, int dy ) {
93 FGBucket result( dlon, dlat );
94 double clat = result.get_center_lat() + dy * FG_BUCKET_SPAN;
96 // walk dy units in the lat direction
97 result.set_bucket( dlon, clat );
99 // find the lon span for the new latitude
100 double span = bucket_span( clat );
102 // walk dx units in the lon direction
103 double tmp = dlon + dx * span;
104 while ( tmp < -180.0 ) {
107 while ( tmp >= 180.0 ) {
110 result.set_bucket( tmp, clat );
116 // calculate the offset between two buckets
117 void fgBucketDiff( const FGBucket& b1, const FGBucket& b2, int *dx, int *dy ) {
119 // Latitude difference
120 double c1_lat = b1.get_center_lat();
121 double c2_lat = b2.get_center_lat();
122 double diff_lat = c2_lat - c1_lat;
125 *dy = (int)rint( diff_lat / FG_BUCKET_SPAN );
127 if ( diff_lat > 0 ) {
128 *dy = (int)( diff_lat / FG_BUCKET_SPAN + 0.5 );
130 *dy = (int)( diff_lat / FG_BUCKET_SPAN - 0.5 );
134 // longitude difference
135 double c1_lon = b1.get_center_lon();
136 double c2_lon = b2.get_center_lon();
137 double diff_lon = c2_lon - c1_lon;
139 if ( bucket_span(c1_lat) <= bucket_span(c2_lat) ) {
140 span = bucket_span(c1_lat);
142 span = bucket_span(c2_lat);
146 *dx = (int)rint( diff_lon / span );
148 if ( diff_lon > 0 ) {
149 *dx = (int)( diff_lon / span + 0.5 );
151 *dx = (int)( diff_lon / span - 0.5 );