From 82c3e2c1e27f05b9617a4da8e4806ace7d38a5e1 Mon Sep 17 00:00:00 2001
From: Daniel Karbach <daniel.karbach@localhorst.tv>
Date: Mon, 18 Dec 2017 10:07:05 +0100
Subject: [PATCH] catch ray/sphere intersect corner case

---
 tst/math/IntersectTest.cpp | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/tst/math/IntersectTest.cpp b/tst/math/IntersectTest.cpp
index cd50845..f428aaa 100644
--- a/tst/math/IntersectTest.cpp
+++ b/tst/math/IntersectTest.cpp
@@ -243,6 +243,35 @@ void IntersectTest::testRaySphereIntersection() {
 		glm::dvec3(1.0, 0.0, 0.0), normal
 	);
 
+	ray.Origin({ 0.5, 0.0, 0.0 }); // a tad to the right
+	CPPUNIT_ASSERT_MESSAGE(
+		"ray does not intersect sphere at origin",
+		Intersect(ray, sphere, normal, dist)
+	);
+	CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+		"distance along ray to unit sphere at origin is not 0.5",
+		0.5, dist, epsilon
+	);
+	AssertEqual(
+		"bad intersection normal",
+		glm::dvec3(1.0, 0.0, 0.0), normal
+	);
+
+	// corner case: ray origin exactly on sphere (should "intersect")
+	ray.Origin({ 1.0, 0.0, 0.0 });
+	CPPUNIT_ASSERT_MESSAGE(
+		"ray touching sphere does not intersect it",
+		Intersect(ray, sphere, normal, dist)
+	);
+	CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(
+		"distance along ray touching unit sphere is not 0.0",
+		0.0, dist, epsilon
+	);
+	AssertEqual(
+		"bad intersection normal",
+		glm::dvec3(1.0, 0.0, 0.0), normal
+	);
+
 	ray.Origin({ 2.0, 0.0, 0.0 }); // move outside
 	CPPUNIT_ASSERT_MESSAGE(
 		"ray pointing away from sphere intersects it for some reason",
-- 
2.39.5