]> git.localhorst.tv Git - blank.git/blob - tst/geometry/IntersectionTest.cpp
faster ray/box test for AABBs
[blank.git] / tst / geometry / IntersectionTest.cpp
1 #include "IntersectionTest.hpp"
2
3 #include "geometry/const.hpp"
4 #include "geometry/primitive.hpp"
5
6 #include <limits>
7 #include <glm/gtx/io.hpp>
8 #include <glm/gtx/transform.hpp>
9
10 CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::IntersectionTest);
11
12
13 namespace blank {
14 namespace test {
15
16 void IntersectionTest::setUp() {
17 }
18
19 void IntersectionTest::tearDown() {
20 }
21
22
23 void IntersectionTest::testSimpleRayBoxIntersection() {
24         Ray ray{ { 0, 0, 0 }, { 1, 0, 0 } }; // at origin, pointing right
25         ray.Update();
26         AABB box{ { -1, -1, -1 }, { 1, 1, 1 } }; // 2x2x2 cube centered around origin
27
28         const float delta = std::numeric_limits<float>::epsilon();
29
30         float distance = 0;
31
32         CPPUNIT_ASSERT_MESSAGE(
33                 "ray at origin not intersecting box at origin",
34                 Intersection(ray, box, distance)
35         );
36
37         // move ray outside the box, but have it still point at it
38         // should be 4 units to the left now
39         ray.orig.x = -5;
40         CPPUNIT_ASSERT_MESSAGE(
41                 "ray pointing at box doesn't intersect",
42                 Intersection(ray, box, distance)
43         );
44         CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
45                 "intersection distance way off",
46                 4.0f, distance, delta
47         );
48
49         // move ray to the other side, so it's pointing away now
50         ray.orig.x = 5;
51         CPPUNIT_ASSERT_MESSAGE(
52                 "ray pointing away from box still intersects",
53                 !Intersection(ray, box, distance)
54         );
55
56         // 45 deg down from 4 units away, so should be about 4 * sqrt(2)
57         ray.orig = { -5.0f, 4.5f, 0.0f };
58         ray.dir = { 0.70710678118654752440f, -0.70710678118654752440f, 0.0f };
59         ray.Update();
60         CPPUNIT_ASSERT_MESSAGE(
61                 "ray pointing at box doesn't intersect",
62                 Intersection(ray, box, distance)
63         );
64         CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
65                 "intersection distance way off",
66                 5.65685424949238019520f, distance, delta
67         );
68 }
69
70 void IntersectionTest::testRayBoxIntersection() {
71         Ray ray{ { 0, 0, 0 }, { 1, 0, 0 } }; // at origin, pointing right
72         AABB box{ { -1, -1, -1 }, { 1, 1, 1 } }; // 2x2x2 cube centered around origin
73         glm::mat4 M(1); // no transformation
74
75         const float delta = std::numeric_limits<float>::epsilon();
76
77         float distance = 0;
78         glm::vec3 normal(0);
79
80         CPPUNIT_ASSERT_MESSAGE(
81                 "ray at origin not intersecting box at origin",
82                 Intersection(ray, box, M, &distance)
83         );
84         // normal undefined, so can't test
85
86         // move ray outside the box, but have it still point at it
87         // should be 4 units to the left now
88         ray.orig.x = -5;
89         CPPUNIT_ASSERT_MESSAGE(
90                 "ray pointing at box doesn't intersect",
91                 Intersection(ray, box, M, &distance, &normal)
92         );
93         CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
94                 "intersection distance way off",
95                 4.0f, distance, delta
96         );
97         CPPUNIT_ASSERT_EQUAL_MESSAGE(
98                 "wrong surface normal at intersection point",
99                 glm::vec3(-1, 0, 0), normal
100         );
101
102         // move ray to the other side, so it's pointing away now
103         ray.orig.x = 5;
104         CPPUNIT_ASSERT_MESSAGE(
105                 "ray pointing away from box still intersects",
106                 !Intersection(ray, box, M)
107         );
108
109         // 45 deg down from 4 units away, so should be about 4 * sqrt(2)
110         ray.orig = { -5.0f, 4.5f, 0.0f };
111         ray.dir = { 0.70710678118654752440f, -0.70710678118654752440f, 0.0f };
112         CPPUNIT_ASSERT_MESSAGE(
113                 "ray pointing at box doesn't intersect",
114                 Intersection(ray, box, M, &distance, &normal)
115         );
116         CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
117                 "intersection distance way off",
118                 5.65685424949238019520f, distance, delta
119         );
120         CPPUNIT_ASSERT_EQUAL_MESSAGE(
121                 "wrong surface normal at intersection point",
122                 glm::vec3(-1, 0, 0), normal
123         );
124 }
125
126 void IntersectionTest::testBoxBoxIntersection() {
127         const float delta = std::numeric_limits<float>::epsilon();
128         float depth = 0;
129         glm::vec3 normal(0);
130
131         AABB box{ { -1, -1, -1 }, { 1, 1, 1 } }; // 2x2x2 cube centered around origin
132         glm::mat4 Ma(1); // identity
133         glm::mat4 Mb(1); // identity
134         // they're identical, so should probably intersect ^^
135
136         CPPUNIT_ASSERT_MESSAGE(
137                 "identical OBBs don't intersect",
138                 Intersection(box, Ma, box, Mb, depth, normal)
139         );
140         // depth is two, but normal can be any
141         // (will probably be the first axis of box a, but any is valid)
142         CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
143                 "penetration depth of coincidental 2x2x2 boxes is not 2",
144                 2.0f, depth, delta
145         );
146
147         Ma = glm::translate(glm::vec3(-2, 0, 0)); // 2 to the left
148         Mb = glm::translate(glm::vec3(2, 0, 0)); // 2 to the right
149         CPPUNIT_ASSERT_MESSAGE(
150                 "distant OBBs intersect (2 apart, no rotation)",
151                 !Intersection(box, Ma, box, Mb, depth, normal)
152         );
153         // depth and normal undefined for non-intersecting objects
154
155         Ma = glm::rotate(PI_0p25, glm::vec3(0, 0, 1)); // rotated 45° around Z
156         Mb = glm::translate(glm::vec3(2.4, 0, 0)); // 2.4 to the right
157         // they should barely touch. intersect by about sqrt(2) - 1.4 if my head works
158         CPPUNIT_ASSERT_MESSAGE(
159                 "OBBs don't intersect (one rotated by 45°)",
160                 Intersection(box, Ma, box, Mb, depth, normal)
161         );
162         CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
163                 "bad penetration depth (with rotation)",
164                 0.01421356237309504880f, depth, delta
165         );
166         CPPUNIT_ASSERT_EQUAL_MESSAGE(
167                 "bad intersection normal (with rotation)",
168                 glm::vec3(1, 0, 0), abs(normal) // normal can be in + or - x, therefore abs()
169         );
170
171         Mb = glm::translate(glm::vec3(3, 0, 0)); // 3 to the right
172         CPPUNIT_ASSERT_MESSAGE(
173                 "OBBs intersect (one rotated by 45°)",
174                 !Intersection(box, Ma, box, Mb, depth, normal)
175         );
176 }
177
178 }
179 }