]> git.mxchange.org Git - flightgear.git/blob - Lib/Bucket/newbucket.cxx
Merge FG_Lib as subdirectory
[flightgear.git] / Lib / 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  * (Log is kept at end of this file)
24  **************************************************************************/
25
26
27 #ifdef HAVE_CONFIG_H
28 #  include "config.h"
29 #endif
30
31
32 #include "newbucket.hxx"
33
34
35 // Build the path name for this bucket
36 string FGBucket::gen_base_path() const {
37     // long int index;
38     int top_lon, top_lat, main_lon, main_lat;
39     char hem, pole;
40     char path[256];
41
42     // index = gen_index();
43
44     path[0] = '\0';
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(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     return path;
83 }
84
85
86 // find the bucket which is offset by the specified tile units in the
87 // X & Y direction.  We need the current lon and lat to resolve
88 // ambiguities when going from a wider tile to a narrower one above or
89 // below.  This assumes that we are feeding in
90 FGBucket fgBucketOffset( double dlon, double dlat, int dx, int dy ) {
91     FGBucket result( dlon, dlat );
92     double clat = result.get_center_lat() + dy * FG_BUCKET_SPAN;
93
94     // walk dy units in the lat direction
95     result.set_bucket( dlon, clat );
96
97     // find the lon span for the new latitude
98     double span = bucket_span( clat );
99
100     // walk dx units in the lon direction
101     result.set_bucket( dlon + dx * span, clat );
102
103     return result;
104 }
105
106
107 // calculate the offset between two buckets
108 void fgBucketDiff( const FGBucket& b1, const FGBucket& b2, int *dx, int *dy ) {
109
110     // Latitude difference
111     double c1_lat = b1.get_center_lat();
112     double c2_lat = b2.get_center_lat();
113     double diff_lat = c2_lat - c1_lat;
114
115 #ifdef HAVE_RINT
116     *dy = (int)rint( diff_lat / FG_BUCKET_SPAN );
117 #else
118     if ( diff_lat > 0 ) {
119         *dy = (int)( diff_lat / FG_BUCKET_SPAN + 0.5 );
120     } else {
121         *dy = (int)( diff_lat / FG_BUCKET_SPAN - 0.5 );
122     }
123 #endif
124
125     // longitude difference
126     double c1_lon = b1.get_center_lon();
127     double c2_lon = b2.get_center_lon();
128     double diff_lon = c2_lon - c1_lon;
129     double span;
130     if ( bucket_span(c1_lat) <= bucket_span(c2_lat) ) {
131         span = bucket_span(c1_lat);
132     } else {
133         span = bucket_span(c2_lat);
134     }
135
136 #ifdef HAVE_RINT
137     *dx = (int)rint( diff_lon / span );
138 #else
139     if ( diff_lon > 0 ) {
140         *dx = (int)( diff_lon / span + 0.5 );
141     } else {
142         *dx = (int)( diff_lon / span - 0.5 );
143     }
144 #endif
145 }
146
147
148 // $Log$
149 // Revision 1.4  1999/03/27 05:34:05  curt
150 // Elimitated some const warnings from the compiler.
151 //
152 // Revision 1.3  1999/02/26 22:07:54  curt
153 // Added initial support for native SGI compilers.
154 //
155 // Revision 1.2  1999/02/11 01:09:33  curt
156 // Added a routine to calculate the offset in bucket units between two buckets.
157 //
158 // Revision 1.1  1999/02/08 23:52:16  curt
159 // Added a new "newbucket.[ch]xx" FGBucket class to replace the old
160 // fgBUCKET struct and C routines.  This FGBucket class adjusts the tile
161 // width towards the poles to ensure the tiles are at least 8 miles wide.
162 //
163