]> git.localhorst.tv Git - blank.git/blob - tst/geometry/LocationTest.cpp
make Intersection() repeatable for normal
[blank.git] / tst / geometry / LocationTest.cpp
1 #include "LocationTest.hpp"
2
3 #include <limits>
4 #include <glm/gtx/io.hpp>
5
6 CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::LocationTest);
7
8
9 namespace blank {
10 namespace test {
11
12 void LocationTest::setUp() {
13 }
14
15 void LocationTest::tearDown() {
16 }
17
18
19 void LocationTest::testSanitize() {
20         {
21                 RoughLocation loc({ 0, 0, 0 }, { 0, 0, 0 });
22                 loc.Sanitize();
23                 AssertEqual(
24                         "sanitize rough zero location",
25                         RoughLocation({ 0, 0, 0 }, { 0, 0, 0 }), loc
26                 );
27         }
28         {
29                 ExactLocation loc({ 0, 0, 0 }, { 0.0f, 0.0f, 0.0f });
30                 loc.Sanitize();
31                 AssertEqual(
32                         "sanitize exact zero location",
33                         ExactLocation({ 0, 0, 0 }, { 0.0f, 0.0f, 0.0f }), loc
34                 );
35         }
36         {
37                 ExactLocation loc({ 0, 0, 0 }, { 15.9f, 0.0f, 0.0f });
38                 loc.Sanitize();
39                 AssertEqual(
40                         "sanitize exact location near upper boundary",
41                         ExactLocation({ 0, 0, 0 }, { 15.9f, 0.0f, 0.0f }), loc
42                 );
43         }
44         {
45                 RoughLocation loc({ 0, 0, 0 }, { 0, 16, 0 });
46                 loc.Sanitize();
47                 AssertEqual(
48                         "sanitize rough location",
49                         RoughLocation({ 0, 1, 0 }, { 0, 0, 0 }), loc
50                 );
51         }
52         {
53                 ExactLocation loc({ 0, 0, 0 }, { 0.0f, 16.0f, 0.0f });
54                 loc.Sanitize();
55                 AssertEqual(
56                         "sanitize exact location",
57                         ExactLocation({ 0, 1, 0 }, { 0.0f, 0.0f, 0.0f }), loc
58                 );
59         }
60         {
61                 RoughLocation loc({ 0, 0, 0 }, { 0, 0, -1 });
62                 loc.Sanitize();
63                 AssertEqual(
64                         "sanitize rough negative location",
65                         RoughLocation({ 0, 0, -1 }, { 0, 0, 15 }), loc
66                 );
67         }
68         {
69                 ExactLocation loc({ 0, 0, 0 }, { 0.0f, 0.0f, -1.0f });
70                 loc.Sanitize();
71                 AssertEqual(
72                         "sanitize exact negative location",
73                         ExactLocation({ 0, 0, -1 }, { 0.0f, 0.0f, 15.0f }), loc
74                 );
75         }
76         {
77                 RoughLocation loc({ 0, 0, 0 }, { 0, 41585, 0 });
78                 loc.Sanitize();
79                 AssertEqual(
80                         "sanitize rough really far location",
81                         RoughLocation({ 0, 2599, 0 }, { 0, 1, 0 }), loc
82                 );
83         }
84         {
85                 ExactLocation loc({ 0, 0, 0 }, { 0.0f, 41585.0f, 0.0f });
86                 loc.Sanitize();
87                 AssertEqual(
88                         "sanitize exact really far location",
89                         ExactLocation({ 0, 2599, 0 }, { 0.0f, 1.0f, 0.0f }), loc
90                 );
91         }
92         {
93                 RoughLocation loc({ 0, 0, 0 }, { -208005, 0, 0 });
94                 loc.Sanitize();
95                 AssertEqual(
96                         "sanitize rough really far negative location",
97                         RoughLocation({ -13001, 0, 0 }, { 11, 0, 0 }), loc
98                 );
99         }
100         {
101                 ExactLocation loc({ 0, 0, 0 }, { -208005.0f, 0.0f, 0.0f });
102                 loc.Sanitize();
103                 AssertEqual(
104                         "sanitize exact really far negative location",
105                         ExactLocation({ -13001, 0, 0 }, { 11.0f, 0.0f, 0.0f }), loc
106                 );
107         }
108         {
109                 RoughLocation loc({ 0, -2, 0 }, { 0, 16, 0 });
110                 loc.Sanitize();
111                 AssertEqual(
112                         "sanitize rough location with non-zero chunk",
113                         RoughLocation({ 0, -1, 0 }, { 0, 0, 0 }), loc
114                 );
115         }
116         {
117                 ExactLocation loc({ 0, -2, 0 }, { 0.0f, 16.0f, 0.0f });
118                 loc.Sanitize();
119                 AssertEqual(
120                         "sanitize exact location with non-zero chunk",
121                         ExactLocation({ 0, -1, 0 }, { 0.0f, 0.0f, 0.0f }), loc
122                 );
123         }
124         {
125                 RoughLocation loc({ 0, 0, 5 }, { 0, 0, -33 });
126                 loc.Sanitize();
127                 AssertEqual(
128                         "sanitize rough negative location with non-zero chunk",
129                         RoughLocation({ 0, 0, 2 }, { 0, 0, 15 }), loc
130                 );
131         }
132         {
133                 ExactLocation loc({ 0, 0, 5 }, { 0.0f, 0.0f, -33.0f });
134                 loc.Sanitize();
135                 AssertEqual(
136                         "sanitize exact negative location with non-zero chunk",
137                         ExactLocation({ 0, 0, 2 }, { 0.0f, 0.0f, 15.0f }), loc
138                 );
139         }
140 }
141
142 void LocationTest::testAbsolute() {
143         {
144                 RoughLocation loc({ 0, 0, 0 }, { 0, 0, 0 });
145                 AssertEqual(
146                         "absolute of rough zero location",
147                         RoughLocation::Fine(0, 0, 0), loc.Absolute()
148                 );
149         }
150         {
151                 ExactLocation loc({ 0, 0, 0 }, { 0.0f, 0.0f, 0.0f });
152                 AssertEqual(
153                         "absolute of exact zero location",
154                         ExactLocation::Fine(0.0f, 0.0f, 0.0f), loc.Absolute()
155                 );
156         }
157         {
158                 RoughLocation loc({ 0, 2, 0 }, { 0, 5, 0 });
159                 AssertEqual(
160                         "absolute of rough location",
161                         RoughLocation::Fine(0, 37, 0), loc.Absolute()
162                 );
163         }
164         {
165                 ExactLocation loc({ 0, 2, 0 }, { 0.0f, 5.0f, 0.0f });
166                 AssertEqual(
167                         "absolute of exact location",
168                         ExactLocation::Fine(0.0f, 37.0f, 0.0f), loc.Absolute()
169                 );
170         }
171         {
172                 RoughLocation loc({ 0, 0, -2 }, { 0, 0, 5 });
173                 AssertEqual(
174                         "absolute of rough negative location",
175                         RoughLocation::Fine(0, 0, -27), loc.Absolute()
176                 );
177         }
178         {
179                 ExactLocation loc({ 0, 0, -2 }, { 0.0f, 0.0f, 5.0f });
180                 AssertEqual(
181                         "absolute of exact negative location",
182                         ExactLocation::Fine(0.0f, 0.0f, -27.0f), loc.Absolute()
183                 );
184         }
185 }
186
187 void LocationTest::testRelative() {
188         glm::ivec3 base(0, 0, 0);
189         {
190                 RoughLocation loc({ 0, 0, 0 }, { 0, 0, 0 });
191                 AssertEqual(
192                         "relative of rough zero location with zero base",
193                         RoughLocation::Fine(0, 0, 0), loc.Relative(base).Absolute()
194                 );
195         }
196         {
197                 ExactLocation loc({ 0, 0, 0 }, { 0.0f, 0.0f, 0.0f });
198                 AssertEqual(
199                         "relative of exact zero location with zero base",
200                         ExactLocation::Fine(0.0f, 0.0f, 0.0f), loc.Relative(base).Absolute()
201                 );
202         }
203         {
204                 RoughLocation loc({ 0, 2, 0 }, { 0, 5, 0 });
205                 AssertEqual(
206                         "relative of rough location with zero base",
207                         RoughLocation::Fine(0, 37, 0), loc.Relative(base).Absolute()
208                 );
209         }
210         {
211                 ExactLocation loc({ 0, 2, 0 }, { 0.0f, 5.0f, 0.0f });
212                 AssertEqual(
213                         "relative of exact location with zero base",
214                         ExactLocation::Fine(0.0f, 37.0f, 0.0f), loc.Relative(base).Absolute()
215                 );
216         }
217         {
218                 RoughLocation loc({ 0, 0, -2 }, { 0, 0, 5 });
219                 AssertEqual(
220                         "relative of rough negative location with zero base",
221                         RoughLocation::Fine(0, 0, -27), loc.Relative(base).Absolute()
222                 );
223         }
224         {
225                 ExactLocation loc({ 0, 0, -2 }, { 0.0f, 0.0f, 5.0f });
226                 AssertEqual(
227                         "relative of exact negative location with zero base",
228                         ExactLocation::Fine(0.0f, 0.0f, -27.0f), loc.Relative(base).Absolute()
229                 );
230         }
231
232         base = glm::ivec3(0, 1, 0);
233         {
234                 RoughLocation loc({ 0, 0, 0 }, { 0, 0, 0 });
235                 AssertEqual(
236                         "relative of rough zero location with positive base",
237                         RoughLocation::Fine(0, -16, 0), loc.Relative(base).Absolute()
238                 );
239         }
240         {
241                 ExactLocation loc({ 0, 0, 0 }, { 0.0f, 0.0f, 0.0f });
242                 AssertEqual(
243                         "relative of exact zero location with positive base",
244                         ExactLocation::Fine(0.0f, -16.0f, 0.0f), loc.Relative(base).Absolute()
245                 );
246         }
247         {
248                 RoughLocation loc({ 0, 2, 0 }, { 0, 5, 0 });
249                 AssertEqual(
250                         "relative of rough location with positive base",
251                         RoughLocation::Fine(0, 21, 0), loc.Relative(base).Absolute()
252                 );
253         }
254         {
255                 ExactLocation loc({ 0, 2, 0 }, { 0.0f, 5.0f, 0.0f });
256                 AssertEqual(
257                         "relative of exact location with positive base",
258                         ExactLocation::Fine(0.0f, 21.0f, 0.0f), loc.Relative(base).Absolute()
259                 );
260         }
261         {
262                 RoughLocation loc({ 0, 0, -2 }, { 0, 0, 5 });
263                 AssertEqual(
264                         "relative of rough negative location with positive base",
265                         RoughLocation::Fine(0, -16, -27), loc.Relative(base).Absolute()
266                 );
267         }
268         {
269                 ExactLocation loc({ 0, 0, -2 }, { 0.0f, 0.0f, 5.0f });
270                 AssertEqual(
271                         "relative of exact negative location with positive base",
272                         ExactLocation::Fine(0.0f, -16.0f, -27.0f), loc.Relative(base).Absolute()
273                 );
274         }
275
276         base = glm::ivec3(-2, 0, 0);
277         {
278                 RoughLocation loc({ 0, 0, 0 }, { 0, 0, 0 });
279                 AssertEqual(
280                         "relative of rough zero location with negative base",
281                         RoughLocation::Fine(32, 0, 0), loc.Relative(base).Absolute()
282                 );
283         }
284         {
285                 ExactLocation loc({ 0, 0, 0 }, { 0.0f, 0.0f, 0.0f });
286                 AssertEqual(
287                         "relative of exact zero location with negative base",
288                         ExactLocation::Fine(32.0f, 0.0f, 0.0f), loc.Relative(base).Absolute()
289                 );
290         }
291         {
292                 RoughLocation loc({ 0, 2, 0 }, { 0, 5, 0 });
293                 AssertEqual(
294                         "relative of rough location with negative base",
295                         RoughLocation::Fine(32, 37, 0), loc.Relative(base).Absolute()
296                 );
297         }
298         {
299                 ExactLocation loc({ 0, 2, 0 }, { 0.0f, 5.0f, 0.0f });
300                 AssertEqual(
301                         "relative of exact location with negative base",
302                         ExactLocation::Fine(32.0f, 37.0f, 0.0f), loc.Relative(base).Absolute()
303                 );
304         }
305         {
306                 RoughLocation loc({ 0, 0, -2 }, { 0, 0, 5 });
307                 AssertEqual(
308                         "relative of rough negative location with negative base",
309                         RoughLocation::Fine(32, 0, -27), loc.Relative(base).Absolute()
310                 );
311         }
312         {
313                 ExactLocation loc({ 0, 0, -2 }, { 0.0f, 0.0f, 5.0f });
314                 AssertEqual(
315                         "relative of exact negative location with negative base",
316                         ExactLocation::Fine(32.0f, 0.0f, -27.0f), loc.Relative(base).Absolute()
317                 );
318         }
319 }
320
321 void LocationTest::testDifference() {
322         {
323                 RoughLocation a({ 0, 0, 0 }, { 0, 0, 0 });
324                 RoughLocation b({ 0, 0, 0 }, { 0, 0, 0 });
325                 AssertEqual(
326                         "difference between rough zero locations",
327                         RoughLocation::Fine(0, 0, 0), a.Difference(b).Absolute()
328                 );
329         }
330         {
331                 ExactLocation a({ 0, 0, 0 }, { 0.0f, 0.0f, 0.0f });
332                 ExactLocation b({ 0, 0, 0 }, { 0.0f, 0.0f, 0.0f });
333                 AssertEqual(
334                         "difference between exact zero locations",
335                         ExactLocation::Fine(0.0f, 0.0f, 0.0f), a.Difference(b).Absolute()
336                 );
337         }
338         {
339                 RoughLocation a({ 0, 0, 0 }, { 5, 0, 0 });
340                 RoughLocation b({ 0, 0, 0 }, { 0, 0, 0 });
341                 AssertEqual(
342                         "difference between rough locations",
343                         RoughLocation::Fine(5, 0, 0), a.Difference(b).Absolute()
344                 );
345         }
346         {
347                 ExactLocation a({ 0, 0, 0 }, { 5.0f, 0.0f, 0.0f });
348                 ExactLocation b({ 0, 0, 0 }, { 0.0f, 0.0f, 0.0f });
349                 AssertEqual(
350                         "difference between exact locations",
351                         ExactLocation::Fine(5.0f, 0.0f, 0.0f), a.Difference(b).Absolute()
352                 );
353         }
354         {
355                 RoughLocation a({ 0, 0, 0 }, { 0, 0, 0 });
356                 RoughLocation b({ 0, 0, 0 }, { 0, 5, 0 });
357                 AssertEqual(
358                         "difference between rough locations",
359                         RoughLocation::Fine(0, -5, 0), a.Difference(b).Absolute()
360                 );
361         }
362         {
363                 ExactLocation a({ 0, 0, 0 }, { 0.0f, 0.0f, 0.0f });
364                 ExactLocation b({ 0, 0, 0 }, { 0.0f, 5.0f, 0.0f });
365                 AssertEqual(
366                         "difference between exact locations",
367                         ExactLocation::Fine(0.0f, -5.0f, 0.0f), a.Difference(b).Absolute()
368                 );
369         }
370         {
371                 RoughLocation a({ 0, 0, 0 }, { 0, 0, 0 });
372                 RoughLocation b({ 0, 0, 0 }, { 0, 0, -3 });
373                 AssertEqual(
374                         "difference between rough locations",
375                         RoughLocation::Fine(0, 0, 3), a.Difference(b).Absolute()
376                 );
377         }
378         {
379                 ExactLocation a({ 0, 0, 0 }, { 0.0f, 0.0f, 0.0f });
380                 ExactLocation b({ 0, 0, 0 }, { 0.0f, 0.0f, -3.0f });
381                 AssertEqual(
382                         "difference between exact locations",
383                         ExactLocation::Fine(0.0f, 0.0f, 3.0f), a.Difference(b).Absolute()
384                 );
385         }
386         {
387                 RoughLocation a({ 1, 0, -1 }, { 5, 14, 9 });
388                 RoughLocation b({ 0, 2, -1 }, { 3, 2, 0 });
389                 AssertEqual(
390                         "difference between rough locations",
391                         RoughLocation::Fine(18, -20, 9), a.Difference(b).Absolute()
392                 );
393         }
394         {
395                 ExactLocation a({ 1, 0, -1 }, { 5.0f, 14.0f, 9.0f });
396                 ExactLocation b({ 0, 2, -1 }, { 3.0f, 2.0f, 0.0f });
397                 AssertEqual(
398                         "difference between exact locations",
399                         ExactLocation::Fine(18.0f, -20.0f, 9.0f), a.Difference(b).Absolute()
400                 );
401         }
402 }
403
404
405 void LocationTest::AssertEqual(
406         const std::string &msg,
407         const glm::ivec3 &expected,
408         const glm::ivec3 &actual
409 ) {
410         CPPUNIT_ASSERT_EQUAL_MESSAGE(
411                 msg + " X",
412                 expected.x, actual.x
413         );
414         CPPUNIT_ASSERT_EQUAL_MESSAGE(
415                 msg + " Y",
416                 expected.y, actual.y
417         );
418         CPPUNIT_ASSERT_EQUAL_MESSAGE(
419                 msg + " Z",
420                 expected.z, actual.z
421         );
422 }
423
424 void LocationTest::AssertEqual(
425         const std::string &msg,
426         const glm::vec3 &expected,
427         const glm::vec3 &actual
428 ) {
429         CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
430                 msg + " X",
431                 expected.x, actual.x, std::numeric_limits<float>::epsilon()
432         );
433         CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
434                 msg + " Y",
435                 expected.y, actual.y, std::numeric_limits<float>::epsilon()
436         );
437         CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
438                 msg + " Z",
439                 expected.z, actual.z, std::numeric_limits<float>::epsilon()
440         );
441 }
442
443 void LocationTest::AssertEqual(
444         const std::string &msg,
445         const RoughLocation &expected,
446         const RoughLocation &actual
447 ) {
448         AssertEqual(
449                 msg + ": bad chunk",
450                 expected.chunk, actual.chunk
451         );
452         AssertEqual(
453                 msg + ": bad block",
454                 expected.block, actual.block
455         );
456 }
457
458 void LocationTest::AssertEqual(
459         const std::string &msg,
460         const ExactLocation &expected,
461         const ExactLocation &actual
462 ) {
463         AssertEqual(
464                 msg + ": bad chunk",
465                 expected.chunk, actual.chunk
466         );
467         AssertEqual(
468                 msg + ": bad block",
469                 expected.block, actual.block
470         );
471 }
472
473 }
474 }