]> git.localhorst.tv Git - blank.git/blob - tst/model/GeometryTest.cpp
some tests for Chunk
[blank.git] / tst / model / GeometryTest.cpp
1 #include "GeometryTest.hpp"
2
3 #include "model/geometry.hpp"
4
5 #include <limits>
6 #include <glm/gtx/io.hpp>
7
8 CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::GeometryTest);
9
10
11 namespace blank {
12 namespace test {
13
14 void GeometryTest::setUp() {
15 }
16
17 void GeometryTest::tearDown() {
18 }
19
20
21 void GeometryTest::testRayAABBIntersection() {
22         Ray ray{ { 0, 0, 0 }, { 1, 0, 0 } }; // at origin, pointing right
23         AABB box{ { -1, -1, -1 }, { 1, 1, 1 } }; // 2x2x2 cube centered around origin
24         glm::mat4 M(1); // no transformation
25
26         const float delta = std::numeric_limits<float>::epsilon();
27
28         float distance = 0;
29         glm::vec3 normal(0);
30
31         CPPUNIT_ASSERT_MESSAGE(
32                 "ray at origin not intersecting box at origin",
33                 Intersection(ray, box, M, &distance)
34         );
35         CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
36                 "intersection distance way off",
37                 0.0f, distance, delta
38         );
39         // normal undefined, so can't test
40
41         // move ray outside the box, but have it still point at it
42         // should be 4 units to the left now
43         ray.orig.x = -5;
44         CPPUNIT_ASSERT_MESSAGE(
45                 "ray pointing at box doesn't intersect",
46                 Intersection(ray, box, M, &distance, &normal)
47         );
48         CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
49                 "intersection distance way off",
50                 4.0f, distance, delta
51         );
52         CPPUNIT_ASSERT_EQUAL_MESSAGE(
53                 "wrong surface normal at intersection point",
54                 glm::vec3(-1, 0, 0), normal
55         );
56
57         // move ray to the other side, so it's pointing away now
58         ray.orig.x = 5;
59         CPPUNIT_ASSERT_MESSAGE(
60                 "ray pointing away from box still intersects",
61                 !Intersection(ray, box, M)
62         );
63 }
64
65 }
66 }