From: James Turner Date: Wed, 12 Feb 2014 15:31:40 +0000 (+0000) Subject: Add new sibling() helper to SGBucket X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=9a09a6a447238f9d0449de02ea732355896e4c65;p=simgear.git Add new sibling() helper to SGBucket - will replace sgBucketOffset eventually - expand test coverage for both sibling() and offset function (one test case is disabled since it fails) --- diff --git a/simgear/bucket/newbucket.cxx b/simgear/bucket/newbucket.cxx index 4094a441..c8d2cd02 100644 --- a/simgear/bucket/newbucket.cxx +++ b/simgear/bucket/newbucket.cxx @@ -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 diff --git a/simgear/bucket/newbucket.hxx b/simgear/bucket/newbucket.hxx index 57ef480d..96495aba 100644 --- a/simgear/bucket/newbucket.hxx +++ b/simgear/bucket/newbucket.hxx @@ -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& ); diff --git a/simgear/bucket/test_bucket.cxx b/simgear/bucket/test_bucket.cxx index cccae2c7..da101058 100644 --- a/simgear/bucket/test_bucket.cxx +++ b/simgear/bucket/test_bucket.cxx @@ -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