00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00029 #ifndef _NEWBUCKET_HXX
00030 #define _NEWBUCKET_HXX
00031
00032 #include <simgear/compiler.h>
00033 #include <simgear/constants.h>
00034 #include <simgear/math/SGMath.hxx>
00035
00036 #include <cmath>
00037 #include <cstdio>
00038 #include <ostream>
00039 #include <string>
00040
00044 #define SG_BUCKET_SPAN 0.125
00045
00049 #define SG_HALF_BUCKET_SPAN ( 0.5 * SG_BUCKET_SPAN )
00050
00051
00052
00053 static double sg_bucket_span( double l ) {
00054 if ( l >= 89.0 ) {
00055 return 360.0;
00056 } else if ( l >= 88.0 ) {
00057 return 8.0;
00058 } else if ( l >= 86.0 ) {
00059 return 4.0;
00060 } else if ( l >= 83.0 ) {
00061 return 2.0;
00062 } else if ( l >= 76.0 ) {
00063 return 1.0;
00064 } else if ( l >= 62.0 ) {
00065 return 0.5;
00066 } else if ( l >= 22.0 ) {
00067 return 0.25;
00068 } else if ( l >= -22.0 ) {
00069 return 0.125;
00070 } else if ( l >= -62.0 ) {
00071 return 0.25;
00072 } else if ( l >= -76.0 ) {
00073 return 0.5;
00074 } else if ( l >= -83.0 ) {
00075 return 1.0;
00076 } else if ( l >= -86.0 ) {
00077 return 2.0;
00078 } else if ( l >= -88.0 ) {
00079 return 4.0;
00080 } else if ( l >= -89.0 ) {
00081 return 8.0;
00082 } else {
00083 return 360.0;
00084 }
00085 }
00086
00087
00095 class SGBucket {
00096
00097 private:
00098 short lon;
00099 short lat;
00100 char x;
00101 char y;
00102
00103 public:
00104
00108 SGBucket();
00109
00115 SGBucket(const double dlon, const double dlat);
00116
00122 SGBucket(const SGGeod& geod);
00123
00130 SGBucket(const bool is_good);
00131
00135 SGBucket(const long int bindex);
00136
00142 void set_bucket( double dlon, double dlat );
00143
00149 void set_bucket( double *lonlat );
00150
00156 void set_bucket(const SGGeod& geod);
00157
00164 inline void make_bad() {
00165 set_bucket(0.0, 0.0);
00166 lon = -1000;
00167 }
00168
00184 inline long int gen_index() const {
00185 return ((lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x;
00186 }
00187
00193 inline std::string gen_index_str() const {
00194 char tmp[20];
00195 std::sprintf(tmp, "%ld",
00196 (((long)lon + 180) << 14) + ((lat + 90) << 6)
00197 + (y << 3) + x);
00198 return (std::string)tmp;
00199 }
00200
00205 std::string gen_base_path() const;
00206
00210 inline double get_center_lon() const {
00211 double span = sg_bucket_span( lat + y / 8.0 + SG_HALF_BUCKET_SPAN );
00212
00213 if ( span >= 1.0 ) {
00214 return lon + span / 2.0;
00215 } else {
00216 return lon + x * span + span / 2.0;
00217 }
00218 }
00219
00223 inline double get_center_lat() const {
00224 return lat + y / 8.0 + SG_HALF_BUCKET_SPAN;
00225 }
00226
00230 double get_width() const;
00231
00235 double get_height() const;
00236
00240 double get_width_m() const;
00241
00245 double get_height_m() const;
00246
00250 SGGeod get_center() const
00251 { return SGGeod::fromDeg(get_center_lon(), get_center_lat()); }
00252
00256 SGGeod get_corner(unsigned num) const
00257 {
00258 double lonFac = ((num + 1) & 2) ? 0.5 : -0.5;
00259 double latFac = ((num ) & 2) ? 0.5 : -0.5;
00260 return SGGeod::fromDeg(get_center_lon() + lonFac*get_width(),
00261 get_center_lat() + latFac*get_height());
00262 }
00263
00264
00265
00270 inline int get_chunk_lon() const { return lon; }
00271
00276 inline int get_chunk_lat() const { return lat; }
00277
00281 inline int get_x() const { return x; }
00282
00286 inline int get_y() const { return y; }
00287
00288
00289
00290 friend std::ostream& operator<< ( std::ostream&, const SGBucket& );
00291 friend bool operator== ( const SGBucket&, const SGBucket& );
00292 };
00293
00294 inline bool operator!= (const SGBucket& lhs, const SGBucket& rhs)
00295 {
00296 return !(lhs == rhs);
00297 }
00298
00299
00310 SGBucket sgBucketOffset( double dlon, double dlat, int x, int y );
00311
00312
00321 void sgBucketDiff( const SGBucket& b1, const SGBucket& b2, int *dx, int *dy );
00322
00323
00329 inline std::ostream&
00330 operator<< ( std::ostream& out, const SGBucket& b )
00331 {
00332 return out << b.lon << ":" << b.x << ", " << b.lat << ":" << b.y;
00333 }
00334
00335
00342 inline bool
00343 operator== ( const SGBucket& b1, const SGBucket& b2 )
00344 {
00345 return ( b1.lon == b2.lon &&
00346 b1.lat == b2.lat &&
00347 b1.x == b2.x &&
00348 b1.y == b2.y );
00349 }
00350
00351
00352 #endif // _NEWBUCKET_HXX
00353
00354