]> git.mxchange.org Git - flightgear.git/blobdiff - src/Scenery/tilemgr.cxx
Port over remaining Point3D usage to the more type and unit safe SG* classes.
[flightgear.git] / src / Scenery / tilemgr.cxx
index 26ce7135a5b1e7640c7e03e3e4521aad77334120..41093a26ceb87f87dd8650bbe983bccf0982fa8f 100644 (file)
@@ -32,8 +32,6 @@
 
 #include <simgear/constants.h>
 #include <simgear/debug/logstream.hxx>
-#include <simgear/math/polar3d.hxx>
-#include <simgear/math/sg_geodesy.hxx>
 #include <simgear/math/vector.hxx>
 #include <simgear/structure/exception.hxx>
 #include <simgear/scene/model/modellib.hxx>
@@ -315,30 +313,21 @@ void FGTileMgr::update_queues()
 // disk.
 int FGTileMgr::update( double visibility_meters )
 {
-    SGLocation *location = globals->get_current_view()->getSGLocation();
-    return update( location, visibility_meters );
+    SGVec3d viewPos = globals->get_current_view()->get_view_pos();
+    return update(SGGeod::fromCart(viewPos), visibility_meters);
 }
 
-
-int FGTileMgr::update( SGLocation *location, double visibility_meters )
+int FGTileMgr::update( const SGGeod& location, double visibility_meters)
 {
     SG_LOG( SG_TERRAIN, SG_DEBUG, "FGTileMgr::update()" );
 
-    longitude = location->getLongitude_deg();
-    latitude = location->getLatitude_deg();
-    // add 1.0m to the max altitude to give a little leeway to the
-    // ground reaction code.
-    altitude_m = location->getAltitudeASL_ft() * SG_FEET_TO_METER + 1.0;
+    longitude = location.getLongitudeDeg();
+    latitude = location.getLatitudeDeg();
 
-    // if current altitude is apparently not initialized, set max
-    // altitude to something big.
-    if ( altitude_m < -1000 ) {
-        altitude_m = 10000;
-    }
     // SG_LOG( SG_TERRAIN, SG_DEBUG, "FGTileMgr::update() for "
     //         << longitude << " " << latatitude );
 
-    current_bucket.set_bucket( longitude, latitude );
+    current_bucket.set_bucket( location );
     // SG_LOG( SG_TERRAIN, SG_DEBUG, "Updating tile list for "
     //         << current_bucket );
     fgSetInt( "/environment/current-tile-id", current_bucket.gen_index() );
@@ -391,21 +380,27 @@ void FGTileMgr::prep_ssg_nodes(float vis) {
     }
 }
 
-bool FGTileMgr::scenery_available(double lat, double lon, double range_m)
+bool FGTileMgr::scenery_available(const SGGeod& position, double range_m)
 {
   // sanity check (unfortunately needed!)
-  if ( lon <= -180.0 || lon >= 180.0 || lat <= -90.0 || lat >= 90.0 )
+  if (position.getLongitudeDeg() < -180 || position.getLongitudeDeg() > 180 ||
+      position.getLatitudeDeg() < -90 || position.getLatitudeDeg() > 90)
     return false;
   
-  SGBucket bucket(lon, lat);
+  SGBucket bucket(position);
   TileEntry *te = tile_cache.get_tile(bucket);
   if (!te || !te->is_loaded())
     return false;
 
+  SGVec3d cartPos = SGVec3d::fromGeod(position);
+
   // Traverse all tiles required to be there for the given visibility.
   // This uses exactly the same algorithm like the tile scheduler.
   double tile_width = bucket.get_width_m();
   double tile_height = bucket.get_height_m();
+  double tile_r = 0.5*sqrt(tile_width*tile_width + tile_height*tile_height);
+  double max_dist = tile_r + range_m;
+  double max_dist2 = max_dist*max_dist;
   
   int xrange = (int)fabs(range_m / tile_width) + 1;
   int yrange = (int)fabs(range_m / tile_height) + 1;
@@ -414,7 +409,11 @@ bool FGTileMgr::scenery_available(double lat, double lon, double range_m)
     for ( int y = -yrange; y <= yrange; ++y ) {
       // We have already checked for the center tile.
       if ( x != 0 || y != 0 ) {
-        SGBucket b = sgBucketOffset( lon, lat, x, y );
+        SGBucket b = sgBucketOffset( position.getLongitudeDeg(),
+                                     position.getLatitudeDeg(), x, y );
+        // Do not ask if it is just the next tile but way out of range.
+        if (max_dist2 < distSqr(cartPos, SGVec3d::fromGeod(b.get_center())))
+          continue;
         TileEntry *te = tile_cache.get_tile(b);
         if (!te || !te->is_loaded())
           return false;