newbucket.hxx

Go to the documentation of this file.
00001 /**************************************************************************
00002  * newbucket.hxx -- new bucket routines for better world modeling
00003  *
00004  * Written by Curtis L. Olson, started February 1999.
00005  *
00006  * Copyright (C) 1999  Curtis L. Olson - http://www.flightgear.org/~curt
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Library General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2 of the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Library General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00021  *
00022  * $Id: newbucket_8hxx-source.html,v 1.15 2007-12-17 15:37:07 curt Exp $
00023  **************************************************************************/
00024 
00029 #ifndef _NEWBUCKET_HXX
00030 #define _NEWBUCKET_HXX
00031 
00032 #include <simgear/compiler.h>
00033 #include <simgear/constants.h>
00034 
00035 #ifdef SG_HAVE_STD_INCLUDES
00036 #  include <cmath>
00037 #  include <cstdio> // sprintf()
00038 #else
00039 #  include <math.h>
00040 #  include <stdio.h> // sprintf()
00041 #endif
00042 
00043 #include STL_IOSTREAM
00044 
00045 // I don't understand ... <math.h> or <cmath> should be included
00046 // already depending on how you defined SG_HAVE_STD_INCLUDES, but I
00047 // can go ahead and add this -- CLO
00048 #ifdef __MWERKS__
00049 SG_USING_STD(sprintf);
00050 SG_USING_STD(fabs);
00051 #endif
00052 
00053 #include STL_STRING
00054 
00055 SG_USING_STD(string);
00056 SG_USING_STD(ostream);
00057 
00058 
00062 #define SG_BUCKET_SPAN      0.125
00063 
00067 #define SG_HALF_BUCKET_SPAN ( 0.5 * SG_BUCKET_SPAN )
00068 
00069 
00070 // return the horizontal tile span factor based on latitude
00071 static double sg_bucket_span( double l ) {
00072     if ( l >= 89.0 ) {
00073         return 360.0;
00074     } else if ( l >= 88.0 ) {
00075         return 8.0;
00076     } else if ( l >= 86.0 ) {
00077         return 4.0;
00078     } else if ( l >= 83.0 ) {
00079         return 2.0;
00080     } else if ( l >= 76.0 ) {
00081         return 1.0;
00082     } else if ( l >= 62.0 ) {
00083         return 0.5;
00084     } else if ( l >= 22.0 ) {
00085         return 0.25;
00086     } else if ( l >= -22.0 ) {
00087         return 0.125;
00088     } else if ( l >= -62.0 ) {
00089         return 0.25;
00090     } else if ( l >= -76.0 ) {
00091         return 0.5;
00092     } else if ( l >= -83.0 ) {
00093         return 1.0;
00094     } else if ( l >= -86.0 ) {
00095         return 2.0;
00096     } else if ( l >= -88.0 ) {
00097         return 4.0;
00098     } else if ( l >= -89.0 ) {
00099         return 8.0;
00100     } else {
00101         return 360.0;
00102     }
00103 }
00104 
00105 
00113 class SGBucket {
00114 
00115 private:
00116     double cx, cy;  // centerpoint (lon, lat) in degrees of bucket
00117     short lon;        // longitude index (-180 to 179)
00118     short lat;        // latitude index (-90 to 89)
00119     char x;          // x subdivision (0 to 7)
00120     char y;          // y subdivision (0 to 7)
00121 
00122 public:
00123 
00127     SGBucket();
00128 
00134     SGBucket(const double dlon, const double dlat);
00135 
00142     SGBucket(const bool is_good);
00143 
00147     SGBucket(const long int bindex);
00148 
00152     ~SGBucket();
00153 
00159     void set_bucket( double dlon, double dlat );
00160 
00166     void set_bucket( double *lonlat );
00167 
00174     inline void make_bad() {
00175         set_bucket(0.0, 0.0);
00176         lon = -1000;
00177     }
00178 
00194     inline long int gen_index() const {
00195         return ((lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x;
00196     }
00197 
00203     inline string gen_index_str() const {
00204         char tmp[20];
00205         sprintf(tmp, "%ld", 
00206                 (((long)lon + 180) << 14) + ((lat + 90) << 6) + (y << 3) + x);
00207         return (string)tmp;
00208     }
00209 
00214     string gen_base_path() const;
00215 
00219     inline double get_center_lon() const {
00220         double span = sg_bucket_span( lat + y / 8.0 + SG_HALF_BUCKET_SPAN );
00221 
00222         if ( span >= 1.0 ) {
00223             return lon + span / 2.0;
00224         } else {
00225             return lon + x * span + span / 2.0;
00226         }
00227     }
00228 
00232     inline double get_center_lat() const {
00233         return lat + y / 8.0 + SG_HALF_BUCKET_SPAN;
00234     }
00235 
00239     double get_width() const;
00240 
00244     double get_height() const;
00245 
00249     double get_width_m() const; 
00250 
00254     double get_height_m() const;
00255  
00256     // Informational methods.
00257 
00262     inline int get_chunk_lon() const { return lon; }
00263 
00268     inline int get_chunk_lat() const { return lat; }
00269 
00273     inline int get_x() const { return x; }
00274 
00278     inline int get_y() const { return y; }
00279 
00280     // friends
00281 
00282     friend ostream& operator<< ( ostream&, const SGBucket& );
00283     friend bool operator== ( const SGBucket&, const SGBucket& );
00284 };
00285 
00286 
00297 SGBucket sgBucketOffset( double dlon, double dlat, int x, int y );
00298 
00299 
00308 void sgBucketDiff( const SGBucket& b1, const SGBucket& b2, int *dx, int *dy );
00309 
00310 
00316 inline ostream&
00317 operator<< ( ostream& out, const SGBucket& b )
00318 {
00319     return out << b.lon << ":" << b.x << ", " << b.lat << ":" << b.y;
00320 }
00321 
00322 
00329 inline bool
00330 operator== ( const SGBucket& b1, const SGBucket& b2 )
00331 {
00332     return ( b1.lon == b2.lon &&
00333              b1.lat == b2.lat &&
00334              b1.x == b2.x &&
00335              b1.y == b2.y );
00336 }
00337 
00338 
00339 #endif // _NEWBUCKET_HXX
00340 
00341 

Generated on Mon Dec 17 09:30:54 2007 for SimGear by  doxygen 1.5.1