]> git.mxchange.org Git - simgear.git/blobdiff - simgear/bucket/newbucket.cxx
Add operator!= to SGBucket
[simgear.git] / simgear / bucket / newbucket.cxx
index 658434f2cb32d9cde7982b24bf7a3521ca62d78a..9a4b20ebfe436cb1610880c293de8e62bb56ea0a 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Written by Curtis L. Olson, started February 1999.
  *
- * Copyright (C) 1999  Curtis L. Olson - curt@flightgear.org
+ * Copyright (C) 1999  Curtis L. Olson - http://www.flightgear.org/~curt/
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Library General Public License for more details.
  *
- * You should have received a copy of the GNU Library General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA  02111-1307, USA.
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  *
  * $Id$
  **************************************************************************/
 
 
 #ifdef HAVE_CONFIG_H
-#  include <config.h>
+#  include <simgear_config.h>
 #endif
 
-
 #include <math.h>
 
-#include <simgear/misc/fgpath.hxx>
+#include <simgear/misc/sg_path.hxx>
 
 #include "newbucket.hxx"
 
@@ -42,13 +40,16 @@ SGBucket::SGBucket() {
 
 
 // constructor for specified location
-inline SGBucket::SGBucket(const double dlon, const double dlat) {
+SGBucket::SGBucket(const double dlon, const double dlat) {
     set_bucket(dlon, dlat);
 }
 
+SGBucket::SGBucket(const SGGeod& geod) {
+    set_bucket(geod);
+}
 
 // create an impossible bucket if false
-inline SGBucket::SGBucket(const bool is_good) {
+SGBucket::SGBucket(const bool is_good) {
     set_bucket(0.0, 0.0);
     if ( !is_good ) {
        lon = -1000;
@@ -57,7 +58,7 @@ inline SGBucket::SGBucket(const bool is_good) {
 
 
 // Parse a unique scenery tile index and find the lon, lat, x, and y
-inline SGBucket::SGBucket(const long int bindex) {
+SGBucket::SGBucket(const long int bindex) {
     long int index = bindex;
        
     lon = index >> 14;
@@ -75,11 +76,6 @@ inline SGBucket::SGBucket(const long int bindex) {
 }
 
 
-// default destructor
-inline SGBucket::~SGBucket() {
-}
-
-
 // Set the bucket params for the specified lat and lon
 void SGBucket::set_bucket( double *lonlat ) {
     set_bucket( lonlat[0], lonlat[1] );
@@ -96,21 +92,21 @@ void SGBucket::set_bucket( double dlon, double dlat ) {
 
     // cout << "diff = " << diff << "  span = " << span << endl;
 
-    if ( (dlon >= 0) || (fabs(diff) < FG_EPSILON) ) {
+    if ( (dlon >= 0) || (fabs(diff) < SG_EPSILON) ) {
        lon = (int)dlon;
     } else {
        lon = (int)dlon - 1;
     }
 
     // find subdivision or super lon if needed
-    if ( span < FG_EPSILON ) {
+    if ( span < SG_EPSILON ) {
        // polar cap
        lon = 0;
        x = 0;
     } else if ( span <= 1.0 ) {
        x = (int)((dlon - lon) / span);
     } else {
-       if ( (dlon >= 0) || (fabs(diff) < FG_EPSILON) ) {
+       if ( (dlon >= 0) || (fabs(diff) < SG_EPSILON) ) {
            lon = (int)( (int)(lon / span) * span);
        } else {
            // cout << " lon = " << lon 
@@ -128,7 +124,7 @@ void SGBucket::set_bucket( double dlon, double dlat ) {
     //
     diff = dlat - (double)(int)dlat;
 
-    if ( (dlat >= 0) || (fabs(diff) < FG_EPSILON) ) {
+    if ( (dlat >= 0) || (fabs(diff) < SG_EPSILON) ) {
        lat = (int)dlat;
     } else {
        lat = (int)dlat - 1;
@@ -137,6 +133,11 @@ void SGBucket::set_bucket( double dlon, double dlat ) {
 }
 
 
+void SGBucket::set_bucket(const SGGeod& geod)
+{
+    set_bucket(geod.getLongitudeDeg(), geod.getLatitudeDeg());
+}
+
 // Build the path name for this bucket
 string SGBucket::gen_base_path() const {
     // long int index;
@@ -180,7 +181,7 @@ string SGBucket::gen_base_path() const {
            hem, top_lon, pole, top_lat, 
            hem, main_lon, pole, main_lat);
 
-    FGPath path( raw_path );
+    SGPath path( raw_path );
 
     return path.str();
 }
@@ -206,10 +207,10 @@ double SGBucket::get_width_m() const {
     } else {
        clat = (int)clat - 0.5;
     }
-    double clat_rad = clat * DEG_TO_RAD;
+    double clat_rad = clat * SGD_DEGREES_TO_RADIANS;
     double cos_lat = cos( clat_rad );
-    double local_radius = cos_lat * EQUATORIAL_RADIUS_M;
-    double local_perimeter = 2.0 * local_radius * FG_PI;
+    double local_radius = cos_lat * SG_EQUATORIAL_RADIUS_M;
+    double local_perimeter = local_radius * SGD_2PI;
     double degree_width = local_perimeter / 360.0;
 
     return sg_bucket_span( get_center_lat() ) * degree_width;
@@ -218,7 +219,7 @@ double SGBucket::get_width_m() const {
 
 // return height of the tile in meters
 double SGBucket::get_height_m() const {
-    double perimeter = 2.0 * EQUATORIAL_RADIUS_M * FG_PI;
+    double perimeter = SG_EQUATORIAL_RADIUS_M * SGD_2PI;
     double degree_height = perimeter / 360.0;
 
     return SG_BUCKET_SPAN * degree_height;
@@ -272,16 +273,32 @@ void sgBucketDiff( const SGBucket& b1, const SGBucket& b2, int *dx, int *dy ) {
 #endif
 
     // longitude difference
-    double c1_lon = b1.get_center_lon();
-    double c2_lon = b2.get_center_lon();
-    double diff_lon = c2_lon - c1_lon;
-    double span;
-    if ( sg_bucket_span(c1_lat) <= sg_bucket_span(c2_lat) ) {
+    double diff_lon=0.0;
+    double span=0.0;
+
+    SGBucket tmp_bucket;
+    // To handle crossing the bucket size boundary
+    //  we need to account for different size buckets.
+
+    if ( sg_bucket_span(c1_lat) <= sg_bucket_span(c2_lat) )
+    {
        span = sg_bucket_span(c1_lat);
     } else {
        span = sg_bucket_span(c2_lat);
     }
 
+    diff_lon = b2.get_center_lon() - b1.get_center_lon();
+
+    if (diff_lon <0.0)
+    {
+       diff_lon -= b1.get_width()*0.5 + b2.get_width()*0.5 - span;
+    } 
+    else
+    {
+       diff_lon += b1.get_width()*0.5 + b2.get_width()*0.5 - span;
+    }
+
+
 #ifdef HAVE_RINT
     *dx = (int)rint( diff_lon / span );
 #else