]> git.mxchange.org Git - simgear.git/blob - simgear/bucket/newbucket.cxx
Added simgear/magvar which impliments WMM 2000 world magnetic variance model.
[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 program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  * $Id$
23  **************************************************************************/
24
25
26 #ifdef HAVE_CONFIG_H
27 #  include <config.h>
28 #endif
29
30
31 #include <math.h>
32
33 #include <simgear/misc/fgpath.hxx>
34
35 #include "newbucket.hxx"
36
37
38 // Build the path name for this bucket
39 string FGBucket::gen_base_path() const {
40     // long int index;
41     int top_lon, top_lat, main_lon, main_lat;
42     char hem, pole;
43     char raw_path[256];
44
45     top_lon = lon / 10;
46     main_lon = lon;
47     if ( (lon < 0) && (top_lon * 10 != lon) ) {
48         top_lon -= 1;
49     }
50     top_lon *= 10;
51     if ( top_lon >= 0 ) {
52         hem = 'e';
53     } else {
54         hem = 'w';
55         top_lon *= -1;
56     }
57     if ( main_lon < 0 ) {
58         main_lon *= -1;
59     }
60     
61     top_lat = lat / 10;
62     main_lat = lat;
63     if ( (lat < 0) && (top_lat * 10 != lat) ) {
64         top_lat -= 1;
65     }
66     top_lat *= 10;
67     if ( top_lat >= 0 ) {
68         pole = 'n';
69     } else {
70         pole = 's';
71         top_lat *= -1;
72     }
73     if ( main_lat < 0 ) {
74         main_lat *= -1;
75     }
76
77     sprintf(raw_path, "%c%03d%c%02d/%c%03d%c%02d", 
78             hem, top_lon, pole, top_lat, 
79             hem, main_lon, pole, main_lat);
80
81     FGPath path( raw_path );
82
83     return path.str();
84 }
85
86
87 // find the bucket which is offset by the specified tile units in the
88 // X & Y direction.  We need the current lon and lat to resolve
89 // ambiguities when going from a wider tile to a narrower one above or
90 // below.  This assumes that we are feeding in
91 FGBucket fgBucketOffset( double dlon, double dlat, int dx, int dy ) {
92     FGBucket result( dlon, dlat );
93     double clat = result.get_center_lat() + dy * FG_BUCKET_SPAN;
94
95     // walk dy units in the lat direction
96     result.set_bucket( dlon, clat );
97
98     // find the lon span for the new latitude
99     double span = bucket_span( clat );
100
101     // walk dx units in the lon direction
102     double tmp = dlon + dx * span;
103     while ( tmp < -180.0 ) {
104         tmp += 360.0;
105     }
106     while ( tmp >= 180.0 ) {
107         tmp -= 360.0;
108     }
109     result.set_bucket( tmp, clat );
110
111     return result;
112 }
113
114
115 // calculate the offset between two buckets
116 void fgBucketDiff( const FGBucket& b1, const FGBucket& b2, int *dx, int *dy ) {
117
118     // Latitude difference
119     double c1_lat = b1.get_center_lat();
120     double c2_lat = b2.get_center_lat();
121     double diff_lat = c2_lat - c1_lat;
122
123 #ifdef HAVE_RINT
124     *dy = (int)rint( diff_lat / FG_BUCKET_SPAN );
125 #else
126     if ( diff_lat > 0 ) {
127         *dy = (int)( diff_lat / FG_BUCKET_SPAN + 0.5 );
128     } else {
129         *dy = (int)( diff_lat / FG_BUCKET_SPAN - 0.5 );
130     }
131 #endif
132
133     // longitude difference
134     double c1_lon = b1.get_center_lon();
135     double c2_lon = b2.get_center_lon();
136     double diff_lon = c2_lon - c1_lon;
137     double span;
138     if ( bucket_span(c1_lat) <= bucket_span(c2_lat) ) {
139         span = bucket_span(c1_lat);
140     } else {
141         span = bucket_span(c2_lat);
142     }
143
144 #ifdef HAVE_RINT
145     *dx = (int)rint( diff_lon / span );
146 #else
147     if ( diff_lon > 0 ) {
148         *dx = (int)( diff_lon / span + 0.5 );
149     } else {
150         *dx = (int)( diff_lon / span - 0.5 );
151     }
152 #endif
153 }
154
155