1 #include "IntersectionTest.hpp"
3 #include "geometry/const.hpp"
4 #include "geometry/primitive.hpp"
7 #include <glm/gtx/io.hpp>
8 #include <glm/gtx/transform.hpp>
10 CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::IntersectionTest);
16 void IntersectionTest::setUp() {
19 void IntersectionTest::tearDown() {
23 void IntersectionTest::testSimpleRayBoxIntersection() {
24 Ray ray{ { 0, 0, 0 }, { 1, 0, 0 } }; // at origin, pointing right
26 AABB box{ { -1, -1, -1 }, { 1, 1, 1 } }; // 2x2x2 cube centered around origin
28 const float delta = std::numeric_limits<float>::epsilon();
32 CPPUNIT_ASSERT_MESSAGE(
33 "ray at origin not intersecting box at origin",
34 Intersection(ray, box, distance)
37 // move ray outside the box, but have it still point at it
38 // should be 4 units to the left now
40 CPPUNIT_ASSERT_MESSAGE(
41 "ray pointing at box doesn't intersect",
42 Intersection(ray, box, distance)
44 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
45 "intersection distance way off",
49 // move ray to the other side, so it's pointing away now
51 CPPUNIT_ASSERT_MESSAGE(
52 "ray pointing away from box still intersects",
53 !Intersection(ray, box, distance)
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 };
60 CPPUNIT_ASSERT_MESSAGE(
61 "ray pointing at box doesn't intersect",
62 Intersection(ray, box, distance)
64 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
65 "intersection distance way off",
66 5.65685424949238019520f, distance, delta
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
75 const float delta = std::numeric_limits<float>::epsilon();
80 CPPUNIT_ASSERT_MESSAGE(
81 "ray at origin not intersecting box at origin",
82 Intersection(ray, box, M, &distance)
84 // normal undefined, so can't test
86 // move ray outside the box, but have it still point at it
87 // should be 4 units to the left now
89 CPPUNIT_ASSERT_MESSAGE(
90 "ray pointing at box doesn't intersect",
91 Intersection(ray, box, M, &distance, &normal)
93 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
94 "intersection distance way off",
97 CPPUNIT_ASSERT_EQUAL_MESSAGE(
98 "wrong surface normal at intersection point",
99 glm::vec3(-1, 0, 0), normal
102 // move ray to the other side, so it's pointing away now
104 CPPUNIT_ASSERT_MESSAGE(
105 "ray pointing away from box still intersects",
106 !Intersection(ray, box, M)
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)
116 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
117 "intersection distance way off",
118 5.65685424949238019520f, distance, delta
120 CPPUNIT_ASSERT_EQUAL_MESSAGE(
121 "wrong surface normal at intersection point",
122 glm::vec3(-1, 0, 0), normal
126 void IntersectionTest::testBoxBoxIntersection() {
127 const float delta = std::numeric_limits<float>::epsilon();
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 ^^
136 CPPUNIT_ASSERT_MESSAGE(
137 "identical OBBs don't intersect",
138 Intersection(box, Ma, box, Mb, depth, normal)
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",
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)
153 // depth and normal undefined for non-intersecting objects
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)
162 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
163 "bad penetration depth (with rotation)",
164 0.01421356237309504880f, depth, delta
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()
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)