]> git.localhorst.tv Git - blank.git/blob - tst/model/GeometryTest.cpp
71830c6176930f31421e31af7afb052dff937292
[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 #include <glm/gtx/transform.hpp>
8
9 CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::GeometryTest);
10
11
12 namespace blank {
13 namespace test {
14
15 void GeometryTest::setUp() {
16 }
17
18 void GeometryTest::tearDown() {
19 }
20
21
22 void GeometryTest::testRayBoxIntersection() {
23         Ray ray{ { 0, 0, 0 }, { 1, 0, 0 } }; // at origin, pointing right
24         AABB box{ { -1, -1, -1 }, { 1, 1, 1 } }; // 2x2x2 cube centered around origin
25         glm::mat4 M(1); // no transformation
26
27         const float delta = std::numeric_limits<float>::epsilon();
28
29         float distance = 0;
30         glm::vec3 normal(0);
31
32         CPPUNIT_ASSERT_MESSAGE(
33                 "ray at origin not intersecting box at origin",
34                 Intersection(ray, box, M, &distance)
35         );
36         CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
37                 "intersection distance way off",
38                 0.0f, distance, delta
39         );
40         // normal undefined, so can't test
41
42         // move ray outside the box, but have it still point at it
43         // should be 4 units to the left now
44         ray.orig.x = -5;
45         CPPUNIT_ASSERT_MESSAGE(
46                 "ray pointing at box doesn't intersect",
47                 Intersection(ray, box, M, &distance, &normal)
48         );
49         CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
50                 "intersection distance way off",
51                 4.0f, distance, delta
52         );
53         CPPUNIT_ASSERT_EQUAL_MESSAGE(
54                 "wrong surface normal at intersection point",
55                 glm::vec3(-1, 0, 0), normal
56         );
57
58         // move ray to the other side, so it's pointing away now
59         ray.orig.x = 5;
60         CPPUNIT_ASSERT_MESSAGE(
61                 "ray pointing away from box still intersects",
62                 !Intersection(ray, box, M)
63         );
64 }
65
66 void GeometryTest::testBoxBoxIntersection() {
67         AABB box{ { -1, -1, -1 }, { 1, 1, 1 } }; // 2x2x2 cube centered around origin
68         glm::mat4 Ma(1); // identity
69         glm::mat4 Mb(1); // identity
70         // they're identical, so should probably intersect ^^
71
72         CPPUNIT_ASSERT_MESSAGE(
73                 "identical OBBs don't intersect",
74                 Intersection(box, Ma, box, Mb)
75         );
76
77         Ma = glm::translate(glm::vec3(-2, 0, 0)); // 2 to the left
78         Mb = glm::translate(glm::vec3(2, 0, 0)); // 2 to the right
79         CPPUNIT_ASSERT_MESSAGE(
80                 "distant OBBs intersect (2 apart, no rotation)",
81                 !Intersection(box, Ma, box, Mb)
82         );
83
84         Ma = glm::rotate(PI_0p25, glm::vec3(0, 0, 1)); // rotated 45° around Z
85         Mb = glm::translate(glm::vec3(2.4, 0, 0)); // 2.4 to the right
86         // they should barely touch. intersect by about 0.01 if my head works
87         CPPUNIT_ASSERT_MESSAGE(
88                 "OBBs don't intersect (one rotated by 45°)",
89                 Intersection(box, Ma, box, Mb)
90         );
91
92         Mb = glm::translate(glm::vec3(3, 0, 0)); // 3 to the right
93         CPPUNIT_ASSERT_MESSAGE(
94                 "OBBs intersect (one rotated by 45°)",
95                 !Intersection(box, Ma, box, Mb)
96         );
97 }
98
99 }
100 }