1 /**************************************************************************
2 * test_bucket.cxx -- unit-tests for SGBucket class
4 * Copyright (C) 2014 James Turner - <zakalawe@mac.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 **************************************************************************/
23 #include <simgear/compiler.h>
33 #include <simgear/bucket/newbucket.hxx>
34 #include <simgear/misc/test_macros.hxx>
36 void testBucketSpans()
38 COMPARE(sg_bucket_span(0.0), 0.125);
39 COMPARE(sg_bucket_span(-20), 0.125);
40 COMPARE(sg_bucket_span(-40), 0.25);
41 COMPARE(sg_bucket_span(89.9), 12.0);
42 COMPARE(sg_bucket_span(88.1), 4.0);
43 COMPARE(sg_bucket_span(-89.9), 12.0);
48 SGBucket b1(5.1, 55.05);
49 COMPARE(b1.get_chunk_lon(), 5);
50 COMPARE(b1.get_chunk_lat(), 55);
51 COMPARE(b1.get_x(), 0);
52 COMPARE(b1.get_y(), 0);
53 COMPARE(b1.gen_index(), 3040320);
54 COMPARE(b1.gen_base_path(), "e000n50/e005n55");
56 SGBucket b2(-10.1, -43.8);
57 COMPARE(b2.get_chunk_lon(), -11);
58 COMPARE(b2.get_chunk_lat(), -44);
59 COMPARE(b2.get_x(), 3);
60 COMPARE(b2.get_y(), 1); // latitude chunks numbered bottom to top, it seems
61 COMPARE(b2.gen_base_path(), "w020s50/w011s44");
63 SGBucket b3(123.48, 9.01);
64 COMPARE(b3.get_chunk_lon(), 123);
65 COMPARE(b3.get_chunk_lat(), 9);
66 COMPARE(b3.get_x(), 3);
67 COMPARE(b3.get_y(), 0);
68 COMPARE(b3.gen_base_path(), "e120n00/e123n09");
74 SGBucket b1(0.0, 89.92);
75 SGBucket b2(10.0, 89.96);
76 COMPARE(b1.get_chunk_lat(), 89);
77 COMPARE(b1.get_chunk_lon(), 0);
78 COMPARE(b1.get_x(), 0);
79 COMPARE(b1.get_y(), 7);
81 COMPARE(b1.gen_index(), b2.gen_index());
83 SGGeod actualNorthPole1 = b1.get_corner(2);
84 SGGeod actualNorthPole2 = b1.get_corner(3);
85 COMPARE_EP(actualNorthPole1.getLatitudeDeg(), 90.0);
86 COMPARE_EP(actualNorthPole1.getLongitudeDeg(), 12.0);
87 COMPARE_EP(actualNorthPole2.getLatitudeDeg(), 90.0);
88 COMPARE_EP(actualNorthPole2.getLongitudeDeg(), 0.0);
90 SGBucket b3(-2, 89.88);
91 SGBucket b4(-7, 89.88);
92 COMPARE(b3.gen_index(), b4.gen_index());
95 SGBucket b5(-170, -89.88);
96 SGBucket b6(-179, -89.88);
98 COMPARE(b5.get_chunk_lat(), -90);
99 COMPARE(b5.get_chunk_lon(), -180);
100 COMPARE(b5.get_x(), 0);
101 COMPARE(b5.get_y(), 0);
102 COMPARE(b5.gen_index(), b6.gen_index());
104 SGGeod actualSouthPole1 = b5.get_corner(0);
105 SGGeod actualSouthPole2 = b5.get_corner(1);
106 COMPARE_EP(actualSouthPole1.getLatitudeDeg(), -90.0);
107 COMPARE_EP(actualSouthPole1.getLongitudeDeg(), -180);
108 COMPARE_EP(actualSouthPole2.getLatitudeDeg(), -90.0);
109 COMPARE_EP(actualSouthPole2.getLongitudeDeg(), -168);
111 // no automatic wrapping of these values occurs
112 SGBucket b7(200, 89.88);
113 COMPARE(b7.get_chunk_lon(), 192);
117 // test the tiles just below the pole (between 86 & 89 degrees N/S)
120 SGBucket b1(1, 88.5);
121 SGBucket b2(-1, 88.8);
122 COMPARE(b1.get_chunk_lon(), 0);
123 COMPARE(b1.get_chunk_lat(), 88);
124 VERIFY(b1.gen_index() != b2.gen_index());
126 SGBucket b3(176.1, 88.5);
127 COMPARE(b3.get_chunk_lon(), 176);
129 SGBucket b4(-178, 88.5);
130 COMPARE(b4.get_chunk_lon(), -180);
135 // bucket just below the 22 degree cutoff, so the next tile north
136 // is twice the width
137 SGBucket b1(-59.8, 21.9);
138 COMPARE(b1.get_chunk_lat(), 21);
139 COMPARE(b1.get_chunk_lon(), -60);
140 COMPARE(b1.get_x(), 1);
141 COMPARE(b1.get_y(), 7);
144 SGBucket b2(b1.sibling(0, 1));
145 COMPARE(b2.get_chunk_lat(), 22);
146 COMPARE(b2.get_chunk_lon(), -60);
147 COMPARE(b2.get_x(), 0);
148 COMPARE(b2.get_y(), 0);
150 COMPARE(b2.gen_index(), sgBucketOffset(-59.8, 21.9, 0, 1));
152 // offset vertically and horizontally. We compute horizontal (x)
153 // movement at the target latitude, so this should move 0.25 * -3 degrees,
154 // NOT 0.125 * -3 degrees.
155 SGBucket b3(b1.sibling(-3, 1));
156 COMPARE(b3.get_chunk_lat(), 22);
157 COMPARE(b3.get_chunk_lon(), -61);
158 COMPARE(b3.get_x(), 1);
159 COMPARE(b3.get_y(), 0);
161 COMPARE(b3.gen_index(), sgBucketOffset(-59.8, 21.9, -3, 1));
164 void testPolarOffset()
166 SGBucket b1(-11.7, -89.6);
167 COMPARE(b1.get_chunk_lat(), -90);
168 COMPARE(b1.get_chunk_lon(), -12);
169 COMPARE(b1.get_x(), 0);
170 COMPARE(b1.get_y(), 3);
172 // offset horizontally
173 SGBucket b2(b1.sibling(-2, 0));
174 COMPARE(b2.get_chunk_lat(), -90);
175 COMPARE(b2.get_chunk_lon(), -36);
176 COMPARE(b2.get_x(), 0);
177 COMPARE(b2.get_y(), 3);
179 COMPARE(b2.gen_index(), sgBucketOffset(-11.7, -89.6, -2, 0));
182 SGBucket b3(-170, 89.1);
183 SGBucket b4(b3.sibling(-1, 0));
184 COMPARE(b4.get_chunk_lat(), 89);
185 COMPARE(b4.get_chunk_lon(), 168);
186 COMPARE(b4.get_x(), 0);
187 COMPARE(b4.get_y(), 0);
189 COMPARE(b4.gen_index(), sgBucketOffset(-170, 89.1, -1, 0));
192 SGBucket b5(177, 87.3);
193 SGBucket b6(b5.sibling(1, 1));
194 COMPARE(b6.get_chunk_lat(), 87);
195 COMPARE(b6.get_chunk_lon(), -180);
196 COMPARE(b6.get_x(), 0);
197 COMPARE(b6.get_y(), 3);
199 COMPARE(b6.gen_index(), sgBucketOffset(177, 87.3, 1, 1));
202 // offset vertically towards the pole
203 SGBucket b3(b1.sibling(0, -5));
204 COMPARE(b3.get_chunk_lat(), -90);
205 COMPARE(b3.get_chunk_lon(), -12);
206 COMPARE(b3.get_x(), 0);
207 COMPARE(b3.get_y(), 0);
209 COMPARE(b3.gen_index(), sgBucketOffset(-11.7, -89.6, 0, 0));
213 // test behaviour of bucket-offset near the anti-meridian (180-meridian)
214 void testOffsetWrap()
217 SGBucket b1(-179.8, 16.8);
218 COMPARE(b1.get_chunk_lat(), 16);
219 COMPARE(b1.get_chunk_lon(), -180);
220 COMPARE(b1.get_x(), 1);
221 COMPARE(b1.get_y(), 6);
223 SGBucket b2(b1.sibling(-2, 0));
224 COMPARE(b2.get_chunk_lat(), 16);
225 COMPARE(b2.get_chunk_lon(), 179);
226 COMPARE(b2.get_x(), 7);
227 COMPARE(b2.get_y(), 6);
228 COMPARE(b2.gen_index(), sgBucketOffset(-179.8, 16.8, -2, 0));
233 int main(int argc, char* argv[])
244 cout << "all tests passed OK" << endl;