]> git.mxchange.org Git - simgear.git/commitdiff
Add new sibling() helper to SGBucket
authorJames Turner <zakalawe@mac.com>
Wed, 12 Feb 2014 15:31:40 +0000 (15:31 +0000)
committerJames Turner <zakalawe@mac.com>
Wed, 12 Feb 2014 15:31:40 +0000 (15:31 +0000)
- will replace sgBucketOffset eventually
- expand test coverage for both sibling() and offset function

(one test case is disabled since it fails)

simgear/bucket/newbucket.cxx
simgear/bucket/newbucket.hxx
simgear/bucket/test_bucket.cxx

index 4094a441bf327973e55d797c726b2acbca9b758c..c8d2cd0227eba3e53b38b410b5de7a1365d628af 100644 (file)
@@ -254,6 +254,16 @@ double SGBucket::get_height_m() const {
     return SG_BUCKET_SPAN * degree_height;
 }
 
+SGBucket SGBucket::sibling(int dx, int dy) const
+{
+    double clat = get_center_lat() + dy * SG_BUCKET_SPAN;
+    // find the lon span for the new latitude
+    double span = sg_bucket_span( clat );
+    
+    double tmp = get_center_lon() + dx * span;
+    tmp = SGMiscd::normalizePeriodic(-180.0, 180.0, tmp);
+    return SGBucket(tmp, clat);
+}
 
 // find the bucket which is offset by the specified tile units in the
 // X & Y direction.  We need the current lon and lat to resolve
index 57ef480d2c6049dfaadfa13cb906eef2baa60ca9..96495aba1726159978d728d98f96b09820fd8f37 100644 (file)
@@ -280,6 +280,11 @@ public:
      */
     inline int get_y() const { return y; }
 
+    /**
+     * @return bucket offset from this by dx,dy
+     */
+    SGBucket sibling(int dx, int dy) const;
+    
     // friends
 
     friend std::ostream& operator<< ( std::ostream&, const SGBucket& );
index cccae2c7c49da66e0cf6cd994c2768c0e9ef701e..da10105848464050a8197309be05972b8051d4ad 100644 (file)
@@ -74,6 +74,7 @@ void testPolar()
 
 }
 
+// test the tiles just below the pole (between 86 & 89 degrees N/S)
 void testNearPolar()
 {
     SGBucket b1(1, 88.5);
@@ -93,6 +94,81 @@ void testNearPolar()
 
 void testOffset()
 {
+    // bucket just below the 22 degree cutoff, so the next tile north
+    // is twice the width
+    SGBucket b1(-59.8, 21.9);
+    COMPARE(b1.get_chunk_lat(), 21);
+    COMPARE(b1.get_chunk_lon(), -60);
+    COMPARE(b1.get_x(), 1);
+    COMPARE(b1.get_y(), 7);
+    
+    // offset vertically
+    SGBucket b2(b1.sibling(0, 1));
+    COMPARE(b2.get_chunk_lat(), 22);
+    COMPARE(b2.get_chunk_lon(), -60);
+    COMPARE(b2.get_x(), 0);
+    COMPARE(b2.get_y(), 0);
+
+    COMPARE(b2.gen_index(), sgBucketOffset(-59.8, 21.9, 0, 1));
+    
+    // offset vertically and horizontally. We compute horizontal (x)
+    // movement at the target latitude, so this should move 0.25 * -3 degrees,
+    // NOT 0.125 * -3 degrees.
+    SGBucket b3(b1.sibling(-3, 1));
+    COMPARE(b3.get_chunk_lat(), 22);
+    COMPARE(b3.get_chunk_lon(), -61);
+    COMPARE(b3.get_x(), 1);
+    COMPARE(b3.get_y(), 0);
+    
+    COMPARE(b3.gen_index(), sgBucketOffset(-59.8, 21.9, -3, 1));
+}
+
+void testPolarOffset()
+{
+    SGBucket b1(-11.7, -89.6);
+    COMPARE(b1.get_chunk_lat(), -90);
+    COMPARE(b1.get_chunk_lon(), -12);
+    COMPARE(b1.get_x(), 0);
+    COMPARE(b1.get_y(), 3);
+    
+    // offset horizontally
+    SGBucket b2(b1.sibling(-2, 0));
+    COMPARE(b2.get_chunk_lat(), -90);
+    COMPARE(b2.get_chunk_lon(), -36);
+    COMPARE(b2.get_x(), 0);
+    COMPARE(b2.get_y(), 3);
+    
+    COMPARE(b2.gen_index(), sgBucketOffset(-11.7, -89.6, -2, 0));
+    
+#if 0
+    // offset vertically towards the pole
+    SGBucket b3(b1.sibling(0, -5));
+    COMPARE(b3.get_chunk_lat(), -90);
+    COMPARE(b3.get_chunk_lon(), -12);
+    COMPARE(b3.get_x(), 0);
+    COMPARE(b3.get_y(), 0);
+    
+    COMPARE(b3.gen_index(), sgBucketOffset(-11.7, -89.6, 0, 0));
+#endif
+}
+
+// test behaviour of bucket-offset near the anti-meridian (180-meridian)
+void testOffsetWrap()
+{
+    // near the equator
+    SGBucket b1(-179.8, 16.8);
+    COMPARE(b1.get_chunk_lat(), 16);
+    COMPARE(b1.get_chunk_lon(), -180);
+    COMPARE(b1.get_x(), 1);
+    COMPARE(b1.get_y(), 6);
+    
+    SGBucket b2(b1.sibling(-2, 0));
+    COMPARE(b2.get_chunk_lat(), 16);
+    COMPARE(b2.get_chunk_lon(), 179);
+    COMPARE(b2.get_x(), 7);
+    COMPARE(b2.get_y(), 6);
+    COMPARE(b2.gen_index(), sgBucketOffset(-179.8, 16.8, -2, 0));
+    
     
 }
 
@@ -125,6 +201,8 @@ int main(int argc, char* argv[])
     testPolar();
     testNearPolar();
     testOffset();
+    testOffsetWrap();
+    testPolarOffset();
     
     cout << "all tests passed OK" << endl;
     return 0; // passed