From 288fed8dcc30049553ea28abf9331bf9cf7623f7 Mon Sep 17 00:00:00 2001
From: Julius Nehring-Wirxel <julius.nehring-wirxel@rwth-aachen.de>
Date: Fri, 15 Oct 2021 13:42:42 +0200
Subject: [PATCH] Add test for segment3 - sphere_boundary intersection

---
 extern/typed-geometry                         |  2 +-
 .../intersections/segment3-sphere-surface.cc  | 49 +++++++++++++++++++
 2 files changed, 50 insertions(+), 1 deletion(-)
 create mode 100644 tests/feature/intersections/segment3-sphere-surface.cc

diff --git a/extern/typed-geometry b/extern/typed-geometry
index 6dd433f..8881f13 160000
--- a/extern/typed-geometry
+++ b/extern/typed-geometry
@@ -1 +1 @@
-Subproject commit 6dd433f303977ebd6c92759041590d41288b25a1
+Subproject commit 8881f131192114d69f6239a2612accc63a70f82f
diff --git a/tests/feature/intersections/segment3-sphere-surface.cc b/tests/feature/intersections/segment3-sphere-surface.cc
new file mode 100644
index 0000000..ef6eddd
--- /dev/null
+++ b/tests/feature/intersections/segment3-sphere-surface.cc
@@ -0,0 +1,49 @@
+#include <test.hh>
+
+#include <typed-geometry/tg.hh>
+
+//#include <glow-extras/viewer/canvas.hh>
+
+TG_FUZZ_TEST(TypedGeometry, Segment3SphereSurface)
+{
+    auto const radius = tg::uniform(rng, 0.01f, 10.0f);
+    auto const center = tg::uniform(rng, tg::aabb3(tg::pos3(-10), tg::pos3(10)));
+    auto const sphere = tg::sphere3(center, radius * 0.9999f); // don't sample too close to the boundary
+    auto const sphere_boundary = tg::sphere_boundary<3, float>(center, radius);
+
+    auto const gen_point_inside = [&]() { return tg::uniform(rng, sphere); };
+    auto const gen_point_outside = [&]() {
+        auto const dir = tg::uniform<tg::dir3>(rng);
+        auto const scaling = tg::uniform(rng, 1.0001f, 4.0f);
+        return center + radius * scaling * dir;
+    };
+
+    // both inside -> no intersection
+    for (auto i = 0; i < 1000; ++i)
+    {
+        auto const p0 = gen_point_inside();
+        auto const p1 = gen_point_inside();
+        auto const intersection = tg::intersection(sphere_boundary, tg::segment3(p0, p1));
+
+        //        auto c = gv::canvas();
+        //        c.add_line(p0, p1);
+        //        c.add_sphere(center, radius, tg::color3::white);
+
+        CHECK(!intersection.any());
+    }
+
+    // one inside, one outside -> 1 intersection
+    for (auto i = 0; i < 1000; ++i)
+    {
+        auto const p0 = gen_point_inside();
+        auto const p1 = gen_point_outside();
+        auto const intersection = tg::intersection(sphere_boundary, tg::segment3(p0, p1));
+
+        //        auto c = gv::canvas();
+        //        c.add_line(p0, p1);
+        //        c.add_sphere(center, radius, tg::color3::white);
+        //        c.add_points(intersection.first(), tg::color3::red);
+
+        CHECK(intersection.size() == 1);
+    }
+}
-- 
GitLab