]> git.localhorst.tv Git - tacos.git/blob - tst/world/FloorTest.cpp
169dba84529ec3c32c60a3ecf6e67aadc5ed86f6
[tacos.git] / tst / world / FloorTest.cpp
1 #include "FloorTest.hpp"
2
3 #include "../vector_assert.hpp"
4
5 #include <world/Floor.hpp>
6
7 #include <glm/gtx/io.hpp>
8
9 CPPUNIT_TEST_SUITE_REGISTRATION(tacos::test::FloorTest);
10
11 namespace tacos {
12 namespace test {
13
14 void FloorTest::setUp() {
15 }
16
17 void FloorTest::tearDown() {
18 }
19
20
21 void FloorTest::testIntersection() {
22         Floor floor(33, 33);
23         Ray ray;
24         glm::vec3 point;
25
26         // default floor has all heights at 0, so a ray within the grid at height 1 pointing
27         // straight down should intersect it
28         // this ray hits the triangle somewhere near the middle
29         ray.origin = glm::vec3(0.5f, 1.0f, 0.75f);
30         ray.direction = glm::vec3(0.0f, -1.0f, 0.0f);
31         CPPUNIT_ASSERT_MESSAGE(
32                 "ray pointing down at a triangle does not intersect floor when it should",
33                 floor.Intersection(ray, point)
34         );
35         AssertEqual(
36                 "unexpected intersection point",
37                 glm::vec3(0.5f, 0.0f, 0.75f), point
38         );
39         // this ray should hit on the low X edge of the triangle
40         ray.origin = glm::vec3(0.0f, 1.0f, 0.5f);
41         ray.direction = glm::vec3(0.0f, -1.0f, 0.0f);
42         CPPUNIT_ASSERT_MESSAGE(
43                 "ray pointing down at a low X triangle edge does not intersect floor when it should",
44                 floor.Intersection(ray, point)
45         );
46         AssertEqual(
47                 "unexpected intersection point",
48                 glm::vec3(0.0f, 0.0f, 0.5f), point
49         );
50         // this ray should hit on the low Z edge of the triangle
51         ray.origin = glm::vec3(0.5f, 1.0f, 0.0f);
52         ray.direction = glm::vec3(0.0f, -1.0f, 0.0f);
53         CPPUNIT_ASSERT_MESSAGE(
54                 "ray pointing down  at a low Z triangle edgedoes not intersect floor when it should",
55                 floor.Intersection(ray, point)
56         );
57         AssertEqual(
58                 "unexpected intersection point",
59                 glm::vec3(0.5f, 0.0f, 0.0f), point
60         );
61         // this ray should hit on the diagonal edge of the triangle
62         ray.origin = glm::vec3(0.5f, 1.0f, 0.5f);
63         ray.direction = glm::vec3(0.0f, -1.0f, 0.0f);
64         CPPUNIT_ASSERT_MESSAGE(
65                 "ray pointing down at a diagonal triangle edgedoes not intersect floor when it should",
66                 floor.Intersection(ray, point)
67         );
68         AssertEqual(
69                 "unexpected intersection point",
70                 glm::vec3(0.5f, 0.0f, 0.5f), point
71         );
72         // this ray points straight away from the floor, so should not intersect
73         ray.origin = glm::vec3(0.5f, 1.0f, 0.5f);
74         ray.direction = glm::vec3(0.0f, 1.0f, 0.0f);
75         CPPUNIT_ASSERT_MESSAGE(
76                 "ray pointing up intersects floor when it shouldn't",
77                 !floor.Intersection(ray, point)
78         );
79         // this ray start below the floor and points down, so stays "underground" the whole time
80         ray.origin = glm::vec3(0.5f, -1.0f, 0.5f);
81         ray.direction = glm::vec3(0.0f, -1.0f, 0.0f);
82         CPPUNIT_ASSERT_MESSAGE(
83                 "ray pointing down intersects floor when it shouldn't",
84                 !floor.Intersection(ray, point)
85         );
86         // this ray start below the floor and points up, that means it intersects, although from the unexpected side
87         ray.origin = glm::vec3(0.5f, -1.0f, 0.5f);
88         ray.direction = glm::vec3(0.0f, 1.0f, 0.0f);
89         CPPUNIT_ASSERT_MESSAGE(
90                 "ray below the floor pointing up does not intersect floor when it should",
91                 floor.Intersection(ray, point)
92         );
93         AssertEqual(
94                 "unexpected intersection point",
95                 glm::vec3(0.5f, 0.0f, 0.5f), point
96         );
97
98         ray.origin = glm::vec3(0.0f, 1.0f, 1.0f);
99         ray.direction = glm::normalize(glm::vec3(1.0f, -1.0f, 0.0f));
100         CPPUNIT_ASSERT_MESSAGE(
101                 "diagonal ray, starts 1 unit above the floor and points down and to the right does not intersect floor when it should",
102                 floor.Intersection(ray, point)
103         );
104         AssertEqual(
105                 "unexpected intersection point",
106                 glm::vec3(1.0f, 0.0f, 1.0f), point
107         );
108         // the same ray, but with origin outside the XZ grid
109         ray.origin = glm::vec3(-1.0f, 2.0f, 1.0f);
110         ray.direction = glm::normalize(glm::vec3(1.0f, -1.0f, 0.0f));
111         CPPUNIT_ASSERT_MESSAGE(
112                 "ray entering from the left does not intersect floor when it should",
113                 floor.Intersection(ray, point)
114         );
115         AssertEqual(
116                 "unexpected intersection point",
117                 glm::vec3(1.0f, 0.0f, 1.0f), point
118         );
119         ray.origin = glm::vec3(3.0f, 2.0f, 1.0f);
120         ray.direction = glm::normalize(glm::vec3(-1.0f, -1.0f, 0.0f));
121         CPPUNIT_ASSERT_MESSAGE(
122                 "ray entering from the right does not intersect floor when it should",
123                 floor.Intersection(ray, point)
124         );
125         AssertEqual(
126                 "unexpected intersection point",
127                 glm::vec3(1.0f, 0.0f, 1.0f), point
128         );
129         ray.origin = glm::vec3(1.0f, 1.0f, 1.0f);
130         ray.direction = glm::normalize(glm::vec3(1.0f, 0.0f, 1.0f));
131         CPPUNIT_ASSERT_MESSAGE(
132                 "ray parallel to the floor intersects floor when it shouldn't",
133                 !floor.Intersection(ray, point)
134         );
135
136         ray.origin = glm::vec3(19.993f, 49.947f, 106.518f);
137         ray.direction = glm::normalize(glm::vec3(-0.07f, -0.528f, -0.846f));
138         CPPUNIT_ASSERT_MESSAGE(
139                 "weird ray from interactive testing doesn't intersect :(",
140                 floor.Intersection(ray, point)
141         );
142         AssertEqual(
143                 "unexpected intersection point",
144                 glm::vec3(13.37123870f, 0.0f, 26.48928486f), point,
145                 0.00001f // using custom delta since values above are somewhat rounded/truncated
146         );
147         ray.origin = glm::vec3(19.995f, 49.952f, 106.515f);
148         ray.direction = glm::normalize(glm::vec3(-0.046f, -0.477f, -0.878f));
149         CPPUNIT_ASSERT_MESSAGE(
150                 "weird ray from interactive testing doesn't intersect :(",
151                 floor.Intersection(ray, point)
152         );
153         AssertEqual(
154                 "unexpected intersection point",
155                 glm::vec3(15.17783f, 0.0f, 14.5698f), point,
156                 0.00001f
157         );
158 }
159
160 }
161 }