From 3f9cd0d7cb982428295cff25963f4ee2726d34a9 Mon Sep 17 00:00:00 2001 From: Egor Afanasenko <60288416+egor1abs@users.noreply.github.com> Date: Sat, 15 Jul 2023 00:20:30 +0300 Subject: [PATCH] Fix R < r torus intersection bug (#2589) Co-authored-by: Paul Romano --- src/surface.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/surface.cpp b/src/surface.cpp index 3050cf104e..01e2f275eb 100644 --- a/src/surface.cpp +++ b/src/surface.cpp @@ -972,7 +972,14 @@ double torus_distance(double x1, double x2, double x3, double u1, double u2, if (roots[i].imag() == 0) { double root = roots[i].real(); if (root > cutoff && root < distance) { - distance = root; + // Avoid roots corresponding to internal surfaces + double s1 = x1 + u1 * root; + double s2 = x2 + u2 * root; + double s3 = x3 + u3 * root; + double check = D * s3 * s3 + s1 * s1 + s2 * s2 + A * A - C * C; + if (check >= 0) { + distance = root; + } } } }