]> git.localhorst.tv Git - tacos.git/blobdiff - tst/world/FloorTest.cpp
ray/floor intersection experiments
[tacos.git] / tst / world / FloorTest.cpp
diff --git a/tst/world/FloorTest.cpp b/tst/world/FloorTest.cpp
new file mode 100644 (file)
index 0000000..169dba8
--- /dev/null
@@ -0,0 +1,161 @@
+#include "FloorTest.hpp"
+
+#include "../vector_assert.hpp"
+
+#include <world/Floor.hpp>
+
+#include <glm/gtx/io.hpp>
+
+CPPUNIT_TEST_SUITE_REGISTRATION(tacos::test::FloorTest);
+
+namespace tacos {
+namespace test {
+
+void FloorTest::setUp() {
+}
+
+void FloorTest::tearDown() {
+}
+
+
+void FloorTest::testIntersection() {
+       Floor floor(33, 33);
+       Ray ray;
+       glm::vec3 point;
+
+       // default floor has all heights at 0, so a ray within the grid at height 1 pointing
+       // straight down should intersect it
+       // this ray hits the triangle somewhere near the middle
+       ray.origin = glm::vec3(0.5f, 1.0f, 0.75f);
+       ray.direction = glm::vec3(0.0f, -1.0f, 0.0f);
+       CPPUNIT_ASSERT_MESSAGE(
+               "ray pointing down at a triangle does not intersect floor when it should",
+               floor.Intersection(ray, point)
+       );
+       AssertEqual(
+               "unexpected intersection point",
+               glm::vec3(0.5f, 0.0f, 0.75f), point
+       );
+       // this ray should hit on the low X edge of the triangle
+       ray.origin = glm::vec3(0.0f, 1.0f, 0.5f);
+       ray.direction = glm::vec3(0.0f, -1.0f, 0.0f);
+       CPPUNIT_ASSERT_MESSAGE(
+               "ray pointing down at a low X triangle edge does not intersect floor when it should",
+               floor.Intersection(ray, point)
+       );
+       AssertEqual(
+               "unexpected intersection point",
+               glm::vec3(0.0f, 0.0f, 0.5f), point
+       );
+       // this ray should hit on the low Z edge of the triangle
+       ray.origin = glm::vec3(0.5f, 1.0f, 0.0f);
+       ray.direction = glm::vec3(0.0f, -1.0f, 0.0f);
+       CPPUNIT_ASSERT_MESSAGE(
+               "ray pointing down  at a low Z triangle edgedoes not intersect floor when it should",
+               floor.Intersection(ray, point)
+       );
+       AssertEqual(
+               "unexpected intersection point",
+               glm::vec3(0.5f, 0.0f, 0.0f), point
+       );
+       // this ray should hit on the diagonal edge of the triangle
+       ray.origin = glm::vec3(0.5f, 1.0f, 0.5f);
+       ray.direction = glm::vec3(0.0f, -1.0f, 0.0f);
+       CPPUNIT_ASSERT_MESSAGE(
+               "ray pointing down at a diagonal triangle edgedoes not intersect floor when it should",
+               floor.Intersection(ray, point)
+       );
+       AssertEqual(
+               "unexpected intersection point",
+               glm::vec3(0.5f, 0.0f, 0.5f), point
+       );
+       // this ray points straight away from the floor, so should not intersect
+       ray.origin = glm::vec3(0.5f, 1.0f, 0.5f);
+       ray.direction = glm::vec3(0.0f, 1.0f, 0.0f);
+       CPPUNIT_ASSERT_MESSAGE(
+               "ray pointing up intersects floor when it shouldn't",
+               !floor.Intersection(ray, point)
+       );
+       // this ray start below the floor and points down, so stays "underground" the whole time
+       ray.origin = glm::vec3(0.5f, -1.0f, 0.5f);
+       ray.direction = glm::vec3(0.0f, -1.0f, 0.0f);
+       CPPUNIT_ASSERT_MESSAGE(
+               "ray pointing down intersects floor when it shouldn't",
+               !floor.Intersection(ray, point)
+       );
+       // this ray start below the floor and points up, that means it intersects, although from the unexpected side
+       ray.origin = glm::vec3(0.5f, -1.0f, 0.5f);
+       ray.direction = glm::vec3(0.0f, 1.0f, 0.0f);
+       CPPUNIT_ASSERT_MESSAGE(
+               "ray below the floor pointing up does not intersect floor when it should",
+               floor.Intersection(ray, point)
+       );
+       AssertEqual(
+               "unexpected intersection point",
+               glm::vec3(0.5f, 0.0f, 0.5f), point
+       );
+
+       ray.origin = glm::vec3(0.0f, 1.0f, 1.0f);
+       ray.direction = glm::normalize(glm::vec3(1.0f, -1.0f, 0.0f));
+       CPPUNIT_ASSERT_MESSAGE(
+               "diagonal ray, starts 1 unit above the floor and points down and to the right does not intersect floor when it should",
+               floor.Intersection(ray, point)
+       );
+       AssertEqual(
+               "unexpected intersection point",
+               glm::vec3(1.0f, 0.0f, 1.0f), point
+       );
+       // the same ray, but with origin outside the XZ grid
+       ray.origin = glm::vec3(-1.0f, 2.0f, 1.0f);
+       ray.direction = glm::normalize(glm::vec3(1.0f, -1.0f, 0.0f));
+       CPPUNIT_ASSERT_MESSAGE(
+               "ray entering from the left does not intersect floor when it should",
+               floor.Intersection(ray, point)
+       );
+       AssertEqual(
+               "unexpected intersection point",
+               glm::vec3(1.0f, 0.0f, 1.0f), point
+       );
+       ray.origin = glm::vec3(3.0f, 2.0f, 1.0f);
+       ray.direction = glm::normalize(glm::vec3(-1.0f, -1.0f, 0.0f));
+       CPPUNIT_ASSERT_MESSAGE(
+               "ray entering from the right does not intersect floor when it should",
+               floor.Intersection(ray, point)
+       );
+       AssertEqual(
+               "unexpected intersection point",
+               glm::vec3(1.0f, 0.0f, 1.0f), point
+       );
+       ray.origin = glm::vec3(1.0f, 1.0f, 1.0f);
+       ray.direction = glm::normalize(glm::vec3(1.0f, 0.0f, 1.0f));
+       CPPUNIT_ASSERT_MESSAGE(
+               "ray parallel to the floor intersects floor when it shouldn't",
+               !floor.Intersection(ray, point)
+       );
+
+       ray.origin = glm::vec3(19.993f, 49.947f, 106.518f);
+       ray.direction = glm::normalize(glm::vec3(-0.07f, -0.528f, -0.846f));
+       CPPUNIT_ASSERT_MESSAGE(
+               "weird ray from interactive testing doesn't intersect :(",
+               floor.Intersection(ray, point)
+       );
+       AssertEqual(
+               "unexpected intersection point",
+               glm::vec3(13.37123870f, 0.0f, 26.48928486f), point,
+               0.00001f // using custom delta since values above are somewhat rounded/truncated
+       );
+       ray.origin = glm::vec3(19.995f, 49.952f, 106.515f);
+       ray.direction = glm::normalize(glm::vec3(-0.046f, -0.477f, -0.878f));
+       CPPUNIT_ASSERT_MESSAGE(
+               "weird ray from interactive testing doesn't intersect :(",
+               floor.Intersection(ray, point)
+       );
+       AssertEqual(
+               "unexpected intersection point",
+               glm::vec3(15.17783f, 0.0f, 14.5698f), point,
+               0.00001f
+       );
+}
+
+}
+}