// David Eberly, Geometric Tools, Redmond WA 98052 // Copyright (c) 1998-2021 // Distributed under the Boost Software License, Version 1.0. // https://www.boost.org/LICENSE_1_0.txt // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt // Version: 4.0.2021.02.10 #pragma once #include #include #include #include #include namespace gte { template class TIQuery, Sphere3> { public: struct Result { Result() : intersect(false) { }; bool intersect; }; Result operator()(Line3 const& line, Sphere3 const& sphere) { // The sphere is (X-C)^T*(X-C)-1 = 0 and the line is X = P+t*D. // Substitute the line equation into the sphere equation to // obtain a quadratic equation Q(t) = t^2 + 2*a1*t + a0 = 0, where // a1 = D^T*(P-C) and a0 = (P-C)^T*(P-C)-1. Real constexpr zero = 0; Result result{}; Vector3 diff = line.origin - sphere.center; Real a0 = Dot(diff, diff) - sphere.radius * sphere.radius; Real a1 = Dot(line.direction, diff); // Intersection occurs when Q(t) has real roots. Real discr = a1 * a1 - a0; result.intersect = (discr >= zero); return result; } }; template class FIQuery, Sphere3> { public: struct Result { Result() : intersect(false), numIntersections(0), parameter{}, point{} { Real constexpr rmax = std::numeric_limits::max(); parameter.fill(rmax); point.fill(Vector3{ rmax, rmax, rmax }); } bool intersect; int numIntersections; std::array parameter; std::array, 2> point; }; Result operator()(Line3 const& line, Sphere3 const& sphere) { Result result{}; DoQuery(line.origin, line.direction, sphere, result); for (int i = 0; i < result.numIntersections; ++i) { result.point[i] = line.origin + result.parameter[i] * line.direction; } return result; } protected: void DoQuery(Vector3 const& lineOrigin, Vector3 const& lineDirection, Sphere3 const& sphere, Result& result) { // The sphere is (X-C)^T*(X-C)-1 = 0 and the line is X = P+t*D. // Substitute the line equation into the sphere equation to // obtain a quadratic equation Q(t) = t^2 + 2*a1*t + a0 = 0, where // a1 = D^T*(P-C) and a0 = (P-C)^T*(P-C)-1. Real constexpr zero = 0; Vector3 diff = lineOrigin - sphere.center; Real a0 = Dot(diff, diff) - sphere.radius * sphere.radius; Real a1 = Dot(lineDirection, diff); // Intersection occurs when Q(t) has real roots. Real discr = a1 * a1 - a0; if (discr > zero) { // The line intersects the sphere in 2 distinct points. result.intersect = true; result.numIntersections = 2; Real root = std::sqrt(discr); result.parameter[0] = -a1 - root; result.parameter[1] = -a1 + root; } else if (discr < zero) { // The line does not intersect the sphere. The parameter[] // values are initialized to invalid numbers, but they should // not be used by the caller. Real constexpr rmax = std::numeric_limits::max(); result.intersect = false; result.numIntersections = 0; result.parameter[0] = +rmax; result.parameter[1] = -rmax; } else { // The line is tangent to the sphere, so the intersection is // a single point. The parameter[1] value is set, because // callers will access the degenerate interval [-a1,-a1]. result.intersect = true; result.numIntersections = 1; result.parameter[0] = -a1; result.parameter[1] = result.parameter[0]; } } }; }